Separated inventories

No Drop/Pickup
This commit is contained in:
Jascha Starke 2012-01-17 12:30:55 +01:00
commit 9cc0fb267d
10 changed files with 361 additions and 0 deletions

View file

@ -0,0 +1,45 @@
package de.jaschastarke.minecraft.limitedcreative;
import org.bukkit.GameMode;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
//import org.bukkit.plugin.PluginManager;
public class Commands {
private static LimitedCreativeCore plugin;
//private static PluginManager pm;
public static class MainCommandExecutor implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
Player player = null;
if (sender instanceof Player) {
player = (Player) sender;
} else {
sender.sendMessage("Console-Commands not supported");
return true;
}
if (args.length >= 1 && args[0].equalsIgnoreCase("load")) {
if (args.length == 1 || args[1].equalsIgnoreCase("survival") || args[1] == "0") {
new Inventory(player).load(GameMode.SURVIVAL);
return true;
}/* else if (args.length == 2 && (args[1].equalsIgnoreCase("creative") || args[1] == "1")) {
new Inventory(player).load(GameMode.CREATIVE);
return true;
}*/
}
return false;
}
}
public static void register(LimitedCreativeCore pplugin) {
plugin = pplugin;
//pm = plugin.getServer().getPluginManager();
plugin.getCommand("limitedcreative").setExecutor(new MainCommandExecutor());
}
}

View file

@ -0,0 +1,15 @@
package de.jaschastarke.minecraft.limitedcreative;
import org.bukkit.configuration.file.FileConfiguration;
public class Configuration {
public Configuration(FileConfiguration cfg) {
}
public boolean getStoreCreative() {
return true;
}
public boolean getDropInCreative() {
return false;
}
}

View file

@ -0,0 +1,73 @@
package de.jaschastarke.minecraft.limitedcreative;
import java.io.File;
import java.io.IOException;
import org.bukkit.GameMode;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import de.jaschastarke.minecraft.limitedcreative.serialize.Armor;
import de.jaschastarke.minecraft.limitedcreative.serialize.Items;
public class Inventory {
protected Player player;
protected PlayerInventory inv;
public Inventory(Player p) {
player = p;
inv = p.getInventory();
}
public void save() {
File f = new File(LimitedCreativeCore.plugin.getDataFolder(), getFileName(player, player.getGameMode()));
storeInventory(inv, f);
}
public void load(GameMode gm) {
File f = new File(LimitedCreativeCore.plugin.getDataFolder(), getFileName(player, gm));
restoreInventory(inv, f);
}
public void load() {
load(player.getGameMode());
}
public void clear() {
inv.setArmorContents(new ItemStack[]{
new ItemStack(0),
new ItemStack(0),
new ItemStack(0),
new ItemStack(0),
});
inv.clear();
}
private String getFileName(Player player, GameMode gm) {
if (gm != GameMode.SURVIVAL) {
return player.getName()+"_"+gm.toString()+".yml";
} else {
return player.getName()+".yml";
}
}
protected static void storeInventory(PlayerInventory pinv, File file) {
YamlConfiguration yml = new YamlConfiguration();
new Armor(pinv).store(yml.createSection("armor"));
new Items(pinv).store(yml.createSection("inv"));
try {
yml.save(file);
} catch (IOException e) {
LimitedCreativeCore.plugin.logger.severe(e.getMessage());
}
}
protected static void restoreInventory(PlayerInventory pinv, File file) {
YamlConfiguration yml = YamlConfiguration.loadConfiguration(file);
new Armor(pinv).restore(yml.getConfigurationSection("armor"));
new Items(pinv).restore(yml.getConfigurationSection("inv"));
}
}

View file

@ -0,0 +1,35 @@
package de.jaschastarke.minecraft.limitedcreative;
import java.util.logging.Logger;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.java.JavaPlugin;
public class LimitedCreativeCore extends JavaPlugin {
public final Logger logger = Logger.getLogger("Minecraft");
public Configuration config;
public WorldGuardIntegration worldguard;
public static LimitedCreativeCore plugin;
@Override
public void onDisable() {
logger.info("["+this.getDescription().getName()+"] cleanly unloaded.");
}
@Override
public void onEnable() {
plugin = this;
config = new Configuration(this.getConfig());
Listener.register(this);
Commands.register(this);
try {
Class.forName("com.sk89q.worldguard.bukkit.WorldGuardPlugin", false, null);
worldguard = new WorldGuardIntegration(this);
worldguard.init();
} catch (ClassNotFoundException e) {}
PluginDescriptionFile df = this.getDescription();
logger.info("["+df.getName() + " v" + df.getVersion() + "] done loading.");
}
}

View file

