2016-12-18 17:28:29 +01:00
|
|
|
/*
|
|
|
|
* 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
|
2016-12-18 17:28:29 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
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;
|
2016-12-18 17:28:29 +01:00
|
|
|
|
2019-05-02 09:10:29 +02:00
|
|
|
public class BlockBatteryBox extends BlockContainerBase {
|
2016-12-18 17:28:29 +01:00
|
|
|
|
2021-02-27 13:24:45 +01:00
|
|
|
public BlockBatteryBox() {
|
2024-03-02 21:23:08 +01:00
|
|
|
super(ActuallyBlocks.defaultPickProps());
|
2016-12-18 17:28:29 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2016-12-18 17:28:29 +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;
|
2016-12-18 17:28:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@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);
|
2016-12-18 17:28:29 +01:00
|
|
|
|
2023-01-15 17:56:02 +01:00
|
|
|
if (!stack.isEmpty()) {
|
|
|
|
if (stack.getItem() instanceof ItemBattery && box.inv.getStackInSlot(0).isEmpty()) {
|
2018-06-23 01:39:30 +02:00
|
|
|
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;
|
2016-12-18 17:28:29 +01:00
|
|
|
}
|
2019-05-02 09:10:29 +02:00
|
|
|
} else {
|
2018-06-23 01:39:30 +02:00
|
|
|
ItemStack inSlot = box.inv.getStackInSlot(0);
|
2023-01-15 17:56:02 +01:00
|
|
|
if (!inSlot.isEmpty()) {
|
2021-08-22 17:09:06 +02:00
|
|
|
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;
|
2016-12-18 17:28:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-03-02 21:23:08 +01:00
|
|
|
return InteractionResult.PASS;
|
2016-12-18 17:28:29 +01:00
|
|
|
}
|
|
|
|
}
|