blob: c5b2438aafa937fdd1b97c0a02b48ac809730124 (
plain)
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
|
package de.hysky.skyblocker.mixin;
import com.llamalad7.mixinextras.injector.ModifyReturnValue;
import de.hysky.skyblocker.config.SkyblockerConfigManager;
import de.hysky.skyblocker.utils.ItemUtils;
import de.hysky.skyblocker.utils.Utils;
import dev.cbyrne.betterinject.annotations.Inject;
import it.unimi.dsi.fastutil.ints.IntIntPair;
import net.minecraft.item.ItemStack;
import net.minecraft.text.Text;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
@Mixin(ItemStack.class)
public abstract class ItemStackMixin {
@Shadow
public abstract int getDamage();
@Shadow
public abstract void setDamage(int damage);
@Unique
private int maxDamage;
@ModifyReturnValue(method = "getName", at = @At("RETURN"))
private Text skyblocker$customItemNames(Text original) {
if (Utils.isOnSkyblock()) {
return SkyblockerConfigManager.get().general.customItemNames.getOrDefault(ItemUtils.getItemUuid((ItemStack) (Object) this), original);
}
return original;
}
/**
* Updates the durability of this item stack every tick when in the inventory.
*/
@Inject(method = "inventoryTick", at = @At("TAIL"))
private void skyblocker$updateDamage() {
if (!skyblocker$shouldProcess()) {
return;
}
skyblocker$getAndCacheDurability();
}
@ModifyReturnValue(method = "getDamage", at = @At("RETURN"))
private int skyblocker$handleDamage(int original) {
// If the durability is already calculated, the original value should be the damage
if (!skyblocker$shouldProcess() || maxDamage != 0) {
return original;
}
return skyblocker$getAndCacheDurability() ? getDamage() : original;
}
@ModifyReturnValue(method = "getMaxDamage", at = @At("RETURN"))
private int skyblocker$handleMaxDamage(int original) {
if (!skyblocker$shouldProcess()) {
return original;
}
// If the max damage is already calculated, return it
if (maxDamage != 0) {
return maxDamage;
}
return skyblocker$getAndCacheDurability() ? maxDamage : original;
}
@ModifyReturnValue(method = "isDamageable", at = @At("RETURN"))
private boolean skyblocker$handleDamageable(boolean original) {
return skyblocker$shouldProcess() || original;
}
@Unique
private boolean skyblocker$shouldProcess() {
return Utils.isOnSkyblock() && SkyblockerConfigManager.get().locations.dwarvenMines.enableDrillFuel && ItemUtils.hasCustomDurability((ItemStack) (Object) this);
}
@Unique
private boolean skyblocker$getAndCacheDurability() {
// Calculate the durability
IntIntPair durability = ItemUtils.getDurability((ItemStack) (Object) this);
// Return if calculating the durability failed
if (durability == null) {
return false;
}
// Saves the calculated durability
maxDamage = durability.rightInt();
setDamage(durability.rightInt() - durability.leftInt());
return true;
}
}
|