Skip to content

Commit

Permalink
Events and components are added by default
Browse files Browse the repository at this point in the history
so the extension can work by itself
  • Loading branch information
TogAr2 committed Aug 9, 2021
1 parent da0de91 commit aaa314e
Show file tree
Hide file tree
Showing 8 changed files with 51 additions and 78 deletions.
32 changes: 14 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,37 +29,33 @@ This may change in the future.

## Usage

To use this extension, you can either build a jar file
and put it in your extensions folder, or shade it as a library in your server.
To use this extension, you can either use the jar file
and put it in your extensions folder, or shade it as a library in your server
(in which case you have to add the events, see below).

The extension will not work by itself, you have to enable separate features.
### Using the events

### Enabling events
To use the events, add `BasicRedstoneExtension.REDSTONE_EVENTS` as a child to any `EventNode` you want.

You can get an `EventNode` with all events listening by using `BasicRedstoneExtension.events()`.
You can add this node as a child to any other node, and the redstone will work in the scope.
You can also get a node with separated events of this extension.

`buttonEvents()` creates an EventNode with the events for button pressing and breaking.

`leverEvents()` creates an EventNode with the events for lever pulling and breaking.

`doorEvents()` creates an EventNode with the events for manual (no redstone) door interaction.

`trapdoorEvents()` creates an EventNode with the events for manual (no redstone) trapdoor interaction.
This only applies when you use it as a library instead of an extension.

### Using redstone components

For every new instance created, you have to specify which redstone components to use in this instance.
For every new instance, you can specify which redstone components to use in this instance.

For example:
```java
Instance instance;
RedstoneComponent component;
PowerNet powerNet = Redstone.getPowerNet(instance);
powerNet.useBuiltinComponents();
powerNet.useComponent(component);
```

`useBuiltinComponents()` will add doors and trapdoors to the instance.
Doors and trapdoors are added by default, but if you don't want them, they can be removed like this:
```java
powerNet.removeComponent(Doors.DOOR_COMPONENT);
powerNet.removeComponent(Trapdoors.TRAPDOOR_COMPONENT);
```

## Customization

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,66 +10,25 @@
import net.minestom.server.extensions.Extension;

public class BasicRedstoneExtension extends Extension {
public static final EventNode<EntityEvent> REDSTONE_EVENTS = events();

/**
* Creates an EventNode with all the builtin redstone events listening.
*
* @return The EventNode
*/
public static EventNode<EntityEvent> events() {
private static EventNode<EntityEvent> events() {
EventNode<EntityEvent> node = EventNode.type("redstone-events", EventFilter.ENTITY);

node.addChild(buttonEvents());
node.addChild(leverEvents());
node.addChild(doorEvents());
node.addChild(trapdoorEvents());
node.addChild(Buttons.events());
node.addChild(Lever.events());
node.addChild(Doors.events());
node.addChild(Trapdoors.events());

return node;
}

/**
* Creates an EventNode with the events for button pressing and breaking listening.
*
* @return The EventNode
*/
public static EventNode<EntityEvent> buttonEvents() {
return Buttons.events();
}

/**
* Creates an EventNode with the events for lever pulling and breaking listening.
*
* @return The EventNode
*/
public static EventNode<EntityEvent> leverEvents() {
return Lever.events();
}

/**
* Creates an EventNode with the events for manual (no redstone) door interaction listening.
*
* @return The EventNode
*/
public static EventNode<EntityEvent> doorEvents() {
return Doors.events();
}

/**
* Creates an EventNode with the events for manual (no redstone) trapdoor interaction listening.
*
* @return The EventNode
*/
public static EventNode<EntityEvent> trapdoorEvents() {
return Trapdoors.events();
}

@Override
public void initialize() {

getEventNode().addChild(REDSTONE_EVENTS);
}

@Override
public void terminate() {

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ public static void setOpen(Instance instance, Point position, boolean open, bool
}
}

/**
* Creates an EventNode with the events for manual (no redstone) door interaction listening.
*
* @return The EventNode
*/
public static EventNode<EntityEvent> events() {
EventNode<EntityEvent> node = EventNode.type("door-events", EventFilter.ENTITY);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ public static void setOpen(Instance instance, Point position, boolean open,
}
}

/**
* Creates an EventNode with the events for manual (no redstone) trapdoor interaction listening.
*
* @return The EventNode
*/
public static EventNode<EntityEvent> events() {
EventNode<EntityEvent> node = EventNode.type("trapdoor-events", EventFilter.ENTITY);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,20 @@ public class PowerNet {

public PowerNet(Instance instance) {
this.instance = instance;
useBuiltinComponents();
}

public void useBuiltinComponents() {
useComponents(Doors.DOOR_COMPONENT);
useComponents(Trapdoors.TRAPDOOR_COMPONENT);
private void useBuiltinComponents() {
useComponent(Doors.DOOR_COMPONENT);
useComponent(Trapdoors.TRAPDOOR_COMPONENT);
}

public void useComponents(RedstoneComponent reactor) {
components.add(reactor);
public void useComponent(RedstoneComponent component) {
components.add(component);
}

public void removeComponent(RedstoneComponent component) {
components.remove(component);
}

private Set<RedstoneComponent> findComponents(Block block) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ public static void powerAdjacent(Instance instance, Point position, Block block,
powerNet.setPower(position.relative(StateUtil.getConnectedBlockFace(block).getOppositeFace()), power);
}

/**
* Creates an EventNode with the events for button pressing and breaking listening.
*
* @return The EventNode
*/
public static EventNode<EntityEvent> events() {
EventNode<EntityEvent> node = EventNode.type("button-events", EventFilter.ENTITY);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ public static void pull(Instance instance, Point position, Block block, @Nullabl
Sound.Source.BLOCK, 0.3F, power ? 0.6F : 0.5F));
}

/**
* Creates an EventNode with the events for lever pulling and breaking listening.
*
* @return The EventNode
*/
public static EventNode<EntityEvent> events() {
EventNode<EntityEvent> node = EventNode.type("lever-events", EventFilter.ENTITY);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,6 @@ public static void main(String[] args) {

MinecraftServer.getCommandManager().register(new NetCommand());

GlobalEventHandler eventHandler = MinecraftServer.getGlobalEventHandler();

eventHandler.addChild(BasicRedstoneExtension.events());

PowerNet powerNet = Redstone.getPowerNet(instance);
powerNet.useBuiltinComponents();

OpenToLAN.open();

server.start("localhost", 25565);
Expand Down

0 comments on commit aaa314e

Please sign in to comment.