修复创造模式充电站的范围显示bug

This commit is contained in:
17146 2025-04-11 21:16:52 +08:00
parent 9bd14bf824
commit c827d01c1a
3 changed files with 24 additions and 7 deletions

View file

@ -70,7 +70,7 @@ public class CreativeChargingStationBlock extends BaseEntityBlock {
if (!pLevel.isClientSide) { if (!pLevel.isClientSide) {
return createTickerHelper( return createTickerHelper(
pBlockEntityType, ModBlockEntities.CREATIVE_CHARGING_STATION.get(), pBlockEntityType, ModBlockEntities.CREATIVE_CHARGING_STATION.get(),
(pLevel1, pPos, pState1, blockEntity) -> CreativeChargingStationBlockEntity.serverTick(blockEntity) CreativeChargingStationBlockEntity::serverTick
); );
} }
return null; return null;
@ -91,6 +91,8 @@ public class CreativeChargingStationBlock extends BaseEntityBlock {
@Override @Override
@ParametersAreNonnullByDefault @ParametersAreNonnullByDefault
protected @NotNull ItemInteractionResult useItemOn(ItemStack stack, BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hitResult) { protected @NotNull ItemInteractionResult useItemOn(ItemStack stack, BlockState state, Level level, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hitResult) {
if (stack.isEmpty()) return ItemInteractionResult.PASS_TO_DEFAULT_BLOCK_INTERACTION;
var cap = stack.getCapability(Capabilities.EnergyStorage.ITEM); var cap = stack.getCapability(Capabilities.EnergyStorage.ITEM);
if (cap == null) return ItemInteractionResult.FAIL; if (cap == null) return ItemInteractionResult.FAIL;
@ -114,7 +116,6 @@ public class CreativeChargingStationBlock extends BaseEntityBlock {
return ItemInteractionResult.SUCCESS; return ItemInteractionResult.SUCCESS;
} }
// TODO 如何交互
@Override @Override
@ParametersAreNonnullByDefault @ParametersAreNonnullByDefault
protected @NotNull InteractionResult useWithoutItem(BlockState state, Level level, BlockPos pos, Player player, BlockHitResult hitResult) { protected @NotNull InteractionResult useWithoutItem(BlockState state, Level level, BlockPos pos, Player player, BlockHitResult hitResult) {

View file

@ -1,5 +1,6 @@
package com.atsuishio.superbwarfare.block.entity; package com.atsuishio.superbwarfare.block.entity;
import com.atsuishio.superbwarfare.block.CreativeChargingStationBlock;
import com.atsuishio.superbwarfare.capability.energy.InfinityEnergyStorage; import com.atsuishio.superbwarfare.capability.energy.InfinityEnergyStorage;
import com.atsuishio.superbwarfare.init.ModBlockEntities; import com.atsuishio.superbwarfare.init.ModBlockEntities;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
@ -11,6 +12,7 @@ import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientGamePacketListener; import net.minecraft.network.protocol.game.ClientGamePacketListener;
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket; import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.Entity;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.AABB; import net.minecraft.world.phys.AABB;
@ -35,7 +37,6 @@ public class CreativeChargingStationBlockEntity extends BlockEntity {
public @NotNull CompoundTag getUpdateTag(HolderLookup.@NotNull Provider registries) { public @NotNull CompoundTag getUpdateTag(HolderLookup.@NotNull Provider registries) {
CompoundTag tag = new CompoundTag(); CompoundTag tag = new CompoundTag();
tag.putBoolean("ShowRange", this.showRange); tag.putBoolean("ShowRange", this.showRange);
saveAdditional(tag, registries);
return tag; return tag;
} }
@ -55,8 +56,11 @@ public class CreativeChargingStationBlockEntity extends BlockEntity {
super(ModBlockEntities.CREATIVE_CHARGING_STATION.get(), pos, state); super(ModBlockEntities.CREATIVE_CHARGING_STATION.get(), pos, state);
} }
public static void serverTick(CreativeChargingStationBlockEntity blockEntity) { public static void serverTick(Level pLevel, BlockPos pPos, BlockState pState, CreativeChargingStationBlockEntity blockEntity) {
if (blockEntity.level == null) return; if (blockEntity.showRange != pState.getValue(CreativeChargingStationBlock.SHOW_RANGE)) {
pLevel.setBlockAndUpdate(pPos, pState.setValue(CreativeChargingStationBlock.SHOW_RANGE, blockEntity.showRange));
setChanged(pLevel, pPos, pState);
}
blockEntity.chargeEntity(); blockEntity.chargeEntity();
blockEntity.chargeBlock(); blockEntity.chargeBlock();
@ -98,4 +102,16 @@ public class CreativeChargingStationBlockEntity extends BlockEntity {
public IEnergyStorage getEnergyStorage(@Nullable Direction side) { public IEnergyStorage getEnergyStorage(@Nullable Direction side) {
return energyStorage; return energyStorage;
} }
@Override
protected void loadAdditional(CompoundTag tag, HolderLookup.Provider registries) {
super.loadAdditional(tag, registries);
this.showRange = tag.getBoolean("ShowRange");
}
@Override
protected void saveAdditional(CompoundTag tag, HolderLookup.Provider registries) {
super.saveAdditional(tag, registries);
tag.putBoolean("ShowRange", this.showRange);
}
} }

View file

@ -1,6 +1,6 @@
package com.atsuishio.superbwarfare.client.renderer.block; package com.atsuishio.superbwarfare.client.renderer.block;
import com.atsuishio.superbwarfare.block.ChargingStationBlock; import com.atsuishio.superbwarfare.block.CreativeChargingStationBlock;
import com.atsuishio.superbwarfare.block.entity.CreativeChargingStationBlockEntity; import com.atsuishio.superbwarfare.block.entity.CreativeChargingStationBlockEntity;
import com.atsuishio.superbwarfare.client.renderer.CustomRenderType; import com.atsuishio.superbwarfare.client.renderer.CustomRenderType;
import com.mojang.blaze3d.vertex.PoseStack; import com.mojang.blaze3d.vertex.PoseStack;
@ -19,7 +19,7 @@ public class CreativeChargingStationBlockEntityRenderer implements BlockEntityRe
@Override @Override
public void render(CreativeChargingStationBlockEntity blockEntity, float partialTick, @NotNull PoseStack poseStack, @NotNull MultiBufferSource bufferSource, int packedLight, int packedOverlay) { public void render(CreativeChargingStationBlockEntity blockEntity, float partialTick, @NotNull PoseStack poseStack, @NotNull MultiBufferSource bufferSource, int packedLight, int packedOverlay) {
if (!blockEntity.getBlockState().hasProperty(ChargingStationBlock.SHOW_RANGE) || !blockEntity.getBlockState().getValue(ChargingStationBlock.SHOW_RANGE)) if (!blockEntity.getBlockState().hasProperty(CreativeChargingStationBlock.SHOW_RANGE) || !blockEntity.getBlockState().getValue(CreativeChargingStationBlock.SHOW_RANGE))
return; return;
poseStack.pushPose(); poseStack.pushPose();