Skip to content

Commit 9b497ab

Browse files
fix(actions): player commands
1 parent d7416c5 commit 9b497ab

2 files changed

Lines changed: 54 additions & 35 deletions

File tree

src/main/java/ce/ajneb97/libs/FoliaAPI.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,29 @@ public static void runTask(Plugin pl, Consumer<Object> run) {
164164
invokeMethod(method, globalRegionScheduler, pl, run);
165165
}
166166

167+
public static void runEntityTask(Plugin plugin, Entity entity, Runnable task) {
168+
if (entity == null || !entity.isValid()) {
169+
plugin.getLogger().warning("Attempted to run task for null or invalid entity");
170+
return;
171+
}
172+
173+
if (isFolia()) {
174+
try {
175+
entity.getScheduler().run(plugin, scheduledTask -> task.run(), null);
176+
} catch (Exception e) {
177+
plugin.getLogger().severe("Failed to schedule Folia entity task: " + e.getMessage());
178+
e.printStackTrace();
179+
}
180+
} else {
181+
try {
182+
bS.runTask(plugin, task);
183+
} catch (Exception e) {
184+
plugin.getLogger().severe("Failed to schedule Bukkit task: " + e.getMessage());
185+
e.printStackTrace();
186+
}
187+
}
188+
}
189+
167190
public static void runTaskForEntity(Plugin pl, Entity entity, Runnable run, Runnable retired, long delay) {
168191
if (!isFolia()) {
169192
bS.runTaskLater(pl, run, delay);
@@ -241,15 +264,15 @@ public static CompletableFuture<Boolean> teleportAsync(Entity entity, Location l
241264
if (teleportMethod != null) {
242265
Object result = invokeMethod(teleportMethod, entity, location);
243266
if (result instanceof CompletableFuture) {
244-
return ((CompletableFuture<Boolean>) result).thenApply(success -> Boolean.TRUE.equals(success));
267+
return ((CompletableFuture<Boolean>) result).thenApply(Boolean.TRUE::equals);
245268
}
246269
return CompletableFuture.completedFuture(result != null);
247270
}
248271
if (entity instanceof Player) {
249272
Method playerTeleportMethod = cachedMethods.get("player.teleportAsync");
250273
Object result = invokeMethod(playerTeleportMethod, entity, location);
251274
if (result instanceof CompletableFuture) {
252-
return ((CompletableFuture<Boolean>) result).thenApply(success -> Boolean.TRUE.equals(success));
275+
return ((CompletableFuture<Boolean>) result).thenApply(Boolean.TRUE::equals);
253276
}
254277
return CompletableFuture.completedFuture(result != null);
255278
}

src/main/java/ce/ajneb97/utils/ActionUtils.java

Lines changed: 29 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -76,23 +76,23 @@ public static void consoleCommand(String actionLine){
7676
});
7777
}
7878

79-
public static void playerCommand(Player player,String actionLine){
80-
FoliaAPI.runTask(ConditionalEventsAPI.getPlugin(), () -> { player.performCommand(actionLine); });
79+
public static void playerCommand(Player player, String actionLine) {
80+
FoliaAPI.runEntityTask(ConditionalEventsAPI.getPlugin(), player, () -> player.performCommand(actionLine));
8181
}
8282

83-
public static void playerCommandAsOp(Player player,String actionLine){
84-
FoliaAPI.runTask(ConditionalEventsAPI.getPlugin(), () -> {
85-
boolean isOp = player.isOp();
86-
player.setOp(true);
87-
player.performCommand(actionLine);
88-
if(!isOp) {
89-
player.setOp(false);
90-
}
91-
});
83+
public static void playerCommandAsOp(Player player, String actionLine) {
84+
FoliaAPI.runEntityTask(ConditionalEventsAPI.getPlugin(), player, () -> {
85+
boolean isOp = player.isOp();
86+
player.setOp(true);
87+
player.performCommand(actionLine);
88+
if (!isOp) {
89+
player.setOp(false);
90+
}
91+
});
9292
}
9393

94-
public static void playerSendChat(Player player,String actionLine){
95-
FoliaAPI.runTask(ConditionalEventsAPI.getPlugin(), () -> { player.chat(MessagesManager.getColoredMessage(actionLine)); });
94+
public static void playerSendChat(Player player, String actionLine) {
95+
FoliaAPI.runEntityTask(ConditionalEventsAPI.getPlugin(), player, () -> player.chat(MessagesManager.getColoredMessage(actionLine)));
9696
}
9797

9898
public static void sendToServer(Player player,String actionLine,ConditionalEvents plugin){
@@ -145,14 +145,14 @@ public static void removeItem(Player player,String actionLine){
145145

146146
String[] sep = actionLine.split(";");
147147
String material = sep[0];
148-
int amount = Integer.valueOf(sep[1]);
148+
int amount = Integer.parseInt(sep[1]);
149149
short datavalue = 0;
150150
String name = null;
151151
String loreContainsLoreLine = null;
152152

153153
for(String sepLine : sep) {
154154
if(sepLine.startsWith("datavalue: ")) {
155-
datavalue = Short.valueOf(sepLine.replace("datavalue: ", ""));
155+
datavalue = Short.parseShort(sepLine.replace("datavalue: ", ""));
156156
}else if(sepLine.startsWith("name: ")) {
157157
name = sepLine.replace("name: ", "");
158158
}else if(sepLine.startsWith("lorecontains: ")) {
@@ -220,7 +220,7 @@ public static void givePotionEffect(LivingEntity livingEntity,String actionLine)
220220
int level = Integer.parseInt(sep[2])-1;
221221
boolean showParticles = true;
222222
if(sep.length >= 4) {
223-
showParticles = Boolean.valueOf(sep[3]);
223+
showParticles = Boolean.parseBoolean(sep[3]);
224224
}
225225
PotionEffect effect = new PotionEffect(potionEffectType,duration,level,false,showParticles);
226226
livingEntity.addPotionEffect(effect);
@@ -238,7 +238,7 @@ public static void removePotionEffect(LivingEntity livingEntity,String actionLin
238238
}
239239

240240
public static void cancelEvent(String actionLine,Event minecraftEvent){
241-
boolean cancel = Boolean.valueOf(actionLine);
241+
boolean cancel = Boolean.parseBoolean(actionLine);
242242
if(minecraftEvent != null && minecraftEvent instanceof Cancellable) {
243243
Cancellable cancellableEvent = (Cancellable) minecraftEvent;
244244
cancellableEvent.setCancelled(cancel);
@@ -252,9 +252,9 @@ public static void kick(Player player,String actionLine){
252252
public static void playSound(LivingEntity livingEntity,String actionLine){
253253
// playsound: sound;volume;pitch;(optional)<x>,<y>,<z>,<world>
254254
String[] sep = actionLine.split(";");
255-
Sound sound = null;
256-
float volume = 0;
257-
float pitch = 0;
255+
Sound sound;
256+
float volume;
257+
float pitch;
258258
try {
259259
sound = getSoundByName(sep[0]);
260260
volume = Float.parseFloat(sep[1]);
@@ -431,7 +431,7 @@ public static void setItem(String actionLine,Event minecraftEvent){
431431
if(minecraftEvent instanceof PlayerFishEvent){
432432
PlayerFishEvent event = (PlayerFishEvent) minecraftEvent;
433433
Entity caught = event.getCaught();
434-
if(caught != null && caught instanceof Item){
434+
if(caught instanceof Item){
435435
Item item = (Item) caught;
436436
item.setItemStack(ItemUtils.getItemFromProperties(sep,null));
437437
}
@@ -610,15 +610,15 @@ public static void summon(String actionLine) {
610610
public static void actionbar(Player player,String actionLine,ConditionalEvents plugin){
611611
String[] sep = actionLine.split(";");
612612
String text = sep[0];
613-
int duration = Integer.valueOf(sep[1]);
613+
int duration = Integer.parseInt(sep[1]);
614614
ActionBarAPI.sendActionBar(player,text,duration,plugin);
615615
}
616616

617617
public static void title(Player player,String actionLine){
618618
String[] sep = actionLine.split(";");
619-
int fadeIn = Integer.valueOf(sep[0]);
620-
int stay = Integer.valueOf(sep[1]);
621-
int fadeOut = Integer.valueOf(sep[2]);
619+
int fadeIn = Integer.parseInt(sep[0]);
620+
int stay = Integer.parseInt(sep[1]);
621+
int fadeOut = Integer.parseInt(sep[2]);
622622

623623
String title = sep[3];
624624
String subtitle = sep[4];
@@ -850,11 +850,7 @@ public static void heal(LivingEntity livingEntity,String actionLine){
850850
double maxHealth = livingEntity.getMaxHealth();
851851
double currentHealth = livingEntity.getHealth();
852852
double newHealth = currentHealth+Double.parseDouble(actionLine);
853-
if(newHealth >= maxHealth){
854-
livingEntity.setHealth(maxHealth);
855-
}else{
856-
livingEntity.setHealth(newHealth);
857-
}
853+
livingEntity.setHealth(Math.min(newHealth, maxHealth));
858854
}
859855

860856
public static void setFoodLevel(Player player,String actionLine){
@@ -1013,7 +1009,7 @@ public static boolean callEvent(String actionLine,LivingEntity livingEntity,Cond
10131009
// call_event: <event>;%variable1%=<value1>;%variable2%=<value2>
10141010
// call_event: <event>;%variable1%=<value1>;%variable2%=<value2>;already_stored
10151011
ArrayList<StoredVariable> variables = new ArrayList<>();
1016-
String eventName = null;
1012+
String eventName;
10171013
try{
10181014
String[] sep = actionLine.split(";");
10191015
eventName = sep[0];
@@ -1046,8 +1042,8 @@ public static boolean callEvent(String actionLine,LivingEntity livingEntity,Cond
10461042

10471043
public static void executeActionGroup(String actionLine,ExecutedEvent executedEvent,ConditionalEvents plugin){
10481044
// execute_action_group: <group1>:<prob1>;<group2>:<prob2>
1049-
ArrayList<String> actionGroups = new ArrayList<String>();
1050-
ArrayList<Integer> probs = new ArrayList<Integer>();
1045+
ArrayList<String> actionGroups = new ArrayList<>();
1046+
ArrayList<Integer> probs = new ArrayList<>();
10511047

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

0 commit comments

Comments
 (0)