禁止堆叠DPS发电机
This commit is contained in:
parent
85c3087711
commit
846e763f45
1 changed files with 32 additions and 10 deletions
|
@ -11,6 +11,7 @@ import net.minecraft.stats.Stats;
|
||||||
import net.minecraft.world.InteractionHand;
|
import net.minecraft.world.InteractionHand;
|
||||||
import net.minecraft.world.InteractionResult;
|
import net.minecraft.world.InteractionResult;
|
||||||
import net.minecraft.world.InteractionResultHolder;
|
import net.minecraft.world.InteractionResultHolder;
|
||||||
|
import net.minecraft.world.entity.Entity;
|
||||||
import net.minecraft.world.entity.MobSpawnType;
|
import net.minecraft.world.entity.MobSpawnType;
|
||||||
import net.minecraft.world.entity.player.Player;
|
import net.minecraft.world.entity.player.Player;
|
||||||
import net.minecraft.world.item.Item;
|
import net.minecraft.world.item.Item;
|
||||||
|
@ -29,6 +30,7 @@ import org.jetbrains.annotations.NotNull;
|
||||||
import javax.annotation.ParametersAreNonnullByDefault;
|
import javax.annotation.ParametersAreNonnullByDefault;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Objects;
|
import java.util.Objects;
|
||||||
|
import java.util.function.Predicate;
|
||||||
|
|
||||||
public class DPSGeneratorDeployer extends Item {
|
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));
|
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
|
@Override
|
||||||
public @NotNull InteractionResult useOn(UseOnContext pContext) {
|
public @NotNull InteractionResult useOn(UseOnContext pContext) {
|
||||||
Level level = pContext.getLevel();
|
Level level = pContext.getLevel();
|
||||||
|
@ -59,6 +63,15 @@ public class DPSGeneratorDeployer extends Item {
|
||||||
pos = blockpos.relative(direction);
|
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) {
|
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);
|
itemstack.shrink(1);
|
||||||
level.gameEvent(pContext.getPlayer(), GameEvent.ENTITY_PLACE, blockpos);
|
level.gameEvent(pContext.getPlayer(), GameEvent.ENTITY_PLACE, blockpos);
|
||||||
|
@ -69,28 +82,37 @@ public class DPSGeneratorDeployer extends Item {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public @NotNull InteractionResultHolder<ItemStack> use(@NotNull Level pLevel, Player pPlayer, @NotNull InteractionHand pHand) {
|
public @NotNull InteractionResultHolder<ItemStack> use(@NotNull Level level, Player player, @NotNull InteractionHand hand) {
|
||||||
ItemStack itemstack = pPlayer.getItemInHand(pHand);
|
ItemStack itemstack = player.getItemInHand(hand);
|
||||||
BlockHitResult blockhitresult = getPlayerPOVHitResult(pLevel, pPlayer, ClipContext.Fluid.SOURCE_ONLY);
|
BlockHitResult blockhitresult = getPlayerPOVHitResult(level, player, ClipContext.Fluid.SOURCE_ONLY);
|
||||||
if (blockhitresult.getType() != HitResult.Type.BLOCK) {
|
if (blockhitresult.getType() != HitResult.Type.BLOCK) {
|
||||||
return InteractionResultHolder.pass(itemstack);
|
return InteractionResultHolder.pass(itemstack);
|
||||||
} else if (!(pLevel instanceof ServerLevel)) {
|
} else if (!(level instanceof ServerLevel)) {
|
||||||
return InteractionResultHolder.success(itemstack);
|
return InteractionResultHolder.success(itemstack);
|
||||||
} else {
|
} else {
|
||||||
BlockPos blockpos = blockhitresult.getBlockPos();
|
BlockPos blockpos = blockhitresult.getBlockPos();
|
||||||
if (!(pLevel.getBlockState(blockpos).getBlock() instanceof LiquidBlock)) {
|
if (!(level.getBlockState(blockpos).getBlock() instanceof LiquidBlock)) {
|
||||||
return InteractionResultHolder.pass(itemstack);
|
return InteractionResultHolder.pass(itemstack);
|
||||||
} else if (pLevel.mayInteract(pPlayer, blockpos) && pPlayer.mayUseItemAt(blockpos, blockhitresult.getDirection(), itemstack)) {
|
} else if (level.mayInteract(player, blockpos) && player.mayUseItemAt(blockpos, blockhitresult.getDirection(), itemstack)) {
|
||||||
DPSGeneratorEntity entity = ModEntities.DPS_GENERATOR.get().spawn((ServerLevel) pLevel, itemstack, pPlayer, blockpos, MobSpawnType.SPAWN_EGG, false, false);
|
// 禁止堆叠
|
||||||
|
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) {
|
if (entity == null) {
|
||||||
return InteractionResultHolder.pass(itemstack);
|
return InteractionResultHolder.pass(itemstack);
|
||||||
} else {
|
} else {
|
||||||
if (!pPlayer.getAbilities().instabuild) {
|
if (!player.getAbilities().instabuild) {
|
||||||
itemstack.shrink(1);
|
itemstack.shrink(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
pPlayer.awardStat(Stats.ITEM_USED.get(this));
|
player.awardStat(Stats.ITEM_USED.get(this));
|
||||||
pLevel.gameEvent(pPlayer, GameEvent.ENTITY_PLACE, entity.position());
|
level.gameEvent(player, GameEvent.ENTITY_PLACE, entity.position());
|
||||||
return InteractionResultHolder.consume(itemstack);
|
return InteractionResultHolder.consume(itemstack);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Add table
Reference in a new issue