2013-01-16 11:33:14 +00:00
|
|
|
package de.jaschastarke.minecraft.limitedcreative;
|
|
|
|
|
2013-01-18 21:51:08 +00:00
|
|
|
import java.io.File;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.WeakHashMap;
|
|
|
|
|
2013-01-16 11:33:14 +00:00
|
|
|
import org.bukkit.GameMode;
|
|
|
|
import org.bukkit.entity.Player;
|
2013-01-18 21:51:08 +00:00
|
|
|
import org.bukkit.inventory.ItemStack;
|
2013-01-16 11:33:14 +00:00
|
|
|
|
|
|
|
import de.jaschastarke.bukkit.lib.CoreModule;
|
2013-01-18 21:51:08 +00:00
|
|
|
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;
|
2013-01-16 11:33:14 +00:00
|
|
|
import de.jaschastarke.minecraft.limitedcreative.inventories.PlayerListener;
|
2013-01-18 21:51:08 +00:00
|
|
|
import de.jaschastarke.minecraft.limitedcreative.inventories.store.InvYamlStorage;
|
|
|
|
import de.jaschastarke.minecraft.limitedcreative.inventories.store.PlayerInventoryStorage;
|
2013-01-16 11:33:14 +00:00
|
|
|
import de.jaschastarke.modularize.IModule;
|
|
|
|
import de.jaschastarke.modularize.ModuleEntry;
|
|
|
|
|
2013-01-18 21:51:08 +00:00
|
|
|
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) {
|
2013-01-16 11:33:14 +00:00
|
|
|
super(plugin);
|
|
|
|
}
|
2013-01-20 12:15:34 +00:00
|
|
|
@Override
|
|
|
|
public String getName() {
|
|
|
|
return "Inventory";
|
|
|
|
}
|
2013-01-16 11:33:14 +00:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public void Initialize(ModuleEntry<IModule> entry) {
|
|
|
|
super.Initialize(entry);
|
2013-01-18 21:51:08 +00:00
|
|
|
listeners.addListener(new PlayerListener(this));
|
|
|
|
config = plugin.getPluginConfig().registerSection(new InventoryConfig(this));
|
|
|
|
armor_config = config.registerSection(new ArmoryConfig(this));
|
2013-01-20 12:15:34 +00:00
|
|
|
|
|
|
|
String incomp = Hooks.InventoryIncompatible.test();
|
|
|
|
if (incomp != null) {
|
|
|
|
getLog().warn(plugin.getLocale().trans("basic.conflict", incomp, this.getName()));
|
|
|
|
}
|
2013-01-18 21:51:08 +00:00
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public void OnEnable() {
|
|
|
|
super.OnEnable();
|
|
|
|
storage = new InvYamlStorage(this, new File(plugin.getDataFolder(), config.getFolder()));
|
|
|
|
inventories = new WeakHashMap<Player, Inventory>();
|
2013-01-20 12:15:34 +00:00
|
|
|
getLog().info(plugin.getLocale().trans("basic.loaded.module"));
|
2013-01-18 21:51:08 +00:00
|
|
|
}
|
|
|
|
public InventoryConfig getConfig() {
|
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
|
|
|
public PlayerInventoryStorage getStorage() {
|
|
|
|
return storage;
|
2013-01-16 11:33:14 +00:00
|
|
|
}
|
|
|
|
|
2013-01-18 21:51:08 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-16 11:33:14 +00:00
|
|
|
public void onSetGameMode(Player player, GameMode gm) {
|
2013-01-22 17:38:48 +00:00
|
|
|
if (plugin.getPermManager().hasPermission(player, InventoryPermissions.KEEP_INVENTORY))
|
2013-01-18 21:51:08 +00:00
|
|
|
return;
|
2013-01-16 11:33:14 +00:00
|
|
|
player.closeInventory();
|
|
|
|
|
|
|
|
GameMode cgm = player.getGameMode();
|
2013-01-18 21:51:08 +00:00
|
|
|
if (gm == GameMode.ADVENTURE && !config.getSeparateAdventure())
|
2013-01-16 11:33:14 +00:00
|
|
|
gm = GameMode.SURVIVAL;
|
2013-01-18 21:51:08 +00:00
|
|
|
if (cgm == GameMode.ADVENTURE && !config.getSeparateAdventure())
|
2013-01-16 11:33:14 +00:00
|
|
|
cgm = GameMode.SURVIVAL;
|
|
|
|
|
|
|
|
if (gm != cgm) {
|
2013-01-18 21:51:08 +00:00
|
|
|
if (gm != GameMode.CREATIVE || config.getStoreCreative()) {
|
|
|
|
getInventory(player).save(cgm);
|
2013-01-16 11:33:14 +00:00
|
|
|
}
|
|
|
|
if (gm == GameMode.CREATIVE) {
|
2013-01-18 21:51:08 +00:00
|
|
|
if (config.getStoreCreative() && getInventory(player).isStored(GameMode.CREATIVE)) {
|
|
|
|
getInventory(player).load(GameMode.CREATIVE);
|
2013-01-16 11:33:14 +00:00
|
|
|
} else {
|
2013-01-18 21:51:08 +00:00
|
|
|
getInventory(player).clear();
|
2013-01-16 11:33:14 +00:00
|
|
|
}
|
2013-01-18 21:51:08 +00:00
|
|
|
setCreativeArmor(player);
|
2013-01-16 11:33:14 +00:00
|
|
|
} else if (gm == GameMode.SURVIVAL) {
|
2013-01-18 21:51:08 +00:00
|
|
|
if (getInventory(player).isStored(GameMode.SURVIVAL))
|
|
|
|
getInventory(player).load(GameMode.SURVIVAL);
|
2013-01-16 11:33:14 +00:00
|
|
|
} else if (gm == GameMode.ADVENTURE) {
|
2013-01-18 21:51:08 +00:00
|
|
|
if (getInventory(player).isStored(GameMode.ADVENTURE))
|
|
|
|
getInventory(player).load(GameMode.ADVENTURE);
|
2013-01-16 11:33:14 +00:00
|
|
|
else
|
2013-01-18 21:51:08 +00:00
|
|
|
getInventory(player).clear();
|
2013-01-16 11:33:14 +00:00
|
|
|
}
|
2013-01-18 21:51:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setCreativeArmor(Player player) {
|
2013-01-22 17:38:48 +00:00
|
|
|
Map<String, ItemStack> armor = armor_config.getCreativeArmor();
|
2013-01-18 21:51:08 +00:00
|
|
|
if (armor != null) {
|
|
|
|
ItemStack[] is = new ItemStack[4];
|
|
|
|
if (armor.containsKey("feet"))
|
2013-01-22 17:38:48 +00:00
|
|
|
is[0] = armor.get("feet");
|
2013-01-18 21:51:08 +00:00
|
|
|
if (armor.containsKey("legs"))
|
2013-01-22 17:38:48 +00:00
|
|
|
is[1] = armor.get("legs");
|
2013-01-18 21:51:08 +00:00
|
|
|
if (armor.containsKey("chest"))
|
2013-01-22 17:38:48 +00:00
|
|
|
is[2] = armor.get("chest");
|
2013-01-18 21:51:08 +00:00
|
|
|
if (armor.containsKey("head"))
|
2013-01-22 17:38:48 +00:00
|
|
|
is[3] = armor.get("head");
|
2013-01-18 21:51:08 +00:00
|
|
|
player.getInventory().setArmorContents(is);
|
|
|
|
}
|
2013-01-16 11:33:14 +00:00
|
|
|
}
|
|
|
|
}
|