ActuallyAdditions/src/main/java/de/ellpeck/actuallyadditions/mod/blocks/BlockBatteryBox.java

84 lines
3.2 KiB
Java
Raw Normal View History

/*
* 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
*
2017-01-01 16:23:26 +01:00
* © 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;
2024-03-02 21:23:08 +01:00
import net.minecraft.core.BlockPos;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityTicker;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.shapes.CollisionContext;
import net.minecraft.world.phys.shapes.VoxelShape;
2021-02-27 17:22:03 +01:00
import javax.annotation.Nullable;
2019-05-02 09:10:29 +02:00
public class BlockBatteryBox extends BlockContainerBase {
public BlockBatteryBox() {
2024-03-02 21:23:08 +01:00
super(ActuallyBlocks.defaultPickProps());
}
2021-02-27 17:22:03 +01:00
// @Override
// public AxisAlignedBB getBoundingBox(BlockState state, IBlockAccess source, BlockPos pos) {
// return BlockSlabs.AABB_BOTTOM_HALF;
// }
2023-01-15 17:56:02 +01:00
@Override
2024-03-02 21:23:08 +01:00
public VoxelShape getShape(BlockState state, BlockGetter worldIn, BlockPos pos, CollisionContext context) {
return VoxelShapes.BATBOX_SHAPE;
2023-01-15 17:56:02 +01:00
}
2024-03-02 21:23:08 +01:00
@Nullable
2023-01-15 17:56:02 +01:00
@Override
2024-03-02 21:23:08 +01:00
public BlockEntity newBlockEntity(BlockPos pos, BlockState state) {
return new TileEntityBatteryBox(pos, state);
2023-01-15 17:56:02 +01:00
}
2021-02-27 17:22:03 +01:00
@Nullable
2023-01-15 17:56:02 +01:00
@Override
2024-03-02 21:23:08 +01:00
public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, BlockState blockState, BlockEntityType<T> entityType) {
return level.isClientSide? TileEntityBatteryBox::clientTick : TileEntityBatteryBox::serverTick;
}
@Override
2024-03-02 21:23:08 +01:00
public InteractionResult use(BlockState state, Level world, BlockPos pos, Player player, InteractionHand hand, BlockHitResult hit) {
BlockEntity tile = world.getBlockEntity(pos);
2024-03-03 01:20:53 +01:00
if (tile instanceof TileEntityBatteryBox box) {
ItemStack stack = player.getItemInHand(hand);
2023-01-15 17:56:02 +01:00
if (!stack.isEmpty()) {
if (stack.getItem() instanceof ItemBattery && box.inv.getStackInSlot(0).isEmpty()) {
box.inv.setStackInSlot(0, stack.copy());
2022-06-24 21:38:07 +02:00
player.setItemInHand(hand, ItemStack.EMPTY);
2024-03-02 21:23:08 +01:00
return InteractionResult.SUCCESS;
}
2019-05-02 09:10:29 +02:00
} else {
ItemStack inSlot = box.inv.getStackInSlot(0);
2023-01-15 17:56:02 +01:00
if (!inSlot.isEmpty()) {
player.setItemInHand(hand, inSlot.copy());
2022-06-24 21:38:07 +02:00
box.inv.setStackInSlot(0, ItemStack.EMPTY);
2024-03-02 21:23:08 +01:00
return InteractionResult.SUCCESS;
}
}
}
2024-03-02 21:23:08 +01:00
return InteractionResult.PASS;
}
}