@ -0,0 +1,53 @@
package de.jaschastarke.minecraft.limitedcreative;
import org.bukkit.GameMode;
import org.bukkit.event.Event;
import org.bukkit.event.Event.Priority;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerGameModeChangeEvent;
import org.bukkit.event.player.PlayerListener;
import org.bukkit.event.player.PlayerPickupItemEvent;
import org.bukkit.plugin.PluginManager;
public final class Listener {
private static LimitedCreativeCore plugin;
private static PluginManager pm;
public static class PlayerListen extends PlayerListener {
@Override
public void onPlayerGameModeChange(PlayerGameModeChangeEvent event) {
if (event.getNewGameMode() == GameMode.CREATIVE) {
new PlayerCore(plugin, event.getPlayer()).onSetCreative();
} else if (event.getNewGameMode() == GameMode.SURVIVAL) {
new PlayerCore(plugin, event.getPlayer()).onSetSurvival();
}
}
@Override
public void onPlayerDropItem(PlayerDropItemEvent event) {
if (event.getPlayer().getGameMode() == GameMode.CREATIVE && !plugin.config.getDropInCreative()) {
event.setCancelled(true);
}
}
@Override
public void onPlayerPickupItem(PlayerPickupItemEvent event) {
if (event.getPlayer().getGameMode() == GameMode.CREATIVE && !plugin.config.getDropInCreative()) {
event.setCancelled(true);
}
}
private void register() {
pm.registerEvent(Event.Type.PLAYER_GAME_MODE_CHANGE, this, Priority.Low, plugin);
pm.registerEvent(Event.Type.PLAYER_DROP_ITEM, this, Priority.High, plugin);
pm.registerEvent(Event.Type.PLAYER_PICKUP_ITEM, this, Priority.High, plugin);
}
}
public static void register(LimitedCreativeCore pplugin) {
plugin = pplugin;
pm = plugin.getServer().getPluginManager();
new PlayerListen().register();
}
}

View file

@ -0,0 +1,31 @@
package de.jaschastarke.minecraft.limitedcreative;
import org.bukkit.GameMode;
import org.bukkit.entity.Player;
public class PlayerCore {
private static LimitedCreativeCore plugin;
private Player player;
public PlayerCore(LimitedCreativeCore pplugin, Player pplayer) {
plugin = pplugin;
player = pplayer;
}
public void onSetCreative() {
Inventory inv = new Inventory(player);
inv.save();
if (plugin.config.getStoreCreative()) {
inv.load(GameMode.CREATIVE);
} else {
inv.clear();
}
}
public void onSetSurvival() {
Inventory inv = new Inventory(player);
if (plugin.config.getStoreCreative()) {
inv.save();
}
inv.load(GameMode.SURVIVAL);
}
}

View file

@ -0,0 +1,22 @@
package de.jaschastarke.minecraft.limitedcreative;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.protection.flags.DefaultFlag;
import com.sk89q.worldguard.protection.flags.StateFlag;
public class WorldGuardIntegration {
public static LimitedCreativeCore plugin;
public static WorldGuardPlugin wg;
public WorldGuardIntegration(LimitedCreativeCore pplugin) {
plugin = pplugin;
wg = (WorldGuardPlugin) plugin.getServer().getPluginManager().getPlugin("WorldGuard");
}
public static final StateFlag CREATIVE_MEMBER = new StateFlag("creative-member", false);
public void init() {
}
}

View file

@ -0,0 +1,47 @@
package de.jaschastarke.minecraft.limitedcreative.serialize;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.PlayerInventory;
public 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)
section.set("helmet", inv.getHelmet());
if (inv.getChestplate() != null && inv.getChestplate().getTypeId() != 0)
section.set("chestplate", inv.getChestplate());
if (inv.getLeggings() != null && inv.getLeggings().getTypeId() != 0)
section.set("leggins", inv.getLeggings());
if (inv.getBoots() != null && inv.getBoots().getTypeId() != 0)
section.set("boots", inv.getBoots());
}
@Override
public void restore(ConfigurationSection section) {
if (section.contains("helmet"))
inv.setHelmet(section.getItemStack("helmet"));
else
inv.setHelmet(null);
if (section.contains("chestplate"))
inv.setChestplate(section.getItemStack("chestplate"));
else
inv.setChestplate(null);
if (section.contains("leggins"))
inv.setLeggings(section.getItemStack("leggins"));
else
inv.setLeggings(null);
if (section.contains("boots"))
inv.setBoots(section.getItemStack("boots"));
else
inv.setBoots(null);
}
}

View file

@ -0,0 +1,32 @@
package de.jaschastarke.minecraft.limitedcreative.serialize;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.PlayerInventory;
import de.jaschastarke.minecraft.limitedcreative.LimitedCreativeCore;
public 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)
section.set(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, section.getItemStack(String.valueOf(i)));
}
}
}
}

View file

@ -0,0 +1,8 @@
package de.jaschastarke.minecraft.limitedcreative.serialize;
import org.bukkit.configuration.ConfigurationSection;
public interface Storeable {
public void store(ConfigurationSection section);
public void restore(ConfigurationSection section);
}