-
Notifications
You must be signed in to change notification settings - Fork 497
/
GateExpansionTimer.java
95 lines (78 loc) · 2.63 KB
/
GateExpansionTimer.java
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
90
91
92
93
94
95
/**
* Copyright (c) 2011-2015, SpaceToad and the BuildCraft Team
* http://www.mod-buildcraft.com
*
* BuildCraft is distributed under the terms of the Minecraft Mod Public
* License 1.0, or MMPL. Please check the contents of the license located in
* http://www.mod-buildcraft.com/MMPL-1.0.txt
*/
package buildcraft.transport.gates;
import java.util.List;
import net.minecraft.tileentity.TileEntity;
import buildcraft.BuildCraftTransport;
import buildcraft.api.gates.GateExpansionController;
import buildcraft.api.gates.IGate;
import buildcraft.api.gates.IGateExpansion;
import buildcraft.api.statements.IStatement;
import buildcraft.api.statements.IStatementParameter;
import buildcraft.api.statements.ITriggerInternal;
import buildcraft.transport.statements.TriggerClockTimer;
import buildcraft.transport.statements.TriggerClockTimer.Time;
public final class GateExpansionTimer extends GateExpansionBuildcraft implements IGateExpansion {
public static GateExpansionTimer INSTANCE = new GateExpansionTimer();
private GateExpansionTimer() {
super("timer");
}
@Override
public GateExpansionController makeController(TileEntity pipeTile) {
return new GateExpansionControllerTimer(pipeTile);
}
private class GateExpansionControllerTimer extends GateExpansionController {
private class Timer {
private static final int ACTIVE_TIME = 5;
private final TriggerClockTimer.Time time;
private int clock;
public Timer(TriggerClockTimer.Time time) {
this.time = time;
}
public void tick() {
if (clock > -ACTIVE_TIME) {
clock--;
} else {
clock = time.delay * 20 + ACTIVE_TIME;
}
}
public boolean isActive() {
return clock < 0;
}
}
private final Timer[] timers = new Timer[TriggerClockTimer.Time.VALUES.length];
public GateExpansionControllerTimer(TileEntity pipeTile) {
super(GateExpansionTimer.this, pipeTile);
for (TriggerClockTimer.Time time : TriggerClockTimer.Time.VALUES) {
timers[time.ordinal()] = new Timer(time);
}
}
@Override
public boolean isTriggerActive(IStatement trigger, IStatementParameter[] parameters) {
if (trigger instanceof TriggerClockTimer) {
TriggerClockTimer timerTrigger = (TriggerClockTimer) trigger;
return timers[timerTrigger.time.ordinal()].isActive();
}
return super.isTriggerActive(trigger, parameters);
}
@Override
public void addTriggers(List<ITriggerInternal> list) {
super.addTriggers(list);
for (Time time : TriggerClockTimer.Time.VALUES) {
list.add(BuildCraftTransport.triggerTimer[time.ordinal()]);
}
}
@Override
public void tick(IGate gate) {
for (Timer timer : timers) {
timer.tick();
}
}
}
}