Skip to content

Commit

Permalink
Support Creative Mode Pick Block (GregTechCEu#2428)
Browse files Browse the repository at this point in the history
  • Loading branch information
ghzdude authored Jun 22, 2024
1 parent 5af02a3 commit 18a0bbc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
25 changes: 22 additions & 3 deletions src/main/java/gregtech/api/block/machines/BlockMachine.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import gregtech.api.block.BlockCustomParticle;
import gregtech.api.block.UnlistedIntegerProperty;
import gregtech.api.block.UnlistedStringProperty;
import gregtech.api.capability.GregtechDataCodes;
import gregtech.api.cover.Cover;
import gregtech.api.cover.IFacadeCover;
import gregtech.api.items.toolitem.ToolClasses;
Expand Down Expand Up @@ -262,9 +263,27 @@ public void onBlockPlacedBy(World worldIn, @NotNull BlockPos pos, @NotNull IBloc
((MetaTileEntityHolder) holder).setCustomName(stack.getDisplayName());
}
MetaTileEntity metaTileEntity = holder.setMetaTileEntity(sampleMetaTileEntity);
if (stack.hasTagCompound()) {
// noinspection ConstantConditions
metaTileEntity.initFromItemStackData(stack.getTagCompound());
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 (metaTileEntity.isValidFrontFacing(EnumFacing.UP)) {
metaTileEntity.setFrontFacing(EnumFacing.getDirectionFromEntityLiving(pos, placer));
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/gregtech/api/capability/GregtechDataCodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ public static int assignId() {

// From MetaTileEntityHolder
public static final String CUSTOM_NAME = "CustomName";
public static final String BLOCK_ENTITY_TAG = "BlockEntityTag";
public static final String TAG_KEY_MTE = "MetaTileEntity";

// From MetaTileEntity
public static final String TAG_KEY_PAINTING_COLOR = "PaintingColor";
Expand Down

0 comments on commit 18a0bbc

Please sign in to comment.