LimitedCreative/src/main/java/de/jaschastarke/minecraft/limitedcreative/ModInventories.java

134 lines
5.2 KiB
Java
Raw Normal View History

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 de.jaschastarke.bukkit.lib.CoreModule;
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;
2013-02-01 21:25:43 +00:00
import de.jaschastarke.modularize.ModuleEntry.ModuleState;
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 String getName() {
return "Inventory";
}
@Override
2013-02-01 21:25:43 +00:00
public void initialize(ModuleEntry<IModule> entry) {
super.initialize(entry);
listeners.addListener(new PlayerListener(this));
2013-02-01 21:25:43 +00:00
config = plugin.getPluginConfig().registerSection(new InventoryConfig(this, entry));
armor_config = config.registerSection(new ArmoryConfig(this));
2013-02-01 21:25:43 +00:00
if (!config.getEnabled()) {
entry.initialState = ModuleState.DISABLED;
return;
}
String incomp = Hooks.InventoryIncompatible.test();
if (incomp != null) {
getLog().warn(plugin.getLocale().trans("basic.conflict", incomp, this.getName()));
2013-02-01 21:25:43 +00:00
entry.initialState = ModuleState.NOT_INITIALIZED;
}
}
@Override
2013-02-01 21:25:43 +00:00
public void onEnable() {
String incomp = Hooks.InventoryIncompatible.test();
if (incomp != null) {
throw new IllegalAccessError(plugin.getLocale().trans("basic.conflict", incomp, this.getName()));
}
super.onEnable();
storage = new InvYamlStorage(this, new File(plugin.getDataFolder(), config.getFolder()));
inventories = new WeakHashMap<Player, Inventory>();
getLog().info(plugin.getLocale().trans("basic.loaded.module"));
}
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) {
2013-01-22 17:38:48 +00:00
if (plugin.getPermManager().hasPermission(player, InventoryPermissions.KEEP_INVENTORY))
return;
player.closeInventory();
GameMode cgm = player.getGameMode();
if (gm == GameMode.ADVENTURE && !config.getSeparateAdventure())
gm = GameMode.SURVIVAL;
if (cgm == GameMode.ADVENTURE && !config.getSeparateAdventure())
cgm = GameMode.SURVIVAL;
if (gm != cgm) {
if (gm != GameMode.CREATIVE || config.getStoreCreative()) {
getInventory(player).save(cgm);
}
if (gm == GameMode.CREATIVE) {
if (config.getStoreCreative() && getInventory(player).isStored(GameMode.CREATIVE)) {
getInventory(player).load(GameMode.CREATIVE);
} else {
getInventory(player).clear();
}
setCreativeArmor(player);
} else if (gm == GameMode.SURVIVAL) {
if (getInventory(player).isStored(GameMode.SURVIVAL))
getInventory(player).load(GameMode.SURVIVAL);
} else if (gm == GameMode.ADVENTURE) {
if (getInventory(player).isStored(GameMode.ADVENTURE))
getInventory(player).load(GameMode.ADVENTURE);
else
getInventory(player).clear();
}
}
}
public void setCreativeArmor(Player player) {
2013-01-22 17:38:48 +00:00
Map<String, ItemStack> armor = armor_config.getCreativeArmor();
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");
if (armor.containsKey("legs"))
2013-01-22 17:38:48 +00:00
is[1] = armor.get("legs");
if (armor.containsKey("chest"))
2013-01-22 17:38:48 +00:00
is[2] = armor.get("chest");
if (armor.containsKey("head"))
2013-01-22 17:38:48 +00:00
is[3] = armor.get("head");
player.getInventory().setArmorContents(is);
}
}
}