Skip to content

Commit

Permalink
Added ShowRevertedIllegals to BetterTooltips
Browse files Browse the repository at this point in the history
Also fixed raw damage tag & true durability options

(cherry picked from commit 83eca25)
  • Loading branch information
0xTas committed Jan 3, 2025
1 parent 40ce5c3 commit 6dc4748
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 7 deletions.
50 changes: 45 additions & 5 deletions src/main/java/dev/stardust/mixin/BetterTooltipsMixin.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package dev.stardust.mixin;

import java.util.Optional;
import net.minecraft.text.Text;
import net.minecraft.nbt.NbtElement;
import net.minecraft.nbt.NbtCompound;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
Expand Down Expand Up @@ -37,6 +40,10 @@ public BetterTooltipsMixin(Category category, String name, String description) {
@Unique
private @Nullable Setting<Boolean> rawDamageTag = null;

@SuppressWarnings({"unused", "FieldCanBeLocal"})
@Unique
private @Nullable Setting<Boolean> showRevertedIllegals = null;

@Inject(method = "<init>", at = @At(value = "FIELD", target = "Lmeteordevelopment/meteorclient/systems/modules/render/BetterTooltips;beehive:Lmeteordevelopment/meteorclient/settings/Setting;", shift = At.Shift.AFTER))
private void addDurabilitySettings(CallbackInfo ci) {
trueDurability = sgOther.add(
Expand All @@ -54,6 +61,13 @@ private void addDurabilitySettings(CallbackInfo ci) {
.defaultValue(false)
.build()
);

showRevertedIllegals = sgOther.add(
new BoolSetting.Builder()
.name("show-reverted-illegals")
.description("Show illegal items which have had their count set to 0.")
.build()
);
}

@Inject(method = "appendTooltip", at = @At("TAIL"))
Expand All @@ -62,12 +76,38 @@ private void appendTrueDurability(ItemStackTooltipEvent event, CallbackInfo ci)

int damage = event.itemStack.getDamage();
int maxDamage = event.itemStack.getMaxDamage();
if (rawDamageTag.get()) {
event.list.add(Text.literal("§7Damage§3: §a§o" + damage + " §8[§7Max§3: §a§o" + maxDamage + "§8]"));
NbtCompound metadata = event.itemStack.getNbt();

if (metadata == null) return;
Optional<Integer> trueDamage = Optional.empty();
if (metadata.contains("tag", NbtElement.COMPOUND_TYPE)) {
NbtCompound tag = metadata.getCompound("tag");

int index = tag.toString().indexOf("Damage:");
if (index != -1) {
try {
String subStr = tag.toString().substring(index + 7);
trueDamage = Optional.of(Integer.parseInt(subStr.substring(0, subStr.indexOf(',') == -1 ? subStr.indexOf('}') : subStr.indexOf(','))));
} catch (Exception ignored) {}
}
}
if (trueDurability != null && trueDurability.get()) {
int durability = maxDamage - damage;
event.list.add(Text.literal("§7Durability§3: §a§o" + durability + " §8[§7Max§3: §a§o" + maxDamage + "§8]"));
if (trueDamage.isPresent()) {
int dmg = trueDamage.get();
if (rawDamageTag != null && rawDamageTag.get()) {
event.list.add(Text.literal("§7Damage§3: §a§o" + dmg + " §8[§7Max§3: §a§o" + maxDamage + "§8]"));
}
if (trueDurability != null && trueDurability.get()) {
int trueDurability = maxDamage - dmg;
event.list.add(Text.literal("§7Durability§3: §a§o" + trueDurability + " §8[§7Max§3: §a§o" + maxDamage + "§8]"));
}
} else {
if (rawDamageTag != null && rawDamageTag.get()) {
event.list.add(Text.literal("§7Damage§3: §a§o" + damage + " §8[§7Max§3: §a§o" + maxDamage + "§8]"));
}
if (trueDurability != null && trueDurability.get()) {
int durability = maxDamage - damage;
event.list.add(Text.literal("§7Durability§3: §a§o" + durability + " §8[§7Max§3: §a§o" + maxDamage + "§8]"));
}
}
}
}
65 changes: 63 additions & 2 deletions src/main/java/dev/stardust/mixin/ItemStackMixin.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package dev.stardust.mixin;

import java.util.List;
import java.util.Optional;
import net.minecraft.item.Item;
import net.minecraft.text.Text;
import net.minecraft.item.Items;
import net.minecraft.util.Rarity;
import net.minecraft.nbt.NbtElement;
import dev.stardust.modules.AntiToS;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.util.Identifier;
import net.minecraft.text.MutableText;
import net.minecraft.registry.Registries;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.jetbrains.annotations.Nullable;
Expand All @@ -16,6 +23,8 @@
import org.spongepowered.asm.mixin.injection.Inject;
import com.llamalad7.mixinextras.sugar.ref.LocalRef;
import meteordevelopment.meteorclient.systems.modules.Modules;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import meteordevelopment.meteorclient.systems.modules.render.BetterTooltips;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

/**
Expand All @@ -27,7 +36,7 @@ public abstract class ItemStackMixin {
public abstract Rarity getRarity();

// See AntiToS.java
@Inject(method = "getTooltip", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;hasCustomName()Z", shift = At.Shift.BEFORE))
@Inject(method = "getTooltip", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;hasCustomName()Z"))
private void censorItemTooltip(@Nullable PlayerEntity player, TooltipContext context, CallbackInfoReturnable<List<Text>> cir, @Local(ordinal = 0)LocalRef<MutableText> name) {
Modules modules = Modules.get();
if (modules == null) return;
Expand All @@ -39,7 +48,7 @@ private void censorItemTooltip(@Nullable PlayerEntity player, TooltipContext con
}
}

@Inject(method = "toHoverableText", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;hasCustomName()Z", shift = At.Shift.BEFORE))
@Inject(method = "toHoverableText", at = @At(value = "INVOKE", target = "Lnet/minecraft/item/ItemStack;hasCustomName()Z"))
private void censorHoveredText(CallbackInfoReturnable<Text> cir, @Local(ordinal = 0)LocalRef<MutableText> name) {
Modules modules = Modules.get();
if (modules == null) return;
Expand All @@ -50,4 +59,56 @@ private void censorHoveredText(CallbackInfoReturnable<Text> cir, @Local(ordinal
name.set(Text.empty().append(antiToS.censorText(name.get().getString())));
}
}

// See BetterTooltipsMixin.java
@Inject(method = "fromNbt", at = @At("HEAD"), cancellable = true)
private static void showRevertedIllegals(NbtCompound nbt, CallbackInfoReturnable<ItemStack> cir) {
if (nbt == null) return;
Modules mods = Modules.get();
if (mods == null) return;

BetterTooltips tooltips = mods.get(BetterTooltips.class);
var showReverted = tooltips.settings.get("show-reverted-illegals");

Optional<Integer> trueDamage = Optional.empty();
if (showReverted != null && ((boolean) showReverted.get())) {
Item item = Registries.ITEM.get(new Identifier(nbt.getString("id")));
int count = nbt.getByte("Count");

if (nbt.contains("tag", NbtElement.COMPOUND_TYPE)) {
NbtCompound tag = nbt.getCompound("tag");
int index = tag.toString().indexOf("Damage:");
if (index != -1) {
try {
String subStr = tag.toString().substring(index + 7);
trueDamage = Optional.of(Integer.parseInt(subStr.substring(0, subStr.indexOf(',') == -1 ? subStr.indexOf('}') : subStr.indexOf(','))));
} catch (Exception ignored) {}
}
}

if (count <= 0 && item != Items.AIR) {
if (item.isDamageable() && trueDamage.isPresent()) {
int dmg = trueDamage.get();
nbt.putInt("Damage", dmg);
}
cir.cancel();
cir.setReturnValue(ItemStackAccessor.invokeInit(item, 69, Optional.of(nbt)));
}
}
}

@Inject(method = "setDamage", at = @At("HEAD"), cancellable = true)
private void enableTrueDamage(int damage, CallbackInfo ci) {
if (damage >= 0) return;
Modules mods = Modules.get();
if (mods == null) return;

BetterTooltips tooltips = mods.get(BetterTooltips.class);
var rawDamageTag = tooltips.settings.get("raw-damage-tag");
var trueDurability = tooltips.settings.get("true-durability");
if ((rawDamageTag != null && ((boolean) rawDamageTag.get())) || (trueDurability != null && ((boolean) trueDurability.get()))) {
ci.cancel();
((ItemStack)(Object) this).getOrCreateNbt().putInt("Damage", damage);
}
}
}
1 change: 1 addition & 0 deletions src/main/resources/stardust.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"NoRenderMixin",
"ItemStackMixin",
"DoorBlockMixin",
"ItemStackAccessor",
"PlayerEntityMixin",
"GoatHornItemMixin",
"LivingEntityMixin",
Expand Down

0 comments on commit 6dc4748

Please sign in to comment.