From 594707ae778d9d52ea8f639e187d810c5ed9d932 Mon Sep 17 00:00:00 2001 From: 17146 <1714673995@qq.com> Date: Thu, 26 Dec 2024 01:32:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=9B=B7=E8=BE=BEserver=20ti?= =?UTF-8?q?ck=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../superbwarfare/block/FuMO25Block.java | 17 ++++++++ .../block/entity/FuMO25BlockEntity.java | 42 ++++++++++++++++++- .../superbwarfare/menu/FuMO25Menu.java | 8 +++- .../message/RadarChangeModeMessage.java | 2 +- 4 files changed, 66 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/atsuishio/superbwarfare/block/FuMO25Block.java b/src/main/java/com/atsuishio/superbwarfare/block/FuMO25Block.java index eadba9d7c..8d27c3e15 100644 --- a/src/main/java/com/atsuishio/superbwarfare/block/FuMO25Block.java +++ b/src/main/java/com/atsuishio/superbwarfare/block/FuMO25Block.java @@ -6,6 +6,7 @@ import net.minecraft.core.BlockPos; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.entity.player.Player; +import net.minecraft.world.item.context.BlockPlaceContext; import net.minecraft.world.level.BlockGetter; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.Block; @@ -16,6 +17,8 @@ import net.minecraft.world.level.block.entity.BlockEntityTicker; import net.minecraft.world.level.block.entity.BlockEntityType; import net.minecraft.world.level.block.state.BlockBehaviour; import net.minecraft.world.level.block.state.BlockState; +import net.minecraft.world.level.block.state.StateDefinition; +import net.minecraft.world.level.block.state.properties.BooleanProperty; import net.minecraft.world.phys.BlockHitResult; import net.minecraft.world.phys.shapes.CollisionContext; import net.minecraft.world.phys.shapes.Shapes; @@ -25,8 +28,11 @@ import org.jetbrains.annotations.Nullable; @SuppressWarnings("deprecation") public class FuMO25Block extends Block implements EntityBlock { + public static final BooleanProperty POWERED = BooleanProperty.create("powered"); + public FuMO25Block() { super(BlockBehaviour.Properties.of().sound(SoundType.METAL).strength(3.0f).requiresCorrectToolForDrops()); + this.registerDefaultState(this.stateDefinition.any().setValue(POWERED, false)); } @Override @@ -79,4 +85,15 @@ public class FuMO25Block extends Block implements EntityBlock { super.onRemove(pState, pLevel, pPos, pNewState, pMovedByPiston); } + + @Override + protected void createBlockStateDefinition(StateDefinition.Builder pBuilder) { + pBuilder.add(POWERED); + } + + @Nullable + @Override + public BlockState getStateForPlacement(BlockPlaceContext pContext) { + return this.defaultBlockState().setValue(POWERED, false); + } } diff --git a/src/main/java/com/atsuishio/superbwarfare/block/entity/FuMO25BlockEntity.java b/src/main/java/com/atsuishio/superbwarfare/block/entity/FuMO25BlockEntity.java index 59a713ed9..4625b2fbe 100644 --- a/src/main/java/com/atsuishio/superbwarfare/block/entity/FuMO25BlockEntity.java +++ b/src/main/java/com/atsuishio/superbwarfare/block/entity/FuMO25BlockEntity.java @@ -1,5 +1,6 @@ package com.atsuishio.superbwarfare.block.entity; +import com.atsuishio.superbwarfare.block.FuMO25Block; import com.atsuishio.superbwarfare.init.ModBlockEntities; import com.atsuishio.superbwarfare.menu.FuMO25Menu; import com.atsuishio.superbwarfare.network.dataslot.ContainerEnergyData; @@ -31,6 +32,9 @@ public class FuMO25BlockEntity extends BlockEntity implements MenuProvider { public static final int DEFAULT_RANGE = 96; public static final int MAX_RANGE = 128; + public static final int DEFAULT_ENERGY_COST = 256; + public static final int MAX_ENERGY_COST = 1024; + public static final int MAX_DATA_COUNT = 3; private LazyOptional energyHandler; @@ -53,7 +57,8 @@ public class FuMO25BlockEntity extends BlockEntity implements MenuProvider { @Override public void set(int pIndex, long pValue) { switch (pIndex) { - case 0 -> FuMO25BlockEntity.this.energyHandler.ifPresent(handler -> handler.receiveEnergy((int) pValue, false)); + case 0 -> + FuMO25BlockEntity.this.energyHandler.ifPresent(handler -> handler.receiveEnergy((int) pValue, false)); case 1 -> FuMO25BlockEntity.this.type = FuncType.values()[(int) pValue]; case 2 -> FuMO25BlockEntity.this.time = (int) pValue; } @@ -71,7 +76,42 @@ public class FuMO25BlockEntity extends BlockEntity implements MenuProvider { } public static void serverTick(Level pLevel, BlockPos pPos, BlockState pState, FuMO25BlockEntity blockEntity) { + int energy = blockEntity.energyHandler.map(EnergyStorage::getEnergyStored).orElse(0); + if (energy <= 0) { + if (pState.getValue(FuMO25Block.POWERED)) { + pLevel.setBlockAndUpdate(pPos, pState.setValue(FuMO25Block.POWERED, false)); + setChanged(pLevel, pPos, pState); + } + if (blockEntity.time > 0) { + blockEntity.time = 0; + blockEntity.setChanged(); + } + } else { + if (!pState.getValue(FuMO25Block.POWERED)) { + pLevel.setBlockAndUpdate(pPos, pState.setValue(FuMO25Block.POWERED, true)); + setChanged(pLevel, pPos, pState); + } + + FuncType funcType = blockEntity.type; + int energyCost; + if (funcType == FuncType.WIDER) { + energyCost = MAX_ENERGY_COST; + } else { + energyCost = DEFAULT_ENERGY_COST; + } + blockEntity.energyHandler.ifPresent(handler -> handler.extractEnergy(energyCost, false)); + + if (blockEntity.time > 0) { + blockEntity.time--; + blockEntity.setChanged(); + } + } + + if (blockEntity.time <= 0 && blockEntity.type != FuncType.NORMAL) { + blockEntity.type = FuncType.NORMAL; + blockEntity.setChanged(); + } } public static void clientTick(Level pLevel, BlockPos pPos, BlockState pState, FuMO25BlockEntity blockEntity) { diff --git a/src/main/java/com/atsuishio/superbwarfare/menu/FuMO25Menu.java b/src/main/java/com/atsuishio/superbwarfare/menu/FuMO25Menu.java index 5873ab614..df7e9247a 100644 --- a/src/main/java/com/atsuishio/superbwarfare/menu/FuMO25Menu.java +++ b/src/main/java/com/atsuishio/superbwarfare/menu/FuMO25Menu.java @@ -157,8 +157,14 @@ public class FuMO25Menu extends EnergyMenu { return this.containerData.get(1); } - public void setFuncType(byte type) { + public void setFuncTypeAndTime(byte type) { this.containerData.set(1, type); + int tick = switch (type) { + case 1, 2 -> 1200; + case 3 -> 600; + default -> 0; + }; + this.containerData.set(2, tick); } public long getTime() { diff --git a/src/main/java/com/atsuishio/superbwarfare/network/message/RadarChangeModeMessage.java b/src/main/java/com/atsuishio/superbwarfare/network/message/RadarChangeModeMessage.java index 349886528..c62de6c87 100644 --- a/src/main/java/com/atsuishio/superbwarfare/network/message/RadarChangeModeMessage.java +++ b/src/main/java/com/atsuishio/superbwarfare/network/message/RadarChangeModeMessage.java @@ -37,7 +37,7 @@ public class RadarChangeModeMessage { if (!player.containerMenu.stillValid(player)) { return; } - fuMO25Menu.setFuncType(mode); + fuMO25Menu.setFuncTypeAndTime(mode); } }); ctx.get().setPacketHandled(true);