diff --git a/src/main/java/ce/ajneb97/libs/FoliaAPI.java b/src/main/java/ce/ajneb97/libs/FoliaAPI.java index 2ba37cc..90b1b64 100644 --- a/src/main/java/ce/ajneb97/libs/FoliaAPI.java +++ b/src/main/java/ce/ajneb97/libs/FoliaAPI.java @@ -164,6 +164,29 @@ public static void runTask(Plugin pl, Consumer run) { invokeMethod(method, globalRegionScheduler, pl, run); } + public static void runEntityTask(Plugin plugin, Entity entity, Runnable task) { + if (entity == null || !entity.isValid()) { + plugin.getLogger().warning("Attempted to run task for null or invalid entity"); + return; + } + + if (isFolia()) { + try { + entity.getScheduler().run(plugin, scheduledTask -> task.run(), null); + } catch (Exception e) { + plugin.getLogger().severe("Failed to schedule Folia entity task: " + e.getMessage()); + e.printStackTrace(); + } + } else { + try { + bS.runTask(plugin, task); + } catch (Exception e) { + plugin.getLogger().severe("Failed to schedule Bukkit task: " + e.getMessage()); + e.printStackTrace(); + } + } + } + public static void runTaskForEntity(Plugin pl, Entity entity, Runnable run, Runnable retired, long delay) { if (!isFolia()) { bS.runTaskLater(pl, run, delay); @@ -241,7 +264,7 @@ public static CompletableFuture teleportAsync(Entity entity, Location l if (teleportMethod != null) { Object result = invokeMethod(teleportMethod, entity, location); if (result instanceof CompletableFuture) { - return ((CompletableFuture) result).thenApply(success -> Boolean.TRUE.equals(success)); + return ((CompletableFuture) result).thenApply(Boolean.TRUE::equals); } return CompletableFuture.completedFuture(result != null); } @@ -249,7 +272,7 @@ public static CompletableFuture teleportAsync(Entity entity, Location l Method playerTeleportMethod = cachedMethods.get("player.teleportAsync"); Object result = invokeMethod(playerTeleportMethod, entity, location); if (result instanceof CompletableFuture) { - return ((CompletableFuture) result).thenApply(success -> Boolean.TRUE.equals(success)); + return ((CompletableFuture) result).thenApply(Boolean.TRUE::equals); } return CompletableFuture.completedFuture(result != null); } diff --git a/src/main/java/ce/ajneb97/utils/ActionUtils.java b/src/main/java/ce/ajneb97/utils/ActionUtils.java index 6bc282c..a406472 100644 --- a/src/main/java/ce/ajneb97/utils/ActionUtils.java +++ b/src/main/java/ce/ajneb97/utils/ActionUtils.java @@ -76,23 +76,23 @@ public static void consoleCommand(String actionLine){ }); } - public static void playerCommand(Player player,String actionLine){ - FoliaAPI.runTask(ConditionalEventsAPI.getPlugin(), () -> { player.performCommand(actionLine); }); + public static void playerCommand(Player player, String actionLine) { + FoliaAPI.runEntityTask(ConditionalEventsAPI.getPlugin(), player, () -> player.performCommand(actionLine)); } - public static void playerCommandAsOp(Player player,String actionLine){ - FoliaAPI.runTask(ConditionalEventsAPI.getPlugin(), () -> { - boolean isOp = player.isOp(); - player.setOp(true); - player.performCommand(actionLine); - if(!isOp) { - player.setOp(false); - } - }); + public static void playerCommandAsOp(Player player, String actionLine) { + FoliaAPI.runEntityTask(ConditionalEventsAPI.getPlugin(), player, () -> { + boolean isOp = player.isOp(); + player.setOp(true); + player.performCommand(actionLine); + if (!isOp) { + player.setOp(false); + } + }); } - public static void playerSendChat(Player player,String actionLine){ - FoliaAPI.runTask(ConditionalEventsAPI.getPlugin(), () -> { player.chat(MessagesManager.getColoredMessage(actionLine)); }); + public static void playerSendChat(Player player, String actionLine) { + FoliaAPI.runEntityTask(ConditionalEventsAPI.getPlugin(), player, () -> player.chat(MessagesManager.getColoredMessage(actionLine))); } public static void sendToServer(Player player,String actionLine,ConditionalEvents plugin){ @@ -145,14 +145,14 @@ public static void removeItem(Player player,String actionLine){ String[] sep = actionLine.split(";"); String material = sep[0]; - int amount = Integer.valueOf(sep[1]); + int amount = Integer.parseInt(sep[1]); short datavalue = 0; String name = null; String loreContainsLoreLine = null; for(String sepLine : sep) { if(sepLine.startsWith("datavalue: ")) { - datavalue = Short.valueOf(sepLine.replace("datavalue: ", "")); + datavalue = Short.parseShort(sepLine.replace("datavalue: ", "")); }else if(sepLine.startsWith("name: ")) { name = sepLine.replace("name: ", ""); }else if(sepLine.startsWith("lorecontains: ")) { @@ -220,7 +220,7 @@ public static void givePotionEffect(LivingEntity livingEntity,String actionLine) int level = Integer.parseInt(sep[2])-1; boolean showParticles = true; if(sep.length >= 4) { - showParticles = Boolean.valueOf(sep[3]); + showParticles = Boolean.parseBoolean(sep[3]); } PotionEffect effect = new PotionEffect(potionEffectType,duration,level,false,showParticles); livingEntity.addPotionEffect(effect); @@ -238,7 +238,7 @@ public static void removePotionEffect(LivingEntity livingEntity,String actionLin } public static void cancelEvent(String actionLine,Event minecraftEvent){ - boolean cancel = Boolean.valueOf(actionLine); + boolean cancel = Boolean.parseBoolean(actionLine); if(minecraftEvent != null && minecraftEvent instanceof Cancellable) { Cancellable cancellableEvent = (Cancellable) minecraftEvent; cancellableEvent.setCancelled(cancel); @@ -252,9 +252,9 @@ public static void kick(Player player,String actionLine){ public static void playSound(LivingEntity livingEntity,String actionLine){ // playsound: sound;volume;pitch;(optional),,, String[] sep = actionLine.split(";"); - Sound sound = null; - float volume = 0; - float pitch = 0; + Sound sound; + float volume; + float pitch; try { sound = getSoundByName(sep[0]); volume = Float.parseFloat(sep[1]); @@ -431,7 +431,7 @@ public static void setItem(String actionLine,Event minecraftEvent){ if(minecraftEvent instanceof PlayerFishEvent){ PlayerFishEvent event = (PlayerFishEvent) minecraftEvent; Entity caught = event.getCaught(); - if(caught != null && caught instanceof Item){ + if(caught instanceof Item){ Item item = (Item) caught; item.setItemStack(ItemUtils.getItemFromProperties(sep,null)); } @@ -610,15 +610,15 @@ public static void summon(String actionLine) { public static void actionbar(Player player,String actionLine,ConditionalEvents plugin){ String[] sep = actionLine.split(";"); String text = sep[0]; - int duration = Integer.valueOf(sep[1]); + int duration = Integer.parseInt(sep[1]); ActionBarAPI.sendActionBar(player,text,duration,plugin); } public static void title(Player player,String actionLine){ String[] sep = actionLine.split(";"); - int fadeIn = Integer.valueOf(sep[0]); - int stay = Integer.valueOf(sep[1]); - int fadeOut = Integer.valueOf(sep[2]); + int fadeIn = Integer.parseInt(sep[0]); + int stay = Integer.parseInt(sep[1]); + int fadeOut = Integer.parseInt(sep[2]); String title = sep[3]; String subtitle = sep[4]; @@ -850,11 +850,7 @@ public static void heal(LivingEntity livingEntity,String actionLine){ double maxHealth = livingEntity.getMaxHealth(); double currentHealth = livingEntity.getHealth(); double newHealth = currentHealth+Double.parseDouble(actionLine); - if(newHealth >= maxHealth){ - livingEntity.setHealth(maxHealth); - }else{ - livingEntity.setHealth(newHealth); - } + livingEntity.setHealth(Math.min(newHealth, maxHealth)); } public static void setFoodLevel(Player player,String actionLine){ @@ -1013,7 +1009,7 @@ public static boolean callEvent(String actionLine,LivingEntity livingEntity,Cond // call_event: ;%variable1%=;%variable2%= // call_event: ;%variable1%=;%variable2%=;already_stored ArrayList variables = new ArrayList<>(); - String eventName = null; + String eventName; try{ String[] sep = actionLine.split(";"); eventName = sep[0]; @@ -1046,8 +1042,8 @@ public static boolean callEvent(String actionLine,LivingEntity livingEntity,Cond public static void executeActionGroup(String actionLine,ExecutedEvent executedEvent,ConditionalEvents plugin){ // execute_action_group: :;: - ArrayList actionGroups = new ArrayList(); - ArrayList probs = new ArrayList(); + ArrayList actionGroups = new ArrayList<>(); + ArrayList probs = new ArrayList<>(); String[] sep = actionLine.split(";"); for(String line : sep){