Skip to content

Commit 79d5f91

Browse files
committed
Add sanity check to make sure core mod loads properly
1 parent 23883fc commit 79d5f91

File tree

6 files changed

+122
-31
lines changed

6 files changed

+122
-31
lines changed

src/main/java/net/earthcomputer/clientcommands/ClientCommandsMod.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ private void registerEventStuff() {
7474

7575
EventManager.addDisconnectListener(e -> TempRules.resetToDefault());
7676

77+
EventManager.addPlayerTickListener(new CoreModSanityCheck());
78+
7779
MinecraftForge.EVENT_BUS.register(EventManager.INSTANCE);
7880
}
7981

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package net.earthcomputer.clientcommands;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
7+
import net.earthcomputer.clientcommands.EventManager.Listener;
8+
import net.earthcomputer.clientcommands.core.ClientCommandsLoadingPlugin;
9+
import net.minecraft.crash.CrashReport;
10+
import net.minecraft.crash.CrashReportCategory;
11+
import net.minecraft.util.ReportedException;
12+
import net.minecraftforge.fml.common.gameevent.TickEvent.PlayerTickEvent;
13+
14+
public class CoreModSanityCheck implements Listener<PlayerTickEvent> {
15+
16+
@Override
17+
public void accept(PlayerTickEvent t) {
18+
List<String> remainingTasks = new ArrayList<>(ClientCommandsLoadingPlugin.getExpectedTasks());
19+
if (!remainingTasks.isEmpty()) {
20+
Collections.sort(remainingTasks);
21+
CrashReport crashReport = CrashReport.makeCrashReport(new AssertionError(),
22+
"CLIENT COMMANDS CORE MOD FAILED TO EXECUTE PROPERLY!");
23+
CrashReportCategory failedCtgy = crashReport.makeCategory("Failed tasks");
24+
remainingTasks.forEach(task -> failedCtgy.addCrashSection(task, ""));
25+
throw new ReportedException(crashReport);
26+
}
27+
}
28+
29+
@Override
30+
public boolean isOneTime() {
31+
return true;
32+
}
33+
34+
}

src/main/java/net/earthcomputer/clientcommands/EventManager.java

Lines changed: 57 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package net.earthcomputer.clientcommands;
22

33
import java.util.ArrayList;
4+
import java.util.Iterator;
45
import java.util.List;
5-
import java.util.function.Consumer;
66

