/
CombinableVerticalSlabBlock.java
130 lines (109 loc) · 4.75 KB
/
CombinableVerticalSlabBlock.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package games.enchanted.registry.types;
import com.mojang.serialization.MapCodec;
import net.minecraft.block.*;
import net.minecraft.fluid.FluidState;
import net.minecraft.fluid.Fluids;
import net.minecraft.item.ItemPlacementContext;
import net.minecraft.item.ItemStack;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvent;
import net.minecraft.state.StateManager;
import net.minecraft.state.property.BooleanProperty;
import net.minecraft.state.property.Properties;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;
import net.minecraft.util.shape.VoxelShape;
import net.minecraft.util.shape.VoxelShapes;
import net.minecraft.world.BlockView;
import net.minecraft.world.World;
import net.minecraft.world.WorldAccess;
public class CombinableVerticalSlabBlock extends HorizontalFacingBlock implements Waterloggable {
public static final BooleanProperty SINGLE = BooleanProperty.of("single_slab");
protected void appendProperties(StateManager.Builder<Block, BlockState> stateManager) {
stateManager.add(SINGLE);
stateManager.add(Properties.HORIZONTAL_FACING);
stateManager.add(Properties.WATERLOGGED);
}
private SoundEvent blockPlaceSound;
public CombinableVerticalSlabBlock(Settings settings, SoundEvent blockSoundEvent) {
super(settings);
setDefaultState(stateManager.getDefaultState().with(SINGLE, true));
setDefaultState(stateManager.getDefaultState().with(Properties.HORIZONTAL_FACING, Direction.NORTH));
setDefaultState(stateManager.getDefaultState().with(Properties.WATERLOGGED, false));
blockPlaceSound = blockSoundEvent;
}
public void playPlaceSound(World world, BlockPos pos) {
world.playSound(
null, pos, blockPlaceSound, SoundCategory.BLOCKS, 1f, 0.75f);
}
@Override
public VoxelShape getOutlineShape(BlockState state, BlockView view, BlockPos pos, ShapeContext ctx) {
// changes hitbox depending on block state
Direction dir = state.get(FACING);
if (!state.get(SINGLE)) {
return VoxelShapes.fullCube();
} else if (dir == Direction.NORTH) {
return VoxelShapes.cuboid(0.0f, 0.0f, 0.0f, 1.0f, 1.0f, 0.5f);
} else if (dir == Direction.SOUTH) {
return VoxelShapes.cuboid(0.0f, 0.0f, 0.5f, 1.0f, 1.0f, 1.0f);
} else if (dir == Direction.EAST) {
return VoxelShapes.cuboid(0.5f, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f);
} else if (dir == Direction.WEST) {
return VoxelShapes.cuboid(0.0f, 0.0f, 0.0f, 0.5f, 1.0f, 1.0f);
}
return VoxelShapes.fullCube();
}
@Override
public boolean hasSidedTransparency(BlockState state) {
return true;
}
protected boolean canReplace(BlockState state, ItemPlacementContext context) {
ItemStack itemStack = context.getStack();
if (!state.get(SINGLE) && !(itemStack.isOf(this.asItem()))) {
return false;
}
double hitposX = context.getHitPos().x - (double)context.getBlockPos().getX();
double hitposZ = context.getHitPos().z - (double)context.getBlockPos().getZ();
Direction facingDirection = state.get(Properties.HORIZONTAL_FACING);
switch (facingDirection.toString()) {
case "north":
return hitposZ >= 0.5;
case "east":
return hitposX <= 0.5;
case "south":
return hitposZ <= 0.5;
case "west":
return hitposX >= 0.5;
default:
return false;
}
}
@Override
public BlockState getPlacementState(ItemPlacementContext ctx) {
BlockState blockState = ctx.getWorld().getBlockState(ctx.getBlockPos());
if (blockState.isOf(this)) {
return blockState.with(SINGLE, false).with(Properties.WATERLOGGED, false);
}
return this.getDefaultState().with(Properties.HORIZONTAL_FACING, ctx.getHorizontalPlayerFacing());
}
@Override
public boolean tryFillWithFluid(WorldAccess world, BlockPos pos, BlockState state, FluidState fluidState) {
if (state.get(SINGLE) != false) {
return Waterloggable.super.tryFillWithFluid(world, pos, state, fluidState);
}
return false;
}
@Override
public FluidState getFluidState(BlockState state) {
if (state.get(Properties.WATERLOGGED).booleanValue()) {
return Fluids.WATER.getStill(false);
}
// return state.getFluidState();
return state.getFluidState();
}
@Override
protected MapCodec<? extends CombinableVerticalSlabBlock> getCodec() {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'getCodec'");
}
}