From 4f3c41def4c93784f1a49dda1043bf1bee96c930 Mon Sep 17 00:00:00 2001 From: 17146 <1714673995@qq.com> Date: Sun, 13 Jul 2025 23:38:00 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E6=95=B4=E5=B9=B8=E8=BF=90=E9=9B=86?= =?UTF-8?q?=E8=A3=85=E7=AE=B1=E7=9A=84=E8=A7=92=E6=A0=87=E5=AD=98=E5=82=A8?= =?UTF-8?q?=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../loot_table/blocks/lucky_container.json | 26 +++++++++++++++++++ .../entity/LuckyContainerBlockEntity.java | 11 ++++++++ .../LuckyContainerItemDecorator.java | 7 +++-- .../datagen/ModBlockLootProvider.java | 5 ++-- .../container/LuckyContainerBlockItem.java | 7 ++--- 5 files changed, 47 insertions(+), 9 deletions(-) diff --git a/src/generated/resources/data/superbwarfare/loot_table/blocks/lucky_container.json b/src/generated/resources/data/superbwarfare/loot_table/blocks/lucky_container.json index d2dd041f2..ac106f1da 100644 --- a/src/generated/resources/data/superbwarfare/loot_table/blocks/lucky_container.json +++ b/src/generated/resources/data/superbwarfare/loot_table/blocks/lucky_container.json @@ -1,4 +1,30 @@ { "type": "minecraft:block", + "pools": [ + { + "bonus_rolls": 0.0, + "conditions": [ + { + "condition": "minecraft:survives_explosion" + } + ], + "entries": [ + { + "type": "minecraft:item", + "name": "superbwarfare:lucky_container" + } + ], + "functions": [ + { + "function": "minecraft:copy_components", + "include": [ + "minecraft:block_entity_data" + ], + "source": "block_entity" + } + ], + "rolls": 1.0 + } + ], "random_sequence": "superbwarfare:blocks/lucky_container" } \ No newline at end of file diff --git a/src/main/java/com/atsuishio/superbwarfare/block/entity/LuckyContainerBlockEntity.java b/src/main/java/com/atsuishio/superbwarfare/block/entity/LuckyContainerBlockEntity.java index e721cb42b..22862d01e 100644 --- a/src/main/java/com/atsuishio/superbwarfare/block/entity/LuckyContainerBlockEntity.java +++ b/src/main/java/com/atsuishio/superbwarfare/block/entity/LuckyContainerBlockEntity.java @@ -34,6 +34,8 @@ public class LuckyContainerBlockEntity extends BlockEntity implements GeoBlockEn @Nullable public ResourceLocation location; + @Nullable + public ResourceLocation icon; public int tick = 0; private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this); @@ -109,6 +111,9 @@ public class LuckyContainerBlockEntity extends BlockEntity implements GeoBlockEn if (tag.contains("Location", 8)) { this.location = ResourceLocation.parse(tag.getString("Location")); } + if (tag.contains("Icon", 8)) { + this.icon = ResourceLocation.parse(tag.getString("Icon")); + } this.tick = tag.getInt("Tick"); } @@ -119,6 +124,9 @@ public class LuckyContainerBlockEntity extends BlockEntity implements GeoBlockEn if (this.location != null) { tag.putString("Location", this.location.toString()); } + if (this.icon != null) { + tag.putString("Icon", this.icon.toString()); + } tag.putInt("Tick", this.tick); } @@ -139,6 +147,9 @@ public class LuckyContainerBlockEntity extends BlockEntity implements GeoBlockEn if (this.location != null) { tag.putString("Location", this.location.toString()); } + if (this.icon != null) { + tag.putString("Icon", this.icon.toString()); + } BlockItem.setBlockEntityData(stack, this.getType(), tag); } } diff --git a/src/main/java/com/atsuishio/superbwarfare/client/decorator/LuckyContainerItemDecorator.java b/src/main/java/com/atsuishio/superbwarfare/client/decorator/LuckyContainerItemDecorator.java index 877e8e286..d3d268507 100644 --- a/src/main/java/com/atsuishio/superbwarfare/client/decorator/LuckyContainerItemDecorator.java +++ b/src/main/java/com/atsuishio/superbwarfare/client/decorator/LuckyContainerItemDecorator.java @@ -2,9 +2,9 @@ package com.atsuishio.superbwarfare.client.decorator; import com.atsuishio.superbwarfare.client.RenderHelper; import com.atsuishio.superbwarfare.item.common.container.LuckyContainerBlockItem; -import com.atsuishio.superbwarfare.tools.NBTTool; import net.minecraft.client.gui.Font; import net.minecraft.client.gui.GuiGraphics; +import net.minecraft.core.component.DataComponents; import net.minecraft.resources.ResourceLocation; import net.minecraft.world.item.ItemStack; import net.neoforged.api.distmarker.Dist; @@ -20,7 +20,10 @@ public class LuckyContainerItemDecorator implements IItemDecorator { @ParametersAreNonnullByDefault public boolean render(GuiGraphics guiGraphics, Font font, ItemStack stack, int xOffset, int yOffset) { if (!(stack.getItem() instanceof LuckyContainerBlockItem)) return false; - var tag = NBTTool.getTag(stack); + var data = stack.get(DataComponents.BLOCK_ENTITY_DATA); + if (data == null) return false; + + var tag = data.copyTag(); if (!tag.contains("Icon")) return false; var iconTag = tag.getString("Icon"); ResourceLocation icon = ResourceLocation.tryParse(iconTag); diff --git a/src/main/java/com/atsuishio/superbwarfare/datagen/ModBlockLootProvider.java b/src/main/java/com/atsuishio/superbwarfare/datagen/ModBlockLootProvider.java index 00d22b071..a0d79edf5 100644 --- a/src/main/java/com/atsuishio/superbwarfare/datagen/ModBlockLootProvider.java +++ b/src/main/java/com/atsuishio/superbwarfare/datagen/ModBlockLootProvider.java @@ -73,8 +73,9 @@ public class ModBlockLootProvider extends BlockLootSubProvider { .include(DataComponents.CONTAINER_LOOT) ) ))); - // TODO 改成正确的loot table - this.add(ModBlocks.LUCKY_CONTAINER.get(), noDrop()); + this.add(ModBlocks.LUCKY_CONTAINER.get(), LootTable.lootTable().withPool(this.applyExplosionCondition(ModBlocks.LUCKY_CONTAINER.get(), + LootPool.lootPool().setRolls(ConstantValue.exactly(1.0F)).add(LootItem.lootTableItem(ModBlocks.LUCKY_CONTAINER.get())) + .apply(CopyComponentsFunction.copyComponents(CopyComponentsFunction.Source.BLOCK_ENTITY).include(DataComponents.BLOCK_ENTITY_DATA))))); } @Override diff --git a/src/main/java/com/atsuishio/superbwarfare/item/common/container/LuckyContainerBlockItem.java b/src/main/java/com/atsuishio/superbwarfare/item/common/container/LuckyContainerBlockItem.java index bd653aa46..929a41d64 100644 --- a/src/main/java/com/atsuishio/superbwarfare/item/common/container/LuckyContainerBlockItem.java +++ b/src/main/java/com/atsuishio/superbwarfare/item/common/container/LuckyContainerBlockItem.java @@ -5,7 +5,6 @@ import com.atsuishio.superbwarfare.client.renderer.item.LuckyContainerBlockItemR import com.atsuishio.superbwarfare.init.ModBlockEntities; import com.atsuishio.superbwarfare.init.ModBlocks; import com.atsuishio.superbwarfare.init.ModItems; -import com.atsuishio.superbwarfare.tools.NBTTool; import net.minecraft.client.renderer.BlockEntityWithoutLevelRenderer; import net.minecraft.nbt.CompoundTag; import net.minecraft.resources.ResourceLocation; @@ -103,12 +102,10 @@ public class LuckyContainerBlockItem extends BlockItem implements GeoItem { CompoundTag tag = new CompoundTag(); tag.putString("Location", location.toString()); - BlockItem.setBlockEntityData(stack, ModBlockEntities.LUCKY_CONTAINER.get(), tag); if (icon != null) { - var iconTag = NBTTool.getTag(stack); - iconTag.putString("Icon", icon.toString()); - NBTTool.saveTag(stack, iconTag); + tag.putString("Icon", icon.toString()); } + BlockItem.setBlockEntityData(stack, ModBlockEntities.LUCKY_CONTAINER.get(), tag); return stack; }