mirror of
https://github.com/CitizensDev/Citizens2.git
synced 2025-02-17 04:41:31 +01:00
Ensure commands run on main thread
This commit is contained in:
parent
b26540619b
commit
d13530ff49
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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<Player> 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;
|
||||
|
Loading…
Reference in New Issue
Block a user