diff --git a/src/main/java/world/bentobox/bentobox/api/commands/admin/range/AdminRangeDisplayCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/admin/range/AdminRangeDisplayCommand.java index d99445e47..dbdf75342 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/admin/range/AdminRangeDisplayCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/admin/range/AdminRangeDisplayCommand.java @@ -3,7 +3,6 @@ package world.bentobox.bentobox.api.commands.admin.range; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.Objects; import org.bukkit.Bukkit; import org.bukkit.Color; @@ -11,11 +10,10 @@ import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.Particle; -import com.google.common.base.Enums; - import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.database.objects.Island; +import world.bentobox.bentobox.util.Util; /** * @author Poslovitch @@ -26,10 +24,9 @@ public class AdminRangeDisplayCommand extends CompositeCommand { private static final String DISPLAY = "display"; private static final String SHOW = "show"; private static final String HIDE = "hide"; - public static final Particle PARTICLE = Enums.getIfPresent(Particle.class, "RESTONE").toJavaUtil() - .orElse(Enums.getIfPresent(Particle.class, "DUST").orNull()); - private static final Particle PARTICLE2 = Enums.getIfPresent(Particle.class, "VILLAGER_HAPPY").toJavaUtil() - .orElse(Enums.getIfPresent(Particle.class, "HAPPY_VILLAGER").orNull()); + public static final Particle PARTICLE = Util.findFirstMatchingEnum(Particle.class, "REDSTONE", "DUST"); + private static final Particle PARTICLE2 = Util.findFirstMatchingEnum(Particle.class, "VILLAGER_HAPPY", + "HAPPY_VILLAGER"); // Map of users to which ranges must be displayed private final Map displayRanges = new HashMap<>(); diff --git a/src/main/java/world/bentobox/bentobox/listeners/flags/protection/BreedingListener.java b/src/main/java/world/bentobox/bentobox/listeners/flags/protection/BreedingListener.java index 6f07f7a07..6e2dca405 100644 --- a/src/main/java/world/bentobox/bentobox/listeners/flags/protection/BreedingListener.java +++ b/src/main/java/world/bentobox/bentobox/listeners/flags/protection/BreedingListener.java @@ -20,6 +20,7 @@ import com.google.common.base.Enums; import world.bentobox.bentobox.api.flags.FlagListener; import world.bentobox.bentobox.lists.Flags; +import world.bentobox.bentobox.util.Util; /** * Handles breeding protection @@ -40,12 +41,8 @@ public class BreedingListener extends FlagListener { bi.put(EntityType.HORSE, Arrays.asList(Material.GOLDEN_APPLE, Material.GOLDEN_CARROT)); bi.put(EntityType.DONKEY, Arrays.asList(Material.GOLDEN_APPLE, Material.GOLDEN_CARROT)); bi.put(EntityType.COW, Collections.singletonList(Material.WHEAT)); - if (Enums.getIfPresent(EntityType.class, "MUSHROOM_COW").isPresent()) { - bi.put(Enums.getIfPresent(EntityType.class, "MUSHROOM_COW").get(), - Collections.singletonList(Material.WHEAT)); - } else { - bi.put(EntityType.MOOSHROOM, Collections.singletonList(Material.WHEAT)); - } + bi.put(Util.findFirstMatchingEnum(EntityType.class, "MUSHROOM_COW", "MOOSHROOM"), + Collections.singletonList(Material.WHEAT)); bi.put(EntityType.SHEEP, Collections.singletonList(Material.WHEAT)); bi.put(EntityType.PIG, Arrays.asList(Material.CARROT, Material.POTATO, Material.BEETROOT)); bi.put(EntityType.CHICKEN, Arrays.asList(Material.WHEAT_SEEDS, Material.PUMPKIN_SEEDS, Material.MELON_SEEDS, Material.BEETROOT_SEEDS)); diff --git a/src/main/java/world/bentobox/bentobox/listeners/flags/protection/LeashListener.java b/src/main/java/world/bentobox/bentobox/listeners/flags/protection/LeashListener.java index eee18a786..86724f379 100644 --- a/src/main/java/world/bentobox/bentobox/listeners/flags/protection/LeashListener.java +++ b/src/main/java/world/bentobox/bentobox/listeners/flags/protection/LeashListener.java @@ -7,10 +7,9 @@ import org.bukkit.event.entity.PlayerLeashEntityEvent; import org.bukkit.event.hanging.HangingPlaceEvent; import org.bukkit.event.player.PlayerUnleashEntityEvent; -import com.google.common.base.Enums; - import world.bentobox.bentobox.api.flags.FlagListener; import world.bentobox.bentobox.lists.Flags; +import world.bentobox.bentobox.util.Util; /** * @author tastybento @@ -44,9 +43,8 @@ public class LeashListener extends FlagListener { */ @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) public void onPlayerLeashHitch(final HangingPlaceEvent e) { - EntityType LEASH_HITCH = Enums.getIfPresent(EntityType.class, "LEASH_HITCH") - .or(Enums.getIfPresent(EntityType.class, "LEASH_KNOT").orNull()); - if (LEASH_HITCH != null && e.getEntity().getType().equals(LEASH_HITCH)) { + EntityType LEASH_HITCH = Util.findFirstMatchingEnum(EntityType.class, "LEASH_HITCH", "LEASH_KNOT"); + if (e.getEntity().getType().equals(LEASH_HITCH)) { checkIsland(e, e.getPlayer(), e.getEntity().getLocation(), Flags.LEASH); } } diff --git a/src/main/java/world/bentobox/bentobox/listeners/flags/protection/TNTListener.java b/src/main/java/world/bentobox/bentobox/listeners/flags/protection/TNTListener.java index 298d0ab79..c565ed111 100644 --- a/src/main/java/world/bentobox/bentobox/listeners/flags/protection/TNTListener.java +++ b/src/main/java/world/bentobox/bentobox/listeners/flags/protection/TNTListener.java @@ -17,10 +17,9 @@ import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.event.entity.EntityExplodeEvent; import org.bukkit.event.player.PlayerInteractEvent; -import com.google.common.base.Enums; - import world.bentobox.bentobox.api.flags.FlagListener; import world.bentobox.bentobox.lists.Flags; +import world.bentobox.bentobox.util.Util; /** * Protects islands from visitors blowing things up @@ -28,9 +27,9 @@ import world.bentobox.bentobox.lists.Flags; */ public class TNTListener extends FlagListener { - private static final EntityType PRIMED_TNT = Enums.getIfPresent(EntityType.class, "PRIMED_TNT").or(EntityType.TNT); - private static final EntityType MINECART_TNT = Enums.getIfPresent(EntityType.class, "PMINECART_TNT)") - .or(EntityType.TNT_MINECART); + private static final EntityType PRIMED_TNT = Util.findFirstMatchingEnum(EntityType.class, "PRIMED_TNT", "TNT"); + private static final EntityType MINECART_TNT = Util.findFirstMatchingEnum(EntityType.class, "MINECART_TNT", + "TNT_MINECART"); /** * Contains {@link EntityType}s that generates an explosion. diff --git a/src/main/java/world/bentobox/bentobox/util/Util.java b/src/main/java/world/bentobox/bentobox/util/Util.java index 4d833c344..0f5438bd2 100644 --- a/src/main/java/world/bentobox/bentobox/util/Util.java +++ b/src/main/java/world/bentobox/bentobox/util/Util.java @@ -42,6 +42,9 @@ import org.bukkit.util.Vector; import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.Nullable; +import com.google.common.base.Enums; +import com.google.common.base.Optional; + import io.papermc.lib.PaperLib; import io.papermc.lib.features.blockstatesnapshot.BlockStateSnapshotResult; import world.bentobox.bentobox.BentoBox; @@ -801,4 +804,26 @@ public class Util { Util.translateColorCodes(input.replaceAll("[\\\\/:*?\"<>|\s]", "_"))). toLowerCase(); } + + /** + * Attempts to find the first matching enum constant from an array of possible string representations. + * This method sequentially checks each string against the enum constants of the specified enum class + * by normalizing the string values to uppercase before comparison, enhancing the likelihood of a match + * if the enum constants are defined in uppercase. + * + * @param enumClass the Class object of the enum type to be checked against + * @param values an array of string values which are potential matches for the enum constants + * @param the type parameter of the enum + * @return the first matching enum constant if a match is found; otherwise, returns null + * @throws NullPointerException if either {@code enumClass} or {@code values} are null + */ + public static > T findFirstMatchingEnum(Class enumClass, String... values) { + for (String value : values) { + Optional enumConstant = Enums.getIfPresent(enumClass, value.toUpperCase()); + if (enumConstant.isPresent()) { + return enumConstant.get(); + } + } + return null; // Return null or throw an exception if no match is found + } } diff --git a/src/main/java/world/bentobox/bentobox/versions/ServerCompatibility.java b/src/main/java/world/bentobox/bentobox/versions/ServerCompatibility.java index db5f8601d..7bf5f8509 100644 --- a/src/main/java/world/bentobox/bentobox/versions/ServerCompatibility.java +++ b/src/main/java/world/bentobox/bentobox/versions/ServerCompatibility.java @@ -231,7 +231,15 @@ public class ServerCompatibility { /** * @since 2.0.0 */ - V1_20_4(Compatibility.COMPATIBLE); + V1_20_4(Compatibility.COMPATIBLE), + /** + * @since 2.4.0 + */ + V1_20_5(Compatibility.COMPATIBLE), + /** + * @since 2.4.0 + */ + V1_20_6(Compatibility.COMPATIBLE); private final Compatibility compatibility;