Skip to content

Commit

Permalink
feat: messages can now be configured
Browse files Browse the repository at this point in the history
  • Loading branch information
Maloschnikow committed Oct 14, 2024
1 parent 4cd6b57 commit a70af01
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ public class SpawnCommand implements BasicCommand {
private final long TELEPORT_DELAY_TICKS;
private final Plugin plugin;

public final String TELEPORT_CANCELLED_MSG;
public final String TELEPORT_ALREADY_ISSUED_MSG;
public final String TELEPORT_COOLDOWN_MSG;
public final String TELEPORT_PROMISE_MSG;
public final String TELEPORT_RANDOM_FAIL_MSG;

public SpawnCommand(Plugin plugin) {

playerLastUse = new Hashtable<>();
Expand All @@ -37,6 +43,12 @@ public SpawnCommand(Plugin plugin) {
this.COOLDOWN_TIME_SEC = Long.valueOf(plugin.getConfig().getLong("cooldown-seconds", 60));
this.TELEPORT_DELAY_TICKS = plugin.getConfig().getInt("delay-ticks", 200);
this.FAIL_PROBABILITY = plugin.getConfig().getInt("fail-probability", 0);

this.TELEPORT_CANCELLED_MSG = plugin.getConfig().getString("teleport-cancelled-message", "<red>Teleportation aborted, because of unexpected movement.</red>");
this.TELEPORT_ALREADY_ISSUED_MSG = plugin.getConfig().getString("teleport-already-issued-message", "<yellow>You will be teleported soon, please standby.</yellow>");
this.TELEPORT_COOLDOWN_MSG = plugin.getConfig().getString("teleport-cooldown-message", "<red>You'll have to wait <bold><dark_red>%remainingTime%</dark_red></bold> seconds until you can run this command again.</red>");
this.TELEPORT_PROMISE_MSG = plugin.getConfig().getString("teleport-promise-message", "<green>You will be teleported in <bold><dark_green>%delay%</dark_green></bold> seconds.</green>");
this.TELEPORT_RANDOM_FAIL_MSG = plugin.getConfig().getString("teleport-random-fail-message", "<red>Not today.</red>");
}

public Dictionary<UUID, BukkitRunnable> getPlayerIssuedTeleports() {
Expand All @@ -54,17 +66,18 @@ public void execute(@NotNull CommandSourceStack stack, @NotNull String[] args) {
Player player = (Player)stack.getSender();
UUID uuid = player.getUniqueId();

// Check if player has already issued a teleport
if (playerIssuedTeleports.get(uuid) != null) {
player.sendRichMessage("<yellow>Du wirst gleich teleportiert, bitte habe etwas Geduld.</yellow>");
player.sendRichMessage(this.TELEPORT_ALREADY_ISSUED_MSG);
return;
}

// Check cooldown
Long lastUse = playerLastUse.get(uuid);
Long currentTime = Long.valueOf(System.currentTimeMillis());
if ((lastUse != null) && ( (currentTime - lastUse) < (COOLDOWN_TIME_SEC * 1000))) {
Long remainingTime = (COOLDOWN_TIME_SEC) - ((currentTime - lastUse) / 1000);
player.sendRichMessage("<red>Warte noch <bold><dark_red>" + remainingTime.toString() + "</dark_red></bold> Sekunden.</red>");
if ((lastUse != null) && ( (currentTime - lastUse) < (this.COOLDOWN_TIME_SEC * 1000))) {
Long remainingTime = (this.COOLDOWN_TIME_SEC) - ((currentTime - lastUse) / 1000);
player.sendRichMessage(this.TELEPORT_COOLDOWN_MSG.replace("%remainingTime%", remainingTime.toString()));
return;
}

Expand All @@ -83,9 +96,9 @@ public void execute(@NotNull CommandSourceStack stack, @NotNull String[] args) {

// Decides on randomness if player is teleported
Random rand = new Random();
int n = rand.nextInt(FAIL_PROBABILITY + 1);
int n = rand.nextInt(this.FAIL_PROBABILITY + 1);
if (n > 0) {
player.sendRichMessage("<green>Du wirst in <bold><dark_green>" + TELEPORT_DELAY_TICKS / 20 +"</dark_green></bold> Sekunden teleportiert.</green>");
player.sendRichMessage(this.TELEPORT_PROMISE_MSG.replace("%delay%",Long.valueOf( this.TELEPORT_DELAY_TICKS / 20).toString()));

// Teleport player with delay

Expand All @@ -102,11 +115,11 @@ public void run() {
Bukkit.getPluginManager().registerEvents(new TeleportToSpawnListener(this), plugin); //do listeners get destroyed? -> of not this could lead to performance issues

playerIssuedTeleports.put(uuid, teleportRunnable);
teleportRunnable.runTaskLater(this.plugin, TELEPORT_DELAY_TICKS);
teleportRunnable.runTaskLater(this.plugin, this.TELEPORT_DELAY_TICKS);

}
else {
player.sendRichMessage("<red>Ne, heute nicht.</red>");
player.sendRichMessage(this.TELEPORT_RANDOM_FAIL_MSG);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ public void onPlayerMove(PlayerMoveEvent event) {
}
l.cancel();
spawnCommand.getPlayerIssuedTeleports().remove(uuid);
event.getPlayer().sendRichMessage("<red>Teleportation abgebrochen, aufgrund von unvorhergesehener Bewegung.</red>");
event.getPlayer().sendRichMessage(this.spawnCommand.TELEPORT_CANCELLED_MSG);
}
}
28 changes: 27 additions & 1 deletion spawncmdplugin/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,30 @@ delay-ticks: 200
# Fail probability
# With a probability of 1 to x (e.g. 1 to 1000) the /spawn command will not teleport the player.
# defaults to 0 (Note that this value defaults to 0, but is set to 1000 in the default config)
fail-probability: 1000
fail-probability: 1000


# Messages
# The messages have this format: https://docs.advntr.dev/minimessage/format.html
#
# Teleport cancelled message will be sent when the player moves and the teleport gets cancelled.
# defaults to "<red>Teleportation aborted, because of unexpected movement.</red>"
teleport-cancelled-message: "<red>Teleportation abgebrochen, aufgrund von unvorhergesehener Bewegung.</red>"

# Teleport already issued message will be sent when the player runs the /spawn command again even though they already wait to be teleported.
# defaults to "<yellow>You will be teleported soon, please standby.</yellow>"
teleport-already-issued-message: "<yellow>Du wirst gleich teleportiert, bitte habe etwas Geduld.</yellow>"

# Teleport cooldown message will be sent when the player runs /spawn but is still on cooldown
# You can use "%remainingTime%" to insert the time they have to wait in seconds
# defaults to "<red>You'll have to wait <bold><dark_red>%remainingTime%</dark_red></bold> seconds until you can run this command again.</red>"
teleport-cooldown-message: "<red>Warte noch <bold><dark_red>%remainingTime%</dark_red></bold> Sekunden.</red>"

# Teleport promise message will be sent when the player /spawn and will be teleported
# You can use "%delay%" to insert the time after they get deleported in seconds
# defaults to "<green>You will be teleported in <bold><dark_green>%delay%</dark_green></bold> seconds.</green>"
teleport-promise-message: "<green>Du wirst in <bold><dark_green>%delay%</dark_green></bold> Sekunden teleportiert.</green>"

# Teleport random fail message will be sent when the spawn issue fails randomly (see fail-probability)
# defaults to: "<red>Not today.</red>"
teleport-random-fail-message: "<red>Ne, heute nicht.</red>"

0 comments on commit a70af01

Please sign in to comment.