-
Notifications
You must be signed in to change notification settings - Fork 79
/
ModUtil.java
682 lines (570 loc) · 16.3 KB
/
ModUtil.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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
package mod.chiselsandbits.helpers;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.apache.commons.lang3.tuple.Pair;
import mod.chiselsandbits.bitbag.BagInventory;
import mod.chiselsandbits.chiseledblock.ItemBlockChiseled;
import mod.chiselsandbits.chiseledblock.NBTBlobConverter;
import mod.chiselsandbits.chiseledblock.TileEntityBlockChiseled;
import mod.chiselsandbits.chiseledblock.data.IntegerBox;
import mod.chiselsandbits.chiseledblock.data.VoxelBlob;
import mod.chiselsandbits.core.ChiselsAndBits;
import mod.chiselsandbits.helpers.StateLookup.CachedStateLookup;
import mod.chiselsandbits.integration.mcmultipart.MCMultipartProxy;
import mod.chiselsandbits.items.ItemBitBag;
import mod.chiselsandbits.items.ItemBitBag.BagPos;
import mod.chiselsandbits.items.ItemChiseledBit;
import mod.chiselsandbits.items.ItemNegativePrint;
import mod.chiselsandbits.items.ItemPositivePrint;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.init.Blocks;
import net.minecraft.init.SoundEvents;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumFacing.Axis;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.MathHelper;
import net.minecraft.util.math.Vec3d;
import net.minecraft.world.ChunkCache;
import net.minecraft.world.IBlockAccess;
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;
public class ModUtil
{
public static final String NBT_SIDE = "side";
public static final String NBT_BLOCKENTITYTAG = "BlockEntityTag";
private final static Random RAND = new Random();
private final static float DEG_TO_RAD = 0.017453292f;
static public EnumFacing getPlaceFace(
final EntityLivingBase placer )
{
return EnumFacing.getHorizontal( MathHelper.floor_double( placer.rotationYaw * 4.0F / 360.0F + 0.5D ) & 3 ).getOpposite();
}
static public Pair<Vec3d, Vec3d> getPlayerRay(
final EntityPlayer playerIn )
{
double reachDistance = 5.0d;
final double x = playerIn.prevPosX + ( playerIn.posX - playerIn.prevPosX );
final double y = playerIn.prevPosY + ( playerIn.posY - playerIn.prevPosY ) + playerIn.getEyeHeight();
final double z = playerIn.prevPosZ + ( playerIn.posZ - playerIn.prevPosZ );
final float playerPitch = playerIn.prevRotationPitch + ( playerIn.rotationPitch - playerIn.prevRotationPitch );
final float playerYaw = playerIn.prevRotationYaw + ( playerIn.rotationYaw - playerIn.prevRotationYaw );
final float yawRayX = MathHelper.sin( -playerYaw * DEG_TO_RAD - (float) Math.PI );
final float yawRayZ = MathHelper.cos( -playerYaw * DEG_TO_RAD - (float) Math.PI );
final float pitchMultiplier = -MathHelper.cos( -playerPitch * DEG_TO_RAD );
final float eyeRayY = MathHelper.sin( -playerPitch * DEG_TO_RAD );
final float eyeRayX = yawRayX * pitchMultiplier;
final float eyeRayZ = yawRayZ * pitchMultiplier;
if ( playerIn instanceof EntityPlayerMP )
{
reachDistance = ( (EntityPlayerMP) playerIn ).interactionManager.getBlockReachDistance();
}
final Vec3d from = new Vec3d( x, y, z );
final Vec3d to = from.addVector( eyeRayX * reachDistance, eyeRayY * reachDistance, eyeRayZ * reachDistance );
return Pair.of( from, to );
}
static public class ItemStackSlot
{
private final IInventory inv;
private final int slot;
private final ItemStack stack;
private final boolean isCreative;
private final boolean isEditable;
private final int toolSlot;
ItemStackSlot(
final IInventory i,
final int s,
final ItemStack st,
final ActingPlayer player,
final boolean canEdit )
{
inv = i;
slot = s;
stack = st;
toolSlot = player.getCurrentItem();
isCreative = player.isCreative();
isEditable = canEdit;
}
public boolean isValid()
{
return isEditable && ( isCreative || stack != null && stack.stackSize > 0 );
}
public void damage(
final ActingPlayer who )
{
if ( isCreative )
{
return;
}
who.damageItem( stack, 1 );
if ( stack.stackSize <= 0 )
{
who.playerDestroyItem( stack, who.getHand() );
inv.setInventorySlotContents( slot, null );
}
}
public void consume()
{
if ( isCreative )
{
return;
}
stack.stackSize--;
if ( stack.stackSize <= 0 )
{
inv.setInventorySlotContents( slot, null );
}
}
public ItemStack getStack()
{
return stack;
}
public void swapWithWeapon()
{
final ItemStack it = inv.getStackInSlot( toolSlot );
inv.setInventorySlotContents( toolSlot, inv.getStackInSlot( slot ) );
inv.setInventorySlotContents( slot, it );
}
};
static public ItemStackSlot findBit(
final ActingPlayer who,
final BlockPos pos,
final int StateID )
{
final ItemStack inHand = who.getCurrentEquippedItem();
final IInventory inv = who.getInventory();
final boolean canEdit = who.canPlayerManipulate( pos, EnumFacing.UP, inHand, true );
if ( inHand != null && inHand.stackSize > 0 && inHand.getItem() instanceof ItemChiseledBit && ItemChiseledBit.getStackState( inHand ) == StateID )
{
return new ItemStackSlot( inv, who.getCurrentItem(), inHand, who, canEdit );
}
for ( int x = 0; x < inv.getSizeInventory(); x++ )
{
final ItemStack is = inv.getStackInSlot( x );
if ( is != null && is.stackSize > 0 && is.getItem() instanceof ItemChiseledBit && ItemChiseledBit.sameBit( is, StateID ) )
{
return new ItemStackSlot( inv, x, is, who, canEdit );
}
}
return new ItemStackSlot( null, -1, null, who, canEdit );
}
public static boolean isHoldingPattern(
final EntityPlayer player )
{
final ItemStack inHand = player.getHeldItemMainhand();
if ( inHand != null && inHand.getItem() instanceof ItemPositivePrint )
{
return true;
}
if ( inHand != null && inHand.getItem() instanceof ItemNegativePrint )
{
return true;
}
return false;
}
public static boolean isHoldingChiseledBlock(
final EntityPlayer player )
{
final ItemStack inHand = player.getHeldItemMainhand();
if ( inHand != null && inHand.getItem() instanceof ItemBlockChiseled )
{
return true;
}
return false;
}
public static int getRotationIndex(
final EnumFacing face )
{
return face.getHorizontalIndex();
}
public static int getRotations(
final EntityLivingBase placer,
final EnumFacing oldYaw )
{
final EnumFacing newFace = ModUtil.getPlaceFace( placer );
int rotations = getRotationIndex( newFace ) - getRotationIndex( oldYaw );
// work out the rotation math...
while ( rotations < 0 )
{
rotations = 4 + rotations;
}
while ( rotations > 4 )
{
rotations = rotations - 4;
}
return 4 - rotations;
}
public static BlockPos getPartialOffset(
final EnumFacing side,
final BlockPos partial,
final IntegerBox modelBounds )
{
int offset_x = modelBounds.minX;
int offset_y = modelBounds.minY;
int offset_z = modelBounds.minZ;
final int partial_x = partial.getX();
final int partial_y = partial.getY();
final int partial_z = partial.getZ();
int middle_x = ( modelBounds.maxX - modelBounds.minX ) / -2;
int middle_y = ( modelBounds.maxY - modelBounds.minY ) / -2;
int middle_z = ( modelBounds.maxZ - modelBounds.minZ ) / -2;
switch ( side )
{
case DOWN:
offset_y = modelBounds.maxY;
middle_y = 0;
break;
case EAST:
offset_x = modelBounds.minX;
middle_x = 0;
break;
case NORTH:
offset_z = modelBounds.maxZ;
middle_z = 0;
break;
case SOUTH:
offset_z = modelBounds.minZ;
middle_z = 0;
break;
case UP:
offset_y = modelBounds.minY;
middle_y = 0;
break;
case WEST:
offset_x = modelBounds.maxX;
middle_x = 0;
break;
default:
throw new NullPointerException();
}
final int t_x = -offset_x + middle_x + partial_x;
final int t_y = -offset_y + middle_y + partial_y;
final int t_z = -offset_z + middle_z + partial_z;
return new BlockPos( t_x, t_y, t_z );
}
static public <T> T firstNonNull(
final T... options )
{
for ( final T i : options )
{
if ( i != null )
{
return i;
}
}
throw new NullPointerException( "Unable to find a non null item." );
}
public static TileEntity getTileEntitySafely(
final IBlockAccess world,
final BlockPos pos )
{
// not going to lie, this is really stupid.
if ( world instanceof ChunkCache )
{
return ( (ChunkCache) world ).func_190300_a( pos, Chunk.EnumCreateEntityType.CHECK );
}
// also stupid...
else if ( world instanceof World )
{
return ( (World) world ).getChunkFromBlockCoords( pos ).getTileEntity( pos, Chunk.EnumCreateEntityType.CHECK );
}
// yep... stupid.
else
{
return world.getTileEntity( pos );
}
}
public static TileEntityBlockChiseled getChiseledTileEntity(
final IBlockAccess world,
final BlockPos pos )
{
final TileEntity te = getTileEntitySafely( world, pos );
if ( te instanceof TileEntityBlockChiseled )
{
return (TileEntityBlockChiseled) te;
}
return MCMultipartProxy.proxyMCMultiPart.getPartFromBlockAccess( world, pos );
}
public static TileEntityBlockChiseled getChiseledTileEntity(
final World world,
final BlockPos pos,
final boolean create )
{
if ( world.isBlockLoaded( pos ) )
{
final TileEntity te = world.getTileEntity( pos );
if ( te instanceof TileEntityBlockChiseled )
{
return (TileEntityBlockChiseled) te;
}
return MCMultipartProxy.proxyMCMultiPart.getChiseledTileEntity( world, pos, create );
}
return null;
}
public static void removeChisledBlock(
final World world,
final BlockPos pos )
{
final TileEntity te = world.getTileEntity( pos );
if ( te instanceof TileEntityBlockChiseled )
{
world.setBlockToAir( pos ); // no physical matter left...
return;
}
MCMultipartProxy.proxyMCMultiPart.removeChisledBlock( te );
}
private final static Random itemRand = new Random();
public static void feedPlayer(
final World world,
final EntityPlayer player,
final EntityItem ei )
{
ItemStack is = ei.getEntityItem();
final List<BagPos> bags = ItemBitBag.getBags( player.inventory );
if ( !containsAtLeastOneOf( player.inventory, is ) )
{
final ItemStack minSize = is.copy();
if ( minSize.stackSize > minSize.getMaxStackSize() )
{
minSize.stackSize = minSize.getMaxStackSize();
}
is.stackSize -= minSize.stackSize;
player.inventory.addItemStackToInventory( minSize );
is.stackSize += minSize.stackSize;
}
for ( final BagPos bp : bags )
{
is = bp.inv.insertItem( is );
}
if ( is != null && !player.inventory.addItemStackToInventory( is ) )
{
ei.setEntityItemStack( is );
world.spawnEntityInWorld( ei );
}
else
{
if ( !ei.isSilent() )
{
ei.worldObj.playSound( (EntityPlayer) null, ei.posX, ei.posY, ei.posZ, SoundEvents.ENTITY_ITEM_PICKUP, SoundCategory.PLAYERS, 0.2F, ( ( itemRand.nextFloat() - itemRand.nextFloat() ) * 0.7F + 1.0F ) * 2.0F );
}
}
player.inventory.markDirty();
if ( player.inventoryContainer != null )
{
player.inventoryContainer.detectAndSendChanges();
}
}
public static boolean containsAtLeastOneOf(
final IInventory inv,
final ItemStack is )
{
boolean seen = false;
for ( int x = 0; x < inv.getSizeInventory(); x++ )
{
final ItemStack which = inv.getStackInSlot( x );
if ( which != null && which.getItem() == is.getItem() && ItemChiseledBit.sameBit( which, ItemChiseledBit.getStackState( is ) ) )
{
if ( !seen )
{
seen = true;
}
}
}
return seen;
}
public static List<BagInventory> getBags(
final ActingPlayer player )
{
if ( player.isCreative() )
{
return java.util.Collections.emptyList();
}
final List<BagInventory> bags = new ArrayList<BagInventory>();
final IInventory inv = player.getInventory();
for ( int zz = 0; zz < inv.getSizeInventory(); zz++ )
{
final ItemStack which = inv.getStackInSlot( zz );
if ( which != null && which.getItem() instanceof ItemBitBag )
{
bags.add( new BagInventory( which ) );
}
}
return bags;
}
public static int consumeBagBit(
final List<BagInventory> bags,
final int inPattern,
final int howMany )
{
int remaining = howMany;
for ( final BagInventory inv : bags )
{
remaining -= inv.extractBit( inPattern, remaining );
if ( remaining == 0 )
{
return howMany;
}
}
return howMany - remaining;
}
public static VoxelBlob getBlobFromStack(
final ItemStack stack,
final EntityLivingBase rotationPlayer )
{
if ( stack.hasTagCompound() )
{
final NBTBlobConverter tmp = new NBTBlobConverter();
NBTTagCompound cData = stack.getSubCompound( NBT_BLOCKENTITYTAG, false );
if ( cData == null )
{
cData = stack.getTagCompound();
}
tmp.readChisleData( cData, VoxelBlob.VERSION_ANY );
VoxelBlob blob = tmp.getBlob();
if ( rotationPlayer != null )
{
int xrotations = ModUtil.getRotations( rotationPlayer, ModUtil.getSide( stack ) );
while ( xrotations-- > 0 )
{
blob = blob.spin( Axis.Y );
}
}
return blob;
}
return new VoxelBlob();
}
public static void sendUpdate(
final World worldObj,
final BlockPos pos )
{
final IBlockState state = worldObj.getBlockState( pos );
worldObj.notifyBlockUpdate( pos, state, state, 0 );
}
public static ItemStack getItemFromBlock(
final IBlockState state )
{
final Block blk = state.getBlock();
final Item i = blk.getItemDropped( state, RAND, 0 );
final int meta = blk.getMetaFromState( state );
final int damage = blk.damageDropped( state );
final Item blockVarient = Item.getItemFromBlock( blk );
// darn conversions...
if ( blk == Blocks.GRASS )
{
return new ItemStack( Blocks.GRASS );
}
if ( i == null )
{
return null;
}
if ( blockVarient == null )
{
return null;
}
if ( blockVarient != i )
{
return null;
}
if ( blockVarient instanceof ItemBlock )
{
final ItemBlock ib = (ItemBlock) blockVarient;
if ( meta != ib.getMetadata( damage ) )
{
// this item dosn't drop itself... BAIL!
return null;
}
}
return new ItemStack( i, 1, damage );
}
public static boolean isNormalCube(
final IBlockState blockType )
{
return blockType.isNormalCube();
}
public static EnumFacing getSide(
final ItemStack stack )
{
if ( stack != null )
{
final NBTTagCompound blueprintTag = stack.getTagCompound();
int byteValue = EnumFacing.NORTH.ordinal();
if ( blueprintTag == null )
{
return EnumFacing.NORTH;
}
if ( blueprintTag.hasKey( NBT_SIDE ) )
{
byteValue = blueprintTag.getByte( NBT_SIDE );
}
if ( blueprintTag.hasKey( NBT_BLOCKENTITYTAG ) )
{
final NBTTagCompound c = blueprintTag.getCompoundTag( NBT_BLOCKENTITYTAG );
if ( c.hasKey( NBT_SIDE ) )
{
byteValue = c.getByte( NBT_SIDE );
}
}
EnumFacing side = EnumFacing.NORTH;
if ( byteValue >= 0 && byteValue < EnumFacing.values().length )
{
side = EnumFacing.values()[byteValue];
}
if ( side == EnumFacing.DOWN || side == EnumFacing.UP )
{
side = EnumFacing.NORTH;
}
return side;
}
return EnumFacing.NORTH;
}
public static void setSide(
final ItemStack stack,
final EnumFacing side )
{
if ( stack != null )
{
NBTTagCompound blueprintTag = stack.getTagCompound();
if ( blueprintTag == null )
{
blueprintTag = new NBTTagCompound();
}
if ( blueprintTag.hasKey( NBT_BLOCKENTITYTAG ) )
{
blueprintTag.getCompoundTag( NBT_BLOCKENTITYTAG ).setByte( NBT_SIDE, (byte) +side.ordinal() );
}
blueprintTag.setInteger( NBT_SIDE, +side.ordinal() );
stack.setTagCompound( blueprintTag );
}
}
private static StateLookup IDRelay = new StateLookup();
public static IBlockState getStateById(
final int blockStateID )
{
return IDRelay.getStateById( blockStateID );
}
public static int getStateId(
final IBlockState state )
{
return IDRelay.getStateId( state );
}
public static void cacheFastStates()
{
if ( !ChiselsAndBits.getConfig().lowMemoryMode )
{
// cache id -> state table as an array for faster rendering lookups.
IDRelay = new CachedStateLookup();
}
}
}