diff --git a/src/cn/citycraft/RealBackpacks/RealBackpacks.java b/src/cn/citycraft/RealBackpacks/RealBackpacks.java index b755b4c..5b7e7ba 100644 --- a/src/cn/citycraft/RealBackpacks/RealBackpacks.java +++ b/src/cn/citycraft/RealBackpacks/RealBackpacks.java @@ -1,381 +1,354 @@ -package cn.citycraft.RealBackpacks; - -import java.lang.reflect.Constructor; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; - -import net.milkbowl.vault.economy.Economy; - -import org.bukkit.ChatColor; -import org.bukkit.Material; -import org.bukkit.configuration.file.FileConfiguration; -import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.ShapedRecipe; -import org.bukkit.inventory.meta.ItemMeta; -import org.bukkit.material.MaterialData; -import org.bukkit.plugin.RegisteredServiceProvider; -import org.bukkit.plugin.java.JavaPlugin; - -import cn.citycraft.RealBackpacks.config.Config; -import cn.citycraft.RealBackpacks.listeners.CraftListener; -import cn.citycraft.RealBackpacks.listeners.EntityListener; -import cn.citycraft.RealBackpacks.listeners.InventoryListener; -import cn.citycraft.RealBackpacks.listeners.PlayerListener; -import cn.citycraft.RealBackpacks.util.MysqlFunctions; -import cn.citycraft.RealBackpacks.util.RBUtil; -import cn.citycraft.RealBackpacks.util.VersionChecker; - -public class RealBackpacks extends JavaPlugin { - - public static RBInterface NMS; - - public static Economy econ = null; - public static boolean globalGlow = true; - - private static boolean average = false; - private static boolean add = false; - public static VersionChecker versionChecker; - private boolean usingMysql = false; - private boolean vault = true; - private boolean usingPermissions = true; - private String user = null; - private String password = null; - - private String url; - public List backpacks = new ArrayList(); - public HashMap> backpackData = new HashMap>(); - public HashMap> backpackLore = new HashMap>(); - public HashMap> backpackRecipe = new HashMap>(); - public HashMap backpackItems = new HashMap(); - public HashMap backpackOverrides = new HashMap(); - public HashMap> backpackBlacklist = new HashMap>(); - - public HashMap> backpackWhitelist = new HashMap>(); - public HashMap playerData = new HashMap(); - public HashMap adminFullView = new HashMap(); - public List adminRestrictedView = new ArrayList(); - - public List slowedPlayers = new ArrayList(); - - // List key - // 0 = Size - // 1 = UseRecipe - // 2 = id - // 3 = name - // 4 =destroyContents - // 5 = dropContents - // 6 = dropBackpack - // 7 = keepBackpack - // 8 = walkSpeedEnabled - // 9 = walkSpeedMultiplier - // 10 = increasedHungerEnabled - // 11 = hungerBarsToDeplete - // 12 = hungerBarsToSubtractWhenEating - // 13 = Purchasable - // 14 = Price - // 15 = OpenWith - // 16 = UseWhitelist - // 17 = addGlow - // 18 = Unstackable - - @Override - public FileConfiguration getConfig() { - if (Config.getInstance() == null) { - Config.load(this); - } - return Config.getInstance(); - } - - private ItemStack getConfigLore(final ItemStack item, final String backpack) { - final List key = backpackData.get(backpack); - final ItemMeta meta = item.getItemMeta(); - final ArrayList lore = new ArrayList(); - lore.clear(); - if (backpackLore.get(backpack) != null) { - for (final String s : backpackLore.get(backpack)) { - lore.add(ChatColor.translateAlternateColorCodes('&', s)); - } - meta.setLore(lore); - } - meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', key.get(3))); - item.setItemMeta(meta); - return item; - } - - public String getPass() { - return password; - } - - public String getUrl() { - return url; - } - - public String getUser() { - return user; - } - - public boolean isAdding() { - return add; - } - - public boolean isAveraging() { - return average; - } - - public boolean isUsingMysql() { - return usingMysql; - } - - public boolean isUsingPerms() { - return usingPermissions; - } - - public boolean isUsingVault() { - return vault; - } - - @Override - public void onEnable() { - final String p = getServer().getClass().getPackage().getName(); - final String version = p.substring(p.lastIndexOf('.') + 1); - String classname = getClass().getPackage().getName() + ".versions." + version; - try { - final Class clazz = Class.forName(classname); - final Constructor cons = clazz.getDeclaredConstructor(getClass()); - final Object obj = cons.newInstance(this); - if (obj instanceof RBInterface) { - NMS = (RBInterface) obj; - } - } catch (final Exception e) { - globalGlow = false; - } - - if (isEnabled()) { - MysqlFunctions.setMysqlFunc(this); - RBUtil.setRBUtil(this); - if (!setupEconomy()) { - getLogger().warning("Vault 未找到, 部分功能无法使用..."); - vault = false; - } else { - getLogger().info("Vault 已找到, 启用经济系统..."); - } - setupLists(); - setup(); - if (isEnabled()) { - getServer().getPluginManager().registerEvents(new PlayerListener(this), this); - getServer().getPluginManager().registerEvents(new CraftListener(this), this); - getServer().getPluginManager().registerEvents(new InventoryListener(this), this); - getServer().getPluginManager().registerEvents(new EntityListener(this), this); - getServer().getScheduler().runTaskTimer(this, new WalkSpeedRunnable(this), 20, 20); - - getCommand("rb").setExecutor(new MainCommand(this)); - - getLogger().info("真实背包已加载 By: 喵♂呜."); - } - } - versionChecker = new VersionChecker(this); - versionChecker.VersionCheck(null); - } - - @Override - public void reloadConfig() { - Config.load(this); - } - - @Override - public void saveConfig() { - if (Config.getInstance() == null) { - Config.load(this); - } - Config.save(); - } - - @SuppressWarnings("deprecation") - public void setup() { - user = getConfig().getString("Data.MySQL.username"); - password = getConfig().getString("Data.MySQL.password"); - url = "jdbc:mysql://" + getConfig().getString("Data.MySQL.ip") + ":" - + getConfig().getInt("Data.MySQL.port") + "/" - + getConfig().getString("Data.MySQL.database"); - - if (!getConfig().isSet("Config.MultipleBackpacksInInventory.average")) { - average = false; - } else { - average = getConfig().getBoolean("Config.MultipleBackpacksInInventory.average"); - } - - if (!getConfig().isSet("Config.MultipleBackpacksInInventory.add")) { - add = false; - } else { - add = getConfig().getBoolean("Config.MultipleBackpacksInInventory.add"); - } - - if (!getConfig().isSet("Data.FileSystem")) { - usingMysql = false; - } else if (getConfig().getString("Data.FileSystem").equalsIgnoreCase("mysql") - || getConfig().getString("Data.FileSystem").equalsIgnoreCase("sql")) { - usingMysql = true; - if (!MysqlFunctions.checkIfTableExists("rb_data")) { - MysqlFunctions.createTables(); - } - } else { - usingMysql = false; - } - - if (!getConfig().isSet("Config.usePermissions")) { - usingPermissions = true; - } else { - usingPermissions = getConfig().getBoolean("Config.usePermissions"); - } - - for (final String backpack : backpacks) { - - final String override = getConfig().getString("Backpacks." + backpack + ".Override"); - if (override != null) { - backpackOverrides.put(backpack, RBUtil.getItemstackFromString(override)); - } else { - backpackOverrides.put(backpack, null); - } - - final List key = backpackData.get(backpack); - final String backpackItem = key.get(2); - backpackItems.put(backpack, - getConfigLore(RBUtil.getItemstackFromString(backpackItem), backpack)); - - ShapedRecipe recipe = null; - if (key.get(1).equalsIgnoreCase("true")) { - recipe = new ShapedRecipe(backpackItems.get(backpack)); - recipe.shape("abc", "def", "ghi"); - int i = 0; - for (final String s : backpackRecipe.get(backpack)) { - final String[] itemIds = s.split(","); - char shapechar = 0; - for (final String itemid : itemIds) { - i++; - switch (i) { - case 1: - shapechar = 'a'; - break; - case 2: - shapechar = 'b'; - break; - case 3: - shapechar = 'c'; - break; - case 4: - shapechar = 'd'; - break; - case 5: - shapechar = 'e'; - break; - case 6: - shapechar = 'f'; - break; - case 7: - shapechar = 'g'; - break; - case 8: - shapechar = 'h'; - break; - case 9: - shapechar = 'i'; - break; - } - final String[] itemsplit = itemid.split(":"); - if (itemsplit[0].equals("0")) { - continue; - } - if (itemsplit.length > 1) { - final Material baseblock = Material.getMaterial(Integer - .parseInt(itemsplit[0])); - final MaterialData ingredient = new MaterialData(baseblock, - (byte) Integer.parseInt(itemsplit[1])); - recipe.setIngredient(shapechar, ingredient); - } else { - final Material baseblock = Material.getMaterial(Integer - .parseInt(itemsplit[0])); - recipe.setIngredient(shapechar, baseblock); - } - } - } - getServer().addRecipe(recipe); - } - } - } - - private boolean setupEconomy() { - if (getServer().getPluginManager().getPlugin("Vault") == null) - return false; - final RegisteredServiceProvider rsp = getServer().getServicesManager() - .getRegistration(Economy.class); - if (rsp == null) - return false; - econ = rsp.getProvider(); - return econ != null; - } - - public void setupLists() { - backpacks.clear(); - backpackRecipe.clear(); - backpackData.clear(); - backpackLore.clear(); - for (final String backpack : getConfig().getConfigurationSection("Backpacks") - .getKeys(false)) { - final List list = new ArrayList(); - backpacks.add(backpack); - list.add(0, getConfig().getString("Backpacks." + backpack + ".Size")); - list.add(1, getConfig().getString("Backpacks." + backpack + ".UseRecipe")); - if (getConfig().getStringList("Backpacks." + backpack + ".Recipe") != null) { - backpackRecipe.put(backpack, - getConfig().getStringList("Backpacks." + backpack + ".Recipe")); - } else { - backpackRecipe.put(backpack, null); - } - list.add(2, getConfig().getString("Backpacks." + backpack + ".BackpackItem.id")); - list.add(3, getConfig().getString("Backpacks." + backpack + ".BackpackItem.name")); - if (getConfig().getStringList("Backpacks." + backpack + ".BackpackItem.lore") != null) { - backpackLore.put(backpack, - getConfig().getStringList("Backpacks." + backpack + ".BackpackItem.lore")); - } else { - backpackLore.put(backpack, null); - } - list.add(4, getConfig().getString("Backpacks." + backpack + ".onDeath.destroyContents")); - list.add(5, getConfig().getString("Backpacks." + backpack + ".onDeath.dropContents")); - list.add(6, getConfig().getString("Backpacks." + backpack + ".onDeath.dropBackpack")); - list.add(7, getConfig().getString("Backpacks." + backpack + ".onDeath.keepBackpack")); - list.add(8, getConfig() - .getString("Backpacks." + backpack + ".WalkSpeedFeature.enabled")); - list.add( - 9, - getConfig().getString( - "Backpacks." + backpack + ".WalkSpeedFeature.walkingSpeed")); - list.add( - 10, - getConfig().getString( - "Backpacks." + backpack + ".IncreasedHungerFeature.enabled")); - list.add( - 11, - getConfig().getString( - "Backpacks." + backpack - + ".IncreasedHungerFeature.extraHungerBarsToDeplete")); - list.add( - 12, - getConfig().getString( - "Backpacks." + backpack - + ".IncreasedHungerFeature.hungerBarsToSubtractWhenEating")); - list.add(13, getConfig().getString("Backpacks." + backpack + ".Purchasable")); - list.add(14, getConfig().getString("Backpacks." + backpack + ".Price")); - list.add(15, getConfig().getString("Backpacks." + backpack + ".OpenWith")); - list.add(16, getConfig().getString("Backpacks." + backpack + ".UseWhitelist")); - list.add(17, getConfig().getString("Backpacks." + backpack + ".addGlow")); - list.add(18, getConfig().getString("Backpacks." + backpack + ".Unstackable")); - backpackData.put(backpack, list); - backpackBlacklist.put(backpack, - getConfig().getStringList("Backpacks." + backpack + ".ItemBlacklist")); - backpackWhitelist.put(backpack, - getConfig().getStringList("Backpacks." + backpack + ".ItemWhitelist")); - } - } - -} +package cn.citycraft.RealBackpacks; + +import java.lang.reflect.Constructor; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +import net.milkbowl.vault.economy.Economy; + +import org.bukkit.ChatColor; +import org.bukkit.Material; +import org.bukkit.configuration.file.FileConfiguration; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.ShapedRecipe; +import org.bukkit.inventory.meta.ItemMeta; +import org.bukkit.material.MaterialData; +import org.bukkit.plugin.RegisteredServiceProvider; +import org.bukkit.plugin.java.JavaPlugin; + +import cn.citycraft.RealBackpacks.config.Config; +import cn.citycraft.RealBackpacks.listeners.CraftListener; +import cn.citycraft.RealBackpacks.listeners.EntityListener; +import cn.citycraft.RealBackpacks.listeners.InventoryListener; +import cn.citycraft.RealBackpacks.listeners.PlayerListener; +import cn.citycraft.RealBackpacks.util.MysqlFunctions; +import cn.citycraft.RealBackpacks.util.RBUtil; +import cn.citycraft.RealBackpacks.util.VersionChecker; + +public class RealBackpacks extends JavaPlugin { + + public static RBInterface NMS; + + public static Economy econ = null; + public static boolean globalGlow = true; + + private static boolean average = false; + private static boolean add = false; + public static VersionChecker versionChecker; + private boolean usingMysql = false; + private boolean vault = true; + private boolean usingPermissions = true; + private String user = null; + private String password = null; + + private String url; + public List backpacks = new ArrayList(); + public HashMap> backpackData = new HashMap>(); + public HashMap> backpackLore = new HashMap>(); + public HashMap> backpackRecipe = new HashMap>(); + public HashMap backpackItems = new HashMap(); + public HashMap backpackOverrides = new HashMap(); + public HashMap> backpackBlacklist = new HashMap>(); + + public HashMap> backpackWhitelist = new HashMap>(); + public HashMap playerData = new HashMap(); + public HashMap adminFullView = new HashMap(); + public List adminRestrictedView = new ArrayList(); + + public List slowedPlayers = new ArrayList(); + + // List key + // 0 = Size + // 1 = UseRecipe + // 2 = id + // 3 = name + // 4 =destroyContents + // 5 = dropContents + // 6 = dropBackpack + // 7 = keepBackpack + // 8 = walkSpeedEnabled + // 9 = walkSpeedMultiplier + // 10 = increasedHungerEnabled + // 11 = hungerBarsToDeplete + // 12 = hungerBarsToSubtractWhenEating + // 13 = Purchasable + // 14 = Price + // 15 = OpenWith + // 16 = UseWhitelist + // 17 = addGlow + // 18 = Unstackable + + @Override + public FileConfiguration getConfig() { + if (Config.getInstance() == null) { + Config.load(this); + } + return Config.getInstance(); + } + + private ItemStack getConfigLore(final ItemStack item, final String backpack) { + final List key = backpackData.get(backpack); + final ItemMeta meta = item.getItemMeta(); + final ArrayList lore = new ArrayList(); + lore.clear(); + if (backpackLore.get(backpack) != null) { + for (final String s : backpackLore.get(backpack)) { + lore.add(ChatColor.translateAlternateColorCodes('&', s)); + } + meta.setLore(lore); + } + meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', key.get(3))); + item.setItemMeta(meta); + return item; + } + + public String getPass() { + return password; + } + + public String getUrl() { + return url; + } + + public String getUser() { + return user; + } + + public boolean isAdding() { + return add; + } + + public boolean isAveraging() { + return average; + } + + public boolean isUsingMysql() { + return usingMysql; + } + + public boolean isUsingPerms() { + return usingPermissions; + } + + public boolean isUsingVault() { + return vault; + } + + @Override + public void onEnable() { + final String p = getServer().getClass().getPackage().getName(); + final String version = p.substring(p.lastIndexOf('.') + 1); + String classname = getClass().getPackage().getName() + ".versions." + version; + try { + final Class clazz = Class.forName(classname); + final Constructor cons = clazz.getDeclaredConstructor(getClass()); + final Object obj = cons.newInstance(this); + if (obj instanceof RBInterface) { + NMS = (RBInterface) obj; + } + } catch (final Exception e) { + globalGlow = false; + } + + if (isEnabled()) { + MysqlFunctions.setMysqlFunc(this); + RBUtil.setRBUtil(this); + if (!setupEconomy()) { + getLogger().warning("Vault 未找到, 部分功能无法使用..."); + vault = false; + } else { + getLogger().info("Vault 已找到, 启用经济系统..."); + } + setupLists(); + setup(); + if (isEnabled()) { + getServer().getPluginManager().registerEvents(new PlayerListener(this), this); + getServer().getPluginManager().registerEvents(new CraftListener(this), this); + getServer().getPluginManager().registerEvents(new InventoryListener(this), this); + getServer().getPluginManager().registerEvents(new EntityListener(this), this); + getServer().getScheduler().runTaskTimer(this, new WalkSpeedRunnable(this), 20, 20); + + getCommand("rb").setExecutor(new MainCommand(this)); + + getLogger().info("真实背包已加载 By: 喵♂呜."); + } + } + new VersionChecker(this); + } + + @Override + public void reloadConfig() { + Config.load(this); + } + + @Override + public void saveConfig() { + if (Config.getInstance() == null) { + Config.load(this); + } + Config.save(); + } + + @SuppressWarnings("deprecation") + public void setup() { + user = getConfig().getString("Data.MySQL.username"); + password = getConfig().getString("Data.MySQL.password"); + url = "jdbc:mysql://" + getConfig().getString("Data.MySQL.ip") + ":" + getConfig().getInt("Data.MySQL.port") + "/" + + getConfig().getString("Data.MySQL.database"); + + if (!getConfig().isSet("Config.MultipleBackpacksInInventory.average")) { + average = false; + } else { + average = getConfig().getBoolean("Config.MultipleBackpacksInInventory.average"); + } + + if (!getConfig().isSet("Config.MultipleBackpacksInInventory.add")) { + add = false; + } else { + add = getConfig().getBoolean("Config.MultipleBackpacksInInventory.add"); + } + + if (!getConfig().isSet("Data.FileSystem")) { + usingMysql = false; + } else if (getConfig().getString("Data.FileSystem").equalsIgnoreCase("mysql") + || getConfig().getString("Data.FileSystem").equalsIgnoreCase("sql")) { + usingMysql = true; + if (!MysqlFunctions.checkIfTableExists("rb_data")) { + MysqlFunctions.createTables(); + } + } else { + usingMysql = false; + } + + if (!getConfig().isSet("Config.usePermissions")) { + usingPermissions = true; + } else { + usingPermissions = getConfig().getBoolean("Config.usePermissions"); + } + + for (final String backpack : backpacks) { + + final String override = getConfig().getString("Backpacks." + backpack + ".Override"); + if (override != null) { + backpackOverrides.put(backpack, RBUtil.getItemstackFromString(override)); + } else { + backpackOverrides.put(backpack, null); + } + + final List key = backpackData.get(backpack); + final String backpackItem = key.get(2); + backpackItems.put(backpack, getConfigLore(RBUtil.getItemstackFromString(backpackItem), backpack)); + + ShapedRecipe recipe = null; + if (key.get(1).equalsIgnoreCase("true")) { + recipe = new ShapedRecipe(backpackItems.get(backpack)); + recipe.shape("abc", "def", "ghi"); + int i = 0; + for (final String s : backpackRecipe.get(backpack)) { + final String[] itemIds = s.split(","); + char shapechar = 0; + for (final String itemid : itemIds) { + i++; + switch (i) { + case 1: + shapechar = 'a'; + break; + case 2: + shapechar = 'b'; + break; + case 3: + shapechar = 'c'; + break; + case 4: + shapechar = 'd'; + break; + case 5: + shapechar = 'e'; + break; + case 6: + shapechar = 'f'; + break; + case 7: + shapechar = 'g'; + break; + case 8: + shapechar = 'h'; + break; + case 9: + shapechar = 'i'; + break; + } + final String[] itemsplit = itemid.split(":"); + if (itemsplit[0].equals("0")) { + continue; + } + if (itemsplit.length > 1) { + final Material baseblock = Material.getMaterial(Integer.parseInt(itemsplit[0])); + final MaterialData ingredient = new MaterialData(baseblock, (byte) Integer.parseInt(itemsplit[1])); + recipe.setIngredient(shapechar, ingredient); + } else { + final Material baseblock = Material.getMaterial(Integer.parseInt(itemsplit[0])); + recipe.setIngredient(shapechar, baseblock); + } + } + } + getServer().addRecipe(recipe); + } + } + } + + private boolean setupEconomy() { + if (getServer().getPluginManager().getPlugin("Vault") == null) + return false; + final RegisteredServiceProvider rsp = getServer().getServicesManager().getRegistration(Economy.class); + if (rsp == null) + return false; + econ = rsp.getProvider(); + return econ != null; + } + + public void setupLists() { + backpacks.clear(); + backpackRecipe.clear(); + backpackData.clear(); + backpackLore.clear(); + for (final String backpack : getConfig().getConfigurationSection("Backpacks").getKeys(false)) { + final List list = new ArrayList(); + backpacks.add(backpack); + list.add(0, getConfig().getString("Backpacks." + backpack + ".Size")); + list.add(1, getConfig().getString("Backpacks." + backpack + ".UseRecipe")); + if (getConfig().getStringList("Backpacks." + backpack + ".Recipe") != null) { + backpackRecipe.put(backpack, getConfig().getStringList("Backpacks." + backpack + ".Recipe")); + } else { + backpackRecipe.put(backpack, null); + } + list.add(2, getConfig().getString("Backpacks." + backpack + ".BackpackItem.id")); + list.add(3, getConfig().getString("Backpacks." + backpack + ".BackpackItem.name")); + if (getConfig().getStringList("Backpacks." + backpack + ".BackpackItem.lore") != null) { + backpackLore.put(backpack, getConfig().getStringList("Backpacks." + backpack + ".BackpackItem.lore")); + } else { + backpackLore.put(backpack, null); + } + list.add(4, getConfig().getString("Backpacks." + backpack + ".onDeath.destroyContents")); + list.add(5, getConfig().getString("Backpacks." + backpack + ".onDeath.dropContents")); + list.add(6, getConfig().getString("Backpacks." + backpack + ".onDeath.dropBackpack")); + list.add(7, getConfig().getString("Backpacks." + backpack + ".onDeath.keepBackpack")); + list.add(8, getConfig().getString("Backpacks." + backpack + ".WalkSpeedFeature.enabled")); + list.add(9, getConfig().getString("Backpacks." + backpack + ".WalkSpeedFeature.walkingSpeed")); + list.add(10, getConfig().getString("Backpacks." + backpack + ".IncreasedHungerFeature.enabled")); + list.add(11, getConfig().getString("Backpacks." + backpack + ".IncreasedHungerFeature.extraHungerBarsToDeplete")); + list.add(12, getConfig().getString("Backpacks." + backpack + ".IncreasedHungerFeature.hungerBarsToSubtractWhenEating")); + list.add(13, getConfig().getString("Backpacks." + backpack + ".Purchasable")); + list.add(14, getConfig().getString("Backpacks." + backpack + ".Price")); + list.add(15, getConfig().getString("Backpacks." + backpack + ".OpenWith")); + list.add(16, getConfig().getString("Backpacks." + backpack + ".UseWhitelist")); + list.add(17, getConfig().getString("Backpacks." + backpack + ".addGlow")); + list.add(18, getConfig().getString("Backpacks." + backpack + ".Unstackable")); + backpackData.put(backpack, list); + backpackBlacklist.put(backpack, getConfig().getStringList("Backpacks." + backpack + ".ItemBlacklist")); + backpackWhitelist.put(backpack, getConfig().getStringList("Backpacks." + backpack + ".ItemWhitelist")); + } + } + +} diff --git a/src/cn/citycraft/RealBackpacks/listeners/PlayerListener.java b/src/cn/citycraft/RealBackpacks/listeners/PlayerListener.java index a62b64e..3da5e82 100644 --- a/src/cn/citycraft/RealBackpacks/listeners/PlayerListener.java +++ b/src/cn/citycraft/RealBackpacks/listeners/PlayerListener.java @@ -1,271 +1,255 @@ -package cn.citycraft.RealBackpacks.listeners; - -import java.sql.SQLException; -import java.util.HashMap; -import java.util.List; - -import org.bukkit.ChatColor; -import org.bukkit.Location; -import org.bukkit.entity.Player; -import org.bukkit.event.EventHandler; -import org.bukkit.event.EventPriority; -import org.bukkit.event.Listener; -import org.bukkit.event.block.Action; -import org.bukkit.event.entity.PlayerDeathEvent; -import org.bukkit.event.player.PlayerDropItemEvent; -import org.bukkit.event.player.PlayerInteractEvent; -import org.bukkit.event.player.PlayerJoinEvent; -import org.bukkit.event.player.PlayerPickupItemEvent; -import org.bukkit.event.player.PlayerRespawnEvent; -import org.bukkit.inventory.Inventory; -import org.bukkit.inventory.ItemStack; - -import cn.citycraft.RealBackpacks.RealBackpacks; -import cn.citycraft.RealBackpacks.config.FileConfig; -import cn.citycraft.RealBackpacks.config.PlayerConfig; -import cn.citycraft.RealBackpacks.util.MysqlFunctions; -import cn.citycraft.RealBackpacks.util.RBUtil; -import cn.citycraft.RealBackpacks.util.Serialization; - -public class PlayerListener implements Listener { - - private final RealBackpacks plugin; - - private final HashMap deadPlayers = new HashMap(); - - public PlayerListener(final RealBackpacks plugin) { - this.plugin = plugin; - } - - @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) - public void onDeath(final PlayerDeathEvent e) { - final Player p = e.getEntity(); - final String name = p.getName(); - for (final String backpack : plugin.backpacks) { - if (!p.getInventory().contains(plugin.backpackItems.get(backpack))) { - continue; - } - p.setWalkSpeed(0.2F); - final List key = plugin.backpackData.get(backpack); - if (key.get(5) != null && key.get(5).equalsIgnoreCase("true")) { - // Drop contents - Inventory binv = null; - if (plugin.isUsingMysql()) { - try { - binv = MysqlFunctions.getBackpackInv(name, backpack); - } catch (final SQLException e1) { - e1.printStackTrace(); - } - } else { - FileConfig config = PlayerConfig.getInstance(plugin, name); - if (config.getStringList(backpack + ".Inventory") == null) { - continue; - } - binv = Serialization.toInventory(config.getStringList(backpack + ".Inventory"), - key.get(3), Integer.parseInt(key.get(0))); - } - if (plugin.playerData.containsKey(name)) { - if (p.getItemOnCursor() != null) { - p.setItemOnCursor(null); - } - } - if (binv != null) { - for (final ItemStack item : binv.getContents()) { - if (item != null) { - p.getWorld().dropItemNaturally(p.getLocation(), item); - } - } - } - RBUtil.destroyContents(name, backpack); - } - if (key.get(4) != null && key.get(4).equalsIgnoreCase("true")) { - // Destroy contents - RBUtil.destroyContents(name, backpack); - p.sendMessage(ChatColor.RED + "因为死亡背包物品已销毁..."); - } - if (key.get(6) != null && key.get(6).equalsIgnoreCase("false")) { - // Drop backpack - e.getDrops().remove(plugin.backpackItems.get(backpack)); - } - if (key.get(7) != null && key.get(7).equalsIgnoreCase("true")) { - deadPlayers.put(name, backpack); - } - } - } - - @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) - public void onDrop(final PlayerDropItemEvent e) { - final Player p = e.getPlayer(); - final String name = p.getName(); - final ItemStack item = e.getItemDrop().getItemStack(); - if (plugin.slowedPlayers.contains(name)) { - for (final String backpack : plugin.backpacks) { - if (plugin.backpackItems.get(backpack).equals(item)) { - plugin.slowedPlayers.remove(name); - p.setWalkSpeed(0.2F); - break; - } - } - } - } - - @EventHandler(priority = EventPriority.HIGH) - public void onInteract(final PlayerInteractEvent e) { - if (e.getAction().equals(Action.PHYSICAL)) - return; - - final Action act = e.getAction(); - final Player p = e.getPlayer(); - final ItemStack item = p.getItemInHand(); - final String name = p.getName(); - if (item.hasItemMeta()) { - for (final String backpack : plugin.backpacks) { - final List key = plugin.backpackData.get(backpack); - if (item.getItemMeta().hasDisplayName() - && item.getItemMeta().getDisplayName() - .equals(ChatColor.translateAlternateColorCodes('&', key.get(3)))) { - if (plugin.isUsingPerms() && !p.hasPermission("rb." + backpack + ".use")) { - p.sendMessage(ChatColor.RED + "你没有打开此背包的权限..."); - continue; - } - final String openWith = key.get(15); - if (openWith != null) { - if (openWith.equalsIgnoreCase("left_click")) { - if (act.equals(Action.RIGHT_CLICK_AIR)) { - continue; - } - if (act.equals(Action.RIGHT_CLICK_BLOCK)) { - continue; - } - } else if (openWith.equalsIgnoreCase("right_click")) { - if (act.equals(Action.LEFT_CLICK_AIR)) { - continue; - } - if (act.equals(Action.LEFT_CLICK_BLOCK)) { - continue; - } - } - } else { - if (act.equals(Action.LEFT_CLICK_AIR)) { - continue; - } - if (act.equals(Action.LEFT_CLICK_BLOCK)) { - continue; - } - } - if (act.equals(Action.RIGHT_CLICK_BLOCK)) { - e.setCancelled(true); - p.updateInventory(); - } - Inventory inv = null; - if (plugin.isUsingMysql()) { - try { - inv = MysqlFunctions.getBackpackInv(name, backpack); - } catch (final SQLException e1) { - e1.printStackTrace(); - } - if (inv == null) { - inv = plugin.getServer().createInventory(p, - Integer.parseInt(key.get(0)), - ChatColor.translateAlternateColorCodes('&', key.get(3))); - } - } else { - FileConfig config = PlayerConfig.getInstance(plugin, name); - if (!config.isSet(backpack + ".Inventory")) { - inv = plugin.getServer().createInventory(p, - Integer.parseInt(key.get(0)), - ChatColor.translateAlternateColorCodes('&', key.get(3))); - } else { - inv = Serialization.toInventory( - config.getStringList(backpack + ".Inventory"), key.get(3), - Integer.parseInt(key.get(0))); - } - } - if (p.getOpenInventory().getTopInventory() != null) { - p.closeInventory(); - } - plugin.playerData.put(name, backpack); - p.openInventory(inv); - break; - } - } - } - } - - @EventHandler(priority = EventPriority.HIGH) - public void onJoin(PlayerJoinEvent e) { - Player player = e.getPlayer(); - if (player.hasPermission("rb.reload")) { - RealBackpacks.versionChecker.VersionCheck(player); - } - } - - @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) - public void onPickup(final PlayerPickupItemEvent e) { - final ItemStack item = e.getItem().getItemStack(); - final Player p = e.getPlayer(); - final String name = p.getName(); - for (final String backpack : plugin.backpacks) { - if (!item.isSimilar(plugin.backpackItems.get(backpack))) { - continue; - } - final List key = plugin.backpackData.get(backpack); - if (!plugin.slowedPlayers.contains(name)) { - plugin.slowedPlayers.add(name); - } - p.setWalkSpeed(Float.parseFloat(key.get(9))); - if (key.get(18) != null && key.get(18).equalsIgnoreCase("true")) { - final Inventory inv = p.getInventory(); - final Location loc = e.getItem().getLocation(); - final ItemStack backpackItem = plugin.backpackItems.get(backpack); - int emptySlots = 0,itemAmount = item.getAmount(); - for (final ItemStack invItem : inv.getContents()) { - if (invItem == null) { - emptySlots++; - } - } - if (emptySlots == 0) { - e.setCancelled(true); - } else { - e.getItem().remove(); - e.setCancelled(true); - if (itemAmount > emptySlots) { - final ItemStack dropItem = backpackItem; - dropItem.setAmount(itemAmount - emptySlots); - p.getWorld().dropItem(loc, dropItem); - itemAmount = emptySlots; - } - if (itemAmount == 1) { - backpackItem.setAmount(1); - inv.setItem(inv.firstEmpty(), backpackItem); - } else if (itemAmount > 1) { - int x = itemAmount; - backpackItem.setAmount(1); - while (x > 0) { - x--; - inv.setItem(inv.firstEmpty(), backpackItem); - } - } - } - } - break; - } - } - - @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) - public void onRespawn(final PlayerRespawnEvent e) { - final Player p = e.getPlayer(); - final String name = p.getName(); - for (final String backpack : plugin.backpacks) { - final List key = plugin.backpackData.get(backpack); - if (key.get(7) != null && key.get(7).equalsIgnoreCase("true") - && deadPlayers.get(name) != null && deadPlayers.get(name).equals(backpack)) { - // Keep backpack - p.getInventory().addItem(plugin.backpackItems.get(backpack)); - p.updateInventory(); - deadPlayers.remove(name); - } - } - } - -} +package cn.citycraft.RealBackpacks.listeners; + +import java.sql.SQLException; +import java.util.HashMap; +import java.util.List; + +import org.bukkit.ChatColor; +import org.bukkit.Location; +import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.event.block.Action; +import org.bukkit.event.entity.PlayerDeathEvent; +import org.bukkit.event.player.PlayerDropItemEvent; +import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.event.player.PlayerPickupItemEvent; +import org.bukkit.event.player.PlayerRespawnEvent; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; + +import cn.citycraft.RealBackpacks.RealBackpacks; +import cn.citycraft.RealBackpacks.config.FileConfig; +import cn.citycraft.RealBackpacks.config.PlayerConfig; +import cn.citycraft.RealBackpacks.util.MysqlFunctions; +import cn.citycraft.RealBackpacks.util.RBUtil; +import cn.citycraft.RealBackpacks.util.Serialization; + +public class PlayerListener implements Listener { + + private final RealBackpacks plugin; + + private final HashMap deadPlayers = new HashMap(); + + public PlayerListener(final RealBackpacks plugin) { + this.plugin = plugin; + } + + @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) + public void onDeath(final PlayerDeathEvent e) { + final Player p = e.getEntity(); + final String name = p.getName(); + for (final String backpack : plugin.backpacks) { + if (!p.getInventory().contains(plugin.backpackItems.get(backpack))) { + continue; + } + p.setWalkSpeed(0.2F); + final List key = plugin.backpackData.get(backpack); + if (key.get(5) != null && key.get(5).equalsIgnoreCase("true")) { + // Drop contents + Inventory binv = null; + if (plugin.isUsingMysql()) { + try { + binv = MysqlFunctions.getBackpackInv(name, backpack); + } catch (final SQLException e1) { + e1.printStackTrace(); + } + } else { + FileConfig config = PlayerConfig.getInstance(plugin, name); + if (config.getStringList(backpack + ".Inventory") == null) { + continue; + } + binv = Serialization.toInventory(config.getStringList(backpack + ".Inventory"), key.get(3), Integer.parseInt(key.get(0))); + } + if (plugin.playerData.containsKey(name)) { + if (p.getItemOnCursor() != null) { + p.setItemOnCursor(null); + } + } + if (binv != null) { + for (final ItemStack item : binv.getContents()) { + if (item != null) { + p.getWorld().dropItemNaturally(p.getLocation(), item); + } + } + } + RBUtil.destroyContents(name, backpack); + } + if (key.get(4) != null && key.get(4).equalsIgnoreCase("true")) { + // Destroy contents + RBUtil.destroyContents(name, backpack); + p.sendMessage(ChatColor.RED + "因为死亡背包物品已销毁..."); + } + if (key.get(6) != null && key.get(6).equalsIgnoreCase("false")) { + // Drop backpack + e.getDrops().remove(plugin.backpackItems.get(backpack)); + } + if (key.get(7) != null && key.get(7).equalsIgnoreCase("true")) { + deadPlayers.put(name, backpack); + } + } + } + + @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) + public void onDrop(final PlayerDropItemEvent e) { + final Player p = e.getPlayer(); + final String name = p.getName(); + final ItemStack item = e.getItemDrop().getItemStack(); + if (plugin.slowedPlayers.contains(name)) { + for (final String backpack : plugin.backpacks) { + if (plugin.backpackItems.get(backpack).equals(item)) { + plugin.slowedPlayers.remove(name); + p.setWalkSpeed(0.2F); + break; + } + } + } + } + + @EventHandler(priority = EventPriority.HIGH) + public void onInteract(final PlayerInteractEvent e) { + if (e.getAction().equals(Action.PHYSICAL)) + return; + + final Action act = e.getAction(); + final Player p = e.getPlayer(); + final ItemStack item = p.getItemInHand(); + final String name = p.getName(); + if (item.hasItemMeta()) { + for (final String backpack : plugin.backpacks) { + final List key = plugin.backpackData.get(backpack); + if (item.getItemMeta().hasDisplayName() + && item.getItemMeta().getDisplayName().equals(ChatColor.translateAlternateColorCodes('&', key.get(3)))) { + if (plugin.isUsingPerms() && !p.hasPermission("rb." + backpack + ".use")) { + p.sendMessage(ChatColor.RED + "你没有打开此背包的权限..."); + continue; + } + final String openWith = key.get(15); + if (openWith != null) { + if (openWith.equalsIgnoreCase("left_click")) { + if (act.equals(Action.RIGHT_CLICK_AIR)) { + continue; + } + if (act.equals(Action.RIGHT_CLICK_BLOCK)) { + continue; + } + } else if (openWith.equalsIgnoreCase("right_click")) { + if (act.equals(Action.LEFT_CLICK_AIR)) { + continue; + } + if (act.equals(Action.LEFT_CLICK_BLOCK)) { + continue; + } + } + } else { + if (act.equals(Action.LEFT_CLICK_AIR)) { + continue; + } + if (act.equals(Action.LEFT_CLICK_BLOCK)) { + continue; + } + } + if (act.equals(Action.RIGHT_CLICK_BLOCK)) { + e.setCancelled(true); + p.updateInventory(); + } + Inventory inv = null; + if (plugin.isUsingMysql()) { + try { + inv = MysqlFunctions.getBackpackInv(name, backpack); + } catch (final SQLException e1) { + e1.printStackTrace(); + } + if (inv == null) { + inv = plugin.getServer().createInventory(p, Integer.parseInt(key.get(0)), + ChatColor.translateAlternateColorCodes('&', key.get(3))); + } + } else { + FileConfig config = PlayerConfig.getInstance(plugin, name); + if (!config.isSet(backpack + ".Inventory")) { + inv = plugin.getServer().createInventory(p, Integer.parseInt(key.get(0)), + ChatColor.translateAlternateColorCodes('&', key.get(3))); + } else { + inv = Serialization.toInventory(config.getStringList(backpack + ".Inventory"), key.get(3), Integer.parseInt(key.get(0))); + } + } + if (p.getOpenInventory().getTopInventory() != null) { + p.closeInventory(); + } + plugin.playerData.put(name, backpack); + p.openInventory(inv); + break; + } + } + } + } + + @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) + public void onPickup(final PlayerPickupItemEvent e) { + final ItemStack item = e.getItem().getItemStack(); + final Player p = e.getPlayer(); + final String name = p.getName(); + for (final String backpack : plugin.backpacks) { + if (!item.isSimilar(plugin.backpackItems.get(backpack))) { + continue; + } + final List key = plugin.backpackData.get(backpack); + if (!plugin.slowedPlayers.contains(name)) { + plugin.slowedPlayers.add(name); + } + p.setWalkSpeed(Float.parseFloat(key.get(9))); + if (key.get(18) != null && key.get(18).equalsIgnoreCase("true")) { + final Inventory inv = p.getInventory(); + final Location loc = e.getItem().getLocation(); + final ItemStack backpackItem = plugin.backpackItems.get(backpack); + int emptySlots = 0, itemAmount = item.getAmount(); + for (final ItemStack invItem : inv.getContents()) { + if (invItem == null) { + emptySlots++; + } + } + if (emptySlots == 0) { + e.setCancelled(true); + } else { + e.getItem().remove(); + e.setCancelled(true); + if (itemAmount > emptySlots) { + final ItemStack dropItem = backpackItem; + dropItem.setAmount(itemAmount - emptySlots); + p.getWorld().dropItem(loc, dropItem); + itemAmount = emptySlots; + } + if (itemAmount == 1) { + backpackItem.setAmount(1); + inv.setItem(inv.firstEmpty(), backpackItem); + } else if (itemAmount > 1) { + int x = itemAmount; + backpackItem.setAmount(1); + while (x > 0) { + x--; + inv.setItem(inv.firstEmpty(), backpackItem); + } + } + } + } + break; + } + } + + @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) + public void onRespawn(final PlayerRespawnEvent e) { + final Player p = e.getPlayer(); + final String name = p.getName(); + for (final String backpack : plugin.backpacks) { + final List key = plugin.backpackData.get(backpack); + if (key.get(7) != null && key.get(7).equalsIgnoreCase("true") && deadPlayers.get(name) != null && deadPlayers.get(name).equals(backpack)) { + // Keep backpack + p.getInventory().addItem(plugin.backpackItems.get(backpack)); + p.updateInventory(); + deadPlayers.remove(name); + } + } + } + +} diff --git a/src/cn/citycraft/RealBackpacks/util/VersionChecker.java b/src/cn/citycraft/RealBackpacks/util/VersionChecker.java index aa78ffc..acacb96 100644 --- a/src/cn/citycraft/RealBackpacks/util/VersionChecker.java +++ b/src/cn/citycraft/RealBackpacks/util/VersionChecker.java @@ -10,46 +10,96 @@ import org.bukkit.ChatColor; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.entity.Player; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.plugin.Plugin; import com.google.common.base.Charsets; -public class VersionChecker { +/** + * 自动更新类 + * + * @author 蒋天蓓 + * 2015年8月14日下午4:01:15 + */ +public class VersionChecker implements Listener { Plugin plugin; + public String checkurl = "https://coding.net/u/502647092/p/%s/git/raw/%s/src/plugin.yml"; + public String branch = "master"; + /** + * @param plugin + * - 插件 + */ public VersionChecker(Plugin plugin) { this.plugin = plugin; + plugin.getServer().getPluginManager().registerEvents(this, plugin); + this.versioncheck(null); } - public void VersionCheck(final Player player) { + /** + * @param plugin + * - 插件 + * @param branch + * - 分支名称 + */ + public VersionChecker(Plugin plugin, String branch) { + this.plugin = plugin; + plugin.getServer().getPluginManager().registerEvents(this, plugin); + this.branch = branch; + this.versioncheck(null); + } + + /** + * 获取插件更新链接 + * + * @param pluginName + * - 插件名称 + * @param branch + * - 插件分支 + * @return 更新链接 + */ + public String getCheckUrl(String pluginName, String branch) { + return String.format(checkurl, pluginName, branch); + } + + @EventHandler + public void onPlayerJoin(PlayerJoinEvent e) { + if (e.getPlayer().isOp()) { + this.versioncheck(e.getPlayer()); + } + } + + /** + * 开始更新 + * + * @param player + * - 获取更新的玩家(null则默认为控制台) + */ + public void versioncheck(final Player player) { Bukkit.getScheduler().runTaskAsynchronously(plugin, new Runnable() { @Override public void run() { - String website = plugin.getDescription().getWebsite(); - String readURL = website - + (website.substring(website.length() - 1).equals("/") ? "" : "/") - + "/lastSuccessfulBuild/artifact/src/plugin.yml"; + String readURL = getCheckUrl(plugin.getName(), branch); FileConfiguration config; String currentVersion = plugin.getDescription().getVersion(); try { URL url = new URL(readURL); - BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream(), - Charsets.UTF_8)); + BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream(), Charsets.UTF_8)); config = YamlConfiguration.loadConfiguration(br); String newVersion = config.getString("version"); br.close(); if (!newVersion.equals(currentVersion)) { String[] msg = new String[] { - ChatColor.GREEN + plugin.getName() + "插件最新版本 v" + newVersion, + ChatColor.GREEN + plugin.getName() + " 插件最新版本 v" + newVersion, ChatColor.RED + "服务器运行版本: v" + currentVersion, - ChatColor.GOLD + "插件更新网站: " + ChatColor.BLUE - + plugin.getDescription().getWebsite() }; + ChatColor.GOLD + "插件更新网站: " + ChatColor.BLUE + plugin.getDescription().getWebsite() + }; if (player != null) { player.sendMessage(msg); } else { - for (String s : msg) { - plugin.getLogger().info(s); - } + plugin.getServer().getConsoleSender().sendMessage(msg); } } } catch (IOException e) {