From 569c4a4c510457b00aba294514b405d6544aab7f Mon Sep 17 00:00:00 2001 From: fullwall Date: Mon, 1 Apr 2013 20:04:40 +0800 Subject: [PATCH] Remove behaviours and /script command --- src/main/java/net/citizensnpcs/Citizens.java | 2 - .../citizensnpcs/commands/NPCCommands.java | 27 --- .../citizensnpcs/commands/ScriptCommands.java | 55 ------ .../npc/CitizensTraitFactory.java | 2 - .../net/citizensnpcs/trait/Behaviour.java | 157 ------------------ 5 files changed, 243 deletions(-) delete mode 100644 src/main/java/net/citizensnpcs/commands/ScriptCommands.java delete mode 100644 src/main/java/net/citizensnpcs/trait/Behaviour.java diff --git a/src/main/java/net/citizensnpcs/Citizens.java b/src/main/java/net/citizensnpcs/Citizens.java index a16474572..1516d8356 100644 --- a/src/main/java/net/citizensnpcs/Citizens.java +++ b/src/main/java/net/citizensnpcs/Citizens.java @@ -39,7 +39,6 @@ import net.citizensnpcs.commands.AdminCommands; import net.citizensnpcs.commands.EditorCommands; import net.citizensnpcs.commands.HelpCommands; import net.citizensnpcs.commands.NPCCommands; -import net.citizensnpcs.commands.ScriptCommands; import net.citizensnpcs.commands.TemplateCommands; import net.citizensnpcs.commands.TraitCommands; import net.citizensnpcs.commands.WaypointCommands; @@ -305,7 +304,6 @@ public class Citizens extends JavaPlugin implements CitizensPlugin { commands.register(EditorCommands.class); commands.register(HelpCommands.class); commands.register(NPCCommands.class); - commands.register(ScriptCommands.class); commands.register(TemplateCommands.class); commands.register(TraitCommands.class); commands.register(WaypointCommands.class); diff --git a/src/main/java/net/citizensnpcs/commands/NPCCommands.java b/src/main/java/net/citizensnpcs/commands/NPCCommands.java index 6086b97d5..1aea7b5b3 100644 --- a/src/main/java/net/citizensnpcs/commands/NPCCommands.java +++ b/src/main/java/net/citizensnpcs/commands/NPCCommands.java @@ -35,7 +35,6 @@ import net.citizensnpcs.npc.NPCSelector; import net.citizensnpcs.npc.Template; import net.citizensnpcs.trait.Age; import net.citizensnpcs.trait.Anchors; -import net.citizensnpcs.trait.Behaviour; import net.citizensnpcs.trait.Controllable; import net.citizensnpcs.trait.CurrentLocation; import net.citizensnpcs.trait.Gravity; @@ -200,26 +199,6 @@ public class NPCCommands { npc.getBukkitEntity().teleport(args.getSenderLocation()); } - @Command( - aliases = { "npc" }, - usage = "behaviour [scripts] (-r)", - desc = "Sets the behaviour of a NPC", - help = Messages.BEHAVIOUR_HELP, - modifiers = { "behaviour", "behavior", "ai" }, - flags = "r", - min = 2, - permission = "citizens.npc.behaviour") - public void behaviour(CommandContext args, CommandSender sender, NPC npc) throws CommandException { - Iterable files = Splitter.on(',').split(args.getJoinedStrings(1, ',')); - if (args.hasFlag('r')) { - npc.getTrait(Behaviour.class).removeScripts(files); - Messaging.sendTr(sender, Messages.BEHAVIOURS_REMOVED); - } else { - npc.getTrait(Behaviour.class).addScripts(files); - Messaging.sendTr(sender, Messages.BEHAVIOURS_ADDED); - } - } - @Command( aliases = { "npc" }, usage = "controllable|control -f", @@ -321,12 +300,6 @@ public class NPCCommands { } } - if (args.hasValueFlag("b") || args.hasValueFlag("behaviours") || args.hasValueFlag("behaviors")) { - String behaviours = args.getFlag("b", args.getFlag("behaviours", args.getFlag("behaviors"))); - npc.getTrait(Behaviour.class).addScripts(Splitter.on(',').split(behaviours)); - msg += " with the specified behaviours"; - } - msg += "."; // Initialize necessary traits diff --git a/src/main/java/net/citizensnpcs/commands/ScriptCommands.java b/src/main/java/net/citizensnpcs/commands/ScriptCommands.java deleted file mode 100644 index f399e54e9..000000000 --- a/src/main/java/net/citizensnpcs/commands/ScriptCommands.java +++ /dev/null @@ -1,55 +0,0 @@ -package net.citizensnpcs.commands; - -import java.io.File; - -import net.citizensnpcs.Citizens; -import net.citizensnpcs.api.CitizensAPI; -import net.citizensnpcs.api.command.Command; -import net.citizensnpcs.api.command.CommandContext; -import net.citizensnpcs.api.command.exception.CommandException; -import net.citizensnpcs.api.npc.NPC; -import net.citizensnpcs.api.scripting.CompileCallback; -import net.citizensnpcs.api.scripting.Script; -import net.citizensnpcs.api.scripting.ScriptFactory; -import net.citizensnpcs.api.util.Messaging; -import net.citizensnpcs.util.Messages; - -import org.bukkit.command.CommandSender; - -import com.google.common.base.Splitter; - -public class ScriptCommands { - public ScriptCommands(Citizens plugin) { - } - - @Command( - aliases = { "script" }, - modifiers = { "compile", "run" }, - usage = "compile|run [file] (--methods [methods])", - desc = "compile and run a script", - min = 2, - max = 2, - permission = "citizens.script.compile") - public void runScript(final CommandContext args, final CommandSender sender, NPC npc) throws CommandException { - File file = new File(CitizensAPI.getScriptFolder(), args.getString(1)); - if (!file.exists()) - throw new CommandException(Messages.SCRIPT_FILE_MISSING, args.getString(1)); - CitizensAPI.getScriptCompiler().compile(file).withCallback(new CompileCallback() { - @Override - public void onCompileTaskFinished() { - } - - @Override - public void onScriptCompiled(File file, ScriptFactory script) { - Script s = script.newInstance(); - if (args.hasValueFlag("methods")) { - for (String m : Splitter.on(',').split(args.getFlag("methods"))) { - s.invoke(m, new Object[] {}); - } - } - Messaging.sendTr(sender, Messages.SCRIPT_COMPILED); - } - }).begin(); - Messaging.sendTr(sender, Messages.SCRIPT_COMPILING); - } -} diff --git a/src/main/java/net/citizensnpcs/npc/CitizensTraitFactory.java b/src/main/java/net/citizensnpcs/npc/CitizensTraitFactory.java index dac518b3f..f20f7ee96 100644 --- a/src/main/java/net/citizensnpcs/npc/CitizensTraitFactory.java +++ b/src/main/java/net/citizensnpcs/npc/CitizensTraitFactory.java @@ -18,7 +18,6 @@ import net.citizensnpcs.api.trait.trait.Spawned; import net.citizensnpcs.api.trait.trait.Speech; import net.citizensnpcs.trait.Age; import net.citizensnpcs.trait.Anchors; -import net.citizensnpcs.trait.Behaviour; import net.citizensnpcs.trait.Controllable; import net.citizensnpcs.trait.CurrentLocation; import net.citizensnpcs.trait.Gravity; @@ -45,7 +44,6 @@ public class CitizensTraitFactory implements TraitFactory { public CitizensTraitFactory() { registerTrait(TraitInfo.create(Age.class).withName("age")); registerTrait(TraitInfo.create(Anchors.class).withName("anchors")); - registerTrait(TraitInfo.create(Behaviour.class).withName("behaviour")); registerTrait(TraitInfo.create(Controllable.class).withName("controllable")); registerTrait(TraitInfo.create(Equipment.class).withName("equipment")); registerTrait(TraitInfo.create(Gravity.class).withName("gravity")); diff --git a/src/main/java/net/citizensnpcs/trait/Behaviour.java b/src/main/java/net/citizensnpcs/trait/Behaviour.java deleted file mode 100644 index b85c93bfe..000000000 --- a/src/main/java/net/citizensnpcs/trait/Behaviour.java +++ /dev/null @@ -1,157 +0,0 @@ -package net.citizensnpcs.trait; - -import java.io.File; -import java.util.Iterator; -import java.util.List; -import java.util.concurrent.Callable; - -import net.citizensnpcs.api.CitizensAPI; -import net.citizensnpcs.api.ai.Goal; -import net.citizensnpcs.api.ai.GoalController.GoalEntry; -import net.citizensnpcs.api.ai.SimpleGoalEntry; -import net.citizensnpcs.api.exception.NPCLoadException; -import net.citizensnpcs.api.scripting.CompileCallback; -import net.citizensnpcs.api.scripting.Script; -import net.citizensnpcs.api.scripting.ScriptFactory; -import net.citizensnpcs.api.trait.Trait; -import net.citizensnpcs.api.util.DataKey; - -import org.apache.commons.lang.Validate; -import org.bukkit.Bukkit; - -import com.google.common.base.Function; -import com.google.common.base.Splitter; -import com.google.common.collect.Iterables; -import com.google.common.collect.Lists; - -public class Behaviour extends Trait { - private final List addedGoals = Lists.newArrayList(); - private final Function fileConverterFunction = new Function() { - @Override - public File apply(String arg0) { - return new File(rootFolder, arg0); - } - }; - private final File rootFolder = new File(CitizensAPI.getScriptFolder(), "behaviours"); - private final List scripts = Lists.newArrayList(); - - public Behaviour() { - super("behaviour"); - if (!rootFolder.exists()) - rootFolder.mkdirs(); - } - - public void addScripts(Iterable scripts) { - BehaviourCallback callback = new BehaviourCallback(); - Iterable transformed = Iterables.transform(scripts, fileConverterFunction); - CitizensAPI.getScriptCompiler().compile(transformed).withCallback(callback).begin(); - } - - @Override - public void load(DataKey key) throws NPCLoadException { - reset(); - if (!key.keyExists("scripts")) - return; - String scripts = key.getString("scripts"); - addScripts(Splitter.on(',').split(scripts)); - } - - @Override - public void onRemove() { - removeGoals(); - } - - @Override - public void onSpawn() { - for (GoalEntry entry : addedGoals) { - npc.getDefaultGoalController().addGoal(entry.getGoal(), entry.getPriority()); - } - } - - private void removeGoals() { - for (GoalEntry entry : addedGoals) { - npc.getDefaultGoalController().removeGoal(entry.getGoal()); - } - } - - public void removeScripts(Iterable files) { - Iterable transformed = Iterables.transform(files, fileConverterFunction); - boolean isSpawned = npc.isSpawned(); - for (File file : transformed) { - if (isSpawned) { - Iterator itr = addedGoals.iterator(); - while (itr.hasNext()) { - BehaviourGoalEntry entry = itr.next(); - if (file.equals(entry.file)) { - itr.remove(); - npc.getDefaultGoalController().removeGoal(entry.getGoal()); - } - } - } - scripts.remove(file); - } - } - - private void reset() { - removeGoals(); - scripts.clear(); - addedGoals.clear(); - } - - @Override - public void save(DataKey key) { - key.removeKey("scripts"); - StringBuilder fileNames = new StringBuilder(); - for (File file : scripts) { - fileNames.append(file.getName() + ','); - } - if (fileNames.length() > 0) - key.setString("scripts", fileNames.substring(0, fileNames.length() - 1)); - } - - public class BehaviourCallback implements CompileCallback { - private File fileInUse; - private final List goals = Lists.newArrayList(); - - public void addGoal(int priority, Goal goal) { - Validate.notNull(goal); - goals.add(new BehaviourGoalEntry(goal, priority, fileInUse)); - } - - @Override - public void onCompileTaskFinished() { - addedGoals.addAll(goals); - if (!npc.isSpawned()) - return; - for (GoalEntry entry : goals) { - npc.getDefaultGoalController().addGoal(entry.getGoal(), entry.getPriority()); - } - } - - @Override - public void onScriptCompiled(final File file, final ScriptFactory script) { - final Script instance = script.newInstance(); - Bukkit.getScheduler().callSyncMethod(CitizensAPI.getPlugin(), new Callable() { - @Override - public Object call() throws Exception { - synchronized (Behaviour.this) { - fileInUse = file; - instance.invoke("addGoals", BehaviourCallback.this, npc); - scripts.add(file); - fileInUse = null; - } - return null; - } - }); - } - } - - private static class BehaviourGoalEntry extends SimpleGoalEntry { - private final File file; - - private BehaviourGoalEntry(Goal goal, int priority, File file) { - super(goal, priority); - this.file = file; - } - } -}