mirror of
https://github.com/CitizensDev/Citizens2.git
synced 2024-11-27 13:15:33 +01:00
Remove behaviours and /script command
This commit is contained in:
parent
32f745a7d0
commit
569c4a4c51
@ -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);
|
||||
|
@ -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<String> 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
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
@ -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"));
|
||||
|
@ -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<BehaviourGoalEntry> addedGoals = Lists.newArrayList();
|
||||
private final Function<String, File> fileConverterFunction = new Function<String, File>() {
|
||||
@Override
|
||||
public File apply(String arg0) {
|
||||
return new File(rootFolder, arg0);
|
||||
}
|
||||
};
|
||||
private final File rootFolder = new File(CitizensAPI.getScriptFolder(), "behaviours");
|
||||
private final List<File> scripts = Lists.newArrayList();
|
||||
|
||||
public Behaviour() {
|
||||
super("behaviour");
|
||||
if (!rootFolder.exists())
|
||||
rootFolder.mkdirs();
|
||||
}
|
||||
|
||||
public void addScripts(Iterable<String> scripts) {
|
||||
BehaviourCallback callback = new BehaviourCallback();
|
||||
Iterable<File> 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<String> files) {
|
||||
Iterable<File> transformed = Iterables.transform(files, fileConverterFunction);
|
||||
boolean isSpawned = npc.isSpawned();
|
||||
for (File file : transformed) {
|
||||
if (isSpawned) {
|
||||
Iterator<BehaviourGoalEntry> 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<BehaviourGoalEntry> 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<Object>() {
|
||||
@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;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user