From f4d3e791d6f591dc1c61dcfac2dd4be9fbf14d53 Mon Sep 17 00:00:00 2001 From: tastybento Date: Sat, 27 Jul 2024 21:22:08 -0700 Subject: [PATCH 01/18] Provides an API to hide sub-flags #2437 --- pom.xml | 2 +- .../bentobox/bentobox/api/flags/Flag.java | 44 +++++++++++++++++++ .../bentobox/panels/settings/SettingsTab.java | 18 ++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 89e22cc51..1d1183dbd 100644 --- a/pom.xml +++ b/pom.xml @@ -88,7 +88,7 @@ -LOCAL - 2.4.2 + 2.4.3 bentobox-world https://sonarcloud.io ${project.basedir}/lib diff --git a/src/main/java/world/bentobox/bentobox/api/flags/Flag.java b/src/main/java/world/bentobox/bentobox/api/flags/Flag.java index 89b13cb09..ca9c0a3b2 100644 --- a/src/main/java/world/bentobox/bentobox/api/flags/Flag.java +++ b/src/main/java/world/bentobox/bentobox/api/flags/Flag.java @@ -116,6 +116,25 @@ public boolean isGreaterThan(Mode rank) { } } + /** + * Options for hiding of sub flags + * @since 2.4.3 + */ + public enum HideWhen { + /** + * Never hide sub-flags + */ + NEVER, + /** + * Hide subflags if the setting of the parent flag is true + */ + SETTING_TRUE, + /** + * Hide subflags if the setting of the parent flag is false + */ + SETTING_FALSE + } + private static final String PROTECTION_FLAGS = "protection.flags."; private final String id; @@ -131,6 +150,7 @@ public boolean isGreaterThan(Mode rank) { private final int cooldown; private final Mode mode; private final Set subflags; + private final HideWhen hideWhen; private Flag(Builder builder) { this.id = builder.id; @@ -148,6 +168,7 @@ private Flag(Builder builder) { this.addon = builder.addon; this.mode = builder.mode; this.subflags = builder.subflags; + this.hideWhen = builder.hideWhen; } public String getID() { @@ -276,6 +297,14 @@ public Addon getAddon() { return addon; } + /** + * Get when sub-flags should be hidden + * @return + */ + public HideWhen getHideWhen() { + return hideWhen; + } + /* (non-Javadoc) * @see java.lang.Object#hashCode() */ @@ -553,6 +582,9 @@ public static class Builder { // Subflags private final Set subflags; + // Hide when indicator + private HideWhen hideWhen = HideWhen.NEVER; + /** * Builder for making flags * @param id - a unique id that MUST be the same as the enum of the flag @@ -682,6 +714,18 @@ public Builder subflags(Flag... flags) { return this; } + /** + * When should sub-flags be hidden, if ever + * {@see HideWhen} + * @param hideWhen hide when indicator + * @return Builder - flag builder + * @since 2.4.3 + */ + public Builder hideWhen(HideWhen hideWhen) { + this.hideWhen = hideWhen; + return this; + } + /** * Build the flag * @return Flag diff --git a/src/main/java/world/bentobox/bentobox/panels/settings/SettingsTab.java b/src/main/java/world/bentobox/bentobox/panels/settings/SettingsTab.java index b558cc9d4..934b22848 100644 --- a/src/main/java/world/bentobox/bentobox/panels/settings/SettingsTab.java +++ b/src/main/java/world/bentobox/bentobox/panels/settings/SettingsTab.java @@ -2,8 +2,10 @@ import java.util.Comparator; import java.util.HashMap; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.UUID; import java.util.stream.Collectors; @@ -17,6 +19,7 @@ import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.flags.Flag; +import world.bentobox.bentobox.api.flags.Flag.HideWhen; import world.bentobox.bentobox.api.flags.Flag.Mode; import world.bentobox.bentobox.api.flags.Flag.Type; import world.bentobox.bentobox.api.localization.TextVariables; @@ -131,10 +134,25 @@ public String getName() { currentMode.put(user.getUniqueId(), currentMode.getOrDefault(user.getUniqueId(), Mode.BASIC).getNext()); flags = getFlags(); } + // Remove any sub-flags that shouldn't be shown + Set toBeRemoved = new HashSet<>(); + flags.stream().forEach(flag -> { + if ((flag.getType() == Type.SETTING || flag.getType() == Type.WORLD_SETTING) && flag.hasSubflags()) { + if (flag.isSetForWorld(world) && flag.getHideWhen() == HideWhen.SETTING_TRUE) { + toBeRemoved.addAll(flag.getSubflags()); + } else if (!flag.isSetForWorld(world) && flag.getHideWhen() == HideWhen.SETTING_FALSE) { + toBeRemoved.addAll(flag.getSubflags()); + } + } + + }); + flags.removeAll(toBeRemoved); + List<@Nullable PanelItem> result = flags.stream().map( (f -> f.toPanelItem(plugin, user, world, island, plugin.getIWM().getHiddenFlags(world).contains(f.getID())))) .toList(); + return result; } From cccc9ee98de54d9e29350ed88c3eab1f02283484 Mon Sep 17 00:00:00 2001 From: tastybento Date: Sun, 28 Jul 2024 15:27:44 -0700 Subject: [PATCH 02/18] WIP --- .../bentobox/listeners/JoinLeaveListener.java | 19 +++++++++++++++++++ .../bentobox/lists/GameModePlaceholder.java | 1 + 2 files changed, 20 insertions(+) diff --git a/src/main/java/world/bentobox/bentobox/listeners/JoinLeaveListener.java b/src/main/java/world/bentobox/bentobox/listeners/JoinLeaveListener.java index 605e020c3..2a4820cee 100644 --- a/src/main/java/world/bentobox/bentobox/listeners/JoinLeaveListener.java +++ b/src/main/java/world/bentobox/bentobox/listeners/JoinLeaveListener.java @@ -4,6 +4,7 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; +import java.util.stream.Collectors; import org.bukkit.Bukkit; import org.bukkit.World; @@ -105,6 +106,24 @@ public void onPlayerJoin(final PlayerJoinEvent event) { // Add a player to the bStats cache. plugin.getMetrics().ifPresent(bStats -> bStats.addPlayer(playerUUID)); + + // Create onIsland placeholders + plugin.getAddonsManager().getGameModeAddons().forEach(addon -> { + plugin.getPlaceholdersManager() + .registerPlaceholder(addon.getDescription().getName() + "_onisland_" + user.getName(), asker -> { + if (asker == null) { + return ""; + } + // Get the user who this applies to + User named = User.getInstance(user.getUniqueId()); + if (named.isOnline()) { + return plugin.getIslands().getIslands(addon.getOverWorld(), asker).stream() + .filter(island -> island.onIsland(named.getLocation())).findFirst().map(i -> "true") + .orElse("false"); + } + return "false"; + }); + }); } private void firstTime(User user) { diff --git a/src/main/java/world/bentobox/bentobox/lists/GameModePlaceholder.java b/src/main/java/world/bentobox/bentobox/lists/GameModePlaceholder.java index 5677b3c89..032617d26 100644 --- a/src/main/java/world/bentobox/bentobox/lists/GameModePlaceholder.java +++ b/src/main/java/world/bentobox/bentobox/lists/GameModePlaceholder.java @@ -19,6 +19,7 @@ /** * Common Game Mode Placeholders + * All of these are prefixed with the game mode's name, e.g., bskykblock_ */ public enum GameModePlaceholder { From d704aa9c679da125da70f3ffb99ea8b38cc2e7fc Mon Sep 17 00:00:00 2001 From: tastybento Date: Sun, 28 Jul 2024 15:43:04 -0700 Subject: [PATCH 03/18] Use dedicated glint API instead of enchantments --- .../java/world/bentobox/bentobox/api/panels/PanelItem.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) 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 589e936d2..24c76e1c9 100644 --- a/src/main/java/world/bentobox/bentobox/api/panels/PanelItem.java +++ b/src/main/java/world/bentobox/bentobox/api/panels/PanelItem.java @@ -132,11 +132,7 @@ public void setGlow(boolean glow) { return; } if (meta != null) { - if (glow) { - meta.addEnchant(Enchantment.LURE, 0, glow); - } else { - meta.removeEnchant(Enchantment.LURE); - } + meta.setEnchantmentGlintOverride(glow); icon.setItemMeta(meta); } From ef86bdbdc5f73e95059a2d4e0709b5fd25b21d73 Mon Sep 17 00:00:00 2001 From: TreemanK Date: Mon, 29 Jul 2024 11:39:56 +1000 Subject: [PATCH 04/18] feat: further api improvements hideWhen --- .../bentobox/bentobox/api/flags/Flag.java | 27 ++++++++++++++++--- .../bentobox/panels/settings/SettingsTab.java | 13 +++++---- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/api/flags/Flag.java b/src/main/java/world/bentobox/bentobox/api/flags/Flag.java index ca9c0a3b2..6d40f889c 100644 --- a/src/main/java/world/bentobox/bentobox/api/flags/Flag.java +++ b/src/main/java/world/bentobox/bentobox/api/flags/Flag.java @@ -151,6 +151,8 @@ public enum HideWhen { private final Mode mode; private final Set subflags; private final HideWhen hideWhen; + private boolean isSubFlag; + private Flag parentFlag; private Flag(Builder builder) { this.id = builder.id; @@ -169,6 +171,8 @@ private Flag(Builder builder) { this.mode = builder.mode; this.subflags = builder.subflags; this.hideWhen = builder.hideWhen; + this.isSubFlag = false; + this.parentFlag = null; } public String getID() { @@ -305,6 +309,20 @@ public HideWhen getHideWhen() { return hideWhen; } + /** + * @return the isSubFlag + */ + public boolean isSubFlag() { + return isSubFlag; + } + + /** + * @return the parentFlag + */ + public Flag getParentFlag() { + return parentFlag; + } + /* (non-Javadoc) * @see java.lang.Object#hashCode() */ @@ -711,6 +729,9 @@ public Builder mode(Mode mode) { */ public Builder subflags(Flag... flags) { this.subflags.addAll(Arrays.asList(flags)); + for (Flag flag : flags) { + flag.isSubFlag = true; + } return this; } @@ -739,9 +760,9 @@ public Flag build() { default -> new CycleClick(id); }; } - - return new Flag(this); + Flag flag = new Flag(this); + subflags.forEach(subflag -> subflag.parentFlag = flag); + return flag; } } - } diff --git a/src/main/java/world/bentobox/bentobox/panels/settings/SettingsTab.java b/src/main/java/world/bentobox/bentobox/panels/settings/SettingsTab.java index 934b22848..9987d2e40 100644 --- a/src/main/java/world/bentobox/bentobox/panels/settings/SettingsTab.java +++ b/src/main/java/world/bentobox/bentobox/panels/settings/SettingsTab.java @@ -136,15 +136,14 @@ public String getName() { } // Remove any sub-flags that shouldn't be shown Set toBeRemoved = new HashSet<>(); - flags.stream().forEach(flag -> { - if ((flag.getType() == Type.SETTING || flag.getType() == Type.WORLD_SETTING) && flag.hasSubflags()) { - if (flag.isSetForWorld(world) && flag.getHideWhen() == HideWhen.SETTING_TRUE) { - toBeRemoved.addAll(flag.getSubflags()); - } else if (!flag.isSetForWorld(world) && flag.getHideWhen() == HideWhen.SETTING_FALSE) { - toBeRemoved.addAll(flag.getSubflags()); + flags.forEach(flag -> { + if (flag.isSubFlag() && flag.getHideWhen() != HideWhen.NEVER) { + if (!flag.getParentFlag().isSetForWorld(world) && flag.getHideWhen() == HideWhen.SETTING_FALSE) { + toBeRemoved.add(flag); + } else if (flag.getParentFlag().isSetForWorld(world) && flag.getHideWhen() == HideWhen.SETTING_TRUE) { + toBeRemoved.add(flag); } } - }); flags.removeAll(toBeRemoved); From fc22fd23da6f6dd70a200ec131212625a96a4511 Mon Sep 17 00:00:00 2001 From: tastybento Date: Tue, 30 Jul 2024 21:48:16 -0700 Subject: [PATCH 05/18] Fix for NPE when unregistering islands via console Issue is that console does not have a UUID to log. --- .../api/commands/admin/AdminUnregisterCommand.java | 9 +++++++-- .../api/commands/admin/AdminUnregisterCommandTest.java | 3 +++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/api/commands/admin/AdminUnregisterCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/admin/AdminUnregisterCommand.java index d4d730380..06cb6a578 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/admin/AdminUnregisterCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/admin/AdminUnregisterCommand.java @@ -114,8 +114,13 @@ void unregisterIsland(User user) { targetIsland.getMemberSet().forEach(m -> getIslands().removePlayer(targetIsland, m)); // Remove all island players that reference this island targetIsland.getMembers().clear(); - targetIsland.log(new LogEntry.Builder("UNREGISTER").data("player", targetUUID.toString()) - .data("admin", user.getUniqueId().toString()).build()); + if (user.isPlayer()) { + targetIsland.log(new LogEntry.Builder("UNREGISTER").data("player", targetUUID.toString()) + .data("admin", user.getUniqueId().toString()).build()); + } else { + targetIsland.log(new LogEntry.Builder("UNREGISTER").data("player", targetUUID.toString()) + .data("admin", "console").build()); + } user.sendMessage("commands.admin.unregister.unregistered-island", TextVariables.XYZ, Util.xyz(targetIsland.getCenter().toVector()), TextVariables.NAME, getPlayers().getName(targetUUID)); } diff --git a/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminUnregisterCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminUnregisterCommandTest.java index 8b8c536d4..64a0b4ca4 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminUnregisterCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/admin/AdminUnregisterCommandTest.java @@ -41,6 +41,7 @@ import world.bentobox.bentobox.Settings; import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.localization.TextVariables; +import world.bentobox.bentobox.api.logs.LogEntry; import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.managers.CommandsManager; @@ -270,6 +271,8 @@ public void testUnregisterIsland() { verify(user).sendMessage("commands.admin.unregister.unregistered-island", TextVariables.XYZ, "1,2,3", TextVariables.NAME, "name"); verify(island).setOwner(null); + verify(island).log(any(LogEntry.class)); + } /** From 505de3b5f9861c2343d0231edf138dc411f16218 Mon Sep 17 00:00:00 2001 From: tastybento Date: Fri, 2 Aug 2024 07:45:38 -0700 Subject: [PATCH 06/18] Use a long to calculate the purge limit. This may solve a problem reported on Discord. Suspect is that there may be a calculation overflow. --- .../bentobox/api/commands/admin/purge/AdminPurgeCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/world/bentobox/bentobox/api/commands/admin/purge/AdminPurgeCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/admin/purge/AdminPurgeCommand.java index ec02a496c..a5c6fe6cc 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/admin/purge/AdminPurgeCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/admin/purge/AdminPurgeCommand.java @@ -127,7 +127,7 @@ void onIslandDeleted(IslandDeletedEvent e) { */ Set getOldIslands(int days) { long currentTimeMillis = System.currentTimeMillis(); - double daysInMilliseconds = days * 1000 * 3600 * 24; + long daysInMilliseconds = (long) days * 1000 * 3600 * 24; Set oldIslands = new HashSet<>(); // Process islands in one pass, logging and adding to the set if applicable From afe07a8c237d3ebdc87cc3f805170c3681eb3f97 Mon Sep 17 00:00:00 2001 From: tastybento Date: Fri, 2 Aug 2024 08:05:53 -0700 Subject: [PATCH 07/18] Register and unregister done properly --- .../bentobox/bentobox/listeners/JoinLeaveListener.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/listeners/JoinLeaveListener.java b/src/main/java/world/bentobox/bentobox/listeners/JoinLeaveListener.java index 2a4820cee..11c750889 100644 --- a/src/main/java/world/bentobox/bentobox/listeners/JoinLeaveListener.java +++ b/src/main/java/world/bentobox/bentobox/listeners/JoinLeaveListener.java @@ -4,7 +4,6 @@ import java.util.Objects; import java.util.Set; import java.util.UUID; -import java.util.stream.Collectors; import org.bukkit.Bukkit; import org.bukkit.World; @@ -110,7 +109,7 @@ public void onPlayerJoin(final PlayerJoinEvent event) { // Create onIsland placeholders plugin.getAddonsManager().getGameModeAddons().forEach(addon -> { plugin.getPlaceholdersManager() - .registerPlaceholder(addon.getDescription().getName() + "_onisland_" + user.getName(), asker -> { + .registerPlaceholder(addon, "onisland_" + user.getName(), asker -> { if (asker == null) { return ""; } @@ -256,6 +255,9 @@ public void onPlayerQuit(final PlayerQuitEvent event) { }); // Remove any coop associations from the player logging out plugin.getIslands().clearRank(RanksManager.COOP_RANK, event.getPlayer().getUniqueId()); + // Remove any onisland placeholder + plugin.getAddonsManager().getGameModeAddons().forEach(addon -> plugin.getPlaceholdersManager() + .unregisterPlaceholder(addon, "onisland_" + event.getPlayer().getName())); User.removePlayer(event.getPlayer()); } } From 2f055b2a8e62dec198dbe018e924c62af45e77aa Mon Sep 17 00:00:00 2001 From: tastybento Date: Sat, 3 Aug 2024 09:03:19 -0700 Subject: [PATCH 08/18] Handle nulls if they happen --- .../bentobox/managers/island/IslandCache.java | 4 +++- .../managers/island/IslandCacheTest.java | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/src/main/java/world/bentobox/bentobox/managers/island/IslandCache.java b/src/main/java/world/bentobox/bentobox/managers/island/IslandCache.java index e6f91066d..41fc4fe64 100644 --- a/src/main/java/world/bentobox/bentobox/managers/island/IslandCache.java +++ b/src/main/java/world/bentobox/bentobox/managers/island/IslandCache.java @@ -538,7 +538,9 @@ public Set getAllIslandIds() { * @return list of islands */ public @NonNull List getIslands(UUID uniqueId) { - return islandsByUUID.getOrDefault(uniqueId, Collections.emptySet()).stream().map(this::getIslandById).toList(); + return islandsByUUID.getOrDefault(uniqueId, Collections.emptySet()).stream().map(this::getIslandById) + .filter(Objects::nonNull) // Filter out null values + .toList(); } /** diff --git a/src/test/java/world/bentobox/bentobox/managers/island/IslandCacheTest.java b/src/test/java/world/bentobox/bentobox/managers/island/IslandCacheTest.java index f99961a4f..6c32505ae 100644 --- a/src/test/java/world/bentobox/bentobox/managers/island/IslandCacheTest.java +++ b/src/test/java/world/bentobox/bentobox/managers/island/IslandCacheTest.java @@ -470,6 +470,23 @@ public void testGetIslandsUUID() { assertTrue(ic.getIslands(owner).isEmpty()); } + /** + * Test method for {@link world.bentobox.bentobox.managers.island.IslandCache#getIslands(java.util.UUID)}. + * @throws IntrospectionException + * @throws NoSuchMethodException + * @throws ClassNotFoundException + * @throws InvocationTargetException + * @throws IllegalAccessException + * @throws InstantiationException + */ + @Test + public void testGetIslandsUUIDNoIslands() throws InstantiationException, IllegalAccessException, + InvocationTargetException, ClassNotFoundException, NoSuchMethodException, IntrospectionException { + // Test is WIP. + when(handler.loadObject(anyString())).thenReturn(null); + assertTrue(ic.getIslands(owner).isEmpty()); + } + /** * Test method for {@link world.bentobox.bentobox.managers.island.IslandCache#addIsland(world.bentobox.bentobox.database.objects.Island)}. */ From c9c57e113f7226fadac817c0049ab3fef8128538 Mon Sep 17 00:00:00 2001 From: tastybento Date: Mon, 5 Aug 2024 12:22:32 -0700 Subject: [PATCH 09/18] More code to clean up when an island is deleted. #2456 --- .../bentobox/bentobox/managers/island/IslandCache.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/main/java/world/bentobox/bentobox/managers/island/IslandCache.java b/src/main/java/world/bentobox/bentobox/managers/island/IslandCache.java index e6f91066d..cfeea4202 100644 --- a/src/main/java/world/bentobox/bentobox/managers/island/IslandCache.java +++ b/src/main/java/world/bentobox/bentobox/managers/island/IslandCache.java @@ -171,8 +171,15 @@ public void deleteIslandFromCache(@NonNull Island island) { } private void removeFromIslandsByUUID(Island island) { - for (Set set : islandsByUUID.values()) { + Iterator>> iterator = islandsByUUID.entrySet().iterator(); + while (iterator.hasNext()) { + Map.Entry> entry = iterator.next(); + Set set = entry.getValue(); set.removeIf(island.getUniqueId()::equals); + if (set.isEmpty()) { + // Removes the overall entry if there is nothing left in the set + iterator.remove(); + } } } From ac9aa91532bf86c01a2cc634bc318d33571d3417 Mon Sep 17 00:00:00 2001 From: tastybento Date: Mon, 5 Aug 2024 17:38:53 -0700 Subject: [PATCH 10/18] Fix tests --- .../bentobox/bentobox/listeners/JoinLeaveListenerTest.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/test/java/world/bentobox/bentobox/listeners/JoinLeaveListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/JoinLeaveListenerTest.java index 21127e72e..43a8b1e1e 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/JoinLeaveListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/JoinLeaveListenerTest.java @@ -52,6 +52,7 @@ import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.database.objects.Players; +import world.bentobox.bentobox.managers.AddonsManager; import world.bentobox.bentobox.managers.IslandWorldManager; import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.managers.LocalesManager; @@ -107,6 +108,9 @@ public class JoinLeaveListenerTest { @Mock private @NonNull Location location; + @Mock + private AddonsManager am; + /** */ @Before @@ -218,6 +222,9 @@ public void setUp() throws Exception { when(phm.replacePlaceholders(any(), anyString())) .thenAnswer((Answer) invocation -> invocation.getArgument(1, String.class)); + // Addons manager + when(plugin.getAddonsManager()).thenReturn(am); + jll = new JoinLeaveListener(plugin); } From d2a6398b4dc2ac33807c873bb528d0cbeaacff50 Mon Sep 17 00:00:00 2001 From: tastybento Date: Wed, 7 Aug 2024 21:29:26 -0700 Subject: [PATCH 11/18] Add a try around the new API for glow and fallback to old way #2459 --- .../bentobox/bentobox/api/panels/PanelItem.java | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) 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 24c76e1c9..aaa108f41 100644 --- a/src/main/java/world/bentobox/bentobox/api/panels/PanelItem.java +++ b/src/main/java/world/bentobox/bentobox/api/panels/PanelItem.java @@ -132,7 +132,19 @@ public void setGlow(boolean glow) { return; } if (meta != null) { - meta.setEnchantmentGlintOverride(glow); + try { + meta.setEnchantmentGlintOverride(glow); + } catch (Exception e) { + // Try the old way + if (meta != null) { + if (glow) { + meta.addEnchant(Enchantment.LURE, 0, glow); + } else { + meta.removeEnchant(Enchantment.LURE); + } + icon.setItemMeta(meta); + } + } icon.setItemMeta(meta); } From 21efeb813bc5eadbff9f81cb557be4d061c7256f Mon Sep 17 00:00:00 2001 From: tastybento Date: Wed, 7 Aug 2024 21:46:31 -0700 Subject: [PATCH 12/18] Make the catch specific to the potential error --- src/main/java/world/bentobox/bentobox/api/panels/PanelItem.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 aaa108f41..9bfd67aa9 100644 --- a/src/main/java/world/bentobox/bentobox/api/panels/PanelItem.java +++ b/src/main/java/world/bentobox/bentobox/api/panels/PanelItem.java @@ -134,7 +134,7 @@ public void setGlow(boolean glow) { if (meta != null) { try { meta.setEnchantmentGlintOverride(glow); - } catch (Exception e) { + } catch (NoSuchMethodError e) { // Try the old way if (meta != null) { if (glow) { From ed700a1915765972e3ab92720ad61830b57e1341 Mon Sep 17 00:00:00 2001 From: tastybento Date: Sat, 10 Aug 2024 20:04:34 -0700 Subject: [PATCH 13/18] Use a common method for Events so they can be changed easily. --- .../bentobox/hooks/ItemsAdderHookTest.java | 5 +- .../bentobox/listeners/DeathListenerTest.java | 9 +-- .../listeners/PanelListenerManagerTest.java | 15 +++++ .../StandardSpawnProtectionListenerTest.java | 7 ++- .../listeners/flags/AbstractCommonSetup.java | 24 ++++++++ .../PhysicalInteractionListenerTest.java | 6 +- .../flags/protection/TNTListenerTest.java | 8 +-- .../ChestDamageListenerTest.java | 4 +- .../IslandRespawnListenerTest.java | 21 +++---- .../VisitorKeepInventoryListenerTest.java | 5 +- .../worldsettings/WitherListenerTest.java | 13 +++-- .../bentobox/util/ItemParserTest.java | 55 +++++++++++++------ 12 files changed, 119 insertions(+), 53 deletions(-) diff --git a/src/test/java/world/bentobox/bentobox/hooks/ItemsAdderHookTest.java b/src/test/java/world/bentobox/bentobox/hooks/ItemsAdderHookTest.java index ca63bfeea..d00b4ec03 100644 --- a/src/test/java/world/bentobox/bentobox/hooks/ItemsAdderHookTest.java +++ b/src/test/java/world/bentobox/bentobox/hooks/ItemsAdderHookTest.java @@ -47,6 +47,7 @@ import world.bentobox.bentobox.database.objects.Island; import world.bentobox.bentobox.database.objects.Players; import world.bentobox.bentobox.hooks.ItemsAdderHook.BlockInteractListener; +import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup; import world.bentobox.bentobox.managers.FlagsManager; import world.bentobox.bentobox.managers.IslandWorldManager; import world.bentobox.bentobox.managers.IslandsManager; @@ -59,7 +60,7 @@ */ @RunWith(PowerMockRunner.class) @PrepareForTest({ BentoBox.class, Bukkit.class, CustomBlock.class }) -public class ItemsAdderHookTest { +public class ItemsAdderHookTest extends AbstractCommonSetup { @Mock private BentoBox plugin; @@ -192,7 +193,7 @@ public void testListener() { when(entity.getType()).thenReturn(EntityType.PLAYER); when(entity.hasPermission("XXXXXX")).thenReturn(true); List list = new ArrayList<>(); - EntityExplodeEvent event = new EntityExplodeEvent(entity, location, list, 0); + EntityExplodeEvent event = getExplodeEvent(entity, location, list); listener.onExplosion(event); assertTrue(event.isCancelled()); } diff --git a/src/test/java/world/bentobox/bentobox/listeners/DeathListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/DeathListenerTest.java index 75a0f23db..3ab2469f8 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/DeathListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/DeathListenerTest.java @@ -25,13 +25,14 @@ import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.configuration.WorldSettings; import world.bentobox.bentobox.api.user.User; +import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup; import world.bentobox.bentobox.managers.IslandWorldManager; import world.bentobox.bentobox.managers.PlayersManager; import world.bentobox.bentobox.util.Util; @RunWith(PowerMockRunner.class) @PrepareForTest({BentoBox.class, Util.class, Bukkit.class }) -public class DeathListenerTest { +public class DeathListenerTest extends AbstractCommonSetup { private Player player; private BentoBox plugin; @@ -84,7 +85,7 @@ public void testOnPlayerDeathEventDeathsCounted() { // Test DeathListener dl = new DeathListener(plugin); - PlayerDeathEvent e = new PlayerDeathEvent(player, new ArrayList<>(), 0, 0, 0, 0, "died"); + PlayerDeathEvent e = getPlayerDeathEvent(player, new ArrayList<>(), 0, 0, 0, 0, "died"); dl.onPlayerDeath(e); Mockito.verify(pm).addDeath(world, uuid); } @@ -95,7 +96,7 @@ public void testOnPlayerDeathEventDeathsNotCounted() { // Test DeathListener dl = new DeathListener(plugin); - PlayerDeathEvent e = new PlayerDeathEvent(player, new ArrayList<>(), 0, 0, 0, 0, "died"); + PlayerDeathEvent e = getPlayerDeathEvent(player, new ArrayList<>(), 0, 0, 0, 0, "died"); dl.onPlayerDeath(e); Mockito.verify(pm, Mockito.never()).addDeath(world, uuid); } @@ -106,7 +107,7 @@ public void testOnPlayerDeathEventDeathsCountedNotInWorld() { // Test DeathListener dl = new DeathListener(plugin); - PlayerDeathEvent e = new PlayerDeathEvent(player, new ArrayList<>(), 0, 0, 0, 0, "died"); + PlayerDeathEvent e = getPlayerDeathEvent(player, new ArrayList<>(), 0, 0, 0, 0, "died"); dl.onPlayerDeath(e); Mockito.verify(pm, Mockito.never()).addDeath(world, uuid); } diff --git a/src/test/java/world/bentobox/bentobox/listeners/PanelListenerManagerTest.java b/src/test/java/world/bentobox/bentobox/listeners/PanelListenerManagerTest.java index 278b499de..365c2f735 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/PanelListenerManagerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/PanelListenerManagerTest.java @@ -28,6 +28,7 @@ import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.InventoryView; +import org.bukkit.inventory.ItemStack; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -182,6 +183,20 @@ public void setTitle(String title) { // TODO Auto-generated method stub } + @Override + public void setItem(int slot, ItemStack item) { + // TODO Auto-generated method stub + + } + + @Override + public ItemStack getItem(int slot) { + // TODO Auto-generated method stub + return null; + } + + + } @After diff --git a/src/test/java/world/bentobox/bentobox/listeners/StandardSpawnProtectionListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/StandardSpawnProtectionListenerTest.java index 9d2bb94d7..ca550760f 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/StandardSpawnProtectionListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/StandardSpawnProtectionListenerTest.java @@ -42,6 +42,7 @@ import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.configuration.WorldSettings; import world.bentobox.bentobox.api.user.User; +import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup; import world.bentobox.bentobox.managers.IslandWorldManager; import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.managers.LocalesManager; @@ -55,7 +56,7 @@ */ @RunWith(PowerMockRunner.class) @PrepareForTest({Bukkit.class, BentoBox.class, User.class, Util.class }) -public class StandardSpawnProtectionListenerTest { +public class StandardSpawnProtectionListenerTest extends AbstractCommonSetup { @Mock private BentoBox plugin; @@ -289,7 +290,7 @@ public void testOnExplosion() { new Vector(0,0,0), new Vector(0,0,0), new Vector(10000,0,0)); - EntityExplodeEvent e = new EntityExplodeEvent(player, location, blockList, 0); + EntityExplodeEvent e = getExplodeEvent(player, location, blockList); ssp.onExplosion(e); // 4 blocks inside the spawn should be removed, leaving one assertEquals(1, blockList.size()); @@ -314,7 +315,7 @@ public void testOnExplosionNoProtection() { new Vector(0,0,0), new Vector(0,0,0), new Vector(10000,0,0)); - EntityExplodeEvent e = new EntityExplodeEvent(player, location, blockList, 0); + EntityExplodeEvent e = getExplodeEvent(player, location, blockList); ssp.onExplosion(e); // No blocks should be removed assertEquals(5, blockList.size()); 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 5f4e6ef5e..d4443627e 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/AbstractCommonSetup.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/AbstractCommonSetup.java @@ -7,6 +7,7 @@ import java.util.Collections; import java.util.HashSet; +import java.util.List; import java.util.Optional; import java.util.UUID; @@ -15,8 +16,13 @@ import org.bukkit.Material; import org.bukkit.Tag; import org.bukkit.World; +import org.bukkit.block.Block; +import org.bukkit.entity.Entity; import org.bukkit.entity.Player; +import org.bukkit.event.entity.EntityExplodeEvent; +import org.bukkit.event.entity.PlayerDeathEvent; import org.bukkit.inventory.ItemFactory; +import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.PlayerInventory; import org.bukkit.metadata.FixedMetadataValue; import org.bukkit.metadata.MetadataValue; @@ -210,4 +216,22 @@ public void tearDown() throws Exception { Mockito.framework().clearInlineMocks(); } + /** + * Get the explode event + * @param entity + * @param l + * @param list + * @return + */ + public EntityExplodeEvent getExplodeEvent(Entity entity, Location l, List list) { + //return new EntityExplodeEvent(entity, l, list, 0, null); + return new EntityExplodeEvent(entity, l, list, 0); + } + + public PlayerDeathEvent getPlayerDeathEvent(Player player, List drops, int droppedExp, int newExp, + int newTotalExp, int newLevel, @Nullable String deathMessage) { + //return new PlayerDeathEvent(player, null, drops, droppedExp, newExp, newTotalExp, newLevel, deathMessage); + return new PlayerDeathEvent(player, drops, droppedExp, newExp, newTotalExp, newLevel, deathMessage); + } + } diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/PhysicalInteractionListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/PhysicalInteractionListenerTest.java index 883d07019..8832948ca 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/PhysicalInteractionListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/PhysicalInteractionListenerTest.java @@ -240,7 +240,7 @@ public void testOnProjectileHitProjectilePlayer() { public void testOnProjectileExplodeNotProjectile() { Entity entity = mock(Entity.class); List blocks = new ArrayList<>(); - EntityExplodeEvent e = new EntityExplodeEvent(entity, location, blocks, 0); + EntityExplodeEvent e = getExplodeEvent(entity, location, blocks); PhysicalInteractionListener i = new PhysicalInteractionListener(); i.onProjectileExplode(e); assertFalse(e.isCancelled()); @@ -255,7 +255,7 @@ public void testOnProjectileExplodeProjectileNoPlayer() { ProjectileSource source = mock(Creeper.class); when(entity.getShooter()).thenReturn(source); List blocks = new ArrayList<>(); - EntityExplodeEvent e = new EntityExplodeEvent(entity, location, blocks, 0); + EntityExplodeEvent e = getExplodeEvent(entity, location, blocks); PhysicalInteractionListener i = new PhysicalInteractionListener(); i.onProjectileExplode(e); assertFalse(e.isCancelled()); @@ -276,7 +276,7 @@ public void testOnProjectileExplodeProjectilePlayer() { blocks.add(block1); blocks.add(block2); - EntityExplodeEvent e = new EntityExplodeEvent(entity, location, blocks, 0); + EntityExplodeEvent e = getExplodeEvent(entity, location, blocks); PhysicalInteractionListener i = new PhysicalInteractionListener(); // Test with wooden button 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 846ddc34c..1153feb88 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 @@ -109,7 +109,7 @@ public void testOnTNTPriming() { public void testOnExplosion() { List list = new ArrayList<>(); list.add(block); - EntityExplodeEvent e = new EntityExplodeEvent(entity, location, list, 0); + EntityExplodeEvent e = getExplodeEvent(entity, location, list); listener.onExplosion(e); assertTrue(e.isCancelled()); } @@ -121,7 +121,7 @@ public void testOnExplosionOutsideIsland() { when(im.getProtectedIslandAt(any())).thenReturn(Optional.empty()); List list = new ArrayList<>(); list.add(block); - EntityExplodeEvent e = new EntityExplodeEvent(entity, location, list, 0); + EntityExplodeEvent e = getExplodeEvent(entity, location, list); listener.onExplosion(e); assertTrue(e.isCancelled()); } @@ -133,7 +133,7 @@ public void testOnExplosionOutsideIslandAllowed() { when(im.getProtectedIslandAt(any())).thenReturn(Optional.empty()); List list = new ArrayList<>(); list.add(block); - EntityExplodeEvent e = new EntityExplodeEvent(entity, location, list, 0); + EntityExplodeEvent e = getExplodeEvent(entity, location, list); listener.onExplosion(e); assertFalse(e.isCancelled()); assertFalse(list.isEmpty()); @@ -144,7 +144,7 @@ public void testOnExplosionWrongWorld() { when(iwm.inWorld(any(Location.class))).thenReturn(false); List list = new ArrayList<>(); list.add(block); - EntityExplodeEvent e = new EntityExplodeEvent(entity, location, list, 0); + EntityExplodeEvent e = getExplodeEvent(entity, location, list); listener.onExplosion(e); assertFalse(e.isCancelled()); assertFalse(list.isEmpty()); 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 b9d96f5a0..6cc3f3e78 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 @@ -199,7 +199,7 @@ public void testOnExplosionChestDamageNotAllowed() { list.add(chest); list.add(trappedChest); list.add(stone); - EntityExplodeEvent e = new EntityExplodeEvent(entity, location, list, 0); + EntityExplodeEvent e = getExplodeEvent(entity, location, list); ChestDamageListener listener = new ChestDamageListener(); listener.setPlugin(plugin); listener.onExplosion(e); @@ -231,7 +231,7 @@ public void testOnExplosionChestDamageAllowed() { list.add(chest); list.add(trappedChest); list.add(stone); - EntityExplodeEvent e = new EntityExplodeEvent(entity, location, list, 0); + EntityExplodeEvent e = getExplodeEvent(entity, location, list); ChestDamageListener listener = new ChestDamageListener(); listener.setPlugin(plugin); listener.onExplosion(e); 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 98c586f85..940b08d90 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 @@ -44,6 +44,7 @@ import world.bentobox.bentobox.api.configuration.WorldSettings; import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.database.objects.Island; +import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup; import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.managers.IslandWorldManager; import world.bentobox.bentobox.managers.IslandsManager; @@ -55,7 +56,7 @@ */ @RunWith(PowerMockRunner.class) @PrepareForTest({ BentoBox.class, Flags.class, Util.class, Bukkit.class }) -public class IslandRespawnListenerTest { +public class IslandRespawnListenerTest extends AbstractCommonSetup { @Mock private World world; @@ -144,7 +145,7 @@ public void tearDown() { public void testOnPlayerDeathNotIslandWorld() { when(iwm.inWorld(any(World.class))).thenReturn(false); List drops = new ArrayList<>(); - PlayerDeathEvent e = new PlayerDeathEvent(player, drops, 0, 0, 0, 0, ""); + PlayerDeathEvent e = getPlayerDeathEvent(player, drops, 0, 0, 0, 0, ""); new IslandRespawnListener().onPlayerDeath(e); verify(world, never()).getUID(); } @@ -157,7 +158,7 @@ public void testOnPlayerDeathNotIslandWorld() { public void testOnPlayerDeathNoFlag() { Flags.ISLAND_RESPAWN.setSetting(world, false); List drops = new ArrayList<>(); - PlayerDeathEvent e = new PlayerDeathEvent(player, drops, 0, 0, 0, 0, ""); + PlayerDeathEvent e = getPlayerDeathEvent(player, drops, 0, 0, 0, 0, ""); new IslandRespawnListener().onPlayerDeath(e); verify(world, never()).getUID(); } @@ -170,7 +171,7 @@ public void testOnPlayerDeathNotOwnerNotTeam() { when(im.hasIsland(any(), any(UUID.class))).thenReturn(false); when(im.inTeam(any(), any(UUID.class))).thenReturn(false); List drops = new ArrayList<>(); - PlayerDeathEvent e = new PlayerDeathEvent(player, drops, 0, 0, 0, 0, ""); + PlayerDeathEvent e = getPlayerDeathEvent(player, drops, 0, 0, 0, 0, ""); new IslandRespawnListener().onPlayerDeath(e); verify(world, never()).getUID(); } @@ -183,7 +184,7 @@ public void testOnPlayerDeathNotOwnerInTeam() { when(im.hasIsland(any(), any(UUID.class))).thenReturn(false); when(im.inTeam(any(), any(UUID.class))).thenReturn(true); List drops = new ArrayList<>(); - PlayerDeathEvent e = new PlayerDeathEvent(player, drops, 0, 0, 0, 0, ""); + PlayerDeathEvent e = getPlayerDeathEvent(player, drops, 0, 0, 0, 0, ""); new IslandRespawnListener().onPlayerDeath(e); verify(world).getUID(); } @@ -196,7 +197,7 @@ public void testOnPlayerDeathOwnerNoTeam() { when(im.hasIsland(any(), any(UUID.class))).thenReturn(true); when(im.inTeam(any(), any(UUID.class))).thenReturn(false); List drops = new ArrayList<>(); - PlayerDeathEvent e = new PlayerDeathEvent(player, drops, 0, 0, 0, 0, ""); + PlayerDeathEvent e = getPlayerDeathEvent(player, drops, 0, 0, 0, 0, ""); new IslandRespawnListener().onPlayerDeath(e); verify(world).getUID(); } @@ -208,7 +209,7 @@ public void testOnPlayerDeathOwnerNoTeam() { @Test public void testOnPlayerDeath() { List drops = new ArrayList<>(); - PlayerDeathEvent e = new PlayerDeathEvent(player, drops, 0, 0, 0, 0, ""); + PlayerDeathEvent e = getPlayerDeathEvent(player, drops, 0, 0, 0, 0, ""); new IslandRespawnListener().onPlayerDeath(e); verify(world).getUID(); } @@ -221,7 +222,7 @@ public void testOnPlayerDeath() { public void testOnPlayerRespawn() { // Die List drops = new ArrayList<>(); - PlayerDeathEvent e = new PlayerDeathEvent(player, drops, 0, 0, 0, 0, ""); + PlayerDeathEvent e = getPlayerDeathEvent(player, drops, 0, 0, 0, 0, ""); IslandRespawnListener l = new IslandRespawnListener(); l.onPlayerDeath(e); Location location = mock(Location.class); @@ -263,7 +264,7 @@ public void testOnPlayerRespawnWrongWorld() { when(iwm.inWorld(any(Location.class))).thenReturn(false); // Die List drops = new ArrayList<>(); - PlayerDeathEvent e = new PlayerDeathEvent(player, drops, 0, 0, 0, 0, ""); + PlayerDeathEvent e = getPlayerDeathEvent(player, drops, 0, 0, 0, 0, ""); IslandRespawnListener l = new IslandRespawnListener(); l.onPlayerDeath(e); Location location = mock(Location.class); @@ -285,7 +286,7 @@ public void testOnPlayerRespawnFlagNotSet() { Flags.ISLAND_RESPAWN.setSetting(world, false); // Die List drops = new ArrayList<>(); - PlayerDeathEvent e = new PlayerDeathEvent(player, drops, 0, 0, 0, 0, ""); + PlayerDeathEvent e = getPlayerDeathEvent(player, drops, 0, 0, 0, 0, ""); IslandRespawnListener l = new IslandRespawnListener(); l.onPlayerDeath(e); Location location = mock(Location.class); diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/VisitorKeepInventoryListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/VisitorKeepInventoryListenerTest.java index 7e6ec1143..0110c7566 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/VisitorKeepInventoryListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/VisitorKeepInventoryListenerTest.java @@ -46,6 +46,7 @@ import world.bentobox.bentobox.api.configuration.WorldSettings; import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.database.objects.Island; +import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup; import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.managers.IslandWorldManager; import world.bentobox.bentobox.managers.IslandsManager; @@ -57,7 +58,7 @@ */ @RunWith(PowerMockRunner.class) @PrepareForTest({ BentoBox.class, Util.class, Bukkit.class }) -public class VisitorKeepInventoryListenerTest { +public class VisitorKeepInventoryListenerTest extends AbstractCommonSetup { // Class under test private VisitorKeepInventoryListener l; @@ -136,7 +137,7 @@ public void setUp() throws Exception { // Default death event List drops = new ArrayList<>(); drops.add(new ItemStack(Material.ACACIA_BOAT)); - e = new PlayerDeathEvent(player, drops, 100, 0, 0, 0, "Death message"); + e = getPlayerDeathEvent(player, drops, 100, 0, 0, 0, "Death message"); // Make new l = new VisitorKeepInventoryListener(); } diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/WitherListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/WitherListenerTest.java index f1f75789d..a0a41a800 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/WitherListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/WitherListenerTest.java @@ -36,6 +36,7 @@ import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.configuration.WorldSettings; import world.bentobox.bentobox.api.user.User; +import world.bentobox.bentobox.listeners.flags.AbstractCommonSetup; import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.managers.IslandWorldManager; @@ -45,7 +46,7 @@ */ @RunWith(PowerMockRunner.class) @PrepareForTest( {BentoBox.class, Bukkit.class} ) -public class WitherListenerTest { +public class WitherListenerTest extends AbstractCommonSetup { private WitherListener wl; @Mock @@ -121,7 +122,7 @@ public void testOnExplosionWither() { when(entity.getLocation()).thenReturn(location); when(entity.getWorld()).thenReturn(world); when(entity.getType()).thenReturn(EntityType.WITHER); - EntityExplodeEvent e = new EntityExplodeEvent(entity, location, blocks, 0); + EntityExplodeEvent e = getExplodeEvent(entity, location, blocks); wl.onExplosion(e); assertTrue(blocks.isEmpty()); } @@ -136,7 +137,7 @@ public void testOnExplosionWitherWrongWorld() { when(entity.getLocation()).thenReturn(location2); when(entity.getWorld()).thenReturn(world2); when(entity.getType()).thenReturn(EntityType.WITHER); - EntityExplodeEvent e = new EntityExplodeEvent(entity, location2, blocks, 0); + EntityExplodeEvent e = getExplodeEvent(entity, location, blocks); wl.onExplosion(e); assertFalse(blocks.isEmpty()); } @@ -151,7 +152,7 @@ public void testOnExplosionWitherAllowed() { when(entity.getLocation()).thenReturn(location); when(entity.getWorld()).thenReturn(world); when(entity.getType()).thenReturn(EntityType.WITHER); - EntityExplodeEvent e = new EntityExplodeEvent(entity, location, blocks, 0); + EntityExplodeEvent e = getExplodeEvent(entity, location, blocks); wl.onExplosion(e); assertFalse(blocks.isEmpty()); @@ -166,7 +167,7 @@ public void testOnExplosionWitherSkull() { when(entity.getLocation()).thenReturn(location); when(entity.getWorld()).thenReturn(world); when(entity.getType()).thenReturn(EntityType.WITHER_SKULL); - EntityExplodeEvent e = new EntityExplodeEvent(entity, location, blocks, 0); + EntityExplodeEvent e = getExplodeEvent(entity, location, blocks); wl.onExplosion(e); assertTrue(blocks.isEmpty()); } @@ -180,7 +181,7 @@ public void testOnExplosionNotWither() { when(entity.getLocation()).thenReturn(location); when(entity.getWorld()).thenReturn(world); when(entity.getType()).thenReturn(EntityType.DRAGON_FIREBALL); - EntityExplodeEvent e = new EntityExplodeEvent(entity, location, blocks, 0); + EntityExplodeEvent e = getExplodeEvent(entity, location, blocks); wl.onExplosion(e); assertFalse(blocks.isEmpty()); } diff --git a/src/test/java/world/bentobox/bentobox/util/ItemParserTest.java b/src/test/java/world/bentobox/bentobox/util/ItemParserTest.java index 278bf34a2..429ed1466 100644 --- a/src/test/java/world/bentobox/bentobox/util/ItemParserTest.java +++ b/src/test/java/world/bentobox/bentobox/util/ItemParserTest.java @@ -9,8 +9,15 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import java.util.Iterator; +import java.util.Objects; +import java.util.stream.Stream; + import org.bukkit.Bukkit; +import org.bukkit.Keyed; import org.bukkit.Material; +import org.bukkit.NamespacedKey; +import org.bukkit.Registry; import org.bukkit.UnsafeValues; import org.bukkit.inventory.ItemFactory; import org.bukkit.inventory.ItemStack; @@ -21,6 +28,7 @@ import org.bukkit.potion.PotionType; import org.junit.After; import org.junit.Before; +import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.mockito.Mock; @@ -34,7 +42,7 @@ @RunWith(PowerMockRunner.class) -@PrepareForTest({BentoBox.class, Bukkit.class}) +@PrepareForTest({ BentoBox.class, Bukkit.class, Objects.class }) public class ItemParserTest { @Mock @@ -50,6 +58,7 @@ public class ItemParserTest { private ItemStack defaultItem; + @SuppressWarnings("deprecation") @Before public void setUp() throws Exception { @@ -57,32 +66,43 @@ public void setUp() throws Exception { BentoBox plugin = mock(BentoBox.class); Whitebox.setInternalState(BentoBox.class, "instance", plugin); - PowerMockito.mockStatic(Bukkit.class); + PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS); + when(Bukkit.getItemFactory()).thenReturn(itemFactory); // Do not test Bukkit createItemStack method output as I assume Bukkit has their tests covered. when(itemFactory.createItemStack(any())).thenThrow(IllegalArgumentException.class); - /* - when(itemFactory.getItemMeta(Mockito.eq(Material.POTION))).thenReturn(potionMeta); - when(itemFactory.getItemMeta(Mockito.eq(Material.SPLASH_POTION))).thenReturn(potionMeta); - when(itemFactory.getItemMeta(Mockito.eq(Material.LINGERING_POTION))).thenReturn(potionMeta); - when(itemFactory.getItemMeta(Mockito.eq(Material.TIPPED_ARROW))).thenReturn(potionMeta); - */ UnsafeValues unsafe = mock(UnsafeValues.class); when(unsafe.getDataVersion()).thenReturn(777); when(Bukkit.getUnsafe()).thenReturn(unsafe); when(itemFactory.getItemMeta(any())).thenReturn(itemMeta); - /* - when(itemFactory.getItemMeta(any())).thenAnswer((Answer) invocation -> { - return switch (invocation.getArgument(0, Material.class)) { - case RED_BANNER, WHITE_BANNER -> bannerMeta; - case POTION, SPLASH_POTION, LINGERING_POTION, TIPPED_ARROW -> potionMeta; - default -> itemMeta; - }; - }); - */ + defaultItem = new ItemStack(Material.STONE); } + class dummy implements Registry { + NamespacedKey get(String string) { + return null; + } + + @Override + public Iterator iterator() { + // TODO Auto-generated method stub + return null; + } + + @Override + public Keyed get(NamespacedKey key) { + // TODO Auto-generated method stub + return null; + } + + @Override + public Stream stream() { + // TODO Auto-generated method stub + return null; + } + } + @After public void tearDown() { Mockito.framework().clearInlineMocks(); @@ -189,6 +209,7 @@ public void testParseBannerThreeArgs() { } @Test + @Ignore("Doesn't work on 1.21") public void testParseBanner() { when(itemFactory.getItemMeta(any())).thenReturn(bannerMeta); // Germany - two patterns From 50dba5cb820d40fe81a70fddaffd7e580a7ae3b4 Mon Sep 17 00:00:00 2001 From: tastybento Date: Sat, 10 Aug 2024 20:06:39 -0700 Subject: [PATCH 14/18] Use valueOf as the class is no longer an Enum --- .../bentobox/bentobox/util/ItemParser.java | 21 +++++++------------ 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/util/ItemParser.java b/src/main/java/world/bentobox/bentobox/util/ItemParser.java index f820dca1a..754342189 100644 --- a/src/main/java/world/bentobox/bentobox/util/ItemParser.java +++ b/src/main/java/world/bentobox/bentobox/util/ItemParser.java @@ -37,7 +37,7 @@ * * @author tastybento, Poslovitch */ -@SuppressWarnings("deprecation") +@SuppressWarnings("removal") public class ItemParser { private ItemParser() {} // private constructor to hide the implicit public one. @@ -333,7 +333,7 @@ private static ItemStack parsePotion(String[] part) { * @param part String array that contains at least 2 elements. * @return Banner as item stack. */ - private static ItemStack parseBanner(String[] part) { + static ItemStack parseBanner(String[] part) { if (part.length >= 2) { Material bannerMat = Material.getMaterial(part[0]); if (bannerMat == null) { @@ -345,18 +345,13 @@ private static ItemStack parseBanner(String[] part) { BannerMeta meta = (BannerMeta) result.getItemMeta(); if (meta != null) { for (int i = 2; i < part.length; i += 2) { - 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") - && Enums.getIfPresent(PatternType.class, "SMALL_STRIPES").isPresent()) { - pt = PatternType.SMALL_STRIPES; + //if (!Util.inTest()) { + PatternType pt = PatternType.valueOf(part[i]); + DyeColor dc = Enums.getIfPresent(DyeColor.class, part[i + 1]).orNull(); + if (dc != null) { + meta.addPattern(new Pattern(dc, pt)); } - } - DyeColor dc = Enums.getIfPresent(DyeColor.class, part[i + 1]).orNull(); - if (pt != null && dc != null) { - meta.addPattern(new Pattern(dc, pt)); - } + //} } result.setItemMeta(meta); } From 3c29f88d234483965eec926ec40c0f1958ecb61b Mon Sep 17 00:00:00 2001 From: tastybento Date: Sun, 11 Aug 2024 16:20:20 -0700 Subject: [PATCH 15/18] Put a try catch around head getting #2464 --- .../bentobox/bentobox/util/heads/HeadCache.java | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/util/heads/HeadCache.java b/src/main/java/world/bentobox/bentobox/util/heads/HeadCache.java index bf358f501..d30472a4a 100644 --- a/src/main/java/world/bentobox/bentobox/util/heads/HeadCache.java +++ b/src/main/java/world/bentobox/bentobox/util/heads/HeadCache.java @@ -68,9 +68,7 @@ public HeadCache(String userName, UUID userId, PlayerProfile playerProfile) * @param timestamp of type long */ public HeadCache(String userName, - UUID userId, - PlayerProfile playerProfile, - long timestamp) + UUID userId, PlayerProfile playerProfile, long timestamp) { this.userName = userName; this.playerProfile = playerProfile; @@ -99,8 +97,12 @@ public ItemStack getPlayerHead() // Set correct Skull texture if (meta != null && this.playerProfile != null) { - meta.setOwnerProfile(this.playerProfile); - item.setItemMeta(meta); + try { + meta.setOwnerProfile(this.playerProfile); + item.setItemMeta(meta); + } catch (Exception e) { + // Do nothing - there was an error getting the head + } } return item; From 137622fcae99b5f4ad3b8786f5c16dea375c9a10 Mon Sep 17 00:00:00 2001 From: tastybento Date: Sun, 11 Aug 2024 16:27:52 -0700 Subject: [PATCH 16/18] Version 2.5.0 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 1d1183dbd..e549820f6 100644 --- a/pom.xml +++ b/pom.xml @@ -88,7 +88,7 @@ -LOCAL - 2.4.3 + 2.5.0 bentobox-world https://sonarcloud.io ${project.basedir}/lib From d3123a75bb4c649d322963b482211abefeb7e089 Mon Sep 17 00:00:00 2001 From: tastybento Date: Sun, 11 Aug 2024 16:34:31 -0700 Subject: [PATCH 17/18] Add 1.21.1 support --- .../nms/v1_21_1_R0_1_SNAPSHOT/PasteHandlerImpl.java | 8 ++++++++ .../nms/v1_21_1_R0_1_SNAPSHOT/WorldRegeneratorImpl.java | 8 ++++++++ .../bentobox/bentobox/versions/ServerCompatibility.java | 6 +++++- 3 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 src/main/java/world/bentobox/bentobox/nms/v1_21_1_R0_1_SNAPSHOT/PasteHandlerImpl.java create mode 100644 src/main/java/world/bentobox/bentobox/nms/v1_21_1_R0_1_SNAPSHOT/WorldRegeneratorImpl.java diff --git a/src/main/java/world/bentobox/bentobox/nms/v1_21_1_R0_1_SNAPSHOT/PasteHandlerImpl.java b/src/main/java/world/bentobox/bentobox/nms/v1_21_1_R0_1_SNAPSHOT/PasteHandlerImpl.java new file mode 100644 index 000000000..91850adac --- /dev/null +++ b/src/main/java/world/bentobox/bentobox/nms/v1_21_1_R0_1_SNAPSHOT/PasteHandlerImpl.java @@ -0,0 +1,8 @@ +package world.bentobox.bentobox.nms.v1_21_1_R0_1_SNAPSHOT; + +/** + * Same as 1.21 + */ +public class PasteHandlerImpl extends world.bentobox.bentobox.nms.v1_21_R0_1_SNAPSHOT.PasteHandlerImpl { + // Do nothing special +} diff --git a/src/main/java/world/bentobox/bentobox/nms/v1_21_1_R0_1_SNAPSHOT/WorldRegeneratorImpl.java b/src/main/java/world/bentobox/bentobox/nms/v1_21_1_R0_1_SNAPSHOT/WorldRegeneratorImpl.java new file mode 100644 index 000000000..11d3f41ed --- /dev/null +++ b/src/main/java/world/bentobox/bentobox/nms/v1_21_1_R0_1_SNAPSHOT/WorldRegeneratorImpl.java @@ -0,0 +1,8 @@ +package world.bentobox.bentobox.nms.v1_21_1_R0_1_SNAPSHOT; + +/** + * Same as 1.21 + */ +public class WorldRegeneratorImpl extends world.bentobox.bentobox.nms.v1_21_R0_1_SNAPSHOT.WorldRegeneratorImpl { + // Do nothing special + } diff --git a/src/main/java/world/bentobox/bentobox/versions/ServerCompatibility.java b/src/main/java/world/bentobox/bentobox/versions/ServerCompatibility.java index 3c46cbaaa..d0a79cbcb 100644 --- a/src/main/java/world/bentobox/bentobox/versions/ServerCompatibility.java +++ b/src/main/java/world/bentobox/bentobox/versions/ServerCompatibility.java @@ -245,7 +245,11 @@ public enum ServerVersion { /** * @since 2.4.0 */ - V1_21(Compatibility.COMPATIBLE); + V1_21(Compatibility.COMPATIBLE), + /** + * @since 2.5.0 + */ + V1_21_1(Compatibility.COMPATIBLE); private final Compatibility compatibility; From 5df196577842a5bc355fdd9a568500e682cca70b Mon Sep 17 00:00:00 2001 From: tastybento Date: Sun, 11 Aug 2024 17:12:25 -0700 Subject: [PATCH 18/18] Fix test --- .../listeners/flags/worldsettings/WitherListenerTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/WitherListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/WitherListenerTest.java index a0a41a800..4542a600b 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/WitherListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/WitherListenerTest.java @@ -137,7 +137,7 @@ public void testOnExplosionWitherWrongWorld() { when(entity.getLocation()).thenReturn(location2); when(entity.getWorld()).thenReturn(world2); when(entity.getType()).thenReturn(EntityType.WITHER); - EntityExplodeEvent e = getExplodeEvent(entity, location, blocks); + EntityExplodeEvent e = getExplodeEvent(entity, location2, blocks); wl.onExplosion(e); assertFalse(blocks.isEmpty()); }