Working Inventory-Feature with some basic configuration

This commit is contained in:
Jascha Starke 2013-01-18 22:51:08 +01:00
parent f80a88174c
commit 672befc658
18 changed files with 882 additions and 49 deletions

3
.gitignore vendored
View file

@ -8,3 +8,6 @@
# maven
/target
# Mac stuff
.DS_Store

49
pom.xml
View file

@ -3,7 +3,7 @@
<groupId>de.jaschastarke</groupId>
<artifactId>LimitedCreative</artifactId>
<name>LimitedCreative</name>
<version>1.4.1a-SNAPSHOT</version>
<version>2.0-SNAPSHOT</version>
<url>https://github.com/possi/LimitedCreative</url>
<properties>
@ -17,11 +17,14 @@
</scm>
<repositories>
<!-- Required dependencies for optional integrations, that aren't hosted yet -->
<!-- Possible public Maven Repository, containing LimitedCreative builds and other dependencies without own rep.
like AuthMe, etc.
Supports http & https -->
<repository>
<id>opt-dep</id>
<url>http://dl.dropbox.com/u/5023975/mvn-repo</url>
<id>de-jas-mvn</id>
<url>https://repository-possi.forge.cloudbees.com/release</url>
</repository>
<!-- Official (Craft-)Bukkit repository -->
<repository>
<id>bukkit-repo</id>
@ -37,27 +40,44 @@
<id>onarandombox</id>
<url>http://repo.onarandombox.com/content/groups/public</url>
</repository>
<!-- Official Vault repository -->
<repository>
<id>vault-repo</id>
<url>http://ci.herocraftonline.com/plugin/repository/everything</url>
<id>vault-repo</id>
<url>http://ci.herocraftonline.com/plugin/repository/everything</url>
</repository>
<!-- Official xAuth repository; it is no good! we keep our own dep-files of it - ->
<repository>
<id>luricos.de-repo</id>
<url>http://repo.luricos.de/bukkit-plugins/</url>
</repository><!- - -->
</repositories>
<dependencies>
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>bukkit</artifactId>
<version>1.4.2-R0.2</version>
<version>1.4.6-R0.1</version>
</dependency>
<dependency>
<groupId>org.bukkit</groupId>
<artifactId>craftbukkit</artifactId>
<version>1.4.6-R0.1</version>
</dependency>
<dependency>
<groupId>com.sk89q</groupId>
<artifactId>worldedit</artifactId>
<version>5.4.5</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.sk89q</groupId>
<artifactId>worldguard</artifactId>
<version>5.6.3</version>
<version>5.6.6-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>uk.org.whoami</groupId>
<artifactId>authme</artifactId>
<version>2.6.7b5</version>
<version>2.7.1</version>
</dependency>
<dependency>
<groupId>com.onarandombox.multiversecore</groupId>
@ -67,7 +87,7 @@
<dependency>
<groupId>com.cypherx</groupId>
<artifactId>xauth</artifactId>
<version>2.0.20</version>
<version>2.0.26</version>
</dependency>
<dependency>
<groupId>de.jaschastarke</groupId>
@ -89,12 +109,21 @@
<includes>
<include>plugin.yml</include>
<include>config.yml</include>
<include>META-INF/descriptions.jos</include>
</includes>
</resource>
<resource>
<targetPath>lang/</targetPath>
<directory>${basedir}/lang/</directory>
</resource>
<resource>
<targetPath>META-INF/</targetPath>
<filtering>true</filtering>
<directory>${project.build.directory}/generated-sources/annotations/META-INF/</directory>
<includes>
<include>descriptions.jos</include>
</includes>
</resource>
</resources>
<plugins>

View file

@ -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();
}
}

View file

@ -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<LimitedCreative> implements Listener {
public FeatureBlockItemSpawn(LimitedCreative plugin) {
super(plugin);
}
public final static long TIME_OFFSET = 250;

View file

@ -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

View file

@ -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<LimitedCreative> {
public ModCmdBlocker(Core plugin) {
public ModCmdBlocker(LimitedCreative plugin) {
super(plugin);
}

View file

@ -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<LimitedCreative> {
public ModCreativeLimits(LimitedCreative plugin) {
super(plugin);
}

View file

@ -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<LimitedCreative> {
protected PlayerInventoryStorage storage;
protected Map<Player, Inventory> inventories;
protected InventoryConfig config;
protected ArmoryConfig armor_config;
public ModInventories(LimitedCreative plugin) {
super(plugin);
}
@Override
public void Initialize(ModuleEntry<IModule> 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<Player, Inventory>();
}
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<String, MaterialData> 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);
}
}
}

View file

@ -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<LimitedCreative> {
public ModRegions(LimitedCreative plugin) {
super(plugin);
}

View file

@ -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<String, MaterialData> getCreativeArmor() {
if (getEnabled()) {
Map<String, MaterialData> armor = new HashMap<String, MaterialData>();
for (Map.Entry<String, Object> 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();
}
}

View file

@ -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<LimitedCreative> 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);
}
}
}

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
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();
}
}

View file

@ -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");
}
}

View file

@ -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());
}
}
}

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
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<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 {
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<String, Object> item = section.getConfigurationSection(path).getValues(false);
item.remove("enchantments");
if (s.contains("enchantments")) {
for (Map.Entry<String, Object> 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<Integer, ItemStack> storeInventory(PlayerInventory inv) {
Map<Integer, ItemStack> map = new HashMap<Integer, ItemStack>();
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<Integer, ItemStack> 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);
}
}
}

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
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<String, Object> 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<String, Object>) is);
} else {
getLog().warn("Failed to restore Item: "+is.toString());
return null;
}
}
/*protected static Map<String, Object> getRecursiveSerialized(ConfigurationSerializable conf) {
Map<String, Object> serialized = new HashMap<String, Object>(conf.serialize()); // de-immutable
for (Map.Entry<String, Object> 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;
}*/
}

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
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);
}
}
}

View file

@ -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 <http://www.gnu.org/licenses/>.
*/
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);
}