Skip to content

Commit 18672ff

Browse files
authored
Fix MTE Initial Sync sending the wrong data (#2587)
1 parent 0b8263d commit 18672ff

3 files changed

Lines changed: 59 additions & 43 deletions

File tree

src/main/java/gregtech/api/block/machines/BlockMachine.java

Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -271,34 +271,38 @@ public void onBlockPlacedBy(World worldIn, @NotNull BlockPos pos, @NotNull IBloc
271271
Objects.requireNonNull(stack.getItem().getRegistryName()).getNamespace());
272272

273273
MetaTileEntity sampleMetaTileEntity = registry.getObjectById(stack.getItemDamage());
274-
if (holder != null && sampleMetaTileEntity != null) {
275-
// TODO Fix this
276-
if (stack.hasDisplayName() && holder instanceof MetaTileEntityHolder) {
277-
((MetaTileEntityHolder) holder).setCustomName(stack.getDisplayName());
278-
}
279-
MetaTileEntity metaTileEntity = holder.setMetaTileEntity(sampleMetaTileEntity);
280-
var stackTag = stack.getTagCompound();
281-
if (stackTag != null && !stackTag.isEmpty()) {
282-
if (stackTag.hasKey(GregtechDataCodes.BLOCK_ENTITY_TAG)) {
283-
var blockTag = stackTag.getCompoundTag(GregtechDataCodes.BLOCK_ENTITY_TAG);
284-
String customName = blockTag.getString(GregtechDataCodes.CUSTOM_NAME);
285-
if (!customName.isEmpty())
286-
((MetaTileEntityHolder) holder).setCustomName(customName);
287-
288-
var mteTag = blockTag.getCompoundTag(GregtechDataCodes.TAG_KEY_MTE);
289-
List<String> removed = new ArrayList<>();
290-
for (var key : mteTag.getKeySet()) {
291-
var trait = metaTileEntity.getMTETrait(key);
292-
if (trait == null) continue;
293-
294-
removed.add(key);
295-
}
296-
removed.forEach(mteTag::removeTag);
297-
metaTileEntity.readFromNBT(mteTag);
298-
} else {
299-
metaTileEntity.initFromItemStackData(stackTag);
274+
if (holder == null || sampleMetaTileEntity == null)
275+
return;
276+
277+
// TODO Fix this
278+
if (stack.hasDisplayName() && holder instanceof MetaTileEntityHolder) {
279+
((MetaTileEntityHolder) holder).setCustomName(stack.getDisplayName());
280+
}
281+
var stackTag = stack.getTagCompound();
282+
NBTTagCompound mteTag = null;
283+
if (stackTag != null && !stackTag.isEmpty()) {
284+
if (stackTag.hasKey(GregtechDataCodes.BLOCK_ENTITY_TAG)) {
285+
var blockTag = stackTag.getCompoundTag(GregtechDataCodes.BLOCK_ENTITY_TAG);
286+
String customName = blockTag.getString(GregtechDataCodes.CUSTOM_NAME);
287+
if (!customName.isEmpty())
288+
((MetaTileEntityHolder) holder).setCustomName(customName);
289+
290+
mteTag = blockTag.getCompoundTag(GregtechDataCodes.TAG_KEY_MTE);
291+
List<String> removed = new ArrayList<>();
292+
for (var key : mteTag.getKeySet()) {
293+
var trait = sampleMetaTileEntity.getMTETrait(key);
294+
if (trait == null) continue;
295+
296+
removed.add(key);
300297
}
298+
removed.forEach(mteTag::removeTag);
301299
}
300+
}
301+
MetaTileEntity metaTileEntity = holder.setMetaTileEntity(sampleMetaTileEntity, mteTag);
302+
if (mteTag == null) {
303+
if (stackTag != null && !stackTag.isEmpty())
304+
metaTileEntity.initFromItemStackData(stackTag);
305+
302306
if (metaTileEntity.isValidFrontFacing(EnumFacing.UP)) {
303307
metaTileEntity.setFrontFacing(EnumFacing.getDirectionFromEntityLiving(pos, placer));
304308
} else {
@@ -314,26 +318,26 @@ public void onBlockPlacedBy(World worldIn, @NotNull BlockPos pos, @NotNull IBloc
314318
}
315319
}
316320
}
317-
if (Mods.AppliedEnergistics2.isModLoaded()) {
318-
if (metaTileEntity.getProxy() != null) {
319-
metaTileEntity.getProxy().setOwner((EntityPlayer) placer);
320-
}
321+
}
322+
if (Mods.AppliedEnergistics2.isModLoaded()) {
323+
if (metaTileEntity.getProxy() != null) {
324+
metaTileEntity.getProxy().setOwner((EntityPlayer) placer);
321325
}
326+
}
322327

323-
// Color machines on place if holding spray can in off-hand
324-
if (placer instanceof EntityPlayer) {
325-
ItemStack offhand = placer.getHeldItemOffhand();
326-
for (int i = 0; i < EnumDyeColor.values().length; i++) {
327-
if (offhand.isItemEqual(MetaItems.SPRAY_CAN_DYES[i].getStackForm())) {
328-
MetaItems.SPRAY_CAN_DYES[i].getBehaviours().get(0).onItemUse((EntityPlayer) placer, worldIn,
329-
pos, EnumHand.OFF_HAND, EnumFacing.UP, 0, 0, 0);
330-
break;
331-
}
328+
// Color machines on place if holding spray can in off-hand
329+
if (placer instanceof EntityPlayer) {
330+
ItemStack offhand = placer.getHeldItemOffhand();
331+
for (int i = 0; i < EnumDyeColor.values().length; i++) {
332+
if (offhand.isItemEqual(MetaItems.SPRAY_CAN_DYES[i].getStackForm())) {
333+
MetaItems.SPRAY_CAN_DYES[i].getBehaviours().get(0).onItemUse((EntityPlayer) placer, worldIn,
334+
pos, EnumHand.OFF_HAND, EnumFacing.UP, 0, 0, 0);
335+
break;
332336
}
333337
}
334-
335-
metaTileEntity.onPlacement(placer);
336338
}
339+
340+
metaTileEntity.onPlacement(placer);
337341
}
338342

339343
@Override

src/main/java/gregtech/api/metatileentity/MetaTileEntityHolder.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,12 @@ public MetaTileEntity getMetaTileEntity() {
8989
* Also can use certain data to preinit the block before data is synced
9090
*/
9191
@Override
92-
public MetaTileEntity setMetaTileEntity(MetaTileEntity sampleMetaTileEntity) {
92+
public MetaTileEntity setMetaTileEntity(@NotNull MetaTileEntity sampleMetaTileEntity,
93+
@Nullable NBTTagCompound tagCompound) {
9394
Preconditions.checkNotNull(sampleMetaTileEntity, "metaTileEntity");
9495
setRawMetaTileEntity(sampleMetaTileEntity.createMetaTileEntity(this));
96+
if (tagCompound != null && !tagCompound.isEmpty())
97+
getMetaTileEntity().readFromNBT(tagCompound);
9598
if (hasWorld() && !getWorld().isRemote) {
9699
updateBlockOpacity();
97100
writeCustomData(INITIALIZE_MTE, buffer -> {

src/main/java/gregtech/api/metatileentity/interfaces/IGregTechTileEntity.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
import gregtech.api.gui.IUIHolder;
44
import gregtech.api.metatileentity.MetaTileEntity;
55

6+
import net.minecraft.nbt.NBTTagCompound;
7+
8+
import org.jetbrains.annotations.NotNull;
9+
import org.jetbrains.annotations.Nullable;
10+
611
/**
712
* A simple compound Interface for all my TileEntities.
813
* <p/>
@@ -13,7 +18,11 @@ public interface IGregTechTileEntity extends IHasWorldObjectAndCoords, INeighbor
1318

1419
MetaTileEntity getMetaTileEntity();
1520

16-
MetaTileEntity setMetaTileEntity(MetaTileEntity metaTileEntity);
21+
default MetaTileEntity setMetaTileEntity(MetaTileEntity metaTileEntity) {
22+
return setMetaTileEntity(metaTileEntity, null);
23+
}
24+
25+
MetaTileEntity setMetaTileEntity(@NotNull MetaTileEntity metaTileEntity, @Nullable NBTTagCompound tagCompound);
1726

1827
long getOffsetTimer(); // todo might not keep this one
1928

0 commit comments

Comments
 (0)