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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
|
package gregtech.api.multitileentity.base;
import static gregtech.GT_Mod.GT_FML_LOGGER;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import net.minecraft.block.Block;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import gregtech.api.enums.GT_Values;
import gregtech.api.multitileentity.interfaces.IMultiTileEntity.IMTE_OnNeighborBlockChange;
import gregtech.api.task.TaskHost;
import gregtech.api.task.TickableTask;
import gregtech.api.util.GT_Log;
import gregtech.api.util.GT_Util;
public abstract class TickableMultiTileEntity extends MultiTileEntity implements TaskHost, IMTE_OnNeighborBlockChange {
/** Variable for seeing if the Tick Function is called right now. */
public boolean isRunningTick = false;
/** Gets set to true when the Block received a Block Update. */
public boolean blockUpdated = false;
/** Timer Value */
protected long timer = 0;
/** Variable for updating Data to the Client */
private boolean sendClientData = false;
private final Map<String, TickableTask<?>> tasks = new HashMap<>();
public TickableMultiTileEntity() {
super(true);
}
@Override
public final void registerTask(@Nonnull TickableTask<?> task) {
if (tasks.containsKey(task.getName())) {
throw new IllegalStateException(String.format("Task with name %s is already registered", task.getName()));
}
tasks.put(task.getName(), task);
}
@Nullable
public TickableTask<?> getTask(@Nonnull String name) {
return tasks.get(name);
}
@Override
public final void updateEntity() {
isRunningTick = true;
final boolean isServerSide = isServerSide();
try {
if (timer++ == 0) {
markDirty();
GT_Util.markChunkDirty(this);
onFirstTick(isServerSide);
}
if (isDead()) {
return;
}
onPreTick(timer, isServerSide);
super.updateEntity();
if (!isServerSide && needsUpdate) {
worldObj.markBlockForUpdate(xCoord, yCoord, zCoord);
needsUpdate = false;
}
onTick(timer, isServerSide);
for (TickableTask<?> task : tasks.values()) {
task.update(timer, isServerSide);
}
if (isServerSide && timer > 2 && sendClientData) {
sendClientData(null);
}
onPostTick(timer, isServerSide);
} catch (Throwable e) {
GT_FML_LOGGER.error("UpdateEntity Failed", e);
e.printStackTrace(GT_Log.err);
try {
onTickFailed(timer, isServerSide);
} catch (Throwable e2) {
GT_FML_LOGGER.error("UpdateEntity:onTickFailed Failed", e);
}
}
isRunningTick = false;
}
@Override
public void sendClientData(EntityPlayerMP aPlayer) {
if (sendClientData) {
// GT_FML_LOGGER.info("Sending client data");
super.sendClientData(aPlayer);
sendClientData = false;
}
}
/**
* The very first Tick happening to this TileEntity.
*/
public void onFirstTick(boolean isServerSide) {
if (isServerSide) {
checkDropCover();
} else {
requestCoverDataIfNeeded();
}
}
/**
* The first part of the Tick, before block update.
*/
public void onPreTick(long tick, boolean isServerSide) {}
/**
* The regular Tick. After block update, before sending data to client.
*/
public void onTick(long tick, boolean isServerSide) {}
/**
* The absolute last part of the Tick, after sending data to client.
*/
public void onPostTick(long tick, boolean isServerSide) {}
/**
* Gets called when there is an Exception/Error happening during one of the Tick methods.
*/
public void onTickFailed(long tick, boolean isServerSide) {}
@Override
protected final void readTasksNBT(NBTTagCompound nbt) {
if (nbt.hasKey(GT_Values.NBT.TASKS)) {
NBTTagCompound tasksTag = nbt.getCompoundTag(GT_Values.NBT.TASKS);
for (TickableTask<?> task : tasks.values()) {
if (tasksTag.hasKey(task.getName())) {
task.readFromNBT(tasksTag.getCompoundTag(task.getName()));
}
}
}
}
@Override
protected final void writeTasksNBT(NBTTagCompound aNBT) {
NBTTagCompound tasksTag = new NBTTagCompound();
for (TickableTask<?> task : tasks.values()) {
NBTTagCompound tag = new NBTTagCompound();
task.writeToNBT(tag);
tasksTag.setTag(task.getName(), tag);
}
aNBT.setTag(GT_Values.NBT.TASKS, tasksTag);
}
@Override
public void onNeighborBlockChange(World aWorld, Block aBlock) {
blockUpdated = true;
}
@Override
public void issueClientUpdate() {
sendClientData = true;
}
@Override
public byte getComparatorValue(ForgeDirection side) {
return 0;
}
}
|