mirror of
https://github.com/PurpurMC/Purpur.git
synced 2026-02-17 16:37:43 +01:00
165 lines
9.4 KiB
Diff
165 lines
9.4 KiB
Diff
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 33d9b5fb6897fefb0f7b9009df7339341cb42317..d58619d1d63a03598b8740dd789d4b6f2c93f8d0 100644
|
|
--- a/src/main/java/net/minecraft/world/item/BlockItem.java
|
|
+++ b/src/main/java/net/minecraft/world/item/BlockItem.java
|
|
@@ -151,7 +151,16 @@ 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.persistentTileEntityLore) {
|
|
+ BlockEntity blockEntity1 = world.getBlockEntity(pos);
|
|
+ if (blockEntity1 != null) {
|
|
+ blockEntity1.setPersistentLore(stack.getOrDefault(DataComponents.LORE, net.minecraft.world.item.component.ItemLore.EMPTY));
|
|
+ }
|
|
+ }
|
|
+ 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 c35513d05bd70e296a0e5ffe92c36016a13173b4..d324eefa500c528776df698e7f9dcdeab76da167 100644
|
|
--- a/src/main/java/net/minecraft/world/level/block/Block.java
|
|
+++ b/src/main/java/net/minecraft/world/level/block/Block.java
|
|
@@ -303,7 +303,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, applyLoreFromTile(itemstack, blockEntity)); // Purpur
|
|
});
|
|
state.spawnAfterBreak((ServerLevel) world, pos, ItemStack.EMPTY, true);
|
|
}
|
|
@@ -322,7 +322,7 @@ public class Block extends BlockBehaviour implements ItemLike {
|
|
event.setExpToDrop(block.getExpDrop(state, serverLevel, pos, net.minecraft.world.item.ItemStack.EMPTY, true)); // Paper - Properly handle xp dropping
|
|
event.callEvent();
|
|
for (org.bukkit.inventory.ItemStack drop : event.getDrops()) {
|
|
- popResource(serverLevel, pos, org.bukkit.craftbukkit.inventory.CraftItemStack.asNMSCopy(drop));
|
|
+ popResource(serverLevel, pos, applyLoreFromTile(org.bukkit.craftbukkit.inventory.CraftItemStack.asNMSCopy(drop), blockEntity)); // Purpur
|
|
}
|
|
state.spawnAfterBreak(serverLevel, pos, ItemStack.EMPTY, false); // Paper - Properly handle xp dropping
|
|
block.popExperience(serverLevel, pos, event.getExpToDrop()); // Paper - Properly handle xp dropping
|
|
@@ -339,13 +339,32 @@ public class Block extends BlockBehaviour implements ItemLike {
|
|
// Paper end - Properly handle xp dropping
|
|
if (world instanceof ServerLevel) {
|
|
Block.getDrops(state, (ServerLevel) world, pos, blockEntity, entity, tool).forEach((itemstack1) -> {
|
|
- Block.popResource(world, pos, itemstack1);
|
|
+ Block.popResource(world, pos, applyLoreFromTile(itemstack1, blockEntity)); // Purpur
|
|
});
|
|
state.spawnAfterBreak((ServerLevel) world, pos, tool, dropExperience); // Paper - Properly handle xp dropping
|
|
}
|
|
|
|
}
|
|
|
|
+ // Purpur start
|
|
+ private static ItemStack applyLoreFromTile(ItemStack stack, @Nullable BlockEntity blockEntity) {
|
|
+ if (stack.getItem() instanceof BlockItem) {
|
|
+ if (blockEntity != null && blockEntity.getLevel() instanceof ServerLevel) {
|
|
+ net.minecraft.world.item.component.ItemLore lore = blockEntity.getPersistentLore();
|
|
+ net.minecraft.core.component.DataComponentPatch.Builder builder = net.minecraft.core.component.DataComponentPatch.builder();
|
|
+ if (blockEntity.getLevel().purpurConfig.persistentTileEntityLore && lore != null) {
|
|
+ builder.set(net.minecraft.core.component.DataComponents.LORE, lore);
|
|
+ }
|
|
+ if (!blockEntity.getLevel().purpurConfig.persistentTileEntityDisplayName) {
|
|
+ builder.remove(net.minecraft.core.component.DataComponents.CUSTOM_NAME);
|
|
+ }
|
|
+ stack.applyComponents(builder.build());
|
|
+ }
|
|
+ }
|
|
+ return stack;
|
|
+ }
|
|
+ // Purpur end
|
|
+
|
|
public static void popResource(Level world, BlockPos pos, ItemStack stack) {
|
|
double d0 = (double) EntityType.ITEM.getHeight() / 2.0D;
|
|
double d1 = (double) pos.getX() + 0.5D + Mth.nextDouble(world.random, -0.25D, 0.25D);
|
|
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 1f929b467a0ece3143af58a657cf5983c07a8d51..eaa6ece956f90632831f0558924eaf18680a252b 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
|
|
@@ -95,6 +95,12 @@ public abstract class BlockEntity {
|
|
if (persistentDataTag instanceof CompoundTag) {
|
|
this.persistentDataContainer.putAll((CompoundTag) persistentDataTag);
|
|
}
|
|
+ // Purpur start
|
|
+ if (nbt.contains("Purpur.persistentLore")) {
|
|
+ net.minecraft.world.item.component.ItemLore.CODEC.decode(net.minecraft.nbt.NbtOps.INSTANCE, nbt.getCompound("Purpur.persistentLore")).result()
|
|
+ .ifPresent(tag -> this.persistentLore = tag.getFirst());
|
|
+ }
|
|
+ // Purpur end
|
|
}
|
|
// CraftBukkit end
|
|
|
|
@@ -111,6 +117,15 @@ public abstract class BlockEntity {
|
|
this.loadAdditional(nbt, registries);
|
|
}
|
|
|
|
+ // Purpur start
|
|
+ protected void saveAdditional(CompoundTag nbt) {
|
|
+ if (this.persistentLore != null) {
|
|
+ net.minecraft.world.item.component.ItemLore.CODEC.encodeStart(net.minecraft.nbt.NbtOps.INSTANCE, this.persistentLore).result()
|
|
+ .ifPresent(tag -> nbt.put("Purpur.persistentLore", tag));
|
|
+ }
|
|
+ }
|
|
+ // Purpur end
|
|
+
|
|
protected void saveAdditional(CompoundTag nbt, HolderLookup.Provider registries) {}
|
|
|
|
public final CompoundTag saveWithFullMetadata(HolderLookup.Provider registries) {
|
|
@@ -419,4 +434,16 @@ public abstract class BlockEntity {
|
|
|
|
<T> T getOrDefault(DataComponentType<? extends T> type, T fallback);
|
|
}
|
|
+ // Purpur start
|
|
+ @Nullable
|
|
+ private net.minecraft.world.item.component.ItemLore persistentLore = null;
|
|
+
|
|
+ public void setPersistentLore(net.minecraft.world.item.component.ItemLore lore) {
|
|
+ this.persistentLore = lore;
|
|
+ }
|
|
+
|
|
+ public @org.jetbrains.annotations.Nullable net.minecraft.world.item.component.ItemLore 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 750e2e857c30b3bf9ae02bf038b26fd74246ce51..70807e2540ac7f7d376df9b41b0b4e5fd3092d60 100644
|
|
--- a/src/main/java/org/purpurmc/purpur/PurpurWorldConfig.java
|
|
+++ b/src/main/java/org/purpurmc/purpur/PurpurWorldConfig.java
|
|
@@ -142,6 +142,8 @@ public class PurpurWorldConfig {
|
|
public boolean mobsIgnoreRails = false;
|
|
public boolean rainStopsAfterSleep = true;
|
|
public boolean thunderStopsAfterSleep = true;
|
|
+ public boolean persistentTileEntityLore = false;
|
|
+ public boolean persistentTileEntityDisplayName = true;
|
|
private void miscGameplayMechanicsSettings() {
|
|
useBetterMending = getBoolean("gameplay-mechanics.use-better-mending", useBetterMending);
|
|
alwaysTameInCreative = getBoolean("gameplay-mechanics.always-tame-in-creative", alwaysTameInCreative);
|
|
@@ -166,6 +168,14 @@ public class PurpurWorldConfig {
|
|
mobsIgnoreRails = getBoolean("gameplay-mechanics.mobs-ignore-rails", mobsIgnoreRails);
|
|
rainStopsAfterSleep = getBoolean("gameplay-mechanics.rain-stops-after-sleep", rainStopsAfterSleep);
|
|
thunderStopsAfterSleep = getBoolean("gameplay-mechanics.thunder-stops-after-sleep", thunderStopsAfterSleep);
|
|
+ if (PurpurConfig.version < 35) {
|
|
+ boolean oldVal = getBoolean("gameplay-mechanics.persistent-tileentity-display-names-and-lore", persistentTileEntityLore);
|
|
+ set("gameplay-mechanics.persistent-tileentity-display-names-and-lore", null);
|
|
+ set("gameplay-mechanics.persistent-tileentity-lore", oldVal);
|
|
+ set("gameplay-mechanics.persistent-tileentity-display-name", !oldVal);
|
|
+ }
|
|
+ persistentTileEntityLore = getBoolean("gameplay-mechanics.persistent-tileentity-lore", persistentTileEntityLore);
|
|
+ persistentTileEntityDisplayName = getBoolean("gameplay-mechanics.persistent-tileentity-display-name", persistentTileEntityDisplayName);
|
|
}
|
|
|
|
public int daytimeTicks = 12000;
|