|
| 1 | +package coffee.waffle.emcutils.feature; |
| 2 | + |
| 3 | +import coffee.waffle.emcutils.Util; |
| 4 | +import coffee.waffle.emcutils.event.TooltipCallback; |
| 5 | +import com.google.gson.JsonArray; |
| 6 | +import com.google.gson.JsonElement; |
| 7 | +import com.google.gson.JsonObject; |
| 8 | +import com.google.gson.JsonParser; |
| 9 | +import net.minecraft.component.DataComponentTypes; |
| 10 | +import net.minecraft.item.ItemStack; |
| 11 | +import net.minecraft.item.SpawnEggItem; |
| 12 | +import net.minecraft.text.Text; |
| 13 | +import net.minecraft.util.Formatting; |
| 14 | + |
| 15 | +import java.math.RoundingMode; |
| 16 | +import java.text.DecimalFormat; |
| 17 | + |
| 18 | +public class HorseStats { |
| 19 | + public static void init() { |
| 20 | + TooltipCallback.ITEM.register((itemStack, list, tooltipContext, type) -> { |
| 21 | + if (!Util.isOnEMC() || !isHorseSpawnEgg(itemStack)) return; |
| 22 | + |
| 23 | + for (Text text : list) { |
| 24 | + if (text.getString().startsWith("This horse is a")) return; |
| 25 | + } |
| 26 | + |
| 27 | + list.add(Text.empty()); |
| 28 | + |
| 29 | + double horseRating = getRating(itemStack); |
| 30 | + |
| 31 | + list.add(Text.of("This horse is a " + horseRating + " out of 10").copy().formatted(Formatting.GREEN)); |
| 32 | + |
| 33 | + itemStack.getItem().appendTooltip(itemStack, tooltipContext, list, type); |
| 34 | + }); |
| 35 | + } |
| 36 | + |
| 37 | + @SuppressWarnings("RegExpRedundantEscape") |
| 38 | + private static boolean isHorseSpawnEgg(ItemStack item) { |
| 39 | + if (item == null || |
| 40 | + !(item.getItem() instanceof SpawnEggItem) || |
| 41 | + item.get(DataComponentTypes.CUSTOM_DATA) == null) return false; |
| 42 | + |
| 43 | + double health = 0D; |
| 44 | + double speed = 0D; |
| 45 | + double jump = 0D; |
| 46 | + |
| 47 | + if (item.get(DataComponentTypes.CUSTOM_DATA).copyNbt().get("EntityTag") != null) { |
| 48 | + try { |
| 49 | + String tagString = item.get(DataComponentTypes.CUSTOM_DATA).copyNbt().get("EntityTag").toString(); |
| 50 | + |
| 51 | + JsonObject entityTag = JsonParser.parseString(tagString).getAsJsonObject(); |
| 52 | + JsonArray attributes = entityTag.getAsJsonArray("Attributes"); |
| 53 | + |
| 54 | + for (JsonElement element : attributes) { |
| 55 | + JsonObject attribute = element.getAsJsonObject(); |
| 56 | + String name = attribute.get("Name").getAsString(); |
| 57 | + double base = attribute.get("Base").getAsDouble(); |
| 58 | + |
| 59 | + if (name.endsWith("movementSpeed") || name.endsWith("movement_speed")) { |
| 60 | + speed = base * 400; |
| 61 | + } else if (name.endsWith("jumpStrength") || name.endsWith("jump_strength")) { |
| 62 | + jump = base * 100; |
| 63 | + } else if (name.endsWith("maxHealth") || name.endsWith("max_health")) { |
| 64 | + health = base; |
| 65 | + } |
| 66 | + } |
| 67 | + } catch (NullPointerException e) { |
| 68 | + return false; |
| 69 | + } |
| 70 | + } else if (item.get(DataComponentTypes.CUSTOM_DATA).copyNbt().get("display") != null) { |
| 71 | + try { |
| 72 | + String display = item.get(DataComponentTypes.CUSTOM_DATA).copyNbt().get("display").toString(); |
| 73 | + |
| 74 | + JsonArray originalLoreArray = JsonParser.parseString(display).getAsJsonObject().getAsJsonArray("OriginalLore"); |
| 75 | + for (int i = 0; i < originalLoreArray.size(); i++) { |
| 76 | + String value = originalLoreArray.get(i).getAsString(); |
| 77 | + switch (value) { |
| 78 | + case "\"spd\"" -> |
| 79 | + speed = Double.parseDouble(originalLoreArray.get(i + 1).getAsString().replaceAll("\"", "")); |
| 80 | + case "\"jmp\"" -> |
| 81 | + jump = Double.parseDouble(originalLoreArray.get(i + 1).getAsString().replaceAll("\"", "")); |
| 82 | + case "\"mhp\"" -> |
| 83 | + health = Double.parseDouble(originalLoreArray.get(i + 1).getAsString().replaceAll("\"", "")); |
| 84 | + } |
| 85 | + } |
| 86 | + } catch (NullPointerException e) { |
| 87 | + return false; |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + return health + speed + jump != 0D; |
| 92 | + } |
| 93 | + |
| 94 | + private static double getRating(ItemStack item) { |
| 95 | + double speed = 0D; |
| 96 | + double jump = 0D; |
| 97 | + double health = 0D; |
| 98 | + |
| 99 | + if (item.get(DataComponentTypes.CUSTOM_DATA).copyNbt().get("EntityTag") != null) { |
| 100 | + String tagString = item.get(DataComponentTypes.CUSTOM_DATA).copyNbt().get("EntityTag").toString(); |
| 101 | + |
| 102 | + JsonObject entityTag = JsonParser.parseString(tagString).getAsJsonObject(); |
| 103 | + JsonArray attributes = entityTag.getAsJsonArray("Attributes"); |
| 104 | + |
| 105 | + for (JsonElement element : attributes) { |
| 106 | + JsonObject attribute = element.getAsJsonObject(); |
| 107 | + String name = attribute.get("Name").getAsString(); |
| 108 | + double base = attribute.get("Base").getAsDouble(); |
| 109 | + |
| 110 | + if (name.endsWith("movementSpeed") || name.endsWith("movement_speed")) { |
| 111 | + speed = base * 400; |
| 112 | + } else if (name.endsWith("jumpStrength") || name.endsWith("jump_strength")) { |
| 113 | + jump = base * 100; |
| 114 | + } else if (name.endsWith("maxHealth") || name.endsWith("max_health")) { |
| 115 | + health = base; |
| 116 | + } |
| 117 | + } |
| 118 | + } else if (item.get(DataComponentTypes.CUSTOM_DATA).copyNbt().get("display") != null) { |
| 119 | + String display = item.get(DataComponentTypes.CUSTOM_DATA).copyNbt().get("display").toString(); |
| 120 | + |
| 121 | + JsonArray originalLoreArray = JsonParser.parseString(display).getAsJsonObject().getAsJsonArray("OriginalLore"); |
| 122 | + for (int i = 0; i < originalLoreArray.size(); i++) { |
| 123 | + String value = originalLoreArray.get(i).getAsString(); |
| 124 | + switch (value) { |
| 125 | + case "\"spd\"" -> |
| 126 | + speed = Double.parseDouble(originalLoreArray.get(i + 1).getAsString().replaceAll("\"", "")); |
| 127 | + case "\"jmp\"" -> |
| 128 | + jump = Double.parseDouble(originalLoreArray.get(i + 1).getAsString().replaceAll("\"", "")); |
| 129 | + case "\"mhp\"" -> |
| 130 | + health = Double.parseDouble(originalLoreArray.get(i + 1).getAsString().replaceAll("\"", "")); |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + double speedRating = speed / 139.5 * 10; |
| 136 | + double jumpRating = jump / 100 * 10; |
| 137 | + double healthRating = health / 30 * 10; |
| 138 | + |
| 139 | + double overallRating = (speedRating + jumpRating + healthRating) / 3; |
| 140 | + |
| 141 | + return (double) Math.round(overallRating * 100d) / 100d; |
| 142 | + } |
| 143 | +} |
0 commit comments