Skip to content

Commit 4e0ea73

Browse files
committed
horse
1 parent f655d5b commit 4e0ea73

File tree

3 files changed

+146
-1
lines changed

3 files changed

+146
-1
lines changed
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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+
}

common/src/main/java/coffee/waffle/emcutils/mixin/ClientPlayNetworkHandlerMixin.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import coffee.waffle.emcutils.Util;
44
import coffee.waffle.emcutils.event.ChatCallback;
55
import coffee.waffle.emcutils.event.CommandCallback;
6+
import coffee.waffle.emcutils.feature.HorseStats;
67
import coffee.waffle.emcutils.feature.UsableItems;
78
import coffee.waffle.emcutils.feature.VaultScreen;
89
import coffee.waffle.emcutils.listener.ChatListener;
@@ -45,6 +46,7 @@ abstract class ClientPlayNetworkHandlerMixin {
4546
ChatListener.init();
4647
CommandListener.init();
4748
UsableItems.init();
49+
HorseStats.init();
4850

4951
emcutils$online = true;
5052
}

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
org.gradle.jvmargs=-Xmx4G
33
org.gradle.parallel=true
44

5-
mod_version=9.0.2
5+
mod_version=9.0.2-horse.1

0 commit comments

Comments
 (0)