-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathTileEntityPipeBase.java
394 lines (343 loc) · 14.1 KB
/
TileEntityPipeBase.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
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
package gregtech.api.pipenet.tile;
import gnu.trove.map.TIntIntMap;
import gnu.trove.map.hash.TIntIntHashMap;
import gregtech.api.capability.GregtechTileCapabilities;
import gregtech.api.cover.CoverBehavior;
import gregtech.api.metatileentity.SyncedTileEntityBase;
import gregtech.api.pipenet.WorldPipeNet;
import gregtech.api.pipenet.block.BlockPipe;
import gregtech.api.pipenet.block.IPipeType;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.PacketBuffer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.common.util.Constants.NBT;
import javax.annotation.Nullable;
import codechicken.lib.raytracer.CuboidRayTraceResult;
import java.util.function.Consumer;
public abstract class TileEntityPipeBase<PipeType extends Enum<PipeType> & IPipeType<NodeDataType>, NodeDataType> extends SyncedTileEntityBase implements IPipeTile<PipeType, NodeDataType> {
private TIntIntMap blockedConnectionsMap = new TIntIntHashMap();
private int blockedConnections = 0;
protected int insulationColor = DEFAULT_INSULATION_COLOR;
protected final PipeCoverableImplementation coverableImplementation = new PipeCoverableImplementation(this);
private NodeDataType cachedNodeData;
private BlockPipe<PipeType, NodeDataType, ?> pipeBlock;
private PipeType pipeType = getPipeTypeClass().getEnumConstants()[0];
private boolean detachedConversionMode;
private boolean wasInDetachedConversionMode;
public TileEntityPipeBase() {
}
public void setDetachedConversionMode(boolean detachedConversionMode) {
this.detachedConversionMode = detachedConversionMode;
this.wasInDetachedConversionMode = true;
}
public boolean wasInDetachedConversionMode() {
return wasInDetachedConversionMode;
}
public void setPipeData(BlockPipe<PipeType, NodeDataType, ?> pipeBlock, PipeType pipeType) {
this.pipeBlock = pipeBlock;
this.pipeType = pipeType;
if(!getWorld().isRemote) {
writeCustomData(-4, this::writePipeProperties);
}
}
@Override
public void transferDataFrom(IPipeTile<PipeType, NodeDataType> tileEntity) {
this.pipeType = tileEntity.getPipeType();
this.blockedConnectionsMap = tileEntity.getBlockedConnectionsMap();
this.insulationColor = tileEntity.getInsulationColor();
if (tileEntity instanceof TileEntityPipeBase) {
this.updateEntries.addAll(((TileEntityPipeBase<?, ?>) tileEntity).updateEntries);
}
tileEntity.getCoverableImplementation().transferDataTo(coverableImplementation);
recomputeBlockedConnections();
}
public abstract Class<PipeType> getPipeTypeClass();
@Override
public abstract boolean supportsTicking();
@Override
public World getPipeWorld() {
return getWorld();
}
@Override
public BlockPos getPipePos() {
return getPos();
}
@Override
public PipeCoverableImplementation getCoverableImplementation() {
return coverableImplementation;
}
@Override
public boolean canPlaceCoverOnSide(EnumFacing side) {
return true;
}
@Override
public IPipeTile<PipeType, NodeDataType> setSupportsTicking() {
if (supportsTicking()) {
return this;
}
//create new tickable tile entity, transfer data, and replace it
IPipeTile<PipeType, NodeDataType> newTile = getPipeBlock().createNewTileEntity(true);
newTile.transferDataFrom(this);
getWorld().setTileEntity(getPos(), (TileEntity) newTile);
return newTile;
}
@Override
public BlockPipe<PipeType, NodeDataType, ?> getPipeBlock() {
if (pipeBlock == null) {
Block block = getBlockState().getBlock();
//noinspection unchecked
this.pipeBlock = block instanceof BlockPipe ? (BlockPipe<PipeType, NodeDataType, ?>) block : null;
}
return pipeBlock;
}
@Override
public int getBlockedConnections() {
return blockedConnections;
}
@Override
public TIntIntMap getBlockedConnectionsMap() {
return new TIntIntHashMap(blockedConnectionsMap);
}
@Override
public int getInsulationColor() {
return insulationColor;
}
@Override
public void setInsulationColor(int insulationColor) {
this.insulationColor = insulationColor;
if (!getWorld().isRemote) {
if (!detachedConversionMode) {
getPipeBlock().getWorldPipeNet(getWorld()).updateMark(getPos(), getCableMark());
}
writeCustomData(-1, buffer -> buffer.writeInt(insulationColor));
markDirty();
}
}
@Override
public boolean isConnectionBlocked(AttachmentType type, EnumFacing side) {
int blockedConnections = blockedConnectionsMap.get(type.ordinal());
return (blockedConnections & 1 << side.getIndex()) > 0;
}
@Override
public void setConnectionBlocked(AttachmentType attachmentType, EnumFacing side, boolean blocked) {
int blockedConnections = blockedConnectionsMap.get(attachmentType.ordinal());
this.blockedConnectionsMap.put(attachmentType.ordinal(), withSideConnectionBlocked(blockedConnections, side, blocked));
recomputeBlockedConnections();
if (!getWorld().isRemote) {
if (!detachedConversionMode) {
updateSideBlockedConnection(side);
}
writeCustomData(-2, buffer -> buffer.writeVarInt(this.blockedConnections));
markDirty();
}
}
private void recomputeBlockedConnections() {
int resultBlockedConnections = 0;
for(int blockedConnections : blockedConnectionsMap.values()) {
resultBlockedConnections |= blockedConnections;
}
this.blockedConnections = resultBlockedConnections;
}
private void updateSideBlockedConnection(EnumFacing side) {
if (!detachedConversionMode) {
WorldPipeNet<?, ?> worldPipeNet = getPipeBlock().getWorldPipeNet(getWorld());
boolean isSideBlocked = false;
int sideIndex = 1 << side.getIndex();
for(int blockedConnections : blockedConnectionsMap.values()) {
isSideBlocked |= (blockedConnections & sideIndex) > 0;
}
worldPipeNet.updateBlockedConnections(getPos(), side, isSideBlocked);
}
}
private int withSideConnectionBlocked(int blockedConnections, EnumFacing side, boolean blocked) {
int index = 1 << side.getIndex();
if(blocked) {
return blockedConnections | index;
} else {
return blockedConnections & ~index;
}
}
@Override
public PipeType getPipeType() {
return pipeType;
}
@Override
public NodeDataType getNodeData() {
if (cachedNodeData == null) {
this.cachedNodeData = getPipeBlock().createProperties(this);
}
return cachedNodeData;
}
private int getCableMark() {
return insulationColor == DEFAULT_INSULATION_COLOR ? 0 : insulationColor;
}
public <T> T getCapabilityInternal(Capability<T> capability, @Nullable EnumFacing facing) {
if (capability == GregtechTileCapabilities.CAPABILITY_COVERABLE) {
return GregtechTileCapabilities.CAPABILITY_COVERABLE.cast(getCoverableImplementation());
}
return super.getCapability(capability, facing);
}
@Nullable
@Override
public final <T> T getCapability(Capability<T> capability, @Nullable EnumFacing facing) {
boolean isCoverable = capability == GregtechTileCapabilities.CAPABILITY_COVERABLE;
CoverBehavior coverBehavior = facing == null ? null : coverableImplementation.getCoverAtSide(facing);
T defaultValue = getCapabilityInternal(capability, facing);
if(isCoverable) {
return defaultValue;
}
if(coverBehavior == null && facing != null) {
boolean isBlocked = (getBlockedConnections() & 1 << facing.getIndex()) > 0;
return isBlocked ? null : defaultValue;
}
if (coverBehavior != null) {
return coverBehavior.getCapability(capability, defaultValue);
}
return defaultValue;
}
@Override
public final boolean hasCapability(Capability<?> capability, @Nullable EnumFacing facing) {
return getCapability(capability, facing) != null;
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
super.writeToNBT(compound);
BlockPipe<PipeType, NodeDataType, ?> pipeBlock = getPipeBlock();
if (pipeBlock != null) {
//noinspection ConstantConditions
compound.setString("PipeBlock", pipeBlock.getRegistryName().toString());
}
compound.setInteger("PipeType", pipeType.ordinal());
NBTTagCompound blockedConnectionsTag = new NBTTagCompound();
for(int attachmentType : blockedConnectionsMap.keys()) {
int blockedConnections = blockedConnectionsMap.get(attachmentType);
blockedConnectionsTag.setInteger(Integer.toString(attachmentType), blockedConnections);
}
compound.setTag("BlockedConnectionsMap", blockedConnectionsTag);
compound.setInteger("InsulationColor", insulationColor);
this.coverableImplementation.writeToNBT(compound);
return compound;
}
@Override
public void readFromNBT(NBTTagCompound compound) {
super.readFromNBT(compound);
if (compound.hasKey("PipeBlock", NBT.TAG_STRING)) {
Block block = Block.REGISTRY.getObject(new ResourceLocation(compound.getString("PipeBlock")));
//noinspection unchecked
this.pipeBlock = block instanceof BlockPipe ? (BlockPipe<PipeType, NodeDataType, ?>) block : null;
}
this.pipeType = getPipeTypeClass().getEnumConstants()[compound.getInteger("PipeType")];
NBTTagCompound blockedConnectionsTag = compound.getCompoundTag("BlockedConnectionsMap");
this.blockedConnectionsMap.clear();
for(String attachmentTypeKey : blockedConnectionsTag.getKeySet()) {
int attachmentType = Integer.parseInt(attachmentTypeKey);
int blockedConnections = blockedConnectionsTag.getInteger(attachmentTypeKey);
this.blockedConnectionsMap.put(attachmentType, blockedConnections);
}
recomputeBlockedConnections();
this.insulationColor = compound.getInteger("InsulationColor");
this.coverableImplementation.readFromNBT(compound);
}
@Override
public void onLoad() {
super.onLoad();
this.coverableImplementation.onLoad();
}
protected void writePipeProperties(PacketBuffer buf) {
buf.writeVarInt(pipeType.ordinal());
}
protected void readPipeProperties(PacketBuffer buf) {
this.pipeType = getPipeTypeClass().getEnumConstants()[buf.readVarInt()];
}
@Override
public void writeInitialSyncData(PacketBuffer buf) {
writePipeProperties(buf);
buf.writeVarInt(blockedConnections);
buf.writeInt(insulationColor);
this.coverableImplementation.writeInitialSyncData(buf);
}
@Override
public void receiveInitialSyncData(PacketBuffer buf) {
readPipeProperties(buf);
this.blockedConnections = buf.readVarInt();
this.insulationColor = buf.readInt();
this.coverableImplementation.readInitialSyncData(buf);
}
@Override
public void receiveCustomData(int discriminator, PacketBuffer buf) {
if (discriminator == -1) {
this.insulationColor = buf.readInt();
scheduleChunkForRenderUpdate();
} else if (discriminator == -2) {
this.blockedConnections = buf.readVarInt();
scheduleChunkForRenderUpdate();
} else if (discriminator == -3) {
this.coverableImplementation.readCustomData(buf.readVarInt(), buf);
} else if(discriminator == -4) {
readPipeProperties(buf);
scheduleChunkForRenderUpdate();
}
}
@Override
public void writeCoverCustomData(int id, Consumer<PacketBuffer> writer) {
writeCustomData(-3, buffer -> {
buffer.writeVarInt(id);
writer.accept(buffer);
});
}
@Override
public void scheduleChunkForRenderUpdate() {
BlockPos pos = getPos();
getWorld().markBlockRangeForRenderUpdate(
pos.getX() - 1, pos.getY() - 1, pos.getZ() - 1,
pos.getX() + 1, pos.getY() + 1, pos.getZ() + 1);
}
@Override
public void notifyBlockUpdate() {
getWorld().notifyNeighborsOfStateChange(getPos(), getBlockType(), true);
getPipeBlock().updateActiveNodeStatus(getWorld(), getPos(), this);
}
@Override
public void markAsDirty() {
markDirty();
}
@Override
public boolean isValidTile() {
return !isInvalid();
}
@Override
public boolean shouldRefresh(World world, BlockPos pos, IBlockState oldState, IBlockState newSate) {
return oldState.getBlock() != newSate.getBlock();
}
/**
* @return true to enable an invocation of createUI when right click is pressed
*/
protected boolean openGUIOnRightClick() {
return false;
}
/**
* Called when player clicks on specific side of this pipe tile entity
*
* @return true if something happened, so animation will be played
*/
public boolean onRightClick(EntityPlayer playerIn, EnumHand hand, EnumFacing facing, CuboidRayTraceResult hitResult) {
if (!playerIn.isSneaking() && openGUIOnRightClick()) {
World world = getWorld();
if (world != null && !world.isRemote) {
PipeTileUIFactory.INSTANCE.openUI(this, (EntityPlayerMP) playerIn);
}
return true;
}
return false;
}
}