diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Mk42Entity.java b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Mk42Entity.java index 4456d19c5..c0a10175d 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Mk42Entity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Mk42Entity.java @@ -32,7 +32,6 @@ import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerPlayer; import net.minecraft.sounds.SoundSource; import net.minecraft.util.Mth; -import net.minecraft.world.Container; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.entity.Entity; @@ -60,7 +59,7 @@ import software.bernie.geckolib.util.GeckoLibUtil; import static com.atsuishio.superbwarfare.tools.RangeTool.calculateLaunchVector; -public class Mk42Entity extends VehicleEntity implements GeoEntity, CannonEntity, Container, LockTargetEntity { +public class Mk42Entity extends VehicleEntity implements GeoEntity, CannonEntity, LockTargetEntity { public static final EntityDataAccessor COOL_DOWN = SynchedEntityData.defineId(Mk42Entity.class, EntityDataSerializers.INT); private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this); @@ -77,9 +76,6 @@ public class Mk42Entity extends VehicleEntity implements GeoEntity, CannonEntity super(type, world); } - // TODO cap - public ItemStack stack = ItemStack.EMPTY; - @Override protected void defineSynchedData(SynchedEntityData.Builder builder) { super.defineSynchedData(builder); @@ -164,8 +160,8 @@ public class Mk42Entity extends VehicleEntity implements GeoEntity, CannonEntity } if (stack.is(ModTags.Items.CROWBAR) && !player.isShiftKeyDown()) { - if (this.stack.getItem() instanceof CannonShellItem) { - var weaponType = this.stack.is(ModItems.AP_5_INCHES.get()) ? 0 : 1; + if (this.items.getFirst().getItem() instanceof CannonShellItem) { + var weaponType = this.items.getFirst().is(ModItems.AP_5_INCHES.get()) ? 0 : 1; setWeaponIndex(0, weaponType); vehicleShoot(player, 0); } @@ -173,8 +169,8 @@ public class Mk42Entity extends VehicleEntity implements GeoEntity, CannonEntity } if (stack.getItem() instanceof CannonShellItem) { - if (this.entityData.get(COOL_DOWN) == 0 && this.stack.isEmpty()) { - this.stack = stack.copyWithCount(1); + if (this.entityData.get(COOL_DOWN) == 0 && this.items.getFirst().isEmpty()) { + this.setItem(0, stack.copyWithCount(1)); if (!player.isCreative()) { stack.shrink(1); } @@ -378,7 +374,7 @@ public class Mk42Entity extends VehicleEntity implements GeoEntity, CannonEntity } if (getFirstPassenger() != player) { - this.stack = ItemStack.EMPTY; + this.clearContent(); } var entityToSpawn = ((CannonShellWeapon) getWeapon(0)).create(player); @@ -576,39 +572,6 @@ public class Mk42Entity extends VehicleEntity implements GeoEntity, CannonEntity return 1; } - @Override - public boolean isEmpty() { - return stack == ItemStack.EMPTY; - } - - @Override - public @NotNull ItemStack getItem(int slot) { - return slot == 0 ? stack : ItemStack.EMPTY; - } - - @Override - public @NotNull ItemStack removeItem(int slot, int amount) { - if (slot != 0 || amount <= 0 || stack.isEmpty()) { - return ItemStack.EMPTY; - } - stack.shrink(1); - if (stack.isEmpty()) { - stack = ItemStack.EMPTY; - } - return stack; - } - - @Override - public @NotNull ItemStack removeItemNoUpdate(int slot) { - return removeItem(0, 1); - } - - @Override - public void setItem(int slot, @NotNull ItemStack stack) { - if (slot != 0) return; - this.stack = stack; - } - @Override public void setChanged() { // if (!entityData.get(INTELLIGENT)) { @@ -621,14 +584,8 @@ public class Mk42Entity extends VehicleEntity implements GeoEntity, CannonEntity return false; } - @Override - public void clearContent() { - this.stack = ItemStack.EMPTY; - } - @Override public boolean canPlaceItem(int slot, @NotNull ItemStack stack) { - if (slot != 0 || this.entityData.get(COOL_DOWN) != 0) return false; - return stack.getItem() instanceof CannonShellItem; + return super.canPlaceItem(slot, stack) && this.entityData.get(COOL_DOWN) == 0 && stack.getItem() instanceof CannonShellItem; } } diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Mle1934Entity.java b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Mle1934Entity.java index 87eefb46f..a135c6723 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Mle1934Entity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/Mle1934Entity.java @@ -32,7 +32,6 @@ import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerPlayer; import net.minecraft.sounds.SoundSource; import net.minecraft.util.Mth; -import net.minecraft.world.Container; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.entity.Entity; @@ -61,7 +60,7 @@ import software.bernie.geckolib.util.GeckoLibUtil; import static com.atsuishio.superbwarfare.tools.RangeTool.calculateLaunchVector; -public class Mle1934Entity extends VehicleEntity implements GeoEntity, CannonEntity, Container, LockTargetEntity { +public class Mle1934Entity extends VehicleEntity implements GeoEntity, CannonEntity, LockTargetEntity { public static final EntityDataAccessor COOL_DOWN = SynchedEntityData.defineId(Mle1934Entity.class, EntityDataSerializers.INT); public static final EntityDataAccessor TYPE = SynchedEntityData.defineId(Mle1934Entity.class, EntityDataSerializers.INT); @@ -80,9 +79,6 @@ public class Mle1934Entity extends VehicleEntity implements GeoEntity, CannonEnt super(type, world); } - // TODO cap - public ItemStack stack = ItemStack.EMPTY; - @Override public VehicleWeapon[][] initWeapons() { return new VehicleWeapon[][]{ @@ -170,8 +166,8 @@ public class Mle1934Entity extends VehicleEntity implements GeoEntity, CannonEnt } if (stack.is(ModTags.Items.CROWBAR) && !player.isShiftKeyDown()) { - if (this.stack.getItem() instanceof CannonShellItem) { - var weaponType = this.stack.is(ModItems.AP_5_INCHES.get()) ? 0 : 1; + if (this.items.getFirst().getItem() instanceof CannonShellItem) { + var weaponType = this.items.getFirst().is(ModItems.AP_5_INCHES.get()) ? 0 : 1; setWeaponIndex(0, weaponType); vehicleShoot(player, 0); } @@ -180,7 +176,7 @@ public class Mle1934Entity extends VehicleEntity implements GeoEntity, CannonEnt if (stack.getItem() instanceof CannonShellItem) { var itemHandler = this.getCapability(Capabilities.ItemHandler.ENTITY); - if (itemHandler != null && this.entityData.get(COOL_DOWN) == 0 && (stack.getItem() == this.stack.getItem() || this.stack.isEmpty())) { + if (itemHandler != null && this.entityData.get(COOL_DOWN) == 0 && (stack.getItem() == this.items.getFirst().getItem() || this.items.getFirst().isEmpty())) { itemHandler.insertItem(0, stack.copyWithCount(1), false); if (!player.isCreative()) { stack.shrink(1); @@ -223,7 +219,7 @@ public class Mle1934Entity extends VehicleEntity implements GeoEntity, CannonEnt int targetX = pos.getX(); int targetY = pos.getY(); int targetZ = pos.getZ(); - var isDepressed = parameters.isDepressed(); +// var isDepressed = parameters.isDepressed(); Matrix4f transform = getVehicleFlatTransform(1); Vector4f worldPosition = transformPosition(transform, 0, 1.4992625f, 1.52065f); @@ -390,11 +386,11 @@ public class Mle1934Entity extends VehicleEntity implements GeoEntity, CannonEnt consumed = InventoryTool.consumeItem(player.getInventory().items, ammo, 2); } } else { - consumed = stack.getCount(); + consumed = this.items.getFirst().getCount(); } if (getFirstPassenger() != player) { - this.stack = ItemStack.EMPTY; + this.clearContent(); } boolean salvoShoot = consumed == 2; @@ -647,39 +643,11 @@ public class Mle1934Entity extends VehicleEntity implements GeoEntity, CannonEnt return 2; } - @Override - public boolean isEmpty() { - return stack == ItemStack.EMPTY; - } - - @Override - public @NotNull ItemStack getItem(int slot) { - return slot == 0 ? stack : ItemStack.EMPTY; - } - - @Override - public @NotNull ItemStack removeItem(int slot, int amount) { - if (slot != 0 || amount <= 0 || stack.isEmpty()) { - return ItemStack.EMPTY; - } - stack.shrink(2); - if (stack.isEmpty()) { - stack = ItemStack.EMPTY; - } - return stack; - } - @Override public @NotNull ItemStack removeItemNoUpdate(int slot) { return removeItem(0, 2); } - @Override - public void setItem(int slot, @NotNull ItemStack stack) { - if (slot != 0) return; - this.stack = stack; - } - @Override public void setChanged() { // if (!entityData.get(INTELLIGENT)) { @@ -692,15 +660,9 @@ public class Mle1934Entity extends VehicleEntity implements GeoEntity, CannonEnt return false; } - @Override - public void clearContent() { - this.stack = ItemStack.EMPTY; - } - @Override public boolean canPlaceItem(int slot, @NotNull ItemStack stack) { - if (slot != 0 || this.entityData.get(COOL_DOWN) != 0) return false; - return stack.getItem() instanceof CannonShellItem; + return super.canPlaceItem(slot, stack) && this.entityData.get(COOL_DOWN) == 0 && stack.getItem() instanceof CannonShellItem; } } diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/MortarEntity.java b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/MortarEntity.java index ec9117b9c..6b93acc38 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/MortarEntity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/MortarEntity.java @@ -25,7 +25,6 @@ import net.minecraft.server.level.ServerPlayer; import net.minecraft.sounds.SoundEvents; import net.minecraft.sounds.SoundSource; import net.minecraft.util.Mth; -import net.minecraft.world.Container; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.entity.EntityType; @@ -48,7 +47,7 @@ import software.bernie.geckolib.util.GeckoLibUtil; import static com.atsuishio.superbwarfare.tools.RangeTool.calculateLaunchVector; -public class MortarEntity extends VehicleEntity implements GeoEntity, Container, LockTargetEntity { +public class MortarEntity extends VehicleEntity implements GeoEntity, LockTargetEntity { public static final EntityDataAccessor FIRE_TIME = SynchedEntityData.defineId(MortarEntity.class, EntityDataSerializers.INT); public static final EntityDataAccessor PITCH = SynchedEntityData.defineId(MortarEntity.class, EntityDataSerializers.FLOAT); @@ -62,7 +61,6 @@ public class MortarEntity extends VehicleEntity implements GeoEntity, Container, private final AnimatableInstanceCache cache = GeckoLibUtil.createInstanceCache(this); private LivingEntity shooter = null; - public ItemStack stack = ItemStack.EMPTY; public MortarEntity(EntityType type, Level level) { super(type, level); @@ -138,7 +136,7 @@ public class MortarEntity extends VehicleEntity implements GeoEntity, Container, } public void fire(@Nullable LivingEntity shooter) { - if (!(this.stack.getItem() instanceof MortarShell)) return; + if (!(this.items.getFirst().getItem() instanceof MortarShell)) return; if (entityData.get(FIRE_TIME) != 0) return; this.shooter = shooter; @@ -170,14 +168,14 @@ public class MortarEntity extends VehicleEntity implements GeoEntity, Container, } if (mainHandItem.is(ModTags.Items.CROWBAR) && !player.isShiftKeyDown()) { - if (this.stack.getItem() instanceof MortarShell && this.entityData.get(FIRE_TIME) == 0) { + if (this.items.getFirst().getItem() instanceof MortarShell && this.entityData.get(FIRE_TIME) == 0) { fire(player); } return InteractionResult.SUCCESS; } - if (mainHandItem.getItem() instanceof MortarShell && !player.isShiftKeyDown() && this.entityData.get(FIRE_TIME) == 0 && stack.isEmpty()) { - this.stack = mainHandItem.copyWithCount(1); + if (mainHandItem.getItem() instanceof MortarShell && !player.isShiftKeyDown() && this.entityData.get(FIRE_TIME) == 0 && this.items.getFirst().isEmpty()) { + this.items.set(0, mainHandItem.copyWithCount(1)); if (!player.isCreative()) { mainHandItem.shrink(1); } @@ -288,10 +286,10 @@ public class MortarEntity extends VehicleEntity implements GeoEntity, Container, this.entityData.set(FIRE_TIME, fireTime - 1); } - if (fireTime == 5 && this.stack.getItem() instanceof MortarShell) { + if (fireTime == 5 && this.items.getFirst().getItem() instanceof MortarShell) { Level level = this.level(); if (level instanceof ServerLevel server) { - MortarShellEntity entityToSpawn = MortarShell.createShell(shooter, level, this.stack); + MortarShellEntity entityToSpawn = MortarShell.createShell(shooter, level, this.items.getFirst()); entityToSpawn.setPos(this.getX(), this.getEyeY(), this.getZ()); entityToSpawn.shoot(this.getLookAngle().x, this.getLookAngle().y, this.getLookAngle().z, 13f, (float) 0.1); level.addFreshEntity(entityToSpawn); @@ -299,7 +297,7 @@ public class MortarEntity extends VehicleEntity implements GeoEntity, Container, 0.007); server.sendParticles(ParticleTypes.CAMPFIRE_COSY_SMOKE, this.getX(), this.getY(), this.getZ(), 50, 2, 0.02, 2, 0.0005); - this.stack = ItemStack.EMPTY; + this.clearContent(); resetTarget(); } } @@ -391,39 +389,6 @@ public class MortarEntity extends VehicleEntity implements GeoEntity, Container, return 1; } - @Override - public boolean isEmpty() { - return stack == ItemStack.EMPTY; - } - - @Override - public @NotNull ItemStack getItem(int slot) { - return slot == 0 ? stack : ItemStack.EMPTY; - } - - @Override - public @NotNull ItemStack removeItem(int slot, int amount) { - if (slot != 0 || amount <= 0 || stack.isEmpty()) { - return ItemStack.EMPTY; - } - stack.shrink(1); - if (stack.isEmpty()) { - stack = ItemStack.EMPTY; - } - return stack; - } - - @Override - public @NotNull ItemStack removeItemNoUpdate(int slot) { - return removeItem(0, 1); - } - - @Override - public void setItem(int slot, @NotNull ItemStack stack) { - if (slot != 0) return; - this.stack = stack; - } - @Override public void setChanged() { if (!entityData.get(INTELLIGENT)) { @@ -431,19 +396,8 @@ public class MortarEntity extends VehicleEntity implements GeoEntity, Container, } } - @Override - public boolean stillValid(@NotNull Player player) { - return false; - } - - @Override - public void clearContent() { - this.stack = ItemStack.EMPTY; - } - @Override public boolean canPlaceItem(int slot, @NotNull ItemStack stack) { - if (slot != 0 || this.entityData.get(FIRE_TIME) != 0) return false; - return stack.getItem() instanceof MortarShell; + return super.canPlaceItem(slot, stack) && this.entityData.get(FIRE_TIME) == 0 && stack.getItem() instanceof MortarShell; } } diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/base/ContainerMobileVehicleEntity.java b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/base/ContainerMobileVehicleEntity.java index 38f5daa28..60067b4cb 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/base/ContainerMobileVehicleEntity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/base/ContainerMobileVehicleEntity.java @@ -2,7 +2,6 @@ package com.atsuishio.superbwarfare.entity.vehicle.base; import com.atsuishio.superbwarfare.init.ModTags; import com.atsuishio.superbwarfare.menu.VehicleMenu; -import com.atsuishio.superbwarfare.tools.InventoryTool; import net.minecraft.core.NonNullList; import net.minecraft.nbt.CompoundTag; import net.minecraft.resources.ResourceKey; @@ -12,12 +11,10 @@ import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.HasCustomInventoryScreen; -import net.minecraft.world.entity.item.ItemEntity; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.vehicle.ContainerEntity; import net.minecraft.world.inventory.AbstractContainerMenu; -import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.Level; import net.minecraft.world.level.gameevent.GameEvent; @@ -31,7 +28,10 @@ public abstract class ContainerMobileVehicleEntity extends MobileVehicleEntity i public static final int DEFAULT_CONTAINER_SIZE = 102; - private final NonNullList items = NonNullList.withSize(this.getContainerSize(), ItemStack.EMPTY); + @Override + public int getContainerSize() { + return DEFAULT_CONTAINER_SIZE; + } public ContainerMobileVehicleEntity(EntityType pEntityType, Level pLevel) { super(pEntityType, pLevel); @@ -118,109 +118,6 @@ public abstract class ContainerMobileVehicleEntity extends MobileVehicleEntity i } @Override - public @NotNull NonNullList getItemStacks() { - return this.items; - } - - /** - * 计算当前载具内指定物品的数量 - * - * @param item 物品类型 - * @return 物品数量 - */ - public int countItem(@Nullable Item item) { - if (item == null) return 0; - return InventoryTool.countItem(this.getItemStacks(), item); - } - - /** - * 判断载具内是否包含指定物品 - * - * @param item 物品类型 - */ - public boolean hasItem(Item item) { - return countItem(item) > 0; - } - - /** - * 消耗载具内指定物品 - * - * @param item 物品类型 - * @param count 要消耗的数量 - * @return 成功消耗的物品数量 - */ - public int consumeItem(Item item, int count) { - return InventoryTool.consumeItem(this.getItemStacks(), item, count); - } - - /** - * 尝试插入指定物品指定数量,如果载具内已满则生成掉落物 - * - * @param item 物品类型 - * @param count 要插入的数量 - */ - public void insertItem(Item item, int count) { - var rest = InventoryTool.insertItem(this.getItemStacks(), item, count, this.getMaxStackSize()); - - if (rest > 0) { - var stackToDrop = new ItemStack(item, rest); - this.level().addFreshEntity(new ItemEntity(this.level(), this.getX(), this.getY(), this.getZ(), stackToDrop)); - } - } - - @Override - public void clearItemStacks() { - this.items.clear(); - } - - @Override - public int getContainerSize() { - return DEFAULT_CONTAINER_SIZE; - } - - @Override - public @NotNull ItemStack getItem(int pSlot) { - return this.items.get(pSlot); - } - - @Override - public @NotNull ItemStack removeItem(int pSlot, int pAmount) { - return ContainerHelper.removeItem(this.items, pSlot, pAmount); - } - - @Override - public @NotNull ItemStack removeItemNoUpdate(int pSlot) { - ItemStack itemstack = this.getItemStacks().get(pSlot); - if (itemstack.isEmpty()) { - return ItemStack.EMPTY; - } else { - this.getItemStacks().set(pSlot, ItemStack.EMPTY); - return itemstack; - } - } - - @Override - public void setItem(int pSlot, @NotNull ItemStack pStack) { - this.getItemStacks().set(pSlot, pStack); - if (!pStack.isEmpty() && pStack.getCount() > this.getMaxStackSize()) { - pStack.setCount(this.getMaxStackSize()); - } - } - - @Override - public void setChanged() { - } - - @Override - public boolean stillValid(@NotNull Player pPlayer) { - return !this.isRemoved() && this.position().closerThan(pPlayer.position(), 8.0D); - } - - @Override - public void clearContent() { - this.getItemStacks().clear(); - } - public boolean hasMenu() { return true; } @@ -238,4 +135,14 @@ public abstract class ContainerMobileVehicleEntity extends MobileVehicleEntity i public void stopOpen(@NotNull Player pPlayer) { this.level().gameEvent(GameEvent.CONTAINER_CLOSE, this.position(), GameEvent.Context.of(pPlayer)); } + + @Override + public @NotNull NonNullList getItemStacks() { + return this.items; + } + + @Override + public void clearItemStacks() { + this.items.clear(); + } } diff --git a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/base/VehicleEntity.java b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/base/VehicleEntity.java index 56c016dc0..9fe5dc644 100644 --- a/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/base/VehicleEntity.java +++ b/src/main/java/com/atsuishio/superbwarfare/entity/vehicle/base/VehicleEntity.java @@ -12,6 +12,7 @@ import com.atsuishio.superbwarfare.init.*; import com.atsuishio.superbwarfare.item.ContainerBlockItem; import com.atsuishio.superbwarfare.network.message.receive.ClientIndicatorMessage; import com.atsuishio.superbwarfare.tools.EntityFindUtil; +import com.atsuishio.superbwarfare.tools.InventoryTool; import com.atsuishio.superbwarfare.tools.ParticleTool; import com.atsuishio.superbwarfare.tools.VectorTool; import com.google.common.collect.ImmutableList; @@ -27,6 +28,7 @@ import net.minecraft.client.player.LocalPlayer; import net.minecraft.client.renderer.GameRenderer; import net.minecraft.core.BlockPos; import net.minecraft.core.Holder; +import net.minecraft.core.NonNullList; import net.minecraft.core.particles.ParticleTypes; import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.nbt.CompoundTag; @@ -42,6 +44,8 @@ import net.minecraft.server.level.ServerPlayer; import net.minecraft.sounds.SoundEvents; import net.minecraft.sounds.SoundSource; import net.minecraft.util.Mth; +import net.minecraft.world.Container; +import net.minecraft.world.ContainerHelper; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.damagesource.DamageSource; @@ -50,10 +54,12 @@ import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.LivingEntity; import net.minecraft.world.entity.Pose; +import net.minecraft.world.entity.item.ItemEntity; import net.minecraft.world.entity.player.Player; import net.minecraft.world.entity.projectile.AbstractArrow; import net.minecraft.world.entity.projectile.Projectile; import net.minecraft.world.entity.vehicle.DismountHelper; +import net.minecraft.world.item.Item; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.Level; import net.minecraft.world.level.gameevent.GameEvent; @@ -80,7 +86,7 @@ import java.util.function.Function; import static com.atsuishio.superbwarfare.client.RenderHelper.preciseBlit; import static com.atsuishio.superbwarfare.tools.ParticleTool.sendParticle; -public abstract class VehicleEntity extends Entity { +public abstract class VehicleEntity extends Entity implements Container { public static final EntityDataAccessor HEALTH = SynchedEntityData.defineId(VehicleEntity.class, EntityDataSerializers.FLOAT); public static final EntityDataAccessor LAST_ATTACKER_UUID = SynchedEntityData.defineId(VehicleEntity.class, EntityDataSerializers.STRING); @@ -135,6 +141,144 @@ public abstract class VehicleEntity extends Entity { entityData.set(MOUSE_SPEED_Y, (float) y); } + // container start + + protected final NonNullList items = NonNullList.withSize(this.getContainerSize(), ItemStack.EMPTY); + + /** + * 计算当前载具内指定物品的数量 + * + * @param item 物品类型 + * @return 物品数量 + */ + public int countItem(@Nullable Item item) { + if (item == null || !this.hasContainer()) return 0; + return InventoryTool.countItem(this.items, item); + } + + /** + * 判断载具内是否包含指定物品 + * + * @param item 物品类型 + */ + public boolean hasItem(Item item) { + if (!this.hasContainer()) return false; + + return countItem(item) > 0; + } + + /** + * 消耗载具内指定物品 + * + * @param item 物品类型 + * @param count 要消耗的数量 + * @return 成功消耗的物品数量 + */ + public int consumeItem(Item item, int count) { + if (!this.hasContainer()) return 0; + + return InventoryTool.consumeItem(this.items, item, count); + } + + /** + * 尝试插入指定物品指定数量,如果载具内已满则生成掉落物 + * + * @param item 物品类型 + * @param count 要插入的数量 + */ + public void insertItem(Item item, int count) { + if (!this.hasContainer()) return; + + var rest = InventoryTool.insertItem(this.items, item, count, this.getMaxStackSize()); + + if (rest > 0) { + var stackToDrop = new ItemStack(item, rest); + this.level().addFreshEntity(new ItemEntity(this.level(), this.getX(), this.getY(), this.getZ(), stackToDrop)); + } + } + + @Override + public int getContainerSize() { + return 0; + } + + @Override + public @NotNull ItemStack getItem(int slot) { + if (!this.hasContainer() || slot >= this.getContainerSize() || slot < 0) return ItemStack.EMPTY; + return this.items.get(slot); + } + + @Override + public @NotNull ItemStack removeItem(int slot, int pAmount) { + if (!this.hasContainer() || slot >= this.getContainerSize() || slot < 0) return ItemStack.EMPTY; + + return ContainerHelper.removeItem(this.items, slot, pAmount); + } + + @Override + public @NotNull ItemStack removeItemNoUpdate(int slot) { + if (!this.hasContainer() || slot >= this.getContainerSize() || slot < 0) return ItemStack.EMPTY; + + ItemStack itemstack = this.items.get(slot); + if (itemstack.isEmpty()) { + return ItemStack.EMPTY; + } else { + this.items.set(slot, ItemStack.EMPTY); + return itemstack; + } + } + + @Override + public void setItem(int slot, @NotNull ItemStack pStack) { + if (!this.hasContainer() || slot >= this.getContainerSize() || slot < 0) return; + + this.items.set(slot, pStack); + if (!pStack.isEmpty() && pStack.getCount() > this.getMaxStackSize()) { + pStack.setCount(this.getMaxStackSize()); + } + } + + @Override + public void setChanged() { + } + + @Override + public boolean stillValid(@NotNull Player pPlayer) { + return this.hasContainer() && !this.isRemoved() && this.position().closerThan(pPlayer.position(), 8.0D); + } + + @Override + public void clearContent() { + this.items.clear(); + } + + public boolean hasMenu() { + return false; + } + + @Override + public boolean isEmpty() { + return this.items.stream().allMatch(ItemStack::isEmpty); + } + + public boolean hasContainer() { + return this.getContainerSize() > 0; + } + + @Override + public boolean canPlaceItem(int slot, @NotNull ItemStack stack) { + if (!this.hasContainer() || slot >= this.getContainerSize() || slot < 0) return false; + return Container.super.canPlaceItem(slot, stack); + } + + @Override + public boolean canTakeItem(@NotNull Container target, int slot, @NotNull ItemStack stack) { + if (!this.hasContainer() || slot >= this.getContainerSize() || slot < 0) return false; + return Container.super.canTakeItem(target, slot, stack); + } + + // container end + // 自定义骑乘 private final List orderedPassengers = generatePassengersList(); @@ -347,6 +491,7 @@ public abstract class VehicleEntity extends Entity { .define(ENERGY, 0); } + // energy start /** * 消耗指定电量 * @@ -389,6 +534,8 @@ public abstract class VehicleEntity extends Entity { return false; } + // energy end + private int[] initSelectedWeaponArray(WeaponVehicleEntity weaponVehicle) { // 初始化武器数组 weaponVehicle.getAllWeapons(); diff --git a/src/main/java/com/atsuishio/superbwarfare/init/ModCapabilities.java b/src/main/java/com/atsuishio/superbwarfare/init/ModCapabilities.java index fe26df868..87a3fd258 100644 --- a/src/main/java/com/atsuishio/superbwarfare/init/ModCapabilities.java +++ b/src/main/java/com/atsuishio/superbwarfare/init/ModCapabilities.java @@ -7,7 +7,6 @@ import com.atsuishio.superbwarfare.block.entity.FuMO25BlockEntity; import com.atsuishio.superbwarfare.capability.energy.ItemEnergyStorage; import com.atsuishio.superbwarfare.capability.laser.LaserCapability; import com.atsuishio.superbwarfare.capability.laser.LaserCapabilityProvider; -import com.atsuishio.superbwarfare.entity.vehicle.base.ContainerMobileVehicleEntity; import com.atsuishio.superbwarfare.entity.vehicle.base.VehicleEntity; import com.atsuishio.superbwarfare.item.CreativeChargingStationBlockItem; import com.atsuishio.superbwarfare.item.EnergyStorageItem; @@ -79,19 +78,17 @@ public class ModCapabilities { // 载具 for (var entity : ModEntities.REGISTRY.getEntries()) { - // 能量 if (entity.get().getBaseClass().isAssignableFrom(VehicleEntity.class)) { + // 能量 event.registerEntity(Capabilities.EnergyStorage.ENTITY, entity.get(), (obj, ctx) -> (obj instanceof VehicleEntity vehicle && vehicle.hasEnergyStorage()) ? vehicle.getEnergyStorage() : null ); - } - // 物品 - if (entity.get().getBaseClass().isAssignableFrom(ContainerMobileVehicleEntity.class)) { + // 物品 event.registerEntity(Capabilities.ItemHandler.ENTITY, entity.get(), - (obj, ctx) -> (obj instanceof ContainerMobileVehicleEntity vehicle) ? new InvWrapper(vehicle) : null + (obj, ctx) -> (obj instanceof VehicleEntity vehicle && vehicle.hasContainer()) ? new InvWrapper(vehicle) : null ); } } diff --git a/src/main/java/com/atsuishio/superbwarfare/network/message/send/ArtilleryIndicatorFireMessage.java b/src/main/java/com/atsuishio/superbwarfare/network/message/send/ArtilleryIndicatorFireMessage.java index 6c1d0b559..430e72535 100644 --- a/src/main/java/com/atsuishio/superbwarfare/network/message/send/ArtilleryIndicatorFireMessage.java +++ b/src/main/java/com/atsuishio/superbwarfare/network/message/send/ArtilleryIndicatorFireMessage.java @@ -45,23 +45,23 @@ public enum ArtilleryIndicatorFireMessage implements CustomPacketPayload { var tag = tags.getCompound(i); Entity entity = EntityFindUtil.findEntity(player.level(), tag.getString("UUID")); if (entity instanceof MortarEntity mortarEntity) { - if (mortarEntity.stack.getItem() instanceof MortarShell && mortarEntity.getEntityData().get(FIRE_TIME) == 0) { + if (mortarEntity.getItem(0).getItem() instanceof MortarShell && mortarEntity.getEntityData().get(FIRE_TIME) == 0) { int randomNumber = (int) (Math.random() * 5) + 1; Mod.queueServerWork(randomNumber, () -> mortarEntity.fire(player)); } } if (entity instanceof Mk42Entity mk42Entity) { - if (mk42Entity.stack.getItem() instanceof CannonShellItem && mk42Entity.getEntityData().get(COOL_DOWN) == 0) { + if (mk42Entity.getItem(0).getItem() instanceof CannonShellItem && mk42Entity.getEntityData().get(COOL_DOWN) == 0) { int randomNumber = (int) (Math.random() * 5) + 1; - var weaponType = mk42Entity.stack.is(ModItems.AP_5_INCHES.get()) ? 0 : 1; + var weaponType = mk42Entity.getItem(0).is(ModItems.AP_5_INCHES.get()) ? 0 : 1; mk42Entity.setWeaponIndex(0, weaponType); Mod.queueServerWork(randomNumber, () -> mk42Entity.vehicleShoot(player, 0)); } } if (entity instanceof Mle1934Entity mle1934Entity) { - if (mle1934Entity.stack.getItem() instanceof CannonShellItem && mle1934Entity.getEntityData().get(COOL_DOWN) == 0) { + if (mle1934Entity.getItem(0).getItem() instanceof CannonShellItem && mle1934Entity.getEntityData().get(COOL_DOWN) == 0) { int randomNumber = (int) (Math.random() * 5) + 1; - var weaponType = mle1934Entity.stack.is(ModItems.AP_5_INCHES.get()) ? 0 : 1; + var weaponType = mle1934Entity.getItem(0).is(ModItems.AP_5_INCHES.get()) ? 0 : 1; mle1934Entity.setWeaponIndex(0, weaponType); Mod.queueServerWork(randomNumber, () -> mle1934Entity.vehicleShoot(player, 0)); }