mirror of
https://github.com/CitizensDev/Citizens2.git
synced 2025-02-28 18:21:19 +01:00
Changes
This commit is contained in:
parent
154192ae38
commit
37ba042c29
@ -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 {
|
||||
|
@ -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, "<a>Done.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onCompileTaskFinished() {
|
||||
}
|
||||
}).begin();
|
||||
if (success) {
|
||||
sender.sendMessage("Compiling...");
|
||||
} else {
|
||||
sender.sendMessage("Could not schedule compilation.");
|
||||
}
|
||||
sender.sendMessage("Compiling...");
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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<GoalEntry> {
|
||||
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;
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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<File> scripts = Lists.newArrayList();
|
||||
private final List<GoalEntry> addedGoals = Lists.newArrayList();
|
||||
private final File rootFolder = new File(CitizensAPI.getScriptFolder(), "behaviours");
|
||||
private final Map<Goal, Integer> addedGoals = Maps.newHashMap();
|
||||
private final Function<String, File> fileConverterFunction = new Function<String, File>() {
|
||||
@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<File> scripts = Lists.newArrayList();
|
||||
|
||||
public Behaviour(NPC npc) {
|
||||
this.npc = npc;
|
||||
}
|
||||
|
||||
public void addScripts(Iterable<String> scripts) {
|
||||
BehaviourCallback callback = new BehaviourCallback(new Goals());
|
||||
Iterable<File> 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<Goal, Integer> entry : addedGoals.entrySet()) {
|
||||
npc.getAI().addGoal(entry.getValue(), entry.getKey());
|
||||
}
|
||||
}
|
||||
|
||||
@ -62,20 +67,16 @@ public class Behaviour extends Trait {
|
||||
removeGoals();
|
||||
}
|
||||
|
||||
public void addScripts(Iterable<String> 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<GoalEntry> 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<Goal, Integer> 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<GoalEntry> goals = Lists.newArrayList();
|
||||
private final Map<Goal, Integer> goals = Maps.newHashMap();
|
||||
|
||||
public void addGoal(int priority, Goal goal) {
|
||||
Validate.notNull(goal);
|
||||
this.goals.add(new GoalEntry(priority, goal));
|
||||
goals.put(goal, priority);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user