禁止堆叠DPS发电机

This commit is contained in:
Light_Quanta 2025-05-12 16:09:30 +08:00
parent 85c3087711
commit 846e763f45
No known key found for this signature in database
GPG key ID: 11A39A1B8C890959

View file

@ -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<Entity> 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<ItemStack> 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<ItemStack> 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 {