Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions src/main/java/ce/ajneb97/libs/FoliaAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,29 @@ public static void runTask(Plugin pl, Consumer<Object> 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);
Expand Down Expand Up @@ -241,15 +264,15 @@ public static CompletableFuture<Boolean> teleportAsync(Entity entity, Location l
if (teleportMethod != null) {
Object result = invokeMethod(teleportMethod, entity, location);
if (result instanceof CompletableFuture) {
return ((CompletableFuture<Boolean>) result).thenApply(success -> Boolean.TRUE.equals(success));
return ((CompletableFuture<Boolean>) result).thenApply(Boolean.TRUE::equals);
}
return CompletableFuture.completedFuture(result != null);
}
if (entity instanceof Player) {
Method playerTeleportMethod = cachedMethods.get("player.teleportAsync");
Object result = invokeMethod(playerTeleportMethod, entity, location);
if (result instanceof CompletableFuture) {
return ((CompletableFuture<Boolean>) result).thenApply(success -> Boolean.TRUE.equals(success));
return ((CompletableFuture<Boolean>) result).thenApply(Boolean.TRUE::equals);
}
return CompletableFuture.completedFuture(result != null);
}
Expand Down
62 changes: 29 additions & 33 deletions src/main/java/ce/ajneb97/utils/ActionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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){
Expand Down Expand Up @@ -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: ")) {
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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)<x>,<y>,<z>,<world>
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]);
Expand Down Expand Up @@ -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));
}
Expand Down Expand Up @@ -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];
Expand Down Expand Up @@ -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){
Expand Down Expand Up @@ -1013,7 +1009,7 @@ public static boolean callEvent(String actionLine,LivingEntity livingEntity,Cond
// call_event: <event>;%variable1%=<value1>;%variable2%=<value2>
// call_event: <event>;%variable1%=<value1>;%variable2%=<value2>;already_stored
ArrayList<StoredVariable> variables = new ArrayList<>();
String eventName = null;
String eventName;
try{
String[] sep = actionLine.split(";");
eventName = sep[0];
Expand Down Expand Up @@ -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: <group1>:<prob1>;<group2>:<prob2>
ArrayList<String> actionGroups = new ArrayList<String>();
ArrayList<Integer> probs = new ArrayList<Integer>();
ArrayList<String> actionGroups = new ArrayList<>();
ArrayList<Integer> probs = new ArrayList<>();

String[] sep = actionLine.split(";");
for(String line : sep){
Expand Down