-
Notifications
You must be signed in to change notification settings - Fork 79
/
BlockBitInfo.java
363 lines (306 loc) · 10.6 KB
/
BlockBitInfo.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
package mod.chiselsandbits.chiseledblock;
import java.util.HashMap;
import java.util.Random;
import gnu.trove.map.TIntObjectMap;
import gnu.trove.map.hash.TIntObjectHashMap;
import mod.chiselsandbits.api.IgnoreBlockLogic;
import mod.chiselsandbits.chiseledblock.data.VoxelType;
import mod.chiselsandbits.core.ChiselsAndBits;
import mod.chiselsandbits.core.Log;
import mod.chiselsandbits.helpers.ModUtil;
import mod.chiselsandbits.render.helpers.ModelUtil;
import net.minecraft.block.Block;
import net.minecraft.block.BlockGlass;
import net.minecraft.block.BlockGlowstone;
import net.minecraft.block.BlockGrass;
import net.minecraft.block.BlockIce;
import net.minecraft.block.BlockMycelium;
import net.minecraft.block.BlockSlime;
import net.minecraft.block.BlockSnowBlock;
import net.minecraft.block.BlockStainedGlass;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.Explosion;
import net.minecraft.world.World;
import net.minecraftforge.fluids.Fluid;
public class BlockBitInfo
{
// imc api...
private static HashMap<Block, Boolean> ignoreLogicBlocks = new HashMap<Block, Boolean>();
static
{
ignoreLogicBlocks.put( Blocks.LEAVES, true );
ignoreLogicBlocks.put( Blocks.LEAVES2, true );
ignoreLogicBlocks.put( Blocks.SNOW, true );
}
// cache data..
private static HashMap<IBlockState, BlockBitInfo> stateBitInfo = new HashMap<IBlockState, BlockBitInfo>();
private static HashMap<Block, Boolean> supportedBlocks = new HashMap<Block, Boolean>();
private static HashMap<IBlockState, Boolean> forcedStates = new HashMap<IBlockState, Boolean>();
private static HashMap<Block, Fluid> fluidBlocks = new HashMap<Block, Fluid>();
private static TIntObjectMap<Fluid> fluidStates = new TIntObjectHashMap<Fluid>();
private static HashMap<IBlockState, Integer> bitColor = new HashMap<IBlockState, Integer>();
public static int getColorFor(
final IBlockState state,
final int tint )
{
Integer out = bitColor.get( state );
if ( out == null )
{
final Block blk = state.getBlock();
final Fluid fluid = BlockBitInfo.getFluidFromBlock( blk );
if ( fluid != null )
{
out = fluid.getColor();
}
else
{
final ItemStack target = ModUtil.getItemFromBlock( state );
if ( ModUtil.isEmpty( target ) )
{
out = 0xffffff;
}
else
{
out = ModelUtil.getItemStackColor( target, tint );
}
}
bitColor.put( state, out );
}
return out;
}
public static void addFluidBlock(
final Block blk,
final Fluid fluid )
{
if ( blk == null )
{
return;
}
fluidBlocks.put( blk, fluid );
for ( final IBlockState state : blk.getBlockState().getValidStates() )
{
try
{
fluidStates.put( ModUtil.getStateId( state ), fluid );
}
catch ( final Throwable t )
{
Log.logError( "Error while determining fluid state.", t );
}
}
stateBitInfo.clear();
supportedBlocks.clear();
}
static public Fluid getFluidFromBlock(
final Block blk )
{
return fluidBlocks.get( blk );
}
public static VoxelType getTypeFromStateID(
final int bit )
{
if ( bit == 0 )
{
return VoxelType.AIR;
}
return fluidStates.containsKey( bit ) ? VoxelType.FLUID : VoxelType.SOLID;
}
public static void ignoreBlockLogic(
final Block which )
{
ignoreLogicBlocks.put( which, true );
reset();
}
public static void forceStateCompatibility(
final IBlockState which,
final boolean forceStatus )
{
forcedStates.put( which, forceStatus );
reset();
}
public static void reset()
{
stateBitInfo.clear();
supportedBlocks.clear();
}
public static BlockBitInfo getBlockInfo(
final IBlockState state )
{
BlockBitInfo bit = stateBitInfo.get( state );
if ( bit == null )
{
bit = BlockBitInfo.createFromState( state );
stateBitInfo.put( state, bit );
}
return bit;
}
@SuppressWarnings( "deprecation" )
public static boolean supportsBlock(
final IBlockState state )
{
if ( forcedStates.containsKey( state ) )
{
return forcedStates.get( state );
}
final Block blk = state.getBlock();
if ( supportedBlocks.containsKey( blk ) )
{
return supportedBlocks.get( blk );
}
try
{
// require basic hardness behavior...
final ReflectionHelperBlock pb = new ReflectionHelperBlock();
final Class<? extends Block> blkClass = blk.getClass();
// custom dropping behavior?
pb.quantityDropped( null );
final Class<?> wc = getDeclaringClass( blkClass, pb.MethodName, Random.class );
final boolean quantityDroppedTest = wc == Block.class || wc == BlockGlowstone.class || wc == BlockStainedGlass.class || wc == BlockGlass.class || wc == BlockSnowBlock.class;
pb.quantityDroppedWithBonus( 0, null );
final boolean quantityDroppedWithBonusTest = getDeclaringClass( blkClass, pb.MethodName, int.class, Random.class ) == Block.class || wc == BlockGlowstone.class;
pb.quantityDropped( null, 0, null );
final boolean quantityDropped2Test = getDeclaringClass( blkClass, pb.MethodName, IBlockState.class, int.class, Random.class ) == Block.class;
final boolean isNotSlab = Item.getItemFromBlock( blk ) != null;
boolean itemExistsOrNotSpecialDrops = quantityDroppedTest && quantityDroppedWithBonusTest && quantityDropped2Test || isNotSlab;
// ignore blocks with custom collision.
pb.onEntityCollidedWithBlock( null, null, null, null );
boolean noCustomCollision = getDeclaringClass( blkClass, pb.MethodName, World.class, BlockPos.class, IBlockState.class, Entity.class ) == Block.class || blkClass == BlockSlime.class;
// full cube specifically is tied to lighting... so for glass
// Compatibility use isFullBlock which can be true for glass.
boolean isFullBlock = blk.isFullBlock( state ) || blkClass == BlockStainedGlass.class || blkClass == BlockGlass.class || blk == Blocks.SLIME_BLOCK || blk == Blocks.ICE;
final BlockBitInfo info = BlockBitInfo.createFromState( state );
final boolean tickingBehavior = blk.getTickRandomly() && ChiselsAndBits.getConfig().blacklistTickingBlocks;
boolean hasBehavior = ( blk.hasTileEntity( state ) || tickingBehavior ) && blkClass != BlockGrass.class && blkClass != BlockMycelium.class && blkClass != BlockIce.class;
final boolean hasItem = Item.getItemFromBlock( blk ) != null;
final boolean supportedMaterial = ChiselsAndBits.getBlocks().getConversion( state ) != null;
final Boolean IgnoredLogic = ignoreLogicBlocks.get( blk );
if ( blkClass.isAnnotationPresent( IgnoreBlockLogic.class ) || IgnoredLogic != null && IgnoredLogic )
{
isFullBlock = true;
noCustomCollision = true;
hasBehavior = false;
itemExistsOrNotSpecialDrops = true;
}
if ( info.isCompatiable && noCustomCollision && info.hardness >= -0.01f && isFullBlock && supportedMaterial && !hasBehavior && itemExistsOrNotSpecialDrops )
{
final boolean result = hasItem && ChiselsAndBits.getConfig().isEnabled( blkClass.getName() );
supportedBlocks.put( blk, result );
if ( result )
{
stateBitInfo.put( state, info );
}
return result;
}
if ( fluidBlocks.containsKey( blk ) )
{
stateBitInfo.put( state, info );
supportedBlocks.put( blk, true );
return true;
}
supportedBlocks.put( blk, false );
return false;
}
catch ( final Throwable t )
{
// if the above test fails for any reason, then the block cannot be
// supported.
supportedBlocks.put( blk, false );
return false;
}
}
private static Class<?> getDeclaringClass(
final Class<?> blkClass,
final String methodName,
final Class<?>... args )
{
try
{
blkClass.getDeclaredMethod( methodName, args );
return blkClass;
}
catch ( final NoSuchMethodException e )
{
// nothing here...
}
catch ( final SecurityException e )
{
// nothing here..
}
catch ( final NoClassDefFoundError e )
{
Log.info( "Unable to determine blocks eligibility for chiseling, " + blkClass.getName() + " attempted to load " + e.getMessage() );
return blkClass;
}
catch ( final Throwable t )
{
return blkClass;
}
return getDeclaringClass(
blkClass.getSuperclass(),
methodName,
args );
}
public final boolean isCompatiable;
public final float hardness;
public final float explosionResistance;
private BlockBitInfo(
final boolean isCompatiable,
final float hardness,
final float explosionResistance )
{
this.isCompatiable = isCompatiable;
this.hardness = hardness;
this.explosionResistance = explosionResistance;
}
public static BlockBitInfo createFromState(
final IBlockState state )
{
try
{
// require basic hardness behavior...
final ReflectionHelperBlock reflectBlock = new ReflectionHelperBlock();
final Block blk = state.getBlock();
final Class<? extends Block> blkClass = blk.getClass();
reflectBlock.getBlockHardness( null, null, null );
final Class<?> hardnessMethodClass = getDeclaringClass( blkClass, reflectBlock.MethodName, IBlockState.class, World.class, BlockPos.class );
final boolean test_a = hardnessMethodClass == Block.class;
reflectBlock.getPlayerRelativeBlockHardness( null, null, null, null );
final boolean test_b = getDeclaringClass( blkClass, reflectBlock.MethodName, IBlockState.class, EntityPlayer.class, World.class, BlockPos.class ) == Block.class;
reflectBlock.getExplosionResistance( null );
final Class<?> exploResistanceClz = getDeclaringClass( blkClass, reflectBlock.MethodName, Entity.class );
final boolean test_c = exploResistanceClz == Block.class;
reflectBlock.getExplosionResistance( null, null, null, null );
final boolean test_d = getDeclaringClass( blkClass, reflectBlock.MethodName, World.class, BlockPos.class, Entity.class, Explosion.class ) == Block.class;
final boolean isFluid = fluidStates.containsKey( ModUtil.getStateId( state ) );
// is it perfect?
if ( test_a && test_b && test_c && test_d && !isFluid )
{
final float blockHardness = blk.blockHardness;
final float resistance = blk.blockResistance;
return new BlockBitInfo( true, blockHardness, resistance );
}
else
{
// less accurate, we can just pretend they are some fixed
// hardness... say like stone?
final Block stone = Blocks.STONE;
return new BlockBitInfo( ChiselsAndBits.getConfig().compatabilityMode, stone.blockHardness, stone.blockResistance );
}
}
catch ( final Exception err )
{
return new BlockBitInfo( false, -1, -1 );
}
}
public static boolean canChisel(
final IBlockState state )
{
return state.getBlock() instanceof BlockChiseled || supportsBlock( state );
}
}