mirror of
https://github.com/CitizensDev/Citizens2.git
synced 2025-01-13 03:40:39 +01:00
Cleanup
This commit is contained in:
parent
a0f7d8c130
commit
27e2a6c8c9
@ -32,12 +32,11 @@ import net.citizensnpcs.command.exception.ServerCommandException;
|
|||||||
import net.citizensnpcs.command.exception.UnhandledCommandException;
|
import net.citizensnpcs.command.exception.UnhandledCommandException;
|
||||||
import net.citizensnpcs.command.exception.WrappedCommandException;
|
import net.citizensnpcs.command.exception.WrappedCommandException;
|
||||||
import net.citizensnpcs.npc.CitizensNPCManager;
|
import net.citizensnpcs.npc.CitizensNPCManager;
|
||||||
|
import net.citizensnpcs.storage.DatabaseStorage;
|
||||||
import net.citizensnpcs.storage.Storage;
|
import net.citizensnpcs.storage.Storage;
|
||||||
import net.citizensnpcs.storage.database.DatabaseStorage;
|
import net.citizensnpcs.storage.YamlStorage;
|
||||||
import net.citizensnpcs.storage.flatfile.YamlStorage;
|
|
||||||
import net.citizensnpcs.util.Messaging;
|
import net.citizensnpcs.util.Messaging;
|
||||||
import net.citizensnpcs.util.StringHelper;
|
import net.citizensnpcs.util.StringHelper;
|
||||||
import net.minecraft.server.MinecraftServer;
|
|
||||||
|
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
import org.bukkit.ChatColor;
|
import org.bukkit.ChatColor;
|
||||||
@ -58,7 +57,8 @@ public class Citizens extends JavaPlugin {
|
|||||||
private CommandManager cmdManager;
|
private CommandManager cmdManager;
|
||||||
private Settings config;
|
private Settings config;
|
||||||
private Storage saves;
|
private Storage saves;
|
||||||
private boolean skipDisable = false;
|
private final boolean compatible = ((CraftServer) getServer()).getServer().getVersion()
|
||||||
|
.startsWith(COMPATIBLE_MC_VERSION);
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onCommand(CommandSender sender, Command cmd, String cmdName, String[] args) {
|
public boolean onCommand(CommandSender sender, Command cmd, String cmdName, String[] args) {
|
||||||
@ -119,7 +119,7 @@ public class Citizens extends JavaPlugin {
|
|||||||
@Override
|
@Override
|
||||||
public void onDisable() {
|
public void onDisable() {
|
||||||
// Don't bother with this part if MC versions are not compatible
|
// Don't bother with this part if MC versions are not compatible
|
||||||
if (!skipDisable) {
|
if (compatible) {
|
||||||
config.save();
|
config.save();
|
||||||
saveNPCs();
|
saveNPCs();
|
||||||
for (NPC npc : npcManager)
|
for (NPC npc : npcManager)
|
||||||
@ -133,11 +133,10 @@ public class Citizens extends JavaPlugin {
|
|||||||
@Override
|
@Override
|
||||||
public void onEnable() {
|
public void onEnable() {
|
||||||
// Disable if the server is not using the compatible Minecraft version
|
// Disable if the server is not using the compatible Minecraft version
|
||||||
String mcVersion = ((MinecraftServer) ((CraftServer) getServer()).getServer()).getVersion();
|
String mcVersion = ((CraftServer) getServer()).getServer().getVersion();
|
||||||
if (!mcVersion.equals(COMPATIBLE_MC_VERSION)) {
|
if (!compatible) {
|
||||||
Messaging.log(Level.SEVERE, "v" + getDescription().getVersion() + " is not compatible with Minecraft v"
|
Messaging.log(Level.SEVERE, "v" + getDescription().getVersion() + " is not compatible with Minecraft v"
|
||||||
+ mcVersion + ". Disabling.");
|
+ mcVersion + ". Disabling.");
|
||||||
skipDisable = true;
|
|
||||||
getServer().getPluginManager().disablePlugin(this);
|
getServer().getPluginManager().disablePlugin(this);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -147,10 +146,11 @@ public class Citizens extends JavaPlugin {
|
|||||||
config.load();
|
config.load();
|
||||||
|
|
||||||
// NPC storage
|
// NPC storage
|
||||||
if (Setting.USE_DATABASE.getBoolean())
|
if (Setting.USE_DATABASE.asBoolean()) {
|
||||||
saves = new DatabaseStorage();
|
saves = new DatabaseStorage();
|
||||||
else
|
} else {
|
||||||
saves = new YamlStorage(getDataFolder() + File.separator + "saves.yml");
|
saves = new YamlStorage(getDataFolder() + File.separator + "saves.yml");
|
||||||
|
}
|
||||||
|
|
||||||
// Register API managers
|
// Register API managers
|
||||||
npcManager = new CitizensNPCManager(saves);
|
npcManager = new CitizensNPCManager(saves);
|
||||||
|
@ -91,11 +91,11 @@ public class EventListen implements Listener {
|
|||||||
Player player = (Player) event.getTarget();
|
Player player = (Player) event.getTarget();
|
||||||
if (!npcManager.npcIsSelectedByPlayer(player, npc)) {
|
if (!npcManager.npcIsSelectedByPlayer(player, npc)) {
|
||||||
if (player.hasPermission("citizens.npc.select")
|
if (player.hasPermission("citizens.npc.select")
|
||||||
&& player.getItemInHand().getTypeId() == Setting.SELECTION_ITEM.getInt()
|
&& player.getItemInHand().getTypeId() == Setting.SELECTION_ITEM.asInt()
|
||||||
&& npc.getTrait(Owner.class).getOwner().equals(player.getName())) {
|
&& npc.getTrait(Owner.class).getOwner().equals(player.getName())) {
|
||||||
npcManager.selectNPC(player, npc);
|
npcManager.selectNPC(player, npc);
|
||||||
Messaging.sendWithNPC(player, Setting.SELECTION_MESSAGE.getString(), npc);
|
Messaging.sendWithNPC(player, Setting.SELECTION_MESSAGE.asString(), npc);
|
||||||
if (!Setting.QUICK_SELECT.getBoolean())
|
if (!Setting.QUICK_SELECT.asBoolean())
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -3,7 +3,7 @@ package net.citizensnpcs;
|
|||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
||||||
import net.citizensnpcs.api.DataKey;
|
import net.citizensnpcs.api.DataKey;
|
||||||
import net.citizensnpcs.storage.flatfile.YamlStorage;
|
import net.citizensnpcs.storage.YamlStorage;
|
||||||
import net.citizensnpcs.util.Messaging;
|
import net.citizensnpcs.util.Messaging;
|
||||||
|
|
||||||
public class Settings {
|
public class Settings {
|
||||||
@ -16,11 +16,12 @@ public class Settings {
|
|||||||
public void load() {
|
public void load() {
|
||||||
DataKey root = config.getKey("");
|
DataKey root = config.getKey("");
|
||||||
for (Setting setting : Setting.values()) {
|
for (Setting setting : Setting.values()) {
|
||||||
if (!root.keyExists(setting.getPath())) {
|
if (!root.keyExists(setting.path)) {
|
||||||
Messaging.log("Writing default setting: '" + setting.getPath() + "'");
|
Messaging.log("Writing default setting: '" + setting.path + "'");
|
||||||
root.setRaw(setting.getPath(), setting.get());
|
root.setRaw(setting.path, setting.get());
|
||||||
} else
|
} else {
|
||||||
setting.set(root.getRaw(setting.getPath()));
|
setting.set(root.getRaw(setting.path));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
save();
|
save();
|
||||||
}
|
}
|
||||||
@ -48,27 +49,23 @@ public class Settings {
|
|||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean getBoolean() {
|
public boolean asBoolean() {
|
||||||
return (Boolean) value;
|
return (Boolean) value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public double getDouble() {
|
public double asDouble() {
|
||||||
return (Double) value;
|
return (Double) value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int getInt() {
|
public int asInt() {
|
||||||
return (Integer) value;
|
return (Integer) value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public long getLong() {
|
public long asLong() {
|
||||||
return (Long) value;
|
return (Long) value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getPath() {
|
public String asString() {
|
||||||
return path;
|
|
||||||
}
|
|
||||||
|
|
||||||
public String getString() {
|
|
||||||
return value.toString();
|
return value.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -131,7 +131,7 @@ public class NPCCommands {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
npcManager.selectNPC(player, toSelect);
|
npcManager.selectNPC(player, toSelect);
|
||||||
Messaging.sendWithNPC(player, Setting.SELECTION_MESSAGE.getString(), toSelect);
|
Messaging.sendWithNPC(player, Setting.SELECTION_MESSAGE.asString(), toSelect);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Command(
|
@Command(
|
||||||
|
@ -80,16 +80,19 @@ public class CitizensNPC extends AbstractNPC {
|
|||||||
mcEntity = manager.spawn(this, loc);
|
mcEntity = manager.spawn(this, loc);
|
||||||
|
|
||||||
// Set the location
|
// Set the location
|
||||||
if (!hasTrait(SpawnLocation.class))
|
// TODO: do this automatically (default traits?)
|
||||||
|
// TODO: is spawned as a trait needed? Takes up memory
|
||||||
|
if (!hasTrait(SpawnLocation.class)) {
|
||||||
addTrait(new SpawnLocation(loc));
|
addTrait(new SpawnLocation(loc));
|
||||||
else
|
} else {
|
||||||
getTrait(SpawnLocation.class).setLocation(loc);
|
getTrait(SpawnLocation.class).setLocation(loc);
|
||||||
|
}
|
||||||
|
|
||||||
if (!hasTrait(Spawned.class))
|
if (!hasTrait(Spawned.class)) {
|
||||||
addTrait(new Spawned(true));
|
addTrait(new Spawned(true));
|
||||||
else
|
} else {
|
||||||
getTrait(Spawned.class).setSpawned(true);
|
getTrait(Spawned.class).setSpawned(true);
|
||||||
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -15,7 +15,6 @@ import net.citizensnpcs.util.ByIdArray;
|
|||||||
import net.citizensnpcs.util.Messaging;
|
import net.citizensnpcs.util.Messaging;
|
||||||
import net.minecraft.server.ItemInWorldManager;
|
import net.minecraft.server.ItemInWorldManager;
|
||||||
import net.minecraft.server.MinecraftServer;
|
import net.minecraft.server.MinecraftServer;
|
||||||
import net.minecraft.server.Packet29DestroyEntity;
|
|
||||||
import net.minecraft.server.WorldServer;
|
import net.minecraft.server.WorldServer;
|
||||||
|
|
||||||
import org.bukkit.Bukkit;
|
import org.bukkit.Bukkit;
|
||||||
@ -24,7 +23,6 @@ import org.bukkit.Server;
|
|||||||
import org.bukkit.World;
|
import org.bukkit.World;
|
||||||
import org.bukkit.craftbukkit.CraftServer;
|
import org.bukkit.craftbukkit.CraftServer;
|
||||||
import org.bukkit.craftbukkit.CraftWorld;
|
import org.bukkit.craftbukkit.CraftWorld;
|
||||||
import org.bukkit.craftbukkit.entity.CraftPlayer;
|
|
||||||
import org.bukkit.entity.Entity;
|
import org.bukkit.entity.Entity;
|
||||||
import org.bukkit.entity.Player;
|
import org.bukkit.entity.Player;
|
||||||
|
|
||||||
@ -61,14 +59,9 @@ public class CitizensNPCManager implements NPCManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public void despawn(NPC npc) {
|
public void despawn(NPC npc) {
|
||||||
CraftNPC mcEntity = ((CitizensNPC) npc).getHandle();
|
npc.getTrait(SpawnLocation.class).setLocation(npc.getBukkitEntity().getLocation());
|
||||||
Location loc = npc.getBukkitEntity().getLocation();
|
|
||||||
npc.getTrait(SpawnLocation.class).setLocation(loc);
|
|
||||||
|
|
||||||
selected.removeAll(npc.getId());
|
selected.removeAll(npc.getId());
|
||||||
for (Player player : Bukkit.getOnlinePlayers())
|
npc.getBukkitEntity().remove();
|
||||||
((CraftPlayer) player).getHandle().netServerHandler.sendPacket(new Packet29DestroyEntity(mcEntity.id));
|
|
||||||
mcEntity.die();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -1,9 +1,8 @@
|
|||||||
package net.citizensnpcs.storage.database;
|
package net.citizensnpcs.storage;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import net.citizensnpcs.api.DataKey;
|
import net.citizensnpcs.api.DataKey;
|
||||||
import net.citizensnpcs.storage.Storage;
|
|
||||||
|
|
||||||
public class DatabaseStorage implements Storage {
|
public class DatabaseStorage implements Storage {
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package net.citizensnpcs.storage.flatfile;
|
package net.citizensnpcs.storage;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@ -7,14 +7,13 @@ import java.util.Collections;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.logging.Level;
|
import java.util.logging.Level;
|
||||||
|
|
||||||
|
import net.citizensnpcs.api.DataKey;
|
||||||
|
import net.citizensnpcs.util.Messaging;
|
||||||
|
|
||||||
import org.bukkit.configuration.ConfigurationSection;
|
import org.bukkit.configuration.ConfigurationSection;
|
||||||
import org.bukkit.configuration.file.FileConfiguration;
|
import org.bukkit.configuration.file.FileConfiguration;
|
||||||
import org.bukkit.configuration.file.YamlConfiguration;
|
import org.bukkit.configuration.file.YamlConfiguration;
|
||||||
|
|
||||||
import net.citizensnpcs.api.DataKey;
|
|
||||||
import net.citizensnpcs.storage.Storage;
|
|
||||||
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;
|
||||||
@ -172,6 +171,8 @@ public class YamlStorage implements Storage {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public DataKey getRelative(String relative) {
|
public DataKey getRelative(String relative) {
|
||||||
|
if (relative == null || relative.isEmpty())
|
||||||
|
return this;
|
||||||
return new YamlKey(getKeyExt(relative));
|
return new YamlKey(getKeyExt(relative));
|
||||||
}
|
}
|
||||||
|
|
@ -12,7 +12,7 @@ import org.bukkit.entity.Player;
|
|||||||
public class Messaging {
|
public class Messaging {
|
||||||
|
|
||||||
public static void debug(Object msg) {
|
public static void debug(Object msg) {
|
||||||
if (Setting.DEBUG_MODE.getBoolean())
|
if (Setting.DEBUG_MODE.asBoolean())
|
||||||
log(msg);
|
log(msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -25,28 +25,25 @@ public class Messaging {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void send(Player player, Object msg) {
|
public static void send(Player player, Object msg) {
|
||||||
String send = "" + msg;
|
String send = msg.toString();
|
||||||
|
|
||||||
for (ChatColor color : ChatColor.values()) {
|
for (ChatColor color : ChatColor.values()) {
|
||||||
if (send.contains("<" + color.getChar() + ">"))
|
send = send.replace("<" + color.getChar() + ">", color.toString());
|
||||||
send = send.replace("<" + color.getChar() + ">", "" + ChatColor.getByChar(color.getChar()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
player.sendMessage(send);
|
player.sendMessage(send);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void sendWithNPC(Player player, Object msg, NPC npc) {
|
public static void sendWithNPC(Player player, Object msg, NPC npc) {
|
||||||
String send = "" + msg;
|
String send = msg.toString();
|
||||||
|
|
||||||
if (send.contains("<npc>"))
|
send = send.replace("<npc>", npc.getName());
|
||||||
send = send.replace("<npc>", npc.getName());
|
send = send.replace("<id>", Integer.toString(npc.getId()));
|
||||||
if (send.contains("<id>"))
|
|
||||||
send = send.replace("<id>", "" + npc.getId());
|
|
||||||
|
|
||||||
send(player, send);
|
send(player, send);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void sendError(Player player, Object msg) {
|
public static void sendError(Player player, Object msg) {
|
||||||
send(player, "" + ChatColor.RED + msg);
|
send(player, ChatColor.RED.toString() + msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user