/
BlockPipe.java
292 lines (254 loc) · 11.6 KB
/
BlockPipe.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
package knightminer.inspirations.utility.block;
import knightminer.inspirations.Inspirations;
import knightminer.inspirations.common.Config;
import knightminer.inspirations.utility.InspirationsUtility;
import knightminer.inspirations.utility.tileentity.TilePipe;
import net.minecraft.block.Block;
import net.minecraft.block.BlockDirectional;
import net.minecraft.block.BlockDropper;
import net.minecraft.block.BlockHopper;
import net.minecraft.block.SoundType;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.PropertyBool;
import net.minecraft.block.properties.PropertyDirection;
import net.minecraft.block.state.BlockStateContainer;
import net.minecraft.block.state.IBlockState;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.Mirror;
import net.minecraft.util.Rotation;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import slimeknights.mantle.block.BlockInventory;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
public class BlockPipe extends BlockInventory {
public static final PropertyDirection FACING = BlockDirectional.FACING;
public static final PropertyBool NORTH = PropertyBool.create("north");
public static final PropertyBool EAST = PropertyBool.create("east");
public static final PropertyBool SOUTH = PropertyBool.create("south");
public static final PropertyBool WEST = PropertyBool.create("west");
public static final PropertyBool UP = PropertyBool.create("up");
public static final PropertyBool DOWN = PropertyBool.create("down");
public static final PropertyBool HOPPER = PropertyBool.create("hopper");
public BlockPipe() {
super(Material.IRON);
this.setHardness(3.0F);
this.setResistance(8.0F);
this.setSoundType(SoundType.METAL);
this.setDefaultState(this.blockState.getBaseState()
.withProperty(FACING, EnumFacing.NORTH)
.withProperty(NORTH, false)
.withProperty(EAST, false)
.withProperty(SOUTH, false)
.withProperty(WEST, false)
.withProperty(UP, false)
.withProperty(DOWN, false)
.withProperty(HOPPER, false));
this.setCreativeTab(CreativeTabs.REDSTONE);
}
/* Block state settings */
@Override
protected BlockStateContainer createBlockState() {
return new BlockStateContainer(this, FACING, NORTH, EAST, SOUTH, WEST, UP, DOWN, HOPPER);
}
@Override
public IBlockState getStateFromMeta(int meta) {
return this.getDefaultState().withProperty(FACING, EnumFacing.getFront(meta & 7));
}
@Override
public int getMetaFromState(IBlockState state) {
return state.getValue(FACING).getIndex();
}
@Override
public IBlockState withRotation(IBlockState state, Rotation rot) {
return state.withProperty(FACING, rot.rotate(state.getValue(FACING)));
}
@Override
public IBlockState withMirror(IBlockState state, Mirror mirror) {
return state.withRotation(mirror.toRotation(state.getValue(FACING)));
}
@Override
public int damageDropped(IBlockState state) {
return 0;
}
@Override
public IBlockState getStateForPlacement(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta, EntityLivingBase placer) {
facing = facing.getOpposite();
// only allow up if allowed in the config
if (!Config.pipeUpwards && facing == EnumFacing.UP) {
facing = EnumFacing.DOWN;
}
return this.getDefaultState().withProperty(FACING, facing);
}
@Override
public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand, EnumFacing side, float clickX, float clickY, float clickZ) {
// return false if holding a pipe to make easier to place
Item item = player.getHeldItem(hand).getItem();
if(item == Item.getItemFromBlock(InspirationsUtility.pipe) || Block.getBlockFromItem(item) instanceof BlockHopper) {
return false;
}
return super.onBlockActivated(world, pos, state, player, hand, side, clickX, clickY, clickZ);
}
/* Model and shape */
@Override
public IBlockState getActualState(IBlockState state, IBlockAccess world, BlockPos pos) {
EnumFacing facing = state.getValue(FACING);
IBlockState offsetState = world.getBlockState(pos.offset(facing));
return state.withProperty(HOPPER, offsetState.getBlock() instanceof BlockHopper && offsetState.getValue(BlockHopper.FACING) != facing.getOpposite())
.withProperty(UP, canConnectTo(world, pos, facing, EnumFacing.UP))
.withProperty(DOWN, canConnectTo(world, pos, facing, EnumFacing.DOWN))
.withProperty(NORTH, canConnectTo(world, pos, facing, EnumFacing.NORTH))
.withProperty(EAST, canConnectTo(world, pos, facing, EnumFacing.EAST))
.withProperty(SOUTH, canConnectTo(world, pos, facing, EnumFacing.SOUTH))
.withProperty(WEST, canConnectTo(world, pos, facing, EnumFacing.WEST));
}
private static boolean canConnectTo(IBlockAccess world, BlockPos pos, EnumFacing facing, EnumFacing side) {
// ignore side pipe is facing
if(facing == side) return false;
IBlockState state = world.getBlockState(pos.offset(side));
Block block = state.getBlock();
EnumFacing opposite = side.getOpposite();
// if it is a known item output thingy and is facing us, connect
if((block instanceof BlockPipe || block instanceof BlockDropper) && state.getValue(FACING) == opposite) return true;
// hopper check, we can skip on down since hoppers cannot face up
return side != EnumFacing.DOWN && block instanceof BlockHopper && state.getValue(BlockHopper.FACING) == opposite;
}
/* Tile Entity */
@Override
public TileEntity createNewTileEntity(World worldIn, int meta) {
return new TilePipe();
}
@Override
protected boolean openGui(EntityPlayer player, World world, BlockPos pos) {
player.openGui(Inspirations.instance, 0, world, pos.getX(), pos.getY(), pos.getZ());
return true;
}
@Override
public boolean isFullCube(IBlockState state) {
return false;
}
@Override
public boolean isOpaqueCube(IBlockState state) {
return false;
}
/* Bounds */
// binary map of bounds, order of bits is EWSNUD
private static final AxisAlignedBB FULL_BOUNDS[] = new AxisAlignedBB[64];
static {
for(int i = 0; i < 64; i++) {
FULL_BOUNDS[i] = new AxisAlignedBB((i & 0b010000) > 0 ? 0 : 0.34375, (i & 0b000001) > 0 ? 0 : 0.21875, (i & 0b000100) > 0 ? 0 : 0.34375,
(i & 0b100000) > 0 ? 1 : 0.65625, (i & 0b000010) > 0 ? 1 : 0.53125, (i & 0b001000) > 0 ? 1 : 0.65625);
}
}
@Nonnull
@Override
public AxisAlignedBB getBoundingBox(IBlockState state, IBlockAccess source, BlockPos pos) {
// yeah, magic bit math stuffs
// order of bits is EWSNUD, we start by or'ing in the index bit shifted
state = state.getActualState(source, pos);
int index = 1 << state.getValue(FACING).getIndex()
| (state.getValue(EAST) ? 32 : 0)
| (state.getValue(WEST) ? 16 : 0)
| (state.getValue(SOUTH) ? 8 : 0)
| (state.getValue(NORTH) ? 4 : 0)
| (state.getValue(UP) ? 2 : 0)
| (state.getValue(DOWN) ? 1 : 0);
return FULL_BOUNDS[index];
}
// base bounds
private static final AxisAlignedBB BOUNDS_CENTER = new AxisAlignedBB(0.375, 0.25, 0.375, 0.625, 0.5, 0.625),
// main bounds for side pipes
BOUNDS_DOWN = new AxisAlignedBB(0.375, 0, 0.375, 0.625, 0.25, 0.625),
BOUNDS_UP = new AxisAlignedBB(0.375, 0.5, 0.375, 0.625, 1, 0.625),
BOUNDS_NORTH = new AxisAlignedBB(0.375, 0.25, 0, 0.625, 0.5, 0.375),
BOUNDS_SOUTH = new AxisAlignedBB(0.375, 0.25, 0.625, 0.625, 0.5, 1 ),
BOUNDS_WEST = new AxisAlignedBB(0, 0.25, 0.375, 0.375, 0.5, 0.625),
BOUNDS_EAST = new AxisAlignedBB(0.625, 0.25, 0.375, 1, 0.5, 0.625),
// extra bounds for the raytrace to select the little connections
BOUNDS_DOWN_CONNECT = new AxisAlignedBB(0.34375, 0, 0.34375, 0.65625, 0.0625, 0.65625),
BOUNDS_UP_CONNECT = new AxisAlignedBB(0.34375, 0.9375, 0.34375, 0.59375, 1, 0.65625),
BOUNDS_NORTH_CONNECT = new AxisAlignedBB(0.34375, 0.21875, 0, 0.65625, 0.53125, 0.0625 ),
BOUNDS_SOUTH_CONNECT = new AxisAlignedBB(0.34375, 0.21875, 0.9375, 0.65625, 0.53125, 1 ),
BOUNDS_WEST_CONNECT = new AxisAlignedBB(0, 0.21875, 0.34375, 0.0625, 0.53125, 0.65625),
BOUNDS_EAST_CONNECT = new AxisAlignedBB(0.9375, 0.21875, 0.34375, 1, 0.53125, 0.65625);
// above side bounds in an array to index easier - DUNSWE
private static final AxisAlignedBB BOUNDS_SIDES[] = {
BOUNDS_DOWN, BOUNDS_UP, BOUNDS_NORTH, BOUNDS_SOUTH, BOUNDS_WEST, BOUNDS_EAST
};
@Override
public void addCollisionBoxToList(IBlockState state, World world, BlockPos pos, AxisAlignedBB entityBox, List<AxisAlignedBB> collidingBoxes, @Nullable Entity entity, boolean p_185477_7_) {
state = state.getActualState(world, pos);
// center
addCollisionBoxToList(pos, entityBox, collidingBoxes, BOUNDS_CENTER);
addCollisionBoxToList(pos, entityBox, collidingBoxes, BOUNDS_SIDES[state.getValue(FACING).getIndex()]);
// add each side
if(state.getValue(UP)) addCollisionBoxToList(pos, entityBox, collidingBoxes, BOUNDS_UP);
if(state.getValue(DOWN)) addCollisionBoxToList(pos, entityBox, collidingBoxes, BOUNDS_DOWN);
if(state.getValue(NORTH)) addCollisionBoxToList(pos, entityBox, collidingBoxes, BOUNDS_NORTH);
if(state.getValue(SOUTH)) addCollisionBoxToList(pos, entityBox, collidingBoxes, BOUNDS_SOUTH);
if(state.getValue(WEST)) addCollisionBoxToList(pos, entityBox, collidingBoxes, BOUNDS_WEST);
if(state.getValue(EAST)) addCollisionBoxToList(pos, entityBox, collidingBoxes, BOUNDS_EAST);
}
@Deprecated
@Override
public RayTraceResult collisionRayTrace(IBlockState state, @Nonnull World world, @Nonnull BlockPos pos, @Nonnull Vec3d start, @Nonnull Vec3d end) {
state = state.getActualState(world, pos);
// basically the same BlockStairs does
// Raytrace through all included AABBs (sides) and return the nearest
// in the case of channels, we need to ensure each piece is actually enabled first though
List<RayTraceResult> list = new ArrayList<>(8);
list.add(rayTrace(pos, start, end, BOUNDS_CENTER));
list.add(rayTrace(pos, start, end, BOUNDS_SIDES[state.getValue(FACING).getIndex()]));
// add each enabled side
if(state.getValue(UP)){
list.add(rayTrace(pos, start, end, BOUNDS_UP));
list.add(rayTrace(pos, start, end, BOUNDS_UP_CONNECT));
}
if(state.getValue(DOWN)) {
list.add(rayTrace(pos, start, end, BOUNDS_DOWN));
list.add(rayTrace(pos, start, end, BOUNDS_DOWN_CONNECT));
}
if(state.getValue(NORTH)) {
list.add(rayTrace(pos, start, end, BOUNDS_NORTH));
list.add(rayTrace(pos, start, end, BOUNDS_NORTH_CONNECT));
}
if(state.getValue(SOUTH)) {
list.add(rayTrace(pos, start, end, BOUNDS_SOUTH));
list.add(rayTrace(pos, start, end, BOUNDS_SOUTH_CONNECT));
}
if(state.getValue(WEST)) {
list.add(rayTrace(pos, start, end, BOUNDS_WEST));
list.add(rayTrace(pos, start, end, BOUNDS_WEST_CONNECT));
}
if(state.getValue(EAST)) {
list.add(rayTrace(pos, start, end, BOUNDS_EAST));
list.add(rayTrace(pos, start, end, BOUNDS_EAST_CONNECT));
}
// compare results
RayTraceResult result = null;
double max = 0.0D;
for(RayTraceResult raytraceresult : list) {
if(raytraceresult != null) {
double distance = raytraceresult.hitVec.squareDistanceTo(end);
if(distance > max) {
result = raytraceresult;
max = distance;
}
}
}
return result;
}
}