Change mob classes to static inner, add rudimentary pathfinder

This commit is contained in:
fullwall 2012-02-04 14:48:23 +08:00
parent 6bb38d534e
commit ad65534ac5
71 changed files with 1225 additions and 1029 deletions

View File

@ -2,11 +2,8 @@ package net.citizensnpcs;
import java.io.File;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
import java.util.logging.Level;
import net.citizensnpcs.Settings.Setting;
@ -51,9 +48,16 @@ import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionDefault;
import org.bukkit.plugin.java.JavaPlugin;
import com.google.common.collect.Lists;
public class Citizens extends JavaPlugin {
private static final String COMPATIBLE_MC_VERSION = "1.1";
@SuppressWarnings("unchecked")
private static final List<Class<? extends Trait>> traits = Lists.newArrayList(Owner.class, Spawned.class,
LookClose.class, SpawnLocation.class);
// TODO: automatically register trait classes in CitizensNPC?
private volatile CitizensNPCManager npcManager;
private final InstanceFactory<Character> characterManager = new DefaultInstanceFactory<Character>();
private final InstanceFactory<Trait> traitManager = new DefaultInstanceFactory<Trait>();
@ -62,6 +66,24 @@ public class Citizens extends JavaPlugin {
private Storage saves;
private boolean compatible;
private boolean handleMistake(CommandSender sender, String command, String modifier) {
int minDist = Integer.MAX_VALUE;
String closest = "";
for (String string : cmdManager.getAllCommandModifiers(command)) {
int distance = StringHelper.getLevenshteinDistance(modifier, string);
if (minDist > distance) {
minDist = distance;
closest = string;
}
}
if (!closest.isEmpty()) {
sender.sendMessage(ChatColor.GRAY + "Unknown command. Did you mean:");
sender.sendMessage(StringHelper.wrap(" /") + command + " " + StringHelper.wrap(closest));
return true;
}
return false;
}
@Override
public boolean onCommand(CommandSender sender, Command cmd, String cmdName, String[] args) {
Player player = null;
@ -149,10 +171,11 @@ public class Citizens extends JavaPlugin {
config.load();
// NPC storage
if (Setting.USE_DATABASE.asBoolean())
if (Setting.USE_DATABASE.asBoolean()) {
saves = new DatabaseStorage();
else
} else {
saves = new YamlStorage(getDataFolder() + File.separator + "saves.yml");
}
// Register API managers
npcManager = new CitizensNPCManager(saves);
@ -167,10 +190,7 @@ public class Citizens extends JavaPlugin {
registerCommands();
registerPermissions();
traitManager.register(SpawnLocation.class);
traitManager.register(Owner.class);
traitManager.register(Spawned.class);
traitManager.register(LookClose.class);
traitManager.registerAll(traits);
Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new NPCUpdater(npcManager), 0, 1);
@ -193,6 +213,29 @@ public class Citizens extends JavaPlugin {
}
}
private void registerCommands() {
cmdManager = new CommandManager();
cmdManager.setInjector(new Injector(npcManager, characterManager));
// cmdManager.register(AdminCommands.class);
cmdManager.register(NPCCommands.class);
}
private void registerPermissions() {
// TODO There has to be a better way than this (maybe use Permission
// annotation to register permissions?)
Map<String, Boolean> children = new HashMap<String, Boolean>();
children.put("citizens.npc.create", true);
children.put("citizens.npc.spawn", true);
children.put("citizens.npc.despawn", true);
children.put("citizens.npc.select", true);
children.put("citizens.npc.tp", true);
children.put("citizens.npc.tphere", true);
Permission perm = new Permission("citizens.*", PermissionDefault.OP, children);
getServer().getPluginManager().addPermission(perm);
}
private void saveNPCs() {
for (NPC npc : npcManager)
npc.save(saves.getKey("npc." + npc.getId()));
@ -221,55 +264,4 @@ public class Citizens extends JavaPlugin {
}
Messaging.log("Loaded " + created + " NPCs (" + spawned + " spawned).");
}
private void registerPermissions() {
// TODO There has to be a better way than this (maybe use Permission
// annotation to register permissions?)
Map<String, Boolean> children = new HashMap<String, Boolean>();
children.put("citizens.npc.create", true);
children.put("citizens.npc.spawn", true);
children.put("citizens.npc.despawn", true);
children.put("citizens.npc.select", true);
children.put("citizens.npc.tp", true);
children.put("citizens.npc.tphere", true);
Permission perm = new Permission("citizens.*", PermissionDefault.OP, children);
getServer().getPluginManager().addPermission(perm);
}
private void registerCommands() {
cmdManager = new CommandManager();
cmdManager.setInjector(new Injector(npcManager, characterManager));
// cmdManager.register(AdminCommands.class);
cmdManager.register(NPCCommands.class);
}
private boolean handleMistake(CommandSender sender, String command, String modifier) {
String[] modifiers = cmdManager.getAllCommandModifiers(command);
Map<Integer, String> values = new TreeMap<Integer, String>();
int i = 0;
for (String string : modifiers) {
values.put(StringHelper.getLevenshteinDistance(modifier, string), modifiers[i]);
++i;
}
int best = 0;
boolean stop = false;
Set<String> possible = new HashSet<String>();
for (Entry<Integer, String> entry : values.entrySet()) {
if (!stop) {
best = entry.getKey();
stop = true;
} else if (entry.getKey() > best)
break;
possible.add(entry.getValue());
}
if (possible.size() > 0) {
sender.sendMessage(ChatColor.GRAY + "Unknown command. Did you mean:");
for (String string : possible)
sender.sendMessage(StringHelper.wrap(" /") + command + " " + StringHelper.wrap(string));
return true;
}
return false;
}
}

View File

@ -1,12 +1,11 @@
package net.citizensnpcs;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.npc.entity.CitizensHumanNPC;
public class NPCUpdater implements Runnable {
private CitizensNPCManager npcManager;
private final CitizensNPCManager npcManager;
public NPCUpdater(CitizensNPCManager npcManager) {
this.npcManager = npcManager;
@ -16,7 +15,8 @@ public class NPCUpdater implements Runnable {
public void run() {
for (NPC npc : npcManager)
// For now only do this for human NPCs
if (((CitizensNPC) npc) instanceof CitizensHumanNPC)
if (npc instanceof CitizensHumanNPC) {
((CitizensHumanNPC) npc).tick();
}
}
}

View File

@ -44,10 +44,6 @@ public class Settings {
this.value = value;
}
private Object get() {
return value;
}
public boolean asBoolean() {
return (Boolean) value;
}
@ -68,6 +64,10 @@ public class Settings {
return value.toString();
}
private Object get() {
return value;
}
private void set(Object value) {
this.value = value;
}

View File

@ -48,38 +48,14 @@ public class CommandContext {
String.class);
}
public int argsLength() {
return args.length - 1;
}
public String getCommand() {
return args[0];
}
public boolean matches(String command) {
return args[0].equalsIgnoreCase(command);
}
public String getString(int index) {
return args[index + 1];
}
public String getString(int index, String def) {
return index + 1 < args.length ? args[index + 1] : def;
}
public String getJoinedStrings(int initialIndex) {
initialIndex = initialIndex + 1;
StringBuilder buffer = new StringBuilder(args[initialIndex]);
for (int i = initialIndex + 1; i < args.length; i++)
buffer.append(" ").append(args[i]);
return buffer.toString();
}
public int getInteger(int index) throws NumberFormatException {
return Integer.parseInt(args[index + 1]);
}
public int getInteger(int index, int def) throws NumberFormatException {
return index + 1 < args.length ? Integer.parseInt(args[index + 1]) : def;
}
public double getDouble(int index) throws NumberFormatException {
return Double.parseDouble(args[index + 1]);
}
@ -88,10 +64,24 @@ public class CommandContext {
return index + 1 < args.length ? Double.parseDouble(args[index + 1]) : def;
}
public String[] getSlice(int index) {
String[] slice = new String[args.length - index];
System.arraycopy(args, index, slice, 0, args.length - index);
return slice;
public Set<Character> getFlags() {
return flags;
}
public int getInteger(int index) throws NumberFormatException {
return Integer.parseInt(args[index + 1]);
}
public int getInteger(int index, int def) throws NumberFormatException {
return index + 1 < args.length ? Integer.parseInt(args[index + 1]) : def;
}
public String getJoinedStrings(int initialIndex) {
initialIndex = initialIndex + 1;
StringBuilder buffer = new StringBuilder(args[initialIndex]);
for (int i = initialIndex + 1; i < args.length; i++)
buffer.append(" ").append(args[i]);
return buffer.toString();
}
public String[] getPaddedSlice(int index, int padding) {
@ -100,19 +90,29 @@ public class CommandContext {
return slice;
}
public boolean hasFlag(char ch) {
return flags.contains(ch);
public String[] getSlice(int index) {
String[] slice = new String[args.length - index];
System.arraycopy(args, index, slice, 0, args.length - index);
return slice;
}
public Set<Character> getFlags() {
return flags;
public String getString(int index) {
return args[index + 1];
}
public String getString(int index, String def) {
return index + 1 < args.length ? args[index + 1] : def;
}
public boolean hasFlag(char ch) {
return flags.contains(ch);
}
public int length() {
return args.length;
}
public int argsLength() {
return args.length - 1;
public boolean matches(String command) {
return args[0].equalsIgnoreCase(command);
}
}

View File

@ -11,11 +11,6 @@ public class CommandIdentifier {
this.modifier = modifier;
}
@Override
public int hashCode() {
return Objects.hashCode(command, modifier);
}
@Override
public boolean equals(Object obj) {
if (this == obj)
@ -38,11 +33,16 @@ public class CommandIdentifier {
return true;
}
public String getCommand() {
return command;
}
public String getModifier() {
return modifier;
}
public String getCommand() {
return command;
@Override
public int hashCode() {
return Objects.hashCode(command, modifier);
}
}

View File

@ -33,9 +33,9 @@ import java.util.logging.Logger;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.npc.trait.trait.Owner;
import net.citizensnpcs.command.annotation.Command;
import net.citizensnpcs.command.annotation.Requirements;
import net.citizensnpcs.command.annotation.NestedCommand;
import net.citizensnpcs.command.annotation.Permission;
import net.citizensnpcs.command.annotation.Requirements;
import net.citizensnpcs.command.annotation.ServerCommand;
import net.citizensnpcs.command.exception.CommandException;
import net.citizensnpcs.command.exception.CommandUsageException;
@ -51,205 +51,6 @@ import org.bukkit.command.ConsoleCommandSender;
import org.bukkit.entity.Player;
public class CommandManager {
// Logger for general errors.
private static final Logger logger = Logger.getLogger(CommandManager.class.getCanonicalName());
/*
* Mapping of commands (including aliases) with a description. Root commands
* are stored under a key of null, whereas child commands are cached under
* their respective Method. The child map has the key of the command name
* (one for each alias) with the method.
*/
private final Map<Method, Map<CommandIdentifier, Method>> commands = new HashMap<Method, Map<CommandIdentifier, Method>>();
// Used to store the instances associated with a method.
private final Map<Method, Object> instances = new HashMap<Method, Object>();
/*
* Mapping of commands (not including aliases) with a description. This is
* only for top level commands.
*/
private final Map<CommandIdentifier, String> descs = new HashMap<CommandIdentifier, String>();
// Stores the injector used to getInstance.
private Injector injector;
private final Map<Method, Requirements> requirements = new HashMap<Method, Requirements>();
private final Map<Method, ServerCommand> serverCommands = new HashMap<Method, ServerCommand>();
/*
* Register an class that contains commands (denoted by Command. If no
* dependency injector is specified, then the methods of the class will be
* registered to be called statically. Otherwise, new instances will be
* created of the command classes and methods will not be called statically.
*/
public void register(Class<?> clazz) {
registerMethods(clazz, null);
}
/*
* Register the methods of a class. This will automatically construct
* instances as necessary.
*/
private void registerMethods(Class<?> clazz, Method parent) {
Object obj = injector.getInstance(clazz);
registerMethods(clazz, parent, obj);
}
// Register the methods of a class.
private void registerMethods(Class<?> clazz, Method parent, Object obj) {
Map<CommandIdentifier, Method> map;
// Make a new hash map to cache the commands for this class
// as looking up methods via reflection is fairly slow
if (commands.containsKey(parent))
map = commands.get(parent);
else {
map = new HashMap<CommandIdentifier, Method>();
commands.put(parent, map);
}
for (Method method : clazz.getMethods()) {
if (!method.isAnnotationPresent(Command.class))
continue;
boolean isStatic = Modifier.isStatic(method.getModifiers());
Command cmd = method.getAnnotation(Command.class);
String[] modifiers = cmd.modifiers();
// Cache the aliases too
for (String alias : cmd.aliases())
for (String modifier : modifiers)
map.put(new CommandIdentifier(alias, modifier), method);
Requirements cmdRequirements = null;
if (method.getDeclaringClass().isAnnotationPresent(Requirements.class))
cmdRequirements = method.getDeclaringClass().getAnnotation(Requirements.class);
if (method.isAnnotationPresent(Requirements.class))
cmdRequirements = method.getAnnotation(Requirements.class);
if (requirements != null)
requirements.put(method, cmdRequirements);
ServerCommand serverCommand = null;
if (method.isAnnotationPresent(ServerCommand.class))
serverCommand = method.getAnnotation(ServerCommand.class);
if (serverCommand != null)
serverCommands.put(method, serverCommand);
// We want to be able invoke with an instance
if (!isStatic) {
// Can't register this command if we don't have an instance
if (obj == null)
continue;
instances.put(method, obj);
}
// Build a list of commands and their usage details, at least for
// root level commands
if (parent == null)
if (cmd.usage().length() == 0)
descs.put(new CommandIdentifier(cmd.aliases()[0], cmd.modifiers()[0]), cmd.desc());
else
descs.put(new CommandIdentifier(cmd.aliases()[0], cmd.modifiers()[0]),
cmd.usage() + " - " + cmd.desc());
// Look for nested commands -- if there are any, those have
// to be cached too so that they can be quickly looked
// up when processing commands
if (method.isAnnotationPresent(NestedCommand.class)) {
NestedCommand nestedCmd = method.getAnnotation(NestedCommand.class);
for (Class<?> nestedCls : nestedCmd.value())
registerMethods(nestedCls, method);
}
}
}
/*
* Checks to see whether there is a command named such at the root level.
* This will check aliases as well.
*/
public boolean hasCommand(String command, String modifier) {
return commands.get(null).containsKey(new CommandIdentifier(command.toLowerCase(), modifier.toLowerCase()))
|| commands.get(null).containsKey(new CommandIdentifier(command.toLowerCase(), "*"));
}
// Get a list of command descriptions. This is only for root commands.
public Map<CommandIdentifier, String> getCommands() {
return descs;
}
// Get the usage string for a command.
private String getUsage(String[] args, int level, Command cmd) {
StringBuilder command = new StringBuilder();
command.append("/");
for (int i = 0; i <= level; i++)
command.append(args[i] + " ");
// removed arbitrary positioning of flags.
command.append(cmd.usage());
return command.toString();
}
// Get the usage string for a nested command.
private String getNestedUsage(String[] args, int level, Method method, Player player) throws CommandException {
StringBuilder command = new StringBuilder();
command.append("/");
for (int i = 0; i <= level; i++)
command.append(args[i] + " ");
Map<CommandIdentifier, Method> map = commands.get(method);
boolean found = false;
command.append("<");
Set<String> allowedCommands = new HashSet<String>();
for (Map.Entry<CommandIdentifier, Method> entry : map.entrySet()) {
Method childMethod = entry.getValue();
found = true;
if (hasPermission(childMethod, player)) {
Command childCmd = childMethod.getAnnotation(Command.class);
allowedCommands.add(childCmd.aliases()[0]);
}
}
if (allowedCommands.size() > 0)
command.append(joinString(allowedCommands, "|", 0));
else {
if (!found)
command.append("?");
else
throw new NoPermissionsException();
}
command.append(">");
return command.toString();
}
public static String joinString(Collection<?> str, String delimiter, int initialIndex) {
if (str.size() == 0)
return "";
StringBuilder buffer = new StringBuilder();
int i = 0;
for (Object o : str) {
if (i >= initialIndex) {
if (i > 0)
buffer.append(delimiter);
buffer.append(o.toString());
}
i++;
}
return buffer.toString();
}
/*
* Attempt to execute a command. This version takes a separate command name
* (for the root command) and then a list of following arguments.
@ -354,6 +155,79 @@ public class CommandManager {
}
}
public String[] getAllCommandModifiers(String command) {
Set<String> cmds = new HashSet<String>();
for (Map<CommandIdentifier, Method> enclosing : commands.values()) {
for (CommandIdentifier identifier : enclosing.keySet()) {
if (identifier.getCommand().equals(command)) {
cmds.add(identifier.getModifier());
}
}
}
return cmds.toArray(new String[cmds.size()]);
}
// Get a list of command descriptions. This is only for root commands.
public Map<CommandIdentifier, String> getCommands() {
return descs;
}
// Get the usage string for a nested command.
private String getNestedUsage(String[] args, int level, Method method, Player player) throws CommandException {
StringBuilder command = new StringBuilder();
command.append("/");
for (int i = 0; i <= level; i++)
command.append(args[i] + " ");
Map<CommandIdentifier, Method> map = commands.get(method);
boolean found = false;
command.append("<");
Set<String> allowedCommands = new HashSet<String>();
for (Map.Entry<CommandIdentifier, Method> entry : map.entrySet()) {
Method childMethod = entry.getValue();
found = true;
if (hasPermission(childMethod, player)) {
Command childCmd = childMethod.getAnnotation(Command.class);
allowedCommands.add(childCmd.aliases()[0]);
}
}
if (allowedCommands.size() > 0)
command.append(joinString(allowedCommands, "|", 0));
else {
if (!found)
command.append("?");
else
throw new NoPermissionsException();
}
command.append(">");
return command.toString();
}
// Get the usage string for a command.
private String getUsage(String[] args, int level, Command cmd) {
StringBuilder command = new StringBuilder();
command.append("/");
for (int i = 0; i <= level; i++)
command.append(args[i] + " ");
// removed arbitrary positioning of flags.
command.append(cmd.usage());
return command.toString();
}
// Returns whether a player has access to a command.
private boolean hasPermission(Method method, Player player) {
Permission permission = method.getAnnotation(Permission.class);
@ -371,19 +245,151 @@ public class CommandManager {
return player.hasPermission("citizens." + perm);
}
public String[] getAllCommandModifiers(String command) {
Set<String> cmds = new HashSet<String>();
for (Map<CommandIdentifier, Method> enclosing : commands.values()) {
for (CommandIdentifier identifier : enclosing.keySet()) {
if (identifier.getCommand().equals(command)) {
cmds.add(identifier.getModifier());
}
}
}
return cmds.toArray(new String[cmds.size()]);
}
public void setInjector(Injector injector) {
this.injector = injector;
}
// Logger for general errors.
private static final Logger logger = Logger.getLogger(CommandManager.class.getCanonicalName());
/*
* Mapping of commands (including aliases) with a description. Root commands
* are stored under a key of null, whereas child commands are cached under
* their respective Method. The child map has the key of the command name
* (one for each alias) with the method.
*/
private final Map<Method, Map<CommandIdentifier, Method>> commands = new HashMap<Method, Map<CommandIdentifier, Method>>();
// Used to store the instances associated with a method.
private final Map<Method, Object> instances = new HashMap<Method, Object>();
/*
* Mapping of commands (not including aliases) with a description. This is
* only for top level commands.
*/
private final Map<CommandIdentifier, String> descs = new HashMap<CommandIdentifier, String>();
// Stores the injector used to getInstance.
private Injector injector;
private final Map<Method, Requirements> requirements = new HashMap<Method, Requirements>();
private final Map<Method, ServerCommand> serverCommands = new HashMap<Method, ServerCommand>();
/*
* Checks to see whether there is a command named such at the root level.
* This will check aliases as well.
*/
public boolean hasCommand(String command, String modifier) {
return commands.get(null).containsKey(new CommandIdentifier(command.toLowerCase(), modifier.toLowerCase()))
|| commands.get(null).containsKey(new CommandIdentifier(command.toLowerCase(), "*"));
}
/*
* Register an class that contains commands (denoted by Command. If no
* dependency injector is specified, then the methods of the class will be
* registered to be called statically. Otherwise, new instances will be
* created of the command classes and methods will not be called statically.
*/
public void register(Class<?> clazz) {
registerMethods(clazz, null);
}
/*
* Register the methods of a class. This will automatically construct
* instances as necessary.
*/
private void registerMethods(Class<?> clazz, Method parent) {
Object obj = injector.getInstance(clazz);
registerMethods(clazz, parent, obj);
}
// Register the methods of a class.
private void registerMethods(Class<?> clazz, Method parent, Object obj) {
Map<CommandIdentifier, Method> map;
// Make a new hash map to cache the commands for this class
// as looking up methods via reflection is fairly slow
if (commands.containsKey(parent))
map = commands.get(parent);
else {
map = new HashMap<CommandIdentifier, Method>();
commands.put(parent, map);
}
for (Method method : clazz.getMethods()) {
if (!method.isAnnotationPresent(Command.class))
continue;
boolean isStatic = Modifier.isStatic(method.getModifiers());
Command cmd = method.getAnnotation(Command.class);
String[] modifiers = cmd.modifiers();
// Cache the aliases too
for (String alias : cmd.aliases())
for (String modifier : modifiers)
map.put(new CommandIdentifier(alias, modifier), method);
Requirements cmdRequirements = null;
if (method.getDeclaringClass().isAnnotationPresent(Requirements.class))
cmdRequirements = method.getDeclaringClass().getAnnotation(Requirements.class);
if (method.isAnnotationPresent(Requirements.class))
cmdRequirements = method.getAnnotation(Requirements.class);
if (requirements != null)
requirements.put(method, cmdRequirements);
ServerCommand serverCommand = null;
if (method.isAnnotationPresent(ServerCommand.class))
serverCommand = method.getAnnotation(ServerCommand.class);
if (serverCommand != null)
serverCommands.put(method, serverCommand);
// We want to be able invoke with an instance
if (!isStatic) {
// Can't register this command if we don't have an instance
if (obj == null)
continue;
instances.put(method, obj);
}
// Build a list of commands and their usage details, at least for
// root level commands
if (parent == null)
if (cmd.usage().length() == 0)
descs.put(new CommandIdentifier(cmd.aliases()[0], cmd.modifiers()[0]), cmd.desc());
else
descs.put(new CommandIdentifier(cmd.aliases()[0], cmd.modifiers()[0]),
cmd.usage() + " - " + cmd.desc());
// Look for nested commands -- if there are any, those have
// to be cached too so that they can be quickly looked
// up when processing commands
if (method.isAnnotationPresent(NestedCommand.class)) {
NestedCommand nestedCmd = method.getAnnotation(NestedCommand.class);
for (Class<?> nestedCls : nestedCmd.value())
registerMethods(nestedCls, method);
}
}
}
public static String joinString(Collection<?> str, String delimiter, int initialIndex) {
if (str.size() == 0)
return "";
StringBuilder buffer = new StringBuilder();
int i = 0;
for (Object o : str) {
if (i >= initialIndex) {
if (i > 0)
buffer.append(delimiter);
buffer.append(o.toString());
}
i++;
}
return buffer.toString();
}
}

View File

@ -26,15 +26,15 @@ public @interface Command {
String[] aliases();
String usage() default "";
String desc();
String[] modifiers() default "";
int min() default 0;
String flags() default "";
int max() default -1;
String flags() default "";
int min() default 0;
String[] modifiers() default "";
String usage() default "";
}

View File

@ -6,7 +6,7 @@ import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface Requirements {
boolean selected() default false;
boolean ownership() default false;
boolean selected() default false;
}

View File

@ -32,13 +32,8 @@ public class NPCCommands {
this.characterManager = characterManager;
}
@Command(
aliases = { "npc" },
usage = "create [name] [type] (character)",
desc = "Create a new NPC",
modifiers = { "create" },
min = 3,
max = 4)
@Command(aliases = { "npc" }, usage = "create [name] [type] (character)", desc = "Create a new NPC",
modifiers = { "create" }, min = 3, max = 4)
@Permission("npc.create")
@Requirements
public void createNPC(CommandContext args, Player player, NPC npc) {
@ -49,7 +44,7 @@ public class NPCCommands {
Messaging.sendError(player, "'" + args.getString(2) + "' is not a valid mob type. Using default NPC.");
}
CitizensNPC create = (CitizensNPC) npcManager.createNPC(type, args.getString(1));
NPC create = npcManager.createNPC(type, args.getString(1));
String successMsg = ChatColor.GREEN + "You created " + StringHelper.wrap(create.getName());
boolean success = true;
if (args.argsLength() == 4) {
@ -77,13 +72,8 @@ public class NPCCommands {
Messaging.send(player, successMsg);
}
@Command(
aliases = { "npc" },
usage = "despawn",
desc = "Despawn an NPC",
modifiers = { "despawn" },
min = 1,
max = 1)
@Command(aliases = { "npc" }, usage = "despawn", desc = "Despawn an NPC", modifiers = { "despawn" }, min = 1,
max = 1)
@Permission("npc.despawn")
public void despawnNPC(CommandContext args, Player player, NPC npc) {
npc.getTrait(Spawned.class).setSpawned(false);
@ -91,13 +81,26 @@ public class NPCCommands {
Messaging.send(player, ChatColor.GREEN + "You despawned " + StringHelper.wrap(npc.getName()) + ".");
}
@Command(
aliases = { "npc" },
usage = "spawn [id]",
desc = "Spawn an existing NPC",
modifiers = { "spawn" },
min = 2,
max = 2)
@Command(aliases = { "npc" }, usage = "select [id]", desc = "Select an NPC", modifiers = { "select" }, min = 2,
max = 2)
@Permission("npc.select")
@Requirements(ownership = true)
public void selectNPC(CommandContext args, Player player, NPC npc) {
NPC toSelect = npcManager.getNPC(args.getInteger(1));
if (toSelect == null || !toSelect.getTrait(Spawned.class).shouldSpawn()) {
Messaging.sendError(player, "No NPC with the ID '" + args.getInteger(1) + "' is spawned.");
return;
}
if (npc != null && toSelect.getId() == npc.getId()) {
Messaging.sendError(player, "You already have that NPC selected.");
return;
}
npcManager.selectNPC(player, toSelect);
Messaging.sendWithNPC(player, Setting.SELECTION_MESSAGE.asString(), toSelect);
}
@Command(aliases = { "npc" }, usage = "spawn [id]", desc = "Spawn an existing NPC", modifiers = { "spawn" },
min = 2, max = 2)
@Permission("npc.spawn")
@Requirements
public void spawnNPC(CommandContext args, Player player, NPC npc) {
@ -124,62 +127,24 @@ public class NPCCommands {
+ " is already spawned at another location. Use '/npc tphere' to teleport the NPC to your location.");
}
@Command(
aliases = { "npc" },
usage = "select [id]",
desc = "Select an NPC",
modifiers = { "select" },
min = 2,
max = 2)
@Permission("npc.select")
@Requirements(ownership = true)
public void selectNPC(CommandContext args, Player player, NPC npc) {
NPC toSelect = npcManager.getNPC(args.getInteger(1));
if (toSelect == null || !toSelect.getTrait(Spawned.class).shouldSpawn()) {
Messaging.sendError(player, "No NPC with the ID '" + args.getInteger(1) + "' is spawned.");
return;
}
if (npc != null && toSelect.getId() == npc.getId()) {
Messaging.sendError(player, "You already have that NPC selected.");
return;
}
npcManager.selectNPC(player, toSelect);
Messaging.sendWithNPC(player, Setting.SELECTION_MESSAGE.asString(), toSelect);
}
@Command(
aliases = { "npc" },
usage = "tphere",
desc = "Teleport an NPC to your location",
modifiers = { "tphere" },
min = 1,
max = 1)
@Command(aliases = { "npc" }, usage = "tphere", desc = "Teleport an NPC to your location",
modifiers = { "tphere" }, min = 1, max = 1)
@Permission("npc.tphere")
public void teleportNPCToPlayer(CommandContext args, Player player, NPC npc) {
npc.getBukkitEntity().teleport(player, TeleportCause.COMMAND);
Messaging.send(player, StringHelper.wrap(npc.getName()) + " was teleported to your location.");
}
@Command(
aliases = { "npc" },
usage = "tp",
desc = "Teleport to an NPC",
modifiers = { "tp", "teleport" },
min = 1,
max = 1)
@Command(aliases = { "npc" }, usage = "tp", desc = "Teleport to an NPC", modifiers = { "tp", "teleport" }, min = 1,
max = 1)
@Permission("npc.tp")
public void teleportToNPC(CommandContext args, Player player, NPC npc) {
player.teleport(npc.getBukkitEntity(), TeleportCause.COMMAND);
Messaging.send(player, ChatColor.GREEN + "You teleported to " + StringHelper.wrap(npc.getName()) + ".");
}
@Command(
aliases = { "npc" },
usage = "lookclose",
desc = "Toggle an NPC's look-close state",
modifiers = { "lookclose", "look", "rotate" },
min = 1,
max = 1)
@Command(aliases = { "npc" }, usage = "lookclose", desc = "Toggle an NPC's look-close state", modifiers = {
"lookclose", "look", "rotate" }, min = 1, max = 1)
@Permission("npc.look-close")
public void toggleNPCLookClose(CommandContext args, Player player, NPC npc) {
npc.getTrait(LookClose.class).setLookClose(!npc.getTrait(LookClose.class).shouldLookClose());

View File

@ -20,8 +20,6 @@
package net.citizensnpcs.command.exception;
public class CommandException extends Exception {
private static final long serialVersionUID = 870638193072101739L;
public CommandException() {
super();
}
@ -33,4 +31,6 @@ public class CommandException extends Exception {
public CommandException(Throwable t) {
super(t);
}
private static final long serialVersionUID = 870638193072101739L;
}

View File

@ -20,9 +20,9 @@
package net.citizensnpcs.command.exception;
public class MissingNestedCommandException extends CommandUsageException {
private static final long serialVersionUID = -4382896182979285355L;
public MissingNestedCommandException(String message, String usage) {
super(message, usage);
}
private static final long serialVersionUID = -4382896182979285355L;
}

View File

@ -2,9 +2,9 @@ package net.citizensnpcs.command.exception;
public class RequirementMissingException extends CommandException {
private static final long serialVersionUID = -4299721983654504028L;
public RequirementMissingException(String message) {
super(message);
}
private static final long serialVersionUID = -4299721983654504028L;
}

View File

@ -20,9 +20,9 @@
package net.citizensnpcs.command.exception;
public class WrappedCommandException extends CommandException {
private static final long serialVersionUID = -4075721444847778918L;
public WrappedCommandException(Throwable t) {
super(t);
}
private static final long serialVersionUID = -4075721444847778918L;
}

View File

@ -1,8 +1,5 @@
package net.citizensnpcs.npc;
import java.lang.reflect.Field;
import java.util.Map;
import net.citizensnpcs.api.event.NPCDespawnEvent;
import net.citizensnpcs.api.event.NPCSpawnEvent;
import net.citizensnpcs.api.npc.AbstractNPC;
@ -12,39 +9,23 @@ import net.citizensnpcs.api.npc.trait.trait.Spawned;
import net.citizensnpcs.npc.ai.CitizensNavigator;
import net.citizensnpcs.trait.LookClose;
import net.citizensnpcs.util.Messaging;
import net.minecraft.server.EntityLiving;
import net.minecraft.server.EntityTypes;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.WorldServer;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.World;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.craftbukkit.CraftWorld;
import org.bukkit.entity.Entity;
public class CitizensNPC extends AbstractNPC {
protected EntityLiving mcEntity;
public abstract class CitizensNPC extends AbstractNPC {
private static final double lookRange = 5;
protected final CitizensNPCManager manager;
protected net.minecraft.server.Entity mcEntity;
private Map<Integer, Class<? extends net.minecraft.server.Entity>> intToClass;
private Map<Class<? extends net.minecraft.server.Entity>, Integer> classToInt;
private Class<? extends EntityLiving> entityClass;
private double lookRange = 5;
protected CitizensNPC(CitizensNPCManager manager, int id, String name, Class<? extends EntityLiving> entityClass) {
protected CitizensNPC(CitizensNPCManager manager, int id, String name) {
super(id, name);
this.manager = manager;
if (intToClass == null || classToInt == null)
createMaps();
this.entityClass = entityClass;
// No need to register entity class for normal human NPC entity
if (entityClass != null)
registerEntityClass(entityClass);
}
protected abstract net.minecraft.server.Entity createHandle(Location loc);
@Override
public boolean despawn() {
if (!isSpawned()) {
@ -60,12 +41,35 @@ public class CitizensNPC extends AbstractNPC {
return true;
}
// TODO: is this necessary? it's a helper method...
protected void faceEntity(Location target) {
if (getBukkitEntity().getWorld() != target.getWorld())
return;
Location loc = getBukkitEntity().getLocation();
double xDiff = target.getX() - loc.getX();
double yDiff = target.getY() - loc.getY();
double zDiff = target.getZ() - loc.getZ();
double distanceXZ = Math.sqrt(xDiff * xDiff + zDiff * zDiff);
double distanceY = Math.sqrt(distanceXZ * distanceXZ + yDiff * yDiff);
double yaw = (Math.acos(xDiff / distanceXZ) * 180 / Math.PI);
double pitch = (Math.acos(yDiff / distanceY) * 180 / Math.PI) - 90;
if (zDiff < 0.0) {
yaw = yaw + (Math.abs(180 - yaw) * 2);
}
mcEntity.yaw = (float) yaw - 90;
mcEntity.pitch = (float) pitch;
}
@Override
public Entity getBukkitEntity() {
return getHandle().getBukkitEntity();
}
public EntityLiving getHandle() {
public net.minecraft.server.Entity getHandle() {
return mcEntity;
}
@ -98,11 +102,8 @@ public class CitizensNPC extends AbstractNPC {
if (spawnEvent.isCancelled())
return false;
if (entityClass != null) {
mcEntity = createEntityFromClass(getWorldServer(loc.getWorld()));
mcEntity.setPosition(loc.getX(), loc.getY(), loc.getZ());
mcEntity.world.addEntity(mcEntity);
}
mcEntity = createHandle(loc);
mcEntity.world.addEntity(mcEntity);
// Set the location
addTrait(new SpawnLocation(loc));
@ -111,79 +112,13 @@ public class CitizensNPC extends AbstractNPC {
return true;
}
protected WorldServer getWorldServer(World world) {
return ((CraftWorld) world).getHandle();
}
protected MinecraftServer getMinecraftServer(Server server) {
return ((CraftServer) server).getServer();
}
public void tick() {
if (mcEntity != null)
// TODO: this needs to be less hard-coded... does everyone want this
// behaviour?
if (mcEntity != null) {
if (getTrait(LookClose.class).shouldLookClose()
&& mcEntity.world.findNearbyPlayer(mcEntity, lookRange) != null)
faceEntity(mcEntity.world.findNearbyPlayer(mcEntity, lookRange).getBukkitEntity());
}
protected void faceEntity(Entity target) {
if (getBukkitEntity().getWorld() != target.getWorld())
return;
Location loc = getBukkitEntity().getLocation();
Location targetLoc = target.getLocation();
double xDiff = targetLoc.getX() - loc.getX();
double yDiff = targetLoc.getY() - loc.getY();
double zDiff = targetLoc.getZ() - loc.getZ();
double distanceXZ = Math.sqrt(xDiff * xDiff + zDiff * zDiff);
double distanceY = Math.sqrt(distanceXZ * distanceXZ + yDiff * yDiff);
double yaw = (Math.acos(xDiff / distanceXZ) * 180 / Math.PI);
double pitch = (Math.acos(yDiff / distanceY) * 180 / Math.PI) - 90;
if (zDiff < 0.0) {
yaw = yaw + (Math.abs(180 - yaw) * 2);
}
mcEntity.yaw = (float) yaw - 90;
mcEntity.pitch = (float) pitch;
}
private void registerEntityClass(Class<? extends net.minecraft.server.Entity> clazz) {
if (classToInt == null || intToClass == null)
return;
Class<?> search = clazz;
while ((search = search.getSuperclass()) != null && net.minecraft.server.Entity.class.isAssignableFrom(search)) {
if (!classToInt.containsKey(search))
continue;
int code = classToInt.get(search);
intToClass.put(code, clazz);
classToInt.put(clazz, code);
return;
}
throw new IllegalArgumentException("unable to find valid entity superclass");
}
@SuppressWarnings("unchecked")
private void createMaps() {
try {
Field field = EntityTypes.class.getDeclaredField("d");
field.setAccessible(true);
intToClass = (Map<Integer, Class<? extends net.minecraft.server.Entity>>) field.get(null);
field = EntityTypes.class.getDeclaredField("e");
field.setAccessible(true);
classToInt = (Map<Class<? extends net.minecraft.server.Entity>, Integer>) field.get(null);
} catch (Exception ex) {
ex.printStackTrace();
}
}
private EntityLiving createEntityFromClass(net.minecraft.server.World world) {
try {
return entityClass.getConstructor(net.minecraft.server.World.class).newInstance(world);
} catch (Exception ex) {
ex.printStackTrace();
return null;
faceEntity(mcEntity.world.findNearbyPlayer(mcEntity, lookRange).getBukkitEntity().getLocation());
}
}
}

View File

@ -9,7 +9,6 @@ import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.npc.NPCManager;
import net.citizensnpcs.api.npc.trait.Character;
import net.citizensnpcs.api.npc.trait.trait.SpawnLocation;
import net.citizensnpcs.storage.Storage;
import net.citizensnpcs.util.ByIdArray;
import net.citizensnpcs.util.NPCBuilder;
@ -31,16 +30,6 @@ public class CitizensNPCManager implements NPCManager {
this.saves = saves;
}
@Override
public NPC createNPC(CreatureType type, String name) {
return createNPC(type, name, null);
}
@Override
public NPC createNPC(CreatureType type, String name, Character character) {
return createNPC(type, generateUniqueId(), name, character);
}
public NPC createNPC(CreatureType type, int id, String name, Character character) {
if (npcs.contains(id))
throw new IllegalArgumentException("An NPC already has the ID '" + id + "'.");
@ -51,15 +40,27 @@ public class CitizensNPCManager implements NPCManager {
return npc;
}
@Override
public NPC createNPC(CreatureType type, String name) {
return createNPC(type, name, null);
}
@Override
public NPC createNPC(CreatureType type, String name, Character character) {
return createNPC(type, generateUniqueId(), name, character);
}
public void despawn(NPC npc) {
npc.getTrait(SpawnLocation.class).setLocation(npc.getBukkitEntity().getLocation());
selected.removeAll(npc.getId());
npc.getBukkitEntity().remove();
}
@Override
public Iterator<NPC> iterator() {
return npcs.iterator();
private int generateUniqueId() {
int count = 0;
while (getNPC(count++) != null)
;
return count - 1;
}
@Override
@ -84,11 +85,12 @@ public class CitizensNPCManager implements NPCManager {
return npcs;
}
private int generateUniqueId() {
int count = 0;
while (getNPC(count++) != null)
;
return count - 1;
public NPC getSelectedNPC(Player player) {
for (int id : selected.keySet()) {
if (selected.get(id).contains(player.getName()))
return getNPC(id);
}
return null;
}
@Override
@ -96,6 +98,17 @@ public class CitizensNPCManager implements NPCManager {
return getNPC(entity) != null;
}
@Override
public Iterator<NPC> iterator() {
return npcs.iterator();
}
public boolean npcIsSelectedByPlayer(Player player, NPC npc) {
if (!selected.containsKey(npc.getId()))
return false;
return selected.get(npc.getId()).contains(player.getName());
}
public void remove(NPC npc) {
if (npc.isSpawned())
despawn(npc);
@ -111,18 +124,4 @@ public class CitizensNPCManager implements NPCManager {
selected.get(existing.getId()).remove(player.getName());
selected.put(npc.getId(), player.getName());
}
public boolean npcIsSelectedByPlayer(Player player, NPC npc) {
if (!selected.containsKey(npc.getId()))
return false;
return selected.get(npc.getId()).contains(player.getName());
}
public NPC getSelectedNPC(Player player) {
for (int id : selected.keySet()) {
if (selected.get(id).contains(player.getName()))
return getNPC(id);
}
return null;
}
}

View File

@ -5,24 +5,33 @@ import net.citizensnpcs.api.npc.ai.NavigatorCallback;
import net.citizensnpcs.npc.CitizensNPC;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import org.bukkit.entity.LivingEntity;
public class CitizensNavigator implements Navigator {
private final CitizensNPC npc;
private PathStrategy executing;
public CitizensNavigator(CitizensNPC npc) {
this.npc = npc;
}
@Override
public void setDestination(Location destination) {
}
@Override
public void registerCallback(NavigatorCallback callback) {
}
public void update() {
if (executing != null) {
executing.update();
}
}
@Override
public void setTarget(Entity target, boolean aggressive) {
public void setDestination(Location destination) {
executing = new MoveStrategy(npc, destination);
}
@Override
public void setTarget(LivingEntity target, boolean aggressive) {
executing = new TargetStrategy(npc, target, aggressive);
}
}

View File

@ -0,0 +1,86 @@
package net.citizensnpcs.npc.ai;
import java.util.Random;
import net.citizensnpcs.npc.CitizensNPC;
import net.minecraft.server.EntityLiving;
import net.minecraft.server.MathHelper;
import net.minecraft.server.PathEntity;
import net.minecraft.server.Vec3D;
import org.bukkit.Location;
import org.bukkit.entity.Player;
public class MoveStrategy implements PathStrategy {
private final EntityLiving handle;
private final PathEntity path;
private final Random random = new Random();
public MoveStrategy(CitizensNPC handle, Location destination) {
this.handle = (EntityLiving) handle.getHandle();
this.path = this.handle.world.a(this.handle, destination.getBlockX(), destination.getBlockY(),
destination.getBlockZ(), 16F);
}
MoveStrategy(EntityLiving handle, PathEntity path) {
this.handle = handle;
this.path = path;
}
private Vec3D getVector() {
Vec3D vec3d = path.a(handle);
double lengthSq = (handle.width * 2.0F);
lengthSq *= lengthSq;
while (vec3d != null && vec3d.d(handle.locX, vec3d.b, handle.locZ) < lengthSq) {
this.path.a(); // Increment path index.
if (this.path.b()) { // finished.
return null;
} else {
vec3d = this.path.a(handle);
}
}
return vec3d;
}
@Override
public boolean update() {
if (handle.dead)
return true;
Vec3D vector = getVector();
if (vector == null)
return true;
int yHeight = MathHelper.floor(handle.boundingBox.b + 0.5D);
boolean inWater = ((Player) handle.getBukkitEntity()).getRemainingAir() < 20;
boolean onFire = handle.fireTicks > 0;
double diffX = vector.a - handle.locX;
double diffZ = vector.c - handle.locZ;
handle.yaw += getYawDifference(diffZ, diffX);
if (vector.b - yHeight > 0.0D) {
jump();
}
handle.d();
// handle.walk();
if (handle.positionChanged) {
jump();
}
if (random.nextFloat() < 0.8F && (inWater || onFire)) {
handle.motY += 0.04D;
}
return false;
}
private float getYawDifference(double diffZ, double diffX) {
float vectorYaw = (float) (Math.atan2(diffZ, diffX) * 180.0D / Math.PI) - 90.0F;
float diffYaw = (vectorYaw - handle.yaw) % 360;
return Math.max(-30F, Math.min(30, diffYaw));
}
private void jump() {
if (handle.onGround)
handle.motY = JUMP_VELOCITY;
}
private static final double JUMP_VELOCITY = 0.49D;
}

View File

@ -0,0 +1,5 @@
package net.citizensnpcs.npc.ai;
public interface PathStrategy {
boolean update();
}

View File

@ -0,0 +1,39 @@
package net.citizensnpcs.npc.ai;
import net.citizensnpcs.npc.CitizensNPC;
import net.minecraft.server.EntityHuman;
import net.minecraft.server.EntityLiving;
import net.minecraft.server.EntityMonster;
import org.bukkit.craftbukkit.entity.CraftLivingEntity;
import org.bukkit.entity.LivingEntity;
public class TargetStrategy implements PathStrategy {
private final EntityLiving handle, target;
private final boolean aggro;
private PathStrategy current = null;
public TargetStrategy(CitizensNPC handle, LivingEntity target, boolean aggro) {
this.handle = (EntityLiving) handle.getHandle();
this.target = ((CraftLivingEntity) target).getHandle();
this.aggro = aggro;
}
@Override
public boolean update() {
if (target == null || target.dead)
return true;
current = new MoveStrategy(handle, handle.world.findPath(handle, target, 16F));
// NPCManager.faceEntity(handle.getBukkitEntity(),
// target.getBukkitEntity());
if (aggro) {
if (handle instanceof EntityMonster) {
((EntityMonster) handle).d(target);
} else if (handle instanceof EntityHuman) {
((EntityHuman) handle).attack(target);
}
}
current.update();
return false;
}
}

View File

@ -1,12 +1,30 @@
package net.citizensnpcs.npc.entity;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.resource.lib.entity.EntityBlazeNPC;
import net.minecraft.server.EntityBlaze;
import net.minecraft.server.World;
public class CitizensBlazeNPC extends CitizensNPC {
import org.bukkit.entity.Blaze;
public class CitizensBlazeNPC extends CitizensMobNPC {
public CitizensBlazeNPC(CitizensNPCManager manager, int id, String name) {
super(manager, id, name, EntityBlazeNPC.class);
}
@Override
public Blaze getBukkitEntity() {
return (Blaze) getHandle().getBukkitEntity();
}
public static class EntityBlazeNPC extends EntityBlaze {
public EntityBlazeNPC(World world) {
super(world);
}
@Override
public void m_() {
}
}
}

View File

@ -1,12 +1,30 @@
package net.citizensnpcs.npc.entity;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.resource.lib.entity.EntityCaveSpiderNPC;
import net.minecraft.server.EntityCaveSpider;
import net.minecraft.server.World;
public class CitizensCaveSpiderNPC extends CitizensNPC {
import org.bukkit.entity.CaveSpider;
public class CitizensCaveSpiderNPC extends CitizensMobNPC {
public CitizensCaveSpiderNPC(CitizensNPCManager manager, int id, String name) {
super(manager, id, name, EntityCaveSpiderNPC.class);
}
@Override
public CaveSpider getBukkitEntity() {
return (CaveSpider) getHandle().getBukkitEntity();
}
public static class EntityCaveSpiderNPC extends EntityCaveSpider {
public EntityCaveSpiderNPC(World world) {
super(world);
}
@Override
public void m_() {
}
}
}

View File

@ -1,12 +1,30 @@
package net.citizensnpcs.npc.entity;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.resource.lib.entity.EntityChickenNPC;
import net.minecraft.server.EntityChicken;
import net.minecraft.server.World;
public class CitizensChickenNPC extends CitizensNPC {
import org.bukkit.entity.Chicken;
public class CitizensChickenNPC extends CitizensMobNPC {
public CitizensChickenNPC(CitizensNPCManager manager, int id, String name) {
super(manager, id, name, EntityChickenNPC.class);
}
@Override
public Chicken getBukkitEntity() {
return (Chicken) getHandle().getBukkitEntity();
}
public static class EntityChickenNPC extends EntityChicken {
public EntityChickenNPC(World world) {
super(world);
}
@Override
public void m_() {
}
}
}

View File

@ -1,12 +1,30 @@
package net.citizensnpcs.npc.entity;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.resource.lib.entity.EntityCowNPC;
import net.minecraft.server.EntityCow;
import net.minecraft.server.World;
public class CitizensCowNPC extends CitizensNPC {
import org.bukkit.entity.Cow;
public class CitizensCowNPC extends CitizensMobNPC {
public CitizensCowNPC(CitizensNPCManager manager, int id, String name) {
super(manager, id, name, EntityCowNPC.class);
}
@Override
public Cow getBukkitEntity() {
return (Cow) getHandle().getBukkitEntity();
}
public static class EntityCowNPC extends EntityCow {
public EntityCowNPC(World world) {
super(world);
}
@Override
public void m_() {
}
}
}

View File

@ -1,12 +1,30 @@
package net.citizensnpcs.npc.entity;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.resource.lib.entity.EntityCreeperNPC;
import net.minecraft.server.EntityCreeper;
import net.minecraft.server.World;
public class CitizensCreeperNPC extends CitizensNPC {
import org.bukkit.entity.Creeper;
public class CitizensCreeperNPC extends CitizensMobNPC {
public CitizensCreeperNPC(CitizensNPCManager manager, int id, String name) {
super(manager, id, name, EntityCreeperNPC.class);
}
@Override
public Creeper getBukkitEntity() {
return (Creeper) getHandle().getBukkitEntity();
}
public static class EntityCreeperNPC extends EntityCreeper {
public EntityCreeperNPC(World world) {
super(world);
}
@Override
public void m_() {
}
}
}

View File

@ -1,12 +1,30 @@
package net.citizensnpcs.npc.entity;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.resource.lib.entity.EntityEnderDragonNPC;
import net.minecraft.server.EntityEnderDragon;
import net.minecraft.server.World;
public class CitizensEnderDragonNPC extends CitizensNPC {
import org.bukkit.entity.EnderDragon;
public class CitizensEnderDragonNPC extends CitizensMobNPC {
public CitizensEnderDragonNPC(CitizensNPCManager manager, int id, String name) {
super(manager, id, name, EntityEnderDragonNPC.class);
}
@Override
public EnderDragon getBukkitEntity() {
return (EnderDragon) getHandle().getBukkitEntity();
}
public static class EntityEnderDragonNPC extends EntityEnderDragon {
public EntityEnderDragonNPC(World world) {
super(world);
}
@Override
public void m_() {
}
}
}

View File

@ -1,12 +1,30 @@
package net.citizensnpcs.npc.entity;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.resource.lib.entity.EntityEndermanNPC;
import net.minecraft.server.EntityEnderman;
import net.minecraft.server.World;
public class CitizensEndermanNPC extends CitizensNPC {
import org.bukkit.entity.Enderman;
public class CitizensEndermanNPC extends CitizensMobNPC {
public CitizensEndermanNPC(CitizensNPCManager manager, int id, String name) {
super(manager, id, name, EntityEndermanNPC.class);
}
@Override
public Enderman getBukkitEntity() {
return (Enderman) getHandle().getBukkitEntity();
}
public static class EntityEndermanNPC extends EntityEnderman {
public EntityEndermanNPC(World world) {
super(world);
}
@Override
public void m_() {
}
}
}

View File

@ -1,12 +1,30 @@
package net.citizensnpcs.npc.entity;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.resource.lib.entity.EntityGhastNPC;
import net.minecraft.server.EntityGhast;
import net.minecraft.server.World;
public class CitizensGhastNPC extends CitizensNPC {
import org.bukkit.entity.Ghast;
public class CitizensGhastNPC extends CitizensMobNPC {
public CitizensGhastNPC(CitizensNPCManager manager, int id, String name) {
super(manager, id, name, EntityGhastNPC.class);
}
@Override
public Ghast getBukkitEntity() {
return (Ghast) getHandle().getBukkitEntity();
}
public static class EntityGhastNPC extends EntityGhast {
public EntityGhastNPC(World world) {
super(world);
}
@Override
public void m_() {
}
}
}

View File

@ -1,12 +1,30 @@
package net.citizensnpcs.npc.entity;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.resource.lib.entity.EntityGiantNPC;
import net.minecraft.server.EntityGiantZombie;
import net.minecraft.server.World;
public class CitizensGiantNPC extends CitizensNPC {
import org.bukkit.entity.Giant;
public class CitizensGiantNPC extends CitizensMobNPC {
public CitizensGiantNPC(CitizensNPCManager manager, int id, String name) {
super(manager, id, name, EntityGiantNPC.class);
}
@Override
public Giant getBukkitEntity() {
return (Giant) getHandle().getBukkitEntity();
}
public static class EntityGiantNPC extends EntityGiantZombie {
public EntityGiantNPC(World world) {
super(world);
}
@Override
public void m_() {
}
}
}

View File

@ -3,30 +3,38 @@ package net.citizensnpcs.npc.entity;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.resource.lib.entity.EntityHumanNPC;
import net.minecraft.server.EntityLiving;
import net.minecraft.server.ItemInWorldManager;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.WorldServer;
import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.craftbukkit.CraftWorld;
import org.bukkit.entity.Player;
public class CitizensHumanNPC extends CitizensNPC {
public CitizensHumanNPC(CitizensNPCManager manager, int id, String name) {
super(manager, id, name, null);
super(manager, id, name);
}
@Override
public boolean spawn(Location loc) {
if (super.spawn(loc)) {
WorldServer ws = getWorldServer(loc.getWorld());
mcEntity = new EntityHumanNPC(getMinecraftServer(ws.getServer()), ws, getFullName(),
new ItemInWorldManager(ws));
((EntityHumanNPC) mcEntity).removeFromPlayerMap(getFullName());
mcEntity.setPositionRotation(loc.getX(), loc.getY(), loc.getZ(), loc.getYaw(), loc.getPitch());
ws.addEntity(mcEntity);
ws.players.remove(mcEntity);
return true;
}
return false;
public Player getBukkitEntity() {
return (Player) getHandle().getBukkitEntity();
}
protected static MinecraftServer getMinecraftServer(Server server) {
return ((CraftServer) server).getServer();
}
@Override
protected EntityLiving createHandle(Location loc) {
WorldServer ws = ((CraftWorld) loc.getWorld()).getHandle();
EntityHumanNPC handle = new EntityHumanNPC(ws.getServer().getServer(), ws, getFullName(),
new ItemInWorldManager(ws));
handle.removeFromPlayerMap(getFullName());
handle.setPositionRotation(loc.getX(), loc.getY(), loc.getZ(), loc.getYaw(), loc.getPitch());
return handle;
}
}

View File

@ -1,12 +1,30 @@
package net.citizensnpcs.npc.entity;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.resource.lib.entity.EntityMagmaCubeNPC;
import net.minecraft.server.EntityMagmaCube;
import net.minecraft.server.World;
public class CitizensMagmaCubeNPC extends CitizensNPC {
import org.bukkit.entity.MagmaCube;
public class CitizensMagmaCubeNPC extends CitizensMobNPC {
public CitizensMagmaCubeNPC(CitizensNPCManager manager, int id, String name) {
super(manager, id, name, EntityMagmaCubeNPC.class);
}
@Override
public MagmaCube getBukkitEntity() {
return (MagmaCube) getHandle().getBukkitEntity();
}
public static class EntityMagmaCubeNPC extends EntityMagmaCube {
public EntityMagmaCubeNPC(World world) {
super(world);
}
@Override
public void m_() {
}
}
}

View File

@ -0,0 +1,83 @@
package net.citizensnpcs.npc.entity;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.util.Map;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.minecraft.server.Entity;
import net.minecraft.server.EntityLiving;
import net.minecraft.server.EntityTypes;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.craftbukkit.CraftWorld;
import org.bukkit.entity.LivingEntity;
public abstract class CitizensMobNPC extends CitizensNPC {
private final Constructor<? extends EntityLiving> constructor;
protected CitizensMobNPC(CitizensNPCManager manager, int id, String name, Class<? extends EntityLiving> clazz) {
super(manager, id, name);
try {
this.constructor = clazz.getConstructor(World.class);
} catch (Exception ex) {
throw new IllegalStateException("unable to find an entity constructor");
}
if (!classToInt.containsKey(clazz))
registerEntityClass(clazz);
}
private EntityLiving createEntityFromClass(net.minecraft.server.World world) {
try {
return constructor.newInstance(world);
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
@Override
protected EntityLiving createHandle(Location loc) {
EntityLiving entity = createEntityFromClass(((CraftWorld) loc.getWorld()).getHandle());
mcEntity.setPositionRotation(loc.getX(), loc.getY(), loc.getZ(), loc.getYaw(), loc.getPitch());
return entity;
}
@Override
public LivingEntity getBukkitEntity() {
return (LivingEntity) getHandle().getBukkitEntity();
}
private static Map<Class<? extends Entity>, Integer> classToInt;
private static Map<Integer, Class<? extends Entity>> intToClass;
private static void registerEntityClass(Class<? extends Entity> clazz) {
Class<?> search = clazz;
while ((search = search.getSuperclass()) != null && Entity.class.isAssignableFrom(search)) {
if (!classToInt.containsKey(search))
continue;
int code = classToInt.get(search);
intToClass.put(code, clazz);
classToInt.put(clazz, code);
return;
}
throw new IllegalArgumentException("unable to find valid entity superclass");
}
static {
try {
Field field = EntityTypes.class.getDeclaredField("d");
field.setAccessible(true);
intToClass = (Map<Integer, Class<? extends Entity>>) field.get(null);
field = EntityTypes.class.getDeclaredField("e");
field.setAccessible(true);
classToInt = (Map<Class<? extends Entity>, Integer>) field.get(null);
} catch (Exception ex) {
ex.printStackTrace();
throw new IllegalStateException(
"Unable to fetch entity class mapping - is Citizens updated for this version of CraftBukkit?");
}
}
}

View File

@ -1,12 +1,30 @@
package net.citizensnpcs.npc.entity;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.resource.lib.entity.EntityMushroomCowNPC;
import net.minecraft.server.EntityMushroomCow;
import net.minecraft.server.World;
public class CitizensMushroomCowNPC extends CitizensNPC {
import org.bukkit.entity.MushroomCow;
public class CitizensMushroomCowNPC extends CitizensMobNPC {
public CitizensMushroomCowNPC(CitizensNPCManager manager, int id, String name) {
super(manager, id, name, EntityMushroomCowNPC.class);
}
@Override
public MushroomCow getBukkitEntity() {
return (MushroomCow) getHandle().getBukkitEntity();
}
public static class EntityMushroomCowNPC extends EntityMushroomCow {
public EntityMushroomCowNPC(World world) {
super(world);
}
@Override
public void m_() {
}
}
}

View File

@ -1,12 +1,30 @@
package net.citizensnpcs.npc.entity;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.resource.lib.entity.EntityPigNPC;
import net.minecraft.server.EntityPig;
import net.minecraft.server.World;
public class CitizensPigNPC extends CitizensNPC {
import org.bukkit.entity.Pig;
public class CitizensPigNPC extends CitizensMobNPC {
public CitizensPigNPC(CitizensNPCManager manager, int id, String name) {
super(manager, id, name, EntityPigNPC.class);
}
@Override
public Pig getBukkitEntity() {
return (Pig) getHandle().getBukkitEntity();
}
public static class EntityPigNPC extends EntityPig {
public EntityPigNPC(World world) {
super(world);
}
@Override
public void m_() {
}
}
}

View File

@ -1,12 +1,30 @@
package net.citizensnpcs.npc.entity;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.resource.lib.entity.EntityPigZombieNPC;
import net.minecraft.server.EntityPigZombie;
import net.minecraft.server.World;
public class CitizensPigZombieNPC extends CitizensNPC {
import org.bukkit.entity.PigZombie;
public class CitizensPigZombieNPC extends CitizensMobNPC {
public CitizensPigZombieNPC(CitizensNPCManager manager, int id, String name) {
super(manager, id, name, EntityPigZombieNPC.class);
}
@Override
public PigZombie getBukkitEntity() {
return (PigZombie) getHandle().getBukkitEntity();
}
public static class EntityPigZombieNPC extends EntityPigZombie {
public EntityPigZombieNPC(World world) {
super(world);
}
@Override
public void m_() {
}
}
}

View File

@ -1,12 +1,30 @@
package net.citizensnpcs.npc.entity;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.resource.lib.entity.EntitySheepNPC;
import net.minecraft.server.EntitySheep;
import net.minecraft.server.World;
public class CitizensSheepNPC extends CitizensNPC {
import org.bukkit.entity.Sheep;
public class CitizensSheepNPC extends CitizensMobNPC {
public CitizensSheepNPC(CitizensNPCManager manager, int id, String name) {
super(manager, id, name, EntitySheepNPC.class);
}
@Override
public Sheep getBukkitEntity() {
return (Sheep) getHandle().getBukkitEntity();
}
public static class EntitySheepNPC extends EntitySheep {
public EntitySheepNPC(World world) {
super(world);
}
@Override
public void m_() {
}
}
}

View File

@ -1,12 +1,30 @@
package net.citizensnpcs.npc.entity;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.resource.lib.entity.EntitySilverfishNPC;
import net.minecraft.server.EntitySilverfish;
import net.minecraft.server.World;
public class CitizensSilverfishNPC extends CitizensNPC {
import org.bukkit.entity.Silverfish;
public class CitizensSilverfishNPC extends CitizensMobNPC {
public CitizensSilverfishNPC(CitizensNPCManager manager, int id, String name) {
super(manager, id, name, EntitySilverfishNPC.class);
}
@Override
public Silverfish getBukkitEntity() {
return (Silverfish) getHandle().getBukkitEntity();
}
public static class EntitySilverfishNPC extends EntitySilverfish {
public EntitySilverfishNPC(World world) {
super(world);
}
@Override
public void m_() {
}
}
}

View File

@ -1,12 +1,30 @@
package net.citizensnpcs.npc.entity;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.resource.lib.entity.EntitySkeletonNPC;
import net.minecraft.server.EntitySkeleton;
import net.minecraft.server.World;
public class CitizensSkeletonNPC extends CitizensNPC {
import org.bukkit.entity.Skeleton;
public class CitizensSkeletonNPC extends CitizensMobNPC {
public CitizensSkeletonNPC(CitizensNPCManager manager, int id, String name) {
super(manager, id, name, EntitySkeletonNPC.class);
}
@Override
public Skeleton getBukkitEntity() {
return (Skeleton) getHandle().getBukkitEntity();
}
public static class EntitySkeletonNPC extends EntitySkeleton {
public EntitySkeletonNPC(World world) {
super(world);
}
@Override
public void m_() {
}
}
}

View File

@ -1,12 +1,30 @@
package net.citizensnpcs.npc.entity;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.resource.lib.entity.EntitySlimeNPC;
import net.minecraft.server.EntitySlime;
import net.minecraft.server.World;
public class CitizensSlimeNPC extends CitizensNPC {
import org.bukkit.entity.Slime;
public class CitizensSlimeNPC extends CitizensMobNPC {
public CitizensSlimeNPC(CitizensNPCManager manager, int id, String name) {
super(manager, id, name, EntitySlimeNPC.class);
}
@Override
public Slime getBukkitEntity() {
return (Slime) getHandle().getBukkitEntity();
}
public static class EntitySlimeNPC extends EntitySlime {
public EntitySlimeNPC(World world) {
super(world);
}
@Override
public void m_() {
}
}
}

View File

@ -1,12 +1,30 @@
package net.citizensnpcs.npc.entity;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.resource.lib.entity.EntitySnowmanNPC;
import net.minecraft.server.EntitySnowman;
import net.minecraft.server.World;
public class CitizensSnowmanNPC extends CitizensNPC {
import org.bukkit.entity.Snowman;
public class CitizensSnowmanNPC extends CitizensMobNPC {
public CitizensSnowmanNPC(CitizensNPCManager manager, int id, String name) {
super(manager, id, name, EntitySnowmanNPC.class);
}
@Override
public Snowman getBukkitEntity() {
return (Snowman) getHandle().getBukkitEntity();
}
public static class EntitySnowmanNPC extends EntitySnowman {
public EntitySnowmanNPC(World world) {
super(world);
}
@Override
public void m_() {
}
}
}

View File

@ -1,12 +1,30 @@
package net.citizensnpcs.npc.entity;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.resource.lib.entity.EntitySpiderNPC;
import net.minecraft.server.EntitySpider;
import net.minecraft.server.World;
public class CitizensSpiderNPC extends CitizensNPC {
import org.bukkit.entity.Spider;
public class CitizensSpiderNPC extends CitizensMobNPC {
public CitizensSpiderNPC(CitizensNPCManager manager, int id, String name) {
super(manager, id, name, EntitySpiderNPC.class);
}
@Override
public Spider getBukkitEntity() {
return (Spider) getHandle().getBukkitEntity();
}
public static class EntitySpiderNPC extends EntitySpider {
public EntitySpiderNPC(World world) {
super(world);
}
@Override
public void m_() {
}
}
}

View File

@ -1,12 +1,30 @@
package net.citizensnpcs.npc.entity;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.resource.lib.entity.EntitySquidNPC;
import net.minecraft.server.EntitySquid;
import net.minecraft.server.World;
public class CitizensSquidNPC extends CitizensNPC {
import org.bukkit.entity.Squid;
public class CitizensSquidNPC extends CitizensMobNPC {
public CitizensSquidNPC(CitizensNPCManager manager, int id, String name) {
super(manager, id, name, EntitySquidNPC.class);
}
@Override
public Squid getBukkitEntity() {
return (Squid) getHandle().getBukkitEntity();
}
public static class EntitySquidNPC extends EntitySquid {
public EntitySquidNPC(World world) {
super(world);
}
@Override
public void m_() {
}
}
}

View File

@ -1,12 +1,30 @@
package net.citizensnpcs.npc.entity;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.resource.lib.entity.EntityVillagerNPC;
import net.minecraft.server.EntityVillager;
import net.minecraft.server.World;
public class CitizensVillagerNPC extends CitizensNPC {
import org.bukkit.entity.Villager;
public class CitizensVillagerNPC extends CitizensMobNPC {
public CitizensVillagerNPC(CitizensNPCManager manager, int id, String name) {
super(manager, id, name, EntityVillagerNPC.class);
}
@Override
public Villager getBukkitEntity() {
return (Villager) getHandle().getBukkitEntity();
}
public static class EntityVillagerNPC extends EntityVillager {
public EntityVillagerNPC(World world) {
super(world);
}
@Override
public void m_() {
}
}
}

View File

@ -1,12 +1,30 @@
package net.citizensnpcs.npc.entity;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.resource.lib.entity.EntityWolfNPC;
import net.minecraft.server.EntityWolf;
import net.minecraft.server.World;
public class CitizensWolfNPC extends CitizensNPC {
import org.bukkit.entity.Wolf;
public class CitizensWolfNPC extends CitizensMobNPC {
public CitizensWolfNPC(CitizensNPCManager manager, int id, String name) {
super(manager, id, name, EntityWolfNPC.class);
}
@Override
public Wolf getBukkitEntity() {
return (Wolf) getHandle().getBukkitEntity();
}
public static class EntityWolfNPC extends EntityWolf {
public EntityWolfNPC(World world) {
super(world);
}
@Override
public void m_() {
}
}
}

View File

@ -1,12 +1,30 @@
package net.citizensnpcs.npc.entity;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.resource.lib.entity.EntityZombieNPC;
import net.minecraft.server.EntityZombie;
import net.minecraft.server.World;
public class CitizensZombieNPC extends CitizensNPC {
import org.bukkit.entity.Zombie;
public class CitizensZombieNPC extends CitizensMobNPC {
public CitizensZombieNPC(CitizensNPCManager manager, int id, String name) {
super(manager, id, name, EntityZombieNPC.class);
}
@Override
public Zombie getBukkitEntity() {
return (Zombie) getHandle().getBukkitEntity();
}
public static class EntityZombieNPC extends EntityZombie {
public EntityZombieNPC(World world) {
super(world);
}
@Override
public void m_() {
}
}
}

View File

@ -1,15 +0,0 @@
package net.citizensnpcs.resource.lib.entity;
import net.minecraft.server.EntityBlaze;
import net.minecraft.server.World;
public class EntityBlazeNPC extends EntityBlaze {
public EntityBlazeNPC(World world) {
super(world);
}
@Override
public void m_() {
}
}

View File

@ -1,15 +0,0 @@
package net.citizensnpcs.resource.lib.entity;
import net.minecraft.server.EntityCaveSpider;
import net.minecraft.server.World;
public class EntityCaveSpiderNPC extends EntityCaveSpider {
public EntityCaveSpiderNPC(World world) {
super(world);
}
@Override
public void m_() {
}
}

View File

@ -1,15 +0,0 @@
package net.citizensnpcs.resource.lib.entity;
import net.minecraft.server.EntityChicken;
import net.minecraft.server.World;
public class EntityChickenNPC extends EntityChicken {
public EntityChickenNPC(World world) {
super(world);
}
@Override
public void m_() {
}
}

View File

@ -1,15 +0,0 @@
package net.citizensnpcs.resource.lib.entity;
import net.minecraft.server.EntityCow;
import net.minecraft.server.World;
public class EntityCowNPC extends EntityCow {
public EntityCowNPC(World world) {
super(world);
}
@Override
public void m_() {
}
}

View File

@ -1,15 +0,0 @@
package net.citizensnpcs.resource.lib.entity;
import net.minecraft.server.EntityCreeper;
import net.minecraft.server.World;
public class EntityCreeperNPC extends EntityCreeper {
public EntityCreeperNPC(World world) {
super(world);
}
@Override
public void m_() {
}
}

View File

@ -1,15 +0,0 @@
package net.citizensnpcs.resource.lib.entity;
import net.minecraft.server.EntityEnderDragon;
import net.minecraft.server.World;
public class EntityEnderDragonNPC extends EntityEnderDragon {
public EntityEnderDragonNPC(World world) {
super(world);
}
@Override
public void m_() {
}
}

View File

@ -1,15 +0,0 @@
package net.citizensnpcs.resource.lib.entity;
import net.minecraft.server.EntityEnderman;
import net.minecraft.server.World;
public class EntityEndermanNPC extends EntityEnderman {
public EntityEndermanNPC(World world) {
super(world);
}
@Override
public void m_() {
}
}

View File

@ -1,15 +0,0 @@
package net.citizensnpcs.resource.lib.entity;
import net.minecraft.server.EntityGhast;
import net.minecraft.server.World;
public class EntityGhastNPC extends EntityGhast {
public EntityGhastNPC(World world) {
super(world);
}
@Override
public void m_() {
}
}

View File

@ -1,15 +0,0 @@
package net.citizensnpcs.resource.lib.entity;
import net.minecraft.server.EntityGiantZombie;
import net.minecraft.server.World;
public class EntityGiantNPC extends EntityGiantZombie {
public EntityGiantNPC(World world) {
super(world);
}
@Override
public void m_() {
}
}

View File

@ -1,15 +0,0 @@
package net.citizensnpcs.resource.lib.entity;
import net.minecraft.server.EntityMagmaCube;
import net.minecraft.server.World;
public class EntityMagmaCubeNPC extends EntityMagmaCube {
public EntityMagmaCubeNPC(World world) {
super(world);
}
@Override
public void m_() {
}
}

View File

@ -1,15 +0,0 @@
package net.citizensnpcs.resource.lib.entity;
import net.minecraft.server.EntityMushroomCow;
import net.minecraft.server.World;
public class EntityMushroomCowNPC extends EntityMushroomCow {
public EntityMushroomCowNPC(World world) {
super(world);
}
@Override
public void m_() {
}
}

View File

@ -1,15 +0,0 @@
package net.citizensnpcs.resource.lib.entity;
import net.minecraft.server.EntityPig;
import net.minecraft.server.World;
public class EntityPigNPC extends EntityPig {
public EntityPigNPC(World world) {
super(world);
}
@Override
public void m_() {
}
}

View File

@ -1,15 +0,0 @@
package net.citizensnpcs.resource.lib.entity;
import net.minecraft.server.EntityPigZombie;
import net.minecraft.server.World;
public class EntityPigZombieNPC extends EntityPigZombie {
public EntityPigZombieNPC(World world) {
super(world);
}
@Override
public void m_() {
}
}

View File

@ -1,15 +0,0 @@
package net.citizensnpcs.resource.lib.entity;
import net.minecraft.server.EntitySheep;
import net.minecraft.server.World;
public class EntitySheepNPC extends EntitySheep {
public EntitySheepNPC(World world) {
super(world);
}
@Override
public void m_() {
}
}

View File

@ -1,15 +0,0 @@
package net.citizensnpcs.resource.lib.entity;
import net.minecraft.server.EntitySilverfish;
import net.minecraft.server.World;
public class EntitySilverfishNPC extends EntitySilverfish {
public EntitySilverfishNPC(World world) {
super(world);
}
@Override
public void m_() {
}
}

View File

@ -1,15 +0,0 @@
package net.citizensnpcs.resource.lib.entity;
import net.minecraft.server.EntitySkeleton;
import net.minecraft.server.World;
public class EntitySkeletonNPC extends EntitySkeleton {
public EntitySkeletonNPC(World world) {
super(world);
}
@Override
public void m_() {
}
}

View File

@ -1,15 +0,0 @@
package net.citizensnpcs.resource.lib.entity;
import net.minecraft.server.EntitySlime;
import net.minecraft.server.World;
public class EntitySlimeNPC extends EntitySlime {
public EntitySlimeNPC(World world) {
super(world);
}
@Override
public void m_() {
}
}

View File

@ -1,15 +0,0 @@
package net.citizensnpcs.resource.lib.entity;
import net.minecraft.server.EntitySnowman;
import net.minecraft.server.World;
public class EntitySnowmanNPC extends EntitySnowman {
public EntitySnowmanNPC(World world) {
super(world);
}
@Override
public void m_() {
}
}

View File

@ -1,15 +0,0 @@
package net.citizensnpcs.resource.lib.entity;
import net.minecraft.server.EntitySpider;
import net.minecraft.server.World;
public class EntitySpiderNPC extends EntitySpider {
public EntitySpiderNPC(World world) {
super(world);
}
@Override
public void m_() {
}
}

View File

@ -1,15 +0,0 @@
package net.citizensnpcs.resource.lib.entity;
import net.minecraft.server.EntitySquid;
import net.minecraft.server.World;
public class EntitySquidNPC extends EntitySquid {
public EntitySquidNPC(World world) {
super(world);
}
@Override
public void m_() {
}
}

View File

@ -1,15 +0,0 @@
package net.citizensnpcs.resource.lib.entity;
import net.minecraft.server.EntityVillager;
import net.minecraft.server.World;
public class EntityVillagerNPC extends EntityVillager {
public EntityVillagerNPC(World world) {
super(world);
}
@Override
public void m_() {
}
}

View File

@ -1,15 +0,0 @@
package net.citizensnpcs.resource.lib.entity;
import net.minecraft.server.EntityWolf;
import net.minecraft.server.World;
public class EntityWolfNPC extends EntityWolf {
public EntityWolfNPC(World world) {
super(world);
}
@Override
public void m_() {
}
}

View File

@ -1,15 +0,0 @@
package net.citizensnpcs.resource.lib.entity;
import net.minecraft.server.EntityZombie;
import net.minecraft.server.World;
public class EntityZombieNPC extends EntityZombie {
public EntityZombieNPC(World world) {
super(world);
}
@Override
public void m_() {
}
}

View File

@ -28,29 +28,6 @@ public class YamlStorage implements Storage {
load();
}
@Override
public void load() {
try {
config.load(file);
} catch (Exception ex) {
ex.printStackTrace();
}
}
@Override
public void save() {
try {
config.save(file);
} catch (Exception ex) {
ex.printStackTrace();
}
}
@Override
public DataKey getKey(String root) {
return new YamlKey(root);
}
private void create() {
try {
Messaging.log("Creating file: " + file.getName());
@ -61,10 +38,33 @@ public class YamlStorage implements Storage {
}
}
@Override
public DataKey getKey(String root) {
return new YamlKey(root);
}
@Override
public void load() {
try {
config.load(file);
} catch (Exception ex) {
ex.printStackTrace();
}
}
private boolean pathExists(String key) {
return config.get(key) != null;
}
@Override
public void save() {
try {
config.save(file);
} catch (Exception ex) {
ex.printStackTrace();
}
}
public class YamlKey extends DataKey {
private final String current;
@ -150,6 +150,14 @@ public class YamlStorage implements Storage {
return res;
}
private String getKeyExt(String from) {
if (from.isEmpty())
return current;
if (from.charAt(0) == '.')
return current.isEmpty() ? from.substring(1, from.length()) : current + from;
return current.isEmpty() ? from : current + "." + from;
}
@Override
public long getLong(String key) {
String path = getKeyExt(key);
@ -169,6 +177,11 @@ public class YamlStorage implements Storage {
return config.getLong(getKeyExt(key), def);
}
@Override
public Object getRaw(String key) {
return config.get(getKeyExt(key));
}
@Override
public DataKey getRelative(String relative) {
if (relative == null || relative.isEmpty())
@ -234,27 +247,14 @@ public class YamlStorage implements Storage {
config.set(getKeyExt(key), value);
}
@Override
public void setString(String key, String value) {
config.set(getKeyExt(key), value);
}
@Override
public Object getRaw(String key) {
return config.get(getKeyExt(key));
}
@Override
public void setRaw(String key, Object value) {
config.set(getKeyExt(key), value);
}
private String getKeyExt(String from) {
if (from.isEmpty())
return current;
if (from.charAt(0) == '.')
return current.isEmpty() ? from.substring(1, from.length()) : current + from;
return current.isEmpty() ? from : current + "." + from;
@Override
public void setString(String key, String value) {
config.set(getKeyExt(key), value);
}
}
}

View File

@ -30,14 +30,14 @@ public class LookClose implements Trait {
key.setBoolean("", shouldLookClose);
}
public boolean shouldLookClose() {
return shouldLookClose;
}
public void setLookClose(boolean shouldLookClose) {
this.shouldLookClose = shouldLookClose;
}
public boolean shouldLookClose() {
return shouldLookClose;
}
@Override
public String toString() {
return "LookClose{" + shouldLookClose + "}";

View File

@ -33,6 +33,10 @@ public class Messaging {
player.sendMessage(send);
}
public static void sendError(Player player, Object msg) {
send(player, ChatColor.RED.toString() + msg);
}
public static void sendWithNPC(Player player, Object msg, NPC npc) {
String send = msg.toString();
@ -41,8 +45,4 @@ public class Messaging {
send(player, send);
}
public static void sendError(Player player, Object msg) {
send(player, ChatColor.RED.toString() + msg);
}
}

View File

@ -3,8 +3,6 @@ package net.citizensnpcs.util;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.entity.CreatureType;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.npc.entity.CitizensBlazeNPC;
@ -32,10 +30,12 @@ import net.citizensnpcs.npc.entity.CitizensVillagerNPC;
import net.citizensnpcs.npc.entity.CitizensWolfNPC;
import net.citizensnpcs.npc.entity.CitizensZombieNPC;
public class NPCBuilder {
private final Map<CreatureType, Class<? extends CitizensNPC>> types = new HashMap<CreatureType, Class<? extends CitizensNPC>>();
import org.bukkit.entity.CreatureType;
public NPCBuilder() {
public class NPCBuilder {
private static final Map<CreatureType, Class<? extends CitizensNPC>> types = new HashMap<CreatureType, Class<? extends CitizensNPC>>();
static {
types.put(CreatureType.BLAZE, CitizensBlazeNPC.class);
types.put(CreatureType.CAVE_SPIDER, CitizensCaveSpiderNPC.class);
types.put(CreatureType.CHICKEN, CitizensChickenNPC.class);