added incompatibility error prevention

This commit is contained in:
aPunch 2012-01-29 14:58:19 -06:00
parent 49d8034bd2
commit a0f7d8c130
2 changed files with 28 additions and 9 deletions

View File

@ -7,6 +7,7 @@ import java.util.Map;
import java.util.Map.Entry; import java.util.Map.Entry;
import java.util.Set; import java.util.Set;
import java.util.TreeMap; import java.util.TreeMap;
import java.util.logging.Level;
import net.citizensnpcs.Settings.Setting; import net.citizensnpcs.Settings.Setting;
import net.citizensnpcs.api.CitizensAPI; import net.citizensnpcs.api.CitizensAPI;
@ -36,24 +37,28 @@ import net.citizensnpcs.storage.database.DatabaseStorage;
import net.citizensnpcs.storage.flatfile.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;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.craftbukkit.CraftServer;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.permissions.Permission; import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionDefault; import org.bukkit.permissions.PermissionDefault;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
public class Citizens extends JavaPlugin { public class Citizens extends JavaPlugin {
private static Storage saves; private static final String COMPATIBLE_MC_VERSION = "1.1";
private volatile CitizensNPCManager npcManager; private volatile CitizensNPCManager npcManager;
private final InstanceFactory<Character> characterManager = new DefaultInstanceFactory<Character>(); private final InstanceFactory<Character> characterManager = new DefaultInstanceFactory<Character>();
private final InstanceFactory<Trait> traitManager = new DefaultInstanceFactory<Trait>(); private final InstanceFactory<Trait> traitManager = new DefaultInstanceFactory<Trait>();
private CommandManager cmdManager; private CommandManager cmdManager;
private Settings config; private Settings config;
private Storage saves;
private boolean skipDisable = false;
@Override @Override
public boolean onCommand(CommandSender sender, Command cmd, String cmdName, String[] args) { public boolean onCommand(CommandSender sender, Command cmd, String cmdName, String[] args) {
@ -113,18 +118,30 @@ public class Citizens extends JavaPlugin {
@Override @Override
public void onDisable() { public void onDisable() {
// Save and despawn all NPCs // Don't bother with this part if MC versions are not compatible
config.save(); if (!skipDisable) {
saveNPCs(); config.save();
for (NPC npc : npcManager) saveNPCs();
npc.despawn(); for (NPC npc : npcManager)
Bukkit.getScheduler().cancelTasks(this); npc.despawn();
Bukkit.getScheduler().cancelTasks(this);
}
Messaging.log("v" + getDescription().getVersion() + " disabled."); Messaging.log("v" + getDescription().getVersion() + " disabled.");
} }
@Override @Override
public void onEnable() { 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 // Configuration file
config = new Settings(this); config = new Settings(this);
config.load(); config.load();
@ -158,11 +175,11 @@ public class Citizens extends JavaPlugin {
try { try {
setupNPCs(); setupNPCs();
} catch (NPCLoadException ex) { } catch (NPCLoadException ex) {
Messaging.log("Issue when loading NPCs: " + ex.getMessage()); Messaging.log(Level.SEVERE, "Issue when loading NPCs: " + ex.getMessage());
} }
} }
}) == -1) { }) == -1) {
Messaging.log("Issue enabling plugin. Disabling."); Messaging.log(Level.SEVERE, "Issue enabling plugin. Disabling.");
getServer().getPluginManager().disablePlugin(this); getServer().getPluginManager().disablePlugin(this);
} }
} }

View File

@ -12,6 +12,7 @@ import net.citizensnpcs.api.npc.trait.trait.SpawnLocation;
import net.citizensnpcs.resource.lib.CraftNPC; import net.citizensnpcs.resource.lib.CraftNPC;
import net.citizensnpcs.storage.Storage; import net.citizensnpcs.storage.Storage;
import net.citizensnpcs.util.ByIdArray; import net.citizensnpcs.util.ByIdArray;
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.Packet29DestroyEntity;
@ -81,6 +82,7 @@ public class CitizensNPCManager implements NPCManager {
@Override @Override
public NPC getNPC(Entity entity) { public NPC getNPC(Entity entity) {
Messaging.log("Version: " + getMinecraftServer(Bukkit.getServer()).getVersion());
for (NPC npc : npcs) for (NPC npc : npcs)
if (npc.isSpawned() && npc.getBukkitEntity().getEntityId() == entity.getEntityId()) if (npc.isSpawned() && npc.getBukkitEntity().getEntityId() == entity.getEntityId())
return npc; return npc;