This repository has been archived on 2023-11-26. You can view files and clone it, but cannot push or open issues or pull requests.
EnergonRelics/src/main/java/com/thebrokenrail/energonrelics/block/InfuserBlock.java

79 lines
2.8 KiB
Java
Raw Normal View History

2020-07-30 02:51:29 +00:00
package com.thebrokenrail.energonrelics.block;
import com.thebrokenrail.energonrelics.block.entity.infuser.InfuserBlockEntity;
2020-08-04 17:06:11 +00:00
import com.thebrokenrail.energonrelics.api.block.energy.EnergyBlock;
2020-07-30 02:51:29 +00:00
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<Block, BlockState> builder) {
super.appendProperties(builder);
builder.add(POWERED);
}
@Override
protected Function<BlockEntityType<BlockEntity>, 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();
}
}
}