From fe130e8eecd4c45e85dab11bf450a0035c957d90 Mon Sep 17 00:00:00 2001 From: 17146 <1714673995@qq.com> Date: Thu, 28 Nov 2024 21:45:02 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=9B=86=E8=A3=85=E7=AE=B1?= =?UTF-8?q?=E8=8E=B7=E5=8F=96=E6=96=B9=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../block/entity/ContainerBlockEntity.java | 9 ++++++--- .../superbwarfare/entity/Mk42Entity.java | 8 +++++++- .../superbwarfare/entity/Mle1934Entity.java | 8 +++++++- .../superbwarfare/item/ContainerBlockItem.java | 18 +++++++++++++++++- 4 files changed, 37 insertions(+), 6 deletions(-) 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 5c7f71cfc..26d1bb49c 100644 --- a/src/main/java/com/atsuishio/superbwarfare/block/entity/ContainerBlockEntity.java +++ b/src/main/java/com/atsuishio/superbwarfare/block/entity/ContainerBlockEntity.java @@ -88,12 +88,15 @@ public class ContainerBlockEntity extends BlockEntity implements GeoBlockEntity @Override public void load(CompoundTag compound) { super.load(compound); - if (compound.contains("Entity")) { - entity.deserializeNBT(compound.getCompound("Entity")); - } if (compound.contains("EntityType")) { this.entityType = EntityType.byString(compound.getString("EntityType")).orElse(null); } + if (compound.contains("Entity") && this.entityType != null && this.level != null) { + this.entity = this.entityType.create(this.level); + if (entity != null) { + entity.load(compound.getCompound("Entity")); + } + } this.tick = compound.getInt("Tick"); } diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/Mk42Entity.java b/src/main/java/com/atsuishio/superbwarfare/entity/Mk42Entity.java index d14c4ea15..ee7f6b86e 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/Mk42Entity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/Mk42Entity.java @@ -1,8 +1,9 @@ package com.atsuishio.superbwarfare.entity; -import com.atsuishio.superbwarfare.init.*; import com.atsuishio.superbwarfare.config.server.ExplosionDestroyConfig; import com.atsuishio.superbwarfare.entity.projectile.CannonShellEntity; +import com.atsuishio.superbwarfare.init.*; +import com.atsuishio.superbwarfare.item.ContainerBlockItem; import com.atsuishio.superbwarfare.item.common.ammo.CannonShellItem; import com.atsuishio.superbwarfare.tools.CustomExplosion; import com.atsuishio.superbwarfare.tools.ParticleTool; @@ -159,6 +160,11 @@ public class Mk42Entity extends Entity implements GeoEntity, ICannonEntity { public InteractionResult interact(Player player, InteractionHand hand) { if (player.isShiftKeyDown()) { if (player.getMainHandItem().getItem() == ModItems.CROWBAR.get() && this.getFirstPassenger() == null) { + ItemStack stack = ContainerBlockItem.createInstance(this); + if (!player.addItem(stack)){ + player.drop(stack, false); + } + this.discard(); return InteractionResult.sidedSuccess(this.level().isClientSide()); } diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/Mle1934Entity.java b/src/main/java/com/atsuishio/superbwarfare/entity/Mle1934Entity.java index 0fbe28b87..f984aa41f 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/Mle1934Entity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/Mle1934Entity.java @@ -1,9 +1,10 @@ package com.atsuishio.superbwarfare.entity; -import com.atsuishio.superbwarfare.init.*; import com.atsuishio.superbwarfare.ModUtils; import com.atsuishio.superbwarfare.config.server.ExplosionDestroyConfig; import com.atsuishio.superbwarfare.entity.projectile.CannonShellEntity; +import com.atsuishio.superbwarfare.init.*; +import com.atsuishio.superbwarfare.item.ContainerBlockItem; import com.atsuishio.superbwarfare.item.common.ammo.CannonShellItem; import com.atsuishio.superbwarfare.tools.CustomExplosion; import com.atsuishio.superbwarfare.tools.ParticleTool; @@ -163,6 +164,11 @@ public class Mle1934Entity extends Entity implements GeoEntity, ICannonEntity { public InteractionResult interact(Player player, InteractionHand hand) { if (player.isShiftKeyDown()) { if (player.getMainHandItem().getItem() == ModItems.CROWBAR.get() && this.getFirstPassenger() == null) { + ItemStack stack = ContainerBlockItem.createInstance(this); + if (!player.addItem(stack)) { + player.drop(stack, false); + } + this.discard(); return InteractionResult.sidedSuccess(this.level().isClientSide()); } diff --git a/src/main/java/com/atsuishio/superbwarfare/item/ContainerBlockItem.java b/src/main/java/com/atsuishio/superbwarfare/item/ContainerBlockItem.java index bc4a550d3..ac4c17d68 100644 --- a/src/main/java/com/atsuishio/superbwarfare/item/ContainerBlockItem.java +++ b/src/main/java/com/atsuishio/superbwarfare/item/ContainerBlockItem.java @@ -6,11 +6,14 @@ 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.InteractionResult; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.EntityType; +import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.BlockItem; import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.context.BlockPlaceContext; import net.minecraftforge.client.extensions.common.IClientItemExtensions; import software.bernie.geckolib.animatable.GeoItem; import software.bernie.geckolib.core.animatable.instance.AnimatableInstanceCache; @@ -27,7 +30,20 @@ public class ContainerBlockItem extends BlockItem implements GeoItem { private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this); public ContainerBlockItem() { - super(ModBlocks.CONTAINER.get(), new Item.Properties()); + super(ModBlocks.CONTAINER.get(), new Item.Properties().stacksTo(1)); + } + + @Override + public InteractionResult place(BlockPlaceContext pContext) { + ItemStack stack = pContext.getItemInHand(); + Player player = pContext.getPlayer(); + if (player != null) { + var tag = BlockItem.getBlockEntityData(stack); + if (tag != null && tag.get("Entity") != null) { + player.getInventory().removeItem(stack); + } + } + return super.place(pContext); } private PlayState predicate(AnimationState event) {