From d13530ff492363fb5385e686f9ea6878fcb505cd Mon Sep 17 00:00:00 2001 From: fullwall Date: Thu, 16 Apr 2020 03:04:42 +0800 Subject: [PATCH] Ensure commands run on main thread --- .../net/citizensnpcs/trait/CommandTrait.java | 33 ++++++++++++------- .../net/citizensnpcs/trait/LookClose.java | 25 +++++++------- 2 files changed, 33 insertions(+), 25 deletions(-) diff --git a/main/src/main/java/net/citizensnpcs/trait/CommandTrait.java b/main/src/main/java/net/citizensnpcs/trait/CommandTrait.java index dc0704bd9..2260f89fd 100644 --- a/main/src/main/java/net/citizensnpcs/trait/CommandTrait.java +++ b/main/src/main/java/net/citizensnpcs/trait/CommandTrait.java @@ -10,6 +10,7 @@ import org.bukkit.entity.Player; import com.google.common.collect.Lists; import com.google.common.collect.Maps; +import net.citizensnpcs.api.CitizensAPI; import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.api.persistence.DelegatePersistence; import net.citizensnpcs.api.persistence.Persist; @@ -87,18 +88,28 @@ public class CommandTrait extends Trait { return output; } - public void dispatch(Player player, Hand hand) { - for (NPCCommand command : commands.values()) { - if (command.hand != hand && command.hand != Hand.BOTH) - continue; - PlayerNPCCommand info = cooldowns.get(player.getUniqueId().toString()); - if (info != null && !info.canUse(player, command)) { - continue; - } - command.run(npc, player); - if (command.cooldown > 0 && info == null) { - cooldowns.put(player.getUniqueId().toString(), new PlayerNPCCommand(command)); + public void dispatch(final Player player, final Hand hand) { + Runnable task = new Runnable() { + @Override + public void run() { + for (NPCCommand command : commands.values()) { + if (command.hand != hand && command.hand != Hand.BOTH) + continue; + PlayerNPCCommand info = cooldowns.get(player.getUniqueId().toString()); + if (info != null && !info.canUse(player, command)) { + continue; + } + command.run(npc, player); + if (command.cooldown > 0 && info == null) { + cooldowns.put(player.getUniqueId().toString(), new PlayerNPCCommand(command)); + } + } } + }; + if (Bukkit.isPrimaryThread()) { + task.run(); + } else { + Bukkit.getScheduler().runTask(CitizensAPI.getPlugin(), task); } } diff --git a/main/src/main/java/net/citizensnpcs/trait/LookClose.java b/main/src/main/java/net/citizensnpcs/trait/LookClose.java index c15432146..7d4f728a8 100644 --- a/main/src/main/java/net/citizensnpcs/trait/LookClose.java +++ b/main/src/main/java/net/citizensnpcs/trait/LookClose.java @@ -1,11 +1,12 @@ package net.citizensnpcs.trait; -import java.util.*; +import java.util.ArrayList; +import java.util.List; +import java.util.Random; import org.bukkit.GameMode; import org.bukkit.Location; import org.bukkit.entity.Entity; -import org.bukkit.entity.EntityType; import org.bukkit.entity.LivingEntity; import org.bukkit.entity.Player; import org.bukkit.metadata.MetadataValue; @@ -49,8 +50,8 @@ public class LookClose extends Trait implements Toggleable, CommandConfigurable } /** - * Returns whether the target can be seen. Will use realistic line of sight if - * {@link #setRealisticLooking(boolean)} is true. + * Returns whether the target can be seen. Will use realistic line of sight if {@link #setRealisticLooking(boolean)} + * is true. */ public boolean canSeeTarget() { return realisticLooking && npc.getEntity() instanceof LivingEntity @@ -70,14 +71,13 @@ public class LookClose extends Trait implements Toggleable, CommandConfigurable public void findNewTarget() { List nearby = new ArrayList<>(); for (Entity entity : npc.getEntity().getNearbyEntities(range, range, range)) { - if (!(entity instanceof Player)) continue; + if (!(entity instanceof Player)) + continue; Player player = (Player) entity; - if (CitizensAPI.getNPCRegistry().getNPC(entity) != null - || player.getGameMode() == GameMode.SPECTATOR + if (CitizensAPI.getNPCRegistry().getNPC(entity) != null || player.getGameMode() == GameMode.SPECTATOR || entity.getLocation(CACHE_LOCATION).getWorld() != NPC_LOCATION.getWorld() - || player.hasPotionEffect(PotionEffectType.INVISIBILITY) - || isPluginVanished((Player) entity)) + || player.hasPotionEffect(PotionEffectType.INVISIBILITY) || isPluginVanished((Player) entity)) continue; nearby.add(player); } @@ -92,7 +92,6 @@ public class LookClose extends Trait implements Toggleable, CommandConfigurable return Double.compare(l1.distanceSquared(NPC_LOCATION), l2.distanceSquared(NPC_LOCATION)); }); - lookingAt = nearby.get(0); } } @@ -181,8 +180,7 @@ public class LookClose extends Trait implements Toggleable, CommandConfigurable } /** - * Enables random looking - will look at a random {@link Location} every so - * often if enabled. + * Enables random looking - will look at a random {@link Location} every so often if enabled. */ public void setRandomLook(boolean enableRandomLook) { this.enableRandomLook = enableRandomLook; @@ -211,8 +209,7 @@ public class LookClose extends Trait implements Toggleable, CommandConfigurable } /** - * Enables/disables realistic looking (using line of sight checks). More - * computationally expensive. + * Enables/disables realistic looking (using line of sight checks). More computationally expensive. */ public void setRealisticLooking(boolean realistic) { this.realisticLooking = realistic;