From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Jason Penilla <11360596+jpenilla@users.noreply.github.com> Date: Wed, 30 Sep 2020 14:32:46 -0700 Subject: [PATCH] Persistent BlockEntity Lore and DisplayName Makes it so that when a BlockEntity is placed in the world and then broken, the dropped ItemStack retains any original custom display name/lore. diff --git a/src/main/java/net/minecraft/world/item/BlockItem.java b/src/main/java/net/minecraft/world/item/BlockItem.java index 8a5f6d955577a8f8a63a846169a63fe9685b6321..eb595d1b6ef59ca18ce1c3c3ccd2aa05242ebe65 100644 --- a/src/main/java/net/minecraft/world/item/BlockItem.java +++ b/src/main/java/net/minecraft/world/item/BlockItem.java @@ -151,7 +151,24 @@ public class BlockItem extends Item { } protected boolean updateCustomBlockEntityTag(BlockPos pos, Level world, @Nullable Player player, ItemStack stack, BlockState state) { - return BlockItem.updateCustomBlockEntityTag(world, player, pos, stack); + // Purpur start + boolean handled = updateCustomBlockEntityTag(world, player, pos, stack); + if (world.purpurConfig.persistentTileEntityDisplayNames && stack.hasTag()) { + CompoundTag display = stack.getTagElement("display"); + if (display != null) { + BlockEntity blockEntity = world.getBlockEntity(pos); + if (blockEntity != null) { + if (display.contains("Name", 8)) { + blockEntity.setPersistentDisplayName(display.getString("Name")); + } + if (display.contains("Lore", 9)) { + blockEntity.setPersistentLore(display.getList("Lore", 8)); + } + } + } + } + return handled; + // Purpur end } @Nullable diff --git a/src/main/java/net/minecraft/world/level/block/Block.java b/src/main/java/net/minecraft/world/level/block/Block.java index 5dede264fc3cb045b6330e35123b5b416c1b1f56..10fa3a544d01ce3cdb72e72952073d86913bd65b 100644 --- a/src/main/java/net/minecraft/world/level/block/Block.java +++ b/src/main/java/net/minecraft/world/level/block/Block.java @@ -20,6 +20,9 @@ import net.minecraft.core.Holder; import net.minecraft.core.IdMapper; import net.minecraft.core.NonNullList; import net.minecraft.core.Registry; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.nbt.ListTag; +import net.minecraft.nbt.StringTag; import net.minecraft.network.chat.Component; import net.minecraft.network.chat.MutableComponent; import net.minecraft.network.chat.TranslatableComponent; @@ -27,6 +30,7 @@ import net.minecraft.server.level.ServerLevel; import net.minecraft.stats.Stats; import net.minecraft.tags.BlockTags; import net.minecraft.util.Mth; +import net.minecraft.world.Nameable; import net.minecraft.world.damagesource.DamageSource; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.EntityType; @@ -325,7 +329,7 @@ public class Block extends BlockBehaviour implements ItemLike { public static void dropResources(BlockState state, LevelAccessor world, BlockPos pos, @Nullable BlockEntity blockEntity) { if (world instanceof ServerLevel) { Block.getDrops(state, (ServerLevel) world, pos, blockEntity).forEach((itemstack) -> { - Block.popResource((ServerLevel) world, pos, itemstack); + Block.popResource((ServerLevel) world, pos, applyDisplayNameAndLoreFromTile(itemstack, blockEntity)); // Purpur }); state.spawnAfterBreak((ServerLevel) world, pos, ItemStack.EMPTY); } @@ -341,7 +345,7 @@ public class Block extends BlockBehaviour implements ItemLike { io.papermc.paper.event.block.BlockBreakBlockEvent event = new io.papermc.paper.event.block.BlockBreakBlockEvent(org.bukkit.craftbukkit.block.CraftBlock.at(world, pos), org.bukkit.craftbukkit.block.CraftBlock.at(world, source), items); event.callEvent(); for (var drop : event.getDrops()) { - popResource(world.getMinecraftWorld(), pos, org.bukkit.craftbukkit.inventory.CraftItemStack.asNMSCopy(drop)); + popResource(world.getMinecraftWorld(), pos, applyDisplayNameAndLoreFromTile(org.bukkit.craftbukkit.inventory.CraftItemStack.asNMSCopy(drop), blockEntity)); // Purpur } state.spawnAfterBreak(world.getMinecraftWorld(), pos, ItemStack.EMPTY); } @@ -352,13 +356,53 @@ public class Block extends BlockBehaviour implements ItemLike { public static void dropResources(BlockState state, Level world, BlockPos pos, @Nullable BlockEntity blockEntity, Entity entity, ItemStack stack) { if (world instanceof ServerLevel) { Block.getDrops(state, (ServerLevel) world, pos, blockEntity, entity, stack).forEach((itemstack1) -> { - Block.popResource(world, pos, itemstack1); + Block.popResource(world, pos, applyDisplayNameAndLoreFromTile(itemstack1, blockEntity)); // Purpur }); state.spawnAfterBreak((ServerLevel) world, pos, stack); } } + // Purpur start + private static ItemStack applyDisplayNameAndLoreFromTile(ItemStack stack, BlockEntity blockEntity) { + if (stack.getItem() instanceof BlockItem) { + if (blockEntity != null && blockEntity.getLevel() instanceof ServerLevel && blockEntity.getLevel().purpurConfig.persistentTileEntityDisplayNames) { + String name = blockEntity.getPersistentDisplayName(); + ListTag lore = blockEntity.getPersistentLore(); + if (blockEntity instanceof Nameable) { + Nameable namedTile = (Nameable) blockEntity; + if (namedTile.hasCustomName()) { + name = Component.Serializer.toJson(namedTile.getCustomName()); + } + } + + if (name != null || lore != null) { + CompoundTag display = stack.getTagElement("display"); + if (display == null) { + display = new CompoundTag(); + } + + if (name != null) { + display.put("Name", StringTag.valueOf(name)); + } + if (lore != null) { + display.put("Lore", lore); + } + + CompoundTag tag = stack.getTag(); + if (tag == null) { + tag = new CompoundTag(); + } + tag.put("display", display); + + stack.setTag(tag); + } + } + } + return stack; + } + // Purpur end + public static void popResource(Level world, BlockPos pos, ItemStack stack) { float f = EntityType.ITEM.getHeight() / 2.0F; // Paper start - don't convert potentially massive numbers to floats diff --git a/src/main/java/net/minecraft/world/level/block/entity/BlockEntity.java b/src/main/java/net/minecraft/world/level/block/entity/BlockEntity.java index d62181bd8bccfcfdd7da8f635bdf7ebc36294705..0fa91bd87dfec9cf8311bcbd5125caf6e308e90e 100644 --- a/src/main/java/net/minecraft/world/level/block/entity/BlockEntity.java +++ b/src/main/java/net/minecraft/world/level/block/entity/BlockEntity.java @@ -6,6 +6,8 @@ import net.minecraft.CrashReportCategory; import net.minecraft.core.BlockPos; import net.minecraft.core.Registry; import net.minecraft.nbt.CompoundTag; +import net.minecraft.nbt.ListTag; +import net.minecraft.nbt.StringTag; import net.minecraft.network.protocol.Packet; import net.minecraft.network.protocol.game.ClientGamePacketListener; import net.minecraft.resources.ResourceLocation; @@ -74,10 +76,27 @@ public abstract class BlockEntity { if (persistentDataTag instanceof CompoundTag) { this.persistentDataContainer.putAll((CompoundTag) persistentDataTag); } + // Purpur start + if (nbt.contains("Purpur.persistentDisplayName")) { + this.persistentDisplayName = nbt.getString("Purpur.persistentDisplayName"); + } + if (nbt.contains("Purpur.persistentLore")) { + this.persistentLore = nbt.getList("Purpur.persistentLore", 8); + } + // Purpur end } // CraftBukkit end - protected void saveAdditional(CompoundTag nbt) {} + protected void saveAdditional(CompoundTag nbt) { + // Purpur start + if (this.persistentDisplayName != null) { + nbt.put("Purpur.persistentDisplayName", StringTag.valueOf(this.persistentDisplayName)); + } + if (this.persistentLore != null) { + nbt.put("Purpur.persistentLore", this.persistentLore); + } + // Purpur end + } public final CompoundTag saveWithFullMetadata() { CompoundTag nbttagcompound = this.saveWithoutMetadata(); @@ -256,4 +275,25 @@ public abstract class BlockEntity { return null; } // CraftBukkit end + + // Purpur start + private String persistentDisplayName = null; + private ListTag persistentLore = null; + + public void setPersistentDisplayName(String json) { + this.persistentDisplayName = json; + } + + public void setPersistentLore(ListTag lore) { + this.persistentLore = lore; + } + + public String getPersistentDisplayName() { + return this.persistentDisplayName; + } + + public ListTag getPersistentLore() { + return this.persistentLore; + } + // Purpur end } diff --git a/src/main/java/org/purpurmc/purpur/PurpurWorldConfig.java b/src/main/java/org/purpurmc/purpur/PurpurWorldConfig.java index e953cba9d472ade2261011b8e5f8f340ca0c3b4e..90d31d52f7b2c8933c6703ab6d621b48ef801df0 100644 --- a/src/main/java/org/purpurmc/purpur/PurpurWorldConfig.java +++ b/src/main/java/org/purpurmc/purpur/PurpurWorldConfig.java @@ -124,6 +124,7 @@ public class PurpurWorldConfig { public boolean milkCuresBadOmen = true; public boolean noteBlockIgnoreAbove = false; public boolean persistentDroppableEntityDisplayNames = false; + public boolean persistentTileEntityDisplayNames = false; public boolean projectilesBypassMobGriefing = false; public boolean tickFluids = true; public double mobsBlindnessMultiplier = 1; @@ -147,6 +148,7 @@ public class PurpurWorldConfig { imposeTeleportRestrictionsOnGateways = getBoolean("gameplay-mechanics.impose-teleport-restrictions-on-gateways", imposeTeleportRestrictionsOnGateways); milkCuresBadOmen = getBoolean("gameplay-mechanics.milk-cures-bad-omen", milkCuresBadOmen); noteBlockIgnoreAbove = getBoolean("gameplay-mechanics.note-block-ignore-above", noteBlockIgnoreAbove); + persistentTileEntityDisplayNames = getBoolean("gameplay-mechanics.persistent-tileentity-display-names-and-lore", persistentTileEntityDisplayNames); persistentDroppableEntityDisplayNames = getBoolean("gameplay-mechanics.persistent-droppable-entity-display-names", persistentDroppableEntityDisplayNames); projectilesBypassMobGriefing = getBoolean("gameplay-mechanics.projectiles-bypass-mob-griefing", projectilesBypassMobGriefing); tickFluids = getBoolean("gameplay-mechanics.tick-fluids", tickFluids);