Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix MTE Initial Sync sending the wrong data #2587

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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