From 97f97f42947ffb18978130ff3fcb06f8d61d4f62 Mon Sep 17 00:00:00 2001 From: ohowe Date: Wed, 12 Jan 2022 16:27:37 -0700 Subject: [PATCH] Added gamemode customziation and 1.18 native --- pom.xml | 12 ++++++------ .../me/ohowe12/spectatormode/SpectatorManager.java | 14 +++++++++----- .../me/ohowe12/spectatormode/util/Messenger.java | 2 +- src/main/resources/config.yml | 3 +++ .../spectatormode/SpectatorManagerTest.java | 11 +++++++++++ .../commands/SpectatorCommandTest.java | 4 +++- .../listener/OnCommandPreprocessListenerTest.java | 2 ++ .../listener/OnGameModeChangeListenerTest.java | 2 ++ .../spectatormode/listener/OnMoveListenerTest.java | 2 ++ .../spectatormode/state/StateHolderTest.java | 6 +++++- .../ohowe12/spectatormode/testutils/TestUtils.java | 6 +++++- .../ohowe12/spectatormode/util/MessengerTest.java | 12 +++++++----- src/test/resources/configs/togglebackc.yml | 1 + 13 files changed, 57 insertions(+), 20 deletions(-) create mode 100644 src/test/resources/configs/togglebackc.yml diff --git a/pom.xml b/pom.xml index 2fc9cb7..34e9eb8 100644 --- a/pom.xml +++ b/pom.xml @@ -30,13 +30,13 @@ me.ohowe12 SpectatorMode - 2.1.9 + 2.2.0 jar SpectatorMode - 16 + 17 UTF-8 @@ -176,7 +176,7 @@ org.spigotmc spigot-api - 1.17.1-R0.1-SNAPSHOT + 1.18.1-R0.1-SNAPSHOT provided @@ -194,7 +194,7 @@ org.junit.jupiter junit-jupiter-engine - 5.8.0 + 5.8.2 test @@ -211,8 +211,8 @@ com.github.seeseemelk - MockBukkit-v1.17 - 1.10.0 + MockBukkit-v1.18 + 1.15.5 test diff --git a/src/main/java/me/ohowe12/spectatormode/SpectatorManager.java b/src/main/java/me/ohowe12/spectatormode/SpectatorManager.java index a4feb16..2dd2085 100644 --- a/src/main/java/me/ohowe12/spectatormode/SpectatorManager.java +++ b/src/main/java/me/ohowe12/spectatormode/SpectatorManager.java @@ -125,7 +125,7 @@ public void toggleToSpectator(Player target, boolean forced, boolean messagesFor } public void toggleToSurvival(Player target, boolean messagesForcedSilenced) { - plugin.getPluginLogger().debugLog("Toggling" + target.getName() + " to survival mode"); + plugin.getPluginLogger().debugLog("Toggling " + target.getName() + " to survival mode"); if (stateHolder.hasPlayer(target)) { removeSpectatorEffects(target); @@ -133,10 +133,15 @@ public void toggleToSurvival(Player target, boolean messagesForcedSilenced) { stateHolder.removePlayer(target); removeFromKicker(target); - target.setGameMode(GameMode.SURVIVAL); - stateHolder.save(); + String switchTo = plugin.getConfigManager().getString("switch-back-to"); + switch (switchTo) { + case "adventure" -> target.setGameMode(GameMode.ADVENTURE); + case "creative" -> target.setGameMode(GameMode.CREATIVE); + default -> target.setGameMode(GameMode.SURVIVAL); + } + stateHolder.save(); sendMessageIfNotSilenced(target, GameMode.SURVIVAL, messagesForcedSilenced); } else { Messenger.send(target, "not-in-state-message"); @@ -202,8 +207,7 @@ private void removeLeads(Player target) { .map(entity -> (LivingEntity) entity) .filter(LivingEntity::isLeashed) .filter(entity -> entity.getLeashHolder() instanceof Player) - .filter(entity -> entity.getLeashHolder().equals(target)) - .collect(Collectors.toList()); + .filter(entity -> entity.getLeashHolder().equals(target)).toList(); for (LivingEntity entity : leads) { entity.setLeashHolder(null); HashMap failedItems = diff --git a/src/main/java/me/ohowe12/spectatormode/util/Messenger.java b/src/main/java/me/ohowe12/spectatormode/util/Messenger.java index d609d1d..0e6b341 100644 --- a/src/main/java/me/ohowe12/spectatormode/util/Messenger.java +++ b/src/main/java/me/ohowe12/spectatormode/util/Messenger.java @@ -34,7 +34,7 @@ import java.util.Objects; -public abstract class Messenger { +public class Messenger { private static SpectatorMode plugin; diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 6cb2089..32310f6 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -96,6 +96,9 @@ spectator-ticks: -1 # if you don't want players to enter vanish when using spectator mode with /gamemode, disable watch-gamemode supervanish-hook: false +# What game mode to change back to. options: survival, creative, adventure +switch-back-to: survival + ### Message section ### #Adding /actionbar/ in front of a message, will make it appear in the actionbar instead of the chat diff --git a/src/test/java/me/ohowe12/spectatormode/SpectatorManagerTest.java b/src/test/java/me/ohowe12/spectatormode/SpectatorManagerTest.java index bee2e86..b7fdf81 100644 --- a/src/test/java/me/ohowe12/spectatormode/SpectatorManagerTest.java +++ b/src/test/java/me/ohowe12/spectatormode/SpectatorManagerTest.java @@ -32,6 +32,7 @@ import me.ohowe12.spectatormode.testutils.TestUtils; +import org.bukkit.Bukkit; import org.bukkit.GameMode; import org.bukkit.Location; import org.junit.jupiter.api.AfterEach; @@ -65,6 +66,7 @@ void setUp() { @AfterEach void tearDown() { + Bukkit.getScheduler().cancelTasks(plugin); MockBukkit.unmock(); } @@ -290,4 +292,13 @@ void togglePlayer_TogglesBefore_NotToggledAgain() { playerMock.assertGameMode(GameMode.SURVIVAL); playerMock.assertNoMoreSaid(); } + + @Test + void togglePlayer_ToggleBackCreative_ToggledToCreative() { + TestUtils.setConfigFileOfPlugin(plugin, "togglebackc.yml"); + spectatorManager.togglePlayer(playerMock); + playerMock.assertGameMode(GameMode.SPECTATOR); + spectatorManager.togglePlayer(playerMock); + playerMock.assertGameMode(GameMode.CREATIVE); + } } diff --git a/src/test/java/me/ohowe12/spectatormode/commands/SpectatorCommandTest.java b/src/test/java/me/ohowe12/spectatormode/commands/SpectatorCommandTest.java index b962428..0c07db8 100644 --- a/src/test/java/me/ohowe12/spectatormode/commands/SpectatorCommandTest.java +++ b/src/test/java/me/ohowe12/spectatormode/commands/SpectatorCommandTest.java @@ -11,6 +11,7 @@ import me.ohowe12.spectatormode.SpectatorMode; import me.ohowe12.spectatormode.testutils.TestUtils; +import org.bukkit.Bukkit; import org.bukkit.GameMode; import org.bukkit.potion.PotionEffectType; import org.junit.jupiter.api.AfterEach; @@ -40,6 +41,7 @@ void setUp() { @AfterEach void tearDown() { + Bukkit.getScheduler().cancelTasks(plugin); MockBukkit.unmock(); } @@ -171,7 +173,7 @@ void forceCommand_WithPermissionInvalidName_InvalidPlayerMessageSent() { } private void assertPermissionMessageSent() { - TestUtils.assertEqualsColored( + TestUtils.assertEqualsColoredNoPrepend( "&cI'm sorry, but you do not have permission to perform this command.", playerMock.nextMessage()); } diff --git a/src/test/java/me/ohowe12/spectatormode/listener/OnCommandPreprocessListenerTest.java b/src/test/java/me/ohowe12/spectatormode/listener/OnCommandPreprocessListenerTest.java index c667c0f..af3b0cc 100644 --- a/src/test/java/me/ohowe12/spectatormode/listener/OnCommandPreprocessListenerTest.java +++ b/src/test/java/me/ohowe12/spectatormode/listener/OnCommandPreprocessListenerTest.java @@ -13,6 +13,7 @@ import me.ohowe12.spectatormode.SpectatorMode; import me.ohowe12.spectatormode.testutils.TestUtils; +import org.bukkit.Bukkit; import org.bukkit.GameMode; import org.bukkit.event.player.PlayerCommandPreprocessEvent; import org.junit.jupiter.api.AfterEach; @@ -51,6 +52,7 @@ void setUp() { @AfterEach void tearDown() { + Bukkit.getScheduler().cancelTasks(plugin); MockBukkit.unmock(); } diff --git a/src/test/java/me/ohowe12/spectatormode/listener/OnGameModeChangeListenerTest.java b/src/test/java/me/ohowe12/spectatormode/listener/OnGameModeChangeListenerTest.java index a674b9f..d973d2d 100644 --- a/src/test/java/me/ohowe12/spectatormode/listener/OnGameModeChangeListenerTest.java +++ b/src/test/java/me/ohowe12/spectatormode/listener/OnGameModeChangeListenerTest.java @@ -14,6 +14,7 @@ import me.ohowe12.spectatormode.SpectatorMode; import me.ohowe12.spectatormode.testutils.TestUtils; +import org.bukkit.Bukkit; import org.bukkit.GameMode; import org.bukkit.event.player.PlayerGameModeChangeEvent; import org.bukkit.potion.PotionEffect; @@ -45,6 +46,7 @@ void setUp() { @AfterEach void tearDown() { + Bukkit.getScheduler().cancelTasks(plugin); MockBukkit.unmock(); } diff --git a/src/test/java/me/ohowe12/spectatormode/listener/OnMoveListenerTest.java b/src/test/java/me/ohowe12/spectatormode/listener/OnMoveListenerTest.java index a711eba..99bcad9 100644 --- a/src/test/java/me/ohowe12/spectatormode/listener/OnMoveListenerTest.java +++ b/src/test/java/me/ohowe12/spectatormode/listener/OnMoveListenerTest.java @@ -5,6 +5,7 @@ import be.seeseemelk.mockbukkit.entity.PlayerMock; import me.ohowe12.spectatormode.SpectatorMode; import me.ohowe12.spectatormode.testutils.TestUtils; +import org.bukkit.Bukkit; import org.bukkit.GameMode; import org.bukkit.Location; import org.bukkit.event.player.PlayerMoveEvent; @@ -42,6 +43,7 @@ void setUp() { @AfterEach void tearDown() { + Bukkit.getScheduler().cancelTasks(plugin); MockBukkit.unmock(); } diff --git a/src/test/java/me/ohowe12/spectatormode/state/StateHolderTest.java b/src/test/java/me/ohowe12/spectatormode/state/StateHolderTest.java index 34a2374..ffe122b 100644 --- a/src/test/java/me/ohowe12/spectatormode/state/StateHolderTest.java +++ b/src/test/java/me/ohowe12/spectatormode/state/StateHolderTest.java @@ -8,6 +8,7 @@ import me.ohowe12.spectatormode.SpectatorMode; +import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.file.FileConfiguration; @@ -31,11 +32,13 @@ class StateHolderTest { private File fileLocation; + private SpectatorMode plugin; + @BeforeEach void setUp() { server = MockBukkit.mock(); - SpectatorMode plugin = MockBukkit.load(SpectatorMode.class); + plugin = MockBukkit.load(SpectatorMode.class); fileLocation = new File(plugin.getDataFolder(), "data.yml"); stateHolder = plugin.getSpectatorManager().getStateHolder(); @@ -45,6 +48,7 @@ void setUp() { @AfterEach void tearDown() { + Bukkit.getScheduler().cancelTasks(plugin); MockBukkit.unmock(); } diff --git a/src/test/java/me/ohowe12/spectatormode/testutils/TestUtils.java b/src/test/java/me/ohowe12/spectatormode/testutils/TestUtils.java index 8d1a13d..3065270 100644 --- a/src/test/java/me/ohowe12/spectatormode/testutils/TestUtils.java +++ b/src/test/java/me/ohowe12/spectatormode/testutils/TestUtils.java @@ -39,11 +39,15 @@ public class TestUtils { public static void assertEqualsColored(String expected, String actual) { + assertEqualsColoredNoPrepend("&f" + expected, actual); + } + + public static void assertEqualsColoredNoPrepend(String expected, String actual) { assertEquals(ChatColor.translateAlternateColorCodes('&', expected), actual); } public static void assertNotEqualsColored(String expected, String actual) { - assertNotEquals(ChatColor.translateAlternateColorCodes('&', expected), actual); + assertNotEquals("§f" + ChatColor.translateAlternateColorCodes('&', expected), actual); } public static void setConfigFileOfPlugin(SpectatorMode plugin, String configName) { diff --git a/src/test/java/me/ohowe12/spectatormode/util/MessengerTest.java b/src/test/java/me/ohowe12/spectatormode/util/MessengerTest.java index e324174..cf6d97d 100644 --- a/src/test/java/me/ohowe12/spectatormode/util/MessengerTest.java +++ b/src/test/java/me/ohowe12/spectatormode/util/MessengerTest.java @@ -10,6 +10,7 @@ import me.ohowe12.spectatormode.SpectatorMode; import me.ohowe12.spectatormode.testutils.TestUtils; +import org.bukkit.Bukkit; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -34,6 +35,7 @@ void setUp() { @AfterEach void tearDown() { + Bukkit.getScheduler().cancelTasks(plugin); MockBukkit.unmock(); } @@ -41,7 +43,7 @@ void tearDown() { void send_SenderKey_Valid() { Messenger.send(playerMock, "regular-message"); - playerMock.assertSaid("Hello world!"); + playerMock.assertSaid("§fHello world!"); playerMock.assertNoMoreSaid(); } @@ -50,7 +52,7 @@ void send_WithTarget_Valid() { PlayerMock otherPlayer = server.addPlayer("Player2"); Messenger.send(playerMock, "message-with-target", otherPlayer); - playerMock.assertSaid("Message with Player2"); + playerMock.assertSaid("§fMessage with Player2"); playerMock.assertNoMoreSaid(); otherPlayer.assertNoMoreSaid(); @@ -60,7 +62,7 @@ void send_WithTarget_Valid() { void send_WithExtra_Valid() { Messenger.send(playerMock, "message-with-extra", " like this"); - playerMock.assertSaid("Something should be appended to this like this"); + playerMock.assertSaid("§fSomething should be appended to this like this"); playerMock.assertNoMoreSaid(); } @@ -69,7 +71,7 @@ void send_WithAll_Valid() { PlayerMock otherPlayer = server.addPlayer("Player2"); Messenger.send(playerMock, "message-with-all-features", otherPlayer, "mocked player"); - playerMock.assertSaid("Player2 is a mocked player"); + playerMock.assertSaid("§fPlayer2 is a mocked player"); playerMock.assertNoMoreSaid(); otherPlayer.assertNoMoreSaid(); @@ -83,7 +85,7 @@ void send_ActionBar_Valid() { // saids a "vanilla" // message on chat components - playerMock.assertSaid("This should be on your action bar"); + playerMock.assertSaid("§fThis should be on your action bar"); playerMock.assertNoMoreSaid(); } diff --git a/src/test/resources/configs/togglebackc.yml b/src/test/resources/configs/togglebackc.yml new file mode 100644 index 0000000..f56f336 --- /dev/null +++ b/src/test/resources/configs/togglebackc.yml @@ -0,0 +1 @@ +switch-back-to: creative