Blockstate values for presence of batteries/capacitors
This commit is contained in:
parent
e4cfa88437
commit
74658e3a69
@ -5,11 +5,14 @@ import net.minecraft.core.Direction;
|
||||
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;
|
||||
import net.minecraft.world.level.block.EntityBlock;
|
||||
import net.minecraft.world.level.block.entity.BlockEntity;
|
||||
import net.minecraft.world.level.block.entity.BlockEntityTicker;
|
||||
import net.minecraft.world.level.block.entity.BlockEntityType;
|
||||
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.shapes.BooleanOp;
|
||||
import net.minecraft.world.phys.shapes.CollisionContext;
|
||||
import net.minecraft.world.phys.shapes.Shapes;
|
||||
@ -22,12 +25,44 @@ import javax.annotation.Nullable;
|
||||
import java.util.List;
|
||||
|
||||
public class BlockBatteryBank extends BlockMatteryRotatable implements EntityBlock {
|
||||
public static final BooleanProperty[] BATTERY_SLOTS_PROPS = new BooleanProperty[] {
|
||||
BooleanProperty.create("battery_0"),
|
||||
BooleanProperty.create("battery_1"),
|
||||
BooleanProperty.create("battery_2"),
|
||||
BooleanProperty.create("battery_3"),
|
||||
BooleanProperty.create("battery_4"),
|
||||
BooleanProperty.create("battery_5"),
|
||||
BooleanProperty.create("battery_6"),
|
||||
BooleanProperty.create("battery_7"),
|
||||
BooleanProperty.create("battery_8"),
|
||||
BooleanProperty.create("battery_9"),
|
||||
BooleanProperty.create("battery_10"),
|
||||
BooleanProperty.create("battery_11"),
|
||||
};
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public BlockState getStateForPlacement(BlockPlaceContext context) {
|
||||
var state = super.getStateForPlacement(context);
|
||||
|
||||
for (var prop : BATTERY_SLOTS_PROPS)
|
||||
state = state.setValue(prop, false);
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, BlockState p_153213_, BlockEntityType<T> type) {
|
||||
return level.isClientSide || type != Registry.BlockEntities.BATTERY_BANK ? null : BlockEntityBatteryBank::tick;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
|
||||
builder.add(BATTERY_SLOTS_PROPS);
|
||||
super.createBlockStateDefinition(builder);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public BlockEntity newBlockEntity(BlockPos blockPos, BlockState blockState) {
|
||||
|
@ -2,10 +2,13 @@ package ru.dbotthepony.mc.otm.block;
|
||||
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.core.Direction;
|
||||
import net.minecraft.world.item.context.BlockPlaceContext;
|
||||
import net.minecraft.world.level.BlockGetter;
|
||||
import net.minecraft.world.level.block.Block;
|
||||
import net.minecraft.world.level.block.EntityBlock;
|
||||
import net.minecraft.world.level.block.entity.BlockEntity;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import net.minecraft.world.level.block.state.StateDefinition;
|
||||
import net.minecraft.world.phys.shapes.CollisionContext;
|
||||
import net.minecraft.world.phys.shapes.Shapes;
|
||||
import net.minecraft.world.phys.shapes.VoxelShape;
|
||||
@ -22,6 +25,23 @@ public class BlockMatterCapacitorBank extends BlockMatteryRotatable implements E
|
||||
return new BlockEntityMatterCapacitorBank(blockPos, blockState);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public BlockState getStateForPlacement(BlockPlaceContext context) {
|
||||
var state = super.getStateForPlacement(context);
|
||||
|
||||
for (var prop : BlockBatteryBank.BATTERY_SLOTS_PROPS)
|
||||
state = state.setValue(prop, false);
|
||||
|
||||
return state;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
|
||||
builder.add(BlockBatteryBank.BATTERY_SLOTS_PROPS);
|
||||
super.createBlockStateDefinition(builder);
|
||||
}
|
||||
|
||||
private static final List<VoxelShape> SHAPES;
|
||||
|
||||
static {
|
||||
|
@ -10,6 +10,7 @@ import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.inventory.AbstractContainerMenu;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.level.block.Block;
|
||||
import net.minecraft.world.level.block.entity.BlockEntity;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import net.minecraftforge.common.capabilities.Capability;
|
||||
@ -19,6 +20,7 @@ import net.minecraftforge.energy.IEnergyStorage;
|
||||
import net.minecraftforge.items.CapabilityItemHandler;
|
||||
import net.minecraftforge.items.IItemHandler;
|
||||
import ru.dbotthepony.mc.otm.Registry;
|
||||
import ru.dbotthepony.mc.otm.block.BlockBatteryBank;
|
||||
import ru.dbotthepony.mc.otm.block.BlockMatteryRotatable;
|
||||
import ru.dbotthepony.mc.otm.capability.IMatteryEnergyStorage;
|
||||
import ru.dbotthepony.mc.otm.capability.MatteryCapability;
|
||||
@ -31,30 +33,33 @@ import java.math.BigDecimal;
|
||||
import java.util.Optional;
|
||||
|
||||
public class BlockEntityBatteryBank extends BlockEntityMattery {
|
||||
// 5 на 3
|
||||
public final MatteryContainer battery_container = new MatteryContainer(this::setChanged, 5 * 3);
|
||||
// 6 на 2
|
||||
public final MatteryContainer battery_container = new MatteryContainer(this::setChanged, 6 * 2) {
|
||||
@Override
|
||||
public void setChanged(int slot, ItemStack new_state, ItemStack old_state) {
|
||||
super.setChanged(slot, new_state, old_state);
|
||||
|
||||
if (level != null) {
|
||||
var state = getBlockState();
|
||||
|
||||
for (int i = 0; i < BlockBatteryBank.BATTERY_SLOTS_PROPS.length; i++) {
|
||||
state = state.setValue(BlockBatteryBank.BATTERY_SLOTS_PROPS[i], getItem(i).getCapability(CapabilityEnergy.ENERGY).isPresent());
|
||||
}
|
||||
|
||||
if (state != getBlockState()) {
|
||||
level.setBlock(getBlockPos(), state, Block.UPDATE_CLIENTS);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
private final LazyOptional<IItemHandler> item_handler_resolver = LazyOptional.of(() -> battery_container.handler(
|
||||
(slot, stack) -> stack.getCapability(CapabilityEnergy.ENERGY).isPresent(),
|
||||
(slot, amount, stack) -> true
|
||||
));
|
||||
|
||||
public static class BatteryBankDistribution {
|
||||
private final BigDecimal[] distribution;
|
||||
private final BigDecimal max_throughput;
|
||||
public record BatteryBankDistribution(BigDecimal[] distribution, BigDecimal max_throughput) {
|
||||
|
||||
public BigDecimal max_throughput() {
|
||||
return max_throughput;
|
||||
}
|
||||
|
||||
public BigDecimal[] distribution() {
|
||||
return distribution;
|
||||
}
|
||||
|
||||
public BatteryBankDistribution(BigDecimal[] distribution, BigDecimal max_throughput) {
|
||||
this.distribution = distribution;
|
||||
this.max_throughput = max_throughput;
|
||||
}
|
||||
}
|
||||
|
||||
public static class BatteryBankEnergy implements IMatteryEnergyStorage {
|
||||
|
@ -10,9 +10,11 @@ import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.inventory.AbstractContainerMenu;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.level.block.Block;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
import net.minecraftforge.common.capabilities.Capability;
|
||||
import net.minecraftforge.common.util.LazyOptional;
|
||||
import ru.dbotthepony.mc.otm.block.BlockBatteryBank;
|
||||
import ru.dbotthepony.mc.otm.capability.IMatterGridCell;
|
||||
import ru.dbotthepony.mc.otm.matter.MatterGrid;
|
||||
import ru.dbotthepony.mc.otm.Registry;
|
||||
@ -142,7 +144,24 @@ public class BlockEntityMatterCapacitorBank extends BlockEntityMattery implement
|
||||
|
||||
private final LazyOptional<IMatterHandler> resolver = LazyOptional.of(() -> matter);
|
||||
|
||||
public MatteryContainer matter_container = new MatteryContainer(this::setChanged, 5 * 3);
|
||||
public MatteryContainer matter_container = new MatteryContainer(this::setChanged, 6 * 2) {
|
||||
@Override
|
||||
public void setChanged(int slot, ItemStack new_state, ItemStack old_state) {
|
||||
super.setChanged(slot, new_state, old_state);
|
||||
|
||||
if (level != null) {
|
||||
var state = getBlockState();
|
||||
|
||||
for (int i = 0; i < BlockBatteryBank.BATTERY_SLOTS_PROPS.length; i++) {
|
||||
state = state.setValue(BlockBatteryBank.BATTERY_SLOTS_PROPS[i], getItem(i).getCapability(MatteryCapability.MATTER).isPresent());
|
||||
}
|
||||
|
||||
if (state != getBlockState()) {
|
||||
level.setBlock(getBlockPos(), state, Block.UPDATE_CLIENTS);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
public BlockEntityMatterCapacitorBank(BlockPos p_155229_, BlockState p_155230_) {
|
||||
super(Registry.BlockEntities.MATTER_CAPACITOR_BANK, p_155229_, p_155230_);
|
||||
|
@ -35,9 +35,9 @@ public class BatteryBankMenu extends MatteryMenu {
|
||||
battery_widget = new BatteryLevelWidget(this, 13, 24, tile.getCapability(MatteryCapability.ENERGY).resolve().get());
|
||||
}
|
||||
|
||||
for (int row = 0; row < 3; row++)
|
||||
for (int column = 0; column < 5; column++)
|
||||
this.addSlot(new BatterySlot(batteries, row * 5 + column, 64 + column * 18, 24 + row * 18));
|
||||
for (int row = 0; row < 2; row++)
|
||||
for (int column = 0; column < 6; column++)
|
||||
this.addSlot(new BatterySlot(batteries, row * 5 + column, 44 + column * 18, 24 + row * 18));
|
||||
|
||||
addInventorySlots();
|
||||
}
|
||||
|
@ -27,9 +27,9 @@ public class MatterCapacitorBankMenu extends MatteryMenu {
|
||||
|
||||
Container container = tile != null ? tile.matter_container : new SimpleContainer(5 * 3);
|
||||
|
||||
for (int row = 0; row < 3; row++)
|
||||
for (int column = 0; column < 5; column++)
|
||||
this.addSlot(new MatterContainerInputSlot(container, row * 5 + column, 64 + column * 18, 20 + row * 18, true, IMatterHandler.MatterDirection.BIDIRECTIONAL));
|
||||
for (int row = 0; row < 2; row++)
|
||||
for (int column = 0; column < 6; column++)
|
||||
this.addSlot(new MatterContainerInputSlot(container, row * 5 + column, 44 + column * 18, 20 + row * 18, true, IMatterHandler.MatterDirection.BIDIRECTIONAL));
|
||||
|
||||
addInventorySlots();
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ import ru.dbotthepony.mc.otm.capability.MatteryCapability;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class BatterySlot extends Slot {
|
||||
public class BatterySlot extends MatterySlot {
|
||||
public boolean auto_bg;
|
||||
|
||||
public BatterySlot(Container container, int index, int x, int y, boolean auto_bg) {
|
||||
|
@ -4,7 +4,7 @@ import net.minecraft.world.Container;
|
||||
import net.minecraft.world.inventory.Slot;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
|
||||
public class MachineOutputSlot extends Slot {
|
||||
public class MachineOutputSlot extends MatterySlot {
|
||||
public boolean auto_bg;
|
||||
public boolean is_main_output;
|
||||
|
||||
|
@ -8,7 +8,7 @@ import ru.dbotthepony.mc.otm.capability.MatteryCapability;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
public class MatterContainerInputSlot extends Slot {
|
||||
public class MatterContainerInputSlot extends MatterySlot {
|
||||
public boolean auto_bg;
|
||||
public IMatterHandler.MatterDirection desired_direction;
|
||||
|
||||
|
@ -8,6 +8,7 @@ import ru.dbotthepony.mc.otm.OverdriveThatMatters;
|
||||
import ru.dbotthepony.mc.otm.container.MatteryContainer;
|
||||
|
||||
// Just a sign that this slot needs auto draw
|
||||
// or belongs to MatteryContainer
|
||||
public class MatterySlot extends Slot {
|
||||
public boolean auto_bg;
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user