package com.thebrokenrail.energonrelics.block; import com.thebrokenrail.energonrelics.block.entity.infuser.InfuserBlockEntity; import com.thebrokenrail.energonrelics.api.block.energy.EnergyBlock; import net.fabricmc.fabric.api.object.builder.v1.block.FabricBlockSettings; import net.minecraft.block.Block; import net.minecraft.block.BlockState; import net.minecraft.block.Blocks; import net.minecraft.block.entity.BlockEntity; import net.minecraft.block.entity.BlockEntityType; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.ItemStack; import net.minecraft.state.StateManager; import net.minecraft.state.property.BooleanProperty; import net.minecraft.state.property.Properties; import net.minecraft.util.ActionResult; import net.minecraft.util.Hand; import net.minecraft.util.hit.BlockHitResult; import net.minecraft.util.math.BlockPos; import net.minecraft.world.World; import java.util.function.Function; public class InfuserBlock extends EnergyBlock { public static final BooleanProperty POWERED = Properties.POWERED; public InfuserBlock() { super(FabricBlockSettings.copy(Blocks.GOLD_BLOCK).nonOpaque().lightLevel(state -> state.get(POWERED) ? 7 : 0).emissiveLighting((state, world, pos) -> state.get(POWERED))); setDefaultState(getDefaultState().with(POWERED, false)); } @Override protected void appendProperties(StateManager.Builder builder) { super.appendProperties(builder); builder.add(POWERED); } @Override protected Function, BlockEntity> getFactory() { return InfuserBlockEntity::new; } @Override public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { boolean success; BlockEntity entity = world.getBlockEntity(pos); if (entity instanceof InfuserBlockEntity) { ItemStack stack = player.getStackInHand(hand); if (((InfuserBlockEntity) entity).insert(stack.getItem())) { if (!player.isCreative()) { stack.decrement(1); } success = true; } else { success = false; } } else { success = false; } if (success) { return ActionResult.SUCCESS; } else { return super.onUse(state, world, pos, player, hand, hit); } } @Override public void onBreak(World world, BlockPos pos, BlockState state, PlayerEntity player) { super.onBreak(world, pos, state, player); BlockEntity entity = world.getBlockEntity(pos); if (entity instanceof InfuserBlockEntity) { ((InfuserBlockEntity) entity).onBreak(); } } }