-
Notifications
You must be signed in to change notification settings - Fork 753
/
ModifiableItem.java
552 lines (462 loc) · 18.8 KB
/
ModifiableItem.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
package slimeknights.tconstruct.library.tools.item;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Multimap;
import lombok.Getter;
import net.minecraft.core.BlockPos;
import net.minecraft.core.NonNullList;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.util.Mth;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.EquipmentSlot.Type;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.ai.attributes.Attribute;
import net.minecraft.world.entity.ai.attributes.AttributeModifier;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Rarity;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.item.UseAnim;
import net.minecraft.world.item.context.UseOnContext;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.world.level.ClipContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraftforge.common.ToolAction;
import net.minecraftforge.common.capabilities.ICapabilityProvider;
import slimeknights.mantle.client.SafeClientAccess;
import slimeknights.tconstruct.library.modifiers.ModifierEntry;
import slimeknights.tconstruct.library.modifiers.TinkerHooks;
import slimeknights.tconstruct.library.modifiers.hook.behavior.AttributesModifierHook;
import slimeknights.tconstruct.library.modifiers.hook.behavior.EnchantmentModifierHook;
import slimeknights.tconstruct.library.modifiers.hook.display.DurabilityDisplayModifierHook;
import slimeknights.tconstruct.library.modifiers.hook.interaction.GeneralInteractionModifierHook;
import slimeknights.tconstruct.library.modifiers.hook.interaction.InteractionSource;
import slimeknights.tconstruct.library.modifiers.hook.interaction.InventoryTickModifierHook;
import slimeknights.tconstruct.library.tools.IndestructibleItemEntity;
import slimeknights.tconstruct.library.tools.capability.ToolCapabilityProvider;
import slimeknights.tconstruct.library.tools.capability.ToolInventoryCapability;
import slimeknights.tconstruct.library.tools.definition.ToolDefinition;
import slimeknights.tconstruct.library.tools.definition.module.mining.IsEffectiveToolHook;
import slimeknights.tconstruct.library.tools.definition.module.mining.MiningSpeedToolHook;
import slimeknights.tconstruct.library.tools.helper.ModifierUtil;
import slimeknights.tconstruct.library.tools.helper.ToolAttackUtil;
import slimeknights.tconstruct.library.tools.helper.ToolBuildHandler;
import slimeknights.tconstruct.library.tools.helper.ToolDamageUtil;
import slimeknights.tconstruct.library.tools.helper.ToolHarvestLogic;
import slimeknights.tconstruct.library.tools.helper.TooltipUtil;
import slimeknights.tconstruct.library.tools.nbt.IModDataView;
import slimeknights.tconstruct.library.tools.nbt.IToolStackView;
import slimeknights.tconstruct.library.tools.nbt.ToolStack;
import slimeknights.tconstruct.library.tools.stat.ToolStats;
import slimeknights.tconstruct.library.utils.Util;
import slimeknights.tconstruct.tools.TinkerToolActions;
import javax.annotation.Nullable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
/**
* A standard modifiable item which implements melee hooks
* This class handles how all the modifier hooks and display data for items made out of different materials
*/
public class ModifiableItem extends Item implements IModifiableDisplay {
/** Tool definition for the given tool */
@Getter
private final ToolDefinition toolDefinition;
/** Cached tool for rendering on UIs */
private ItemStack toolForRendering;
public ModifiableItem(Properties properties, ToolDefinition toolDefinition) {
super(properties);
this.toolDefinition = toolDefinition;
}
/* Basic properties */
@Override
public int getMaxStackSize(ItemStack stack) {
return 1;
}
@Override
public boolean isNotReplaceableByPickAction(ItemStack stack, Player player, int inventorySlot) {
return true;
}
/* Enchanting */
@Override
public boolean isEnchantable(ItemStack stack) {
return false;
}
@Override
public boolean isBookEnchantable(ItemStack stack, ItemStack book) {
return false;
}
@Override
public boolean canApplyAtEnchantingTable(ItemStack stack, Enchantment enchantment) {
return enchantment.isCurse() && super.canApplyAtEnchantingTable(stack, enchantment);
}
@Override
public int getEnchantmentLevel(ItemStack stack, Enchantment enchantment) {
return EnchantmentModifierHook.getEnchantmentLevel(stack, enchantment);
}
@Override
public Map<Enchantment,Integer> getAllEnchantments(ItemStack stack) {
return EnchantmentModifierHook.getAllEnchantments(stack);
}
/* Loading */
@Nullable
@Override
public ICapabilityProvider initCapabilities(ItemStack stack, @Nullable CompoundTag nbt) {
return new ToolCapabilityProvider(stack);
}
@Override
public void verifyTagAfterLoad(CompoundTag nbt) {
ToolStack.verifyTag(this, nbt, getToolDefinition());
}
@Override
public void onCraftedBy(ItemStack stack, Level worldIn, Player playerIn) {
ToolStack.ensureInitialized(stack, getToolDefinition());
}
/* Display */
@Override
public boolean isFoil(ItemStack stack) {
// we use enchantments to handle some modifiers, so don't glow from them
// however, if a modifier wants to glow let them
return ModifierUtil.checkVolatileFlag(stack, SHINY);
}
@Override
public Rarity getRarity(ItemStack stack) {
int rarity = ModifierUtil.getVolatileInt(stack, RARITY);
return Rarity.values()[Mth.clamp(rarity, 0, 3)];
}
/* Indestructible items */
@Override
public boolean hasCustomEntity(ItemStack stack) {
return IndestructibleItemEntity.hasCustomEntity(stack);
}
@Override
public Entity createEntity(Level world, Entity original, ItemStack stack) {
return IndestructibleItemEntity.createFrom(world, original, stack);
}
/* Damage/Durability */
@Override
public boolean isRepairable(ItemStack stack) {
// handle in the tinker station
return false;
}
@Override
public boolean canBeDepleted() {
return true;
}
@Override
public int getMaxDamage(ItemStack stack) {
if (!canBeDepleted()) {
return 0;
}
ToolStack tool = ToolStack.from(stack);
int durability = tool.getStats().getInt(ToolStats.DURABILITY);
// vanilla deletes tools if max damage == getDamage, so tell vanilla our max is one higher when broken
return tool.isBroken() ? durability + 1 : durability;
}
@Override
public int getDamage(ItemStack stack) {
if (!canBeDepleted()) {
return 0;
}
return ToolStack.from(stack).getDamage();
}
@Override
public void setDamage(ItemStack stack, int damage) {
if (canBeDepleted()) {
ToolStack.from(stack).setDamage(damage);
}
}
@Override
public <T extends LivingEntity> int damageItem(ItemStack stack, int amount, T damager, Consumer<T> onBroken) {
ToolDamageUtil.handleDamageItem(stack, amount, damager, onBroken);
return 0;
}
/* Durability display */
@Override
public boolean isBarVisible(ItemStack pStack) {
return DurabilityDisplayModifierHook.showDurabilityBar(pStack);
}
@Override
public int getBarColor(ItemStack pStack) {
return DurabilityDisplayModifierHook.getDurabilityRGB(pStack);
}
@Override
public int getBarWidth(ItemStack pStack) {
return DurabilityDisplayModifierHook.getDurabilityWidth(pStack);
}
/* Attacking */
@Override
public boolean onLeftClickEntity(ItemStack stack, Player player, Entity entity) {
return ToolAttackUtil.attackEntity(stack, player, entity);
}
@Override
public Multimap<Attribute,AttributeModifier> getAttributeModifiers(IToolStackView tool, EquipmentSlot slot) {
return AttributesModifierHook.getHeldAttributeModifiers(tool, slot);
}
@Override
public Multimap<Attribute, AttributeModifier> getAttributeModifiers(EquipmentSlot slot, ItemStack stack) {
CompoundTag nbt = stack.getTag();
if (nbt == null || slot.getType() != Type.HAND) {
return ImmutableMultimap.of();
}
return getAttributeModifiers(ToolStack.from(stack), slot);
}
@Override
public boolean canDisableShield(ItemStack stack, ItemStack shield, LivingEntity entity, LivingEntity attacker) {
return canPerformAction(stack, TinkerToolActions.SHIELD_DISABLE);
}
/* Harvest logic */
@Override
public boolean isCorrectToolForDrops(ItemStack stack, BlockState state) {
return IsEffectiveToolHook.isEffective(ToolStack.from(stack), state);
}
@Override
public boolean mineBlock(ItemStack stack, Level worldIn, BlockState state, BlockPos pos, LivingEntity entityLiving) {
return ToolHarvestLogic.mineBlock(stack, worldIn, state, pos, entityLiving);
}
@Override
public float getDestroySpeed(ItemStack stack, BlockState state) {
return MiningSpeedToolHook.getDestroySpeed(stack, state);
}
@Override
public boolean onBlockStartBreak(ItemStack stack, BlockPos pos, Player player) {
return ToolHarvestLogic.handleBlockBreak(stack, pos, player);
}
/* Modifier interactions */
@Override
public void inventoryTick(ItemStack stack, Level worldIn, Entity entityIn, int itemSlot, boolean isSelected) {
InventoryTickModifierHook.heldInventoryTick(stack, worldIn, entityIn, itemSlot, isSelected);
}
/* Right click hooks */
/** If true, this interaction hook should defer to the offhand */
protected static boolean shouldInteract(@Nullable LivingEntity player, ToolStack toolStack, InteractionHand hand) {
IModDataView volatileData = toolStack.getVolatileData();
if (volatileData.getBoolean(NO_INTERACTION)) {
return false;
}
// off hand always can interact
if (hand == InteractionHand.OFF_HAND) {
return true;
}
// main hand may wish to defer to the offhand if it has a tool
return player == null || !volatileData.getBoolean(DEFER_OFFHAND) || player.getOffhandItem().isEmpty();
}
@Override
public InteractionResult onItemUseFirst(ItemStack stack, UseOnContext context) {
ToolStack tool = ToolStack.from(stack);
InteractionHand hand = context.getHand();
if (shouldInteract(context.getPlayer(), tool, hand)) {
for (ModifierEntry entry : tool.getModifierList()) {
InteractionResult result = entry.getHook(TinkerHooks.BLOCK_INTERACT).beforeBlockUse(tool, entry, context, InteractionSource.RIGHT_CLICK);
if (result.consumesAction()) {
return result;
}
}
}
return InteractionResult.PASS;
}
@Override
public InteractionResult useOn(UseOnContext context) {
ToolStack tool = ToolStack.from(context.getItemInHand());
InteractionHand hand = context.getHand();
if (shouldInteract(context.getPlayer(), tool, hand)) {
for (ModifierEntry entry : tool.getModifierList()) {
InteractionResult result = entry.getHook(TinkerHooks.BLOCK_INTERACT).afterBlockUse(tool, entry, context, InteractionSource.RIGHT_CLICK);
if (result.consumesAction()) {
return result;
}
}
}
return InteractionResult.PASS;
}
@Override
public InteractionResult interactLivingEntity(ItemStack stack, Player playerIn, LivingEntity target, InteractionHand hand) {
ToolStack tool = ToolStack.from(stack);
if (shouldInteract(playerIn, tool, hand)) {
for (ModifierEntry entry : tool.getModifierList()) {
InteractionResult result = entry.getHook(TinkerHooks.ENTITY_INTERACT).afterEntityUse(tool, entry, playerIn, target, hand, InteractionSource.RIGHT_CLICK);
if (result.consumesAction()) {
return result;
}
}
}
return InteractionResult.PASS;
}
@Override
public InteractionResultHolder<ItemStack> use(Level worldIn, Player playerIn, InteractionHand hand) {
ItemStack stack = playerIn.getItemInHand(hand);
ToolStack tool = ToolStack.from(stack);
if (shouldInteract(playerIn, tool, hand)) {
for (ModifierEntry entry : tool.getModifierList()) {
InteractionResult result = entry.getHook(TinkerHooks.GENERAL_INTERACT).onToolUse(tool, entry, playerIn, hand, InteractionSource.RIGHT_CLICK);
if (result.consumesAction()) {
return new InteractionResultHolder<>(result, stack);
}
}
}
InteractionResult result = ToolInventoryCapability.tryOpenContainer(stack, tool, playerIn, Util.getSlotType(hand));
return new InteractionResultHolder<>(result, stack);
}
@Override
public void onUseTick(Level pLevel, LivingEntity entityLiving, ItemStack stack, int timeLeft) {
ToolStack tool = ToolStack.from(stack);
ModifierEntry activeModifier = GeneralInteractionModifierHook.getActiveModifier(tool);
if (activeModifier != ModifierEntry.EMPTY) {
activeModifier.getHook(TinkerHooks.GENERAL_INTERACT).onUsingTick(tool, activeModifier, entityLiving, timeLeft);
}
}
@Override
public boolean canContinueUsing(ItemStack oldStack, ItemStack newStack) {
if (super.canContinueUsing(oldStack, newStack)) {
if (oldStack != newStack) {
GeneralInteractionModifierHook.finishUsing(ToolStack.from(oldStack));
}
}
return super.canContinueUsing(oldStack, newStack);
}
@Override
public ItemStack finishUsingItem(ItemStack stack, Level worldIn, LivingEntity entityLiving) {
ToolStack tool = ToolStack.from(stack);
ModifierEntry activeModifier = GeneralInteractionModifierHook.getActiveModifier(tool);
if (activeModifier != ModifierEntry.EMPTY) {
activeModifier.getHook(TinkerHooks.GENERAL_INTERACT).onFinishUsing(tool, activeModifier, entityLiving);
}
GeneralInteractionModifierHook.finishUsing(tool);
return stack;
}
@Override
public void releaseUsing(ItemStack stack, Level worldIn, LivingEntity entityLiving, int timeLeft) {
ToolStack tool = ToolStack.from(stack);
ModifierEntry activeModifier = GeneralInteractionModifierHook.getActiveModifier(tool);
if (activeModifier != ModifierEntry.EMPTY) {
activeModifier.getHook(TinkerHooks.GENERAL_INTERACT).onStoppedUsing(tool, activeModifier, entityLiving, timeLeft);
}
GeneralInteractionModifierHook.finishUsing(tool);
}
@Override
public int getUseDuration(ItemStack stack) {
ToolStack tool = ToolStack.from(stack);
ModifierEntry activeModifier = GeneralInteractionModifierHook.getActiveModifier(tool);
if (activeModifier != ModifierEntry.EMPTY) {
return activeModifier.getHook(TinkerHooks.GENERAL_INTERACT).getUseDuration(tool, activeModifier);
}
return 0;
}
@Override
public UseAnim getUseAnimation(ItemStack stack) {
ToolStack tool = ToolStack.from(stack);
ModifierEntry activeModifier = GeneralInteractionModifierHook.getActiveModifier(tool);
if (activeModifier != ModifierEntry.EMPTY) {
return activeModifier.getHook(TinkerHooks.GENERAL_INTERACT).getUseAction(tool, activeModifier);
}
return UseAnim.NONE;
}
@Override
public boolean canPerformAction(ItemStack stack, ToolAction toolAction) {
return ModifierUtil.canPerformAction(ToolStack.from(stack), toolAction);
}
/* Tooltips */
@Override
public Component getName(ItemStack stack) {
return TooltipUtil.getDisplayName(stack, getToolDefinition());
}
@Override
public void appendHoverText(ItemStack stack, @Nullable Level level, List<Component> tooltip, TooltipFlag flag) {
TooltipUtil.addInformation(this, stack, level, tooltip, SafeClientAccess.getTooltipKey(), flag);
}
@Override
public int getDefaultTooltipHideFlags(ItemStack stack) {
return TooltipUtil.getModifierHideFlags(getToolDefinition());
}
/* Display items */
@Override
public void fillItemCategory(CreativeModeTab group, NonNullList<ItemStack> items) {
if (this.allowedIn(group)) {
ToolBuildHandler.addDefaultSubItems(this, items);
}
}
@Override
public ItemStack getRenderTool() {
if (toolForRendering == null) {
toolForRendering = ToolBuildHandler.buildToolForRendering(this, this.getToolDefinition());
}
return toolForRendering;
}
/* Misc */
/**
* Logic to prevent reanimation on tools when properties such as autorepair change.
* @param oldStack Old stack instance
* @param newStack New stack instance
* @param slotChanged If true, a slot changed
* @return True if a reequip animation should be triggered
*/
public static boolean shouldCauseReequip(ItemStack oldStack, ItemStack newStack, boolean slotChanged) {
if (oldStack == newStack) {
return false;
}
// basic changes
if (slotChanged || oldStack.getItem() != newStack.getItem()) {
return true;
}
// if the tool props changed,
ToolStack oldTool = ToolStack.from(oldStack);
ToolStack newTool = ToolStack.from(newStack);
// check if modifiers or materials changed
if (!oldTool.getMaterials().equals(newTool.getMaterials())) {
return true;
}
if (!oldTool.getModifierList().equals(newTool.getModifierList())) {
return true;
}
// if the attributes changed, reequip
Multimap<Attribute,AttributeModifier> attributesNew = newStack.getAttributeModifiers(EquipmentSlot.MAINHAND);
Multimap<Attribute, AttributeModifier> attributesOld = oldStack.getAttributeModifiers(EquipmentSlot.MAINHAND);
if (attributesNew.size() != attributesOld.size()) {
return true;
}
for (Attribute attribute : attributesOld.keySet()) {
if (!attributesNew.containsKey(attribute)) {
return true;
}
Iterator<AttributeModifier> iter1 = attributesNew.get(attribute).iterator();
Iterator<AttributeModifier> iter2 = attributesOld.get(attribute).iterator();
while (iter1.hasNext() && iter2.hasNext()) {
if (!iter1.next().equals(iter2.next())) {
return true;
}
}
}
// no changes, no reequip
return false;
}
@Override
public boolean shouldCauseBlockBreakReset(ItemStack oldStack, ItemStack newStack) {
return shouldCauseReequipAnimation(oldStack, newStack, false);
}
@Override
public boolean shouldCauseReequipAnimation(ItemStack oldStack, ItemStack newStack, boolean slotChanged) {
return shouldCauseReequip(oldStack, newStack, slotChanged);
}
/* Helpers */
/**
* Creates a raytrace and casts it to a BlockRayTraceResult
*
* @param worldIn the world
* @param player the given player
* @param fluidMode the fluid mode to use for the raytrace event
*
* @return Raytrace
*/
public static BlockHitResult blockRayTrace(Level worldIn, Player player, ClipContext.Fluid fluidMode) {
return Item.getPlayerPOVHitResult(worldIn, player, fluidMode);
}
}