77
import net.earthcomputer.clientcommands.ToolDamageManager.ToolDamagedEvent;
88
import net.earthcomputer.clientcommands.command.CommandRelog;
@@ -47,7 +47,7 @@ private EventManager() {
4747

4848
private static Listeners<ClientDisconnectionFromServerEvent> disconnectListeners = new Listeners<>();
4949

50-
public static void addDisconnectListener(Consumer<ClientDisconnectionFromServerEvent> listener) {
50+
public static void addDisconnectListener(Listener<ClientDisconnectionFromServerEvent> listener) {
5151
disconnectListeners.add(listener);
5252
}
5353

@@ -64,7 +64,7 @@ public void onDisconnect(ClientDisconnectionFromServerEvent e) {
6464

6565
private static Listeners<ClientTickEvent> tickListeners = new Listeners<>();
6666

67-
public static void addTickListener(Consumer<ClientTickEvent> listener) {
67+
public static void addTickListener(Listener<ClientTickEvent> listener) {
6868
tickListeners.add(listener);
6969
}
7070

@@ -77,7 +77,7 @@ public void onTick(ClientTickEvent e) {
7777

7878
private static Listeners<PacketEvent.Outbound.Pre> outboundPacketPreListeners = new Listeners<>();
7979

80-
public static void addOutboundPacketPreListener(Consumer<PacketEvent.Outbound.Pre> listener) {
80+
public static void addOutboundPacketPreListener(Listener<PacketEvent.Outbound.Pre> listener) {
8181
outboundPacketPreListeners.add(listener);
8282
}
8383

@@ -99,7 +99,7 @@ public static Packet<?> firePacketOutboundPre(NetworkManager netManager, Packet<
9999

100100
private static Listeners<PacketEvent.Outbound.Post> outboundPacketPostListeners = new Listeners<>();
101101

102-
public static void addOutboundPacketPostListener(Consumer<PacketEvent.Outbound.Post> listener) {
102+
public static void addOutboundPacketPostListener(Listener<PacketEvent.Outbound.Post> listener) {
103103
outboundPacketPostListeners.add(listener);
104104
}
105105

@@ -118,7 +118,7 @@ public static void firePacketOutboundPost(NetworkManager netManager, Packet<?> p
118118

119119
private static Listeners<PacketEvent.Inbound.Pre> inboundPacketPreListeners = new Listeners<>();
120120

121-
public static void addInboundPacketPreListener(Consumer<PacketEvent.Inbound.Pre> listener) {
121+
public static void addInboundPacketPreListener(Listener<PacketEvent.Inbound.Pre> listener) {
122122
inboundPacketPreListeners.add(listener);
123123
}
124124

@@ -146,7 +146,7 @@ public static Packet<?> firePacketInboundPre(NetworkManager netManager, Packet<?
146146

147147
private static Listeners<PacketEvent.Inbound.Post> inboundPacketPostListeners = new Listeners<>();
148148

149-
public static void addInboundPacketPostListener(Consumer<PacketEvent.Inbound.Post> listener) {
149+
public static void addInboundPacketPostListener(Listener<PacketEvent.Inbound.Post> listener) {
150150
inboundPacketPostListeners.add(listener);
151151
}
152152

@@ -172,7 +172,7 @@ public static void firePacketInboundPost(NetworkManager netManager, Packet<?> pa
172172

173173
private static Listeners<GuiOpenEvent> guiOpenListeners = new Listeners<>();
174174

175-
public static void addGuiOpenListener(Consumer<GuiOpenEvent> listener) {
175+
public static void addGuiOpenListener(Listener<GuiOpenEvent> listener) {
176176
guiOpenListeners.add(listener);
177177
}
178178

@@ -185,7 +185,7 @@ public void onGuiOpen(GuiOpenEvent e) {
185185

186186
private static Listeners<InitGuiEvent.Post> initGuiListeners = new Listeners<>();
187187

188-
public static void addInitGuiListener(Consumer<InitGuiEvent.Post> listener) {
188+
public static void addInitGuiListener(Listener<InitGuiEvent.Post> listener) {
189189
initGuiListeners.add(listener);
190190
}
191191

@@ -198,7 +198,7 @@ public void onInitGui(InitGuiEvent.Post e) {
198198

199199
private static Listeners<ActionPerformedEvent.Pre> guiActionPerformedListeners = new Listeners<>();
200200

201-
public static void addGuiActionPerformedListener(Consumer<ActionPerformedEvent.Pre> listener) {
201+
public static void addGuiActionPerformedListener(Listener<ActionPerformedEvent.Pre> listener) {
202202
guiActionPerformedListeners.add(listener);
203203
}
204204

@@ -211,7 +211,7 @@ public void onGuiActionPerformed(ActionPerformedEvent.Pre e) {
211211

212212
private static Listeners<DrawScreenEvent.Post> guiOverlayListeners = new Listeners<>();
213213

214-
public static void addGuiOverlayListener(Consumer<DrawScreenEvent.Post> listener) {
214+
public static void addGuiOverlayListener(Listener<DrawScreenEvent.Post> listener) {
215215
guiOverlayListeners.add(listener);
216216
}
217217

@@ -224,7 +224,7 @@ public void onGuiOverlay(DrawScreenEvent.Post e) {
224224

225225
private static Listeners<ClientChatEvent> chatSentListeners = new Listeners<>();
226226

227-
public static void addChatSentListener(Consumer<ClientChatEvent> listener) {
227+
public static void addChatSentListener(Listener<ClientChatEvent> listener) {
228228
chatSentListeners.add(listener);
229229
}
230230

@@ -237,7 +237,7 @@ public void onChatSent(ClientChatEvent e) {
237237

238238
private static Listeners<ClientChatReceivedEvent> chatReceivedListeners = new Listeners<>();
239239

240-
public static void addChatReceivedListener(Consumer<ClientChatReceivedEvent> listener) {
240+
public static void addChatReceivedListener(Listener<ClientChatReceivedEvent> listener) {
241241
chatReceivedListeners.add(listener);
242242
}
243243

@@ -250,7 +250,7 @@ public void onChatReceived(ClientChatReceivedEvent e) {
250250

251251
private static Listeners<EntityConstructing> entityConstructingListeners = new Listeners<>();
252252

253-
public static void addEntityConstructingListener(Consumer<EntityConstructing> listener) {
253+
public static void addEntityConstructingListener(Listener<EntityConstructing> listener) {
254254
entityConstructingListeners.add(listener);
255255
}
256256

@@ -263,7 +263,7 @@ public void onEntityConstructing(EntityConstructing e) {
263263

264264
private static Listeners<EntityJoinWorldEvent> entitySpawnListeners = new Listeners<>();
265265

266-
public static void addEntitySpawnListener(Consumer<EntityJoinWorldEvent> listener) {
266+
public static void addEntitySpawnListener(Listener<EntityJoinWorldEvent> listener) {
267267
entitySpawnListeners.add(listener);
268268
}
269269

@@ -278,7 +278,7 @@ public void onEntitySpawn(EntityJoinWorldEvent e) {
278278

279279
private static Listeners<PlayerTickEvent> playerTickListeners = new Listeners<>();
280280

281-
public static void addPlayerTickListener(Consumer<PlayerTickEvent> listener) {
281+
public static void addPlayerTickListener(Listener<PlayerTickEvent> listener) {
282282
playerTickListeners.add(listener);
283283
}
284284

@@ -293,7 +293,7 @@ public void onPlayerTick(PlayerTickEvent e) {
293293

294294
private static Listeners<LivingAttackEvent> livingAttackListeners = new Listeners<>();
295295

296-
public static void addLivingAttackListener(Consumer<LivingAttackEvent> listener) {
296+
public static void addLivingAttackListener(Listener<LivingAttackEvent> listener) {
297297
livingAttackListeners.add(listener);
298298
}
299299

@@ -306,7 +306,7 @@ public void onLivingAttack(LivingAttackEvent e) {
306306

307307
private static Listeners<AnvilRepairEvent> anvilRepairListeners = new Listeners<>();
308308

309-
public static void addAnvilRepairListener(Consumer<AnvilRepairEvent> listener) {
309+
public static void addAnvilRepairListener(Listener<AnvilRepairEvent> listener) {
310310
anvilRepairListeners.add(listener);
311311
}
312312

@@ -321,7 +321,7 @@ public void onAnvilRepair(AnvilRepairEvent e) {
321321

322322
private static Listeners<LeftClickBlock> attackBlockListeners = new Listeners<>();
323323

324-
public static void addAttackBlockListener(Consumer<LeftClickBlock> listener) {
324+
public static void addAttackBlockListener(Listener<LeftClickBlock> listener) {
325325
attackBlockListeners.add(listener);
326326
}
327327

@@ -336,7 +336,7 @@ public void onAttackBlock(LeftClickBlock e) {
336336

337337
private static Listeners<AttackEntityEvent> attackEntityListeners = new Listeners<>();
338338

339-
public static void addAttackEntityListener(Consumer<AttackEntityEvent> listener) {
339+
public static void addAttackEntityListener(Listener<AttackEntityEvent> listener) {
340340
attackEntityListeners.add(listener);
341341
}
342342

@@ -351,7 +351,7 @@ public void onAttackEntity(AttackEntityEvent e) {
351351

352352
private static Listeners<RightClickBlock> useBlockListeners = new Listeners<>();
353353

354-
public static void addUseBlockListener(Consumer<RightClickBlock> listener) {
354+
public static void addUseBlockListener(Listener<RightClickBlock> listener) {
355355
useBlockListeners.add(listener);
356356
}
357357

@@ -366,7 +366,7 @@ public void onUseBlock(RightClickBlock e) {
366366

367367
private static Listeners<RightClickItem> useItemListeners = new Listeners<>();
368368

369-
public static void addUseItemListener(Consumer<RightClickItem> listener) {
369+
public static void addUseItemListener(Listener<RightClickItem> listener) {
370370
useItemListeners.add(listener);
371371
}
372372

@@ -381,7 +381,7 @@ public void onUseItem(RightClickItem e) {
381381

382382
private static Listeners<LivingEntityUseItemEvent.Stop> stopUseItemListeners = new Listeners<>();
383383

384-
public static void addStopUseItemListener(Consumer<LivingEntityUseItemEvent.Stop> listener) {
384+
public static void addStopUseItemListener(Listener<LivingEntityUseItemEvent.Stop> listener) {
385385
stopUseItemListeners.add(listener);
386386
}
387387

@@ -396,7 +396,7 @@ public void onStopUseItem(LivingEntityUseItemEvent.Stop e) {
396396

397397
private static Listeners<EntityInteract> useEntityListeners = new Listeners<>();
398398

399-
public static void addUseEntityListener(Consumer<EntityInteract> listener) {
399+
public static void addUseEntityListener(Listener<EntityInteract> listener) {
400400
useEntityListeners.add(listener);
401401
}
402402

@@ -411,7 +411,7 @@ public void onUseEntity(EntityInteract e) {
411411

412412
private static Listeners<ArrowLooseEvent> fireBowListeners = new Listeners<>();
413413

414-
public static void addFireBowListener(Consumer<ArrowLooseEvent> listener) {
414+
public static void addFireBowListener(Listener<ArrowLooseEvent> listener) {
415415
fireBowListeners.add(listener);
416416
}
417417

@@ -426,7 +426,7 @@ public void onFireBow(ArrowLooseEvent e) {
426426

427427
private static Listeners<ToolDamagedEvent.Pre> preDamageItemListeners = new Listeners<>();
428428

429-
public static void addPreDamageItemListener(Consumer<ToolDamagedEvent.Pre> listener) {
429+
public static void addPreDamageItemListener(Listener<ToolDamagedEvent.Pre> listener) {
430430
preDamageItemListeners.add(listener);
431431
}
432432

@@ -439,7 +439,7 @@ public void onPreDamageItem(ToolDamagedEvent.Pre e) {
439439

440440
private static Listeners<ToolDamagedEvent.Post> postDamageItemListeners = new Listeners<>();
441441

442-
public static void addPostDamageItemListener(Consumer<ToolDamagedEvent.Post> listener) {
442+
public static void addPostDamageItemListener(Listener<ToolDamagedEvent.Post> listener) {
443443
postDamageItemListeners.add(listener);
444444
}
445445

@@ -450,15 +450,41 @@ public void onPostDamageItem(ToolDamagedEvent.Post e) {
450450

451451
// IMPLEMENTATION
452452

453+
@FunctionalInterface
454+
public static interface Listener<E extends Event> {
455+
void accept(E e);
456+
457+
default boolean isOneTime() {
458+
return false;
459+
}
460+
}
461+
453462
private static class Listeners<E extends Event> {
454-
private List<Consumer<E>> listeners = new ArrayList<>();
463+
private List<Listener<E>> listeners = new ArrayList<>();
464+
private List<Listener<E>> toAdd = new ArrayList<>();
465+
private boolean invoking = false;
455466

456467
public void invoke(E e) {
457-
listeners.forEach(l -> l.accept(e));
468+
invoking = true;
469+
Iterator<Listener<E>> itr = listeners.iterator();
470+
while (itr.hasNext()) {
471+
Listener<E> listener = itr.next();
472+
listener.accept(e);
473+
if (listener.isOneTime()) {
474+
itr.remove();
475+
}
476+
}
477+
invoking = false;
478+
listeners.addAll(toAdd);
479+
toAdd.clear();
458480
}
459481

460-
public void add(Consumer<E> listener) {
461-
listeners.add(listener);
482+
public void add(Listener<E> listener) {
483+
if (invoking) {
484+
toAdd.add(listener);
485+
} else {
486+
listeners.add(listener);
487+
}
462488
}
463489
}
464490

src/main/java/net/earthcomputer/clientcommands/core/ClientCommandsLoadingPlugin.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
11
package net.earthcomputer.clientcommands.core;
22

3+
import java.util.HashSet;
34
import java.util.Map;
5+
import java.util.Set;
46

57
import net.minecraftforge.fml.relauncher.IFMLLoadingPlugin;
68

79
public class ClientCommandsLoadingPlugin implements IFMLLoadingPlugin {
810

11+
static final Set<String> EXPECTED_TASKS = new HashSet<>();
12+
13+
static {
14+
EXPECTED_TASKS.add("loadCoreMod");
15+
}
16+
17+
public static Set<String> getExpectedTasks() {
18+
return EXPECTED_TASKS;
19+
}
20+
921
@Override
1022
public String[] getASMTransformerClass() {
23+
EXPECTED_TASKS.remove("loadCoreMod");
1124
return new String[] { ProxyTransformer.class.getName(), NetHandlerTransformer.class.getName(),
1225
IntegratedServerRaceFixTransformer.class.getName() };
1326
}

src/main/java/net/earthcomputer/clientcommands/core/IntegratedServerRaceFixTransformer.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313

1414
public class IntegratedServerRaceFixTransformer implements IClassTransformer {
1515

16+
static {
17+
ClientCommandsLoadingPlugin.EXPECTED_TASKS.add("integratedServerRaceFix");
18+
}
19+
1620
@Override
1721
public byte[] transform(String name, String transformedName, byte[] basicClass) {
1822
if (!"net.minecraft.network.NetHandlerPlayServer".equals(transformedName)) {
@@ -51,6 +55,7 @@ private void transformOnDisconnect(MethodNode method) {
5155
if ("x".equals(methodInsn.name) || "func_71263_m".equals(methodInsn.name)
5256
|| "initiateShutdown".equals(methodInsn.name)) {
5357
method.instructions.set(insn, insn = new InsnNode(Opcodes.POP));
58+
ClientCommandsLoadingPlugin.EXPECTED_TASKS.remove("integratedServerRaceFix");
5459
break;
5560
}
5661
}

0 commit comments

Comments
 (0)