|
1 | 1 | package mod.gate.features; |
2 | 2 |
|
3 | 3 | import mod.gate.core.config.Config; |
4 | | -import mod.gate.core.events.TickEvent; |
5 | 4 | import mod.gate.core.events.Event; |
6 | | -import mod.gate.core.events.JoinWorldEvent; |
7 | 5 | import mod.gate.utils.ChatUtils; |
8 | 6 | import mod.gate.utils.Tablist; |
9 | 7 | import net.minecraft.text.ClickEvent; |
10 | 8 | import net.minecraft.text.HoverEvent; |
11 | 9 | import net.minecraft.text.Style; |
12 | 10 | import net.minecraft.text.Text; |
13 | | -import org.apache.commons.lang3.reflect.FieldUtils; |
| 11 | + |
| 12 | +import java.util.ArrayList; |
14 | 13 |
|
15 | 14 | public class ForgeReminder { |
16 | 15 | //region CONFIG |
17 | 16 | @Config(description = "the configuration for the forge reminder") |
18 | | - public ForgeConfig forgereminder = new ForgeConfig(); |
| 17 | + public ForgeConfig forgeReminder = new ForgeConfig(); |
19 | 18 | //endregion |
20 | 19 |
|
| 20 | + @Event(event = Tablist.TablistUpdateEvent.class) |
| 21 | + public void onTablistUpdate(Tablist.TablistUpdateEvent event) { |
| 22 | + if (event.type != Tablist.TablistUpdateType.OTHER) return; |
21 | 23 |
|
22 | | - private boolean hasReceivedTabList = false; |
| 24 | + // Finding if the 5th slot is unlocked, it isn't shown on Tablist |
| 25 | + if (event.line.equals("Forges (+1 more)") && !forgeReminder.slots.get(5).unlocked) { |
| 26 | + forgeReminder.slots.get(5).unlocked = true; |
| 27 | + } |
23 | 28 |
|
24 | | - @Event(event = JoinWorldEvent.class) |
25 | | - public void onJoinWorldEvent(JoinWorldEvent event) { |
26 | | - if (!this.hasReceivedTabList || !this.forgereminder.enabled) return; |
27 | | - //region check for slots |
28 | | - for (int i = 1; i <= 5; i++) { |
29 | | - try { |
30 | | - long slotTime = FieldUtils.getField(ForgeConfig.class,"slot" + i).getLong(forgereminder); |
| 29 | + for (ForgeSlot slot : forgeReminder.slots) { |
| 30 | + if (slot.slotNumber != 5) { |
| 31 | + processLine(slot, event.line); |
| 32 | + }; |
31 | 33 |
|
32 | | - //if (FieldUtils.getField(ForgeConfig.class, "unlocked" + i).getBoolean(forgereminder)) ChatUtils.sendChatMessage("slot" + i + " is currently at timestamp: " + slotTime); |
33 | | - if (FieldUtils.getField(ForgeConfig.class, "unlocked" + i).getBoolean(forgereminder) |
34 | | - && System.currentTimeMillis() >= slotTime && slotTime != 0L) |
35 | | - { |
36 | | - //forgereminder.getClass().getField("slot" + i).set(forgereminder, 0L);//set that when the item is removed from forge |
| 34 | + if (!slot.unlocked && System.currentTimeMillis() < slot.time && slot.time == 0L && !slot.notify) continue; |
| 35 | + slot.notify = false; |
37 | 36 |
|
38 | | - Style style = Style.EMPTY.withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, Text.of("Click to warp to the forge (if unlocked)"))); |
39 | | - style.withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/warpforge"));//TODO find a way to make this click event work |
| 37 | + Style style = Style.EMPTY |
| 38 | + .withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, Text.of("Click to warp to the forge [if unlocked]"))) |
| 39 | + //TODO find a way to make this click event work |
| 40 | + .withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/warpforge")); |
40 | 41 |
|
41 | | - ChatUtils.sendChatMessage(Text.of("§l§6[Gate/ForgeReminder]§f§b Forge §3slot " + i + " is available for pickup.").getWithStyle(style).get(0)); |
42 | | - } |
43 | | - }catch (Exception e) { |
44 | | - e.printStackTrace(); |
45 | | - } |
| 42 | + ChatUtils.sendChatMessage(Text.of(ChatUtils.PREFIX + "§8 | [§bForgeReminder§8] §f> " + "Forge §3slot " + slot.slotNumber + " §fis available for pickup.").getWithStyle(style).get(0)); |
46 | 43 | } |
47 | | - //endregion |
48 | | - |
49 | | - this.hasReceivedTabList = false;//first scoreboard packet isn't received at that point |
50 | 44 | } |
51 | 45 |
|
52 | | - @Event(event = TickEvent.class) |
53 | | - public void onTick(TickEvent event) { |
54 | | - if (this.hasReceivedTabList || Tablist.getLines() == null || !this.forgereminder.enabled) return; |
55 | | - this.hasReceivedTabList = true; |
56 | | - //first tick of the scoreboard being accessible |
57 | | - if (!Tablist.getArea().equals("dwarven_mines")) return; |
58 | | - |
59 | | - |
60 | | - for (String line: Tablist.getLines()) { |
61 | | - if (line.equals("Forges (+1 more)") && !forgereminder.unlocked5) { |
62 | | - forgereminder.unlocked5 = true;//5th one is unlocked |
63 | | - } |
64 | | - |
65 | | - for (int i = 1; i<=4; i++) {//forge 5 isn't accessible through tablist |
66 | | - try { |
67 | | - if (line.startsWith(i+")")) { |
68 | | - if (line.endsWith("EMPTY")) { |
69 | | - if (FieldUtils.getField(ForgeConfig.class, "slot" + i).getLong(forgereminder) != 0L) { |
70 | | - forgereminder.getClass().getField("slot" + i).set(forgereminder, 0L);//clear date |
71 | | - } |
72 | | - |
73 | | - if (!FieldUtils.getField(ForgeConfig.class, "unlocked" + i).getBoolean(forgereminder)) { |
74 | | - forgereminder.getClass().getField("unlocked" + i).set(forgereminder, true);//set to unlocked |
75 | | - } |
76 | | - |
77 | | - } else if (line.endsWith("Ready!")) { |
78 | | - FieldUtils.getField(ForgeConfig.class, "slot"+i).set(forgereminder, System.currentTimeMillis()); |
79 | | - if (!FieldUtils.getField(ForgeConfig.class, "unlocked" + i).getBoolean(forgereminder)) { |
80 | | - forgereminder.getClass().getField("unlocked" + i).set(forgereminder, true);//set to unlocked |
81 | | - } |
82 | | - } else { |
83 | | - String DatetoParse = line.substring(3); |
84 | | - |
85 | | - long total = 0L; |
86 | | - |
87 | | - for (String time: DatetoParse.split(" ")) { |
88 | | - try { |
89 | | - if (time.contains("s")) { |
90 | | - time = time.replace("s", ""); |
91 | | - int s = Integer.parseInt(time); |
92 | | - total+= s * 1000L; |
93 | | - } else if (time.contains("m")) { |
94 | | - time = time.replace("m", ""); |
95 | | - int m = Integer.parseInt(time); |
96 | | - total += m * 60000L; |
97 | | - } else if (time.contains("h")) { |
98 | | - time = time.replace("h", ""); |
99 | | - int h = Integer.parseInt(time); |
100 | | - total += h * 3600000L; |
101 | | - } else if (time.contains("d")) { |
102 | | - time = time.replace("d", ""); |
103 | | - int d = Integer.parseInt(time); |
104 | | - total += d * 86400000L; |
105 | | - } |
106 | | - }catch (Exception ignored) {} |
107 | | - //System.out.println("parsed time: " + total); |
108 | | - total += System.currentTimeMillis(); |
109 | | - //might be useful to do a check to see if result is accurate |
110 | | - |
111 | | - //System.out.println("when it will end: " + (total + System.currentTimeMillis())); |
112 | | - FieldUtils.getField(ForgeConfig.class, "slot"+i).set(forgereminder, total); |
113 | | - } |
114 | | - |
115 | | - if (!FieldUtils.getField(ForgeConfig.class, "unlocked" + i).getBoolean(forgereminder)) { |
116 | | - forgereminder.getClass().getField("unlocked" + i).set(forgereminder, true);//set to unlocked |
117 | | - } |
118 | | - } |
| 46 | + private void processLine(ForgeSlot slot, String line) { |
| 47 | + if (!line.startsWith(slot.slotNumber + ")")) return; |
| 48 | + |
| 49 | + if (line.endsWith("EMPTY")) { |
| 50 | + if (slot.time != 0L) slot.time = 0L; |
| 51 | + |
| 52 | + slot.unlocked = true; |
| 53 | + slot.notify = true; |
| 54 | + } else if (line.endsWith("Ready!")) { |
| 55 | + slot.time = System.currentTimeMillis(); |
| 56 | + |
| 57 | + slot.unlocked = true; |
| 58 | + } else { |
| 59 | + String DateToParse = line.substring(3); |
| 60 | + long total = 0L; |
| 61 | + |
| 62 | + for (String time : DateToParse.split(" ")) { |
| 63 | + if (time.contains("s")) { |
| 64 | + time = time.replace("s", ""); |
| 65 | + int s = Integer.parseInt(time); |
| 66 | + total+= s * 1000L; |
| 67 | + } else if (time.contains("m")) { |
| 68 | + time = time.replace("m", ""); |
| 69 | + int m = Integer.parseInt(time); |
| 70 | + total += m * 60000L; |
| 71 | + } else if (time.contains("h")) { |
| 72 | + time = time.replace("h", ""); |
| 73 | + int h = Integer.parseInt(time); |
| 74 | + total += h * 3600000L; |
| 75 | + } else if (time.contains("d")) { |
| 76 | + time = time.replace("d", ""); |
| 77 | + int d = Integer.parseInt(time); |
| 78 | + total += d * 86400000L; |
119 | 79 | } |
120 | | - }catch (Exception e) { |
121 | | - e.printStackTrace(); |
122 | 80 | } |
| 81 | + |
| 82 | + total += System.currentTimeMillis(); |
| 83 | + |
| 84 | + slot.time = total; |
| 85 | + |
| 86 | + slot.unlocked = true; |
| 87 | + slot.notify = true; |
123 | 88 | } |
124 | | - } |
125 | 89 | } |
126 | 90 |
|
127 | 91 | public static class ForgeConfig { |
128 | 92 | public boolean enabled = true; |
| 93 | + public ArrayList<ForgeSlot> slots = new ArrayList<>(); |
| 94 | + |
| 95 | + public ForgeConfig() { |
| 96 | + slots.add(new ForgeSlot(1)); |
| 97 | + slots.add(new ForgeSlot(2)); |
| 98 | + slots.add(new ForgeSlot(3)); |
| 99 | + slots.add(new ForgeSlot(4)); |
| 100 | + slots.add(new ForgeSlot(5)); |
| 101 | + } |
| 102 | + } |
129 | 103 |
|
130 | | - public long slot1 = 0L; |
131 | | - public boolean unlocked1 = false; |
132 | | - |
133 | | - public long slot2 = 0L; |
134 | | - public boolean unlocked2 = false; |
135 | | - |
136 | | - public long slot3 = 0L; |
137 | | - public boolean unlocked3 = false; |
138 | | - |
139 | | - public long slot4 = 0L; |
140 | | - public boolean unlocked4 = false; |
141 | | - |
142 | | - public long slot5 = 0L;//inaccessible through tab list |
143 | | - public boolean unlocked5 = false;//inaccessible through tab list |
| 104 | + public static class ForgeSlot { |
| 105 | + public long time; |
| 106 | + public boolean unlocked; |
| 107 | + public int slotNumber; |
| 108 | + public boolean notify; |
| 109 | + |
| 110 | + public ForgeSlot(int slotNumber) { |
| 111 | + this.slotNumber = slotNumber; |
| 112 | + this.time = 0L; |
| 113 | + this.unlocked = false; |
| 114 | + this.notify = true; |
| 115 | + } |
144 | 116 | } |
145 | 117 | } |
0 commit comments