ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/BlockBatteryBox.java
2022-06-24 14:38:07 -05:00

72 lines
2.6 KiB
Java

/*
* This file ("BlockBatteryBox.java") is part of the Actually Additions mod for Minecraft.
* It is created and owned by Ellpeck and distributed
* under the Actually Additions License to be found at
* http://ellpeck.de/actaddlicense
* View the source code at https://github.com/Ellpeck/ActuallyAdditions
*
* © 2015-2017 Ellpeck
*/
package de.ellpeck.actuallyadditions.mod.blocks;
import de.ellpeck.actuallyadditions.mod.blocks.base.BlockContainerBase;
import de.ellpeck.actuallyadditions.mod.items.ItemBattery;
import de.ellpeck.actuallyadditions.mod.tile.TileEntityBatteryBox;
import de.ellpeck.actuallyadditions.mod.util.StackUtil;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.World;
import javax.annotation.Nullable;
public class BlockBatteryBox extends BlockContainerBase {
public BlockBatteryBox() {
super(ActuallyBlocks.defaultPickProps(0));
}
// @Override
// public AxisAlignedBB getBoundingBox(BlockState state, IBlockAccess source, BlockPos pos) {
// return BlockSlabs.AABB_BOTTOM_HALF;
// }
@Nullable
//@Override
public TileEntity newBlockEntity(IBlockReader worldIn) {
return new TileEntityBatteryBox();
}
@Override
public ActionResultType use(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockRayTraceResult hit) {
TileEntity tile = world.getBlockEntity(pos);
if (tile instanceof TileEntityBatteryBox) {
TileEntityBatteryBox box = (TileEntityBatteryBox) tile;
ItemStack stack = player.getItemInHand(hand);
if (StackUtil.isValid(stack)) {
if (stack.getItem() instanceof ItemBattery && !StackUtil.isValid(box.inv.getStackInSlot(0))) {
box.inv.setStackInSlot(0, stack.copy());
player.setItemInHand(hand, ItemStack.EMPTY);
return ActionResultType.SUCCESS;
}
} else {
ItemStack inSlot = box.inv.getStackInSlot(0);
if (StackUtil.isValid(inSlot)) {
player.setItemInHand(hand, inSlot.copy());
box.inv.setStackInSlot(0, ItemStack.EMPTY);
return ActionResultType.SUCCESS;
}
}
}
return ActionResultType.PASS;
}
}