-
Notifications
You must be signed in to change notification settings - Fork 79
/
ItemChisel.java
536 lines (465 loc) · 14.1 KB
/
ItemChisel.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
package mod.chiselsandbits.items;
import java.util.HashSet;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import javax.annotation.Nonnull;
import org.apache.commons.lang3.tuple.Pair;
import com.google.common.base.Stopwatch;
import mod.chiselsandbits.chiseledblock.BlockBitInfo;
import mod.chiselsandbits.chiseledblock.BlockChiseled;
import mod.chiselsandbits.chiseledblock.data.BitLocation;
import mod.chiselsandbits.chiseledblock.data.VoxelBlob;
import mod.chiselsandbits.core.ChiselsAndBits;
import mod.chiselsandbits.core.ClientSide;
import mod.chiselsandbits.helpers.ActingPlayer;
import mod.chiselsandbits.helpers.BitOperation;
import mod.chiselsandbits.helpers.ChiselModeManager;
import mod.chiselsandbits.helpers.ChiselToolType;
import mod.chiselsandbits.helpers.IContinuousInventory;
import mod.chiselsandbits.helpers.IItemInInventory;
import mod.chiselsandbits.helpers.LocalStrings;
import mod.chiselsandbits.helpers.ModUtil;
import mod.chiselsandbits.integration.mcmultipart.MCMultipartProxy;
import mod.chiselsandbits.integration.mods.LittleTiles;
import mod.chiselsandbits.interfaces.IChiselModeItem;
import mod.chiselsandbits.interfaces.IItemScrollWheel;
import mod.chiselsandbits.modes.ChiselMode;
import mod.chiselsandbits.modes.IToolMode;
import mod.chiselsandbits.network.NetworkRouter;
import mod.chiselsandbits.network.packets.PacketChisel;
import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemTool;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ActionResult;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.math.Vec3d;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraft.world.World;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
public class ItemChisel extends ItemTool implements IItemScrollWheel, IChiselModeItem
{
final private static float one_16th = 1.0f / 16.0f;
public ItemChisel(
final ToolMaterial material )
{
super( 0.1F, -2.8F, material, new HashSet<Block>() );
long uses = 1;
switch ( material )
{
case DIAMOND:
uses = ChiselsAndBits.getConfig().diamondChiselUses;
break;
case GOLD:
uses = ChiselsAndBits.getConfig().goldChiselUses;
break;
case IRON:
uses = ChiselsAndBits.getConfig().ironChiselUses;
break;
case STONE:
uses = ChiselsAndBits.getConfig().stoneChiselUses;
break;
default:
break;
}
setMaxDamage( ChiselsAndBits.getConfig().damageTools ? (int) Math.max( 0, uses ) : 0 );
}
public ToolMaterial whatMaterial()
{
return toolMaterial;
}
@Override
@SideOnly( Side.CLIENT )
public void addInformation(
final ItemStack stack,
final World worldIn,
final List<String> tooltip,
final ITooltipFlag advanced )
{
super.addInformation( stack, worldIn, tooltip, advanced );
ChiselsAndBits.getConfig().helpText( LocalStrings.HelpChisel, tooltip,
ClientSide.instance.getKeyName( Minecraft.getMinecraft().gameSettings.keyBindAttack ),
ClientSide.instance.getModeKey() );
}
private static Stopwatch timer;
public static void resetDelay()
{
timer = null;
}
@Override
/**
* alter digging behavior to chisel, uses packets to enable server to stay
* in-sync.
*/
public boolean onBlockStartBreak(
final ItemStack itemstack,
final BlockPos pos,
final EntityPlayer player )
{
return ItemChisel.fromBreakToChisel( ChiselMode.castMode( ChiselModeManager.getChiselMode( player, ChiselToolType.CHISEL, EnumHand.MAIN_HAND ) ), itemstack, pos, player, EnumHand.MAIN_HAND );
}
static public boolean fromBreakToChisel(
final ChiselMode mode,
final ItemStack itemstack,
final @Nonnull BlockPos pos,
final EntityPlayer player,
final EnumHand hand )
{
final IBlockState state = player.getEntityWorld().getBlockState( pos );
if ( ItemChiseledBit.checkRequiredSpace( player, state ) ) {
return false;
}
if ( BlockBitInfo.canChisel( state ) || MCMultipartProxy.proxyMCMultiPart.isMultiPartTileEntity( player.getEntityWorld(), pos ) || LittleTiles.isLittleTilesBlock( player.getEntityWorld().getTileEntity( pos ) ) )
{
if ( itemstack != null && ( timer == null || timer.elapsed( TimeUnit.MILLISECONDS ) > 150 ) )
{
timer = Stopwatch.createStarted();
if ( mode == ChiselMode.DRAWN_REGION )
{
final Pair<Vec3d, Vec3d> PlayerRay = ModUtil.getPlayerRay( player );
final Vec3d ray_from = PlayerRay.getLeft();
final Vec3d ray_to = PlayerRay.getRight();
final RayTraceResult mop = player.worldObj.getBlockState( pos ).getBlock().collisionRayTrace( player.getEntityWorld().getBlockState( pos ), player.worldObj, pos, ray_from, ray_to );
if ( mop != null && mop.typeOfHit == RayTraceResult.Type.BLOCK )
{
final BitLocation loc = new BitLocation( mop, true, BitOperation.CHISEL );
ClientSide.instance.pointAt( ChiselToolType.CHISEL, loc, hand );
return true;
}
return true;
}
if ( !player.worldObj.isRemote )
{
return true;
}
final Pair<Vec3d, Vec3d> PlayerRay = ModUtil.getPlayerRay( player );
final Vec3d ray_from = PlayerRay.getLeft();
final Vec3d ray_to = PlayerRay.getRight();
final RayTraceResult mop = player.worldObj.getBlockState( pos ).getBlock().collisionRayTrace( player.worldObj.getBlockState( pos ), player.worldObj, pos, ray_from, ray_to );
if ( mop != null && mop.typeOfHit == RayTraceResult.Type.BLOCK )
{
useChisel( mode, player, player.worldObj, pos, mop.sideHit, (float) ( mop.hitVec.xCoord - pos.getX() ), (float) ( mop.hitVec.yCoord - pos.getY() ), (float) ( mop.hitVec.zCoord - pos.getZ() ), hand );
}
}
return true;
}
if ( player.getEntityWorld() != null && player.getEntityWorld().isRemote )
{
if ( ClientSide.instance.getStartPos() != null )
{
return true;
}
}
return false;
}
@Override
public String getHighlightTip(
final ItemStack item,
final String displayName )
{
if ( ChiselsAndBits.getConfig().itemNameModeDisplay )
{
if ( ChiselsAndBits.getConfig().perChiselMode || FMLCommonHandler.instance().getEffectiveSide() == Side.SERVER )
{
return displayName + " - " + ChiselMode.getMode( item ).string.getLocal();
}
else
{
return displayName + " - " + ChiselModeManager.getChiselMode( ClientSide.instance.getPlayer(), ChiselToolType.CHISEL, EnumHand.MAIN_HAND ).getName().getLocal();
}
}
return displayName;
}
@Override
public ActionResult<ItemStack> onItemRightClick(
final World worldIn,
final EntityPlayer playerIn,
final EnumHand hand )
{
final ItemStack itemStackIn = playerIn.getHeldItem( hand );
if ( worldIn.isRemote && ChiselsAndBits.getConfig().enableRightClickModeChange )
{
final IToolMode mode = ChiselModeManager.getChiselMode( playerIn, ChiselToolType.CHISEL, hand );
ChiselModeManager.scrollOption( ChiselToolType.CHISEL, mode, mode, playerIn.isSneaking() ? -1 : 1 );
return new ActionResult<ItemStack>( EnumActionResult.SUCCESS, itemStackIn );
}
return super.onItemRightClick( worldIn, playerIn, hand );
}
@Override
public EnumActionResult onItemUseFirst(
final EntityPlayer player,
final World world,
final BlockPos pos,
final EnumFacing side,
final float hitX,
final float hitY,
final float hitZ,
final EnumHand hand )
{
if ( world.isRemote && ChiselsAndBits.getConfig().enableRightClickModeChange )
{
onItemRightClick( world, player, hand );
return EnumActionResult.SUCCESS;
}
return EnumActionResult.FAIL;
}
/**
* uses a chisel, this is called from onBlockStartBreak converts block, and
* handles everything short of modifying the voxel data.
*
* @param stack
* @param player
* @param world
* @param pos
* @param side
* @param hitX
* @param hitY
* @param hitZ
*/
static void useChisel(
final ChiselMode mode,
final EntityPlayer player,
final World world,
final @Nonnull BlockPos pos,
final EnumFacing side,
final float hitX,
final float hitY,
final float hitZ,
final EnumHand hand )
{
final BitLocation location = new BitLocation( new RayTraceResult( RayTraceResult.Type.BLOCK, new Vec3d( hitX, hitY, hitZ ), side, pos ), false, BitOperation.CHISEL );
final PacketChisel pc = new PacketChisel( BitOperation.CHISEL, location, side, mode, hand );
final int extractedState = pc.doAction( player );
if ( extractedState != 0 )
{
ClientSide.breakSound( world, pos, extractedState );
NetworkRouter.instance.sendToServer( pc );
}
}
/**
* Modifies VoxelData of TileEntityChiseled
*
* @param selected
*
* @param player
* @param vb
* @param world
* @param pos
* @param side
* @param x
* @param y
* @param z
* @param output
* @return
*/
static public ItemStack chiselBlock(
final IContinuousInventory selected,
final ActingPlayer player,
final VoxelBlob vb,
final World world,
final BlockPos pos,
final EnumFacing side,
final int x,
final int y,
final int z,
ItemStack output,
final List<EntityItem> spawnlist )
{
final boolean isCreative = player.isCreative();
final int blk = vb.get( x, y, z );
if ( blk == 0 )
{
return output;
}
if ( !canMine( selected, ModUtil.getStateById( blk ), player.getPlayer(), world, pos ) )
{
return output;
}
if ( !selected.useItem( blk ) )
{
return output;
}
final boolean spawnBit = ChiselsAndBits.getItems().itemBlockBit != null;
if ( !world.isRemote && !isCreative )
{
double hitX = x * one_16th;
double hitY = y * one_16th;
double hitZ = z * one_16th;
final double offset = 0.5;
hitX += side.getFrontOffsetX() * offset;
hitY += side.getFrontOffsetY() * offset;
hitZ += side.getFrontOffsetZ() * offset;
if ( output == null || !ItemChiseledBit.sameBit( output, blk ) || ModUtil.getStackSize( output ) == 64 )
{
output = ItemChiseledBit.createStack( blk, 1, true );
if ( spawnBit )
{
spawnlist.add( new EntityItem( world, pos.getX() + hitX, pos.getY() + hitY, pos.getZ() + hitZ, output ) );
}
}
else
{
ModUtil.adjustStackSize( output, 1 );
}
}
else
{
// return value...
output = ItemChiseledBit.createStack( blk, 1, true );
}
vb.clear( x, y, z );
return output;
}
private static boolean testingChisel = false;
public static boolean canMine(
final IContinuousInventory chiselInv,
final IBlockState state,
final EntityPlayer player,
final World world,
final @Nonnull BlockPos pos )
{
final int targetState = ModUtil.getStateId( state );
IItemInInventory chiselSlot = chiselInv.getItem( targetState );
ItemStack chisel = chiselSlot.getStack();
if ( player.capabilities.isCreativeMode )
{
return world.isBlockModifiable( player, pos );
}
if ( ModUtil.isEmpty( chisel ) )
{
return false;
}
if ( ChiselsAndBits.getConfig().enableChiselToolHarvestCheck )
{
// this is the earily check.
if ( state.getBlock() instanceof BlockChiseled )
{
return true;
}
do
{
final Block blk = world.getBlockState( pos ).getBlock();
BlockChiseled.setActingAs( state );
testingChisel = true;
chiselSlot.swapWithWeapon();
final boolean canHarvest = blk.canHarvestBlock( world, pos, player );
chiselSlot.swapWithWeapon();
testingChisel = false;
BlockChiseled.setActingAs( null );
if ( canHarvest )
{
return true;
}
chiselInv.fail( targetState );
chiselSlot = chiselInv.getItem( targetState );
chisel = chiselSlot.getStack();
}
while ( !ModUtil.isEmpty( chisel ) );
return false;
}
return true;
}
private static final String DAMAGE_KEY = "damage";
@Override
public int getDamage(
final ItemStack stack )
{
return Math.max( getMetadata( stack ), getNBT( stack ).getInteger( DAMAGE_KEY ) );
}
@Override
public boolean isDamaged(
final ItemStack stack )
{
return getDamage( stack ) > 0;
}
@Override
public void setDamage(
final ItemStack stack,
int damage )
{
if ( damage < 0 )
{
damage = 0;
}
getNBT( stack ).setInteger( DAMAGE_KEY, damage );
}
private NBTTagCompound getNBT(
final ItemStack stack )
{
if ( !stack.hasTagCompound() )
{
stack.setTagCompound( new NBTTagCompound() );
}
return stack.getTagCompound();
}
@Override
public boolean canHarvestBlock(
final IBlockState blk )
{
Item it;
switch ( getToolMaterialName() )
{
case "DIAMOND":
it = Items.DIAMOND_PICKAXE;
break;
case "GOLD":
it = Items.GOLDEN_PICKAXE;
break;
case "IRON":
it = Items.IRON_PICKAXE;
break;
default:
case "STONE":
it = Items.STONE_PICKAXE;
break;
case "WOOD":
it = Items.WOODEN_PICKAXE;
break;
}
return blk.getBlock() instanceof BlockChiseled || it.canHarvestBlock( blk );
}
@Override
public int getHarvestLevel(
final ItemStack stack,
final String toolClass,
final EntityPlayer player,
final IBlockState blockState )
{
if ( testingChisel && stack.getItem() instanceof ItemChisel )
{
final String pattern = "(^|,)" + Pattern.quote( toolClass ) + "(,|$)";
final Pattern p = Pattern.compile( pattern );
final Matcher m = p.matcher( ChiselsAndBits.getConfig().enableChiselToolHarvestCheckTools );
if ( m.find() )
{
final ItemChisel ic = (ItemChisel) stack.getItem();
return ic.toolMaterial.getHarvestLevel();
}
}
return super.getHarvestLevel( stack, toolClass, player, blockState );
}
@Override
public void scroll(
final EntityPlayer player,
final ItemStack stack,
final int dwheel )
{
final IToolMode mode = ChiselModeManager.getChiselMode( player, ChiselToolType.CHISEL, EnumHand.MAIN_HAND );
ChiselModeManager.scrollOption( ChiselToolType.CHISEL, mode, mode, dwheel );
}
}