aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/de/hysky/skyblocker/mixins/ItemStackMixin.java
blob: 1493cf26757ff226bdad7ec351c3814b80aa630c (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
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
package de.hysky.skyblocker.mixins;

import com.google.gson.JsonParser;
import com.llamalad7.mixinextras.injector.ModifyReturnValue;
import com.mojang.serialization.JsonOps;

import de.hysky.skyblocker.config.SkyblockerConfigManager;
import de.hysky.skyblocker.injected.SkyblockerStack;
import de.hysky.skyblocker.skyblock.PetCache.PetInfo;
import de.hysky.skyblocker.skyblock.item.tooltip.ItemTooltip;
import de.hysky.skyblocker.skyblock.profileviewer.ProfileViewerScreen;
import de.hysky.skyblocker.utils.ItemUtils;
import de.hysky.skyblocker.utils.Utils;
import it.unimi.dsi.fastutil.ints.IntIntPair;
import net.minecraft.client.MinecraftClient;
import net.minecraft.component.ComponentHolder;
import net.minecraft.component.type.ItemEnchantmentsComponent;
import net.minecraft.item.ItemStack;
import net.minecraft.item.tooltip.TooltipAppender;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.text.Text;
import org.jetbrains.annotations.Nullable;
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;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyVariable;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import java.util.Locale;
import java.util.Optional;

@Mixin(ItemStack.class)
public abstract class ItemStackMixin implements ComponentHolder, SkyblockerStack {

	@Shadow
	public abstract int getDamage();

	@Shadow
	public abstract void setDamage(int damage);

	@Unique
	private int maxDamage;

	@Unique
	private String skyblockId;

	@Unique
	private String skyblockApiId;

	@Unique
	private String neuName;

	@ModifyReturnValue(method = "getName", at = @At("RETURN"))
	private Text skyblocker$customItemNames(Text original) {
		if (Utils.isOnSkyblock()) {
			return SkyblockerConfigManager.get().general.customItemNames.getOrDefault(ItemUtils.getItemUuid(this), original);
		}

		return original;
	}

	@ModifyVariable(method = "appendTooltip", at = @At("STORE"))
	private TooltipAppender skyblocker$hideVanillaEnchants(TooltipAppender original) {
		return Utils.isOnSkyblock() && original instanceof ItemEnchantmentsComponent component ? component.withShowInTooltip(false) : original;
	}

	/**
	 * Updates the durability of this item stack every tick when in the inventory.
	 */
	@Inject(method = "inventoryTick", at = @At("TAIL"))
	private void skyblocker$updateDamage(CallbackInfo ci) {
		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;
	}

	@ModifyReturnValue(method = "isDamaged", at = @At("RETURN"))
	private boolean skyblocker$handleDamaged(boolean original) {
		return skyblocker$shouldProcess() || original;
	}

	@Unique
	private boolean skyblocker$shouldProcess() { // Durability bar renders atop of tooltips in ProfileViewer so disable on this screen
		return !(MinecraftClient.getInstance().currentScreen instanceof ProfileViewerScreen) && Utils.isOnSkyblock() && SkyblockerConfigManager.get().mining.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;
	}

	@Override
	@Nullable
	public String getSkyblockId() {
		if (skyblockId != null && !skyblockId.isEmpty()) return skyblockId;
		return skyblockId = skyblocker$getSkyblockId(true);
	}

	@Override
	@Nullable
	public String getSkyblockApiId() {
		if (skyblockApiId != null && !skyblockApiId.isEmpty()) return skyblockApiId;
		return skyblockApiId = skyblocker$getSkyblockId(false);
	}

	@Override
	@Nullable
	public String getNeuName() {
		if (neuName != null && !neuName.isEmpty()) return neuName;
		String apiId = getSkyblockApiId();
		String id = getSkyblockId();
		if (apiId == null || id == null) return null;

		if (apiId.startsWith("ISSHINY_")) apiId = id;

		return neuName = ItemTooltip.getNeuName(id, apiId);
	}

	@Unique
	private String skyblocker$getSkyblockId(boolean internalIDOnly) {
		NbtCompound customData = ItemUtils.getCustomData((ItemStack) (Object) this);

		if (customData == null || !customData.contains(ItemUtils.ID, NbtElement.STRING_TYPE)) {
			return null;
		}
		String customDataString = customData.getString(ItemUtils.ID);

		if (internalIDOnly) {
			return customDataString;
		}

		// Transformation to API format.
		//TODO future - remove this and just handle it directly for the NEU id conversion because this whole system is confusing and hard to follow
		if (customData.contains("is_shiny")) {
			return "ISSHINY_" + customDataString;
		}

		switch (customDataString) {
			case "ENCHANTED_BOOK" -> {
				if (customData.contains("enchantments")) {
					NbtCompound enchants = customData.getCompound("enchantments");
					Optional<String> firstEnchant = enchants.getKeys().stream().findFirst();
					String enchant = firstEnchant.orElse("");
					return "ENCHANTMENT_" + enchant.toUpperCase(Locale.ENGLISH) + "_" + enchants.getInt(enchant);
				}
			}

			case "PET" -> {
				if (customData.contains("petInfo")) {
					PetInfo petInfo = PetInfo.CODEC.parse(JsonOps.INSTANCE, JsonParser.parseString(customData.getString("petInfo"))).getOrThrow();
					return "LVL_1_" + petInfo.tier() + "_" + petInfo.type();
				}
			}

			case "POTION" -> {
				String enhanced = customData.contains("enhanced") ? "_ENHANCED" : "";
				String extended = customData.contains("extended") ? "_EXTENDED" : "";
				String splash = customData.contains("splash") ? "_SPLASH" : "";
				if (customData.contains("potion") && customData.contains("potion_level")) {
					return (customData.getString("potion") + "_" + customDataString + "_" + customData.getInt("potion_level")
							+ enhanced + extended + splash).toUpperCase(Locale.ENGLISH);
				}
			}

			case "RUNE" -> {
				if (customData.contains("runes")) {
					NbtCompound runes = customData.getCompound("runes");
					Optional<String> firstRunes = runes.getKeys().stream().findFirst();
					String rune = firstRunes.orElse("");
					return rune.toUpperCase(Locale.ENGLISH) + "_RUNE_" + runes.getInt(rune);
				}
			}

			case "ATTRIBUTE_SHARD" -> {
				if (customData.contains("attributes")) {
					NbtCompound shards = customData.getCompound("attributes");
					Optional<String> firstShards = shards.getKeys().stream().findFirst();
					String shard = firstShards.orElse("");
					return customDataString + "-" + shard.toUpperCase(Locale.ENGLISH) + "_" + shards.getInt(shard);
				}
			}

			case "NEW_YEAR_CAKE" -> {
				return customDataString + "_" + customData.getInt("new_years_cake");
			}

			case "PARTY_HAT_CRAB", "PARTY_HAT_CRAB_ANIMATED", "BALLOON_HAT_2024" -> {
				return customDataString + "_" + customData.getString("party_hat_color").toUpperCase(Locale.ENGLISH);
			}

			case "PARTY_HAT_SLOTH" -> {
				return customDataString + "_" + customData.getString("party_hat_emoji").toUpperCase(Locale.ENGLISH);
			}

			case "CRIMSON_HELMET", "CRIMSON_CHESTPLATE", "CRIMSON_LEGGINGS", "CRIMSON_BOOTS" -> {
				NbtCompound attributes = customData.getCompound("attributes");

				if (attributes.contains("magic_find") && attributes.contains("veteran")) {
					return customDataString + "-MAGIC_FIND-VETERAN";
				}
			}

			case "AURORA_HELMET", "AURORA_CHESTPLATE", "AURORA_LEGGINGS", "AURORA_BOOTS" -> {
				NbtCompound attributes = customData.getCompound("attributes");

				if (attributes.contains("mana_pool") && attributes.contains("mana_regeneration")) {
					return customDataString + "-MANA_POOL-MANA_REGENERATION";
				}
			}

			case "TERROR_HELMET", "TERROR_CHESTPLATE", "TERROR_LEGGINGS", "TERROR_BOOTS" -> {
				NbtCompound attributes = customData.getCompound("attributes");

				if (attributes.contains("lifeline") && attributes.contains("mana_pool")) {
					return customDataString + "-LIFELINE-MANA_POOL";
				}
			}

			case "MIDAS_SWORD" -> {
				if (customData.getInt("winning_bid") >= 50000000) {
					return customDataString + "_50M";
				}
			}

			case "MIDAS_STAFF" -> {
				if (customData.getInt("winning_bid") >= 100000000) {
					return customDataString + "_100M";
				}
			}
		}
		return customDataString;
	}
}