/
LootrInventoryBlock.java
110 lines (95 loc) · 3.99 KB
/
LootrInventoryBlock.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package noobanidus.mods.lootr.blocks;
import net.minecraft.block.BlockState;
import net.minecraft.block.ChestBlock;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.fluid.FluidState;
import net.minecraft.fluid.Fluids;
import net.minecraft.inventory.container.INamedContainerProvider;
import net.minecraft.item.BlockItemUseContext;
import net.minecraft.state.properties.ChestType;
import net.minecraft.tileentity.ChestTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.tileentity.TileEntityType;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.Direction;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.util.math.shapes.ISelectionContext;
import net.minecraft.util.math.shapes.VoxelShape;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.IWorld;
import net.minecraft.world.World;
import net.minecraft.world.server.ServerWorld;
import noobanidus.mods.lootr.data.NewChestData;
import noobanidus.mods.lootr.init.ModTiles;
import noobanidus.mods.lootr.tiles.SpecialLootChestTile;
import noobanidus.mods.lootr.tiles.SpecialLootInventoryTile;
import noobanidus.mods.lootr.util.ChestUtil;
import javax.annotation.Nullable;
import java.util.UUID;
import java.util.function.Supplier;
@SuppressWarnings("NullableProblems")
public class LootrInventoryBlock extends ChestBlock {
public LootrInventoryBlock(Properties properties) {
this(properties, () -> ModTiles.SPECIAL_LOOT_INVENTORY);
}
public LootrInventoryBlock(Properties builder, Supplier<TileEntityType<? extends ChestTileEntity>> tileEntityTypeIn) {
super(builder, tileEntityTypeIn);
}
@Override
public ActionResultType onBlockActivated(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockRayTraceResult trace) {
if (player.isSneaking()) {
ChestUtil.handleLootSneak(this, world, pos, player);
} else if (!ChestBlock.isBlocked(world, pos)) {
ChestUtil.handleLootInventory(this, world, pos, player);
}
return ActionResultType.SUCCESS;
}
@Override
public void onReplaced(BlockState oldState, World world, BlockPos pos, BlockState newState, boolean isMoving) {
if (oldState.getBlock() != newState.getBlock() && world instanceof ServerWorld) {
NewChestData.deleteLootChest((ServerWorld) world, pos);
}
super.onReplaced(oldState, world, pos, newState, isMoving);
}
@Override
public boolean hasTileEntity(BlockState state) {
return true;
}
@Override
public TileEntity createNewTileEntity(IBlockReader p_196283_1_) {
return new SpecialLootInventoryTile();
}
@Nullable
@Override
public TileEntity createTileEntity(BlockState state, IBlockReader world) {
return new SpecialLootInventoryTile();
}
@Override
public BlockState updatePostPlacement(BlockState stateIn, Direction facing, BlockState facingState, IWorld worldIn, BlockPos currentPos, BlockPos facingPos) {
if (stateIn.get(WATERLOGGED)) {
worldIn.getPendingFluidTicks().scheduleTick(currentPos, Fluids.WATER, Fluids.WATER.getTickRate(worldIn));
}
return stateIn;
}
@Override
public VoxelShape getShape(BlockState state, IBlockReader worldIn, BlockPos pos, ISelectionContext context) {
return SHAPE_SINGLE;
}
@Override
public BlockState getStateForPlacement(BlockItemUseContext context) {
Direction direction = context.getPlacementHorizontalFacing().getOpposite();
FluidState fluidstate = context.getWorld().getFluidState(context.getPos());
return this.getDefaultState().with(FACING, direction).with(TYPE, ChestType.SINGLE).with(WATERLOGGED, fluidstate.getFluid() == Fluids.WATER);
}
@Override
public FluidState getFluidState(BlockState state) {
return state.get(WATERLOGGED) ? Fluids.WATER.getStillFluidState(false) : super.getFluidState(state);
}
@Override
@Nullable
public INamedContainerProvider getContainer(BlockState state, World worldIn, BlockPos pos) {
return null;
}
}