优化判定

This commit is contained in:
17146 2024-12-30 17:16:33 +08:00
parent b389cfed15
commit f441ca3dcf
4 changed files with 22 additions and 2 deletions

View file

@ -11,6 +11,7 @@ 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;
import net.minecraft.world.level.block.EntityBlock; import net.minecraft.world.level.block.EntityBlock;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.SoundType; import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityTicker; import net.minecraft.world.level.block.entity.BlockEntityTicker;
@ -50,7 +51,12 @@ public class FuMO25Block extends Block implements EntityBlock {
@Override @Override
public VoxelShape getShape(BlockState pState, BlockGetter pLevel, BlockPos pPos, CollisionContext pContext) { public VoxelShape getShape(BlockState pState, BlockGetter pLevel, BlockPos pPos, CollisionContext pContext) {
return Shapes.or(box(1, 0, 1, 15, 1, 15), box(7, 1, 7, 9, 16, 9)); return Shapes.or(box(1, 0, 1, 15, 6, 15), box(6, 6, 6, 10, 24, 10));
}
@Override
public RenderShape getRenderShape(BlockState pState) {
return RenderShape.MODEL;
} }
@Nullable @Nullable

View file

@ -45,12 +45,13 @@ public class FuMO25BlockEntity extends BlockEntity implements MenuProvider {
public static final int DEFAULT_MIN_ENERGY = 64000; public static final int DEFAULT_MIN_ENERGY = 64000;
public static final int MAX_DATA_COUNT = 3; public static final int MAX_DATA_COUNT = 4;
private LazyOptional<EnergyStorage> energyHandler; private LazyOptional<EnergyStorage> energyHandler;
public FuncType type = FuncType.NORMAL; public FuncType type = FuncType.NORMAL;
public int time = 0; public int time = 0;
public boolean powered = false;
protected final ContainerEnergyData dataAccess = new ContainerEnergyData() { protected final ContainerEnergyData dataAccess = new ContainerEnergyData() {
@ -60,6 +61,7 @@ public class FuMO25BlockEntity extends BlockEntity implements MenuProvider {
case 0 -> FuMO25BlockEntity.this.energyHandler.map(EnergyStorage::getEnergyStored).orElse(0); case 0 -> FuMO25BlockEntity.this.energyHandler.map(EnergyStorage::getEnergyStored).orElse(0);
case 1 -> FuMO25BlockEntity.this.type.ordinal(); case 1 -> FuMO25BlockEntity.this.type.ordinal();
case 2 -> FuMO25BlockEntity.this.time; case 2 -> FuMO25BlockEntity.this.time;
case 3 -> FuMO25BlockEntity.this.powered ? 1 : 0;
default -> 0; default -> 0;
}; };
} }
@ -71,6 +73,7 @@ public class FuMO25BlockEntity extends BlockEntity implements MenuProvider {
FuMO25BlockEntity.this.energyHandler.ifPresent(handler -> handler.receiveEnergy((int) pValue, false)); 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;
case 3 -> FuMO25BlockEntity.this.powered = pValue == 1;
} }
} }
@ -99,6 +102,7 @@ public class FuMO25BlockEntity extends BlockEntity implements MenuProvider {
if (energy < energyCost) { if (energy < energyCost) {
if (pState.getValue(FuMO25Block.POWERED)) { if (pState.getValue(FuMO25Block.POWERED)) {
pLevel.setBlockAndUpdate(pPos, pState.setValue(FuMO25Block.POWERED, false)); pLevel.setBlockAndUpdate(pPos, pState.setValue(FuMO25Block.POWERED, false));
blockEntity.powered = false;
setChanged(pLevel, pPos, pState); setChanged(pLevel, pPos, pState);
} }
if (blockEntity.time > 0) { if (blockEntity.time > 0) {
@ -109,6 +113,7 @@ public class FuMO25BlockEntity extends BlockEntity implements MenuProvider {
if (!pState.getValue(FuMO25Block.POWERED)) { if (!pState.getValue(FuMO25Block.POWERED)) {
if (energy >= DEFAULT_MIN_ENERGY) { if (energy >= DEFAULT_MIN_ENERGY) {
pLevel.setBlockAndUpdate(pPos, pState.setValue(FuMO25Block.POWERED, true)); pLevel.setBlockAndUpdate(pPos, pState.setValue(FuMO25Block.POWERED, true));
blockEntity.powered = true;
setChanged(pLevel, pPos, pState); setChanged(pLevel, pPos, pState);
} }
} else { } else {
@ -156,6 +161,7 @@ public class FuMO25BlockEntity extends BlockEntity implements MenuProvider {
} }
this.type = FuncType.values()[Mth.clamp(pTag.getInt("Type"), 0, 3)]; this.type = FuncType.values()[Mth.clamp(pTag.getInt("Type"), 0, 3)];
this.time = pTag.getInt("Time"); this.time = pTag.getInt("Time");
this.powered = pTag.getBoolean("Powered");
} }
@Override @Override
@ -165,6 +171,7 @@ public class FuMO25BlockEntity extends BlockEntity implements MenuProvider {
getCapability(ForgeCapabilities.ENERGY).ifPresent(handler -> pTag.put("Energy", ((EnergyStorage) handler).serializeNBT())); getCapability(ForgeCapabilities.ENERGY).ifPresent(handler -> pTag.put("Energy", ((EnergyStorage) handler).serializeNBT()));
pTag.putInt("Type", this.type.ordinal()); pTag.putInt("Type", this.type.ordinal());
pTag.putInt("Time", this.time); pTag.putInt("Time", this.time);
pTag.putBoolean("Powered", this.powered);
} }
@Override @Override

View file

@ -95,6 +95,7 @@ public class FuMO25Screen extends AbstractContainerScreen<FuMO25Menu> {
if (entities == null || entities.isEmpty()) return; if (entities == null || entities.isEmpty()) return;
var pos = FuMO25ScreenHelper.pos; var pos = FuMO25ScreenHelper.pos;
if (pos == null) return; if (pos == null) return;
if (!FuMO25Screen.this.menu.isPowered()) return;
int type = (int) FuMO25Screen.this.menu.getFuncType(); int type = (int) FuMO25Screen.this.menu.getFuncType();
int range = type == 1 ? FuMO25BlockEntity.MAX_RANGE : FuMO25BlockEntity.DEFAULT_RANGE; int range = type == 1 ? FuMO25BlockEntity.MAX_RANGE : FuMO25BlockEntity.DEFAULT_RANGE;
@ -121,6 +122,7 @@ public class FuMO25Screen extends AbstractContainerScreen<FuMO25Menu> {
private void renderScan(GuiGraphics guiGraphics) { private void renderScan(GuiGraphics guiGraphics) {
if (FuMO25Screen.this.menu.getEnergy() <= 0) return; if (FuMO25Screen.this.menu.getEnergy() <= 0) return;
if (!FuMO25Screen.this.menu.isPowered()) return;
var poseStack = guiGraphics.pose(); var poseStack = guiGraphics.pose();
poseStack.pushPose(); poseStack.pushPose();
@ -179,6 +181,7 @@ public class FuMO25Screen extends AbstractContainerScreen<FuMO25Menu> {
var pos = FuMO25ScreenHelper.pos; var pos = FuMO25ScreenHelper.pos;
if (pos == null) return super.mouseClicked(pMouseX, pMouseY, pButton); if (pos == null) return super.mouseClicked(pMouseX, pMouseY, pButton);
if (pButton != 0) return super.mouseClicked(pMouseX, pMouseY, pButton); if (pButton != 0) return super.mouseClicked(pMouseX, pMouseY, pButton);
if (!FuMO25Screen.this.menu.isPowered()) return super.mouseClicked(pMouseX, pMouseY, pButton);
int type = (int) FuMO25Screen.this.menu.getFuncType(); int type = (int) FuMO25Screen.this.menu.getFuncType();
int range = type == 1 ? FuMO25BlockEntity.MAX_RANGE : FuMO25BlockEntity.DEFAULT_RANGE; int range = type == 1 ? FuMO25BlockEntity.MAX_RANGE : FuMO25BlockEntity.DEFAULT_RANGE;

View file

@ -185,6 +185,10 @@ public class FuMO25Menu extends EnergyMenu {
return this.containerData.get(2); return this.containerData.get(2);
} }
public boolean isPowered() {
return this.containerData.get(3) == 1;
}
static class ParaSlot extends Slot { static class ParaSlot extends Slot {
public ParaSlot(Container pContainer, int pSlot, int pX, int pY) { public ParaSlot(Container pContainer, int pSlot, int pX, int pY) {