From 04007f6dbc137ad2fc61c1cc0b7cc8307eeac720 Mon Sep 17 00:00:00 2001 From: nossr50 Date: Mon, 4 Nov 2024 18:56:38 -0800 Subject: [PATCH] fixed fishing errors Fixes #5108 --- pom.xml | 1 + .../gmail/nossr50/skills/fishing/Fishing.java | 12 +--- .../skills/fishing/FishingManager.java | 4 +- .../nossr50/util/adapter/BiomeAdapter.java | 57 +++++++++---------- 4 files changed, 35 insertions(+), 39 deletions(-) diff --git a/pom.xml b/pom.xml index 13b027483..8ff1c5106 100644 --- a/pom.xml +++ b/pom.xml @@ -13,6 +13,7 @@ + 1.21.3-R0.1-SNAPSHOT 4.3.5-SNAPSHOT UTF-8 diff --git a/src/main/java/com/gmail/nossr50/skills/fishing/Fishing.java b/src/main/java/com/gmail/nossr50/skills/fishing/Fishing.java index 965b9c924..d453edaf0 100644 --- a/src/main/java/com/gmail/nossr50/skills/fishing/Fishing.java +++ b/src/main/java/com/gmail/nossr50/skills/fishing/Fishing.java @@ -3,23 +3,17 @@ package com.gmail.nossr50.skills.fishing; import com.gmail.nossr50.config.treasure.FishingTreasureConfig; import com.gmail.nossr50.datatypes.treasure.ShakeTreasure; import com.gmail.nossr50.util.Misc; -import com.gmail.nossr50.util.adapter.BiomeAdapter; import org.bukkit.Material; -import org.bukkit.block.Biome; import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.LivingEntity; import org.bukkit.inventory.ItemStack; import java.util.HashMap; import java.util.List; -import java.util.Set; public final class Fishing { - protected static final HashMap> ENCHANTABLE_CACHE = new HashMap<>(); - - public static Set masterAnglerBiomes = BiomeAdapter.WATER_BIOMES; - public static Set iceFishingBiomes = BiomeAdapter.ICE_BIOMES; + static final HashMap> ENCHANTABLE_CACHE = new HashMap<>(); private Fishing() {} @@ -30,7 +24,7 @@ public final class Fishing { * Targeted entity * @return possibleDrops List of ItemStack that can be dropped */ - protected static List findPossibleDrops(LivingEntity target) { + static List findPossibleDrops(LivingEntity target) { if (FishingTreasureConfig.getInstance().shakeMap.containsKey(target.getType())) return FishingTreasureConfig.getInstance().shakeMap.get(target.getType()); @@ -44,7 +38,7 @@ public final class Fishing { * List of ItemStack that can be dropped * @return Chosen ItemStack */ - protected static ItemStack chooseDrop(List possibleDrops) { + static ItemStack chooseDrop(List possibleDrops) { int dropProbability = Misc.getRandom().nextInt(100); double cumulatedProbability = 0; 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 4d37adced..3ca634ec6 100644 --- a/src/main/java/com/gmail/nossr50/skills/fishing/FishingManager.java +++ b/src/main/java/com/gmail/nossr50/skills/fishing/FishingManager.java @@ -16,6 +16,7 @@ import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.runnables.skills.MasterAnglerTask; import com.gmail.nossr50.skills.SkillManager; import com.gmail.nossr50.util.*; +import com.gmail.nossr50.util.adapter.BiomeAdapter; import com.gmail.nossr50.util.compat.layers.skills.MasterAnglerCompatibilityLayer; import com.gmail.nossr50.util.player.NotificationManager; import com.gmail.nossr50.util.random.ProbabilityUtil; @@ -173,7 +174,8 @@ public class FishingManager extends SkillManager { } // Make sure this is a body of water, not just a block of ice. - if (!Fishing.iceFishingBiomes.contains(block.getBiome()) && (block.getRelative(BlockFace.DOWN, 3).getType() != Material.WATER)) { + if (!BiomeAdapter.ICE_BIOMES.contains(block.getBiome()) + && (block.getRelative(BlockFace.DOWN, 3).getType() != Material.WATER)) { return false; } diff --git a/src/main/java/com/gmail/nossr50/util/adapter/BiomeAdapter.java b/src/main/java/com/gmail/nossr50/util/adapter/BiomeAdapter.java index cc56e3513..ceeeef0ba 100644 --- a/src/main/java/com/gmail/nossr50/util/adapter/BiomeAdapter.java +++ b/src/main/java/com/gmail/nossr50/util/adapter/BiomeAdapter.java @@ -1,45 +1,44 @@ package com.gmail.nossr50.util.adapter; import org.bukkit.block.Biome; +import org.jetbrains.annotations.NotNull; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.*; +import java.util.function.Function; public class BiomeAdapter { - public static final Set WATER_BIOMES; public static final Set ICE_BIOMES; + static final List knownColdBiomes = Arrays.asList("COLD_OCEAN", "DEEP_COLD_OCEAN", "ICE_SPIKES", + "FROZEN_PEAKS", "FROZEN_OCEAN", "FROZEN_RIVER", "DEEP_FROZEN_OCEAN", "SNOWY_TAIGA", + "OLD_GROWTH_PINE_TAIGA", "OLD_GROWTH_SPRUCE_TAIGA", "TAIGA", "SNOWY_SLOPES", "SNOWY_BEACH"); + static { - final List allBiomes = getAllBiomes(); - final Set waterBiomes = new HashSet<>(); final Set iceBiomes = new HashSet<>(); - for (Biome biome : allBiomes) { - String biomeName = getBiomeName(biome); - if (isWater(biomeName) && !isCold(biomeName)) { - waterBiomes.add(biome); - } else if (isCold(biomeName)) { - iceBiomes.add(biome); - } - } - WATER_BIOMES = Collections.unmodifiableSet(waterBiomes); + knownColdBiomes.stream() + .map(biomeFromString()) + .filter(Objects::nonNull) + .forEach(iceBiomes::add); ICE_BIOMES = Collections.unmodifiableSet(iceBiomes); } - @SuppressWarnings("deprecation") - private static List getAllBiomes() { - return Arrays.asList(Biome.values()); - } - - @SuppressWarnings("deprecation") - private static String getBiomeName(Biome biome) { - return biome.name(); - } - - private static boolean isWater(String name) { - return name.contains("RIVER") || name.contains("OCEAN"); - } - - private static boolean isCold(String name) { - return (name.contains("COLD") || name.contains("ICE") - || name.contains("FROZEN") || name.contains("TAIGA")) && !name.contains("WARM"); + private static @NotNull Function biomeFromString() { + return potentialBiome -> { + try { + Class biomeClass = Class.forName("org.bukkit.block.Biome"); + Method methodValueOf = biomeClass.getMethod("valueOf", String.class); + return methodValueOf.invoke(null, potentialBiome) == null + ? null + : (Biome) methodValueOf.invoke(null, potentialBiome); + } catch (IllegalArgumentException | NullPointerException e) { + return null; + } catch (ClassNotFoundException | NoSuchMethodException | InvocationTargetException + | IllegalAccessException e) { + // Thrown when the method is not found or the class is not found + throw new RuntimeException(e); + } + }; } }