-
Notifications
You must be signed in to change notification settings - Fork 2
/
CombinableVerticalSlabBlock.java
193 lines (158 loc) · 7.03 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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package games.enchanted.registry.types;
import com.mojang.serialization.MapCodec;
import net.minecraft.block.*;
import net.minecraft.entity.ai.pathing.NavigationType;
import net.minecraft.entity.player.PlayerEntity;
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.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.hit.BlockHitResult;
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(this.stateManager.getDefaultState().with(SINGLE, true));
setDefaultState(this.stateManager.getDefaultState().with(Properties.HORIZONTAL_FACING, Direction.NORTH));
this.setDefaultState((BlockState) ((BlockState) this.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;
}
@Override
public boolean canPathfindThrough(BlockState state, BlockView world, BlockPos pos, NavigationType type) {
return false;
}
@Override
public ActionResult onUse(BlockState state, World world, BlockPos pos,
PlayerEntity player, Hand hand,
BlockHitResult hit) {
ActionResult ACTION;
ACTION = combineSlab(state, world, pos, player, hand, hit);
return ACTION;
}
private ActionResult combineSlab(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand,
BlockHitResult hit) {
player.getStackInHand(hand);
// checks if player clicked on block with same item type
if (player.getAbilities().allowModifyWorld
&& ItemStack.areEqual(new ItemStack(this), player.getStackInHand(hand))
&& state.get(SINGLE)) {
ActionResult RESULT = ActionResult.FAIL;
player.getBlockX();
if (state.get(SINGLE)) {
Direction HITSIDE = hit.getSide();
// is facing north and hit from south
if (state.get(FACING) == Direction.NORTH) {
if (HITSIDE == Direction.SOUTH) {
world.setBlockState(pos, state.with(SINGLE, false));
RESULT = sucessfulPlace(player, hand, world, pos);
}
}
// is facing east and hit from west
else if (state.get(FACING) == Direction.EAST) {
if (HITSIDE == Direction.WEST) {
world.setBlockState(pos, state.with(SINGLE, false));
RESULT = sucessfulPlace(player, hand, world, pos);
}
}
// is facing south and hit from north
else if (state.get(FACING) == Direction.SOUTH) {
if (HITSIDE == Direction.NORTH) {
world.setBlockState(pos, state.with(SINGLE, false));
RESULT = sucessfulPlace(player, hand, world, pos);
}
}
// is facing west and hit from east
else if (state.get(FACING) == Direction.WEST) {
if (HITSIDE == Direction.EAST) {
world.setBlockState(pos, state.with(SINGLE, false));
RESULT = sucessfulPlace(player, hand, world, pos);
}
}
else {
}
}
return RESULT;
} else {
return ActionResult.PASS;
}
}
private ActionResult sucessfulPlace(PlayerEntity player, Hand hand, World world, BlockPos pos) {
// checks if player is in creative and removes 1 item if not
boolean isInCreative = player.getAbilities().creativeMode;
if (!isInCreative) {
player.getStackInHand(hand).setCount(player.getStackInHand(hand).getCount() - 1);
}
playPlaceSound(world, pos);
// makes arm swing
return ActionResult.success(world.isClient);
}
@Override
public BlockState getPlacementState(ItemPlacementContext ctx) {
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'");
}
}