From 7e978f539db8292458d5c62d98ab81d34502bc4f Mon Sep 17 00:00:00 2001 From: HypherionMC Date: Sun, 25 Sep 2022 02:19:05 +0200 Subject: [PATCH] Fix Color Handling for Battery Neon (Forge/Fabric) --- .../blockentities/BatteryNeonBlockEntity.java | 40 +++++++++++++++++++ .../common/blocks/BatteryNeon.java | 14 +++---- 2 files changed, 47 insertions(+), 7 deletions(-) diff --git a/Common/src/main/java/me/hypherionmc/hyperlighting/common/blockentities/BatteryNeonBlockEntity.java b/Common/src/main/java/me/hypherionmc/hyperlighting/common/blockentities/BatteryNeonBlockEntity.java index d862573..6668384 100644 --- a/Common/src/main/java/me/hypherionmc/hyperlighting/common/blockentities/BatteryNeonBlockEntity.java +++ b/Common/src/main/java/me/hypherionmc/hyperlighting/common/blockentities/BatteryNeonBlockEntity.java @@ -1,6 +1,7 @@ package me.hypherionmc.hyperlighting.common.blockentities; 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.systems.SimpleInventory; 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.items.WirelessBattery; import net.minecraft.core.BlockPos; +import net.minecraft.core.Direction; 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.level.Level; import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.block.state.BlockState; +import java.util.Optional; + /** * @author HypherionSA * @date 24/09/2022 @@ -61,6 +68,15 @@ public class BatteryNeonBlockEntity extends CraterBlockEntity implements ITickab @Override public void sendUpdates() { 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.sendBlockUpdated(this.getBlockPos(), this.level.getBlockState(this.getBlockPos()), state, 3); this.setChanged(); @@ -97,4 +113,28 @@ public class BatteryNeonBlockEntity extends CraterBlockEntity implements ITickab } 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 Optional getForgeCapability(ForgeCapability capability, Direction side) { + if (capability == ForgeCapability.ENERGY && side == null) { + return (Optional) Optional.of(energyStorage); + } + return Optional.empty(); + } } diff --git a/Common/src/main/java/me/hypherionmc/hyperlighting/common/blocks/BatteryNeon.java b/Common/src/main/java/me/hypherionmc/hyperlighting/common/blocks/BatteryNeon.java index fe1239a..666829b 100644 --- a/Common/src/main/java/me/hypherionmc/hyperlighting/common/blocks/BatteryNeon.java +++ b/Common/src/main/java/me/hypherionmc/hyperlighting/common/blocks/BatteryNeon.java @@ -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.BooleanProperty; 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.phys.BlockHitResult; 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 DirectionProperty FACING = BlockStateProperties.FACING; + public static final EnumProperty 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 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); @@ -59,7 +62,7 @@ public class BatteryNeon extends BaseEntityBlock implements DyableBlock { public BatteryNeon(String name) { 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))); } @@ -111,14 +114,14 @@ public class BatteryNeon extends BaseEntityBlock implements DyableBlock { @Override protected void createBlockStateDefinition(StateDefinition.Builder builder) { - builder.add(LIT, FACING); + builder.add(LIT, FACING, COLOR); super.createBlockStateDefinition(builder); } @Nullable @Override 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() { return (state, world, pos, tintIndex) -> { if (state.getValue(LIT)) { - if (world != null && world.getBlockEntity(pos) instanceof BatteryNeonBlockEntity be) { - return (be.getInventory().getItemHandler().getItem(1).getItem() instanceof DyeItem dyeItem ? dyeItem.getDyeColor().getMaterialColor().col : DyeColor.WHITE.getTextColor()); - } + return state.getValue(COLOR).getMaterialColor().col; } else { return DyeColor.BLACK.getMaterialColor().col; } - return DyeColor.BLACK.getMaterialColor().col; }; }