From d9f98b1aa9b2ff97fd840ba51b92e9f84a771b81 Mon Sep 17 00:00:00 2001 From: nossr50 Date: Tue, 29 Dec 2020 12:39:06 -0800 Subject: [PATCH] Fix a bug where books weren't registered + tweaked how treasures were loaded --- .../treasure/FishingTreasureConfig.java | 119 ++++++++++-------- .../nossr50/datatypes/treasure/Rarity.java | 5 + .../nossr50/listeners/PlayerListener.java | 2 +- .../skills/fishing/FishingManager.java | 2 +- 4 files changed, 74 insertions(+), 54 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/config/treasure/FishingTreasureConfig.java b/src/main/java/com/gmail/nossr50/config/treasure/FishingTreasureConfig.java index d71ef7247..69d835541 100755 --- a/src/main/java/com/gmail/nossr50/config/treasure/FishingTreasureConfig.java +++ b/src/main/java/com/gmail/nossr50/config/treasure/FishingTreasureConfig.java @@ -15,6 +15,7 @@ import org.bukkit.inventory.meta.PotionMeta; import org.bukkit.potion.PotionData; import org.bukkit.potion.PotionType; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.*; @@ -94,7 +95,7 @@ public class FishingTreasureConfig extends ConfigLoader { } } - private void loadTreasures(String type) { + private void loadTreasures(@NotNull String type) { boolean isFishing = type.equals("Fishing"); boolean isShake = type.contains("Shake"); @@ -125,9 +126,7 @@ public class FishingTreasureConfig extends ConfigLoader { if (materialName.contains("INVENTORY")) { // Use magic material BEDROCK to know that we're grabbing something from the inventory and not a normal treasure - if (!shakeMap.containsKey(EntityType.PLAYER)) - shakeMap.put(EntityType.PLAYER, new ArrayList<>()); - shakeMap.get(EntityType.PLAYER).add(new ShakeTreasure(new ItemStack(Material.BEDROCK, 1, (byte) 0), 1, getInventoryStealDropChance(), getInventoryStealDropLevel())); + addShakeTreasure(new ShakeTreasure(new ItemStack(Material.BEDROCK, 1, (byte) 0), 1, getInventoryStealDropChance(), getInventoryStealDropLevel()), EntityType.PLAYER); continue; } else { material = Material.matchMaterial(materialName); @@ -145,7 +144,7 @@ public class FishingTreasureConfig extends ConfigLoader { amount = 1; } - if (material != null && material.isBlock() && (data > 127 || data < -128)) { + if (material.isBlock() && (data > 127 || data < -128)) { reason.add("Data of " + treasureName + " is invalid! " + data); } @@ -175,10 +174,14 @@ public class FishingTreasureConfig extends ConfigLoader { Rarity rarity = null; if (isFishing) { - rarity = Rarity.getRarity(config.getString(type + "." + treasureName + ".Rarity")); + String rarityStr = config.getString(type + "." + treasureName + ".Rarity"); - if (rarity == null) { - reason.add("Invalid Rarity for item: " + treasureName); + if(rarityStr != null) { + rarity = Rarity.getRarity(rarityStr); + } else { + mcMMO.p.getLogger().severe("Please edit your config and add a Rarity definition for - " + treasureName); + mcMMO.p.getLogger().severe("Skipping this treasure until rarity is defined - " + treasureName); + continue; } } @@ -202,6 +205,11 @@ public class FishingTreasureConfig extends ConfigLoader { item = new ItemStack(mat, amount, data); PotionMeta itemMeta = (PotionMeta) item.getItemMeta(); + if(itemMeta == null) { + mcMMO.p.getLogger().severe("Item meta when adding potion to fishing treasure was null, contact the mcMMO devs!"); + continue; + } + PotionType potionType = null; try { potionType = PotionType.valueOf(config.getString(type + "." + treasureName + ".PotionData.PotionType", "WATER")); @@ -225,67 +233,74 @@ public class FishingTreasureConfig extends ConfigLoader { } item.setItemMeta(itemMeta); } - } else if (material != null) { - if(material == Material.ENCHANTED_BOOK) { - //If any whitelisted enchants exist we use whitelist-based matching - item = new ItemStack(material, 1); - ItemMeta itemMeta = item.getItemMeta(); + } else if(material == Material.ENCHANTED_BOOK) { + //If any whitelisted enchants exist we use whitelist-based matching + item = new ItemStack(material, 1); + ItemMeta itemMeta = item.getItemMeta(); - List allowedEnchantsList = config.getStringList(type + "." + treasureName + ".Enchantments_Whitelist"); - List disallowedEnchantsList = config.getStringList(type + "." + treasureName + ".Enchantments_Blacklist"); + List allowedEnchantsList = config.getStringList(type + "." + treasureName + ".Enchantments_Whitelist"); + List disallowedEnchantsList = config.getStringList(type + "." + treasureName + ".Enchantments_Blacklist"); - Set blackListedEnchants = new HashSet<>(); - Set whiteListedEnchants = new HashSet<>(); + Set blackListedEnchants = new HashSet<>(); + Set whiteListedEnchants = new HashSet<>(); - matchAndFillSet(disallowedEnchantsList, blackListedEnchants); - matchAndFillSet(allowedEnchantsList, whiteListedEnchants); + matchAndFillSet(disallowedEnchantsList, blackListedEnchants); + matchAndFillSet(allowedEnchantsList, whiteListedEnchants); - if (customName != null && itemMeta != null) { - itemMeta.setDisplayName(ChatColor.translateAlternateColorCodes('&', customName)); - item.setItemMeta(itemMeta); - } - - FishingTreasureBook fishingTreasureBook = new FishingTreasureBook(item, xp, blackListedEnchants, whiteListedEnchants); - //TODO: Add book support for shake - continue; //The code in this whole file is a disaster, ignore this hacky solution :P - } else { - item = new ItemStack(material, amount, data); - - if (customName != null) { - ItemMeta itemMeta = item.getItemMeta(); - itemMeta.setDisplayName(ChatColor.translateAlternateColorCodes('&', customName)); - item.setItemMeta(itemMeta); - } - - if (config.contains(type + "." + treasureName + ".Lore")) { - ItemMeta itemMeta = item.getItemMeta(); - List lore = new ArrayList<>(); - for (String s : config.getStringList(type + "." + treasureName + ".Lore")) { - lore.add(ChatColor.translateAlternateColorCodes('&', s)); - } - itemMeta.setLore(lore); - item.setItemMeta(itemMeta); - } + if (customName != null && itemMeta != null) { + itemMeta.setDisplayName(ChatColor.translateAlternateColorCodes('&', customName)); + item.setItemMeta(itemMeta); } + FishingTreasureBook fishingTreasureBook = new FishingTreasureBook(item, xp, blackListedEnchants, whiteListedEnchants); + addFishingTreasure(rarity, fishingTreasureBook); + //TODO: Add book support for shake + continue; //The code in this whole file is a disaster, ignore this hacky solution :P + } else { + item = new ItemStack(material, amount, data); + + if (customName != null) { + ItemMeta itemMeta = item.getItemMeta(); + itemMeta.setDisplayName(ChatColor.translateAlternateColorCodes('&', customName)); + item.setItemMeta(itemMeta); + } + + if (config.contains(type + "." + treasureName + ".Lore")) { + ItemMeta itemMeta = item.getItemMeta(); + List lore = new ArrayList<>(); + for (String s : config.getStringList(type + "." + treasureName + ".Lore")) { + lore.add(ChatColor.translateAlternateColorCodes('&', s)); + } + itemMeta.setLore(lore); + item.setItemMeta(itemMeta); + } } + if (noErrorsInConfig(reason)) { if (isFishing) { - fishingRewards.get(rarity).add(new FishingTreasure(item, xp)); + addFishingTreasure(rarity, new FishingTreasure(item, xp)); } else if (isShake) { ShakeTreasure shakeTreasure = new ShakeTreasure(item, xp, dropChance, dropLevel); EntityType entityType = EntityType.valueOf(type.substring(6)); - if (!shakeMap.containsKey(entityType)) - shakeMap.put(entityType, new ArrayList<>()); - shakeMap.get(entityType).add(shakeTreasure); + addShakeTreasure(shakeTreasure, entityType); } } } } + private void addShakeTreasure(@NotNull ShakeTreasure shakeTreasure, @NotNull EntityType entityType) { + if (!shakeMap.containsKey(entityType)) + shakeMap.put(entityType, new ArrayList<>()); + shakeMap.get(entityType).add(shakeTreasure); + } + + private void addFishingTreasure(@NotNull Rarity rarity, @NotNull FishingTreasure fishingTreasure) { + fishingRewards.get(rarity).add(fishingTreasure); + } + private boolean hasCustomName(@NotNull String type, @NotNull String treasureName) { return config.contains(type + "." + treasureName + ".Custom_Name"); } @@ -296,7 +311,7 @@ public class FishingTreasureConfig extends ConfigLoader { * @param enchantListStr the users string list of enchantments * @param permissiveList the permissive list of enchantments */ - private void matchAndFillSet(List enchantListStr, Set permissiveList) { + private void matchAndFillSet(@NotNull List enchantListStr, @NotNull Set permissiveList) { if(enchantListStr.isEmpty()) { return; } @@ -359,11 +374,11 @@ public class FishingTreasureConfig extends ConfigLoader { return config.getInt("Shake.PLAYER.INVENTORY.Drop_Level"); } - public double getItemDropRate(int tier, Rarity rarity) { + public double getItemDropRate(int tier, @NotNull Rarity rarity) { return config.getDouble("Item_Drop_Rates.Tier_" + tier + "." + rarity.toString()); } - public double getEnchantmentDropRate(int tier, Rarity rarity) { + public double getEnchantmentDropRate(int tier, @NotNull Rarity rarity) { return config.getDouble("Enchantment_Drop_Rates.Tier_" + tier + "." + rarity.toString()); } } diff --git a/src/main/java/com/gmail/nossr50/datatypes/treasure/Rarity.java b/src/main/java/com/gmail/nossr50/datatypes/treasure/Rarity.java index ff76b670d..4666b32b1 100644 --- a/src/main/java/com/gmail/nossr50/datatypes/treasure/Rarity.java +++ b/src/main/java/com/gmail/nossr50/datatypes/treasure/Rarity.java @@ -1,5 +1,6 @@ package com.gmail.nossr50.datatypes.treasure; +import com.gmail.nossr50.mcMMO; import org.jetbrains.annotations.NotNull; public enum Rarity { @@ -11,6 +12,10 @@ public enum Rarity { COMMON; public static @NotNull Rarity getRarity(@NotNull String string) { + if(string.equalsIgnoreCase("Records")) { + mcMMO.p.getLogger().severe("Entries in fishing treasures have Records set as rarity, however Records was renamed to Mythic. Please update your treasures to read MYTHIC instead of RECORDS for rarity, or delete the config file to regenerate a new one."); + return Rarity.MYTHIC; //People that copy paste their configs will have Records interpretted as Mythic + } try { return valueOf(string); } diff --git a/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java b/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java index d7f727278..e2981c32c 100644 --- a/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java +++ b/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java @@ -419,7 +419,7 @@ public class PlayerListener implements Listener { } } - fishingManager.handleFishing((Item) caught); + fishingManager.processFishing((Item) caught); fishingManager.setFishingTarget(); } return; diff --git a/src/main/java/com/gmail/nossr50/skills/fishing/FishingManager.java b/src/main/java/com/gmail/nossr50/skills/fishing/FishingManager.java index 1211966c3..52c4163ab 100644 --- a/src/main/java/com/gmail/nossr50/skills/fishing/FishingManager.java +++ b/src/main/java/com/gmail/nossr50/skills/fishing/FishingManager.java @@ -380,7 +380,7 @@ public class FishingManager extends SkillManager { * * @param fishingCatch The {@link Item} initially caught */ - public void handleFishing(@NotNull Item fishingCatch) { + public void processFishing(@NotNull Item fishingCatch) { this.fishingCatch = fishingCatch; int fishXp = ExperienceConfig.getInstance().getXp(PrimarySkillType.FISHING, fishingCatch.getItemStack().getType()); int treasureXp = 0;