Skip to content

Commit 066fd20

Browse files
authored
Merge pull request #136 from Reasonlesss/fix/action-dump-crash
Prevent crashes if new types are added to DiamondFire
2 parents e3c1534 + 75de3bb commit 066fd20

4 files changed

Lines changed: 78 additions & 83 deletions

File tree

src/main/java/dev/dfonline/codeclient/dev/menu/SlotGhostManager.java

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@
2020
import net.minecraft.client.render.RenderLayer;
2121
import net.minecraft.entity.player.PlayerInventory;
2222
import net.minecraft.item.ItemStack;
23+
import net.minecraft.item.Items;
2324
import net.minecraft.screen.slot.Slot;
24-
import net.minecraft.text.Text;
2525
import net.minecraft.util.hit.BlockHitResult;
2626
import org.jetbrains.annotations.Nullable;
2727

2828
import java.util.ArrayList;
29+
import java.util.List;
2930

3031
public class SlotGhostManager extends Feature {
3132
private Action action;
@@ -84,10 +85,10 @@ public void render(DrawContext context, int mouseX, int mouseY, int x, int y, fl
8485

8586
@Nullable
8687
public Argument getArgument(Slot slot) {
87-
var args = new ArrayList<Argument>();
88+
List<Argument> args = new ArrayList<>();
8889

89-
for (var group : action.icon.getArgGroups()) {
90-
var pos = group.getPossibilities();
90+
for (Icon.ArgumentGroup group : action.icon.getArgGroups()) {
91+
List<Icon.ArgumentGroup.ArgumentPossibilities> pos = group.getPossibilities();
9192
args.addAll(pos.get((int) (time / 30F) % pos.size()).arguments());
9293
}
9394

@@ -97,35 +98,29 @@ public Argument getArgument(Slot slot) {
9798
}
9899

99100
public void drawSlot(DrawContext context, Slot slot) {
100-
try {
101-
if (!(CodeClient.MC.currentScreen instanceof GenericContainerScreen || CodeClient.MC.currentScreen instanceof CustomChestMenu)) {
102-
action = null;
103-
}
104-
if (badAction() || slot.inventory instanceof PlayerInventory)
105-
return;
106-
if (slot.hasStack()) return;
107-
var arg = getArgument(slot);
108-
if (arg == null) return;
109-
Icon.Type type = Icon.Type.valueOf(arg.type);
110-
ItemStack itemStack = type.getIcon();
111-
if (itemStack.isEmpty()) return;
112-
// itemStack.setCount(slot.id);
113-
context.fill(slot.x, slot.y, slot.x + 16, slot.y + 16, arg.optional ? 0x50__90_90_ff : 0x30__ff_00_00);
114-
context.drawItem(itemStack, slot.x, slot.y);
115-
context.drawStackOverlay(CodeClient.MC.textRenderer, itemStack, slot.x, slot.y);
116-
// context.drawText(CodeClient.MC.textRenderer, Text.literal(arg.type),slot.x,slot.y,0xFFFFFF00,true);
117-
context.fill(RenderLayer.getGuiGhostRecipeOverlay(), slot.x, slot.y, slot.x + 16, slot.y + 16, 0x40ffffff);
118-
} catch (Exception e) {
119-
context.drawText(CodeClient.MC.textRenderer, Text.literal("Error."), slot.x, slot.y, 0xFF0000, true);
101+
if (!(CodeClient.MC.currentScreen instanceof GenericContainerScreen || CodeClient.MC.currentScreen instanceof CustomChestMenu)) {
102+
action = null;
120103
}
104+
if (badAction() || slot.inventory instanceof PlayerInventory)
105+
return;
106+
if (slot.hasStack()) return;
107+
Argument arg = getArgument(slot);
108+
if (arg == null) return;
109+
ItemStack itemStack = arg.getItem();
110+
111+
if (itemStack.isEmpty()) return;
112+
context.fill(slot.x, slot.y, slot.x + 16, slot.y + 16, arg.optional ? 0x50__90_90_ff : 0x30__ff_00_00);
113+
context.drawItem(itemStack, slot.x, slot.y);
114+
context.drawStackOverlay(CodeClient.MC.textRenderer, itemStack, slot.x, slot.y);
115+
context.fill(RenderLayer.getGuiGhostRecipeOverlay(), slot.x, slot.y, slot.x + 16, slot.y + 16, 0x40ffffff);
121116
}
122117

123118
@Override @Nullable
124119
public ItemStack getHoverStack(Slot slot) {
125120
if (badAction() || slot.inventory instanceof PlayerInventory)
126121
return null;
127122
if (slot.hasStack()) return null;
128-
var arg = getArgument(slot);
123+
Argument arg = getArgument(slot);
129124
if (arg == null) return null;
130125
return arg.getItem();
131126
}

src/main/java/dev/dfonline/codeclient/dev/overlay/ChestPeeker.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,10 @@ public List<Text> getOverlayText() {
150150
text.append(item.getCount() + "x ");
151151
text.append(item.getName());
152152
} else {
153+
JsonObject object = JsonParser.parseString(varItem).getAsJsonObject();
153154
try {
154-
JsonObject object = JsonParser.parseString(varItem).getAsJsonObject();
155155
Type type = Type.valueOf(object.get("id").getAsString());
156156
JsonObject data = object.get("data").getAsJsonObject();
157-
// JsonArray lore = data.get("display").getAsJsonObject().get("Lore").getAsJsonArray();
158157
text.append(Text.literal(type.name.toUpperCase()).fillStyle(Style.EMPTY.withColor(type.color)).append(" "));
159158
if (type == Type.var) {
160159
Scope scope = Scope.valueOf(data.get("scope").getAsString());
@@ -202,8 +201,11 @@ public List<Text> getOverlayText() {
202201
text.append(Text.literal(data.get("option").getAsString()).formatted(Formatting.AQUA));
203202
}
204203
if (type == Type.hint) continue;
205-
} catch (Exception ignored) {
206-
continue;
204+
} catch (IllegalArgumentException ignored) {
205+
text.append(Text.literal(object.get("id").getAsString().toUpperCase())
206+
.styled(style -> style.withColor(TextColor.fromRgb(0x808080)))
207+
.append(" "));
208+
text.append(item.getName());
207209
}
208210
}
209211
texts.add(text);

src/main/java/dev/dfonline/codeclient/hypercube/actiondump/Argument.java

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
package dev.dfonline.codeclient.hypercube.actiondump;
22

3+
import com.ibm.icu.text.CaseMap;
34
import dev.dfonline.codeclient.Utility;
45
import dev.dfonline.codeclient.data.DFItem;
56
import net.minecraft.item.ItemStack;
7+
import net.minecraft.item.Items;
68
import net.minecraft.text.MutableText;
79
import net.minecraft.text.Text;
10+
import net.minecraft.text.TextColor;
811
import net.minecraft.util.Formatting;
912
import org.jetbrains.annotations.Nullable;
1013

1114
import java.util.ArrayList;
1215
import java.util.List;
16+
import java.util.Locale;
1317
import java.util.Objects;
1418

1519
public class Argument {
@@ -30,9 +34,10 @@ public Icon.Type getType() {
3034
}
3135

3236
public ItemStack getItem() {
33-
Icon.Type icon = getType();
34-
if(icon == null) return ItemStack.EMPTY;
35-
var item = icon.getIcon();
37+
Icon.Type type = getType();
38+
ItemStack item = type == null
39+
? Items.GRAY_DYE.getDefaultStack()
40+
: type.getIcon();
3641

3742
DFItem dfItem = DFItem.of(item);
3843
// First line is item name, others are lore
@@ -50,10 +55,22 @@ public List<Text> getLore() {
5055
int i = 0;
5156
if (this.text != null) addToLore(lore, this.text);
5257
if (this.description != null) for (String line : this.description) {
53-
Icon.Type type = Icon.Type.valueOf(this.type);
58+
MutableText text = Text.empty().formatted(Formatting.GRAY).styled(s -> s.withItalic(false));
59+
MutableText typeText;
60+
61+
try {
62+
Icon.Type type = Icon.Type.valueOf(this.type);
63+
typeText = Text.literal(type.display).setStyle(Text.empty().getStyle().withColor(type.color).withItalic(false));
64+
} catch (IllegalArgumentException e) {
65+
// Show a grayed out version of the name.
66+
String properCase = CaseMap.Title.toTitle().apply(Locale.ENGLISH, null, this.type.replaceAll("_", " "));
67+
typeText = Text.literal(properCase).setStyle(Text.empty().getStyle()
68+
.withColor(TextColor.fromRgb(0x808080))
69+
.withItalic(false));
70+
}
71+
5472
if (i == 0) {
55-
MutableText text = Text.empty().formatted(Formatting.GRAY).styled(s -> s.withItalic(false));
56-
MutableText typeText = Text.literal(type.display).setStyle(Text.empty().getStyle().withColor(type.color).withItalic(false));
73+
5774
if (this.plural) typeText.append("(s)");
5875
text.append(typeText);
5976
if (this.optional) {
@@ -80,10 +97,12 @@ public List<Text> getLore() {
8097
public boolean isOr() {
8198
return text != null && text.endsWith("OR");
8299
}
100+
83101
public boolean isSplitter() {
84-
return Objects.equals(text,"");
102+
return Objects.equals(text, "");
85103
}
86104

87105
private void addToLore(ArrayList<Text> lore, String text) {
88106
lore.add(Utility.textFromString(text));
89-
}}
107+
}
108+
}

src/main/java/dev/dfonline/codeclient/hypercube/actiondump/Icon.java

Lines changed: 25 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package dev.dfonline.codeclient.hypercube.actiondump;
22

3+
import com.ibm.icu.text.CaseMap;
34
import dev.dfonline.codeclient.Utility;
45
import dev.dfonline.codeclient.data.DFItem;
56
import dev.dfonline.codeclient.data.ItemData;
@@ -19,10 +20,12 @@
1920
import net.minecraft.util.Formatting;
2021
import net.minecraft.util.Identifier;
2122
import net.minecraft.util.Util;
23+
import org.apache.commons.lang3.text.WordUtils;
2224
import org.jetbrains.annotations.Nullable;
2325

2426
import java.util.ArrayList;
2527
import java.util.List;
28+
import java.util.Locale;
2629

2730
public class Icon {
2831
private static final TextColor GOLD = TextColor.fromFormatting(Formatting.GOLD);
@@ -78,33 +81,7 @@ public ItemStack getItem() {
7881
addToLore(lore, "Chest Parameters:");
7982
boolean hasOptional = false;
8083
for (Argument arg : arguments) {
81-
int i = 0;
82-
if (arg.text != null) addToLore(lore, arg.text);
83-
if (arg.description != null && description.length != 0) for (String line : arg.description) {
84-
Type type = Type.valueOf(arg.type);
85-
if (i == 0) {
86-
MutableText text = Text.empty().formatted(Formatting.GRAY).styled(s -> s.withItalic(false));
87-
MutableText typeText = Text.literal(type.display).setStyle(Text.empty().getStyle().withColor(type.color).withItalic(false));
88-
if (arg.plural) typeText.append("(s)");
89-
text.append(typeText);
90-
if (arg.optional) {
91-
text.append(Text.literal("*").formatted(Formatting.WHITE));
92-
hasOptional = true;
93-
}
94-
text.append(Text.literal(" - ").formatted(Formatting.DARK_GRAY));
95-
text.append(Utility.textFromString(line).formatted(Formatting.GRAY));
96-
lore.add(text);
97-
} else lore.add(Utility.textFromString(line).formatted(Formatting.GRAY));
98-
i++;
99-
}
100-
if (arg.notes != null) for (String[] lines : arg.notes) {
101-
i = 0;
102-
if (lines != null) for (String line : lines) {
103-
if (i == 0) addToLore(lore, "§9⏵ §7" + line);
104-
else addToLore(lore, "§7" + line);
105-
i++;
106-
}
107-
}
84+
lore.addAll(arg.getLore());
10885
}
10986
if (tags != null && tags != 0) {
11087
lore.add(Text.literal("# ").formatted(Formatting.DARK_AQUA).styled(s -> s.withItalic(false)).append(Text.literal(tags + " Tag" + (tags != 1 ? "s" : "")).formatted(Formatting.GRAY)));
@@ -149,17 +126,16 @@ public ItemStack getItem() {
149126
if (cancelledAutomatically) addToLore(lore, "§4∅ §cCancelled automatically");
150127
else addToLore(lore, "§4∅ §cCancellable");
151128
}
152-
if(requireTokens) {
153-
addToLore(lore,"");
129+
if (requireTokens) {
130+
addToLore(lore, "");
154131
lore.add(Text.literal("Unlock with Tokens").withColor(0xffd42a));
155132
}
156-
if(requiredRank != null) {
157-
if(requireTokens) {
133+
if (requiredRank != null) {
134+
if (requireTokens) {
158135
lore.add(Text.literal("OR").withColor(0xff55aa));
159136
lore.add(Text.literal("Unlock with " + requiredRank.name).withColor(requiredRank.color.getRgb()));
160-
}
161-
else {
162-
addToLore(lore,"");
137+
} else {
138+
addToLore(lore, "");
163139
lore.add(Text.literal(requiredRank.name + " Exclusive").setStyle(Style.EMPTY.withColor(requiredRank.color.getRgb()).withItalic(false)));
164140
}
165141
}
@@ -185,8 +161,8 @@ private void addToLore(ArrayList<Text> lore, String text) {
185161
public List<ArgumentGroup> getArgGroups() {
186162
var groups = new ArrayList<ArgumentGroup>();
187163
var group = new ArrayList<Argument>();
188-
for(var arg: arguments) {
189-
if(!arg.isSplitter()) group.add(arg);
164+
for (var arg : arguments) {
165+
if (!arg.isSplitter()) group.add(arg);
190166
else {
191167
groups.add(new ArgumentGroup(group));
192168
group = new ArrayList<>();
@@ -200,6 +176,7 @@ public enum Type {
200176
TEXT(TextColor.fromFormatting(Formatting.AQUA), "String", Items.STRING, dev.dfonline.codeclient.hypercube.item.Text::new),
201177
COMPONENT(TextColor.fromRgb(0x7fd42a), "Styled Text", Items.BOOK, Component::new),
202178
NUMBER(TextColor.fromFormatting(Formatting.RED), "Number", Items.SLIME_BALL, Number::new),
179+
BYTE(TextColor.fromFormatting(Formatting.RED), "Byte", Items.SLIME_BALL, Number::new),
203180
LOCATION(TextColor.fromFormatting(Formatting.GREEN), "Location", Items.PAPER, Location::new),
204181
VECTOR(TextColor.fromRgb(0x2AFFAA), "Vector", Items.PRISMARINE_SHARD, Vector::new),
205182
SOUND(TextColor.fromFormatting(Formatting.BLUE), "Sound", Items.NAUTILUS_SHELL, Sound::new),
@@ -216,18 +193,19 @@ public enum Type {
216193
BLOCK_TAG(TextColor.fromFormatting(Formatting.AQUA), "Block Tag", Items.CHAIN_COMMAND_BLOCK, dev.dfonline.codeclient.hypercube.item.Text::new),
217194
LIST(TextColor.fromFormatting(Formatting.DARK_GREEN), "List", Items.SKULL_BANNER_PATTERN), // Ender chest or empty banner pattern
218195
DICT(TextColor.fromRgb(0x55AAFF), "Dictionary", Items.KNOWLEDGE_BOOK), // Knowledge book or chest minecart
219-
NONE(TextColor.fromRgb(0x808080), "None", Items.AIR),
196+
NONE(TextColor.fromRgb(0x808080), "None", Items.AIR)
220197
;
221198

222199
public final TextColor color;
223200
public final String display;
224201
private final ItemStack icon;
225-
@Nullable public final Icon.Type.getVarItem getVarItem;
202+
@Nullable
203+
public final Icon.Type.getVarItem getVarItem;
226204

227205
Type(TextColor color, String display, Item icon) {
228206
this.color = color;
229207
this.display = display;
230-
var item = icon.getDefaultStack();
208+
ItemStack item = icon.getDefaultStack();
231209
this.icon = item;
232210
getVarItem = null;
233211
}
@@ -249,14 +227,15 @@ public ItemStack getIcon() {
249227
}
250228

251229
public enum RequiredRank {
252-
Noble("Noble",TextColor.fromRgb(0x7fff7f)),
253-
Emperor("Emperor",TextColor.fromRgb(0x55aaff)),
254-
Mythic("Mythic",TextColor.fromRgb(0xd42ad4)),
255-
Overlord("Overlord",TextColor.fromFormatting(Formatting.RED)),
256-
Dev("",TextColor.fromRgb(0));
230+
Noble("Noble", TextColor.fromRgb(0x7fff7f)),
231+
Emperor("Emperor", TextColor.fromRgb(0x55aaff)),
232+
Mythic("Mythic", TextColor.fromRgb(0xd42ad4)),
233+
Overlord("Overlord", TextColor.fromFormatting(Formatting.RED)),
234+
Dev("", TextColor.fromRgb(0));
257235

258236
public final String name;
259237
public final TextColor color;
238+
260239
RequiredRank(String name, TextColor color) {
261240
this.name = name;
262241
this.color = color;
@@ -267,8 +246,8 @@ public record ArgumentGroup(List<Argument> arguments) {
267246
public List<ArgumentPossibilities> getPossibilities() {
268247
var groups = new ArrayList<ArgumentPossibilities>();
269248
var group = new ArrayList<Argument>();
270-
for(var arg: arguments) {
271-
if(!arg.isOr()) group.add(arg);
249+
for (var arg : arguments) {
250+
if (!arg.isOr()) group.add(arg);
272251
else {
273252
groups.add(new ArgumentPossibilities(group));
274253
group = new ArrayList<>();

0 commit comments

Comments
 (0)