Skip to content

Commit

Permalink
fix: Screen crash if kubejs is not present.
Browse files Browse the repository at this point in the history
  • Loading branch information
Senior-S committed Sep 17, 2024
1 parent a3b2543 commit 0808fed
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,21 @@ private static int execute(CommandContext<CommandSourceStack> 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<LockItem.Aptitude> 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));

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}

}

0 comments on commit 0808fed

Please sign in to comment.