-
Notifications
You must be signed in to change notification settings - Fork 497
/
BlockBuildCraftBase.java
343 lines (289 loc) · 14.9 KB
/
BlockBuildCraftBase.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
package buildcraft.core.lib.block;
import buildcraft.api.core.EnumColor;
import buildcraft.api.enums.*;
import buildcraft.api.properties.BuildCraftExtendedProperty;
import buildcraft.api.properties.BuildCraftProperties;
import buildcraft.api.properties.BuildCraftProperty;
import buildcraft.core.BCCreativeTab;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.block.properties.IProperty;
import net.minecraft.block.state.BlockState;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.item.ItemStack;
import net.minecraft.util.*;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraftforge.common.property.ExtendedBlockState;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public abstract class BlockBuildCraftBase extends Block {
public static final BuildCraftProperty<EnumFacing> FACING_PROP = BuildCraftProperties.BLOCK_FACING;
public static final BuildCraftProperty<EnumFacing> FACING_6_PROP = BuildCraftProperties.BLOCK_FACING_6;
public static final BuildCraftProperty<EnumEngineType> ENGINE_TYPE = BuildCraftProperties.ENGINE_TYPE;
public static final BuildCraftProperty<EnumColor> COLOR_PROP = BuildCraftProperties.BLOCK_COLOR;
public static final BuildCraftProperty<EnumSpring> SPRING_TYPE = BuildCraftProperties.SPRING_TYPE;
public static final BuildCraftProperty<EnumEnergyStage> ENERGY_STAGE = BuildCraftProperties.ENERGY_STAGE;
public static final BuildCraftProperty<EnumFillerPattern> FILLER_PATTERN = BuildCraftProperties.FILLER_PATTERN;
public static final BuildCraftProperty<EnumBlueprintType> BLUEPRINT_TYPE = BuildCraftProperties.BLUEPRINT_TYPE;
public static final BuildCraftProperty<EnumLaserTableType> LASER_TABLE_TYPE = BuildCraftProperties.LASER_TABLE_TYPE;
public static final BuildCraftProperty<EnumDecoratedBlock> DECORATED_TYPE = BuildCraftProperties.DECORATED_BLOCK;
public static final BuildCraftProperty<Integer> GENERIC_PIPE_DATA = BuildCraftProperties.GENERIC_PIPE_DATA;
public static final BuildCraftProperty<Boolean> JOINED_BELOW = BuildCraftProperties.JOINED_BELOW;
public static final BuildCraftProperty<Boolean> MOVING = BuildCraftProperties.MOVING;
public static final BuildCraftProperty<Integer> LED_POWER = BuildCraftProperties.LED_POWER;
public static final BuildCraftProperty<Boolean> LED_DONE = BuildCraftProperties.LED_DONE;
public static final BuildCraftProperty<Boolean> CONNECTED_UP = BuildCraftProperties.CONNECTED_UP;
public static final BuildCraftProperty<Boolean> CONNECTED_DOWN = BuildCraftProperties.CONNECTED_DOWN;
public static final BuildCraftProperty<Boolean> CONNECTED_EAST = BuildCraftProperties.CONNECTED_EAST;
public static final BuildCraftProperty<Boolean> CONNECTED_WEST = BuildCraftProperties.CONNECTED_WEST;
public static final BuildCraftProperty<Boolean> CONNECTED_NORTH = BuildCraftProperties.CONNECTED_NORTH;
public static final BuildCraftProperty<Boolean> CONNECTED_SOUTH = BuildCraftProperties.CONNECTED_SOUTH;
public static final Map<EnumFacing, BuildCraftProperty<Boolean>> CONNECTED_MAP = BuildCraftProperties.CONNECTED_MAP;
@SuppressWarnings("unchecked")
public static final BuildCraftProperty<Boolean>[] CONNECTED_ARRAY = CONNECTED_MAP.values().toArray(new BuildCraftProperty[6]);
protected BuildCraftProperty<?>[] properties;
protected BuildCraftProperty<?>[] nonMetaProperties;
protected BuildCraftExtendedProperty<?>[] extendedProperties;
protected boolean hasExtendedProperties;
protected List<BuildCraftProperty<?>> propertyList;
protected final Map<Integer, IBlockState> intToState = Maps.newHashMap();
protected final Map<IBlockState, Integer> stateToInt = Maps.newHashMap();
protected final BlockState myBlockState;
/** True if this block can rotate in any of the horizontal directions */
public boolean horizontallyRotatable;
/** True if this block can rotate in any of the six facing directions */
public boolean allRotatable;
protected BlockBuildCraftBase(Material material) {
this(material, BCCreativeTab.get("main"), false, new BuildCraftProperty<?>[0]);
}
protected BlockBuildCraftBase(Material material, BCCreativeTab creativeTab) {
this(material, creativeTab, false, new BuildCraftProperty<?>[0]);
}
protected BlockBuildCraftBase(Material material, BuildCraftProperty<?>... properties) {
this(material, BCCreativeTab.get("main"), false, properties);
}
protected BlockBuildCraftBase(Material material, BCCreativeTab bcCreativeTab, BuildCraftProperty<?>... properties) {
this(material, bcCreativeTab, false, properties);
}
protected BlockBuildCraftBase(Material material, BCCreativeTab bcCreativeTab, boolean hasExtendedProps, BuildCraftProperty<?>... properties) {
super(material);
setCreativeTab(bcCreativeTab);
setHardness(5F);
List<BuildCraftProperty<?>> metas = Lists.newArrayList();
List<BuildCraftProperty<?>> nonMetas = Lists.newArrayList();
List<BuildCraftExtendedProperty<?>> infinites = Lists.newArrayList();
this.hasExtendedProperties = fillStateListsPre(hasExtendedProps, metas, nonMetas, infinites, properties);
this.properties = metas.toArray(new BuildCraftProperty<?>[0]);
this.nonMetaProperties = nonMetas.toArray(new BuildCraftProperty<?>[0]);
this.extendedProperties = infinites.toArray(new BuildCraftExtendedProperty<?>[0]);
this.myBlockState = createBlockState();
fillStateMapPost(metas, nonMetas, properties);
}
@SuppressWarnings("static-method")
protected boolean fillStateListsPre(boolean hasExtendedProps, List<BuildCraftProperty<?>> metas, List<BuildCraftProperty<?>> nonMetas,
List<BuildCraftExtendedProperty<?>> infinites, BuildCraftProperty<?>... properties) {
int total = 1;
for (BuildCraftProperty<?> prop : properties) {
if (prop == null) {
/* Used by some blocks (e.g. the filler) if they do or do not want to have a specific property at
* runtime (per block). Is used in the format "wantProperty ? someProp : null" */
continue;
}
if (prop instanceof BuildCraftExtendedProperty<?>) {
infinites.add((BuildCraftExtendedProperty<?>) prop);
hasExtendedProps = true;
continue;
}
total *= prop.getAllowedValues().size();
if (total > 16) {
nonMetas.add(prop);
} else {
metas.add(prop);
}
}
return hasExtendedProps;
}
protected void fillStateMapPost(List<BuildCraftProperty<?>> metas, List<BuildCraftProperty<?>> nonMetas, BuildCraftProperty<?>... properties) {
IBlockState defaultState = getBlockState().getBaseState();
Map<IBlockState, Integer> tempValidStates = Maps.newHashMap();
tempValidStates.put(defaultState, 0);
boolean canRotate = false;
boolean canSixRotate = false;
for (BuildCraftProperty<?> prop : properties) {
if (prop == null) {
continue;
}
if (prop instanceof BuildCraftExtendedProperty<?>) {
continue;
}
if (prop == FACING_PROP) {
canRotate = true;
}
if (prop == FACING_6_PROP) {
canRotate = true;
canSixRotate = true;
}
List<? extends Comparable> allowedValues = prop.getAllowedValues();
defaultState = withProperty(defaultState, prop, allowedValues.iterator().next());
Map<IBlockState, Integer> newValidStates = Maps.newHashMap();
int mul = metas.contains(prop) ? allowedValues.size() : 1;
for (Entry<IBlockState, Integer> entry : tempValidStates.entrySet()) {
int index = 0;
Collections.sort(allowedValues);
for (Comparable<?> comp : allowedValues) {
int pos = entry.getValue() * mul + index;
newValidStates.put(withProperty(entry.getKey(), prop, comp), pos);
if (mul > 1) {
index++;
}
}
}
tempValidStates = newValidStates;
}
horizontallyRotatable = canRotate;
allRotatable = canSixRotate;
for (Entry<IBlockState, Integer> entry : tempValidStates.entrySet()) {
int i = entry.getValue();
stateToInt.put(entry.getKey(), i);
if (!intToState.containsKey(i)) {
intToState.put(i, entry.getKey());
}
}
setDefaultState(defaultState);
List<BuildCraftProperty<?>> allProperties = Lists.newArrayList();
allProperties.addAll(metas);
allProperties.addAll(nonMetas);
propertyList = Collections.unmodifiableList(allProperties);
}
// Generic helper methods, these stop generics from being strange
@SuppressWarnings("unchecked")
private IBlockState withProperty(IBlockState state, BuildCraftProperty prop, Comparable value) {
return withProperty0(state, prop, value);
}
private <V extends Comparable<V>, T extends V> IBlockState withProperty0(IBlockState state, BuildCraftProperty<V> prop, T value) {
return state.withProperty(prop, value);
}
@Override
public BlockState getBlockState() {
return this.myBlockState;
}
@Override
protected BlockState createBlockState() {
if (properties == null) {
// Will be overridden later
return new BlockState(this, new IProperty[] {});
}
IProperty[] props = new IProperty[properties.length + nonMetaProperties.length];
System.arraycopy(properties, 0, props, 0, properties.length);
System.arraycopy(nonMetaProperties, 0, props, properties.length, nonMetaProperties.length);
if (hasExtendedProperties) {
return new ExtendedBlockState(this, props, extendedProperties);
}
return new BlockState(this, props);
}
@Override
public int getMetaFromState(IBlockState state) {
return stateToInt.containsKey(state) ? stateToInt.get(state) : 0;
}
@Override
public IBlockState getStateFromMeta(int meta) {
return intToState.containsKey(meta) ? intToState.get(meta) : getDefaultState();
}
@Override
public IBlockState onBlockPlaced(World worldIn, BlockPos pos, EnumFacing facing, float hitX, float hitY, float hitZ, int meta,
EntityLivingBase placer) {
if (allRotatable) {// TODO (CHECK): Do we want to do this for all blocks that have 6 facing directions
return getStateFromMeta(meta).withProperty(FACING_6_PROP, facing);
} else {
return getStateFromMeta(meta);
}
}
@Override
public void onBlockPlacedBy(World world, BlockPos pos, IBlockState state, EntityLivingBase entity, ItemStack stack) {
// If it was allRotatable the vertical direction would have been taken into account above
if (horizontallyRotatable && !allRotatable) {
EnumFacing orientation = entity.getHorizontalFacing();
world.setBlockState(pos, state.withProperty(FACING_PROP, orientation.getOpposite()));
}
}
/** Override this to easily allow the collision boxes and selected bounding boxes to be calculated */
public AxisAlignedBB getBox(IBlockAccess world, BlockPos pos, IBlockState state) {
return new AxisAlignedBB(0, 0, 0, 1, 1, 1);
}
/** You must override one of {@link #getBoxes(IBlockAccess, BlockPos, IBlockState)} or
* {@link #getBox(IBlockAccess, BlockPos, IBlockState)} otherwise you will get a crash. */
public AxisAlignedBB[] getBoxes(IBlockAccess world, BlockPos pos, IBlockState state) {
return new AxisAlignedBB[] { getBox(world, pos, state) };
}
/** Exposed so subclasses can call Block's collision ray trace method without needing to resort to idk,
* reflection? */
public MovingObjectPosition collisionRayTrace_super(World world, BlockPos pos, Vec3 origin, Vec3 direction) {
return super.collisionRayTrace(world, pos, origin, direction);
}
@Override
public MovingObjectPosition collisionRayTrace(World world, BlockPos pos, Vec3 origin, Vec3 direction) {
IBlockState state = world.getBlockState(pos);
AxisAlignedBB[] aabbs = getBoxes(world, pos, state);
MovingObjectPosition closest = null;
for (AxisAlignedBB aabb : aabbs) {
aabb = aabb.offset(pos.getX(), pos.getY(), pos.getZ()).expand(-0.01, -0.01, -0.01);
MovingObjectPosition mop = aabb.calculateIntercept(origin, direction);
if (mop != null) {
if (closest != null && mop.hitVec.distanceTo(origin) < closest.hitVec.distanceTo(origin)) {
closest = mop;
} else {
closest = mop;
}
}
}
if (closest == null) {
return null;
} else {
return new MovingObjectPosition(closest.hitVec, closest.sideHit, pos);
}
}
@Override
@SuppressWarnings({ "rawtypes", "unchecked" })
public void addCollisionBoxesToList(World world, BlockPos pos, IBlockState state, AxisAlignedBB mask, List list, Entity par7Entity) {
if (!isCollidable()) {
return;
} else {
for (AxisAlignedBB bb : getBoxes(world, pos, state)) {
bb = bb.offset(pos.getX(), pos.getY(), pos.getZ());
if (mask.intersectsWith(bb)) {
list.add(bb);
}
}
}
}
@Override
public boolean canCollideCheck(IBlockState state, boolean hitIfLiquid) {
return true;
}
@Override
public AxisAlignedBB getCollisionBoundingBox(World world, BlockPos pos, IBlockState state) {
if (isCollidable()) {
return getBox(world, pos, state).offset(pos.getX(), pos.getY(), pos.getZ());
} else {
return null;
}
}
@Override
@SideOnly(Side.CLIENT)
public AxisAlignedBB getSelectedBoundingBox(World world, BlockPos pos) {
return getBox(world, pos, world.getBlockState(pos)).offset(pos.getX(), pos.getY(), pos.getZ());
}
/* @Override public void setBlockBoundsBasedOnState(IBlockAccess world, BlockPos pos) { AxisAlignedBB[] bbs =
* getBoxes(world, pos, world.getBlockState(pos)); AxisAlignedBB bb = bbs[0]; for (int i = 1; i < bbs.length; i++) {
* bb = bb.union(bbs[i]); } minX = bb.minX; minY = bb.minY; minZ = bb.minZ; maxX = bb.maxX; maxY = bb.maxY; maxZ =
* bb.maxZ; } */
}