-
Notifications
You must be signed in to change notification settings - Fork 150
Expand file tree
/
Copy pathMetaTileEntityHolder.java
More file actions
292 lines (261 loc) · 11.2 KB
/
MetaTileEntityHolder.java
File metadata and controls
292 lines (261 loc) · 11.2 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
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
package gregtech.api.metatileentity;
import com.google.common.base.Preconditions;
import gregtech.api.GTValues;
import gregtech.api.GregTechAPI;
import gregtech.api.block.machines.BlockMachine;
import gregtech.api.cover.CoverBehavior;
import gregtech.api.gui.IUIHolder;
import gregtech.api.util.FirstTickScheduler;
import gregtech.api.util.FirstTickTask;
import gregtech.api.util.GTControlledRegistry;
import gregtech.api.util.GTLog;
import net.minecraft.block.state.IBlockState;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.Mirror;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.Rotation;
import net.minecraft.util.math.AxisAlignedBB;
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 java.util.List;
import java.util.stream.Collectors;
public class MetaTileEntityHolder extends TickableTileEntityBase implements IUIHolder, FirstTickTask {
private MetaTileEntity metaTileEntity;
private boolean needToUpdateLightning = false;
public MetaTileEntity getMetaTileEntity() {
return metaTileEntity;
}
/**
* Sets this holder's current meta tile entity to copy of given one
* Note that this method copies given meta tile entity and returns actual instance
* so it is safe to call it on sample meta tile entities
*/
public MetaTileEntity setMetaTileEntity(MetaTileEntity sampleMetaTileEntity) {
Preconditions.checkNotNull(sampleMetaTileEntity, "metaTileEntity");
this.metaTileEntity = sampleMetaTileEntity.createMetaTileEntity(this);
this.metaTileEntity.holder = this;
this.metaTileEntity.onAttached();
if (hasWorld() && !getWorld().isRemote) {
updateBlockOpacity();
writeCustomData(-1, buffer -> {
buffer.writeVarInt(GregTechAPI.META_TILE_ENTITY_REGISTRY.getIdByObjectName(metaTileEntity.metaTileEntityId));
metaTileEntity.writeInitialSyncData(buffer);
});
//just to update neighbours so cables and other things will work properly
this.needToUpdateLightning = true;
world.neighborChanged(getPos(), getBlockType(), getPos());
markDirty();
}
return metaTileEntity;
}
private void updateBlockOpacity() {
IBlockState currentState = world.getBlockState(getPos());
boolean isMetaTileEntityOpaque = metaTileEntity.isOpaqueCube();
if (currentState.getValue(BlockMachine.OPAQUE) != isMetaTileEntityOpaque) {
world.setBlockState(getPos(), currentState.withProperty(BlockMachine.OPAQUE, isMetaTileEntityOpaque));
}
}
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);
}
public void notifyBlockUpdate() {
getWorld().notifyNeighborsOfStateChange(pos, getBlockType(), false);
}
@Override
public void readFromNBT(NBTTagCompound compound) {
super.readFromNBT(compound);
if (compound.hasKey("MetaId", NBT.TAG_STRING)) {
String metaTileEntityIdRaw = compound.getString("MetaId");
ResourceLocation metaTileEntityId;
if (metaTileEntityIdRaw.indexOf(':') == -1) {
metaTileEntityId = convertMetaTileEntityId(metaTileEntityIdRaw);
} else {
metaTileEntityId = new ResourceLocation(metaTileEntityIdRaw);
}
MetaTileEntity sampleMetaTileEntity = metaTileEntityId == null ? null : GregTechAPI.META_TILE_ENTITY_REGISTRY.getObject(metaTileEntityId);
NBTTagCompound metaTileEntityData = compound.getCompoundTag("MetaTileEntity");
if (sampleMetaTileEntity != null) {
this.metaTileEntity = sampleMetaTileEntity.createMetaTileEntity(this);
this.metaTileEntity.holder = this;
this.metaTileEntity.readFromNBT(metaTileEntityData);
} else {
GTLog.logger.error("Failed to load MetaTileEntity with invalid ID " + metaTileEntityIdRaw);
}
}
}
private static List<String> registeredModIDs = null;
private static ResourceLocation convertMetaTileEntityId(String metaTileEntityIdOld) {
ResourceLocation gregtechId = new ResourceLocation(GTValues.MODID, metaTileEntityIdOld);
GTControlledRegistry<ResourceLocation, MetaTileEntity> registry = GregTechAPI.META_TILE_ENTITY_REGISTRY;
if (registry.containsKey(gregtechId)) {
return gregtechId; //remap to gregtech meta tile entities first
}
//try to lookup by different registry IDs
if (registeredModIDs == null) {
registeredModIDs = registry.getKeys().stream()
.map(ResourceLocation::getNamespace)
.distinct().collect(Collectors.toList());
registeredModIDs.remove(GTValues.MODID);
}
for (String registryModId : registeredModIDs) {
ResourceLocation probableId = new ResourceLocation(registryModId, metaTileEntityIdOld);
if (registry.containsKey(probableId)) {
return probableId;
}
}
GTLog.logger.error("Failed to convert old MetaTileEntity string ID " + metaTileEntityIdOld);
return null;
}
@Override
public NBTTagCompound writeToNBT(NBTTagCompound compound) {
super.writeToNBT(compound);
if (metaTileEntity != null) {
compound.setString("MetaId", metaTileEntity.metaTileEntityId.toString());
NBTTagCompound metaTileEntityData = new NBTTagCompound();
metaTileEntity.writeToNBT(metaTileEntityData);
compound.setTag("MetaTileEntity", metaTileEntityData);
}
return compound;
}
@Override
public boolean hasCapability(Capability<?> capability, @Nullable EnumFacing facing) {
Object metaTileEntityValue = metaTileEntity == null ? null : metaTileEntity.getCoverCapability(capability, facing);
return metaTileEntityValue != null || super.hasCapability(capability, facing);
}
@Nullable
@Override
public <T> T getCapability(Capability<T> capability, @Nullable EnumFacing facing) {
T metaTileEntityValue = metaTileEntity == null ? null : metaTileEntity.getCoverCapability(capability, facing);
return metaTileEntityValue != null ? metaTileEntityValue : super.getCapability(capability, facing);
}
@Override
public void update() {
if (metaTileEntity != null) {
metaTileEntity.update();
}
if (this.needToUpdateLightning) {
getWorld().checkLight(getPos());
this.needToUpdateLightning = false;
}
//increment only after current tick, so meta tile entities will get first tick as timer == 0
//and update their settings which depend on getTimer() % N properly
super.update();
}
@Override
public void writeInitialSyncData(PacketBuffer buf) {
if (metaTileEntity != null) {
buf.writeBoolean(true);
buf.writeVarInt(GregTechAPI.META_TILE_ENTITY_REGISTRY.getIdByObjectName(metaTileEntity.metaTileEntityId));
metaTileEntity.writeInitialSyncData(buf);
} else buf.writeBoolean(false);
}
@Override
public void receiveInitialSyncData(PacketBuffer buf) {
if (buf.readBoolean()) {
int metaTileEntityId = buf.readVarInt();
setMetaTileEntity(GregTechAPI.META_TILE_ENTITY_REGISTRY.getObjectById(metaTileEntityId));
this.metaTileEntity.receiveInitialSyncData(buf);
scheduleChunkForRenderUpdate();
this.needToUpdateLightning = true;
}
}
@Override
public void receiveCustomData(int discriminator, PacketBuffer buffer) {
if (discriminator == -1) {
int metaTileEntityId = buffer.readVarInt();
setMetaTileEntity(GregTechAPI.META_TILE_ENTITY_REGISTRY.getObjectById(metaTileEntityId));
this.metaTileEntity.receiveInitialSyncData(buffer);
scheduleChunkForRenderUpdate();
this.needToUpdateLightning = true;
} else if (metaTileEntity != null) {
metaTileEntity.receiveCustomData(discriminator, buffer);
}
}
@Override
public boolean isValid() {
return !super.isInvalid() && metaTileEntity != null;
}
@Override
public boolean isRemote() {
return getWorld().isRemote;
}
@Override
public void markAsDirty() {
markDirty();
}
@Override
public void onLoad() {
super.onLoad();
if (this.metaTileEntity != null) {
FirstTickScheduler.addTask(this);
}
}
@Override
public void handleFirstTick() {
if (this.metaTileEntity != null) {
this.metaTileEntity.onLoad();
}
}
@Override
public void onChunkUnload() {
super.onChunkUnload();
if (metaTileEntity != null) {
metaTileEntity.onUnload();
}
}
@Override
public boolean shouldRefresh(World world, BlockPos pos, IBlockState oldState, IBlockState newSate) {
return oldState.getBlock() != newSate.getBlock(); //MetaTileEntityHolder should never refresh (until block changes)
}
@Override
public void rotate(Rotation rotationIn) {
if (metaTileEntity != null) {
metaTileEntity.setFrontFacing(rotationIn.rotate(metaTileEntity.getFrontFacing()));
}
}
@Override
public void mirror(Mirror mirrorIn) {
if (metaTileEntity != null) {
rotate(mirrorIn.toRotation(metaTileEntity.getFrontFacing()));
}
}
@Override
public boolean shouldRenderInPass(int pass) {
if (metaTileEntity == null) return false;
for (EnumFacing side: EnumFacing.VALUES){
CoverBehavior cover = metaTileEntity.getCoverAtSide(side);
if (cover instanceof IFastRenderMetaTileEntity && ((IFastRenderMetaTileEntity) cover).shouldRenderInPass(pass)) {
return true;
} else if(cover instanceof IRenderMetaTileEntity && ((IRenderMetaTileEntity) cover).shouldRenderInPass(pass)) {
return true;
}
}
if (metaTileEntity instanceof IRenderMetaTileEntity) {
return ((IRenderMetaTileEntity) metaTileEntity).shouldRenderInPass(pass);
} else if (metaTileEntity instanceof IFastRenderMetaTileEntity) {
return ((IFastRenderMetaTileEntity) metaTileEntity).shouldRenderInPass(pass);
}
return false;
}
@Override
public AxisAlignedBB getRenderBoundingBox() {
if (metaTileEntity instanceof IRenderMetaTileEntity) {
return ((IRenderMetaTileEntity) metaTileEntity).getRenderBoundingBox();
} else if (metaTileEntity instanceof IFastRenderMetaTileEntity) {
return ((IFastRenderMetaTileEntity) metaTileEntity).getRenderBoundingBox();
}
return new AxisAlignedBB(getPos());
}
@Override
public boolean canRenderBreaking() {
return false;
}
}