From 9cc0fb267d565be93cd5c6adc2a3fe956d2ee9c2 Mon Sep 17 00:00:00 2001 From: Jascha Starke Date: Tue, 17 Jan 2012 12:30:55 +0100 Subject: [PATCH] Separated inventories No Drop/Pickup --- .../minecraft/limitedcreative/Commands.java | 45 ++++++++++++ .../limitedcreative/Configuration.java | 15 ++++ .../minecraft/limitedcreative/Inventory.java | 73 +++++++++++++++++++ .../limitedcreative/LimitedCreativeCore.java | 35 +++++++++ .../minecraft/limitedcreative/Listener.java | 53 ++++++++++++++ .../minecraft/limitedcreative/PlayerCore.java | 31 ++++++++ .../WorldGuardIntegration.java | 22 ++++++ .../limitedcreative/serialize/Armor.java | 47 ++++++++++++ .../limitedcreative/serialize/Items.java | 32 ++++++++ .../limitedcreative/serialize/Storeable.java | 8 ++ 10 files changed, 361 insertions(+) create mode 100644 LimitedCreative/src/de/jaschastarke/minecraft/limitedcreative/Commands.java create mode 100644 LimitedCreative/src/de/jaschastarke/minecraft/limitedcreative/Configuration.java create mode 100644 LimitedCreative/src/de/jaschastarke/minecraft/limitedcreative/Inventory.java create mode 100644 LimitedCreative/src/de/jaschastarke/minecraft/limitedcreative/LimitedCreativeCore.java create mode 100644 LimitedCreative/src/de/jaschastarke/minecraft/limitedcreative/Listener.java create mode 100644 LimitedCreative/src/de/jaschastarke/minecraft/limitedcreative/PlayerCore.java create mode 100644 LimitedCreative/src/de/jaschastarke/minecraft/limitedcreative/WorldGuardIntegration.java create mode 100644 LimitedCreative/src/de/jaschastarke/minecraft/limitedcreative/serialize/Armor.java create mode 100644 LimitedCreative/src/de/jaschastarke/minecraft/limitedcreative/serialize/Items.java create mode 100644 LimitedCreative/src/de/jaschastarke/minecraft/limitedcreative/serialize/Storeable.java diff --git a/LimitedCreative/src/de/jaschastarke/minecraft/limitedcreative/Commands.java b/LimitedCreative/src/de/jaschastarke/minecraft/limitedcreative/Commands.java new file mode 100644 index 0000000..2d6b8d9 --- /dev/null +++ b/LimitedCreative/src/de/jaschastarke/minecraft/limitedcreative/Commands.java @@ -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()); + } +} diff --git a/LimitedCreative/src/de/jaschastarke/minecraft/limitedcreative/Configuration.java b/LimitedCreative/src/de/jaschastarke/minecraft/limitedcreative/Configuration.java new file mode 100644 index 0000000..64d80c2 --- /dev/null +++ b/LimitedCreative/src/de/jaschastarke/minecraft/limitedcreative/Configuration.java @@ -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; + } +} diff --git a/LimitedCreative/src/de/jaschastarke/minecraft/limitedcreative/Inventory.java b/LimitedCreative/src/de/jaschastarke/minecraft/limitedcreative/Inventory.java new file mode 100644 index 0000000..b7bae58 --- /dev/null +++ b/LimitedCreative/src/de/jaschastarke/minecraft/limitedcreative/Inventory.java @@ -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")); + } +} diff --git a/LimitedCreative/src/de/jaschastarke/minecraft/limitedcreative/LimitedCreativeCore.java b/LimitedCreative/src/de/jaschastarke/minecraft/limitedcreative/LimitedCreativeCore.java new file mode 100644 index 0000000..58d05f6 --- /dev/null +++ b/LimitedCreative/src/de/jaschastarke/minecraft/limitedcreative/LimitedCreativeCore.java @@ -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."); + } +} diff --git a/LimitedCreative/src/de/jaschastarke/minecraft/limitedcreative/Listener.java b/LimitedCreative/src/de/jaschastarke/minecraft/limitedcreative/Listener.java new file mode 100644 index 0000000..7c5400e --- /dev/null +++ b/LimitedCreative/src/de/jaschastarke/minecraft/limitedcreative/Listener.java @@ -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(); + } +} diff --git a/LimitedCreative/src/de/jaschastarke/minecraft/limitedcreative/PlayerCore.java b/LimitedCreative/src/de/jaschastarke/minecraft/limitedcreative/PlayerCore.java new file mode 100644 index 0000000..51daf98 --- /dev/null +++ b/LimitedCreative/src/de/jaschastarke/minecraft/limitedcreative/PlayerCore.java @@ -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); + } +} diff --git a/LimitedCreative/src/de/jaschastarke/minecraft/limitedcreative/WorldGuardIntegration.java b/LimitedCreative/src/de/jaschastarke/minecraft/limitedcreative/WorldGuardIntegration.java new file mode 100644 index 0000000..8889ddc --- /dev/null +++ b/LimitedCreative/src/de/jaschastarke/minecraft/limitedcreative/WorldGuardIntegration.java @@ -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() { + + } +} diff --git a/LimitedCreative/src/de/jaschastarke/minecraft/limitedcreative/serialize/Armor.java b/LimitedCreative/src/de/jaschastarke/minecraft/limitedcreative/serialize/Armor.java new file mode 100644 index 0000000..971ea02 --- /dev/null +++ b/LimitedCreative/src/de/jaschastarke/minecraft/limitedcreative/serialize/Armor.java @@ -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); + } + +} diff --git a/LimitedCreative/src/de/jaschastarke/minecraft/limitedcreative/serialize/Items.java b/LimitedCreative/src/de/jaschastarke/minecraft/limitedcreative/serialize/Items.java new file mode 100644 index 0000000..761bb35 --- /dev/null +++ b/LimitedCreative/src/de/jaschastarke/minecraft/limitedcreative/serialize/Items.java @@ -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))); + } + } + } + +} diff --git a/LimitedCreative/src/de/jaschastarke/minecraft/limitedcreative/serialize/Storeable.java b/LimitedCreative/src/de/jaschastarke/minecraft/limitedcreative/serialize/Storeable.java new file mode 100644 index 0000000..2894f5a --- /dev/null +++ b/LimitedCreative/src/de/jaschastarke/minecraft/limitedcreative/serialize/Storeable.java @@ -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); +}