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