forked from wormzjl/ModularMachinery
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathMachineEvent.java
More file actions
89 lines (76 loc) · 2.75 KB
/
MachineEvent.java
File metadata and controls
89 lines (76 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package github.kasuminova.mmce.common.event.machine;
import crafttweaker.annotations.ZenRegister;
import github.kasuminova.mmce.common.handler.UpgradeMachineEventHandler;
import github.kasuminova.mmce.common.helper.IMachineController;
import hellfirepvp.modularmachinery.ModularMachinery;
import hellfirepvp.modularmachinery.common.machine.DynamicMachine;
import hellfirepvp.modularmachinery.common.tiles.base.MachineComponentTileNotifiable;
import hellfirepvp.modularmachinery.common.tiles.base.TileMultiblockMachineController;
import net.minecraft.tileentity.TileEntity;
import net.minecraftforge.fml.common.eventhandler.Event;
import stanhebben.zenscript.annotations.ZenClass;
import stanhebben.zenscript.annotations.ZenGetter;
import stanhebben.zenscript.annotations.ZenSetter;
import java.util.List;
@ZenRegister
@ZenClass("mods.modularmachinery.MachineEvent")
public class MachineEvent extends Event {
protected final TileMultiblockMachineController controller;
public MachineEvent(TileMultiblockMachineController controller) {
this.controller = controller;
}
@ZenGetter("controller")
public IMachineController getIMachineController() {
return controller;
}
public TileMultiblockMachineController getController() {
return controller;
}
public void postEvent() {
try {
// ModularMachinery.EVENT_BUS.post(this);
postEventToComponents();
UpgradeMachineEventHandler.onMachineEvent(this);
if (!isCanceled()) {
postCrTEvent();
}
} catch (Exception e) {
ModularMachinery.log.warn("Caught an exception when post event!", e);
}
}
public void postEventToComponents() {
for (TileEntity tileEntity : controller.getFoundComponents().keySet()) {
if (tileEntity instanceof final MachineComponentTileNotifiable componentTile) {
componentTile.onMachineEvent(this);
if (isCanceled()) {
break;
}
}
}
}
public void postCrTEvent() {
DynamicMachine foundMachine = controller.getFoundMachine();
if (foundMachine == null) {
return;
}
List<IEventHandler<MachineEvent>> handlers = foundMachine.getMachineEventHandlers(getClass());
if (handlers == null) {
return;
}
for (IEventHandler<MachineEvent> handler : handlers) {
handler.invoke(this);
if (isCanceled()) {
break;
}
}
}
@Override
@ZenSetter("canceled")
public void setCanceled(boolean cancel) {
super.setCanceled(cancel);
}
@Override
public boolean isCancelable() {
return true;
}
}