From 2b4ca80a95074b0724ec95f588a9ad037f184c8d Mon Sep 17 00:00:00 2001 From: GJ Date: Fri, 4 May 2012 14:04:42 -0400 Subject: [PATCH] Start of work on allowing custom tool/block mods. This version should allow for XP gain from vanilla blocks with custom tools. Please report any issues to facilitate further development. --- .../java/com/gmail/nossr50/config/Config.java | 6 +- .../gmail/nossr50/config/LoadTreasures.java | 4 +- .../nossr50/config/mods/LoadCustomTools.java | 109 +++++++++++++++++ .../com/gmail/nossr50/datatypes/ToolType.java | 2 +- .../nossr50/datatypes/mods/CustomTool.java | 59 +++++++++ .../nossr50/datatypes/treasure/Treasure.java | 4 +- .../nossr50/listeners/BlockListener.java | 4 +- src/main/java/com/gmail/nossr50/mcMMO.java | 70 ++++++++++- .../com/gmail/nossr50/skills/misc/Repair.java | 2 +- .../com/gmail/nossr50/util/ItemChecks.java | 44 +++++-- src/main/resources/config.yml | 7 ++ src/main/resources/tools.yml | 113 ++++++++++++++++++ 12 files changed, 399 insertions(+), 25 deletions(-) create mode 100644 src/main/java/com/gmail/nossr50/config/mods/LoadCustomTools.java create mode 100644 src/main/java/com/gmail/nossr50/datatypes/mods/CustomTool.java create mode 100644 src/main/resources/tools.yml diff --git a/src/main/java/com/gmail/nossr50/config/Config.java b/src/main/java/com/gmail/nossr50/config/Config.java index bed299cb5..6a5ae5067 100644 --- a/src/main/java/com/gmail/nossr50/config/Config.java +++ b/src/main/java/com/gmail/nossr50/config/Config.java @@ -53,7 +53,10 @@ public class Config extends ConfigLoader { public double getHardcoreDeathStatPenaltyPercentage() { return config.getDouble("Hardcore.Death_Stat_Loss_Penalty_Percentage", 75); } public double getHardcoreVampirismStatLeechPercentage() { return config.getDouble("Hardcore.Vampirism_Stat_Leech_Percentage", 5); } public boolean getHardcoreVampirismEnabled() { return config.getBoolean("Hardcore.Vampirism", false); } - + + /* SMP Mods */ + public boolean getToolModsEnabled() { return config.getBoolean("Mods.Tool_Mods_Enabled", false); } + public boolean getBlockModsEnabled() { return config.getBoolean("Mods.Block_Mods_Enabled", false); } /* Commands */ public boolean getCommandXPLockEnabled() { return config.getBoolean("Commands.xplock.Enabled", true); } @@ -446,7 +449,6 @@ public class Config extends ConfigLoader { @Override protected void load() { - // If it doesn't exist, copy it from the .jar if (!configFile.exists()) { dataFolder.mkdir(); plugin.saveDefaultConfig(); diff --git a/src/main/java/com/gmail/nossr50/config/LoadTreasures.java b/src/main/java/com/gmail/nossr50/config/LoadTreasures.java index 60f8feb8b..c7cc066b8 100644 --- a/src/main/java/com/gmail/nossr50/config/LoadTreasures.java +++ b/src/main/java/com/gmail/nossr50/config/LoadTreasures.java @@ -48,8 +48,6 @@ public class LoadTreasures extends ConfigLoader{ @Override protected void load() { - - // If it doesn't exist, copy it from the .jar if (!configFile.exists()) { dataFolder.mkdir(); plugin.saveTreasuresConfig(); @@ -95,7 +93,7 @@ public class LoadTreasures extends ConfigLoader{ int data = config.getInt("Treasures." + treasureName + ".Data"); if (Material.getMaterial(id) == null) { - reason.add("Invlid id: " + id); + reason.add("Invalid id: " + id); } if (amount < 1) { diff --git a/src/main/java/com/gmail/nossr50/config/mods/LoadCustomTools.java b/src/main/java/com/gmail/nossr50/config/mods/LoadCustomTools.java new file mode 100644 index 000000000..b14d60101 --- /dev/null +++ b/src/main/java/com/gmail/nossr50/config/mods/LoadCustomTools.java @@ -0,0 +1,109 @@ +package com.gmail.nossr50.config.mods; + +import java.io.File; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Set; + +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.inventory.ItemStack; + +import com.gmail.nossr50.mcMMO; +import com.gmail.nossr50.config.ConfigLoader; +import com.gmail.nossr50.datatypes.mods.CustomTool; + +public class LoadCustomTools extends ConfigLoader { + private static LoadCustomTools instance; + + public static LoadCustomTools getInstance() { + if (instance == null) { + instance = new LoadCustomTools(mcMMO.p); + } + + return instance; + } + + public List customAxes = new ArrayList(); + public List customBows = new ArrayList(); + public List customHoes = new ArrayList(); + public List customPickaxes = new ArrayList(); + public List customShovels = new ArrayList(); + public List customSwords = new ArrayList(); + + public List customAxeIDs = new ArrayList(); + public List customBowIDs = new ArrayList(); + public List customHoeIDs = new ArrayList(); + public List customPickaxeIDs = new ArrayList(); + public List customShovelIDs = new ArrayList(); + public List customSwordIDs = new ArrayList(); + + private LoadCustomTools(mcMMO plugin) { + super(plugin, "ModConfigs" + File.separator + "tools.yml"); + config = plugin.getToolsConfig(); + load(); + } + + @Override + protected void load() { + if (!configFile.exists()) { + dataFolder.mkdir(); + plugin.saveToolsConfig(); + } + + addDefaults(); + loadKeys(); + } + + @Override + protected void loadKeys() { + plugin.getLogger().info("Loading mcMMO tools.yml File..."); + + loadTool("Axes", customAxes, customAxeIDs); + loadTool("Bows", customBows, customBowIDs); + loadTool("Hoes", customHoes, customHoeIDs); + loadTool("Pickaxes", customPickaxes, customPickaxeIDs); + loadTool("Shovels", customShovels, customShovelIDs); + loadTool("Swords", customSwords, customSwordIDs); + } + + private void loadTool(String toolType, List toolList, List idList) { + ConfigurationSection toolSection = config.getConfigurationSection(toolType); + Set toolConfigSet = toolSection.getKeys(false); + Iterator iterator = toolConfigSet.iterator(); + + while (iterator.hasNext()) { + String toolName = iterator.next(); + + int id = config.getInt(toolType + "." + toolName + ".ID"); + double multiplier = config.getDouble(toolType + "." + toolName + ".XP_Modifier", 1.0); + boolean repairable = config.getBoolean(toolType + "." + toolName + ".Repairable"); + int repairID = config.getInt(toolType + "." + toolName + ".Repair_Material_ID"); + byte repairData = (byte) config.getInt(toolType + "." + toolName + ".Repair_Material_Data_Value"); + short durability = (short) config.getInt(toolType + "." + toolName + ".Durability"); + + if (id == 0) { + plugin.getLogger().warning("Missing ID. This item will be skipped."); + continue; + } + + if (repairable && (repairID == 0 || durability == 0)) { + plugin.getLogger().warning("Incomplete repair information. This item will be unrepairable."); + repairable = false; + } + + CustomTool tool; + + if (repairable) { + ItemStack repairMaterial = new ItemStack(repairID, 1, (short) 0, repairData); + tool = new CustomTool(durability, repairMaterial, repairable, multiplier, id); + } + else { + tool = new CustomTool(durability, null, repairable, multiplier, id); + } + + toolList.add(tool); + idList.add(id); + } + } +} diff --git a/src/main/java/com/gmail/nossr50/datatypes/ToolType.java b/src/main/java/com/gmail/nossr50/datatypes/ToolType.java index 4430ae6c0..e2c05b159 100644 --- a/src/main/java/com/gmail/nossr50/datatypes/ToolType.java +++ b/src/main/java/com/gmail/nossr50/datatypes/ToolType.java @@ -48,7 +48,7 @@ public enum ToolType { return ItemChecks.isHoe(is); case PICKAXE: - return ItemChecks.isMiningPick(is); + return ItemChecks.isPickaxe(is); case SHOVEL: return ItemChecks.isShovel(is); diff --git a/src/main/java/com/gmail/nossr50/datatypes/mods/CustomTool.java b/src/main/java/com/gmail/nossr50/datatypes/mods/CustomTool.java new file mode 100644 index 000000000..45d8fa1ea --- /dev/null +++ b/src/main/java/com/gmail/nossr50/datatypes/mods/CustomTool.java @@ -0,0 +1,59 @@ +package com.gmail.nossr50.datatypes.mods; + +import org.bukkit.inventory.ItemStack; + +public class CustomTool { + private int itemID; + private double xpMultiplier; + private boolean repairable; + private ItemStack repairMaterial; + private short durability; + + public CustomTool(short durability, ItemStack repairMaterial, boolean repairable, double xpMultiplier, int itemID) { + this.itemID = itemID; + this.xpMultiplier = xpMultiplier; + this.repairable = repairable; + this.repairMaterial = repairMaterial; + this.durability = durability; + } + + public int getItemID() { + return itemID; + } + + public void setItemID(int itemID) { + this.itemID = itemID; + } + + public double getXpMultiplier() { + return xpMultiplier; + } + + public void setXpMultiplier(Double xpMultiplier) { + this.xpMultiplier = xpMultiplier; + } + + public boolean isRepairable() { + return repairable; + } + + public void setRepairable(boolean repairable) { + this.repairable = repairable; + } + + public ItemStack getRepairMaterial() { + return repairMaterial; + } + + public void setRepairMaterial(ItemStack repairMaterial) { + this.repairMaterial = repairMaterial; + } + + public short getDurability() { + return durability; + } + + public void setDurability(short durability) { + this.durability = durability; + } +} diff --git a/src/main/java/com/gmail/nossr50/datatypes/treasure/Treasure.java b/src/main/java/com/gmail/nossr50/datatypes/treasure/Treasure.java index 8c3d18e49..29aedbe90 100644 --- a/src/main/java/com/gmail/nossr50/datatypes/treasure/Treasure.java +++ b/src/main/java/com/gmail/nossr50/datatypes/treasure/Treasure.java @@ -4,7 +4,7 @@ import org.bukkit.inventory.ItemStack; public abstract class Treasure { private int xp; - private Double dropChance; + private double dropChance; private int dropLevel; private ItemStack drop; @@ -31,7 +31,7 @@ public abstract class Treasure { this.xp = xp; } - public Double getDropChance() { + public double getDropChance() { return dropChance; } diff --git a/src/main/java/com/gmail/nossr50/listeners/BlockListener.java b/src/main/java/com/gmail/nossr50/listeners/BlockListener.java index 16545a1c7..6f1f59cfd 100644 --- a/src/main/java/com/gmail/nossr50/listeners/BlockListener.java +++ b/src/main/java/com/gmail/nossr50/listeners/BlockListener.java @@ -164,7 +164,7 @@ public class BlockListener implements Listener { */ if (Permissions.getInstance().mining(player) && BlockChecks.canBeSuperBroken(mat)) { - if (Config.getInstance().getMiningRequiresTool() && ItemChecks.isMiningPick(inhand)) { + if (Config.getInstance().getMiningRequiresTool() && ItemChecks.isPickaxe(inhand)) { Mining.miningBlockCheck(player, block); } else if (!Config.getInstance().getMiningRequiresTool()) { @@ -279,7 +279,7 @@ public class BlockListener implements Listener { } else if (PP.getAbilityMode(AbilityType.SUPER_BREAKER) && Skills.triggerCheck(player, block, AbilityType.SUPER_BREAKER)) { if(!player.getItemInHand().containsEnchantment(Enchantment.SILK_TOUCH)) { - if (Config.getInstance().getMiningRequiresTool() && ItemChecks.isMiningPick(inhand)) { + if (Config.getInstance().getMiningRequiresTool() && ItemChecks.isPickaxe(inhand)) { event.setInstaBreak(true); Mining.SuperBreakerBlockCheck(player, block); } diff --git a/src/main/java/com/gmail/nossr50/mcMMO.java b/src/main/java/com/gmail/nossr50/mcMMO.java index 115b1a6b3..759952c31 100644 --- a/src/main/java/com/gmail/nossr50/mcMMO.java +++ b/src/main/java/com/gmail/nossr50/mcMMO.java @@ -56,6 +56,7 @@ public class mcMMO extends JavaPlugin { public static String flatFileDirectory; public static String usersFile; public static String leaderboardDirectory; + public static String modDirectory; /** * Things to be run when the plugin is enabled. @@ -67,6 +68,7 @@ public class mcMMO extends JavaPlugin { mainDirectory = getDataFolder().getPath() + File.separator; flatFileDirectory = mainDirectory + "FlatFileStuff" + File.separator; leaderboardDirectory = flatFileDirectory + "Leaderboards" + File.separator; + modDirectory = mainDirectory + "ModConfigs" + File.separator; usersFile = flatFileDirectory + "mcmmo.users"; if (!Config.getInstance().getUseMySQL()) { @@ -79,11 +81,12 @@ public class mcMMO extends JavaPlugin { pm.registerEvents(playerListener, this); pm.registerEvents(blockListener, this); pm.registerEvents(entityListener, this); + if (Config.getInstance().getHardcoreEnabled()) { pm.registerEvents(hardcoreListener, this); } - PluginDescriptionFile pdfFile = this.getDescription(); + PluginDescriptionFile pdfFile = getDescription(); //Setup the leaderboards if (Config.getInstance().getUseMySQL()) { @@ -117,7 +120,8 @@ public class mcMMO extends JavaPlugin { try { Metrics metrics = new Metrics(this); metrics.start(); - } catch (IOException e) { + } + catch (IOException e) { System.out.println("Failed to submit stats."); } } @@ -168,12 +172,13 @@ public class mcMMO extends JavaPlugin { x.save(); } - this.getServer().getScheduler().cancelTasks(this); //This removes our tasks + getServer().getScheduler().cancelTasks(this); //This removes our tasks //Remove other tasks BEFORE starting the Backup, or we just cancel it straight away. try { ZipLibrary.mcMMObackup(); - } catch (IOException e) { + } + catch (IOException e) { getLogger().severe(e.toString()); } @@ -304,7 +309,7 @@ public class mcMMO extends JavaPlugin { } /* - * Boilerplate Custom Config Stuff + * Boilerplate Custom Config Stuff (Treasures) */ private FileConfiguration treasuresConfig = null; @@ -352,7 +357,60 @@ public class mcMMO extends JavaPlugin { treasuresConfig.save(treasuresConfigFile); } catch (IOException ex) { - this.getLogger().severe("Could not save config to " + treasuresConfigFile + ex.toString()); + getLogger().severe("Could not save config to " + treasuresConfigFile + ex.toString()); + } + } + + /* + * Boilerplate Custom Config Stuff (Tools) + */ + + private FileConfiguration toolsConfig = null; + private File toolsConfigFile = null; + + /** + * Reload the Tools.yml file. + */ + public void reloadToolsConfig() { + if (toolsConfigFile == null) { + toolsConfigFile = new File(modDirectory, "tools.yml"); + } + + toolsConfig = YamlConfiguration.loadConfiguration(toolsConfigFile); + InputStream defConfigStream = getResource("tools.yml"); // Look for defaults in the jar + + if (defConfigStream != null) { + YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream); + toolsConfig.setDefaults(defConfig); + } + } + + /** + * Get the Tools config information. + * + * @return the configuration object for tools.yml + */ + public FileConfiguration getToolsConfig() { + if (toolsConfig == null) { + reloadToolsConfig(); + } + + return toolsConfig; + } + + /** + * Save the Tools config informtion. + */ + public void saveToolsConfig() { + if (toolsConfig == null || toolsConfigFile == null) { + return; + } + + try { + toolsConfig.save(toolsConfigFile); + } + catch (IOException ex) { + getLogger().severe("Could not save config to " + toolsConfigFile + ex.toString()); } } } diff --git a/src/main/java/com/gmail/nossr50/skills/misc/Repair.java b/src/main/java/com/gmail/nossr50/skills/misc/Repair.java index 5ba6e5dee..395a546a7 100644 --- a/src/main/java/com/gmail/nossr50/skills/misc/Repair.java +++ b/src/main/java/com/gmail/nossr50/skills/misc/Repair.java @@ -327,7 +327,7 @@ public class Repair { else if (ItemChecks.isHoe(is) || ItemChecks.isSword(is) || is.getType().equals(Material.SHEARS)) { ramt = maxDurability / 2; } - else if (ItemChecks.isAxe(is) || ItemChecks.isMiningPick(is) || ItemChecks.isStringTool(is)) { + else if (ItemChecks.isAxe(is) || ItemChecks.isPickaxe(is) || ItemChecks.isStringTool(is)) { ramt = maxDurability / 3; } else if (ItemChecks.isBoots(is)) { diff --git a/src/main/java/com/gmail/nossr50/util/ItemChecks.java b/src/main/java/com/gmail/nossr50/util/ItemChecks.java index 5997c5aa0..6d4b0f09b 100644 --- a/src/main/java/com/gmail/nossr50/util/ItemChecks.java +++ b/src/main/java/com/gmail/nossr50/util/ItemChecks.java @@ -2,7 +2,11 @@ package com.gmail.nossr50.util; import org.bukkit.inventory.ItemStack; +import com.gmail.nossr50.config.Config; +import com.gmail.nossr50.config.mods.LoadCustomTools; + public class ItemChecks { + private static Config configInstance = Config.getInstance(); /** * Checks if the item is a sword. @@ -20,7 +24,12 @@ public class ItemChecks { return true; default: - return false; + if (configInstance.getToolModsEnabled() && LoadCustomTools.getInstance().customSwordIDs.contains(is.getTypeId())) { + return true; + } + else { + return false; + } } } @@ -40,7 +49,12 @@ public class ItemChecks { return true; default: - return false; + if (configInstance.getToolModsEnabled() && LoadCustomTools.getInstance().customHoeIDs.contains(is.getTypeId())) { + return true; + } + else { + return false; + } } } @@ -49,7 +63,7 @@ public class ItemChecks { * * @param is Item to check * @return true if the item is a shovel, false otherwise - */ + */ public static boolean isShovel(ItemStack is) { switch (is.getType()) { case DIAMOND_SPADE: @@ -60,7 +74,12 @@ public class ItemChecks { return true; default: - return false; + if (configInstance.getToolModsEnabled() && LoadCustomTools.getInstance().customShovelIDs.contains(is.getTypeId())) { + return true; + } + else { + return false; + } } } @@ -80,7 +99,12 @@ public class ItemChecks { return true; default: - return false; + if (configInstance.getToolModsEnabled() && LoadCustomTools.getInstance().customAxeIDs.contains(is.getTypeId())) { + return true; + } + else { + return false; + } } } @@ -90,7 +114,7 @@ public class ItemChecks { * @param is Item to check * @return true if the item is a pickaxe, false otherwise */ - public static boolean isMiningPick(ItemStack is) { + public static boolean isPickaxe(ItemStack is) { switch (is.getType()) { case DIAMOND_PICKAXE: case GOLD_PICKAXE: @@ -100,7 +124,12 @@ public class ItemChecks { return true; default: - return false; + if (configInstance.getToolModsEnabled() && LoadCustomTools.getInstance().customPickaxeIDs.contains(is.getTypeId())) { + return true; + } + else { + return false; + } } } @@ -395,4 +424,3 @@ public class ItemChecks { } } } - \ No newline at end of file diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 35ed5597c..295c5ff32 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -40,6 +40,13 @@ Hardcore: Vampirism: false Vampirism_Stat_Leech_Percentage: 5 +# +# Settings for SMP Mods +### +Mods: + Tool_Mods_Enabled: false + Block_Mods_Enabled: false + # # Settings for mcMMO items ### diff --git a/src/main/resources/tools.yml b/src/main/resources/tools.yml new file mode 100644 index 000000000..d24fbd1fb --- /dev/null +++ b/src/main/resources/tools.yml @@ -0,0 +1,113 @@ +# +# Settings for Axes +### +Axes: + Axe_1: + ID: 999 + XP_Modifer: 1.0 + Repairable: true + Repair_Material_ID: 99 + Repair_Material_Data_Value: 0 + Durability: 9999 + Axe_2: + ID: 999 + XP_Modifer: 1.0 + Repairable: true + Repair_Material_ID: 99 + Repair_Material_Data_Value: 0 + Durability: 9999 + +# +# Settings for Bows +### +Bows: + Bow_1: + ID: 999 + XP_Modifer: 1.0 + Repairable: true + Repair_Material_ID: 99 + Repair_Material_Data_Value: 0 + Durability: 9999 + Bow_2: + ID: 999 + XP_Modifer: 1.0 + Repairable: true + Repair_Material_ID: 99 + Repair_Material_Data_Value: 0 + Durability: 9999 + +# +# Settings for Hoes +### +Hoes: + Hoe_1: + ID: 999 + XP_Modifer: 1.0 + Repairable: true + Repair_Material_ID: 99 + Repair_Material_Data_Value: 0 + Durability: 9999 + Hoe_2: + ID: 999 + XP_Modifer: 1.0 + Repairable: true + Repair_Material_ID: 99 + Repair_Material_Data_Value: 0 + Durability: 9999 + +# +# Settings for Pickaxes +### +Pickaxes: + Pickaxe_1: + ID: 999 + XP_Modifer: 1.0 + Repairable: true + Repair_Material_ID: 99 + Repair_Material_Data_Value: 0 + Durability: 9999 + Pickaxe_2: + ID: 999 + XP_Modifer: 1.0 + Repairable: true + Repair_Material_ID: 99 + Repair_Material_Data_Value: 0 + Durability: 9999 + +# +# Settings for Shovels +### +Shovels: + Shovel_1: + ID: 999 + XP_Modifer: 1.0 + Repairable: true + Repair_Material_ID: 99 + Repair_Material_Data_Value: 0 + Durability: 9999 + Shovel_2: + ID: 999 + XP_Modifer: 1.0 + Repairable: true + Repair_Material_ID: 99 + Repair_Material_Data_Value: 0 + Durability: 9999 + +# +# Settings for Swords +### +Swords: + Sword_1: + ID: 999 + XP_Modifer: 1.0 + Repairable: true + Repair_Material_ID: 99 + Repair_Material_Data_Value: 0 + Durability: 9999 + Sword_2: + ID: 999 + XP_Modifer: 1.0 + Repairable: true + Repair_Material_ID: 99 + Repair_Material_Data_Value: 0 + Durability: 9999