This commit is contained in:
fullwall 2012-02-12 11:00:23 +08:00
commit e4b31ae186
13 changed files with 20 additions and 443 deletions

Binary file not shown.

View File

@ -6,7 +6,6 @@ import java.util.logging.Level;
import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.CitizensAPI;
import net.citizensnpcs.api.DataKey;
import net.citizensnpcs.api.exception.NPCException;
import net.citizensnpcs.api.exception.NPCLoadException;
import net.citizensnpcs.api.npc.NPC;
@ -17,6 +16,10 @@ import net.citizensnpcs.api.npc.trait.trait.Inventory;
import net.citizensnpcs.api.npc.trait.trait.Owner;
import net.citizensnpcs.api.npc.trait.trait.SpawnLocation;
import net.citizensnpcs.api.npc.trait.trait.Spawned;
import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.api.util.DatabaseStorage;
import net.citizensnpcs.api.util.Storage;
import net.citizensnpcs.api.util.YamlStorage;
import net.citizensnpcs.command.CommandManager;
import net.citizensnpcs.command.Injector;
import net.citizensnpcs.command.command.AdminCommands;
@ -29,9 +32,6 @@ import net.citizensnpcs.command.exception.ServerCommandException;
import net.citizensnpcs.command.exception.UnhandledCommandException;
import net.citizensnpcs.command.exception.WrappedCommandException;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.storage.DatabaseStorage;
import net.citizensnpcs.storage.Storage;
import net.citizensnpcs.storage.YamlStorage;
import net.citizensnpcs.trait.LookClose;
import net.citizensnpcs.trait.Sneak;
import net.citizensnpcs.util.Messaging;
@ -256,8 +256,8 @@ public class Citizens extends JavaPlugin {
String type = key.getString("traits.type");
NPC npc = npcManager.createNPC(
type.equalsIgnoreCase("DEFAULT") ? CreatureType.MONSTER : CreatureType.valueOf(key.getString(
"traits.type").toUpperCase()), id, key.getString("name"), null);
type.equalsIgnoreCase("DEFAULT") ? null : CreatureType.valueOf(key.getString("traits.type")
.toUpperCase()), id, key.getString("name"), null);
try {
npc.load(key);
} catch (NPCException ex) {

View File

@ -2,8 +2,8 @@ package net.citizensnpcs;
import java.io.File;
import net.citizensnpcs.api.DataKey;
import net.citizensnpcs.storage.YamlStorage;
import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.api.util.YamlStorage;
import net.citizensnpcs.util.Messaging;
public class Settings {
@ -30,11 +30,9 @@ public class Settings {
}
public enum Setting {
DEBUG_MODE("general.debug-mode", false),
USE_DATABASE("use-database", false),
SELECTION_ITEM("npc.selection.item", 280),
SELECTION_MESSAGE("npc.selection.message", "<b>You selected <a><npc><b>!"),
QUICK_SELECT("npc.selection.quick-select", false);
DEBUG_MODE("general.debug-mode", false), USE_DATABASE("use-database", false), SELECTION_ITEM(
"npc.selection.item", 280), SELECTION_MESSAGE("npc.selection.message", "<b>You selected <a><npc><b>!"), QUICK_SELECT(
"npc.selection.quick-select", false);
private String path;
private Object value;

View File

@ -1,6 +1,6 @@
package net.citizensnpcs;
import net.citizensnpcs.api.DataKey;
import net.citizensnpcs.api.util.DataKey;
public class Template {
private final DataKey template;

View File

@ -51,7 +51,7 @@ public class NPCCommands {
Messaging.sendError(player, "NPC names cannot be longer than 16 characters. The name has been shortened.");
name = name.substring(0, 15);
}
CreatureType type = CreatureType.MONSTER; // Default NPC type
CreatureType type = null;
if (args.hasValueFlag("type"))
try {
type = CreatureType.valueOf(args.getFlag("type").toUpperCase().replace('-', '_'));
@ -89,7 +89,7 @@ public class NPCCommands {
create.addTrait(new Owner(player.getName()));
// Set the mob type
create.addTrait(new MobType(type == CreatureType.MONSTER ? "DEFAULT" : type.toString()));
create.addTrait(new MobType(type == null ? "DEFAULT" : type.toString()));
create.spawn(player.getLocation());
npcManager.selectNPC(player, create);

View File

@ -9,7 +9,7 @@ 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.api.util.Storage;
import net.citizensnpcs.util.ByIdArray;
import net.citizensnpcs.util.NPCBuilder;

View File

@ -1,7 +1,5 @@
package net.citizensnpcs.npc.entity;
import net.citizensnpcs.api.DataKey;
import net.citizensnpcs.api.exception.NPCLoadException;
import net.citizensnpcs.npc.CitizensNPC;
import net.citizensnpcs.npc.CitizensNPCManager;
import net.citizensnpcs.resource.lib.EntityHumanNPC;
@ -55,9 +53,4 @@ public class CitizensHumanNPC extends CitizensNPC {
handle.setPositionRotation(loc.getX(), loc.getY(), loc.getZ(), loc.getYaw(), loc.getPitch());
return handle;
}
@Override
public void load(DataKey key) throws NPCLoadException {
super.load(key);
}
}

View File

@ -1,143 +0,0 @@
package net.citizensnpcs.storage;
import java.util.List;
import net.citizensnpcs.api.DataKey;
public class DatabaseStorage implements Storage {
@Override
public DataKey getKey(String root) {
// TODO Auto-generated method stub
return null;
}
@Override
public void load() {
// TODO Auto-generated method stub
}
@Override
public void save() {
// TODO Auto-generated method stub
}
public class DatabaseKey extends DataKey {
@Override
public void copy(String to) {
// TODO Auto-generated method stub
}
@Override
public boolean getBoolean(String key) {
// TODO Auto-generated method stub
return false;
}
@Override
public double getDouble(String key) {
// TODO Auto-generated method stub
return 0;
}
@Override
public int getInt(String key) {
// TODO Auto-generated method stub
return 0;
}
@Override
public List<DataKey> getIntegerSubKeys() {
// TODO Auto-generated method stub
return null;
}
@Override
public long getLong(String key) {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getRaw(String key) {
// TODO Auto-generated method stub
return null;
}
@Override
public DataKey getRelative(String relative) {
// TODO Auto-generated method stub
return null;
}
@Override
public String getString(String key) {
// TODO Auto-generated method stub
return null;
}
@Override
public Iterable<DataKey> getSubKeys() {
// TODO Auto-generated method stub
return null;
}
@Override
public boolean keyExists(String key) {
// TODO Auto-generated method stub
return false;
}
@Override
public String name() {
// TODO Auto-generated method stub
return null;
}
@Override
public void removeKey(String key) {
// TODO Auto-generated method stub
}
@Override
public void setBoolean(String key, boolean value) {
// TODO Auto-generated method stub
}
@Override
public void setDouble(String key, double value) {
// TODO Auto-generated method stub
}
@Override
public void setInt(String key, int value) {
// TODO Auto-generated method stub
}
@Override
public void setLong(String key, long value) {
// TODO Auto-generated method stub
}
@Override
public void setRaw(String path, Object value) {
// TODO Auto-generated method stub
}
@Override
public void setString(String key, String value) {
// TODO Auto-generated method stub
}
}
}

View File

@ -1,12 +0,0 @@
package net.citizensnpcs.storage;
import net.citizensnpcs.api.DataKey;
public interface Storage {
public DataKey getKey(String root);
public void load();
public void save();
}

View File

@ -1,261 +0,0 @@
package net.citizensnpcs.storage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import net.citizensnpcs.api.DataKey;
import net.citizensnpcs.util.Messaging;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
public class YamlStorage implements Storage {
private final FileConfiguration config;
private final File file;
public YamlStorage(String fileName, String header) {
config = new YamlConfiguration();
file = new File(fileName);
if (!file.exists()) {
create();
config.options().header(header);
save();
} else
load();
}
private void create() {
try {
Messaging.log("Creating file: " + file.getName());
file.getParentFile().mkdirs();
file.createNewFile();
} catch (IOException ex) {
Messaging.log(Level.SEVERE, "Could not create file: " + file.getName());
}
}
@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;
public YamlKey(String root) {
current = root;
}
@Override
public void copy(String to) {
ConfigurationSection root = config.getConfigurationSection(current);
if (root == null)
return;
config.createSection(to, root.getValues(true));
}
@Override
public boolean getBoolean(String key) {
String path = getKeyExt(key);
if (pathExists(path)) {
if (config.getString(path) == null)
return config.getBoolean(path);
return Boolean.parseBoolean(config.getString(path));
}
return false;
}
@Override
public boolean getBoolean(String key, boolean def) {
return config.getBoolean(getKeyExt(key), def);
}
@Override
public double getDouble(String key) {
String path = getKeyExt(key);
if (pathExists(path)) {
if (config.getString(path) == null) {
if (config.get(path) instanceof Integer)
return config.getInt(path);
return config.getDouble(path);
}
return Double.parseDouble(config.getString(path));
}
return 0;
}
@Override
public double getDouble(String key, double def) {
return config.getDouble(getKeyExt(key), def);
}
@Override
public int getInt(String key) {
String path = getKeyExt(key);
if (pathExists(path)) {
if (config.getString(path) == null)
return config.getInt(path);
return Integer.parseInt(config.getString(path));
}
return 0;
}
@Override
public int getInt(String key, int def) {
return config.getInt(getKeyExt(key), def);
}
@Override
public List<DataKey> getIntegerSubKeys() {
List<DataKey> res = new ArrayList<DataKey>();
ConfigurationSection section = config.getConfigurationSection(current);
if (section == null)
return res;
List<Integer> keys = new ArrayList<Integer>();
for (String key : section.getKeys(false)) {
try {
keys.add(Integer.parseInt(key));
} catch (NumberFormatException ex) {
}
}
Collections.sort(keys);
for (int key : keys)
res.add(getRelative(Integer.toString(key)));
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);
if (pathExists(path)) {
if (config.getString(path) == null) {
if (config.get(path) instanceof Integer)
return config.getInt(path);
return config.getLong(path);
}
return Long.parseLong(config.getString(path));
}
return 0;
}
@Override
public long getLong(String key, long def) {
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())
return this;
return new YamlKey(getKeyExt(relative));
}
@Override
public String getString(String key) {
String path = getKeyExt(key);
if (pathExists(path)) {
return config.get(path).toString();
}
return "";
}
@Override
public Iterable<DataKey> getSubKeys() {
List<DataKey> res = new ArrayList<DataKey>();
ConfigurationSection section = config.getConfigurationSection(current);
if (section == null)
return res;
for (String key : section.getKeys(false)) {
res.add(getRelative(key));
}
return res;
}
@Override
public boolean keyExists(String key) {
return config.get(getKeyExt(key)) != null;
}
@Override
public String name() {
int last = current.lastIndexOf('.');
return current.substring(last == 0 ? 0 : last + 1);
}
@Override
public void removeKey(String key) {
config.set(getKeyExt(key), null);
save();
}
@Override
public void setBoolean(String key, boolean value) {
config.set(getKeyExt(key), value);
}
@Override
public void setDouble(String key, double value) {
config.set(getKeyExt(key), String.valueOf(value));
}
@Override
public void setInt(String key, int value) {
config.set(getKeyExt(key), value);
}
@Override
public void setLong(String key, long value) {
config.set(getKeyExt(key), value);
}
@Override
public void setRaw(String key, Object value) {
config.set(getKeyExt(key), value);
}
@Override
public void setString(String key, String value) {
config.set(getKeyExt(key), value);
}
}
}

View File

@ -3,11 +3,11 @@ package net.citizensnpcs.trait;
import org.bukkit.Location;
import org.bukkit.entity.Entity;
import net.citizensnpcs.api.DataKey;
import net.citizensnpcs.api.exception.NPCLoadException;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.npc.trait.SaveId;
import net.citizensnpcs.api.npc.trait.Trait;
import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.npc.CitizensNPC;
import net.minecraft.server.EntityLiving;

View File

@ -1,10 +1,10 @@
package net.citizensnpcs.trait;
import net.citizensnpcs.api.DataKey;
import net.citizensnpcs.api.exception.NPCLoadException;
import net.citizensnpcs.api.npc.NPC;
import net.citizensnpcs.api.npc.trait.SaveId;
import net.citizensnpcs.api.npc.trait.Trait;
import net.citizensnpcs.api.util.DataKey;
import net.citizensnpcs.npc.entity.CitizensHumanNPC;
import net.minecraft.server.EntityPlayer;

View File

@ -46,7 +46,6 @@ public class NPCBuilder {
types.put(CreatureType.GHAST, CitizensGhastNPC.class);
types.put(CreatureType.GIANT, CitizensGiantNPC.class);
types.put(CreatureType.MAGMA_CUBE, CitizensMagmaCubeNPC.class);
types.put(CreatureType.MONSTER, CitizensHumanNPC.class);
types.put(CreatureType.MUSHROOM_COW, CitizensMushroomCowNPC.class);
types.put(CreatureType.PIG, CitizensPigNPC.class);
types.put(CreatureType.PIG_ZOMBIE, CitizensPigZombieNPC.class);
@ -64,6 +63,9 @@ public class NPCBuilder {
public CitizensNPC getByType(CreatureType type, CitizensNPCManager npcManager, int id, String name) {
Class<? extends CitizensNPC> npcClass = types.get(type);
if (npcClass == null)
npcClass = CitizensHumanNPC.class;
try {
return npcClass.getConstructor(CitizensNPCManager.class, int.class, String.class).newInstance(npcManager,
id, name);