From 1008a673ce1cd0e66ea146aebd295d77563b8b11 Mon Sep 17 00:00:00 2001 From: 17146 <1714673995@qq.com> Date: Thu, 28 Nov 2024 20:08:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0blockitem=E7=9A=84nbt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../59eb3dbb5f86130e09b3c62d89b9525ee01cf52d | 4 +-- .../loot_tables/blocks/container.json | 9 +++++-- .../superbwarfare/block/ContainerBlock.java | 11 +++++--- .../block/entity/ContainerBlockEntity.java | 19 +++++++++++--- .../datagen/ModBlockLootProvider.java | 3 ++- .../datagen/ModBlockTagProvider.java | 2 +- .../atsuishio/superbwarfare/init/ModTabs.java | 5 +++- .../item/ContainerBlockItem.java | 26 +++++++++++++++++++ 8 files changed, 65 insertions(+), 14 deletions(-) diff --git a/src/generated/resources/.cache/59eb3dbb5f86130e09b3c62d89b9525ee01cf52d b/src/generated/resources/.cache/59eb3dbb5f86130e09b3c62d89b9525ee01cf52d index 417ea1a2e..e2ef320bc 100644 --- a/src/generated/resources/.cache/59eb3dbb5f86130e09b3c62d89b9525ee01cf52d +++ b/src/generated/resources/.cache/59eb3dbb5f86130e09b3c62d89b9525ee01cf52d @@ -1,7 +1,7 @@ -// 1.20.1 2024-11-28T19:06:08.9130891 Loot Tables +// 1.20.1 2024-11-28T19:45:16.1283469 Loot Tables 98386b2e65b2afb7cf5b0cabf6a7b0f062c1e640 data/superbwarfare/loot_tables/blocks/barbed_wire.json 811e69929a14e6736015849c220100bb58d914c9 data/superbwarfare/loot_tables/blocks/cemented_carbide_block.json -92190e844ccc6a9e63e4c063ee3a0932f722156a data/superbwarfare/loot_tables/blocks/container.json +8ed3f1d518c86fe6ed14f14f3121c2e24e109269 data/superbwarfare/loot_tables/blocks/container.json dabbe17417c944567dc6dfcdcba32b8377c24f69 data/superbwarfare/loot_tables/blocks/deepslate_galena_ore.json a7c1af3e194d35a64544902468bd964f45f2f9ef data/superbwarfare/loot_tables/blocks/deepslate_scheelite_ore.json 76fa64ff714374c1655f4d11776fda132287a014 data/superbwarfare/loot_tables/blocks/deepslate_silver_ore.json diff --git a/src/generated/resources/data/superbwarfare/loot_tables/blocks/container.json b/src/generated/resources/data/superbwarfare/loot_tables/blocks/container.json index a9d6f59c6..e98abdf22 100644 --- a/src/generated/resources/data/superbwarfare/loot_tables/blocks/container.json +++ b/src/generated/resources/data/superbwarfare/loot_tables/blocks/container.json @@ -20,8 +20,13 @@ "ops": [ { "op": "replace", - "source": "Test", - "target": "BlockEntityTag.Test" + "source": "Entity", + "target": "BlockEntityTag.Entity" + }, + { + "op": "replace", + "source": "EntityType", + "target": "BlockEntityTag.EntityType" } ], "source": "block_entity" diff --git a/src/main/java/com/atsuishio/superbwarfare/block/ContainerBlock.java b/src/main/java/com/atsuishio/superbwarfare/block/ContainerBlock.java index 769fb0076..fbb806302 100644 --- a/src/main/java/com/atsuishio/superbwarfare/block/ContainerBlock.java +++ b/src/main/java/com/atsuishio/superbwarfare/block/ContainerBlock.java @@ -24,7 +24,7 @@ import javax.annotation.Nullable; import java.util.List; @SuppressWarnings("deprecation") -public class ContainerBlock extends BaseEntityBlock implements SimpleWaterloggedBlock, EntityBlock { +public class ContainerBlock extends BaseEntityBlock { public static final DirectionProperty FACING = HorizontalDirectionalBlock.FACING; public static final BooleanProperty OPENED = BooleanProperty.create("opened"); @@ -35,12 +35,15 @@ public class ContainerBlock extends BaseEntityBlock implements SimpleWaterlogged } @Override - public void appendHoverText(ItemStack pStack, @org.jetbrains.annotations.Nullable BlockGetter pLevel, List pTooltip, TooltipFlag pFlag) { + public void appendHoverText(ItemStack pStack, @Nullable BlockGetter pLevel, List pTooltip, TooltipFlag pFlag) { super.appendHoverText(pStack, pLevel, pTooltip, pFlag); CompoundTag compoundtag = BlockItem.getBlockEntityData(pStack); if (compoundtag != null) { - if (compoundtag.contains("Test")) { - pTooltip.add(Component.literal("Test: " + compoundtag.getInt("Test"))); + if (compoundtag.contains("Entity")) { + pTooltip.add(Component.literal("Entity: " + compoundtag.getString("Entity"))); + } + if (compoundtag.contains("EntityType")) { + pTooltip.add(Component.literal("EntityType: " + compoundtag.getString("EntityType"))); } } } diff --git a/src/main/java/com/atsuishio/superbwarfare/block/entity/ContainerBlockEntity.java b/src/main/java/com/atsuishio/superbwarfare/block/entity/ContainerBlockEntity.java index e94f5ed29..48ef19a67 100644 --- a/src/main/java/com/atsuishio/superbwarfare/block/entity/ContainerBlockEntity.java +++ b/src/main/java/com/atsuishio/superbwarfare/block/entity/ContainerBlockEntity.java @@ -4,6 +4,8 @@ import com.atsuishio.superbwarfare.init.ModBlockEntities; import net.minecraft.core.BlockPos; import net.minecraft.nbt.CompoundTag; import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket; +import net.minecraft.world.entity.Entity; +import net.minecraft.world.entity.EntityType; import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.state.BlockState; import software.bernie.geckolib.animatable.GeoBlockEntity; @@ -17,7 +19,8 @@ import software.bernie.geckolib.util.GeckoLibUtil; public class ContainerBlockEntity extends BlockEntity implements GeoBlockEntity { - public int test = 0; + public EntityType entityType; + public Entity entity = null; private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this); @@ -42,13 +45,23 @@ public class ContainerBlockEntity extends BlockEntity implements GeoBlockEntity @Override public void load(CompoundTag compound) { super.load(compound); - this.test = compound.getInt("Test"); + if (compound.contains("Entity")) { + entity.deserializeNBT(compound.getCompound("Entity")); + } + if (compound.contains("EntityType")) { + this.entityType = EntityType.byString(compound.getString("EntityType")).orElse(null); + } } @Override public void saveAdditional(CompoundTag compound) { super.saveAdditional(compound); - compound.putInt("Test", this.test); + if (this.entity != null) { + compound.put("Entity", this.entity.serializeNBT()); + } + if (this.entityType != null) { + compound.putString("EntityType", EntityType.getKey(this.entityType).toString()); + } } @Override diff --git a/src/main/java/com/atsuishio/superbwarfare/datagen/ModBlockLootProvider.java b/src/main/java/com/atsuishio/superbwarfare/datagen/ModBlockLootProvider.java index f5818503d..ce31c7856 100644 --- a/src/main/java/com/atsuishio/superbwarfare/datagen/ModBlockLootProvider.java +++ b/src/main/java/com/atsuishio/superbwarfare/datagen/ModBlockLootProvider.java @@ -43,7 +43,8 @@ public class ModBlockLootProvider extends BlockLootSubProvider { this.add(ModBlocks.CONTAINER.get(), LootTable.lootTable().withPool(this.applyExplosionCondition(ModBlocks.CONTAINER.get(), LootPool.lootPool().setRolls(ConstantValue.exactly(1.0F)).add(LootItem.lootTableItem(ModBlocks.CONTAINER.get())) - .apply(CopyNbtFunction.copyData(ContextNbtProvider.BLOCK_ENTITY).copy("Test", "BlockEntityTag.Test"))))); + .apply(CopyNbtFunction.copyData(ContextNbtProvider.BLOCK_ENTITY).copy("Entity", "BlockEntityTag.Entity") + .copy("EntityType", "BlockEntityTag.EntityType"))))); } @Override diff --git a/src/main/java/com/atsuishio/superbwarfare/datagen/ModBlockTagProvider.java b/src/main/java/com/atsuishio/superbwarfare/datagen/ModBlockTagProvider.java index 0cd53d1f6..4e1cc507a 100644 --- a/src/main/java/com/atsuishio/superbwarfare/datagen/ModBlockTagProvider.java +++ b/src/main/java/com/atsuishio/superbwarfare/datagen/ModBlockTagProvider.java @@ -12,6 +12,7 @@ import javax.annotation.Nullable; import java.util.concurrent.CompletableFuture; public class ModBlockTagProvider extends BlockTagsProvider { + public ModBlockTagProvider(PackOutput output, CompletableFuture lookupProvider, @Nullable ExistingFileHelper existingFileHelper) { super(output, lookupProvider, ModUtils.MODID, existingFileHelper); } @@ -29,6 +30,5 @@ public class ModBlockTagProvider extends BlockTagsProvider { ModBlocks.CEMENTED_CARBIDE_BLOCK.get(), ModBlocks.SILVER_ORE.get(), ModBlocks.DEEPSLATE_SILVER_ORE.get(), ModBlocks.SILVER_BLOCK.get(), ModBlocks.JUMP_PAD.get(), ModBlocks.CONTAINER.get()); this.tag(BlockTags.MINEABLE_WITH_SHOVEL).add(ModBlocks.SANDBAG.get()); - } } diff --git a/src/main/java/com/atsuishio/superbwarfare/init/ModTabs.java b/src/main/java/com/atsuishio/superbwarfare/init/ModTabs.java index 1e0c1c38b..e6158bd9b 100644 --- a/src/main/java/com/atsuishio/superbwarfare/init/ModTabs.java +++ b/src/main/java/com/atsuishio/superbwarfare/init/ModTabs.java @@ -2,6 +2,7 @@ package com.atsuishio.superbwarfare.init; import com.atsuishio.superbwarfare.ModUtils; import com.atsuishio.superbwarfare.item.ArmorPlate; +import com.atsuishio.superbwarfare.item.ContainerBlockItem; import com.atsuishio.superbwarfare.item.gun.handgun.Glock17Item; import com.atsuishio.superbwarfare.item.gun.handgun.Glock18Item; import com.atsuishio.superbwarfare.item.gun.handgun.M1911Item; @@ -105,7 +106,9 @@ public class ModTabs { if (registryObject.get() == ModItems.ARMOR_PLATE.get()) { output.accept(ArmorPlate.getInfiniteInstance()); } - + if (registryObject.get() == ModItems.CONTAINER.get()) { + output.accept(ContainerBlockItem.createMk42Instance()); + } })) .build()); diff --git a/src/main/java/com/atsuishio/superbwarfare/item/ContainerBlockItem.java b/src/main/java/com/atsuishio/superbwarfare/item/ContainerBlockItem.java index 8b81b671c..4013634d0 100644 --- a/src/main/java/com/atsuishio/superbwarfare/item/ContainerBlockItem.java +++ b/src/main/java/com/atsuishio/superbwarfare/item/ContainerBlockItem.java @@ -1,10 +1,16 @@ package com.atsuishio.superbwarfare.item; import com.atsuishio.superbwarfare.client.renderer.block.ContainerBlockItemRenderer; +import com.atsuishio.superbwarfare.init.ModBlockEntities; import com.atsuishio.superbwarfare.init.ModBlocks; +import com.atsuishio.superbwarfare.init.ModEntities; import net.minecraft.client.renderer.BlockEntityWithoutLevelRenderer; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.world.entity.Entity; +import net.minecraft.world.entity.EntityType; import net.minecraft.world.item.BlockItem; import net.minecraft.world.item.Item; +import net.minecraft.world.item.ItemStack; import net.minecraftforge.client.extensions.common.IClientItemExtensions; import software.bernie.geckolib.animatable.GeoItem; import software.bernie.geckolib.core.animatable.instance.AnimatableInstanceCache; @@ -50,4 +56,24 @@ public class ContainerBlockItem extends BlockItem implements GeoItem { public AnimatableInstanceCache getAnimatableInstanceCache() { return this.cache; } + + public static ItemStack createInstance(Entity entity) { + ItemStack stack = new ItemStack(ModBlocks.CONTAINER.get()); + CompoundTag tag = new CompoundTag(); + tag.put("Entity", entity.serializeNBT()); + BlockItem.setBlockEntityData(stack, ModBlockEntities.CONTAINER.get(), tag); + return stack; + } + + public static ItemStack createInstance(EntityType entityType) { + ItemStack stack = new ItemStack(ModBlocks.CONTAINER.get()); + CompoundTag tag = new CompoundTag(); + tag.putString("EntityType", EntityType.getKey(entityType).toString()); + BlockItem.setBlockEntityData(stack, ModBlockEntities.CONTAINER.get(), tag); + return stack; + } + + public static ItemStack createMk42Instance() { + return createInstance(ModEntities.MK_42.get()); + } }