Fix Color Handling for Battery Neon (Forge/Fabric)

This commit is contained in:
2022-09-25 02:19:05 +02:00
parent d1fa3eac46
commit 7e978f539d
2 changed files with 47 additions and 7 deletions

View File

@@ -1,6 +1,7 @@
package me.hypherionmc.hyperlighting.common.blockentities; package me.hypherionmc.hyperlighting.common.blockentities;
import me.hypherionmc.craterlib.api.blockentities.ITickable; import me.hypherionmc.craterlib.api.blockentities.ITickable;
import me.hypherionmc.craterlib.api.blockentities.caps.ForgeCapability;
import me.hypherionmc.craterlib.common.blockentity.CraterBlockEntity; import me.hypherionmc.craterlib.common.blockentity.CraterBlockEntity;
import me.hypherionmc.craterlib.systems.SimpleInventory; import me.hypherionmc.craterlib.systems.SimpleInventory;
import me.hypherionmc.craterlib.systems.energy.CustomEnergyStorage; import me.hypherionmc.craterlib.systems.energy.CustomEnergyStorage;
@@ -8,12 +9,18 @@ import me.hypherionmc.hyperlighting.common.blocks.BatteryNeon;
import me.hypherionmc.hyperlighting.common.init.HLBlockEntities; import me.hypherionmc.hyperlighting.common.init.HLBlockEntities;
import me.hypherionmc.hyperlighting.common.items.WirelessBattery; import me.hypherionmc.hyperlighting.common.items.WirelessBattery;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.core.Direction;
import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.CompoundTag;
import net.minecraft.world.Containers;
import net.minecraft.world.item.DyeColor;
import net.minecraft.world.item.DyeItem;
import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level; 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 java.util.Optional;
/** /**
* @author HypherionSA * @author HypherionSA
* @date 24/09/2022 * @date 24/09/2022
@@ -61,6 +68,15 @@ public class BatteryNeonBlockEntity extends CraterBlockEntity implements ITickab
@Override @Override
public void sendUpdates() { public void sendUpdates() {
BlockState state = level.getBlockState(this.getBlockPos()); BlockState state = level.getBlockState(this.getBlockPos());
if (inventory.getItemHandler().getItem(1).getItem() instanceof DyeItem dyeItem) {
if (state.getValue(BatteryNeon.COLOR) != dyeItem.getDyeColor()) {
state = state.setValue(BatteryNeon.COLOR, dyeItem.getDyeColor());
level.setBlock(this.getBlockPos(), state, 2);
}
} else {
state = state.setValue(BatteryNeon.COLOR, DyeColor.WHITE);
level.setBlock(this.getBlockPos(), state, 2);
}
this.level.blockEntityChanged(this.getBlockPos()); this.level.blockEntityChanged(this.getBlockPos());
this.level.sendBlockUpdated(this.getBlockPos(), this.level.getBlockState(this.getBlockPos()), state, 3); this.level.sendBlockUpdated(this.getBlockPos(), this.level.getBlockState(this.getBlockPos()), state, 3);
this.setChanged(); this.setChanged();
@@ -97,4 +113,28 @@ public class BatteryNeonBlockEntity extends CraterBlockEntity implements ITickab
} }
this.sendUpdates(); this.sendUpdates();
} }
@Override
public void setRemoved() {
dropInventory();
super.setRemoved();
}
public void dropInventory() {
if (!inventory.getItemHandler().getItem(0).isEmpty()) {
Containers.dropItemStack(level, worldPosition.getX(), worldPosition.getY(), worldPosition.getZ(), inventory.getItemHandler().getItem(0));
}
if (!inventory.getItemHandler().getItem(1).isEmpty()) {
Containers.dropItemStack(level, worldPosition.getX(), worldPosition.getY(), worldPosition.getZ(), inventory.getItemHandler().getItem(1));
}
}
@Override
public <T> Optional<T> getForgeCapability(ForgeCapability capability, Direction side) {
if (capability == ForgeCapability.ENERGY && side == null) {
return (Optional<T>) Optional.of(energyStorage);
}
return Optional.empty();
}
} }

View File

@@ -33,6 +33,7 @@ import net.minecraft.world.level.block.state.StateDefinition;
import net.minecraft.world.level.block.state.properties.BlockStateProperties; import net.minecraft.world.level.block.state.properties.BlockStateProperties;
import net.minecraft.world.level.block.state.properties.BooleanProperty; import net.minecraft.world.level.block.state.properties.BooleanProperty;
import net.minecraft.world.level.block.state.properties.DirectionProperty; import net.minecraft.world.level.block.state.properties.DirectionProperty;
import net.minecraft.world.level.block.state.properties.EnumProperty;
import net.minecraft.world.level.material.Material; import net.minecraft.world.level.material.Material;
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;
@@ -50,6 +51,8 @@ public class BatteryNeon extends BaseEntityBlock implements DyableBlock {
public static final BooleanProperty LIT = BlockStateProperties.LIT; public static final BooleanProperty LIT = BlockStateProperties.LIT;
public static final DirectionProperty FACING = BlockStateProperties.FACING; public static final DirectionProperty FACING = BlockStateProperties.FACING;
public static final EnumProperty<DyeColor> COLOR = EnumProperty.create("color", DyeColor.class);
private static final VoxelShape DOWN_BOUNDING_BOX = Block.box(0, 0, 7.008, 16, 3.008, 8.992); private static final VoxelShape DOWN_BOUNDING_BOX = Block.box(0, 0, 7.008, 16, 3.008, 8.992);
private static final VoxelShape UP_BOUNDING_BOX = Block.box(0, 12.8, 7.008, 16, 16, 8.992); private static final VoxelShape UP_BOUNDING_BOX = Block.box(0, 12.8, 7.008, 16, 16, 8.992);
private static final VoxelShape SOUTH_BOUNDING_BOX = Block.box(0, 7.008, 12.992, 16, 8.992, 16); private static final VoxelShape SOUTH_BOUNDING_BOX = Block.box(0, 7.008, 12.992, 16, 8.992, 16);
@@ -59,7 +62,7 @@ public class BatteryNeon extends BaseEntityBlock implements DyableBlock {
public BatteryNeon(String name) { public BatteryNeon(String name) {
super(Properties.of(Material.GLASS).sound(SoundType.GLASS).lightLevel(BlockStateUtils.createLightLevelFromLitBlockState(14))); super(Properties.of(Material.GLASS).sound(SoundType.GLASS).lightLevel(BlockStateUtils.createLightLevelFromLitBlockState(14)));
this.registerDefaultState(this.defaultBlockState().setValue(FACING, Direction.NORTH)); this.registerDefaultState(this.defaultBlockState().setValue(FACING, Direction.NORTH).setValue(COLOR, DyeColor.WHITE));
HLItems.ITEMS.register(name, () -> new BlockItemDyable(this, new Item.Properties().tab(CommonRegistration.LIGHTS_TAB))); HLItems.ITEMS.register(name, () -> new BlockItemDyable(this, new Item.Properties().tab(CommonRegistration.LIGHTS_TAB)));
} }
@@ -111,14 +114,14 @@ public class BatteryNeon extends BaseEntityBlock implements DyableBlock {
@Override @Override
protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) { protected void createBlockStateDefinition(StateDefinition.Builder<Block, BlockState> builder) {
builder.add(LIT, FACING); builder.add(LIT, FACING, COLOR);
super.createBlockStateDefinition(builder); super.createBlockStateDefinition(builder);
} }
@Nullable @Nullable
@Override @Override
public BlockState getStateForPlacement(BlockPlaceContext context) { public BlockState getStateForPlacement(BlockPlaceContext context) {
return this.defaultBlockState().setValue(FACING, context.getClickedFace()).setValue(LIT, false); return this.defaultBlockState().setValue(FACING, context.getClickedFace()).setValue(LIT, false).setValue(COLOR, DyeColor.WHITE);
} }
@@ -131,13 +134,10 @@ public class BatteryNeon extends BaseEntityBlock implements DyableBlock {
public BlockColor dyeHandler() { public BlockColor dyeHandler() {
return (state, world, pos, tintIndex) -> { return (state, world, pos, tintIndex) -> {
if (state.getValue(LIT)) { if (state.getValue(LIT)) {
if (world != null && world.getBlockEntity(pos) instanceof BatteryNeonBlockEntity be) { return state.getValue(COLOR).getMaterialColor().col;
return (be.getInventory().getItemHandler().getItem(1).getItem() instanceof DyeItem dyeItem ? dyeItem.getDyeColor().getMaterialColor().col : DyeColor.WHITE.getTextColor());
}
} else { } else {
return DyeColor.BLACK.getMaterialColor().col; return DyeColor.BLACK.getMaterialColor().col;
} }
return DyeColor.BLACK.getMaterialColor().col;
}; };
} }