Skip to content

Commit aaa314e

Browse files
committed
Events and components are added by default
so the extension can work by itself
1 parent da0de91 commit aaa314e

File tree

8 files changed

+51
-78
lines changed

8 files changed

+51
-78
lines changed

README.md

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -29,37 +29,33 @@ This may change in the future.
2929

3030
## Usage
3131

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

35-
The extension will not work by itself, you have to enable separate features.
36+
### Using the events
3637

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

39-
You can get an `EventNode` with all events listening by using `BasicRedstoneExtension.events()`.
40-
You can add this node as a child to any other node, and the redstone will work in the scope.
41-
You can also get a node with separated events of this extension.
42-
43-
`buttonEvents()` creates an EventNode with the events for button pressing and breaking.
44-
45-
`leverEvents()` creates an EventNode with the events for lever pulling and breaking.
46-
47-
`doorEvents()` creates an EventNode with the events for manual (no redstone) door interaction.
48-
49-
`trapdoorEvents()` creates an EventNode with the events for manual (no redstone) trapdoor interaction.
40+
This only applies when you use it as a library instead of an extension.
5041

5142
### Using redstone components
5243

53-
For every new instance created, you have to specify which redstone components to use in this instance.
44+
For every new instance, you can specify which redstone components to use in this instance.
5445

5546
For example:
5647
```java
5748
Instance instance;
49+
RedstoneComponent component;
5850
PowerNet powerNet = Redstone.getPowerNet(instance);
59-
powerNet.useBuiltinComponents();
51+
powerNet.useComponent(component);
6052
```
6153

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

6460
## Customization
6561

src/main/java/io/github/bloepiloepi/basicredstone/BasicRedstoneExtension.java

Lines changed: 7 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -10,66 +10,25 @@
1010
import net.minestom.server.extensions.Extension;
1111

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

14-
/**
15-
* Creates an EventNode with all the builtin redstone events listening.
16-
*
17-
* @return The EventNode
18-
*/
19-
public static EventNode<EntityEvent> events() {
15+
private static EventNode<EntityEvent> events() {
2016
EventNode<EntityEvent> node = EventNode.type("redstone-events", EventFilter.ENTITY);
2117

22-
node.addChild(buttonEvents());
23-
node.addChild(leverEvents());
24-
node.addChild(doorEvents());
25-
node.addChild(trapdoorEvents());
18+
node.addChild(Buttons.events());
19+
node.addChild(Lever.events());
20+
node.addChild(Doors.events());
21+
node.addChild(Trapdoors.events());
2622

2723
return node;
2824
}
2925

30-
/**
31-
* Creates an EventNode with the events for button pressing and breaking listening.
32-
*
33-
* @return The EventNode
34-
*/
35-
public static EventNode<EntityEvent> buttonEvents() {
36-
return Buttons.events();
37-
}
38-
39-
/**
40-
* Creates an EventNode with the events for lever pulling and breaking listening.
41-
*
42-
* @return The EventNode
43-
*/
44-
public static EventNode<EntityEvent> leverEvents() {
45-
return Lever.events();
46-
}
47-
48-
/**
49-
* Creates an EventNode with the events for manual (no redstone) door interaction listening.
50-
*
51-
* @return The EventNode
52-
*/
53-
public static EventNode<EntityEvent> doorEvents() {
54-
return Doors.events();
55-
}
56-
57-
/**
58-
* Creates an EventNode with the events for manual (no redstone) trapdoor interaction listening.
59-
*
60-
* @return The EventNode
61-
*/
62-
public static EventNode<EntityEvent> trapdoorEvents() {
63-
return Trapdoors.events();
64-
}
65-
6626
@Override
6727
public void initialize() {
68-
28+
getEventNode().addChild(REDSTONE_EVENTS);
6929
}
7030

7131
@Override
7232
public void terminate() {
73-
7433
}
7534
}

src/main/java/io/github/bloepiloepi/basicredstone/door/Doors.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,11 @@ public static void setOpen(Instance instance, Point position, boolean open, bool
108108
}
109109
}
110110

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

src/main/java/io/github/bloepiloepi/basicredstone/door/Trapdoors.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,11 @@ public static void setOpen(Instance instance, Point position, boolean open,
8888
}
8989
}
9090

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

src/main/java/io/github/bloepiloepi/basicredstone/redstone/PowerNet.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,20 @@ public class PowerNet {
2828

2929
public PowerNet(Instance instance) {
3030
this.instance = instance;
31+
useBuiltinComponents();
3132
}
3233

33-
public void useBuiltinComponents() {
34-
useComponents(Doors.DOOR_COMPONENT);
35-
useComponents(Trapdoors.TRAPDOOR_COMPONENT);
34+
private void useBuiltinComponents() {
35+
useComponent(Doors.DOOR_COMPONENT);
36+
useComponent(Trapdoors.TRAPDOOR_COMPONENT);
3637
}
3738

38-
public void useComponents(RedstoneComponent reactor) {
39-
components.add(reactor);
39+
public void useComponent(RedstoneComponent component) {
40+
components.add(component);
41+
}
42+
43+
public void removeComponent(RedstoneComponent component) {
44+
components.remove(component);
4045
}
4146

4247
private Set<RedstoneComponent> findComponents(Block block) {

src/main/java/io/github/bloepiloepi/basicredstone/redstone/sources/Buttons.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,11 @@ public static void powerAdjacent(Instance instance, Point position, Block block,
124124
powerNet.setPower(position.relative(StateUtil.getConnectedBlockFace(block).getOppositeFace()), power);
125125
}
126126

127+
/**
128+
* Creates an EventNode with the events for button pressing and breaking listening.
129+
*
130+
* @return The EventNode
131+
*/
127132
public static EventNode<EntityEvent> events() {
128133
EventNode<EntityEvent> node = EventNode.type("button-events", EventFilter.ENTITY);
129134

src/main/java/io/github/bloepiloepi/basicredstone/redstone/sources/Lever.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ public static void pull(Instance instance, Point position, Block block, @Nullabl
3434
Sound.Source.BLOCK, 0.3F, power ? 0.6F : 0.5F));
3535
}
3636

37+
/**
38+
* Creates an EventNode with the events for lever pulling and breaking listening.
39+
*
40+
* @return The EventNode
41+
*/
3742
public static EventNode<EntityEvent> events() {
3843
EventNode<EntityEvent> node = EventNode.type("lever-events", EventFilter.ENTITY);
3944

src/test/java/io/github/bloepiloepi/basicredstone/test/RedstoneTest.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,6 @@ public static void main(String[] args) {
3030

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

33-
GlobalEventHandler eventHandler = MinecraftServer.getGlobalEventHandler();
34-
35-
eventHandler.addChild(BasicRedstoneExtension.events());
36-
37-
PowerNet powerNet = Redstone.getPowerNet(instance);
38-
powerNet.useBuiltinComponents();
39-
4033
OpenToLAN.open();
4134

4235
server.start("localhost", 25565);

0 commit comments

Comments
 (0)