1.0 compatibility

This commit is contained in:
Jascha Starke 2012-01-17 23:31:35 +01:00
parent a2ad1b65bb
commit 866118a0f2
3 changed files with 51 additions and 5 deletions

View file

@ -11,6 +11,8 @@ public class LimitedCreativeCore extends JavaPlugin {
public WorldGuardIntegration worldguard; public WorldGuardIntegration worldguard;
public static LimitedCreativeCore plugin; public static LimitedCreativeCore plugin;
public static boolean serializeFallBack = false;
@Override @Override
public void onDisable() { public void onDisable() {
logger.info("["+this.getDescription().getName()+"] cleanly unloaded."); logger.info("["+this.getDescription().getName()+"] cleanly unloaded.");
@ -20,6 +22,8 @@ public class LimitedCreativeCore extends JavaPlugin {
public void onEnable() { public void onEnable() {
plugin = this; plugin = this;
serializeFallBack = versionCompare(getServer().getBukkitVersion().replaceAll("-.*$", ""), "1.1") < 0;
config = new Configuration(this.getConfig()); config = new Configuration(this.getConfig());
Listener.register(this); Listener.register(this);
Commands.register(this); Commands.register(this);
@ -32,4 +36,18 @@ public class LimitedCreativeCore extends JavaPlugin {
PluginDescriptionFile df = this.getDescription(); PluginDescriptionFile df = this.getDescription();
logger.info("["+df.getName() + " v" + df.getVersion() + "] done loading."); logger.info("["+df.getName() + " v" + df.getVersion() + "] done loading.");
} }
public static int versionCompare(String vers1, String vers2) {
String[] v1 = vers1.split("\\.");
String[] v2 = vers2.split("\\.");
int i = 0;
while (i < v1.length && i < v2.length && v1[i].equals(v2[i])) {
i++;
}
if (i < v1.length && i < v2.length) {
int diff = new Integer(v1[i]).compareTo(new Integer(v2[i]));
return diff < 0 ? -1 : (diff == 0 ? 0 : 1);
}
return v1.length < v2.length ? -1 : (v1.length == v2.length ? 0 : 1);
}
} }

View file

@ -65,7 +65,7 @@ public final class Listener {
event.setCancelled(true); event.setCancelled(true);
} }
if (plugin.config.getSignBlock() && block.getState() instanceof Sign) { if (plugin.config.getSignBlock() && block.getState() instanceof Sign) {
event.getPlayer().sendMessage("Access to interact with signs is not allowed in creative mode"); event.getPlayer().sendMessage("To interact with signs is not allowed in creative mode");
event.setCancelled(true); event.setCancelled(true);
} }
} }

View file

@ -1,8 +1,14 @@
package de.jaschastarke.minecraft.limitedcreative.serialize; package de.jaschastarke.minecraft.limitedcreative.serialize;
import java.util.Map;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory; import org.bukkit.inventory.PlayerInventory;
import de.jaschastarke.minecraft.limitedcreative.LimitedCreativeCore;
public class Items implements Storeable { public class Items implements Storeable {
private PlayerInventory inv; private PlayerInventory inv;
public Items(PlayerInventory pi) { public Items(PlayerInventory pi) {
@ -13,7 +19,7 @@ public class Items implements Storeable {
public void store(ConfigurationSection section) { public void store(ConfigurationSection section) {
for (int i = 0; i < inv.getSize(); i++) { for (int i = 0; i < inv.getSize(); i++) {
if (inv.getItem(i) != null && inv.getItem(i).getTypeId() != 0) if (inv.getItem(i) != null && inv.getItem(i).getTypeId() != 0)
section.set(String.valueOf(i), inv.getItem(i)); sectionSetItem(section, String.valueOf(i), inv.getItem(i));
} }
} }
@ -21,10 +27,32 @@ public class Items implements Storeable {
public void restore(ConfigurationSection section) { public void restore(ConfigurationSection section) {
inv.clear(); inv.clear();
for (int i = 0; i < inv.getSize(); i++) { for (int i = 0; i < inv.getSize(); i++) {
if (section.contains(String.valueOf(i))) { if (section.contains(String.valueOf(i)))
inv.setItem(i, section.getItemStack(String.valueOf(i))); inv.setItem(i, sectionGetItem(section, String.valueOf(i)));
}
} }
} }
public static void sectionSetItem(ConfigurationSection section, String path, ItemStack item) {
if (!LimitedCreativeCore.serializeFallBack) {
section.set(path, item);
} else { // compatibility fallback
Map<String, Object> serialize = item.serialize();
if (serialize.containsKey("type") && serialize.get("type") instanceof Material)
serialize.put("type", serialize.get("type").toString());
section.createSection(path, serialize);
};
}
public static ItemStack sectionGetItem(ConfigurationSection section, String path) {
if (section.isItemStack(path)) {
return section.getItemStack(path);
} else { // compatibility fallback
ConfigurationSection s = section.getConfigurationSection(path);
Map<String, Object> serialize = s.getValues(false);
if (s.contains("enchantments"))
serialize.put("enchantments", s.getConfigurationSection("enchantments").getValues(false));
if (s.contains("damage"))
serialize.put("damage", new Integer(s.getInt("damage")).shortValue());
return ItemStack.deserialize(serialize);
}
}
} }