mirror of
https://github.com/CitizensDev/Citizens2.git
synced 2024-11-26 20:55:44 +01:00
use spaces
This commit is contained in:
parent
51e0313cb6
commit
acbecb8c56
@ -1,6 +1,6 @@
|
||||
<project name="Citizens" default="dist" basedir=".">
|
||||
<description>
|
||||
Citizens build file
|
||||
Citizens build file
|
||||
</description>
|
||||
<!-- set global properties for this build -->
|
||||
<property name="src" location="src" />
|
||||
@ -13,7 +13,8 @@
|
||||
|
||||
<!-- Compile the code -->
|
||||
<target name="dist" depends="init" description="compile the source">
|
||||
<javac srcdir="${src}" destdir="${build}" debug="on" debuglevel="lines,vars,source" includeantruntime="false" encoding="Cp1252">
|
||||
<javac srcdir="${src}" destdir="${build}" debug="on"
|
||||
debuglevel="lines,vars,source" includeantruntime="false" encoding="Cp1252">
|
||||
<classpath>
|
||||
<pathelement path="${lib}" />
|
||||
<pathelement location="${lib}/bukkit-1.1-R1-SNAPSHOT.jar" />
|
||||
|
@ -22,72 +22,72 @@ import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
public class Citizens extends JavaPlugin {
|
||||
private CitizensNPCManager npcManager;
|
||||
private CitizensCharacterManager characterManager;
|
||||
private Storage saves;
|
||||
private CitizensNPCManager npcManager;
|
||||
private CitizensCharacterManager characterManager;
|
||||
private Storage saves;
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
Messaging.log("v" + getDescription().getVersion() + " disabled.");
|
||||
}
|
||||
@Override
|
||||
public void onDisable() {
|
||||
Messaging.log("v" + getDescription().getVersion() + " disabled.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
npcManager = new CitizensNPCManager();
|
||||
characterManager = new CitizensCharacterManager();
|
||||
CitizensAPI.setNPCManager(npcManager);
|
||||
CitizensAPI.setCharacterManager(characterManager);
|
||||
@Override
|
||||
public void onEnable() {
|
||||
npcManager = new CitizensNPCManager();
|
||||
characterManager = new CitizensCharacterManager();
|
||||
CitizensAPI.setNPCManager(npcManager);
|
||||
CitizensAPI.setCharacterManager(characterManager);
|
||||
|
||||
// TODO database support
|
||||
saves = new YamlStorage(getDataFolder() + File.separator + "saves.yml");
|
||||
// TODO database support
|
||||
saves = new YamlStorage(getDataFolder() + File.separator + "saves.yml");
|
||||
|
||||
// Register events
|
||||
new EventListen(this);
|
||||
// Register events
|
||||
new EventListen(this);
|
||||
|
||||
Messaging.log("v" + getDescription().getVersion() + " enabled.");
|
||||
Messaging.log("v" + getDescription().getVersion() + " enabled.");
|
||||
|
||||
// Setup NPCs after all plugins have been enabled (allows for multiworld
|
||||
// support and for NPCs to properly register external settings)
|
||||
if (Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
setupNPCs();
|
||||
} catch (NPCLoadException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}, /* TODO how long should delay be? */100) == -1) {
|
||||
Messaging.log("Issue enabling plugin. Disabling.");
|
||||
getServer().getPluginManager().disablePlugin(this);
|
||||
}
|
||||
}
|
||||
// Setup NPCs after all plugins have been enabled (allows for multiworld
|
||||
// support and for NPCs to properly register external settings)
|
||||
if (Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
setupNPCs();
|
||||
} catch (NPCLoadException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}, /* TODO how long should delay be? */100) == -1) {
|
||||
Messaging.log("Issue enabling plugin. Disabling.");
|
||||
getServer().getPluginManager().disablePlugin(this);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command cmd, String cmdName, String[] args) {
|
||||
if (args[0].equals("test")) {
|
||||
NPC npc = npcManager.createNPC("aPunch");
|
||||
npc.spawn(((Player) sender).getLocation());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
@Override
|
||||
public boolean onCommand(CommandSender sender, Command cmd, String cmdName, String[] args) {
|
||||
if (args[0].equals("test")) {
|
||||
NPC npc = npcManager.createNPC("aPunch");
|
||||
npc.spawn(((Player) sender).getLocation());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
private void setupNPCs() throws NPCLoadException {
|
||||
// TODO needs fixing
|
||||
for (DataKey key : saves.getKey("npc").getIntegerSubKeys()) {
|
||||
int id = Integer.parseInt(key.name());
|
||||
if (!key.keyExists("name"))
|
||||
throw new NPCLoadException("Could not find a name for the NPC with ID '" + id + "'.");
|
||||
Character character = characterManager.getCharacter(key.getString("character"));
|
||||
NPC npc = npcManager.createNPC(key.getString("name"), character);
|
||||
if (character != null) {
|
||||
character.load(key);
|
||||
}
|
||||
for (Trait t : npc.getTraits()) {
|
||||
t.load(key);
|
||||
}
|
||||
npc.spawn(npc.getTrait(LocationTrait.class).getLocation());
|
||||
}
|
||||
Messaging.log("Loaded " + npcManager.getNPCs().size() + " NPCs.");
|
||||
}
|
||||
private void setupNPCs() throws NPCLoadException {
|
||||
// TODO needs fixing
|
||||
for (DataKey key : saves.getKey("npc").getIntegerSubKeys()) {
|
||||
int id = Integer.parseInt(key.name());
|
||||
if (!key.keyExists("name"))
|
||||
throw new NPCLoadException("Could not find a name for the NPC with ID '" + id + "'.");
|
||||
Character character = characterManager.getCharacter(key.getString("character"));
|
||||
NPC npc = npcManager.createNPC(key.getString("name"), character);
|
||||
if (character != null) {
|
||||
character.load(key);
|
||||
}
|
||||
for (Trait t : npc.getTraits()) {
|
||||
t.load(key);
|
||||
}
|
||||
npc.spawn(npc.getTrait(LocationTrait.class).getLocation());
|
||||
}
|
||||
Messaging.log("Loaded " + npcManager.getNPCs().size() + " NPCs.");
|
||||
}
|
||||
}
|
@ -18,63 +18,63 @@ import org.bukkit.event.world.ChunkLoadEvent;
|
||||
import org.bukkit.event.world.ChunkUnloadEvent;
|
||||
|
||||
public class EventListen implements Listener {
|
||||
private Set<Integer> toRespawn = new HashSet<Integer>();
|
||||
private Set<Integer> toRespawn = new HashSet<Integer>();
|
||||
|
||||
public EventListen(Citizens plugin) {
|
||||
plugin.getServer().getPluginManager().registerEvents(this, plugin);
|
||||
}
|
||||
public EventListen(Citizens plugin) {
|
||||
plugin.getServer().getPluginManager().registerEvents(this, plugin);
|
||||
}
|
||||
|
||||
/*
|
||||
* Entity events
|
||||
*/
|
||||
@EventHandler()
|
||||
public void onEntityDamage(EntityDamageEvent event) {
|
||||
if (event.isCancelled() || !CitizensAPI.getNPCManager().isNPC(event.getEntity()))
|
||||
return;
|
||||
/*
|
||||
* Entity events
|
||||
*/
|
||||
@EventHandler
|
||||
public void onEntityDamage(EntityDamageEvent event) {
|
||||
if (event.isCancelled() || !CitizensAPI.getNPCManager().isNPC(event.getEntity()))
|
||||
return;
|
||||
|
||||
if (event instanceof EntityDamageByEntityEvent) {
|
||||
EntityDamageByEntityEvent e = (EntityDamageByEntityEvent) event;
|
||||
if (e.getDamager() instanceof Player) {
|
||||
NPC npc = CitizensAPI.getNPCManager().getNPC(event.getEntity());
|
||||
npc.getCharacter().onLeftClick(npc, (Player) e.getDamager());
|
||||
}
|
||||
}
|
||||
}
|
||||
if (event instanceof EntityDamageByEntityEvent) {
|
||||
EntityDamageByEntityEvent e = (EntityDamageByEntityEvent) event;
|
||||
if (e.getDamager() instanceof Player) {
|
||||
NPC npc = CitizensAPI.getNPCManager().getNPC(event.getEntity());
|
||||
npc.getCharacter().onLeftClick(npc, (Player) e.getDamager());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler()
|
||||
public void onEntityTarget(EntityTargetEvent event) {
|
||||
if (event.isCancelled() || !CitizensAPI.getNPCManager().isNPC(event.getEntity())
|
||||
|| !(event.getTarget() instanceof Player))
|
||||
return;
|
||||
@EventHandler
|
||||
public void onEntityTarget(EntityTargetEvent event) {
|
||||
if (event.isCancelled() || !CitizensAPI.getNPCManager().isNPC(event.getEntity())
|
||||
|| !(event.getTarget() instanceof Player))
|
||||
return;
|
||||
|
||||
NPC npc = CitizensAPI.getNPCManager().getNPC(event.getEntity());
|
||||
npc.getCharacter().onRightClick(npc, (Player) event.getTarget());
|
||||
}
|
||||
NPC npc = CitizensAPI.getNPCManager().getNPC(event.getEntity());
|
||||
npc.getCharacter().onRightClick(npc, (Player) event.getTarget());
|
||||
}
|
||||
|
||||
/*
|
||||
* World events
|
||||
*/
|
||||
@EventHandler()
|
||||
public void onChunkLoad(ChunkLoadEvent event) {
|
||||
for (int id : toRespawn) {
|
||||
NPC npc = CitizensAPI.getNPCManager().getNPC(id);
|
||||
npc.spawn(npc.getTrait(LocationTrait.class).getLocation());
|
||||
toRespawn.remove(id);
|
||||
}
|
||||
}
|
||||
/*
|
||||
* World events
|
||||
*/
|
||||
@EventHandler
|
||||
public void onChunkLoad(ChunkLoadEvent event) {
|
||||
for (int id : toRespawn) {
|
||||
NPC npc = CitizensAPI.getNPCManager().getNPC(id);
|
||||
npc.spawn(npc.getTrait(LocationTrait.class).getLocation());
|
||||
toRespawn.remove(id);
|
||||
}
|
||||
}
|
||||
|
||||
@EventHandler()
|
||||
public void onChunkUnload(ChunkUnloadEvent event) {
|
||||
if (event.isCancelled())
|
||||
return;
|
||||
@EventHandler
|
||||
public void onChunkUnload(ChunkUnloadEvent event) {
|
||||
if (event.isCancelled())
|
||||
return;
|
||||
|
||||
for (NPC npc : CitizensAPI.getNPCManager().getNPCs()) {
|
||||
Location loc = npc.getTrait(LocationTrait.class).getLocation();
|
||||
if (event.getWorld().equals(loc.getWorld()) && event.getChunk().getX() == loc.getChunk().getX()
|
||||
&& event.getChunk().getZ() == loc.getChunk().getZ()) {
|
||||
toRespawn.add(npc.getId());
|
||||
npc.despawn();
|
||||
}
|
||||
}
|
||||
}
|
||||
for (NPC npc : CitizensAPI.getNPCManager().getNPCs()) {
|
||||
Location loc = npc.getTrait(LocationTrait.class).getLocation();
|
||||
if (event.getWorld().equals(loc.getWorld()) && event.getChunk().getX() == loc.getChunk().getX()
|
||||
&& event.getChunk().getZ() == loc.getChunk().getZ()) {
|
||||
toRespawn.add(npc.getId());
|
||||
npc.despawn();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -19,172 +19,167 @@ import org.bukkit.ChatColor;
|
||||
import org.bukkit.Location;
|
||||
|
||||
public class CitizensNPC implements NPC {
|
||||
private final int id;
|
||||
private Character character = null;
|
||||
private final Map<Class<? extends Trait>, Trait> traits = new HashMap<Class<? extends Trait>, Trait>();
|
||||
private String name;
|
||||
private CraftNPC mcEntity;
|
||||
private boolean spawned;
|
||||
private final CitizensNPCManager manager;
|
||||
private final Factory factory = new Factory();
|
||||
private final int id;
|
||||
private Character character = null;
|
||||
private final Map<Class<? extends Trait>, Trait> traits = new HashMap<Class<? extends Trait>, Trait>();
|
||||
private String name;
|
||||
private CraftNPC mcEntity;
|
||||
private boolean spawned;
|
||||
private final CitizensNPCManager manager;
|
||||
private final Factory factory = new Factory();
|
||||
|
||||
public CitizensNPC(String name, Character character) {
|
||||
this.name = name;
|
||||
this.character = character;
|
||||
manager = (CitizensNPCManager) CitizensAPI.getNPCManager();
|
||||
id = manager.getUniqueID();
|
||||
}
|
||||
public CitizensNPC(String name, Character character) {
|
||||
this.name = name;
|
||||
this.character = character;
|
||||
manager = (CitizensNPCManager) CitizensAPI.getNPCManager();
|
||||
id = manager.getUniqueID();
|
||||
}
|
||||
|
||||
public CitizensNPC(int id, String name, Character character) {
|
||||
this.name = name;
|
||||
this.character = character;
|
||||
manager = (CitizensNPCManager) CitizensAPI.getNPCManager();
|
||||
this.id = id;
|
||||
}
|
||||
public CitizensNPC(int id, String name, Character character) {
|
||||
this.name = name;
|
||||
this.character = character;
|
||||
manager = (CitizensNPCManager) CitizensAPI.getNPCManager();
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getFullName() {
|
||||
return name;
|
||||
}
|
||||
@Override
|
||||
public String getFullName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getName() {
|
||||
return ChatColor.stripColor(name);
|
||||
}
|
||||
@Override
|
||||
public String getName() {
|
||||
return ChatColor.stripColor(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
@Override
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addTrait(Class<? extends Trait> trait) {
|
||||
if (!hasTrait(trait))
|
||||
traits.put(trait, factory.create(trait));
|
||||
else
|
||||
Messaging.debug("The NPC already has the trait '" + trait.getName() + "'.");
|
||||
}
|
||||
@Override
|
||||
public void addTrait(Class<? extends Trait> trait) {
|
||||
if (!hasTrait(trait))
|
||||
traits.put(trait, factory.create(trait));
|
||||
else
|
||||
Messaging.debug("The NPC already has the trait '" + trait.getName() + "'.");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Character getCharacter() {
|
||||
return character;
|
||||
}
|
||||
@Override
|
||||
public Character getCharacter() {
|
||||
return character;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
@Override
|
||||
public int getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Navigator getNavigator() {
|
||||
// TODO add default navigator
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
public Navigator getNavigator() {
|
||||
// TODO add default navigator
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends Trait> T getTrait(Class<T> trait) {
|
||||
Trait t = traits.get(trait);
|
||||
if (t != null) {
|
||||
return trait.cast(t);
|
||||
}
|
||||
@Override
|
||||
public <T extends Trait> T getTrait(Class<T> trait) {
|
||||
Trait t = traits.get(trait);
|
||||
if (t != null) {
|
||||
return trait.cast(t);
|
||||
}
|
||||
|
||||
T create = factory.create(trait);
|
||||
addTrait(trait);
|
||||
return create;
|
||||
}
|
||||
T create = factory.create(trait);
|
||||
addTrait(trait);
|
||||
return create;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterable<Trait> getTraits() {
|
||||
return traits.values();
|
||||
}
|
||||
@Override
|
||||
public Iterable<Trait> getTraits() {
|
||||
return traits.values();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasTrait(Class<? extends Trait> trait) {
|
||||
return traits.containsKey(trait);
|
||||
}
|
||||
@Override
|
||||
public boolean hasTrait(Class<? extends Trait> trait) {
|
||||
return traits.containsKey(trait);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeTrait(Class<? extends Trait> trait) {
|
||||
if (!hasTrait(trait)) {
|
||||
Messaging.debug("The NPC does not have a trait with the name of '" + trait.getName() + ".");
|
||||
return;
|
||||
}
|
||||
traits.remove(trait);
|
||||
}
|
||||
@Override
|
||||
public void removeTrait(Class<? extends Trait> trait) {
|
||||
if (!hasTrait(trait)) {
|
||||
Messaging.debug("The NPC does not have a trait with the name of '" + trait.getName() + ".");
|
||||
return;
|
||||
}
|
||||
traits.remove(trait);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setCharacter(Character character) {
|
||||
if (this.character.equals(character)) {
|
||||
Messaging.debug("The NPC already has the character '" + character.getName() + "'.");
|
||||
return;
|
||||
}
|
||||
this.character = character;
|
||||
}
|
||||
@Override
|
||||
public void setCharacter(Character character) {
|
||||
if (this.character.equals(character)) {
|
||||
Messaging.debug("The NPC already has the character '" + character.getName() + "'.");
|
||||
return;
|
||||
}
|
||||
this.character = character;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isSpawned() {
|
||||
return spawned;
|
||||
}
|
||||
@Override
|
||||
public boolean isSpawned() {
|
||||
return spawned;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void spawn(Location loc) {
|
||||
if (isSpawned()) {
|
||||
System.out.println("The NPC is already spawned.");
|
||||
return;
|
||||
}
|
||||
@Override
|
||||
public void spawn(Location loc) {
|
||||
if (isSpawned()) {
|
||||
Messaging.debug("The NPC is already spawned.");
|
||||
return;
|
||||
}
|
||||
|
||||
NPCSpawnEvent spawnEvent = new NPCSpawnEvent(this, loc);
|
||||
Bukkit.getPluginManager().callEvent(spawnEvent);
|
||||
if (spawnEvent.isCancelled()) {
|
||||
return;
|
||||
}
|
||||
NPCSpawnEvent spawnEvent = new NPCSpawnEvent(this, loc);
|
||||
Bukkit.getPluginManager().callEvent(spawnEvent);
|
||||
if (spawnEvent.isCancelled()) {
|
||||
return;
|
||||
}
|
||||
|
||||
spawned = true;
|
||||
getTrait(LocationTrait.class).setLocation(loc);
|
||||
mcEntity = manager.spawn(this, loc);
|
||||
}
|
||||
spawned = true;
|
||||
getTrait(LocationTrait.class).setLocation(loc);
|
||||
mcEntity = manager.spawn(this, loc);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void despawn() {
|
||||
if (!isSpawned()) {
|
||||
System.out.println("The NPC is already despawned.");
|
||||
return;
|
||||
}
|
||||
@Override
|
||||
public void despawn() {
|
||||
if (!isSpawned()) {
|
||||
Messaging.debug("The NPC is already despawned.");
|
||||
return;
|
||||
}
|
||||
|
||||
Bukkit.getPluginManager().callEvent(new NPCDespawnEvent(this));
|
||||
Bukkit.getPluginManager().callEvent(new NPCDespawnEvent(this));
|
||||
|
||||
spawned = false;
|
||||
mcEntity.die();
|
||||
manager.despawn(this);
|
||||
}
|
||||
spawned = false;
|
||||
mcEntity.die();
|
||||
manager.despawn(this);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
if (isSpawned()) {
|
||||
despawn();
|
||||
}
|
||||
manager.remove(this);
|
||||
}
|
||||
@Override
|
||||
public void remove() {
|
||||
if (isSpawned()) {
|
||||
despawn();
|
||||
}
|
||||
manager.remove(this);
|
||||
}
|
||||
|
||||
public CraftNPC getHandle() {
|
||||
return mcEntity;
|
||||
}
|
||||
public CraftNPC getHandle() {
|
||||
return mcEntity;
|
||||
}
|
||||
|
||||
private class Factory {
|
||||
private final Map<Class<? extends Trait>, Trait> created = new HashMap<Class<? extends Trait>, Trait>();
|
||||
private class Factory {
|
||||
|
||||
public <T extends Trait> T create(Class<T> clazz) {
|
||||
if (created.containsKey(clazz)) {
|
||||
return clazz.cast(created.get(clazz));
|
||||
}
|
||||
try {
|
||||
Trait trait = clazz.newInstance();
|
||||
created.put(clazz, trait);
|
||||
return clazz.cast(trait);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
public <T extends Trait> T create(Class<T> clazz) {
|
||||
try {
|
||||
Trait trait = clazz.newInstance();
|
||||
return clazz.cast(trait);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -25,91 +25,91 @@ import net.minecraft.server.MinecraftServer;
|
||||
import net.minecraft.server.WorldServer;
|
||||
|
||||
public class CitizensNPCManager implements NPCManager {
|
||||
private Map<Entity, NPC> spawned = new HashMap<Entity, NPC>();
|
||||
private Map<Integer, NPC> byID = new HashMap<Integer, NPC>();
|
||||
private Map<Entity, NPC> spawned = new HashMap<Entity, NPC>();
|
||||
private Map<Integer, NPC> byID = new HashMap<Integer, NPC>();
|
||||
|
||||
@Override
|
||||
public NPC createNPC(String name) {
|
||||
return createNPC(name, null);
|
||||
}
|
||||
@Override
|
||||
public NPC createNPC(String name) {
|
||||
return createNPC(name, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public NPC createNPC(String name, Character character) {
|
||||
CitizensNPC npc = new CitizensNPC(name, character);
|
||||
byID.put(npc.getId(), npc);
|
||||
return npc;
|
||||
}
|
||||
@Override
|
||||
public NPC createNPC(String name, Character character) {
|
||||
CitizensNPC npc = new CitizensNPC(name, character);
|
||||
byID.put(npc.getId(), npc);
|
||||
return npc;
|
||||
}
|
||||
|
||||
@Override
|
||||
public NPC getNPC(int id) {
|
||||
return byID.get(id);
|
||||
}
|
||||
@Override
|
||||
public NPC getNPC(int id) {
|
||||
return byID.get(id);
|
||||
}
|
||||
|
||||
@Override
|
||||
public NPC getNPC(Entity entity) {
|
||||
return spawned.get(entity);
|
||||
}
|
||||
@Override
|
||||
public NPC getNPC(Entity entity) {
|
||||
return spawned.get(entity);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<NPC> getNPCs() {
|
||||
return spawned.values();
|
||||
}
|
||||
@Override
|
||||
public Collection<NPC> getNPCs() {
|
||||
return spawned.values();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<NPC> getNPCs(Class<? extends Trait> trait) {
|
||||
Set<NPC> npcs = new HashSet<NPC>();
|
||||
for (NPC npc : spawned.values()) {
|
||||
if (npc.hasTrait(trait))
|
||||
npcs.add(npc);
|
||||
}
|
||||
return npcs;
|
||||
}
|
||||
@Override
|
||||
public Collection<NPC> getNPCs(Class<? extends Trait> trait) {
|
||||
Set<NPC> npcs = new HashSet<NPC>();
|
||||
for (NPC npc : spawned.values()) {
|
||||
if (npc.hasTrait(trait))
|
||||
npcs.add(npc);
|
||||
}
|
||||
return npcs;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isNPC(Entity entity) {
|
||||
return spawned.containsKey(entity);
|
||||
}
|
||||
@Override
|
||||
public boolean isNPC(Entity entity) {
|
||||
return spawned.containsKey(entity);
|
||||
}
|
||||
|
||||
public int getUniqueID() {
|
||||
int count = 0;
|
||||
while (true) {
|
||||
if (getNPC(count) == null)
|
||||
break;
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
public int getUniqueID() {
|
||||
int count = 0;
|
||||
while (true) {
|
||||
if (getNPC(count) == null)
|
||||
break;
|
||||
count++;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
public CraftNPC spawn(NPC npc, Location loc) {
|
||||
WorldServer ws = getWorldServer(loc.getWorld());
|
||||
CraftNPC mcEntity = new CraftNPC(getMinecraftServer(ws.getServer()), ws, npc.getFullName(),
|
||||
new ItemInWorldManager(ws));
|
||||
mcEntity.setPositionRotation(loc.getX(), loc.getY(), loc.getZ(), loc.getYaw(), loc.getPitch());
|
||||
ws.addEntity(mcEntity);
|
||||
ws.players.remove(mcEntity);
|
||||
public CraftNPC spawn(NPC npc, Location loc) {
|
||||
WorldServer ws = getWorldServer(loc.getWorld());
|
||||
CraftNPC mcEntity = new CraftNPC(getMinecraftServer(ws.getServer()), ws, npc.getFullName(),
|
||||
new ItemInWorldManager(ws));
|
||||
mcEntity.setPositionRotation(loc.getX(), loc.getY(), loc.getZ(), loc.getYaw(), loc.getPitch());
|
||||
ws.addEntity(mcEntity);
|
||||
ws.players.remove(mcEntity);
|
||||
|
||||
spawned.put(mcEntity.getPlayer(), npc);
|
||||
return mcEntity;
|
||||
}
|
||||
spawned.put(mcEntity.getPlayer(), npc);
|
||||
return mcEntity;
|
||||
}
|
||||
|
||||
public void despawn(NPC npc) {
|
||||
CraftNPC mcEntity = ((CitizensNPC) npc).getHandle();
|
||||
// TODO send Packet29DestroyEntity
|
||||
getWorldServer(npc.getTrait(LocationTrait.class).getLocation().getWorld()).removeEntity(mcEntity);
|
||||
spawned.remove(mcEntity.getPlayer());
|
||||
}
|
||||
public void despawn(NPC npc) {
|
||||
CraftNPC mcEntity = ((CitizensNPC) npc).getHandle();
|
||||
// TODO send Packet29DestroyEntity
|
||||
getWorldServer(npc.getTrait(LocationTrait.class).getLocation().getWorld()).removeEntity(mcEntity);
|
||||
spawned.remove(mcEntity.getPlayer());
|
||||
}
|
||||
|
||||
public void remove(NPC npc) {
|
||||
if (spawned.containsKey(((CitizensNPC) npc).getHandle()))
|
||||
despawn(npc);
|
||||
byID.remove(npc.getId());
|
||||
}
|
||||
public void remove(NPC npc) {
|
||||
if (spawned.containsKey(((CitizensNPC) npc).getHandle()))
|
||||
despawn(npc);
|
||||
byID.remove(npc.getId());
|
||||
}
|
||||
|
||||
private WorldServer getWorldServer(World world) {
|
||||
return ((CraftWorld) world).getHandle();
|
||||
}
|
||||
private WorldServer getWorldServer(World world) {
|
||||
return ((CraftWorld) world).getHandle();
|
||||
}
|
||||
|
||||
private MinecraftServer getMinecraftServer(Server server) {
|
||||
return ((CraftServer) server).getServer();
|
||||
}
|
||||
private MinecraftServer getMinecraftServer(Server server) {
|
||||
return ((CraftServer) server).getServer();
|
||||
}
|
||||
}
|
@ -9,33 +9,33 @@ import net.citizensnpcs.api.npc.trait.Character;
|
||||
import net.citizensnpcs.api.npc.trait.CharacterManager;
|
||||
|
||||
public class CitizensCharacterManager implements CharacterManager {
|
||||
private final Map<String, Class<? extends Character>> registered = new HashMap<String, Class<? extends Character>>();
|
||||
private final Set<Character> characters = new HashSet<Character>();
|
||||
private final Map<String, Class<? extends Character>> registered = new HashMap<String, Class<? extends Character>>();
|
||||
private final Set<Character> characters = new HashSet<Character>();
|
||||
|
||||
@Override
|
||||
public Character getCharacter(String name) {
|
||||
if (registered.get(name) == null) {
|
||||
return null;
|
||||
}
|
||||
for (Character character : characters) {
|
||||
if (character.getName().equals(name)) {
|
||||
return character;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
public Character getCharacter(String name) {
|
||||
if (registered.get(name) == null) {
|
||||
return null;
|
||||
}
|
||||
for (Character character : characters) {
|
||||
if (character.getName().equals(name)) {
|
||||
return character;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void registerCharacter(Class<? extends Character> character) {
|
||||
if (registered.containsValue(character)) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
Character register = character.newInstance();
|
||||
registered.put(register.getName(), character);
|
||||
characters.add(register);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
@Override
|
||||
public void registerCharacter(Class<? extends Character> character) {
|
||||
if (registered.containsValue(character)) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
Character register = character.newInstance();
|
||||
registered.put(register.getName(), character);
|
||||
characters.add(register);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
@ -11,25 +11,25 @@ import net.minecraft.server.World;
|
||||
|
||||
public class CraftNPC extends EntityPlayer {
|
||||
|
||||
public CraftNPC(MinecraftServer minecraftServer, World world, String string, ItemInWorldManager itemInWorldManager) {
|
||||
super(minecraftServer, world, string, itemInWorldManager);
|
||||
itemInWorldManager.setGameMode(0);
|
||||
public CraftNPC(MinecraftServer minecraftServer, World world, String string, ItemInWorldManager itemInWorldManager) {
|
||||
super(minecraftServer, world, string, itemInWorldManager);
|
||||
itemInWorldManager.setGameMode(0);
|
||||
|
||||
NPCSocket socket = new NPCSocket();
|
||||
NPCSocket socket = new NPCSocket();
|
||||
|
||||
NetworkManager netMgr = new NPCNetworkManager(socket, "npc mgr", new NetHandler() {
|
||||
@Override
|
||||
public boolean c() {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
netServerHandler = new NPCNetHandler(minecraftServer, netMgr, this);
|
||||
netMgr.a(netServerHandler);
|
||||
NetworkManager netMgr = new NPCNetworkManager(socket, "npc mgr", new NetHandler() {
|
||||
@Override
|
||||
public boolean c() {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
netServerHandler = new NPCNetHandler(minecraftServer, netMgr, this);
|
||||
netMgr.a(netServerHandler);
|
||||
|
||||
try {
|
||||
socket.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
try {
|
||||
socket.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
@ -20,75 +20,75 @@ import net.minecraft.server.Packet51MapChunk;
|
||||
|
||||
public class NPCNetHandler extends NetServerHandler {
|
||||
|
||||
public NPCNetHandler(MinecraftServer minecraftServer, NetworkManager networkManager, EntityPlayer entityPlayer) {
|
||||
super(minecraftServer, networkManager, entityPlayer);
|
||||
}
|
||||
public NPCNetHandler(MinecraftServer minecraftServer, NetworkManager networkManager, EntityPlayer entityPlayer) {
|
||||
super(minecraftServer, networkManager, entityPlayer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void a() {
|
||||
}
|
||||
@Override
|
||||
public void a() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendMessage(String string) {
|
||||
}
|
||||
@Override
|
||||
public void sendMessage(String string) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void a(String string, Object[] objects) {
|
||||
}
|
||||
@Override
|
||||
public void a(String string, Object[] objects) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void a(Packet packet) {
|
||||
}
|
||||
@Override
|
||||
public void a(Packet packet) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendPacket(Packet packet) {
|
||||
}
|
||||
@Override
|
||||
public void sendPacket(Packet packet) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void a(Packet3Chat packet) {
|
||||
}
|
||||
@Override
|
||||
public void a(Packet3Chat packet) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void a(Packet10Flying packet) {
|
||||
}
|
||||
@Override
|
||||
public void a(Packet10Flying packet) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void a(Packet14BlockDig packet) {
|
||||
}
|
||||
@Override
|
||||
public void a(Packet14BlockDig packet) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void a(Packet15Place packet) {
|
||||
}
|
||||
@Override
|
||||
public void a(Packet15Place packet) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void a(Packet16BlockItemSwitch packet) {
|
||||
}
|
||||
@Override
|
||||
public void a(Packet16BlockItemSwitch packet) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void a(Packet28EntityVelocity packet) {
|
||||
}
|
||||
@Override
|
||||
public void a(Packet28EntityVelocity packet) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void a(Packet51MapChunk packet) {
|
||||
}
|
||||
@Override
|
||||
public void a(Packet51MapChunk packet) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void a(Packet101CloseWindow packet) {
|
||||
}
|
||||
@Override
|
||||
public void a(Packet101CloseWindow packet) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void a(Packet102WindowClick packet) {
|
||||
}
|
||||
@Override
|
||||
public void a(Packet102WindowClick packet) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void a(Packet106Transaction packet) {
|
||||
}
|
||||
@Override
|
||||
public void a(Packet106Transaction packet) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void a(Packet255KickDisconnect packet) {
|
||||
}
|
||||
@Override
|
||||
public void a(Packet255KickDisconnect packet) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void a(Packet130UpdateSign packet) {
|
||||
}
|
||||
@Override
|
||||
public void a(Packet130UpdateSign packet) {
|
||||
}
|
||||
}
|
@ -9,44 +9,44 @@ import net.minecraft.server.Packet;
|
||||
|
||||
public class NPCNetworkManager extends NetworkManager {
|
||||
|
||||
public NPCNetworkManager(Socket socket, String string, NetHandler netHandler) {
|
||||
super(socket, string, netHandler);
|
||||
public NPCNetworkManager(Socket socket, String string, NetHandler netHandler) {
|
||||
super(socket, string, netHandler);
|
||||
|
||||
try {
|
||||
// the field above the 3 synchronized lists.
|
||||
Field f = NetworkManager.class.getDeclaredField("l");
|
||||
f.setAccessible(true);
|
||||
f.set(this, false);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
try {
|
||||
// the field above the 3 synchronized lists
|
||||
Field f = NetworkManager.class.getDeclaredField("l");
|
||||
f.setAccessible(true);
|
||||
f.set(this, false);
|
||||
} catch (Exception e) {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void a(NetHandler netHandler) {
|
||||
}
|
||||
@Override
|
||||
public void a(NetHandler netHandler) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void queue(Packet packet) {
|
||||
}
|
||||
@Override
|
||||
public void queue(Packet packet) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void a(String s, Object... objects) {
|
||||
}
|
||||
@Override
|
||||
public void a(String s, Object... objects) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void a() {
|
||||
}
|
||||
@Override
|
||||
public void a() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void b() {
|
||||
}
|
||||
@Override
|
||||
public void b() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void d() {
|
||||
}
|
||||
@Override
|
||||
public void d() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public int e() {
|
||||
return 0;
|
||||
}
|
||||
@Override
|
||||
public int e() {
|
||||
return 0;
|
||||
}
|
||||
}
|
@ -8,13 +8,13 @@ import java.net.Socket;
|
||||
|
||||
public class NPCSocket extends Socket {
|
||||
|
||||
@Override
|
||||
public InputStream getInputStream() {
|
||||
return new ByteArrayInputStream(new byte[10]);
|
||||
}
|
||||
@Override
|
||||
public InputStream getInputStream() {
|
||||
return new ByteArrayInputStream(new byte[10]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public OutputStream getOutputStream() {
|
||||
return new ByteArrayOutputStream();
|
||||
}
|
||||
@Override
|
||||
public OutputStream getOutputStream() {
|
||||
return new ByteArrayOutputStream();
|
||||
}
|
||||
}
|
@ -4,9 +4,9 @@ import net.citizensnpcs.api.DataKey;
|
||||
|
||||
public interface Storage {
|
||||
|
||||
public void load();
|
||||
public void load();
|
||||
|
||||
public void save();
|
||||
public void save();
|
||||
|
||||
public DataKey getKey(String root);
|
||||
public DataKey getKey(String root);
|
||||
}
|
@ -7,138 +7,138 @@ import net.citizensnpcs.storage.Storage;
|
||||
|
||||
public class DatabaseStorage implements Storage {
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
// TODO Auto-generated method stub
|
||||
@Override
|
||||
public void load() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void save() {
|
||||
// TODO Auto-generated method stub
|
||||
@Override
|
||||
public void save() {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataKey getKey(String root) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
public DataKey getKey(String root) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
public class DatabaseKey extends DataKey {
|
||||
public class DatabaseKey extends DataKey {
|
||||
|
||||
@Override
|
||||
public void copy(String to) {
|
||||
// TODO Auto-generated method stub
|
||||
@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 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 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 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 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 long getLong(String key) {
|
||||
// TODO Auto-generated method stub
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataKey getRelative(String relative) {
|
||||
// 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 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 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 boolean keyExists(String key) {
|
||||
// TODO Auto-generated method stub
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
@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 removeKey(String key) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBoolean(String key, boolean value) {
|
||||
// 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 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 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 setLong(String key, long value) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setString(String key, String value) {
|
||||
// TODO Auto-generated method stub
|
||||
@Override
|
||||
public void setString(String key, String value) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getRaw(String key) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
public Object getRaw(String key) {
|
||||
// TODO Auto-generated method stub
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRaw(String path, Object value) {
|
||||
// TODO Auto-generated method stub
|
||||
@Override
|
||||
public void setRaw(String path, Object value) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -16,239 +16,239 @@ import net.citizensnpcs.storage.Storage;
|
||||
import net.citizensnpcs.util.Messaging;
|
||||
|
||||
public class YamlStorage implements Storage {
|
||||
private final FileConfiguration config;
|
||||
private final File file;
|
||||
private final FileConfiguration config;
|
||||
private final File file;
|
||||
|
||||
public YamlStorage(String fileName) {
|
||||
config = new YamlConfiguration();
|
||||
file = new File(fileName);
|
||||
if (!file.exists()) {
|
||||
create();
|
||||
save();
|
||||
} else
|
||||
load();
|
||||
}
|
||||
public YamlStorage(String fileName) {
|
||||
config = new YamlConfiguration();
|
||||
file = new File(fileName);
|
||||
if (!file.exists()) {
|
||||
create();
|
||||
save();
|
||||
} else
|
||||
load();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void load() {
|
||||
try {
|
||||
config.load(file);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
@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 void save() {
|
||||
try {
|
||||
config.save(file);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataKey getKey(String root) {
|
||||
return new YamlKey(root);
|
||||
}
|
||||
@Override
|
||||
public DataKey getKey(String root) {
|
||||
return new YamlKey(root);
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
||||
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());
|
||||
}
|
||||
}
|
||||
|
||||
public class YamlKey extends DataKey {
|
||||
private final String current;
|
||||
public class YamlKey extends DataKey {
|
||||
private final String current;
|
||||
|
||||
public YamlKey(String root) {
|
||||
current = root;
|
||||
}
|
||||
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 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 (keyExists(path)) {
|
||||
if (config.getString(path) == null)
|
||||
return config.getBoolean(path);
|
||||
return Boolean.parseBoolean(config.getString(path));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
@Override
|
||||
public boolean getBoolean(String key) {
|
||||
String path = getKeyExt(key);
|
||||
if (keyExists(path)) {
|
||||
if (config.getString(path) == null)
|
||||
return config.getBoolean(path);
|
||||
return Boolean.parseBoolean(config.getString(path));
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean getBoolean(String keyExt, boolean def) {
|
||||
return config.getBoolean(getKeyExt(keyExt), def);
|
||||
}
|
||||
@Override
|
||||
public boolean getBoolean(String keyExt, boolean def) {
|
||||
return config.getBoolean(getKeyExt(keyExt), def);
|
||||
}
|
||||
|
||||
@Override
|
||||
public double getDouble(String key) {
|
||||
String path = getKeyExt(key);
|
||||
if (keyExists(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) {
|
||||
String path = getKeyExt(key);
|
||||
if (keyExists(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 keyExt, double def) {
|
||||
return config.getDouble(getKeyExt(keyExt), def);
|
||||
}
|
||||
@Override
|
||||
public double getDouble(String keyExt, double def) {
|
||||
return config.getDouble(getKeyExt(keyExt), def);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInt(String key) {
|
||||
String path = getKeyExt(key);
|
||||
if (keyExists(path)) {
|
||||
if (config.getString(path) == null)
|
||||
return config.getInt(path);
|
||||
return Integer.parseInt(config.getString(path));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
@Override
|
||||
public int getInt(String key) {
|
||||
String path = getKeyExt(key);
|
||||
if (keyExists(path)) {
|
||||
if (config.getString(path) == null)
|
||||
return config.getInt(path);
|
||||
return Integer.parseInt(config.getString(path));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getInt(String path, int value) {
|
||||
return config.getInt(getKeyExt(path), value);
|
||||
}
|
||||
@Override
|
||||
public int getInt(String path, int value) {
|
||||
return config.getInt(getKeyExt(path), value);
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
@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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public long getLong(String key) {
|
||||
String path = getKeyExt(key);
|
||||
if (keyExists(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) {
|
||||
String path = getKeyExt(key);
|
||||
if (keyExists(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 keyExt, long def) {
|
||||
return config.getLong(getKeyExt(keyExt), def);
|
||||
}
|
||||
@Override
|
||||
public long getLong(String keyExt, long def) {
|
||||
return config.getLong(getKeyExt(keyExt), def);
|
||||
}
|
||||
|
||||
@Override
|
||||
public DataKey getRelative(String relative) {
|
||||
return new YamlKey(getKeyExt(relative));
|
||||
}
|
||||
@Override
|
||||
public DataKey getRelative(String relative) {
|
||||
return new YamlKey(getKeyExt(relative));
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getString(String key) {
|
||||
String path = getKeyExt(key);
|
||||
if (keyExists(path)) {
|
||||
return config.get(path).toString();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
@Override
|
||||
public String getString(String key) {
|
||||
String path = getKeyExt(key);
|
||||
if (keyExists(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 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 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 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);
|
||||
}
|
||||
@Override
|
||||
public void removeKey(String key) {
|
||||
config.set(getKeyExt(key), null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setBoolean(String key, boolean value) {
|
||||
config.set(getKeyExt(key), value);
|
||||
}
|
||||
@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 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 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 setLong(String key, long value) {
|
||||
config.set(getKeyExt(key), value);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setString(String key, String value) {
|
||||
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 Object getRaw(String key) {
|
||||
return config.get(getKeyExt(key));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setRaw(String key, Object value) {
|
||||
config.set(getKeyExt(key), value);
|
||||
}
|
||||
@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;
|
||||
}
|
||||
}
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
@ -6,16 +6,16 @@ import org.bukkit.Bukkit;
|
||||
|
||||
public class Messaging {
|
||||
|
||||
public static void log(Level level, Object msg) {
|
||||
Bukkit.getLogger().log(level, "[Citizens] " + msg);
|
||||
}
|
||||
public static void log(Level level, Object msg) {
|
||||
Bukkit.getLogger().log(level, "[Citizens] " + msg);
|
||||
}
|
||||
|
||||
public static void log(Object msg) {
|
||||
log(Level.INFO, msg);
|
||||
}
|
||||
public static void log(Object msg) {
|
||||
log(Level.INFO, msg);
|
||||
}
|
||||
|
||||
public static void debug(Object msg) {
|
||||
// TODO add debug setting
|
||||
log(msg);
|
||||
}
|
||||
public static void debug(Object msg) {
|
||||
// TODO add debug setting
|
||||
log(msg);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user