Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Group teleport cooldowns #5186

Open
wants to merge 4 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ public boolean cooldown(final boolean check, final CompletableFuture<Boolean> fu
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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public interface ISettings extends IConf {

boolean isForcePassengerTeleport();

double getTeleportCooldown();
double getTeleportCooldown(String group);

double getTeleportDelay();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,8 +473,8 @@ public String getNicknamePrefix() {
}

@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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public void cooldown(final boolean check) throws Exception {
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));
Expand Down
7 changes: 3 additions & 4 deletions Essentials/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,9 @@ force-safe-teleport-location: false
teleport-passenger-dismount: true

# The delay, in seconds, required between /home, /tp, etc.
teleport-cooldown: 0

# 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
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.
Expand Down
Original file line number Diff line number Diff line change
@@ -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) {
}
}
}