From 6949432cb653a9c7c5bb44b1f906fe0b229010e5 Mon Sep 17 00:00:00 2001 From: tastybento Date: Sun, 28 Apr 2024 17:56:02 -0700 Subject: [PATCH 1/4] 1.20.5 compatibility. Not all backward compatibility done Won't run on 1.20.4 yet without stopping due to enum incompatibility --- pom.xml | 4 +- .../commands/admin/DefaultAdminCommand.java | 2 - .../blueprints/AdminBlueprintCommand.java | 38 ++++-- .../admin/range/AdminRangeDisplayCommand.java | 11 +- .../admin/team/AdminTeamFixCommand.java | 35 ----- .../bentobox/api/panels/PanelItem.java | 6 +- .../bentobox/bentobox/api/user/User.java | 18 +-- .../bentobox/hooks/LangUtilsHook.java | 126 +++++++----------- .../flags/protection/BreedingListener.java | 8 +- .../flags/protection/LeashListener.java | 6 +- .../flags/protection/TNTListener.java | 8 +- .../bentobox/bentobox/util/ItemParser.java | 20 ++- .../api/localization/BentoBoxLocaleTest.java | 2 +- .../bentobox/bentobox/api/user/UserTest.java | 17 +-- .../clicklisteners/GeoMobLimitTabTest.java | 37 ++--- .../flags/protection/TNTListenerTest.java | 2 +- .../ChestDamageListenerTest.java | 4 +- .../customizable/IslandCreationPanelTest.java | 4 - 18 files changed, 162 insertions(+), 186 deletions(-) delete mode 100644 src/main/java/world/bentobox/bentobox/api/commands/admin/team/AdminTeamFixCommand.java diff --git a/pom.xml b/pom.xml index 8aa65b84f..985bfe14e 100644 --- a/pom.xml +++ b/pom.xml @@ -73,7 +73,7 @@ 42.2.18 5.0.1 - 1.20.4-R0.1-SNAPSHOT + 1.20.5-R0.1-SNAPSHOT 1.20.4-R0.1-SNAPSHOT @@ -88,7 +88,7 @@ -LOCAL - 2.3.0 + 2.4.0 bentobox-world https://sonarcloud.io ${project.basedir}/lib diff --git a/src/main/java/world/bentobox/bentobox/api/commands/admin/DefaultAdminCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/admin/DefaultAdminCommand.java index dbcd126de..320191518 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/admin/DefaultAdminCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/admin/DefaultAdminCommand.java @@ -12,7 +12,6 @@ import world.bentobox.bentobox.api.commands.admin.resets.AdminResetsCommand; import world.bentobox.bentobox.api.commands.admin.team.AdminTeamAddCommand; import world.bentobox.bentobox.api.commands.admin.team.AdminTeamCommand; import world.bentobox.bentobox.api.commands.admin.team.AdminTeamDisbandCommand; -import world.bentobox.bentobox.api.commands.admin.team.AdminTeamFixCommand; import world.bentobox.bentobox.api.commands.admin.team.AdminTeamKickCommand; import world.bentobox.bentobox.api.commands.admin.team.AdminTeamSetownerCommand; import world.bentobox.bentobox.api.localization.TextVariables; @@ -63,7 +62,6 @@ public abstract class DefaultAdminCommand extends CompositeCommand { new AdminTeamKickCommand(this); new AdminTeamDisbandCommand(this); new AdminTeamSetownerCommand(this); - new AdminTeamFixCommand(this); // Blueprints new AdminBlueprintCommand(this); // Register/unregister islands diff --git a/src/main/java/world/bentobox/bentobox/api/commands/admin/blueprints/AdminBlueprintCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/admin/blueprints/AdminBlueprintCommand.java index 8246d157d..9308301c5 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/admin/blueprints/AdminBlueprintCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/admin/blueprints/AdminBlueprintCommand.java @@ -12,6 +12,7 @@ import org.bukkit.Particle; import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.commands.ConfirmableCommand; +import world.bentobox.bentobox.api.commands.admin.range.AdminRangeDisplayCommand; import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.blueprints.BlueprintClipboard; import world.bentobox.bentobox.managers.BlueprintsManager; @@ -23,7 +24,6 @@ public class AdminBlueprintCommand extends ConfirmableCommand { // Map containing selection cuboid display tasks private Map displayClipboards; - private static final Particle PARTICLE = Particle.REDSTONE; private static final Particle.DustOptions PARTICLE_DUST_OPTIONS = new Particle.DustOptions(Color.RED, 1.0F); public AdminBlueprintCommand(CompositeCommand parent) { @@ -99,26 +99,38 @@ public class AdminBlueprintCommand extends ConfirmableCommand { // Drawing x-axes for (int x = minX; x <= maxX; x++) { - user.spawnParticle(PARTICLE, PARTICLE_DUST_OPTIONS, x + 0.5, minY + 0.5, minZ + 0.5); - user.spawnParticle(PARTICLE, PARTICLE_DUST_OPTIONS, x + 0.5, maxY + 0.5, minZ + 0.5); - user.spawnParticle(PARTICLE, PARTICLE_DUST_OPTIONS, x + 0.5, minY + 0.5, maxZ + 0.5); - user.spawnParticle(PARTICLE, PARTICLE_DUST_OPTIONS, x + 0.5, maxY + 0.5, maxZ + 0.5); + user.spawnParticle(AdminRangeDisplayCommand.PARTICLE, PARTICLE_DUST_OPTIONS, x + 0.5, minY + 0.5, + minZ + 0.5); + user.spawnParticle(AdminRangeDisplayCommand.PARTICLE, PARTICLE_DUST_OPTIONS, x + 0.5, maxY + 0.5, + minZ + 0.5); + user.spawnParticle(AdminRangeDisplayCommand.PARTICLE, PARTICLE_DUST_OPTIONS, x + 0.5, minY + 0.5, + maxZ + 0.5); + user.spawnParticle(AdminRangeDisplayCommand.PARTICLE, PARTICLE_DUST_OPTIONS, x + 0.5, maxY + 0.5, + maxZ + 0.5); } // Drawing y-axes for (int y = minY; y <= maxY; y++) { - user.spawnParticle(PARTICLE, PARTICLE_DUST_OPTIONS, minX + 0.5, y + 0.5, minZ + 0.5); - user.spawnParticle(PARTICLE, PARTICLE_DUST_OPTIONS, maxX + 0.5, y + 0.5, minZ + 0.5); - user.spawnParticle(PARTICLE, PARTICLE_DUST_OPTIONS, minX + 0.5, y + 0.5, maxZ + 0.5); - user.spawnParticle(PARTICLE, PARTICLE_DUST_OPTIONS, maxX + 0.5, y + 0.5, maxZ + 0.5); + user.spawnParticle(AdminRangeDisplayCommand.PARTICLE, PARTICLE_DUST_OPTIONS, minX + 0.5, y + 0.5, + minZ + 0.5); + user.spawnParticle(AdminRangeDisplayCommand.PARTICLE, PARTICLE_DUST_OPTIONS, maxX + 0.5, y + 0.5, + minZ + 0.5); + user.spawnParticle(AdminRangeDisplayCommand.PARTICLE, PARTICLE_DUST_OPTIONS, minX + 0.5, y + 0.5, + maxZ + 0.5); + user.spawnParticle(AdminRangeDisplayCommand.PARTICLE, PARTICLE_DUST_OPTIONS, maxX + 0.5, y + 0.5, + maxZ + 0.5); } // Drawing z-axes for (int z = minZ; z <= maxZ; z++) { - user.spawnParticle(PARTICLE, PARTICLE_DUST_OPTIONS, minX + 0.5, minY + 0.5, z + 0.5); - user.spawnParticle(PARTICLE, PARTICLE_DUST_OPTIONS, maxX + 0.5, minY + 0.5, z + 0.5); - user.spawnParticle(PARTICLE, PARTICLE_DUST_OPTIONS, minX + 0.5, maxY + 0.5, z + 0.5); - user.spawnParticle(PARTICLE, PARTICLE_DUST_OPTIONS, maxX + 0.5, maxY + 0.5, z + 0.5); + user.spawnParticle(AdminRangeDisplayCommand.PARTICLE, PARTICLE_DUST_OPTIONS, minX + 0.5, minY + 0.5, + z + 0.5); + user.spawnParticle(AdminRangeDisplayCommand.PARTICLE, PARTICLE_DUST_OPTIONS, maxX + 0.5, minY + 0.5, + z + 0.5); + user.spawnParticle(AdminRangeDisplayCommand.PARTICLE, PARTICLE_DUST_OPTIONS, minX + 0.5, maxY + 0.5, + z + 0.5); + user.spawnParticle(AdminRangeDisplayCommand.PARTICLE, PARTICLE_DUST_OPTIONS, maxX + 0.5, maxY + 0.5, + z + 0.5); } } 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 f79049829..d99445e47 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,6 +3,7 @@ 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; @@ -10,6 +11,8 @@ 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; @@ -23,6 +26,10 @@ 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()); // Map of users to which ranges must be displayed private final Map displayRanges = new HashMap<>(); @@ -76,11 +83,11 @@ public class AdminRangeDisplayCommand extends CompositeCommand { // Draw the default protected area if island protected zone is different if (island.getProtectionRange() != getPlugin().getIWM().getIslandProtectionRange(getWorld())) { - drawZone(user, Particle.VILLAGER_HAPPY, null, island, getPlugin().getIWM().getIslandProtectionRange(getWorld())); + drawZone(user, PARTICLE2, null, island, getPlugin().getIWM().getIslandProtectionRange(getWorld())); } // Draw the island area - drawZone(user, Particle.REDSTONE, new Particle.DustOptions(Color.GRAY, 1.0F), island, island.getRange()); + drawZone(user, PARTICLE, new Particle.DustOptions(Color.GRAY, 1.0F), island, island.getRange()); }); }, 20, 30)); } diff --git a/src/main/java/world/bentobox/bentobox/api/commands/admin/team/AdminTeamFixCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/admin/team/AdminTeamFixCommand.java deleted file mode 100644 index b739b2a24..000000000 --- a/src/main/java/world/bentobox/bentobox/api/commands/admin/team/AdminTeamFixCommand.java +++ /dev/null @@ -1,35 +0,0 @@ -package world.bentobox.bentobox.api.commands.admin.team; - -import java.util.List; - -import world.bentobox.bentobox.api.commands.CompositeCommand; -import world.bentobox.bentobox.api.user.User; - -public class AdminTeamFixCommand extends CompositeCommand { - - - public AdminTeamFixCommand(CompositeCommand parent) { - super(parent, "fix"); - } - - @Override - public void setup() { - setPermission("mod.team.fix"); - setDescription("commands.admin.team.fix.description"); - } - - @Override - public boolean canExecute(User user, String label, List args) { - // If args are not right, show help - if (!args.isEmpty()) { - showHelp(this, user); - return false; - } - return true; - } - @Override - public boolean execute(User user, String label, List args) { - getIslands().checkTeams(user, getWorld()); - return true; - } -} diff --git a/src/main/java/world/bentobox/bentobox/api/panels/PanelItem.java b/src/main/java/world/bentobox/bentobox/api/panels/PanelItem.java index 219beb636..41d5bab66 100644 --- a/src/main/java/world/bentobox/bentobox/api/panels/PanelItem.java +++ b/src/main/java/world/bentobox/bentobox/api/panels/PanelItem.java @@ -48,7 +48,6 @@ public class PanelItem { meta.addItemFlags(ItemFlag.HIDE_DESTROYS); meta.addItemFlags(ItemFlag.HIDE_PLACED_ON); meta.addItemFlags(ItemFlag.HIDE_ENCHANTS); - meta.addItemFlags(ItemFlag.HIDE_POTION_EFFECTS); icon.setItemMeta(meta); } @@ -89,7 +88,6 @@ public class PanelItem { this.name = name; if (meta != null) { meta.setDisplayName(name); - meta.setLocalizedName(name); //Localized name cannot be overridden by the player using an anvils icon.setItemMeta(meta); } } @@ -135,9 +133,9 @@ public class PanelItem { } if (meta != null) { if (glow) { - meta.addEnchant(Enchantment.ARROW_DAMAGE, 0, glow); + meta.addEnchant(Enchantment.POWER, 0, glow); } else { - meta.removeEnchant(Enchantment.ARROW_DAMAGE); + meta.removeEnchant(Enchantment.POWER); } icon.setItemMeta(meta); diff --git a/src/main/java/world/bentobox/bentobox/api/user/User.java b/src/main/java/world/bentobox/bentobox/api/user/User.java index 4e20a172c..d91d402fe 100644 --- a/src/main/java/world/bentobox/bentobox/api/user/User.java +++ b/src/main/java/world/bentobox/bentobox/api/user/User.java @@ -14,6 +14,7 @@ import java.util.UUID; import org.apache.commons.lang.math.NumberUtils; import org.bukkit.Bukkit; import org.bukkit.ChatColor; +import org.bukkit.Color; import org.bukkit.GameMode; import org.bukkit.Location; import org.bukkit.OfflinePlayer; @@ -33,6 +34,8 @@ import org.bukkit.util.Vector; import org.eclipse.jdt.annotation.NonNull; import org.eclipse.jdt.annotation.Nullable; +import com.google.common.base.Enums; + import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.addons.Addon; import world.bentobox.bentobox.api.events.OfflineMessageEvent; @@ -62,19 +65,18 @@ public class User implements MetaDataAble { private static final Map> VALIDATION_CHECK; static { Map> v = new EnumMap<>(Particle.class); - v.put(Particle.REDSTONE, Particle.DustOptions.class); - v.put(Particle.ITEM_CRACK, ItemStack.class); - v.put(Particle.BLOCK_CRACK, BlockData.class); - v.put(Particle.BLOCK_DUST, BlockData.class); + v.put(Particle.DUST, Particle.DustOptions.class); + v.put(Particle.ITEM, ItemStack.class); + v.put(Particle.ITEM_COBWEB, ItemStack.class); v.put(Particle.FALLING_DUST, BlockData.class); + v.put(Particle.BLOCK, BlockData.class); v.put(Particle.BLOCK_MARKER, BlockData.class); v.put(Particle.DUST_COLOR_TRANSITION, DustTransition.class); + v.put(Particle.DUST_PILLAR, BlockData.class); v.put(Particle.VIBRATION, Vibration.class); v.put(Particle.SCULK_CHARGE, Float.class); v.put(Particle.SHRIEK, Integer.class); - v.put(Particle.LEGACY_BLOCK_CRACK, BlockData.class); - v.put(Particle.LEGACY_BLOCK_DUST, BlockData.class); - v.put(Particle.LEGACY_FALLING_DUST, BlockData.class); + v.put(Particle.ENTITY_EFFECT, Color.class); VALIDATION_CHECK = Collections.unmodifiableMap(v); } @@ -730,7 +732,7 @@ public class User implements MetaDataAble { // Check if this particle is beyond the viewing distance of the server if (this.player != null && this.player.getLocation().toVector().distanceSquared(new Vector(x, y, z)) < (Bukkit.getServer().getViewDistance() * 256 * Bukkit.getServer().getViewDistance())) { - if (particle.equals(Particle.REDSTONE)) { + if (particle.equals(Particle.DUST)) { player.spawnParticle(particle, x, y, z, 1, 0, 0, 0, 1, dustOptions); } else if (dustOptions != null) { player.spawnParticle(particle, x, y, z, 1, dustOptions); diff --git a/src/main/java/world/bentobox/bentobox/hooks/LangUtilsHook.java b/src/main/java/world/bentobox/bentobox/hooks/LangUtilsHook.java index 77809ce18..d1161d574 100644 --- a/src/main/java/world/bentobox/bentobox/hooks/LangUtilsHook.java +++ b/src/main/java/world/bentobox/bentobox/hooks/LangUtilsHook.java @@ -1,8 +1,10 @@ package world.bentobox.bentobox.hooks; +import java.util.List; import java.util.Locale; import java.util.Map.Entry; import java.util.logging.Logger; +import java.util.stream.Collectors; import org.bukkit.DyeColor; import org.bukkit.Material; @@ -264,29 +266,57 @@ public class LangUtilsHook extends Hook { if (hooked) { return LanguageHelper.getPotionName(potionType, getUserLocale(user)); } + return generalPotionName(potionType); + } + + private static String generalPotionName(PotionType potionType) { return switch (potionType) { - case UNCRAFTABLE -> "Uncraftable Potion"; case WATER -> "Water Bottle"; case MUNDANE -> "Mundane Potion"; case THICK -> "Thick Potion"; case AWKWARD -> "Awkward Potion"; case NIGHT_VISION -> "Potion of Night Vision"; case INVISIBILITY -> "Potion of Invisibility"; - case JUMP -> "Potion of Leaping"; case FIRE_RESISTANCE -> "Potion of Fire Resistance"; - case SPEED -> "Potion of Swiftness"; case SLOWNESS -> "Potion of Slowness"; case WATER_BREATHING -> "Potion of Water Breathing"; - case INSTANT_HEAL -> "Potion of Healing"; - case INSTANT_DAMAGE -> "Potion of Harming"; case POISON -> "Potion of Poison"; - case REGEN -> "Potion of Regeneration"; case STRENGTH -> "Potion of Strength"; case WEAKNESS -> "Potion of Weakness"; case LUCK -> "Potion of Luck"; case TURTLE_MASTER -> "Potion of the Turtle Master"; case SLOW_FALLING -> "Potion of Slow Falling"; - default -> "Unknown Potion"; + case HARMING -> "Potion of Harming"; + case HEALING -> "Potion of Healing"; + case INFESTED -> "Infested Potion"; + case LEAPING -> "Potion of Leaping"; + case LONG_FIRE_RESISTANCE -> "Potion of Long Fire Resistance"; + case LONG_INVISIBILITY -> "Potion of Long Invisibility"; + case LONG_NIGHT_VISION -> "Potion of Long Night Vision"; + case LONG_POISON -> "Potion of Long Poison"; + case LONG_REGENERATION -> "Potion of Long Regeneration"; + case LONG_SLOWNESS -> "Potion of Long Slowness"; + case LONG_SLOW_FALLING -> "Potion of Long Slow Falling"; + case LONG_STRENGTH -> "Potion of Long Strength"; + case LONG_SWIFTNESS -> "Potion of Long Swiftness"; + case LONG_TURTLE_MASTER -> "Potion of Long Turtle Master"; + case LONG_WATER_BREATHING -> "Potion of Long Water Breathing"; + case LONG_WEAKNESS -> "Potion of Long Weakness"; + case OOZING -> "Potion of Oozing"; + case REGENERATION -> "Potion of Regeneration"; + case STRONG_HARMING -> "Potion of Strong Harming"; + case STRONG_HEALING -> "Potion of Strong Healing"; + case STRONG_LEAPING -> "Potion of Strong Leaping"; + case STRONG_POISON -> "Potion of Strong Poison"; + case STRONG_REGENERATION -> "Potion of Strong Regeneration"; + case STRONG_SLOWNESS -> "Potion of Strong Slowness"; + case STRONG_STRENGTH -> "Potion of Strong Strength"; + case STRONG_SWIFTNESS -> "Potion of Swiftness"; + case STRONG_TURTLE_MASTER -> "Potion of Strong Turtle Master"; + case SWIFTNESS -> "Potion of Swiftness"; + case WEAVING -> "Potion of Weaving"; + case WIND_CHARGED -> "Potion of Wind Charged"; + default -> "Potion (Unknown)"; }; } @@ -302,30 +332,7 @@ public class LangUtilsHook extends Hook { if (hooked) { return LanguageHelper.getSplashPotionName(potionType, getUserLocale(user)); } - return switch (potionType) { - case UNCRAFTABLE -> "Splash Uncraftable Potion"; - case WATER -> "Splash Water Bottle"; - case MUNDANE -> "Mundane Splash Potion"; - case THICK -> "Thick Splash Potion"; - case AWKWARD -> "Awkward Splash Potion"; - case NIGHT_VISION -> "Splash Potion of Night Vision"; - case INVISIBILITY -> "Splash Potion of Invisibility"; - case JUMP -> "Splash Potion of Leaping"; - case FIRE_RESISTANCE -> "Splash Potion of Fire Resistance"; - case SPEED -> "Splash Potion of Swiftness"; - case SLOWNESS -> "Splash Potion of Slowness"; - case WATER_BREATHING -> "Splash Potion of Water Breathing"; - case INSTANT_HEAL -> "Splash Potion of Healing"; - case INSTANT_DAMAGE -> "Splash Potion of Harming"; - case POISON -> "Splash Potion of Poison"; - case REGEN -> "Splash Potion of Regeneration"; - case STRENGTH -> "Splash Potion of Strength"; - case WEAKNESS -> "Splash Potion of Weakness"; - case LUCK -> "Splash Potion of Luck"; - case TURTLE_MASTER -> "Splash Potion of the Turtle Master"; - case SLOW_FALLING -> "Splash Potion of Slow Falling"; - default -> "Unknown Splash Potion"; - }; + return "Splash" + generalPotionName(potionType); } /** @@ -339,30 +346,7 @@ public class LangUtilsHook extends Hook { if (hooked) { return LanguageHelper.getLingeringPotionName(potionType, getUserLocale(user)); } - return switch (potionType) { - case UNCRAFTABLE -> "Lingering Uncraftable Potion"; - case WATER -> "Lingering Water Bottle"; - case MUNDANE -> "Mundane Lingering Potion"; - case THICK -> "Thick Lingering Potion"; - case AWKWARD -> "Awkward Lingering Potion"; - case NIGHT_VISION -> "Lingering Potion of Night Vision"; - case INVISIBILITY -> "Lingering Potion of Invisibility"; - case JUMP -> "Lingering Potion of Leaping"; - case FIRE_RESISTANCE -> "Lingering Potion of Fire Resistance"; - case SPEED -> "Lingering Potion of Swiftness"; - case SLOWNESS -> "Lingering Potion of Slowness"; - case WATER_BREATHING -> "Lingering Potion of Water Breathing"; - case INSTANT_HEAL -> "Lingering Potion of Healing"; - case INSTANT_DAMAGE -> "Lingering Potion of Harming"; - case POISON -> "Lingering Potion of Poison"; - case REGEN -> "Lingering Potion of Regeneration"; - case STRENGTH -> "Lingering Potion of Strength"; - case WEAKNESS -> "Lingering Potion of Weakness"; - case LUCK -> "Lingering Potion of Luck"; - case TURTLE_MASTER -> "Lingering Potion of the Turtle Master"; - case SLOW_FALLING -> "Lingering Potion of Slow Falling"; - default -> "Unknown Lingering Potion"; - }; + return "Lingering" + generalPotionName(potionType); } /** @@ -376,28 +360,7 @@ public class LangUtilsHook extends Hook { if (hooked) { return LanguageHelper.getTippedArrowName(potionType, getUserLocale(user)); } - return switch (potionType) { - case UNCRAFTABLE -> "Uncraftable Tipped Arrow"; - case WATER -> "Arrow of Splashing"; - case MUNDANE, THICK, AWKWARD -> "Tipped Arrow"; - case NIGHT_VISION -> "Arrow of Night Vision"; - case INVISIBILITY -> "Arrow of Invisibility"; - case JUMP -> "Arrow of Leaping"; - case FIRE_RESISTANCE -> "Arrow of Fire Resistance"; - case SPEED -> "Arrow of Swiftness"; - case SLOWNESS -> "Arrow of Slowness"; - case WATER_BREATHING -> "Arrow of Water Breathing"; - case INSTANT_HEAL -> "Arrow of Healing"; - case INSTANT_DAMAGE -> "Arrow of Harming"; - case POISON -> "Arrow of Poison"; - case REGEN -> "Arrow of Regeneration"; - case STRENGTH -> "Arrow of Strength"; - case WEAKNESS -> "Arrow of Weakness"; - case LUCK -> "Arrow of Luck"; - case TURTLE_MASTER -> "Arrow of the Turtle Master"; - case SLOW_FALLING -> "Arrow of Slow Falling"; - default -> "Unknown Arrow"; - }; + return generalPotionName(potionType).replaceAll("Potion", "Arrow"); } /** @@ -413,11 +376,12 @@ public class LangUtilsHook extends Hook { if (hooked) { return LanguageHelper.getPotionBaseEffectName(potionType, getUserLocale(user)); } - PotionEffectType effectType = potionType.getEffectType(); - if (effectType == null) { + List effects = potionType.getPotionEffects(); + if (effects.isEmpty()) { return "No Effects"; } - return Util.prettifyText(effectType.getName()); + return effects.stream().map(effect -> Util.prettifyText(effect.getType().getKey().getKey())) + .collect(Collectors.joining(", ")); } /** @@ -430,7 +394,7 @@ public class LangUtilsHook extends Hook { public static String getPotionEffectName(PotionEffectType effectType, User user) { return hooked ? LanguageHelper.getPotionEffectName(effectType, getUserLocale(user)) - : Util.prettifyText(effectType.getName()); + : Util.prettifyText(effectType.getKey().getKey()); } /** 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 6d61d761a..6f07f7a07 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 @@ -28,7 +28,6 @@ import world.bentobox.bentobox.lists.Flags; * */ public class BreedingListener extends FlagListener { - /** * A list of items that cause breeding if a player has them in their hand and they click an animal * This list may need to be extended with future versions of Minecraft. @@ -41,7 +40,12 @@ 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)); - bi.put(EntityType.MUSHROOM_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(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 c07366d56..eee18a786 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,6 +7,8 @@ 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; @@ -42,7 +44,9 @@ public class LeashListener extends FlagListener { */ @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) public void onPlayerLeashHitch(final HangingPlaceEvent e) { - if (e.getEntity().getType().equals(EntityType.LEASH_HITCH)) { + 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)) { 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 1543de382..298d0ab79 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,6 +17,8 @@ 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; @@ -26,11 +28,15 @@ 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); + /** * Contains {@link EntityType}s that generates an explosion. * @since 1.5.0 */ - private static final List TNT_TYPES = List.of(EntityType.PRIMED_TNT, EntityType.MINECART_TNT); + private static final List TNT_TYPES = List.of(PRIMED_TNT, MINECART_TNT); /** * Contains {@link Material}s that can be used to prime a TNT. diff --git a/src/main/java/world/bentobox/bentobox/util/ItemParser.java b/src/main/java/world/bentobox/bentobox/util/ItemParser.java index c315aa897..89115c47e 100644 --- a/src/main/java/world/bentobox/bentobox/util/ItemParser.java +++ b/src/main/java/world/bentobox/bentobox/util/ItemParser.java @@ -145,6 +145,7 @@ public class ItemParser { ItemParser.setCustomModelData(returnValue, customModelData); } } catch (Exception exception) { + exception.printStackTrace(); BentoBox.getInstance().logError("Could not parse item " + text + " " + exception.getLocalizedMessage()); returnValue = defaultItemStack; } @@ -261,7 +262,8 @@ public class ItemParser { boolean isUpgraded = !part[2].isEmpty() && !part[2].equalsIgnoreCase("1"); boolean isExtended = part[3].equalsIgnoreCase("EXTENDED"); PotionData data = new PotionData(type, isExtended, isUpgraded); - potionMeta.setBasePotionData(data); + // TODO: Set extended and u[graded settings. + potionMeta.setBasePotionType(type); result.setItemMeta(potionMeta); result.setAmount(Integer.parseInt(part[5])); return result; @@ -285,7 +287,7 @@ public class ItemParser { private static ItemStack parsePotion(String[] part) { if (part.length == 6) { BentoBox.getInstance().logWarning("The old potion parsing detected for " + part[0] + - ". Please update your configs, as SPIGOT changed potion types."); + ". Please update your configs, as SPIGOT changed potion types."); return parsePotionOld(part); } @@ -314,7 +316,7 @@ public class ItemParser { if (result.getItemMeta() instanceof PotionMeta meta) { PotionType potionType = Enums.getIfPresent(PotionType.class, part[1].toUpperCase(Locale.ENGLISH)). - or(PotionType.WATER); + or(PotionType.WATER); meta.setBasePotionType(potionType); result.setItemMeta(meta); } @@ -340,7 +342,17 @@ public class ItemParser { BannerMeta meta = (BannerMeta) result.getItemMeta(); if (meta != null) { for (int i = 2; i < part.length; i += 2) { - meta.addPattern(new Pattern(DyeColor.valueOf(part[i + 1]), PatternType.valueOf(part[i]))); + PatternType pt = Enums.getIfPresent(PatternType.class, part[i]).orNull(); + if (pt == null) { + // Try to convert old to new + if (part[i].trim().equals("STRIPE_SMALL")) { + pt = PatternType.SMALL_STRIPES; + } + } + DyeColor dc = Enums.getIfPresent(DyeColor.class, part[i + 1]).orNull(); + if (pt != null && dc != null) { + meta.addPattern(new Pattern(dc, pt)); + } } result.setItemMeta(meta); } diff --git a/src/test/java/world/bentobox/bentobox/api/localization/BentoBoxLocaleTest.java b/src/test/java/world/bentobox/bentobox/api/localization/BentoBoxLocaleTest.java index a195ebecd..2404d66b9 100644 --- a/src/test/java/world/bentobox/bentobox/api/localization/BentoBoxLocaleTest.java +++ b/src/test/java/world/bentobox/bentobox/api/localization/BentoBoxLocaleTest.java @@ -55,7 +55,7 @@ public class BentoBoxLocaleTest { Locale locale = Locale.US; YamlConfiguration config = new YamlConfiguration(); - config.set("meta.banner", "WHITE_BANNER:1:STRIPE_SMALL:RED:SQUARE_TOP_RIGHT:CYAN:SQUARE_TOP_RIGHT:BLUE"); + config.set("meta.banner", "WHITE_BANNER:1:SMALL_STRIPES:RED:SQUARE_TOP_RIGHT:CYAN:SQUARE_TOP_RIGHT:BLUE"); List authors = new ArrayList<>(); authors.add("tastybento"); authors.add("tastybento2"); diff --git a/src/test/java/world/bentobox/bentobox/api/user/UserTest.java b/src/test/java/world/bentobox/bentobox/api/user/UserTest.java index 4a487410d..ba8eb6cf7 100644 --- a/src/test/java/world/bentobox/bentobox/api/user/UserTest.java +++ b/src/test/java/world/bentobox/bentobox/api/user/UserTest.java @@ -842,9 +842,10 @@ public class UserTest { User.clearUsers(); User p = User.getInstance(player); try { - p.spawnParticle(Particle.REDSTONE, 4, 0.0d, 0.0d, 0.0d); + p.spawnParticle(Particle.DUST, 4, 0.0d, 0.0d, 0.0d); } catch (Exception e) { - assertEquals("A non-null DustOptions must be provided when using Particle.REDSTONE as particle.", e.getMessage()); + assertEquals("A non-null DustOptions must be provided when using Particle.DUST as particle.", + e.getMessage()); } } @@ -878,8 +879,8 @@ public class UserTest { User p = User.getInstance(player); DustOptions dust = mock(DustOptions.class); - p.spawnParticle(Particle.REDSTONE, dust, 0.0d, 0.0d, 0.0d); - verify(player).spawnParticle(Particle.REDSTONE, 0.0d, 0.0d, 0.0d, 1, 0, 0, 0, 1, dust); + p.spawnParticle(Particle.DUST, dust, 0.0d, 0.0d, 0.0d); + verify(player).spawnParticle(Particle.DUST, 0.0d, 0.0d, 0.0d, 1, 0, 0, 0, 1, dust); } @@ -896,8 +897,8 @@ public class UserTest { User p = User.getInstance(player); DustOptions dust = mock(DustOptions.class); - p.spawnParticle(Particle.REDSTONE, dust, 0.0d, 0.0d, 0.0d); - verify(player).spawnParticle(Particle.REDSTONE, 0.0d, 0.0d, 0.0d, 1, 0, 0, 0, 1, dust); + p.spawnParticle(Particle.DUST, dust, 0.0d, 0.0d, 0.0d); + verify(player).spawnParticle(Particle.DUST, 0.0d, 0.0d, 0.0d, 1, 0, 0, 0, 1, dust); } @@ -914,8 +915,8 @@ public class UserTest { User p = User.getInstance(player); DustOptions dust = mock(DustOptions.class); - p.spawnParticle(Particle.REDSTONE, dust, 0, 0, 0); - verify(player).spawnParticle(Particle.REDSTONE, 0.0d, 0.0d, 0.0d, 1, 0, 0, 0, 1, dust); + p.spawnParticle(Particle.DUST, dust, 0, 0, 0); + verify(player).spawnParticle(Particle.DUST, 0.0d, 0.0d, 0.0d, 1, 0, 0, 0, 1, dust); } diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/clicklisteners/GeoMobLimitTabTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/clicklisteners/GeoMobLimitTabTest.java index 4ecccedc1..2864222cc 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/clicklisteners/GeoMobLimitTabTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/clicklisteners/GeoMobLimitTabTest.java @@ -76,7 +76,9 @@ public class GeoMobLimitTabTest { // IWM when(plugin.getIWM()).thenReturn(iwm); when(iwm.getAddon(any())).thenReturn(Optional.of(gma)); + // Make list of the first 4 creatures on the list - it's alphabetical and follows the list of Living Entities list = new ArrayList<>(); + list.add("ARMADILLO"); list.add("AXOLOTL"); list.add("BAT"); list.add("COW"); @@ -102,24 +104,27 @@ public class GeoMobLimitTabTest { @Test public void testOnClick() { GeoMobLimitTab tab = new GeoMobLimitTab(user, EntityLimitTabType.GEO_LIMIT, world); - // AXOLOTL, BAT, and COW in list + // ARMADILLO, AXOLOTL, BAT, and COW in list + assertEquals(4, list.size()); + assertEquals("COW", list.get(3)); + assertEquals("BAT", list.get(2)); + assertEquals("AXOLOTL", list.get(1)); + assertEquals("ARMADILLO", list.get(0)); + + // Click on ARMADILLO + tab.onClick(panel, user, ClickType.LEFT, 10); + list.forEach(System.out::println); assertEquals(3, list.size()); assertEquals("COW", list.get(2)); assertEquals("BAT", list.get(1)); assertEquals("AXOLOTL", list.get(0)); - - // Click on AXOLOTL + // Click on ARMADILLO again to have it added to the end of the list tab.onClick(panel, user, ClickType.LEFT, 10); - list.forEach(System.out::println); - assertEquals(2, list.size()); - assertEquals("COW", list.get(1)); - assertEquals("BAT", list.get(0)); - // Click on AXOLOTL again to have it added - tab.onClick(panel, user, ClickType.LEFT, 10); - assertEquals(3, list.size()); - assertEquals("BAT", list.get(0)); - assertEquals("COW", list.get(1)); - assertEquals("AXOLOTL", list.get(2)); + assertEquals(4, list.size()); + assertEquals("COW", list.get(2)); + assertEquals("BAT", list.get(1)); + assertEquals("AXOLOTL", list.get(0)); + assertEquals("ARMADILLO", list.get(3)); verify(gma, times(2)).saveWorldSettings(); } @@ -165,7 +170,8 @@ public class GeoMobLimitTabTest { List<@Nullable PanelItem> items = tab.getPanelItems(); assertFalse(items.isEmpty()); items.forEach(i -> { - if (i.getName().equals("Axolotl") || i.getName().equals("Cow") || i.getName().equals("Bat")) { + if (i.getName().equals("Armadillo") || i.getName().equals("Axolotl") || i.getName().equals("Cow") + || i.getName().equals("Bat")) { assertEquals("Name : " + i.getName(), Material.RED_SHULKER_BOX, i.getItem().getType()); } else { assertEquals("Name : " + i.getName(), Material.GREEN_SHULKER_BOX, i.getItem().getType()); @@ -182,7 +188,8 @@ public class GeoMobLimitTabTest { List<@Nullable PanelItem> items = tab.getPanelItems(); assertFalse(items.isEmpty()); items.forEach(i -> { - if (i.getName().equals("Axolotl") || i.getName().equals("Cow") || i.getName().equals("Bat")) { + if (i.getName().equals("Armadillo") || i.getName().equals("Axolotl") || i.getName().equals("Cow") + || i.getName().equals("Bat")) { assertEquals("Name : " + i.getName(), Material.GREEN_SHULKER_BOX, i.getItem().getType()); } else { assertEquals("Name : " + i.getName(), Material.RED_SHULKER_BOX, i.getItem().getType()); diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/TNTListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/TNTListenerTest.java index 5256e6bbc..b2f6b8932 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/TNTListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/TNTListenerTest.java @@ -77,7 +77,7 @@ public class TNTListenerTest extends AbstractCommonSetup { when(block.getWorld()).thenReturn(world); // Entity - when(entity.getType()).thenReturn(EntityType.PRIMED_TNT); + when(entity.getType()).thenReturn(EntityType.TNT); when(entity.getWorld()).thenReturn(world); when(entity.getLocation()).thenReturn(location); diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/ChestDamageListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/ChestDamageListenerTest.java index e4d10b570..b9d96f5a0 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/ChestDamageListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/ChestDamageListenerTest.java @@ -185,7 +185,7 @@ public class ChestDamageListenerTest extends AbstractCommonSetup public void testOnExplosionChestDamageNotAllowed() { Flags.CHEST_DAMAGE.setSetting(world, false); Entity entity = mock(Entity.class); - when(entity.getType()).thenReturn(EntityType.PRIMED_TNT); + when(entity.getType()).thenReturn(EntityType.TNT); List list = new ArrayList<>(); Block chest = mock(Block.class); when(chest.getType()).thenReturn(Material.CHEST); @@ -217,7 +217,7 @@ public class ChestDamageListenerTest extends AbstractCommonSetup public void testOnExplosionChestDamageAllowed() { Flags.CHEST_DAMAGE.setSetting(world, true); Entity entity = mock(Entity.class); - when(entity.getType()).thenReturn(EntityType.PRIMED_TNT); + when(entity.getType()).thenReturn(EntityType.TNT); List list = new ArrayList<>(); Block chest = mock(Block.class); when(chest.getType()).thenReturn(Material.CHEST); diff --git a/src/test/java/world/bentobox/bentobox/panels/customizable/IslandCreationPanelTest.java b/src/test/java/world/bentobox/bentobox/panels/customizable/IslandCreationPanelTest.java index 12b982e36..3c2a48a17 100644 --- a/src/test/java/world/bentobox/bentobox/panels/customizable/IslandCreationPanelTest.java +++ b/src/test/java/world/bentobox/bentobox/panels/customizable/IslandCreationPanelTest.java @@ -209,7 +209,6 @@ public class IslandCreationPanelTest { verify(inv).setItem(eq(0), any()); verify(inv).setItem(eq(1), any()); verify(meta).setDisplayName(eq("test")); - verify(meta).setLocalizedName(eq("test")); verify(meta).setLore(eq(List.of("A description", "", "panels.tips.click-to-choose"))); } @@ -224,15 +223,12 @@ public class IslandCreationPanelTest { verify(inv).setItem(eq(0), any()); verify(inv).setItem(eq(1), any()); verify(meta).setDisplayName(eq("test")); - verify(meta).setLocalizedName(eq("test")); verify(meta).setLore(eq(List.of("A description", "", "panels.tips.click-to-choose"))); verify(inv).setItem(eq(0), any()); verify(meta).setDisplayName(eq("test2")); - verify(meta).setLocalizedName(eq("test2")); verify(meta).setLore(eq(List.of("A description 2", "", "panels.tips.click-to-choose"))); verify(inv).setItem(eq(1), any()); verify(meta).setDisplayName(eq("test3")); - verify(meta).setLocalizedName(eq("test3")); verify(meta).setLore(eq(List.of("A description 3", "", "panels.tips.click-to-choose"))); } From 63cc0a01d9303886497757e821c6c795a93e53e6 Mon Sep 17 00:00:00 2001 From: tastybento Date: Mon, 29 Apr 2024 19:46:06 -0700 Subject: [PATCH 2/4] Fix compatibility for 1.20.6 --- .../admin/range/AdminRangeDisplayCommand.java | 11 +++----- .../flags/protection/BreedingListener.java | 9 +++---- .../flags/protection/LeashListener.java | 8 +++--- .../flags/protection/TNTListener.java | 9 +++---- .../world/bentobox/bentobox/util/Util.java | 25 +++++++++++++++++++ .../versions/ServerCompatibility.java | 10 +++++++- 6 files changed, 48 insertions(+), 24 deletions(-) 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; From e33823d0c0b65d75272fc6b8d0f74629cc22a968 Mon Sep 17 00:00:00 2001 From: tastybento Date: Mon, 29 Apr 2024 21:37:08 -0700 Subject: [PATCH 3/4] Fix tests --- .../flags/protection/BreedingListener.java | 8 +++--- .../flags/protection/TNTListener.java | 25 ++++++++++++++----- .../bentobox/bentobox/util/ItemParser.java | 1 - .../world/bentobox/bentobox/util/Util.java | 7 +++++- .../world/bentobox/bentobox/TestBentoBox.java | 6 +++++ .../admin/AdminSettingsCommandTest.java | 1 + .../commands/island/IslandGoCommandTest.java | 1 + .../flags/clicklisteners/CycleClickTest.java | 1 + .../clicklisteners/IslandToggleClickTest.java | 1 + .../StandardSpawnProtectionListenerTest.java | 1 + .../listeners/flags/AbstractCommonSetup.java | 2 ++ .../flags/protection/TNTListenerTest.java | 4 +++ .../flags/settings/PVPListenerTest.java | 1 + .../CleanSuperFlatListenerTest.java | 1 + .../worldsettings/EnterExitListenerTest.java | 8 +++--- .../InvincibleVisitorsListenerTest.java | 1 + .../IslandRespawnListenerTest.java | 1 + .../OfflineGrowthListenerTest.java | 8 +++--- .../OfflineRedstoneListenerTest.java | 1 + .../worldsettings/PistonPushListenerTest.java | 1 + .../worldsettings/RemoveMobsListenerTest.java | 1 + .../bentobox/managers/FlagsManagerTest.java | 3 +++ .../bentobox/managers/IslandsManagerTest.java | 1 + 23 files changed, 68 insertions(+), 17 deletions(-) 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 6e2dca405..0a09e196b 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 @@ -17,10 +17,10 @@ import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.ItemStack; import com.google.common.base.Enums; +import com.google.common.base.Optional; import world.bentobox.bentobox.api.flags.FlagListener; import world.bentobox.bentobox.lists.Flags; -import world.bentobox.bentobox.util.Util; /** * Handles breeding protection @@ -41,8 +41,10 @@ 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)); - bi.put(Util.findFirstMatchingEnum(EntityType.class, "MUSHROOM_COW", "MOOSHROOM"), - Collections.singletonList(Material.WHEAT)); + Optional mc = Enums.getIfPresent(EntityType.class, "MUSHROOM_COW"); + if (mc.isPresent()) { + bi.put(mc.get(), 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/TNTListener.java b/src/main/java/world/bentobox/bentobox/listeners/flags/protection/TNTListener.java index c565ed111..5703a3909 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 @@ -1,5 +1,6 @@ package world.bentobox.bentobox.listeners.flags.protection; +import java.io.IOException; import java.util.List; import org.bukkit.Location; @@ -17,6 +18,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 com.google.common.base.Optional; + import world.bentobox.bentobox.api.flags.FlagListener; import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.util.Util; @@ -26,17 +30,26 @@ import world.bentobox.bentobox.util.Util; * @author tastybento */ public class TNTListener extends FlagListener { - - 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. * @since 1.5.0 */ - private static final List TNT_TYPES = List.of(PRIMED_TNT, MINECART_TNT); + private static final List TNT_TYPES = List.of( + findFirstMatchingEnum(EntityType.class, "PRIMED_TNT", "TNT"), + findFirstMatchingEnum(EntityType.class, "MINECART_TNT", "TNT_MINECART")); + private static > T findFirstMatchingEnum(Class enumClass, String... values) { + if (enumClass == null || values == null) { + return null; + } + for (String value : values) { + Optional enumConstant = Enums.getIfPresent(enumClass, value.toUpperCase()); + if (enumConstant.isPresent()) { + return enumConstant.get(); + } + } + return null; // Return null if no match is found + } /** * Contains {@link Material}s that can be used to prime a TNT. * @since 1.5.0 diff --git a/src/main/java/world/bentobox/bentobox/util/ItemParser.java b/src/main/java/world/bentobox/bentobox/util/ItemParser.java index 89115c47e..f294e829e 100644 --- a/src/main/java/world/bentobox/bentobox/util/ItemParser.java +++ b/src/main/java/world/bentobox/bentobox/util/ItemParser.java @@ -145,7 +145,6 @@ public class ItemParser { ItemParser.setCustomModelData(returnValue, customModelData); } } catch (Exception exception) { - exception.printStackTrace(); BentoBox.getInstance().logError("Could not parse item " + text + " " + exception.getLocalizedMessage()); returnValue = defaultItemStack; } diff --git a/src/main/java/world/bentobox/bentobox/util/Util.java b/src/main/java/world/bentobox/bentobox/util/Util.java index 0f5438bd2..74f253bed 100644 --- a/src/main/java/world/bentobox/bentobox/util/Util.java +++ b/src/main/java/world/bentobox/bentobox/util/Util.java @@ -1,5 +1,6 @@ package world.bentobox.bentobox.util; +import java.io.IOException; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -815,15 +816,19 @@ public class Util { * @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 IOException * @throws NullPointerException if either {@code enumClass} or {@code values} are null */ public static > T findFirstMatchingEnum(Class enumClass, String... values) { + if (enumClass == null || values == null) { + return null; + } 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 + return null; // Return null if no match is found } } diff --git a/src/test/java/world/bentobox/bentobox/TestBentoBox.java b/src/test/java/world/bentobox/bentobox/TestBentoBox.java index fdfe61772..a00cce458 100644 --- a/src/test/java/world/bentobox/bentobox/TestBentoBox.java +++ b/src/test/java/world/bentobox/bentobox/TestBentoBox.java @@ -36,6 +36,8 @@ import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; +import org.mockito.Mockito; +import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; @@ -93,6 +95,10 @@ public class TestBentoBox extends AbstractCommonSetup { when(ownerOfIsland.getUniqueId()).thenReturn(uuid); when(visitorToIsland.getUniqueId()).thenReturn(VISITOR_UUID); + // Util + PowerMockito.mockStatic(Util.class); + when(Util.findFirstMatchingEnum(any(), any())).thenCallRealMethod(); + island.setOwner(uuid); island.setProtectionRange(100); HashMap members = new HashMap<>(); diff --git a/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminSettingsCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminSettingsCommandTest.java index c22fc6c60..d7dc9588f 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminSettingsCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminSettingsCommandTest.java @@ -155,6 +155,7 @@ public class AdminSettingsCommandTest extends RanksManagerBeforeClassTest { PowerMockito.mockStatic(Util.class); when(Util.getUUID(anyString())).thenReturn(uuid); when(Util.tabLimit(any(), any())).thenCallRealMethod(); + when(Util.findFirstMatchingEnum(any(), any())).thenCallRealMethod(); // Settings Settings settings = new Settings(); diff --git a/src/test/java/world/bentobox/bentobox/api/commands/island/IslandGoCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/island/IslandGoCommandTest.java index cf55bd3d5..ce7414c2a 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/island/IslandGoCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/island/IslandGoCommandTest.java @@ -160,6 +160,7 @@ public class IslandGoCommandTest { when(iwm.getAddon(any())).thenReturn(Optional.empty()); PowerMockito.mockStatic(Util.class); + when(Util.findFirstMatchingEnum(any(), any())).thenCallRealMethod(); // Locales LocalesManager lm = mock(LocalesManager.class); diff --git a/src/test/java/world/bentobox/bentobox/api/flags/clicklisteners/CycleClickTest.java b/src/test/java/world/bentobox/bentobox/api/flags/clicklisteners/CycleClickTest.java index 9c86acc54..b5e47bd07 100644 --- a/src/test/java/world/bentobox/bentobox/api/flags/clicklisteners/CycleClickTest.java +++ b/src/test/java/world/bentobox/bentobox/api/flags/clicklisteners/CycleClickTest.java @@ -215,6 +215,7 @@ public class CycleClickTest { // Util PowerMockito.mockStatic(Util.class); when(Util.getWorld(any())).thenReturn(world); + when(Util.findFirstMatchingEnum(any(), any())).thenCallRealMethod(); // Event when(Bukkit.getPluginManager()).thenReturn(pim); diff --git a/src/test/java/world/bentobox/bentobox/api/flags/clicklisteners/IslandToggleClickTest.java b/src/test/java/world/bentobox/bentobox/api/flags/clicklisteners/IslandToggleClickTest.java index 9a840f7bd..433dd3b94 100644 --- a/src/test/java/world/bentobox/bentobox/api/flags/clicklisteners/IslandToggleClickTest.java +++ b/src/test/java/world/bentobox/bentobox/api/flags/clicklisteners/IslandToggleClickTest.java @@ -92,6 +92,7 @@ public class IslandToggleClickTest { when(user.getUniqueId()).thenReturn(uuid); PowerMockito.mockStatic(Util.class); when(Util.getWorld(any())).thenReturn(mock(World.class)); + when(Util.findFirstMatchingEnum(any(), any())).thenCallRealMethod(); FlagsManager fm = mock(FlagsManager.class); when(flag.isSetForWorld(any())).thenReturn(false); diff --git a/src/test/java/world/bentobox/bentobox/listeners/StandardSpawnProtectionListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/StandardSpawnProtectionListenerTest.java index 8beb740c2..73e19892a 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/StandardSpawnProtectionListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/StandardSpawnProtectionListenerTest.java @@ -134,6 +134,7 @@ public class StandardSpawnProtectionListenerTest { // Util translate color codes (used in user translate methods) when(Util.translateColorCodes(anyString())).thenAnswer((Answer) invocation -> invocation.getArgument(0, String.class)); + when(Util.findFirstMatchingEnum(any(), any())).thenCallRealMethod(); // Set up class ssp = new StandardSpawnProtectionListener(plugin); diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/AbstractCommonSetup.java b/src/test/java/world/bentobox/bentobox/listeners/flags/AbstractCommonSetup.java index e780d3b84..bae0e4553 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/AbstractCommonSetup.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/AbstractCommonSetup.java @@ -169,6 +169,8 @@ public abstract class AbstractCommonSetup { PowerMockito.mockStatic(Util.class); when(Util.getWorld(any())).thenReturn(mock(World.class)); + // Util + when(Util.findFirstMatchingEnum(any(), any())).thenCallRealMethod(); // Util translate color codes (used in user translate methods) when(Util.translateColorCodes(anyString())).thenAnswer((Answer) invocation -> invocation.getArgument(0, String.class)); diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/TNTListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/TNTListenerTest.java index b2f6b8932..99dcf74d8 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/TNTListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/TNTListenerTest.java @@ -4,6 +4,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; @@ -81,6 +82,9 @@ public class TNTListenerTest extends AbstractCommonSetup { when(entity.getWorld()).thenReturn(world); when(entity.getLocation()).thenReturn(location); + // Util + when(Util.findFirstMatchingEnum(any(), anyString())).thenCallRealMethod(); + listener = new TNTListener(); listener.setPlugin(plugin); diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/settings/PVPListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/settings/PVPListenerTest.java index 13ec1e49b..8d4168f2a 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/settings/PVPListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/settings/PVPListenerTest.java @@ -231,6 +231,7 @@ public class PVPListenerTest { // Util translate color codes (used in user translate methods) when(Util.translateColorCodes(anyString())).thenAnswer((Answer) invocation -> invocation.getArgument(0, String.class)); + when(Util.findFirstMatchingEnum(any(), any())).thenCallRealMethod(); } diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/CleanSuperFlatListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/CleanSuperFlatListenerTest.java index 603b0a503..b41e004f5 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/CleanSuperFlatListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/CleanSuperFlatListenerTest.java @@ -82,6 +82,7 @@ public class CleanSuperFlatListenerTest { PowerMockito.mockStatic(Util.class, Mockito.RETURNS_MOCKS); when(Util.getWorld(any())).thenReturn(world); + when(Util.findFirstMatchingEnum(any(), any())).thenCallRealMethod(); // Regenerator when(Util.getRegenerator()).thenReturn(regenerator); diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/EnterExitListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/EnterExitListenerTest.java index 09b45ac2b..26f342887 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/EnterExitListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/EnterExitListenerTest.java @@ -102,6 +102,11 @@ public class EnterExitListenerTest { Settings s = mock(Settings.class); when(plugin.getSettings()).thenReturn(s); + // Util + PowerMockito.mockStatic(Util.class); + when(Util.getWorld(any())).thenReturn(world); + when(Util.findFirstMatchingEnum(any(), any())).thenCallRealMethod(); + // Player Player p = mock(Player.class); // Sometimes use Mockito.withSettings().verboseLogging() @@ -189,9 +194,6 @@ public class EnterExitListenerTest { // Listener listener = new EnterExitListener(); - PowerMockito.mockStatic(Util.class); - when(Util.getWorld(any())).thenReturn(world); - // World Settings WorldSettings ws = mock(WorldSettings.class); when(iwm.getWorldSettings(any())).thenReturn(ws); diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/InvincibleVisitorsListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/InvincibleVisitorsListenerTest.java index 0a1f9c1a8..f476af056 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/InvincibleVisitorsListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/InvincibleVisitorsListenerTest.java @@ -137,6 +137,7 @@ public class InvincibleVisitorsListenerTest { PowerMockito.mockStatic(Util.class); when(Util.getWorld(any())).thenReturn(mock(World.class)); when(Util.prettifyText(anyString())).thenCallRealMethod(); + when(Util.findFirstMatchingEnum(any(), any())).thenCallRealMethod(); // Util translate color codes (used in user translate methods) when(Util.translateColorCodes(anyString())).thenAnswer((Answer) invocation -> invocation.getArgument(0, String.class)); FlagsManager fm = mock(FlagsManager.class); diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/IslandRespawnListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/IslandRespawnListenerTest.java index 4d5a85741..7bfe5f939 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/IslandRespawnListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/IslandRespawnListenerTest.java @@ -103,6 +103,7 @@ public class IslandRespawnListenerTest { PowerMockito.mockStatic(Util.class); when(Util.getWorld(any())).thenReturn(world); + when(Util.findFirstMatchingEnum(any(), any())).thenCallRealMethod(); // World Settings WorldSettings ws = mock(WorldSettings.class); diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/OfflineGrowthListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/OfflineGrowthListenerTest.java index 17e6f7a2b..3427b8917 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/OfflineGrowthListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/OfflineGrowthListenerTest.java @@ -66,6 +66,11 @@ public class OfflineGrowthListenerTest { BentoBox plugin = mock(BentoBox.class); Whitebox.setInternalState(BentoBox.class, "instance", plugin); + // Util + PowerMockito.mockStatic(Util.class); + when(Util.getWorld(any())).thenReturn(world); + when(Util.findFirstMatchingEnum(any(), any())).thenCallRealMethod(); + // Owner UUID uuid = UUID.randomUUID(); @@ -92,9 +97,6 @@ public class OfflineGrowthListenerTest { when(block.getLocation()).thenReturn(inside); when(block.getType()).thenReturn(Material.KELP); - PowerMockito.mockStatic(Util.class); - when(Util.getWorld(any())).thenReturn(world); - // World Settings when(iwm.inWorld(any(World.class))).thenReturn(true); when(plugin.getIWM()).thenReturn(iwm); diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/OfflineRedstoneListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/OfflineRedstoneListenerTest.java index 1624808de..a32b1079e 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/OfflineRedstoneListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/OfflineRedstoneListenerTest.java @@ -94,6 +94,7 @@ public class OfflineRedstoneListenerTest { // Util PowerMockito.mockStatic(Util.class); when(Util.getWorld(any())).thenReturn(world); + when(Util.findFirstMatchingEnum(any(), any())).thenCallRealMethod(); // World Settings when(iwm.inWorld(any(World.class))).thenReturn(true); diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/PistonPushListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/PistonPushListenerTest.java index 1fe59ca16..882c48a32 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/PistonPushListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/PistonPushListenerTest.java @@ -94,6 +94,7 @@ public class PistonPushListenerTest { PowerMockito.mockStatic(Util.class); when(Util.getWorld(any())).thenReturn(world); + when(Util.findFirstMatchingEnum(any(), any())).thenCallRealMethod(); // World Settings IslandWorldManager iwm = mock(IslandWorldManager.class); diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/RemoveMobsListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/RemoveMobsListenerTest.java index 7a9883951..9583ddd7c 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/RemoveMobsListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/RemoveMobsListenerTest.java @@ -97,6 +97,7 @@ public class RemoveMobsListenerTest { PowerMockito.mockStatic(Util.class); when(Util.getWorld(any())).thenReturn(world); + when(Util.findFirstMatchingEnum(any(), any())).thenCallRealMethod(); // World Settings IslandWorldManager iwm = mock(IslandWorldManager.class); diff --git a/src/test/java/world/bentobox/bentobox/managers/FlagsManagerTest.java b/src/test/java/world/bentobox/bentobox/managers/FlagsManagerTest.java index 5b92a5a7d..2368a0aae 100644 --- a/src/test/java/world/bentobox/bentobox/managers/FlagsManagerTest.java +++ b/src/test/java/world/bentobox/bentobox/managers/FlagsManagerTest.java @@ -87,6 +87,9 @@ public class FlagsManagerTest { when(Bukkit.getLogger()).thenReturn(Logger.getAnonymousLogger()); //PowerMockito.mockStatic(Flags.class); + // Util + when(Util.findFirstMatchingEnum(any(), any())).thenCallRealMethod(); + } @After diff --git a/src/test/java/world/bentobox/bentobox/managers/IslandsManagerTest.java b/src/test/java/world/bentobox/bentobox/managers/IslandsManagerTest.java index 7931f9df7..e54e0b357 100644 --- a/src/test/java/world/bentobox/bentobox/managers/IslandsManagerTest.java +++ b/src/test/java/world/bentobox/bentobox/managers/IslandsManagerTest.java @@ -238,6 +238,7 @@ public class IslandsManagerTest extends AbstractCommonSetup { // Worlds translate to world PowerMockito.mockStatic(Util.class); when(Util.getWorld(any())).thenReturn(world); + when(Util.findFirstMatchingEnum(any(), any())).thenCallRealMethod(); // Island when(island.getOwner()).thenReturn(uuid); From 15335eb9929044d8d79ce2766639cd90403666c4 Mon Sep 17 00:00:00 2001 From: tastybento Date: Mon, 29 Apr 2024 22:26:09 -0700 Subject: [PATCH 4/4] Add NMS for latest --- pom.xml | 6 +++ .../nms/v1_20_R4/PasteHandlerImpl.java | 52 +++++++++++++++++++ .../nms/v1_20_R4/WorldRegeneratorImpl.java | 26 ++++++++++ 3 files changed, 84 insertions(+) create mode 100644 src/main/java/world/bentobox/bentobox/nms/v1_20_R4/PasteHandlerImpl.java create mode 100644 src/main/java/world/bentobox/bentobox/nms/v1_20_R4/WorldRegeneratorImpl.java diff --git a/pom.xml b/pom.xml index 985bfe14e..a5965830c 100644 --- a/pom.xml +++ b/pom.xml @@ -230,6 +230,12 @@ ${spigot.version} provided + + org.spigotmc.... + spigot + 1.20.6-R0.1-SNAPSHOT + provided + org.spigotmc. spigot diff --git a/src/main/java/world/bentobox/bentobox/nms/v1_20_R4/PasteHandlerImpl.java b/src/main/java/world/bentobox/bentobox/nms/v1_20_R4/PasteHandlerImpl.java new file mode 100644 index 000000000..c9e27d0ef --- /dev/null +++ b/src/main/java/world/bentobox/bentobox/nms/v1_20_R4/PasteHandlerImpl.java @@ -0,0 +1,52 @@ +package world.bentobox.bentobox.nms.v1_20_R4; + +import java.util.concurrent.CompletableFuture; + +import org.bukkit.Location; +import org.bukkit.block.Block; +import org.bukkit.block.data.BlockData; +import org.bukkit.craftbukkit.v1_20_R4.CraftWorld; +import org.bukkit.craftbukkit.v1_20_R4.block.data.CraftBlockData; + +import net.minecraft.core.BlockPosition; +import net.minecraft.world.level.block.state.IBlockData; +import net.minecraft.world.level.chunk.Chunk; +import world.bentobox.bentobox.blueprints.dataobjects.BlueprintBlock; +import world.bentobox.bentobox.database.objects.Island; +import world.bentobox.bentobox.nms.PasteHandler; +import world.bentobox.bentobox.util.DefaultPasteUtil; +import world.bentobox.bentobox.util.Util; + +public class PasteHandlerImpl implements PasteHandler { + + protected static final IBlockData AIR = ((CraftBlockData) AIR_BLOCKDATA).getState(); + + /** + * Set the block to the location + * + * @param island - island + * @param location - location + * @param bpBlock - blueprint block + */ + @Override + public CompletableFuture setBlock(Island island, Location location, BlueprintBlock bpBlock) { + return Util.getChunkAtAsync(location).thenRun(() -> { + Block block = location.getBlock(); + // Set the block data - default is AIR + BlockData bd = DefaultPasteUtil.createBlockData(bpBlock); + CraftBlockData craft = (CraftBlockData) bd; + net.minecraft.world.level.World nmsWorld = ((CraftWorld) location.getWorld()).getHandle(); + Chunk nmsChunk = nmsWorld.d(location.getBlockX() >> 4, location.getBlockZ() >> 4); + BlockPosition bp = new BlockPosition(location.getBlockX(), location.getBlockY(), location.getBlockZ()); + // Setting the block to air before setting to another state prevents some console errors + nmsChunk.a(bp, AIR, false); + nmsChunk.a(bp, craft.getState(), false); + block.setBlockData(bd, false); + DefaultPasteUtil.setBlockState(island, block, bpBlock); + // Set biome + if (bpBlock.getBiome() != null) { + block.setBiome(bpBlock.getBiome()); + } + }); + } +} diff --git a/src/main/java/world/bentobox/bentobox/nms/v1_20_R4/WorldRegeneratorImpl.java b/src/main/java/world/bentobox/bentobox/nms/v1_20_R4/WorldRegeneratorImpl.java new file mode 100644 index 000000000..8a728d2e2 --- /dev/null +++ b/src/main/java/world/bentobox/bentobox/nms/v1_20_R4/WorldRegeneratorImpl.java @@ -0,0 +1,26 @@ +package world.bentobox.bentobox.nms.v1_20_R4; + +import org.bukkit.block.data.BlockData; +import org.bukkit.craftbukkit.v1_20_R4.CraftWorld; +import org.bukkit.craftbukkit.v1_20_R4.block.data.CraftBlockData; + +import net.minecraft.core.BlockPosition; +import net.minecraft.world.level.World; +import net.minecraft.world.level.chunk.Chunk; +import world.bentobox.bentobox.nms.CopyWorldRegenerator; + +public class WorldRegeneratorImpl extends CopyWorldRegenerator { + + @Override + public void setBlockInNativeChunk(org.bukkit.Chunk chunk, int x, int y, int z, BlockData blockData, + boolean applyPhysics) { + CraftBlockData craft = (CraftBlockData) blockData; + World nmsWorld = ((CraftWorld) chunk.getWorld()).getHandle(); + Chunk nmsChunk = nmsWorld.d(chunk.getX(), chunk.getZ()); + BlockPosition bp = new BlockPosition((chunk.getX() << 4) + x, y, (chunk.getZ() << 4) + z); + // Setting the block to air before setting to another state prevents some console errors + nmsChunk.a(bp, PasteHandlerImpl.AIR, applyPhysics); + nmsChunk.a(bp, craft.getState(), applyPhysics); + } + +} \ No newline at end of file