From 0808fed62ca4faaccab104ed2d886d38f76a5e97 Mon Sep 17 00:00:00 2001 From: Senior-S Date: Tue, 17 Sep 2024 15:35:37 -0300 Subject: [PATCH] fix: Screen crash if kubejs is not present. --- .../client/screen/JustLevelingScreen.java | 2 +- .../common/command/RegisterItem.java | 15 +++++++++++ .../integration/KubeJSIntegration.java | 27 ++++++++++++++----- 3 files changed, 37 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/seniors/justlevelingfork/client/screen/JustLevelingScreen.java b/src/main/java/com/seniors/justlevelingfork/client/screen/JustLevelingScreen.java index 051890d..5a667ab 100644 --- a/src/main/java/com/seniors/justlevelingfork/client/screen/JustLevelingScreen.java +++ b/src/main/java/com/seniors/justlevelingfork/client/screen/JustLevelingScreen.java @@ -451,7 +451,7 @@ public void drawSkills(GuiGraphics matrixStack, int x, int y, int mouseX, int mo if (this.checkMouse) { Utils.playSound(); if (KubeJSIntegration.isModLoaded()) { - boolean cancelled = KubeJSIntegration.postLevelUpEvent(client.player, aptitude); + boolean cancelled = new KubeJSIntegration().postLevelUpEvent(client.player, aptitude); if (!cancelled) { AptitudeLevelUpSP.send(aptitude); diff --git a/src/main/java/com/seniors/justlevelingfork/common/command/RegisterItem.java b/src/main/java/com/seniors/justlevelingfork/common/command/RegisterItem.java index 91a95e7..8091bc9 100644 --- a/src/main/java/com/seniors/justlevelingfork/common/command/RegisterItem.java +++ b/src/main/java/com/seniors/justlevelingfork/common/command/RegisterItem.java @@ -47,6 +47,21 @@ private static int execute(CommandContext command) throws Co if (optionalLockItem.isPresent()) { LockItem lockItem = optionalLockItem.get(); int index = HandlerLockItemsConfig.HANDLER.instance().lockItemList.indexOf(lockItem); + if (level < 1) { + if (lockItem.Aptitudes.size() <= 1) { + HandlerLockItemsConfig.HANDLER.instance().lockItemList.remove(index); + player.sendSystemMessage(Component.literal("Removing item from lockItemList...")); + } + else { + Optional aptitude = lockItem.Aptitudes.stream().filter(c -> c.Aptitude.toString().equalsIgnoreCase(aptitudeName)).findFirst(); + aptitude.ifPresent(value -> lockItem.Aptitudes.remove(value)); + + HandlerLockItemsConfig.HANDLER.instance().lockItemList.set(index, lockItem); + player.sendSystemMessage(Component.literal("Removing aptitude from item...")); + } + + return Command.SINGLE_SUCCESS; + } lockItem.Aptitudes.stream().filter(c -> c.Aptitude.toString().equalsIgnoreCase(aptitudeName)).findFirst().ifPresent(value -> lockItem.Aptitudes.remove(value)); diff --git a/src/main/java/com/seniors/justlevelingfork/integration/KubeJSIntegration.java b/src/main/java/com/seniors/justlevelingfork/integration/KubeJSIntegration.java index b2fe7e3..661ab14 100644 --- a/src/main/java/com/seniors/justlevelingfork/integration/KubeJSIntegration.java +++ b/src/main/java/com/seniors/justlevelingfork/integration/KubeJSIntegration.java @@ -1,21 +1,36 @@ package com.seniors.justlevelingfork.integration; -import com.seniors.justlevelingfork.kubejs.events.CustomEvents; -import com.seniors.justlevelingfork.kubejs.events.LevelUpEvent; import com.seniors.justlevelingfork.registry.aptitude.Aptitude; import net.minecraft.world.entity.player.Player; import net.minecraftforge.fml.ModList; +import java.lang.reflect.Method; + public class KubeJSIntegration { public static boolean isModLoaded() { return ModList.get().isLoaded("kubejs"); } - public static boolean postLevelUpEvent(Player player, Aptitude aptitude) { - LevelUpEvent event = new LevelUpEvent(player, aptitude); - CustomEvents.APTITUDE_LEVELUP.post(event); - return event.getCancelled(); + public boolean postLevelUpEvent(Player player, Aptitude aptitude) { + + // Required in case KubeJS is not present + // In a future I should move this into a different mod + try { + Class eventClass = Class.forName("com.seniors.justlevelingfork.kubejs.events.LevelUpEvent"); + Object eventInstance = eventClass.getConstructor(Player.class, Aptitude.class).newInstance(player, aptitude); + + Class customEventsClass = Class.forName("com.seniors.justlevelingfork.kubejs.events.CustomEvents"); + Object aptitudeLevelUpField = customEventsClass.getField("APTITUDE_LEVELUP").get(null); + Method postMethod = aptitudeLevelUpField.getClass().getMethod("post", Class.forName("dev.latvian.mods.kubejs.event.EventJS")); + + postMethod.invoke(aptitudeLevelUpField, eventInstance); + + return (boolean) eventClass.getMethod("getCancelled").invoke(eventInstance); + + } catch (Exception e) { + return false; + } } }