From 9728521ea7920826b16829bf3d97a1f329029bb2 Mon Sep 17 00:00:00 2001 From: aPunch Date: Sun, 29 Jan 2012 14:58:19 -0600 Subject: [PATCH] added incompatibility error prevention --- src/net/citizensnpcs/Citizens.java | 35 ++++++++++++++----- .../citizensnpcs/npc/CitizensNPCManager.java | 2 ++ 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/net/citizensnpcs/Citizens.java b/src/net/citizensnpcs/Citizens.java index 7a4f7e35b..3b35db433 100644 --- a/src/net/citizensnpcs/Citizens.java +++ b/src/net/citizensnpcs/Citizens.java @@ -7,6 +7,7 @@ import java.util.Map; import java.util.Map.Entry; import java.util.Set; import java.util.TreeMap; +import java.util.logging.Level; import net.citizensnpcs.Settings.Setting; import net.citizensnpcs.api.CitizensAPI; @@ -36,24 +37,28 @@ import net.citizensnpcs.storage.database.DatabaseStorage; import net.citizensnpcs.storage.flatfile.YamlStorage; import net.citizensnpcs.util.Messaging; import net.citizensnpcs.util.StringHelper; +import net.minecraft.server.MinecraftServer; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.command.CommandSender; +import org.bukkit.craftbukkit.CraftServer; import org.bukkit.entity.Player; import org.bukkit.permissions.Permission; import org.bukkit.permissions.PermissionDefault; import org.bukkit.plugin.java.JavaPlugin; public class Citizens extends JavaPlugin { - private static Storage saves; + private static final String COMPATIBLE_MC_VERSION = "1.1"; private volatile CitizensNPCManager npcManager; private final InstanceFactory characterManager = new DefaultInstanceFactory(); private final InstanceFactory traitManager = new DefaultInstanceFactory(); private CommandManager cmdManager; private Settings config; + private Storage saves; + private boolean skipDisable = false; @Override public boolean onCommand(CommandSender sender, Command cmd, String cmdName, String[] args) { @@ -113,18 +118,30 @@ public class Citizens extends JavaPlugin { @Override public void onDisable() { - // Save and despawn all NPCs - config.save(); - saveNPCs(); - for (NPC npc : npcManager) - npc.despawn(); - Bukkit.getScheduler().cancelTasks(this); + // Don't bother with this part if MC versions are not compatible + if (!skipDisable) { + config.save(); + saveNPCs(); + for (NPC npc : npcManager) + npc.despawn(); + Bukkit.getScheduler().cancelTasks(this); + } Messaging.log("v" + getDescription().getVersion() + " disabled."); } @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)) { + Messaging.log(Level.SEVERE, "v" + getDescription().getVersion() + " is not compatible with Minecraft v" + + mcVersion + ". Disabling."); + skipDisable = true; + getServer().getPluginManager().disablePlugin(this); + return; + } + // Configuration file config = new Settings(this); config.load(); @@ -158,11 +175,11 @@ public class Citizens extends JavaPlugin { try { setupNPCs(); } catch (NPCLoadException ex) { - Messaging.log("Issue when loading NPCs: " + ex.getMessage()); + Messaging.log(Level.SEVERE, "Issue when loading NPCs: " + ex.getMessage()); } } }) == -1) { - Messaging.log("Issue enabling plugin. Disabling."); + Messaging.log(Level.SEVERE, "Issue enabling plugin. Disabling."); getServer().getPluginManager().disablePlugin(this); } } diff --git a/src/net/citizensnpcs/npc/CitizensNPCManager.java b/src/net/citizensnpcs/npc/CitizensNPCManager.java index eb56e09d7..6a0f894dd 100644 --- a/src/net/citizensnpcs/npc/CitizensNPCManager.java +++ b/src/net/citizensnpcs/npc/CitizensNPCManager.java @@ -12,6 +12,7 @@ import net.citizensnpcs.api.npc.trait.trait.SpawnLocation; import net.citizensnpcs.resource.lib.CraftNPC; import net.citizensnpcs.storage.Storage; 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; @@ -81,6 +82,7 @@ public class CitizensNPCManager implements NPCManager { @Override public NPC getNPC(Entity entity) { + Messaging.log("Version: " + getMinecraftServer(Bukkit.getServer()).getVersion()); for (NPC npc : npcs) if (npc.isSpawned() && npc.getBukkitEntity().getEntityId() == entity.getEntityId()) return npc;