添加雷达server tick方法

This commit is contained in:
17146 2024-12-26 01:32:06 +08:00
parent 9682e6f9b2
commit 594707ae77
4 changed files with 66 additions and 3 deletions

View file

@ -6,6 +6,7 @@ import net.minecraft.core.BlockPos;
import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult; import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player; 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.BlockGetter;
import net.minecraft.world.level.Level; import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.Block; 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.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockBehaviour; import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.BlockState; 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.BlockHitResult;
import net.minecraft.world.phys.shapes.CollisionContext; import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.Shapes; import net.minecraft.world.phys.shapes.Shapes;
@ -25,8 +28,11 @@ import org.jetbrains.annotations.Nullable;
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public class FuMO25Block extends Block implements EntityBlock { public class FuMO25Block extends Block implements EntityBlock {
public static final BooleanProperty POWERED = BooleanProperty.create("powered");
public FuMO25Block() { public FuMO25Block() {
super(BlockBehaviour.Properties.of().sound(SoundType.METAL).strength(3.0f).requiresCorrectToolForDrops()); super(BlockBehaviour.Properties.of().sound(SoundType.METAL).strength(3.0f).requiresCorrectToolForDrops());
this.registerDefaultState(this.stateDefinition.any().setValue(POWERED, false));
} }
@Override @Override
@ -79,4 +85,15 @@ public class FuMO25Block extends Block implements EntityBlock {
super.onRemove(pState, pLevel, pPos, pNewState, pMovedByPiston); super.onRemove(pState, pLevel, pPos, pNewState, pMovedByPiston);
} }
@Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> pBuilder) {
pBuilder.add(POWERED);
}
@Nullable
@Override
public BlockState getStateForPlacement(BlockPlaceContext pContext) {
return this.defaultBlockState().setValue(POWERED, false);
}
} }

View file

@ -1,5 +1,6 @@
package com.atsuishio.superbwarfare.block.entity; package com.atsuishio.superbwarfare.block.entity;
import com.atsuishio.superbwarfare.block.FuMO25Block;
import com.atsuishio.superbwarfare.init.ModBlockEntities; import com.atsuishio.superbwarfare.init.ModBlockEntities;
import com.atsuishio.superbwarfare.menu.FuMO25Menu; import com.atsuishio.superbwarfare.menu.FuMO25Menu;
import com.atsuishio.superbwarfare.network.dataslot.ContainerEnergyData; 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 DEFAULT_RANGE = 96;
public static final int MAX_RANGE = 128; 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; public static final int MAX_DATA_COUNT = 3;
private LazyOptional<EnergyStorage> energyHandler; private LazyOptional<EnergyStorage> energyHandler;
@ -53,7 +57,8 @@ public class FuMO25BlockEntity extends BlockEntity implements MenuProvider {
@Override @Override
public void set(int pIndex, long pValue) { public void set(int pIndex, long pValue) {
switch (pIndex) { 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 1 -> FuMO25BlockEntity.this.type = FuncType.values()[(int) pValue];
case 2 -> FuMO25BlockEntity.this.time = (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) { 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) { public static void clientTick(Level pLevel, BlockPos pPos, BlockState pState, FuMO25BlockEntity blockEntity) {

View file

@ -157,8 +157,14 @@ public class FuMO25Menu extends EnergyMenu {
return this.containerData.get(1); return this.containerData.get(1);
} }
public void setFuncType(byte type) { public void setFuncTypeAndTime(byte type) {
this.containerData.set(1, 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() { public long getTime() {

View file

@ -37,7 +37,7 @@ public class RadarChangeModeMessage {
if (!player.containerMenu.stillValid(player)) { if (!player.containerMenu.stillValid(player)) {
return; return;
} }
fuMO25Menu.setFuncType(mode); fuMO25Menu.setFuncTypeAndTime(mode);
} }
}); });
ctx.get().setPacketHandled(true); ctx.get().setPacketHandled(true);