diff --git a/.gitignore b/.gitignore index 7961ff4..46af922 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,6 @@ # maven /target + +# Mac stuff +.DS_Store \ No newline at end of file diff --git a/pom.xml b/pom.xml index f020bf8..d43cf78 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ de.jaschastarke LimitedCreative LimitedCreative - 1.4.1a-SNAPSHOT + 2.0-SNAPSHOT https://github.com/possi/LimitedCreative @@ -17,11 +17,14 @@ - + - opt-dep - http://dl.dropbox.com/u/5023975/mvn-repo + de-jas-mvn + https://repository-possi.forge.cloudbees.com/release + bukkit-repo @@ -37,27 +40,44 @@ onarandombox http://repo.onarandombox.com/content/groups/public + - vault-repo - http://ci.herocraftonline.com/plugin/repository/everything + vault-repo + http://ci.herocraftonline.com/plugin/repository/everything + org.bukkit bukkit - 1.4.2-R0.2 + 1.4.6-R0.1 + + + org.bukkit + craftbukkit + 1.4.6-R0.1 + + + com.sk89q + worldedit + 5.4.5 + compile com.sk89q worldguard - 5.6.3 + 5.6.6-SNAPSHOT uk.org.whoami authme - 2.6.7b5 + 2.7.1 com.onarandombox.multiversecore @@ -67,7 +87,7 @@ com.cypherx xauth - 2.0.20 + 2.0.26 de.jaschastarke @@ -89,12 +109,21 @@ plugin.yml config.yml + META-INF/descriptions.jos lang/ ${basedir}/lang/ + + META-INF/ + true + ${project.build.directory}/generated-sources/annotations/META-INF/ + + descriptions.jos + + diff --git a/src/de/jaschastarke/minecraft/limitedcreative/Config.java b/src/de/jaschastarke/minecraft/limitedcreative/Config.java new file mode 100644 index 0000000..a8ac42b --- /dev/null +++ b/src/de/jaschastarke/minecraft/limitedcreative/Config.java @@ -0,0 +1,31 @@ +package de.jaschastarke.minecraft.limitedcreative; + +import java.io.IOException; + +import de.jaschastarke.bukkit.lib.Core; +import de.jaschastarke.bukkit.lib.configuration.PluginConfiguration; +import de.jaschastarke.maven.ArchiveDocComments; +import de.jaschastarke.utils.ClassDescriptorStorage; + +/** + * Limited Creative - Configuration + * + * (YAML-Syntax: http://en.wikipedia.org/wiki/YAML) + */ +@ArchiveDocComments +public class Config extends PluginConfiguration { + public Config(Core plugin) { + super(plugin); + } + + @Override + public void save() { + try { + ClassDescriptorStorage.load(plugin.getResource("META-INF/descriptions.jos")); + } catch (IOException e) { + plugin.getLog().severe("Failed to load ConfigNode-Descriptions"); + } + super.save(); + } + +} diff --git a/src/de/jaschastarke/minecraft/limitedcreative/FeatureBlockItemSpawn.java b/src/de/jaschastarke/minecraft/limitedcreative/FeatureBlockItemSpawn.java index de4b819..4139f88 100644 --- a/src/de/jaschastarke/minecraft/limitedcreative/FeatureBlockItemSpawn.java +++ b/src/de/jaschastarke/minecraft/limitedcreative/FeatureBlockItemSpawn.java @@ -15,11 +15,10 @@ import org.bukkit.event.Listener; import org.bukkit.event.entity.ItemSpawnEvent; import org.bukkit.inventory.ItemStack; -import de.jaschastarke.bukkit.lib.Core; import de.jaschastarke.bukkit.lib.CoreModule; -public class FeatureBlockItemSpawn extends CoreModule implements Listener { - public FeatureBlockItemSpawn(Core plugin) { +public class FeatureBlockItemSpawn extends CoreModule implements Listener { + public FeatureBlockItemSpawn(LimitedCreative plugin) { super(plugin); } public final static long TIME_OFFSET = 250; diff --git a/src/de/jaschastarke/minecraft/limitedcreative/LimitedCreative.java b/src/de/jaschastarke/minecraft/limitedcreative/LimitedCreative.java index c8d1e03..e003a07 100644 --- a/src/de/jaschastarke/minecraft/limitedcreative/LimitedCreative.java +++ b/src/de/jaschastarke/minecraft/limitedcreative/LimitedCreative.java @@ -5,13 +5,23 @@ import de.jaschastarke.bukkit.lib.Core; public class LimitedCreative extends Core { private i18n lang; + protected Config config = null; + @Override public void OnInitialize() { + config = new Config(this); lang = new i18n("lang/messages"); addModule(new ModInventories(this)); addModule(new ModCreativeLimits(this)); addModule(new ModRegions(this)); addModule(new ModCmdBlocker(this)); + Hooks.inizializeHooks(this); + + config.save(); + } + + public Config getPluginConfig() { + return config; } @Deprecated diff --git a/src/de/jaschastarke/minecraft/limitedcreative/ModCmdBlocker.java b/src/de/jaschastarke/minecraft/limitedcreative/ModCmdBlocker.java index 58463f0..06e5dcf 100644 --- a/src/de/jaschastarke/minecraft/limitedcreative/ModCmdBlocker.java +++ b/src/de/jaschastarke/minecraft/limitedcreative/ModCmdBlocker.java @@ -1,11 +1,10 @@ package de.jaschastarke.minecraft.limitedcreative; -import de.jaschastarke.bukkit.lib.Core; import de.jaschastarke.bukkit.lib.CoreModule; -public class ModCmdBlocker extends CoreModule { +public class ModCmdBlocker extends CoreModule { - public ModCmdBlocker(Core plugin) { + public ModCmdBlocker(LimitedCreative plugin) { super(plugin); } diff --git a/src/de/jaschastarke/minecraft/limitedcreative/ModCreativeLimits.java b/src/de/jaschastarke/minecraft/limitedcreative/ModCreativeLimits.java index a3017f1..545942b 100644 --- a/src/de/jaschastarke/minecraft/limitedcreative/ModCreativeLimits.java +++ b/src/de/jaschastarke/minecraft/limitedcreative/ModCreativeLimits.java @@ -1,12 +1,11 @@ package de.jaschastarke.minecraft.limitedcreative; -import de.jaschastarke.bukkit.lib.Core; import de.jaschastarke.bukkit.lib.CoreModule; import de.jaschastarke.modularize.IModule; import de.jaschastarke.modularize.ModuleEntry; -public class ModCreativeLimits extends CoreModule { - public ModCreativeLimits(Core plugin) { +public class ModCreativeLimits extends CoreModule { + public ModCreativeLimits(LimitedCreative plugin) { super(plugin); } diff --git a/src/de/jaschastarke/minecraft/limitedcreative/ModInventories.java b/src/de/jaschastarke/minecraft/limitedcreative/ModInventories.java index caf56bc..3846e81 100644 --- a/src/de/jaschastarke/minecraft/limitedcreative/ModInventories.java +++ b/src/de/jaschastarke/minecraft/limitedcreative/ModInventories.java @@ -1,58 +1,114 @@ package de.jaschastarke.minecraft.limitedcreative; +import java.io.File; +import java.util.Map; +import java.util.WeakHashMap; + import org.bukkit.GameMode; import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.material.MaterialData; -import de.jaschastarke.bukkit.lib.Core; import de.jaschastarke.bukkit.lib.CoreModule; +import de.jaschastarke.bukkit.lib.permissions.PermissionManager; +import de.jaschastarke.minecraft.limitedcreative.inventories.ArmoryConfig; +import de.jaschastarke.minecraft.limitedcreative.inventories.Inventory; +import de.jaschastarke.minecraft.limitedcreative.inventories.InventoryConfig; +import de.jaschastarke.minecraft.limitedcreative.inventories.InventoryPermissions; import de.jaschastarke.minecraft.limitedcreative.inventories.PlayerListener; +import de.jaschastarke.minecraft.limitedcreative.inventories.store.InvYamlStorage; +import de.jaschastarke.minecraft.limitedcreative.inventories.store.PlayerInventoryStorage; import de.jaschastarke.modularize.IModule; import de.jaschastarke.modularize.ModuleEntry; -public class ModInventories extends CoreModule { - public ModInventories(Core plugin) { +public class ModInventories extends CoreModule { + protected PlayerInventoryStorage storage; + protected Map inventories; + protected InventoryConfig config; + protected ArmoryConfig armor_config; + + public ModInventories(LimitedCreative plugin) { super(plugin); } @Override public void Initialize(ModuleEntry entry) { super.Initialize(entry); - listeners.registerEvents(new PlayerListener(this)); + listeners.addListener(new PlayerListener(this)); + config = plugin.getPluginConfig().registerSection(new InventoryConfig(this)); + armor_config = config.registerSection(new ArmoryConfig(this)); + } + @Override + public void OnEnable() { + super.OnEnable(); + storage = new InvYamlStorage(this, new File(plugin.getDataFolder(), config.getFolder())); + inventories = new WeakHashMap(); + } + public InventoryConfig getConfig() { + return config; } + public PlayerInventoryStorage getStorage() { + return storage; + } + + public Inventory getInventory(Player player) { + if (inventories.containsKey(player)) { + return inventories.get(player); + } else { + Inventory inv = new Inventory(storage, player); + inventories.put(player, inv); + return inv; + } + } + public void onSetGameMode(Player player, GameMode gm) { - //if (hasPermission(Perms.KEEPINVENTORY)) - // return true; + if (PermissionManager.hasPermission(player, InventoryPermissions.KEEP_INVENTORY)) + return; player.closeInventory(); - - /* GameMode cgm = player.getGameMode(); - if (gm == GameMode.ADVENTURE && !plugin.config.getAdventureInv()) + if (gm == GameMode.ADVENTURE && !config.getSeparateAdventure()) gm = GameMode.SURVIVAL; - if (cgm == GameMode.ADVENTURE && !plugin.config.getAdventureInv()) + if (cgm == GameMode.ADVENTURE && !config.getSeparateAdventure()) cgm = GameMode.SURVIVAL; if (gm != cgm) { - if (gm != GameMode.CREATIVE || plugin.config.getStoreCreative()) { - getInv().save(cgm); + if (gm != GameMode.CREATIVE || config.getStoreCreative()) { + getInventory(player).save(cgm); } if (gm == GameMode.CREATIVE) { - if (plugin.config.getStoreCreative() && getInv().isStored(GameMode.CREATIVE)) { - getInv().load(GameMode.CREATIVE); + if (config.getStoreCreative() && getInventory(player).isStored(GameMode.CREATIVE)) { + getInventory(player).load(GameMode.CREATIVE); } else { - getInv().clear(); + getInventory(player).clear(); } - setCreativeArmor(); + setCreativeArmor(player); } else if (gm == GameMode.SURVIVAL) { - if (getInv().isStored(GameMode.SURVIVAL)) - getInv().load(GameMode.SURVIVAL); + if (getInventory(player).isStored(GameMode.SURVIVAL)) + getInventory(player).load(GameMode.SURVIVAL); } else if (gm == GameMode.ADVENTURE) { - if (getInv().isStored(GameMode.ADVENTURE)) - getInv().load(GameMode.ADVENTURE); + if (getInventory(player).isStored(GameMode.ADVENTURE)) + getInventory(player).load(GameMode.ADVENTURE); else - getInv().clear(); + getInventory(player).clear(); } - }*/ + } + } + + public void setCreativeArmor(Player player) { + Map armor = armor_config.getCreativeArmor(); + if (armor != null) { + ItemStack[] is = new ItemStack[4]; + if (armor.containsKey("feet")) + is[0] = armor.get("feet").toItemStack(1); + if (armor.containsKey("legs")) + is[1] = armor.get("legs").toItemStack(1); + if (armor.containsKey("chest")) + is[2] = armor.get("chest").toItemStack(1); + if (armor.containsKey("head")) + is[3] = armor.get("head").toItemStack(1); + player.getInventory().setArmorContents(is); + } } } diff --git a/src/de/jaschastarke/minecraft/limitedcreative/ModRegions.java b/src/de/jaschastarke/minecraft/limitedcreative/ModRegions.java index eb7fbdf..7091bf4 100644 --- a/src/de/jaschastarke/minecraft/limitedcreative/ModRegions.java +++ b/src/de/jaschastarke/minecraft/limitedcreative/ModRegions.java @@ -1,11 +1,9 @@ package de.jaschastarke.minecraft.limitedcreative; -import de.jaschastarke.bukkit.lib.Core; import de.jaschastarke.bukkit.lib.CoreModule; -public class ModRegions extends CoreModule { - - public ModRegions(Core plugin) { +public class ModRegions extends CoreModule { + public ModRegions(LimitedCreative plugin) { super(plugin); } diff --git a/src/de/jaschastarke/minecraft/limitedcreative/inventories/ArmoryConfig.java b/src/de/jaschastarke/minecraft/limitedcreative/inventories/ArmoryConfig.java new file mode 100644 index 0000000..9f13997 --- /dev/null +++ b/src/de/jaschastarke/minecraft/limitedcreative/inventories/ArmoryConfig.java @@ -0,0 +1,86 @@ +package de.jaschastarke.minecraft.limitedcreative.inventories; + +import java.util.HashMap; +import java.util.Map; + +import org.bukkit.Bukkit; +import org.bukkit.material.MaterialData; + +import de.jaschastarke.bukkit.lib.ModuleLogger; +import de.jaschastarke.bukkit.lib.configuration.Configuration; +import de.jaschastarke.bukkit.lib.items.MaterialDataNotRecognizedException; +import de.jaschastarke.bukkit.lib.items.MaterilNotRecognizedException; +import de.jaschastarke.bukkit.lib.items.Utils; +import de.jaschastarke.configuration.annotations.IConfigurationSubGroup; +import de.jaschastarke.configuration.annotations.IsConfigurationNode; +import de.jaschastarke.maven.ArchiveDocComments; +import de.jaschastarke.minecraft.limitedcreative.LimitedCreative; +import de.jaschastarke.minecraft.limitedcreative.ModInventories; + +/** + * CreativeArmor + * + * When set, all creative Player automatically wears the given items as Armor. So they are better seen by other Players. + */ +@ArchiveDocComments +public class ArmoryConfig extends Configuration implements IConfigurationSubGroup { + protected ModInventories mod; + public ArmoryConfig(ModInventories modInventories) { + mod = modInventories; + } + @Override + public String getNodeName() { + return "armor"; + } + + /** + * CreativeArmorEnabled + * + * When disabled, the players Armor isn't swapped + * + * default: true + */ + @IsConfigurationNode + public boolean getEnabled() { + return config.getBoolean("enabled", true); + } + + /** + * CreativeArmor-Items + * + * Allows changing of the "Creative-Armor" to be wear when in creative mode + * + * *see Blacklist for details on Item-Types + */ + public Map getCreativeArmor() { + if (getEnabled()) { + Map armor = new HashMap(); + for (Map.Entry entry : config.getValues(false).entrySet()) { + if (!entry.getKey().equals("enabled")) { + MaterialData md = null; + try { + md = Utils.parseMaterial((String) entry.getValue()); + } catch (MaterilNotRecognizedException e) { + getLog().warn(L("exception.config.material_not_found", entry.getValue())); + } catch (MaterialDataNotRecognizedException e) { + getLog().warn(L("exception.config.materiak_data_not_found", entry.getValue())); + } + + if (md != null) + armor.put(entry.getKey(), md); + } + } + return armor.size() > 0 ? armor : null; + } + return null; + } + + @Deprecated + public String L(String msg, Object... objects) { + return ((LimitedCreative) Bukkit.getPluginManager().getPlugin("LimitedCreative")).getLocale().trans(msg, objects); + } + @Deprecated + public ModuleLogger getLog() { + return ((LimitedCreative) Bukkit.getPluginManager().getPlugin("LimitedCreative")).getModule(ModInventories.class).getLog(); + } +} diff --git a/src/de/jaschastarke/minecraft/limitedcreative/inventories/AuthMeInventories.java b/src/de/jaschastarke/minecraft/limitedcreative/inventories/AuthMeInventories.java new file mode 100644 index 0000000..d8067de --- /dev/null +++ b/src/de/jaschastarke/minecraft/limitedcreative/inventories/AuthMeInventories.java @@ -0,0 +1,52 @@ +package de.jaschastarke.minecraft.limitedcreative.inventories; + +import org.bukkit.GameMode; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; + +import uk.org.whoami.authme.events.RestoreInventoryEvent; +import uk.org.whoami.authme.events.StoreInventoryEvent; +import de.jaschastarke.bukkit.lib.SimpleModule; +import de.jaschastarke.minecraft.limitedcreative.LimitedCreative; +import de.jaschastarke.minecraft.limitedcreative.ModInventories; + +public class AuthMeInventories extends SimpleModule implements Listener { + ModInventories invmod; + public AuthMeInventories(LimitedCreative plugin, ModInventories modInventories) { + super(plugin); + invmod = modInventories; + } + + @EventHandler + public void onStoreInventory(StoreInventoryEvent event) { + if (isDebug()) + getLog().debug("AuthMe Store Event: "+event.getPlayer().getName()); + + event.getPlayer().closeInventory(); + GameMode cgm = event.getPlayer().getGameMode(); + + if (cgm == GameMode.ADVENTURE && !invmod.getConfig().getSeparateAdventure()) + cgm = GameMode.SURVIVAL; + + if (cgm != GameMode.CREATIVE || invmod.getConfig().getStoreCreative()) { + invmod.getInventory(event.getPlayer()).save(cgm); + } + } + @EventHandler + public void onRestoreInventory(RestoreInventoryEvent event) { + if (isDebug()) + getLog().debug("AuthMe Restore Event: "+event.getPlayer().getName()); + + event.getPlayer().closeInventory(); + GameMode cgm = event.getPlayer().getGameMode(); + + if (cgm == GameMode.ADVENTURE && !invmod.getConfig().getSeparateAdventure()) + cgm = GameMode.SURVIVAL; + + Inventory inv = invmod.getInventory(event.getPlayer()); + if (inv.isStored(cgm)) { + inv.load(cgm); + event.setCancelled(true); + } + } +} diff --git a/src/de/jaschastarke/minecraft/limitedcreative/inventories/Inventory.java b/src/de/jaschastarke/minecraft/limitedcreative/inventories/Inventory.java new file mode 100644 index 0000000..7a7dc75 --- /dev/null +++ b/src/de/jaschastarke/minecraft/limitedcreative/inventories/Inventory.java @@ -0,0 +1,77 @@ +/* + * Limited Creative - (Bukkit Plugin) + * Copyright (C) 2012 jascha@ja-s.de + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package de.jaschastarke.minecraft.limitedcreative.inventories; + +import org.bukkit.GameMode; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.PlayerInventory; + +import de.jaschastarke.minecraft.limitedcreative.inventories.store.PlayerInventoryStorage; + +public class Inventory { + private PlayerInventoryStorage storage; + protected Player player; + + public enum Target { + SURVIVAL, + CREATIVE, + ADVENTURE; + + public static Target getTarget(GameMode gm) { + return Target.valueOf(gm.name()); + } + } + + public Inventory(PlayerInventoryStorage storage, Player player) { + this.storage= storage; + this.player = player; + } + public Player getPlayer() { + return player.getPlayer(); + } + private PlayerInventory inv() { + return player.getInventory(); + } + + public void save() { + save(getPlayer().getGameMode()); + } + public void save(GameMode gm) { + storage.getLog().debug(getPlayer().getName()+": store inventory: "+gm); + storage.store(this, Target.getTarget(gm)); + } + + public void load() { + load(getPlayer().getGameMode()); + } + + public boolean isStored(GameMode gm) { + return storage.contains(this, Target.getTarget(gm)); + } + + public void load(GameMode gm) { + storage.getLog().debug(getPlayer().getName()+": load inventory: "+gm); + storage.load(this, Target.getTarget(gm)); + } + + public void clear() { + inv().setArmorContents(new ItemStack[0]); + inv().clear(); + } +} diff --git a/src/de/jaschastarke/minecraft/limitedcreative/inventories/InventoryConfig.java b/src/de/jaschastarke/minecraft/limitedcreative/inventories/InventoryConfig.java new file mode 100644 index 0000000..df9f7e2 --- /dev/null +++ b/src/de/jaschastarke/minecraft/limitedcreative/inventories/InventoryConfig.java @@ -0,0 +1,71 @@ +package de.jaschastarke.minecraft.limitedcreative.inventories; + +import de.jaschastarke.bukkit.lib.configuration.Configuration; +import de.jaschastarke.configuration.annotations.IConfigurationSubGroup; +import de.jaschastarke.configuration.annotations.IsConfigurationNode; +import de.jaschastarke.maven.ArchiveDocComments; +import de.jaschastarke.minecraft.limitedcreative.ModInventories; + +@ArchiveDocComments +public class InventoryConfig extends Configuration implements IConfigurationSubGroup { + protected ModInventories mod; + public InventoryConfig(ModInventories modInventories) { + mod = modInventories; + } + @Override + public String getNodeName() { + return "store"; + } + + /** + * SeparatedInventoryEnabled + * + * Use this option to disable the separated inventories feature, for the case you only need the other features. + * + * default: true + */ + @IsConfigurationNode + public boolean getEnabled() { + return config.getBoolean("enabled", true); + } + + /** + * StoreCreative + * + * Should the creative-inventory also be stored on disk, when switching to survival? + * If disabled, the inventory gets cleared every time on switching to creative. + * + * default: true + */ + @IsConfigurationNode + public boolean getStoreCreative() { + return config.getBoolean("storeCreative", config.getBoolean("creative", true)); + } + + /** + * SeparateAdventureInventory + * + * When true, your players get a separate inventory when switching to adventure gamemode (2). Otherwise + * they have the default survival inventory while in adventure gamemode. + * + * default: false + */ + @IsConfigurationNode + public boolean getSeparateAdventure() { + return config.getBoolean("separateAdventure", config.getBoolean("adventure", true)); + } + + /** + * InventoryFolder + * + * The folder inside the datadir-folder (plugin/LimitedCreative) where the inventories are saved to. + * By default the inventories are saved to "plugin/LimitedCreative/inventories". + * + * default: "inventories" + */ + @IsConfigurationNode + public String getFolder() { + return config.getString("folder", "inventories"); + } + +} diff --git a/src/de/jaschastarke/minecraft/limitedcreative/inventories/PlayerListener.java b/src/de/jaschastarke/minecraft/limitedcreative/inventories/PlayerListener.java index 08e2fd7..62daafd 100644 --- a/src/de/jaschastarke/minecraft/limitedcreative/inventories/PlayerListener.java +++ b/src/de/jaschastarke/minecraft/limitedcreative/inventories/PlayerListener.java @@ -17,12 +17,13 @@ */ package de.jaschastarke.minecraft.limitedcreative.inventories; +import org.bukkit.GameMode; import org.bukkit.event.Event; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.player.PlayerGameModeChangeEvent; import org.bukkit.event.player.PlayerInteractEvent; -import org.bukkit.event.player.PlayerQuitEvent; +import org.bukkit.event.player.PlayerRespawnEvent; import de.jaschastarke.minecraft.limitedcreative.Hooks; import de.jaschastarke.minecraft.limitedcreative.ModInventories; @@ -60,9 +61,9 @@ public class PlayerListener implements Listener { } @EventHandler - public void onLogout(PlayerQuitEvent event) { - if (event.getPlayer() != null) { - //mod.getInventories().remove(event.getPlayer()); + public void onPlayerRespawn(PlayerRespawnEvent event) { + if (event.getPlayer().getGameMode() == GameMode.CREATIVE) { + mod.setCreativeArmor(event.getPlayer()); } } } diff --git a/src/de/jaschastarke/minecraft/limitedcreative/inventories/store/Fallback.java b/src/de/jaschastarke/minecraft/limitedcreative/inventories/store/Fallback.java new file mode 100644 index 0000000..cc08897 --- /dev/null +++ b/src/de/jaschastarke/minecraft/limitedcreative/inventories/store/Fallback.java @@ -0,0 +1,170 @@ +/* + * Limited Creative - (Bukkit Plugin) + * Copyright (C) 2012 jascha@ja-s.de + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package de.jaschastarke.minecraft.limitedcreative.inventories.store; + +import java.util.HashMap; +import java.util.Map; + +import org.bukkit.Material; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.PlayerInventory; + +public class Fallback { + + public static void loadVersion1(PlayerInventory inv, ConfigurationSection sect) { + new Armor(inv).restore(sect.getConfigurationSection("armor")); + new Items(inv).restore(sect.getConfigurationSection("inv")); + } + + public static interface Storeable { + public void store(ConfigurationSection section); + public void restore(ConfigurationSection section); + } + + public static class Items implements Storeable { + private PlayerInventory inv; + public Items(PlayerInventory pi) { + inv = pi; + } + + @Override + public void store(ConfigurationSection section) { + for (int i = 0; i < inv.getSize(); i++) { + if (inv.getItem(i) != null && inv.getItem(i).getTypeId() != 0) + sectionSetItem(section, String.valueOf(i), inv.getItem(i)); + } + } + + @Override + public void restore(ConfigurationSection section) { + inv.clear(); + for (int i = 0; i < inv.getSize(); i++) { + if (section.contains(String.valueOf(i))) + inv.setItem(i, sectionGetItem(section, String.valueOf(i))); + } + } + + public static void sectionSetItem(ConfigurationSection section, String path, ItemStack item) { + //if (!Core.plugin.config.getUnsafeStorage()) { + section.set(path, item); + /*} else { // unsafe enchants fallback + Map 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 { + ConfigurationSection s = section.getConfigurationSection(path); + Material type = Material.getMaterial(s.getString("type")); + short damage = new Integer(s.getInt("damage", 0)).shortValue(); + int amount = s.getInt("amaount", 1); + + ItemStack result = new ItemStack(type, amount, damage); + Map item = section.getConfigurationSection(path).getValues(false); + item.remove("enchantments"); + if (s.contains("enchantments")) { + for (Map.Entry entry : s.getConfigurationSection("enchantments").getValues(false).entrySet()) { + Enchantment enchantment = Enchantment.getByName(entry.getKey().toString()); + if ((enchantment != null) && (entry.getValue() instanceof Integer)) { + result.addUnsafeEnchantment(enchantment, (Integer) entry.getValue()); + } + } + } + return result; + } + } + public static Map storeInventory(PlayerInventory inv) { + Map map = new HashMap(); + for (int i = 0; i < inv.getSize(); i++) { + if (inv.getItem(i) != null && inv.getItem(i).getTypeId() != 0) { + map.put(i, inv.getItem(i)); + } + } + for (int i = 0; i < inv.getArmorContents().length; i++) { + map.put((i * -1) - 1, inv.getArmorContents()[i]); + } + return map; + } + public static void restoreInventory(PlayerInventory inv, Map map) { + for (int i = 0; i < inv.getSize(); i++) { + if (map.containsKey(i)) { + inv.setItem(i, map.get(i)); + } else { + inv.setItem(i, null); + } + } + for (int i = 0; i < inv.getArmorContents().length; i++) { + int _i = (i * -1) - 1; + if (map.containsKey(_i)) { + inv.getArmorContents()[i] = map.get(_i); + } else { + inv.getArmorContents()[i] = null; + } + } + } + } + + public static class Armor implements Storeable { + private PlayerInventory inv; + public Armor(PlayerInventory pi) { + inv = pi; + } + + @Override + public void store(ConfigurationSection section) { + if (inv.getHelmet() != null && inv.getHelmet().getTypeId() != 0) + Items.sectionSetItem(section, "helmet", inv.getHelmet()); + if (inv.getChestplate() != null && inv.getChestplate().getTypeId() != 0) + Items.sectionSetItem(section, "chestplate", inv.getChestplate()); + if (inv.getLeggings() != null && inv.getLeggings().getTypeId() != 0) + Items.sectionSetItem(section, "leggins", inv.getLeggings()); + if (inv.getBoots() != null && inv.getBoots().getTypeId() != 0) + Items.sectionSetItem(section, "boots", inv.getBoots()); + } + + @Override + public void restore(ConfigurationSection section) { + if (section.contains("helmet")) + inv.setHelmet(Items.sectionGetItem(section, "helmet")); + else + inv.setHelmet(null); + + if (section.contains("chestplate")) + inv.setChestplate(Items.sectionGetItem(section, "chestplate")); + else + inv.setChestplate(null); + + if (section.contains("leggins")) + inv.setLeggings(Items.sectionGetItem(section, "leggins")); + else + inv.setLeggings(null); + + if (section.contains("boots")) + inv.setBoots(Items.sectionGetItem(section, "boots")); + else + inv.setBoots(null); + } + + } +} diff --git a/src/de/jaschastarke/minecraft/limitedcreative/inventories/store/InvConfStorage.java b/src/de/jaschastarke/minecraft/limitedcreative/inventories/store/InvConfStorage.java new file mode 100644 index 0000000..95718b8 --- /dev/null +++ b/src/de/jaschastarke/minecraft/limitedcreative/inventories/store/InvConfStorage.java @@ -0,0 +1,129 @@ +/* + * Limited Creative - (Bukkit Plugin) + * Copyright (C) 2012 jascha@ja-s.de + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package de.jaschastarke.minecraft.limitedcreative.inventories.store; + +import java.util.Map; + +import org.bukkit.Material; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.enchantments.Enchantment; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.PlayerInventory; +import org.bukkit.inventory.meta.ItemMeta; + +import de.jaschastarke.minecraft.limitedcreative.inventories.Inventory; + +abstract public class InvConfStorage extends PlayerInventoryStorage { + private static final int ARMOR_SIZE = 4; + + public void store(Inventory pinv, ConfigurationSection sect) { + PlayerInventory inv = pinv.getPlayer().getInventory(); + sect.set("version", 4); + /*if (Core.plugin.config.getUnsafeStorage()) + sect.set("unsafe", true);*/ + storeItems(sect.createSection("armor"), inv.getArmorContents()); + storeItems(sect.createSection("inv"), inv.getContents()); + } + + public void load(Inventory pinv, ConfigurationSection sect) { + PlayerInventory inv = pinv.getPlayer().getInventory(); + + if (!sect.contains("version")) { + Fallback.loadVersion1(inv, sect); + } else { + inv.setArmorContents(restoreItems(sect.getConfigurationSection("armor"), ARMOR_SIZE)); + inv.setContents(restoreItems(sect.getConfigurationSection("inv"), inv.getSize())); + } + } + + protected void storeItems(ConfigurationSection sect, ItemStack[] items) { + for (int i = 0; i < items.length; i++) { + ItemStack is = items[i]; + if (is != null && is.getType() != Material.AIR) { + sect.set(String.valueOf(i), serialize(is)); + } + } + } + protected ItemStack[] restoreItems(ConfigurationSection sect, int size) { + ItemStack[] items = new ItemStack[size]; + if (sect != null) { + for (int i = 0; i < size; i++) { + if (sect.contains(String.valueOf(i))) { + if (sect.isItemStack(String.valueOf(i))) { + items[i] = sect.getItemStack(String.valueOf(i)); + } else { + items[i] = deserializeItemStack(sect.get(String.valueOf(i))); + } + } else { + items[i] = null; + } + } + } + return items; + } + + protected Object serialize(ItemStack is) {/* + if (Core.plugin.config.getUnsafeStorage()) { + return getRecursiveSerialized(is); + }*/ + return is; + } + + @SuppressWarnings("unchecked") + protected ItemStack deserializeItemStack(Object is) { + if (is instanceof ConfigurationSection) { + ConfigurationSection sect = (ConfigurationSection) is; + Material type = Material.getMaterial(sect.getString("type")); + short damage = new Integer(sect.getInt("damage", 0)).shortValue(); + int amount = sect.getInt("amount", 1); + + ItemStack result = new ItemStack(type, amount, damage); + if (sect.contains("enchantments")) { // conf-version 2 + for (Map.Entry entry : sect.getConfigurationSection("enchantments").getValues(false).entrySet()) { + Enchantment enchantment = Enchantment.getByName(entry.getKey().toString()); + if ((enchantment != null) && (entry.getValue() instanceof Integer)) { + result.addUnsafeEnchantment(enchantment, (Integer) entry.getValue()); + } + } + } + if (sect.contains("tag")) { // Backward compatibility for 1.4.5-R0.2; Was Conf-Version 2, but should be 3 ;) + ConfigurationSection tag = sect.getConfigurationSection("tag"); + ItemMeta meta = result.getItemMeta(); + meta.setDisplayName(tag.getString("name")); + result.setItemMeta(meta); + } + return result; + } else if (is instanceof Map) { + return ItemStack.deserialize((Map) is); + } else { + getLog().warn("Failed to restore Item: "+is.toString()); + return null; + } + } + + /*protected static Map getRecursiveSerialized(ConfigurationSerializable conf) { + Map serialized = new HashMap(conf.serialize()); // de-immutable + for (Map.Entry entry : serialized.entrySet()) { + if (entry.getValue() instanceof ConfigurationSerializable) { + entry.setValue(getRecursiveSerialized((ConfigurationSerializable) entry.getValue())); // immutable + //serialized.put(entry.getKey(), getRecursiveSerialized((ConfigurationSerializable) entry.getValue())); + } + } + return serialized; + }*/ +} diff --git a/src/de/jaschastarke/minecraft/limitedcreative/inventories/store/InvYamlStorage.java b/src/de/jaschastarke/minecraft/limitedcreative/inventories/store/InvYamlStorage.java new file mode 100644 index 0000000..326960b --- /dev/null +++ b/src/de/jaschastarke/minecraft/limitedcreative/inventories/store/InvYamlStorage.java @@ -0,0 +1,79 @@ +/* + * Limited Creative - (Bukkit Plugin) + * Copyright (C) 2012 jascha@ja-s.de + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package de.jaschastarke.minecraft.limitedcreative.inventories.store; + +import java.io.File; +import java.io.IOException; + +import org.bukkit.configuration.file.YamlConfiguration; + +import de.jaschastarke.bukkit.lib.CoreModule; +import de.jaschastarke.bukkit.lib.ModuleLogger; +import de.jaschastarke.minecraft.limitedcreative.inventories.Inventory; +import de.jaschastarke.minecraft.limitedcreative.inventories.Inventory.Target; + +public class InvYamlStorage extends InvConfStorage { + private static final String SUFFIX = ".yml"; + + private CoreModule mod; + private File dir; + public InvYamlStorage(CoreModule mod, File file) { + this.mod = mod; + dir = file; + } + + public ModuleLogger getLog() { + return mod.getLog(); + } + + @Override + public void load(Inventory pinv, Target target) { + load(pinv, YamlConfiguration.loadConfiguration(getFile(pinv, target))); + } + + @Override + public void store(Inventory pinv, Target target) { + YamlConfiguration yml = new YamlConfiguration(); + yml.options().header("DO NOT MODIFY THIS FILE"); + store(pinv, yml); + try { + yml.save(getFile(pinv, target)); + } catch (IOException e) { + mod.getLog().warn("Failed to save Inventory for Player " + pinv.getPlayer().getName()); + e.printStackTrace(); + } + } + + @Override + public void remove(Inventory pinv, Target target) { + getFile(pinv, target).delete(); + } + + @Override + public boolean contains(Inventory pinv, Target target) { + return getFile(pinv, target).exists(); + } + + protected File getFile(Inventory pinv, Target target) { + if (target != default_target) { + return new File(dir, pinv.getPlayer().getName() + "_" + target.toString().toLowerCase() + SUFFIX); + } else { + return new File(dir, pinv.getPlayer().getName() + SUFFIX); + } + } +} diff --git a/src/de/jaschastarke/minecraft/limitedcreative/inventories/store/PlayerInventoryStorage.java b/src/de/jaschastarke/minecraft/limitedcreative/inventories/store/PlayerInventoryStorage.java new file mode 100644 index 0000000..336e6fa --- /dev/null +++ b/src/de/jaschastarke/minecraft/limitedcreative/inventories/store/PlayerInventoryStorage.java @@ -0,0 +1,44 @@ +/* + * Limited Creative - (Bukkit Plugin) + * Copyright (C) 2012 jascha@ja-s.de + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package de.jaschastarke.minecraft.limitedcreative.inventories.store; + +import de.jaschastarke.minecraft.limitedcreative.inventories.Inventory; +import de.jaschastarke.minecraft.limitedcreative.inventories.Inventory.Target; +import de.jaschastarke.utils.ISimpleLogger; + +public abstract class PlayerInventoryStorage { + protected Target default_target = Target.SURVIVAL; + abstract public ISimpleLogger getLog(); + + final public void store(Inventory pinv) { + store(pinv, default_target); + } + final public void load(Inventory pinv) { + load(pinv, default_target); + } + final public void remove(Inventory pinv) { + remove(pinv, default_target); + } + final public boolean contains(Inventory pinv) { + return contains(pinv, default_target); + } + abstract public void store(Inventory pinv, Target target); + abstract public void load(Inventory pinv, Target target); + abstract public void remove(Inventory pinv, Target target); + abstract public boolean contains(Inventory pinv, Target target); +}