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