Skip to content

Commit

Permalink
Fix MTE Initial Sync sending the wrong data (GregTechCEu#2587)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghzdude authored Sep 12, 2024
1 parent 0b8263d commit 18672ff
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 43 deletions.
86 changes: 45 additions & 41 deletions src/main/java/gregtech/api/block/machines/BlockMachine.java
Original file line number Diff line number Diff line change
Expand Up @@ -271,34 +271,38 @@ public void onBlockPlacedBy(World worldIn, @NotNull BlockPos pos, @NotNull IBloc
Objects.requireNonNull(stack.getItem().getRegistryName()).getNamespace());

MetaTileEntity sampleMetaTileEntity = registry.getObjectById(stack.getItemDamage());
if (holder != null && sampleMetaTileEntity != null) {
// TODO Fix this
if (stack.hasDisplayName() && holder instanceof MetaTileEntityHolder) {
((MetaTileEntityHolder) holder).setCustomName(stack.getDisplayName());
}
MetaTileEntity metaTileEntity = holder.setMetaTileEntity(sampleMetaTileEntity);
var stackTag = stack.getTagCompound();
if (stackTag != null && !stackTag.isEmpty()) {
if (stackTag.hasKey(GregtechDataCodes.BLOCK_ENTITY_TAG)) {
var blockTag = stackTag.getCompoundTag(GregtechDataCodes.BLOCK_ENTITY_TAG);
String customName = blockTag.getString(GregtechDataCodes.CUSTOM_NAME);
if (!customName.isEmpty())
((MetaTileEntityHolder) holder).setCustomName(customName);

var mteTag = blockTag.getCompoundTag(GregtechDataCodes.TAG_KEY_MTE);
List<String> removed = new ArrayList<>();
for (var key : mteTag.getKeySet()) {
var trait = metaTileEntity.getMTETrait(key);
if (trait == null) continue;

removed.add(key);
}
removed.forEach(mteTag::removeTag);
metaTileEntity.readFromNBT(mteTag);
} else {
metaTileEntity.initFromItemStackData(stackTag);
if (holder == null || sampleMetaTileEntity == null)
return;

// TODO Fix this
if (stack.hasDisplayName() && holder instanceof MetaTileEntityHolder) {
((MetaTileEntityHolder) holder).setCustomName(stack.getDisplayName());
}
var stackTag = stack.getTagCompound();
NBTTagCompound mteTag = null;
if (stackTag != null && !stackTag.isEmpty()) {
if (stackTag.hasKey(GregtechDataCodes.BLOCK_ENTITY_TAG)) {
var blockTag = stackTag.getCompoundTag(GregtechDataCodes.BLOCK_ENTITY_TAG);
String customName = blockTag.getString(GregtechDataCodes.CUSTOM_NAME);
if (!customName.isEmpty())
((MetaTileEntityHolder) holder).setCustomName(customName);

mteTag = blockTag.getCompoundTag(GregtechDataCodes.TAG_KEY_MTE);
List<String> removed = new ArrayList<>();
for (var key : mteTag.getKeySet()) {
var trait = sampleMetaTileEntity.getMTETrait(key);
if (trait == null) continue;

removed.add(key);
}
removed.forEach(mteTag::removeTag);
}
}
MetaTileEntity metaTileEntity = holder.setMetaTileEntity(sampleMetaTileEntity, mteTag);
if (mteTag == null) {
if (stackTag != null && !stackTag.isEmpty())
metaTileEntity.initFromItemStackData(stackTag);

if (metaTileEntity.isValidFrontFacing(EnumFacing.UP)) {
metaTileEntity.setFrontFacing(EnumFacing.getDirectionFromEntityLiving(pos, placer));
} else {
Expand All @@ -314,26 +318,26 @@ public void onBlockPlacedBy(World worldIn, @NotNull BlockPos pos, @NotNull IBloc
}
}
}
if (Mods.AppliedEnergistics2.isModLoaded()) {
if (metaTileEntity.getProxy() != null) {
metaTileEntity.getProxy().setOwner((EntityPlayer) placer);
}
}
if (Mods.AppliedEnergistics2.isModLoaded()) {
if (metaTileEntity.getProxy() != null) {
metaTileEntity.getProxy().setOwner((EntityPlayer) placer);
}
}

// Color machines on place if holding spray can in off-hand
if (placer instanceof EntityPlayer) {
ItemStack offhand = placer.getHeldItemOffhand();
for (int i = 0; i < EnumDyeColor.values().length; i++) {
if (offhand.isItemEqual(MetaItems.SPRAY_CAN_DYES[i].getStackForm())) {
MetaItems.SPRAY_CAN_DYES[i].getBehaviours().get(0).onItemUse((EntityPlayer) placer, worldIn,
pos, EnumHand.OFF_HAND, EnumFacing.UP, 0, 0, 0);
break;
}
// Color machines on place if holding spray can in off-hand
if (placer instanceof EntityPlayer) {
ItemStack offhand = placer.getHeldItemOffhand();
for (int i = 0; i < EnumDyeColor.values().length; i++) {
if (offhand.isItemEqual(MetaItems.SPRAY_CAN_DYES[i].getStackForm())) {
MetaItems.SPRAY_CAN_DYES[i].getBehaviours().get(0).onItemUse((EntityPlayer) placer, worldIn,
pos, EnumHand.OFF_HAND, EnumFacing.UP, 0, 0, 0);
break;
}
}

metaTileEntity.onPlacement(placer);
}

metaTileEntity.onPlacement(placer);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,12 @@ public MetaTileEntity getMetaTileEntity() {
* Also can use certain data to preinit the block before data is synced
*/
@Override
public MetaTileEntity setMetaTileEntity(MetaTileEntity sampleMetaTileEntity) {
public MetaTileEntity setMetaTileEntity(@NotNull MetaTileEntity sampleMetaTileEntity,
@Nullable NBTTagCompound tagCompound) {
Preconditions.checkNotNull(sampleMetaTileEntity, "metaTileEntity");
setRawMetaTileEntity(sampleMetaTileEntity.createMetaTileEntity(this));
if (tagCompound != null && !tagCompound.isEmpty())
getMetaTileEntity().readFromNBT(tagCompound);
if (hasWorld() && !getWorld().isRemote) {
updateBlockOpacity();
writeCustomData(INITIALIZE_MTE, buffer -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
import gregtech.api.gui.IUIHolder;
import gregtech.api.metatileentity.MetaTileEntity;

import net.minecraft.nbt.NBTTagCompound;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
* A simple compound Interface for all my TileEntities.
* <p/>
Expand All @@ -13,7 +18,11 @@ public interface IGregTechTileEntity extends IHasWorldObjectAndCoords, INeighbor

MetaTileEntity getMetaTileEntity();

MetaTileEntity setMetaTileEntity(MetaTileEntity metaTileEntity);
default MetaTileEntity setMetaTileEntity(MetaTileEntity metaTileEntity) {
return setMetaTileEntity(metaTileEntity, null);
}

MetaTileEntity setMetaTileEntity(@NotNull MetaTileEntity metaTileEntity, @Nullable NBTTagCompound tagCompound);

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

Expand Down

0 comments on commit 18672ff

Please sign in to comment.