From 81d6c2a3a31398a11faca137d7c7ebab73167e18 Mon Sep 17 00:00:00 2001 From: fullwall Date: Thu, 3 May 2012 23:22:31 +0800 Subject: [PATCH] Changes --- src/main/java/net/citizensnpcs/Citizens.java | 4 +- .../command/command/ScriptCommands.java | 12 +-- .../citizensnpcs/npc/CitizensNPCManager.java | 4 +- .../net/citizensnpcs/npc/ai/CitizensAI.java | 36 ++++----- .../npc/entity/CitizensHumanNPC.java | 2 +- .../net/citizensnpcs/trait/Behaviour.java | 77 +++++++++++-------- 6 files changed, 69 insertions(+), 66 deletions(-) diff --git a/src/main/java/net/citizensnpcs/Citizens.java b/src/main/java/net/citizensnpcs/Citizens.java index 683339904..7ac2c5b5f 100644 --- a/src/main/java/net/citizensnpcs/Citizens.java +++ b/src/main/java/net/citizensnpcs/Citizens.java @@ -11,7 +11,7 @@ import net.citizensnpcs.api.event.CitizensReloadEvent; import net.citizensnpcs.api.exception.NPCLoadException; import net.citizensnpcs.api.npc.NPC; import net.citizensnpcs.api.scripting.EventRegistrar; -import net.citizensnpcs.api.scripting.PluginProvider; +import net.citizensnpcs.api.scripting.ObjectProvider; import net.citizensnpcs.api.scripting.ScriptCompiler; import net.citizensnpcs.api.trait.TraitManager; import net.citizensnpcs.api.util.DataKey; @@ -239,7 +239,7 @@ public class Citizens extends JavaPlugin { setupScripting(); ScriptCompiler compiler = CitizensAPI.getScriptCompiler(); compiler.registerGlobalContextProvider(new EventRegistrar(this)); - compiler.registerGlobalContextProvider(new PluginProvider(this)); + compiler.registerGlobalContextProvider(new ObjectProvider("plugin", this)); } public void reload() throws NPCLoadException { diff --git a/src/main/java/net/citizensnpcs/command/command/ScriptCommands.java b/src/main/java/net/citizensnpcs/command/command/ScriptCommands.java index 0071f4591..3d6e22f7c 100644 --- a/src/main/java/net/citizensnpcs/command/command/ScriptCommands.java +++ b/src/main/java/net/citizensnpcs/command/command/ScriptCommands.java @@ -38,7 +38,7 @@ public class ScriptCommands { File file = new File(plugin.getDataFolder(), args.getString(1)); if (!file.exists()) throw new CommandException("The file '" + args.getString(1) + "' doesn't exist!"); - boolean success = CitizensAPI.getScriptCompiler().compile(file).withCallback(new CompileCallback() { + CitizensAPI.getScriptCompiler().compile(file).withCallback(new CompileCallback() { @Override public void onScriptCompiled(ScriptFactory script) { Script s = script.newInstance(); @@ -49,11 +49,11 @@ public class ScriptCommands { } Messaging.send(sender, "Done."); } + + @Override + public void onCompileTaskFinished() { + } }).begin(); - if (success) { - sender.sendMessage("Compiling..."); - } else { - sender.sendMessage("Could not schedule compilation."); - } + sender.sendMessage("Compiling..."); } } diff --git a/src/main/java/net/citizensnpcs/npc/CitizensNPCManager.java b/src/main/java/net/citizensnpcs/npc/CitizensNPCManager.java index f648bf3e8..f861e398e 100644 --- a/src/main/java/net/citizensnpcs/npc/CitizensNPCManager.java +++ b/src/main/java/net/citizensnpcs/npc/CitizensNPCManager.java @@ -56,7 +56,7 @@ public class CitizensNPCManager implements NPCManager { return createNPC(type, generateUniqueId(), name, character); } - public void despawn(NPC npc, boolean keepSelected) { + void despawn(NPC npc, boolean keepSelected) { if (!keepSelected) npc.removeMetadata("selectors", plugin); npc.getBukkitEntity().remove(); @@ -106,7 +106,7 @@ public class CitizensNPCManager implements NPCManager { return npcs.iterator(); } - public void remove(NPC npc) { + void remove(NPC npc) { npcs.remove(npc.getId()); removeData(npc); } diff --git a/src/main/java/net/citizensnpcs/npc/ai/CitizensAI.java b/src/main/java/net/citizensnpcs/npc/ai/CitizensAI.java index 072289e6b..1c9fcb172 100644 --- a/src/main/java/net/citizensnpcs/npc/ai/CitizensAI.java +++ b/src/main/java/net/citizensnpcs/npc/ai/CitizensAI.java @@ -1,7 +1,6 @@ package net.citizensnpcs.npc.ai; import java.lang.ref.WeakReference; -import java.util.Collections; import java.util.List; import net.citizensnpcs.api.ai.AI; @@ -33,7 +32,6 @@ public class CitizensAI implements AI { if (goals.contains(goal)) return; goals.add(new GoalEntry(priority, goal)); - Collections.sort(goals); } @Override @@ -59,8 +57,8 @@ public class CitizensAI implements AI { GoalEntry item = goals.get(i); if (item == test) continue; - if (test.priority >= item.priority) { - if (!test.goal.isCompatibleWith(item.goal) && executingGoals.contains(item)) { + if (test.getPriority() >= item.getPriority()) { + if (!test.getGoal().isCompatibleWith(item.getGoal()) && executingGoals.contains(item)) { return false; } } /*else if (executingGoals.contains(item) && !item.goal.requiresUpdates()) { @@ -153,18 +151,18 @@ public class CitizensAI implements AI { boolean executing = executingGoals.contains(entry); if (executing) { - if (!entry.goal.continueExecuting() || !isGoalAllowable(entry)) { - entry.goal.reset(); + if (!entry.getGoal().continueExecuting() || !isGoalAllowable(entry)) { + entry.getGoal().reset(); executingGoals.remove(entry); } - } else if (entry.goal.shouldExecute() && isGoalAllowable(entry)) { - entry.goal.start(); + } else if (entry.getGoal().shouldExecute() && isGoalAllowable(entry)) { + entry.getGoal().start(); executingGoals.add(entry); } } for (int i = 0; i < executingGoals.size(); ++i) { - executingGoals.get(i).goal.update(); + executingGoals.get(i).getGoal().update(); } } @@ -174,14 +172,14 @@ public class CitizensAI implements AI { for (Goal goal : toRemove) { for (int i = 0; i < executingGoals.size(); ++i) { GoalEntry entry = executingGoals.get(i); - if (entry.goal.equals(goal)) { - entry.goal.reset(); + if (entry.getGoal().equals(goal)) { + entry.getGoal().reset(); executingGoals.remove(i); } } for (int i = 0; i < goals.size(); ++i) { GoalEntry entry = goals.get(i); - if (entry.goal.equals(goal)) + if (entry.getGoal().equals(goal)) goals.remove(i); } } @@ -190,8 +188,8 @@ public class CitizensAI implements AI { } public static class GoalEntry implements Comparable { - final Goal goal; - final int priority; + private final Goal goal; + private final int priority; public GoalEntry(int priority, Goal goal) { this.priority = priority; @@ -213,10 +211,7 @@ public class CitizensAI implements AI { @Override public int hashCode() { - final int prime = 31; - int result = 1; - result = prime * result + ((goal == null) ? 0 : goal.hashCode()); - return result; + return 31 + ((goal == null) ? 0 : goal.hashCode()); } @Override @@ -224,10 +219,7 @@ public class CitizensAI implements AI { if (this == obj) { return true; } - if (obj == null) { - return false; - } - if (getClass() != obj.getClass()) { + if (obj == null || getClass() != obj.getClass()) { return false; } GoalEntry other = (GoalEntry) obj; diff --git a/src/main/java/net/citizensnpcs/npc/entity/CitizensHumanNPC.java b/src/main/java/net/citizensnpcs/npc/entity/CitizensHumanNPC.java index 8763fffae..89968a462 100644 --- a/src/main/java/net/citizensnpcs/npc/entity/CitizensHumanNPC.java +++ b/src/main/java/net/citizensnpcs/npc/entity/CitizensHumanNPC.java @@ -28,7 +28,7 @@ public class CitizensHumanNPC extends CitizensNPC implements Equipable { EntityHumanNPC handle = new EntityHumanNPC(ws.getServer().getServer(), ws, StringHelper.parseColors(getFullName()), new ItemInWorldManager(ws), this); handle.setPositionRotation(loc.getX(), loc.getY(), loc.getZ(), loc.getYaw(), loc.getPitch()); - handle.X = loc.getYaw(); + handle.X = loc.getYaw() % 360; return handle; } diff --git a/src/main/java/net/citizensnpcs/trait/Behaviour.java b/src/main/java/net/citizensnpcs/trait/Behaviour.java index 753a43f1d..4b983b7bb 100644 --- a/src/main/java/net/citizensnpcs/trait/Behaviour.java +++ b/src/main/java/net/citizensnpcs/trait/Behaviour.java @@ -2,6 +2,8 @@ package net.citizensnpcs.trait; import java.io.File; import java.util.List; +import java.util.Map; +import java.util.Map.Entry; import net.citizensnpcs.api.CitizensAPI; import net.citizensnpcs.api.ai.Goal; @@ -11,24 +13,39 @@ import net.citizensnpcs.api.scripting.CompileCallback; import net.citizensnpcs.api.scripting.ScriptFactory; import net.citizensnpcs.api.trait.Trait; import net.citizensnpcs.api.util.DataKey; -import net.citizensnpcs.npc.ai.CitizensAI.GoalEntry; import org.apache.commons.lang.Validate; +import com.google.common.base.Function; import com.google.common.base.Joiner; import com.google.common.base.Splitter; +import com.google.common.collect.Iterables; import com.google.common.collect.Lists; +import com.google.common.collect.Maps; public class Behaviour extends Trait { - private final List scripts = Lists.newArrayList(); - private final List addedGoals = Lists.newArrayList(); - private final File rootFolder = new File(CitizensAPI.getScriptFolder(), "behaviours"); + private final Map addedGoals = Maps.newHashMap(); + private final Function fileConverterFunction = new Function() { + @Override + public File apply(String arg0) { + return new File(rootFolder, arg0); + } + }; private final NPC npc; + private final File rootFolder = new File(CitizensAPI.getScriptFolder(), "behaviours"); + + private final List scripts = Lists.newArrayList(); public Behaviour(NPC npc) { this.npc = npc; } + public void addScripts(Iterable scripts) { + BehaviourCallback callback = new BehaviourCallback(new Goals()); + Iterable transformed = Iterables.transform(scripts, fileConverterFunction); + CitizensAPI.getScriptCompiler().compile(transformed).withCallback(callback).begin(); + } + @Override public void load(DataKey key) throws NPCLoadException { reset(); @@ -38,22 +55,10 @@ public class Behaviour extends Trait { addScripts(Splitter.on(",").split(scripts)); } - private void reset() { - removeGoals(); - scripts.clear(); - addedGoals.clear(); - } - - private void removeGoals() { - for (GoalEntry entry : addedGoals) { - npc.getAI().removeGoal(entry.getGoal()); - } - } - @Override public void onNPCSpawn() { - for (GoalEntry entry : addedGoals) { - npc.getAI().addGoal(entry.getPriority(), entry.getGoal()); + for (Entry entry : addedGoals.entrySet()) { + npc.getAI().addGoal(entry.getValue(), entry.getKey()); } } @@ -62,20 +67,16 @@ public class Behaviour extends Trait { removeGoals(); } - public void addScripts(Iterable scripts) { - BehaviourCallback callback = new BehaviourCallback(new Goals()); - for (String script : scripts) { - File file = new File(rootFolder, script); - if (!file.exists()) - continue; - CitizensAPI.getScriptCompiler().compile(file).withCallback(callback).begin(); - this.scripts.add(file); + private void removeGoals() { + for (Goal entry : addedGoals.keySet()) { + npc.getAI().removeGoal(entry); } - List added = callback.goals.goals; - for (GoalEntry entry : added) { - npc.getAI().addGoal(entry.getPriority(), entry.getGoal()); - } - addedGoals.addAll(added); + } + + private void reset() { + removeGoals(); + scripts.clear(); + addedGoals.clear(); } @Override @@ -90,6 +91,16 @@ public class Behaviour extends Trait { this.goals = goals; } + @Override + public void onCompileTaskFinished() { + addedGoals.putAll(goals.goals); + if (!npc.isSpawned()) + return; + for (Entry entry : goals.goals.entrySet()) { + npc.getAI().addGoal(entry.getValue(), entry.getKey()); + } + } + @Override public void onScriptCompiled(ScriptFactory script) { script.newInstance().invoke("addGoals", goals, npc); @@ -97,11 +108,11 @@ public class Behaviour extends Trait { } public static class Goals { - private final List goals = Lists.newArrayList(); + private final Map goals = Maps.newHashMap(); public void addGoal(int priority, Goal goal) { Validate.notNull(goal); - this.goals.add(new GoalEntry(priority, goal)); + goals.put(goal, priority); } } }