From 00f388045a65d6455e3c7166f4fb32c050af71cc Mon Sep 17 00:00:00 2001 From: Cole Riggle Date: Sun, 11 Dec 2022 20:41:06 -0500 Subject: [PATCH 1/4] Added teleport-cooldown group support --- .../src/main/java/com/earth2me/essentials/AsyncTeleport.java | 3 ++- .../src/main/java/com/earth2me/essentials/ISettings.java | 2 +- .../src/main/java/com/earth2me/essentials/Settings.java | 4 ++-- .../src/main/java/com/earth2me/essentials/Teleport.java | 3 ++- Essentials/src/main/resources/config.yml | 4 +++- 5 files changed, 10 insertions(+), 6 deletions(-) diff --git a/Essentials/src/main/java/com/earth2me/essentials/AsyncTeleport.java b/Essentials/src/main/java/com/earth2me/essentials/AsyncTeleport.java index 19ab45818..a7db5368f 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/AsyncTeleport.java +++ b/Essentials/src/main/java/com/earth2me/essentials/AsyncTeleport.java @@ -53,7 +53,8 @@ public class AsyncTeleport implements IAsyncTeleport { final Calendar time = new GregorianCalendar(); if (teleportOwner.getLastTeleportTimestamp() > 0) { // Take the current time, and remove the delay from it. - final double cooldown = ess.getSettings().getTeleportCooldown(); + final String group = teleportOwner.getGroup(); + final double cooldown = ess.getSettings().getTeleportCooldown(group); final Calendar earliestTime = new GregorianCalendar(); earliestTime.add(Calendar.SECOND, -(int) cooldown); earliestTime.add(Calendar.MILLISECOND, -(int) ((cooldown * 1000.0) % 1000.0)); diff --git a/Essentials/src/main/java/com/earth2me/essentials/ISettings.java b/Essentials/src/main/java/com/earth2me/essentials/ISettings.java index 4da7f5254..3e1b59c17 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/ISettings.java +++ b/Essentials/src/main/java/com/earth2me/essentials/ISettings.java @@ -123,7 +123,7 @@ public interface ISettings extends IConf { boolean isForcePassengerTeleport(); - double getTeleportCooldown(); + double getTeleportCooldown(String group); double getTeleportDelay(); diff --git a/Essentials/src/main/java/com/earth2me/essentials/Settings.java b/Essentials/src/main/java/com/earth2me/essentials/Settings.java index c0b1c6182..8b3686640 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/Settings.java +++ b/Essentials/src/main/java/com/earth2me/essentials/Settings.java @@ -473,8 +473,8 @@ public class Settings implements net.ess3.api.ISettings { } @Override - public double getTeleportCooldown() { - return config.getDouble("teleport-cooldown", 0); + public double getTeleportCooldown(String group) { + return config.getDouble("group-teleport-cooldowns." + (group == null ? "Default" : group),0); } @Override diff --git a/Essentials/src/main/java/com/earth2me/essentials/Teleport.java b/Essentials/src/main/java/com/earth2me/essentials/Teleport.java index 6038b2e35..dd7f4d9cf 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/Teleport.java +++ b/Essentials/src/main/java/com/earth2me/essentials/Teleport.java @@ -44,7 +44,8 @@ public class Teleport implements ITeleport { final Calendar time = new GregorianCalendar(); if (teleportOwner.getLastTeleportTimestamp() > 0) { // Take the current time, and remove the delay from it. - final double cooldown = ess.getSettings().getTeleportCooldown(); + final String group = teleportOwner.getGroup(); + final double cooldown = ess.getSettings().getTeleportCooldown(group); final Calendar earliestTime = new GregorianCalendar(); earliestTime.add(Calendar.SECOND, -(int) cooldown); earliestTime.add(Calendar.MILLISECOND, -(int) ((cooldown * 1000.0) % 1000.0)); diff --git a/Essentials/src/main/resources/config.yml b/Essentials/src/main/resources/config.yml index ba35a3157..86afa3834 100644 --- a/Essentials/src/main/resources/config.yml +++ b/Essentials/src/main/resources/config.yml @@ -92,7 +92,9 @@ force-safe-teleport-location: false teleport-passenger-dismount: true # The delay, in seconds, required between /home, /tp, etc. -teleport-cooldown: 0 +group-teleport-cooldowns: + default: 5 + admin: 3 # The delay, in seconds, before a user actually teleports. If the user moves or gets attacked in this timeframe, the teleport is cancelled. teleport-delay: 0 From f45149b868dc280b334e753df09418718e11eeef Mon Sep 17 00:00:00 2001 From: Cole Riggle Date: Sun, 11 Dec 2022 20:42:38 -0500 Subject: [PATCH 2/4] Updated config.yml with more sensical defaults --- Essentials/src/main/resources/config.yml | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Essentials/src/main/resources/config.yml b/Essentials/src/main/resources/config.yml index 86afa3834..55734adb0 100644 --- a/Essentials/src/main/resources/config.yml +++ b/Essentials/src/main/resources/config.yml @@ -96,8 +96,10 @@ group-teleport-cooldowns: default: 5 admin: 3 -# The delay, in seconds, before a user actually teleports. If the user moves or gets attacked in this timeframe, the teleport is cancelled. -teleport-delay: 0 +# The delay, in seconds, required between /home, /tp, etc. +group-teleport-cooldowns: + default: 0 + # admins: 3 # The delay, in seconds, a player can't be attacked by other players after they have been teleported by a command. # This will also prevent the player attacking other players. From b7d81b604a8bfb692624c448765c5ed3519b9963 Mon Sep 17 00:00:00 2001 From: Cole Riggle Date: Sun, 11 Dec 2022 20:43:19 -0500 Subject: [PATCH 3/4] Fixed duplication in config.yml --- Essentials/src/main/resources/config.yml | 5 ----- 1 file changed, 5 deletions(-) diff --git a/Essentials/src/main/resources/config.yml b/Essentials/src/main/resources/config.yml index 55734adb0..0a17f89e4 100644 --- a/Essentials/src/main/resources/config.yml +++ b/Essentials/src/main/resources/config.yml @@ -91,11 +91,6 @@ force-safe-teleport-location: false # If this is set to false, attempted teleports will be canceled with a warning. teleport-passenger-dismount: true -# The delay, in seconds, required between /home, /tp, etc. -group-teleport-cooldowns: - default: 5 - admin: 3 - # The delay, in seconds, required between /home, /tp, etc. group-teleport-cooldowns: default: 0 From 8d372cd76806f242a4c0115f3703a891a6939763 Mon Sep 17 00:00:00 2001 From: Cole Riggle Date: Sun, 11 Dec 2022 21:14:47 -0500 Subject: [PATCH 4/4] Unit test added --- .../essentials/TeleportationTest.java | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 Essentials/src/test/java/com/earth2me/essentials/TeleportationTest.java diff --git a/Essentials/src/test/java/com/earth2me/essentials/TeleportationTest.java b/Essentials/src/test/java/com/earth2me/essentials/TeleportationTest.java new file mode 100644 index 000000000..7edbf0fd3 --- /dev/null +++ b/Essentials/src/test/java/com/earth2me/essentials/TeleportationTest.java @@ -0,0 +1,68 @@ +package com.earth2me.essentials; + +import com.earth2me.essentials.commands.IEssentialsCommand; +import com.earth2me.essentials.commands.NoChargeException; +import org.bukkit.plugin.InvalidDescriptionException; +import org.junit.Test; + +import java.io.IOException; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +public class TeleportationTest { + + private final OfflinePlayer base1; + private final Essentials ess; + private final FakeServer server; + + public TeleportationTest() { + server = FakeServer.getServer(); + ess = new Essentials(server); + try { + ess.setupForTesting(server); + } catch (final InvalidDescriptionException ex) { + fail("InvalidDescriptionException"); + } catch (final IOException ex) { + fail("IOException"); + } + base1 = server.createPlayer("testPlayer1"); + server.addPlayer(base1); + ess.getUser(base1); + } + + @Test + public void testTeleportationCooldownConfig() { + final User user1 = ess.getUser(base1); + + final double defaultTeleportCooldown = ess.getSettings().getTeleportCooldown("default"); + assertEquals(defaultTeleportCooldown, 0, 0.01); + + boolean fail = false; + try { + // Test two home commands in quick succession; will work as long as config is properly set + runCommand("home", user1, "a"); + runCommand("home", user1, "a"); + } catch (Exception e) { + fail = true; + } finally { + assertEquals(fail, false); + } + } + + private void runCommand(final String command, final User user, final String args) throws Exception { + runCommand(command, user, args.split("\\s+")); + } + + private void runCommand(final String command, final User user, final String[] args) throws Exception { + final IEssentialsCommand cmd; + + try { + cmd = (IEssentialsCommand) Essentials.class.getClassLoader() + .loadClass("com.earth2me.essentials.commands.Command" + command).newInstance(); + cmd.setEssentials(ess); + cmd.run(server, user, command, null, args); + } catch (final NoChargeException ignored) { + } + } +}