diff --git a/src/main/java/com/atsuishio/superbwarfare/item/DPSGeneratorDeployer.java b/src/main/java/com/atsuishio/superbwarfare/item/DPSGeneratorDeployer.java index 632f3ad69..642ac1b73 100644 --- a/src/main/java/com/atsuishio/superbwarfare/item/DPSGeneratorDeployer.java +++ b/src/main/java/com/atsuishio/superbwarfare/item/DPSGeneratorDeployer.java @@ -11,6 +11,7 @@ import net.minecraft.stats.Stats; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.InteractionResultHolder; +import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.MobSpawnType; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.Item; @@ -29,6 +30,7 @@ import org.jetbrains.annotations.NotNull; import javax.annotation.ParametersAreNonnullByDefault; import java.util.List; import java.util.Objects; +import java.util.function.Predicate; public class DPSGeneratorDeployer extends Item { @@ -42,6 +44,8 @@ public class DPSGeneratorDeployer extends Item { tooltipComponents.add(Component.translatable("des.superbwarfare.dps_generator_deployer").withStyle(ChatFormatting.GRAY).withStyle(ChatFormatting.ITALIC)); } + private static final Predicate IS_GENERATOR = e -> e instanceof DPSGeneratorEntity; + @Override public @NotNull InteractionResult useOn(UseOnContext pContext) { Level level = pContext.getLevel(); @@ -59,6 +63,15 @@ public class DPSGeneratorDeployer extends Item { pos = blockpos.relative(direction); } + // 禁止堆叠 + if (!level.getEntities( + (Entity) null, + ModEntities.DPS_GENERATOR.get().getSpawnAABB(pos.getX() + 0.5, pos.getY() + 0.5, pos.getZ() + 0.5), + IS_GENERATOR + ).isEmpty()) { + return InteractionResult.FAIL; + } + if (ModEntities.DPS_GENERATOR.get().spawn((ServerLevel) level, itemstack, pContext.getPlayer(), pos, MobSpawnType.SPAWN_EGG, true, !Objects.equals(blockpos, pos) && direction == Direction.UP) != null) { itemstack.shrink(1); level.gameEvent(pContext.getPlayer(), GameEvent.ENTITY_PLACE, blockpos); @@ -69,28 +82,37 @@ public class DPSGeneratorDeployer extends Item { } @Override - public @NotNull InteractionResultHolder use(@NotNull Level pLevel, Player pPlayer, @NotNull InteractionHand pHand) { - ItemStack itemstack = pPlayer.getItemInHand(pHand); - BlockHitResult blockhitresult = getPlayerPOVHitResult(pLevel, pPlayer, ClipContext.Fluid.SOURCE_ONLY); + public @NotNull InteractionResultHolder use(@NotNull Level level, Player player, @NotNull InteractionHand hand) { + ItemStack itemstack = player.getItemInHand(hand); + BlockHitResult blockhitresult = getPlayerPOVHitResult(level, player, ClipContext.Fluid.SOURCE_ONLY); if (blockhitresult.getType() != HitResult.Type.BLOCK) { return InteractionResultHolder.pass(itemstack); - } else if (!(pLevel instanceof ServerLevel)) { + } else if (!(level instanceof ServerLevel)) { return InteractionResultHolder.success(itemstack); } else { BlockPos blockpos = blockhitresult.getBlockPos(); - if (!(pLevel.getBlockState(blockpos).getBlock() instanceof LiquidBlock)) { + if (!(level.getBlockState(blockpos).getBlock() instanceof LiquidBlock)) { return InteractionResultHolder.pass(itemstack); - } else if (pLevel.mayInteract(pPlayer, blockpos) && pPlayer.mayUseItemAt(blockpos, blockhitresult.getDirection(), itemstack)) { - DPSGeneratorEntity entity = ModEntities.DPS_GENERATOR.get().spawn((ServerLevel) pLevel, itemstack, pPlayer, blockpos, MobSpawnType.SPAWN_EGG, false, false); + } else if (level.mayInteract(player, blockpos) && player.mayUseItemAt(blockpos, blockhitresult.getDirection(), itemstack)) { + // 禁止堆叠 + if (!level.getEntities( + (Entity) null, + ModEntities.DPS_GENERATOR.get().getSpawnAABB(blockpos.getX() + 0.5, blockpos.getY() + 0.5, blockpos.getZ() + 0.5), + IS_GENERATOR + ).isEmpty()) { + return InteractionResultHolder.fail(itemstack); + } + + DPSGeneratorEntity entity = ModEntities.DPS_GENERATOR.get().spawn((ServerLevel) level, itemstack, player, blockpos, MobSpawnType.SPAWN_EGG, false, false); if (entity == null) { return InteractionResultHolder.pass(itemstack); } else { - if (!pPlayer.getAbilities().instabuild) { + if (!player.getAbilities().instabuild) { itemstack.shrink(1); } - pPlayer.awardStat(Stats.ITEM_USED.get(this)); - pLevel.gameEvent(pPlayer, GameEvent.ENTITY_PLACE, entity.position()); + player.awardStat(Stats.ITEM_USED.get(this)); + level.gameEvent(player, GameEvent.ENTITY_PLACE, entity.position()); return InteractionResultHolder.consume(itemstack); } } else {