From a66d34468cd82643512a98bae2d30496766e282e Mon Sep 17 00:00:00 2001 From: 17146 <1714673995@qq.com> Date: Sun, 4 May 2025 18:29:53 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=B0=8F=E9=9B=86=E8=A3=85?= =?UTF-8?q?=E7=AE=B1=E7=9A=84=E6=98=BE=E7=A4=BA=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../atsuishio/superbwarfare/block/ContainerBlock.java | 2 -- .../superbwarfare/block/SmallContainerBlock.java | 10 ++++------ .../superbwarfare/item/SmallContainerBlockItem.java | 11 +++-------- 3 files changed, 7 insertions(+), 16 deletions(-) diff --git a/src/main/java/com/atsuishio/superbwarfare/block/ContainerBlock.java b/src/main/java/com/atsuishio/superbwarfare/block/ContainerBlock.java index f48d515e5..7d7c19de6 100644 --- a/src/main/java/com/atsuishio/superbwarfare/block/ContainerBlock.java +++ b/src/main/java/com/atsuishio/superbwarfare/block/ContainerBlock.java @@ -212,7 +212,6 @@ public class ContainerBlock extends BaseEntityBlock { return this.defaultBlockState().setValue(FACING, context.getHorizontalDirection().getOpposite()).setValue(OPENED, false); } - @Override @ParametersAreNonnullByDefault public @NotNull ItemStack getCloneItemStack(BlockState state, HitResult target, LevelReader level, BlockPos pos, Player player) { @@ -220,6 +219,5 @@ public class ContainerBlock extends BaseEntityBlock { level.getBlockEntity(pos, ModBlockEntities.CONTAINER.get()).ifPresent((blockEntity) -> blockEntity.saveToItem(itemStack, level.registryAccess())); return itemStack; } - } diff --git a/src/main/java/com/atsuishio/superbwarfare/block/SmallContainerBlock.java b/src/main/java/com/atsuishio/superbwarfare/block/SmallContainerBlock.java index d6d837180..613f93f45 100644 --- a/src/main/java/com/atsuishio/superbwarfare/block/SmallContainerBlock.java +++ b/src/main/java/com/atsuishio/superbwarfare/block/SmallContainerBlock.java @@ -10,7 +10,6 @@ import net.minecraft.ChatFormatting; import net.minecraft.core.BlockPos; import net.minecraft.core.Direction; import net.minecraft.core.component.DataComponents; -import net.minecraft.nbt.CompoundTag; import net.minecraft.network.chat.Component; import net.minecraft.sounds.SoundSource; import net.minecraft.world.InteractionResult; @@ -90,10 +89,9 @@ public class SmallContainerBlock extends BaseEntityBlock { public void appendHoverText(ItemStack stack, Item.TooltipContext context, List tooltipComponents, TooltipFlag tooltipFlag) { super.appendHoverText(stack, context, tooltipComponents, tooltipFlag); - var data = stack.get(DataComponents.BLOCK_ENTITY_DATA); - CompoundTag tag = data != null ? data.copyTag() : null; - if (tag != null) { - String lootTable = tag.getString("LootTable"); + var data = stack.get(DataComponents.CONTAINER_LOOT); + if (data != null) { + String lootTable = data.lootTable().location().toString(); if (lootTable.startsWith(Mod.MODID + ":containers/")) { var split = lootTable.split(Mod.MODID + ":containers/"); if (split.length == 2) { @@ -101,7 +99,7 @@ public class SmallContainerBlock extends BaseEntityBlock { } tooltipComponents.add(Component.translatable("des.superbwarfare.small_container." + lootTable).withStyle(ChatFormatting.GRAY)); } else { - long seed = tag.getLong("LootTableSeed"); + long seed = data.seed(); if (seed != 0 && seed % 205 == 0) { tooltipComponents.add(Component.translatable("des.superbwarfare.small_container.special").withStyle(ChatFormatting.GRAY)); } else { diff --git a/src/main/java/com/atsuishio/superbwarfare/item/SmallContainerBlockItem.java b/src/main/java/com/atsuishio/superbwarfare/item/SmallContainerBlockItem.java index 74cd7f0a3..952a80ce2 100644 --- a/src/main/java/com/atsuishio/superbwarfare/item/SmallContainerBlockItem.java +++ b/src/main/java/com/atsuishio/superbwarfare/item/SmallContainerBlockItem.java @@ -2,16 +2,16 @@ package com.atsuishio.superbwarfare.item; import com.atsuishio.superbwarfare.Mod; import com.atsuishio.superbwarfare.client.renderer.item.SmallContainerBlockItemRenderer; -import com.atsuishio.superbwarfare.init.ModBlockEntities; import com.atsuishio.superbwarfare.init.ModBlocks; import com.atsuishio.superbwarfare.init.ModItems; import net.minecraft.client.renderer.BlockEntityWithoutLevelRenderer; +import net.minecraft.core.component.DataComponents; import net.minecraft.core.registries.Registries; -import net.minecraft.nbt.CompoundTag; import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.item.BlockItem; import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.component.SeededContainerLoot; import net.minecraft.world.level.storage.loot.LootTable; import net.neoforged.bus.api.SubscribeEvent; import net.neoforged.fml.common.EventBusSubscriber; @@ -77,12 +77,7 @@ public class SmallContainerBlockItem extends BlockItem implements GeoItem { public static ItemStack createInstance(ResourceKey lootTable, long lootTableSeed) { ItemStack stack = new ItemStack(ModBlocks.SMALL_CONTAINER.get()); - CompoundTag tag = new CompoundTag(); - tag.putString("LootTable", lootTable.location().toString()); - if (lootTableSeed != 0L) { - tag.putLong("LootTableSeed", lootTableSeed); - } - BlockItem.setBlockEntityData(stack, ModBlockEntities.SMALL_CONTAINER.get(), tag); + stack.set(DataComponents.CONTAINER_LOOT, new SeededContainerLoot(lootTable, lootTableSeed)); return stack; } }