From a70af01c50421b62ef353fd616848201ab131cfa Mon Sep 17 00:00:00 2001 From: Maloschnikow <123018032+Maloschnikow@users.noreply.github.com> Date: Mon, 14 Oct 2024 11:26:59 +0200 Subject: [PATCH] feat: messages can now be configured --- .../spawncmdplugin/SpawnCommand.java | 29 ++++++++++++++----- .../TeleportToSpawnListener.java | 2 +- spawncmdplugin/src/main/resources/config.yml | 28 +++++++++++++++++- 3 files changed, 49 insertions(+), 10 deletions(-) diff --git a/spawncmdplugin/src/main/java/io/maloschnikow/spawncmdplugin/SpawnCommand.java b/spawncmdplugin/src/main/java/io/maloschnikow/spawncmdplugin/SpawnCommand.java index eb06ab2..86e29e6 100644 --- a/spawncmdplugin/src/main/java/io/maloschnikow/spawncmdplugin/SpawnCommand.java +++ b/spawncmdplugin/src/main/java/io/maloschnikow/spawncmdplugin/SpawnCommand.java @@ -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<>(); @@ -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", "Teleportation aborted, because of unexpected movement."); + this.TELEPORT_ALREADY_ISSUED_MSG = plugin.getConfig().getString("teleport-already-issued-message", "You will be teleported soon, please standby."); + this.TELEPORT_COOLDOWN_MSG = plugin.getConfig().getString("teleport-cooldown-message", "You'll have to wait %remainingTime% seconds until you can run this command again."); + this.TELEPORT_PROMISE_MSG = plugin.getConfig().getString("teleport-promise-message", "You will be teleported in %delay% seconds."); + this.TELEPORT_RANDOM_FAIL_MSG = plugin.getConfig().getString("teleport-random-fail-message", "Not today."); } public Dictionary getPlayerIssuedTeleports() { @@ -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("Du wirst gleich teleportiert, bitte habe etwas Geduld."); + 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("Warte noch " + remainingTime.toString() + " Sekunden."); + 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; } @@ -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("Du wirst in " + TELEPORT_DELAY_TICKS / 20 +" Sekunden teleportiert."); + player.sendRichMessage(this.TELEPORT_PROMISE_MSG.replace("%delay%",Long.valueOf( this.TELEPORT_DELAY_TICKS / 20).toString())); // Teleport player with delay @@ -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("Ne, heute nicht."); + player.sendRichMessage(this.TELEPORT_RANDOM_FAIL_MSG); } } } diff --git a/spawncmdplugin/src/main/java/io/maloschnikow/spawncmdplugin/TeleportToSpawnListener.java b/spawncmdplugin/src/main/java/io/maloschnikow/spawncmdplugin/TeleportToSpawnListener.java index 9dbfb6d..00b6ddd 100644 --- a/spawncmdplugin/src/main/java/io/maloschnikow/spawncmdplugin/TeleportToSpawnListener.java +++ b/spawncmdplugin/src/main/java/io/maloschnikow/spawncmdplugin/TeleportToSpawnListener.java @@ -24,6 +24,6 @@ public void onPlayerMove(PlayerMoveEvent event) { } l.cancel(); spawnCommand.getPlayerIssuedTeleports().remove(uuid); - event.getPlayer().sendRichMessage("Teleportation abgebrochen, aufgrund von unvorhergesehener Bewegung."); + event.getPlayer().sendRichMessage(this.spawnCommand.TELEPORT_CANCELLED_MSG); } } diff --git a/spawncmdplugin/src/main/resources/config.yml b/spawncmdplugin/src/main/resources/config.yml index 5e805fe..9c2da01 100644 --- a/spawncmdplugin/src/main/resources/config.yml +++ b/spawncmdplugin/src/main/resources/config.yml @@ -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 \ No newline at end of file +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 "Teleportation aborted, because of unexpected movement." +teleport-cancelled-message: "Teleportation abgebrochen, aufgrund von unvorhergesehener Bewegung." + +# 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 "You will be teleported soon, please standby." +teleport-already-issued-message: "Du wirst gleich teleportiert, bitte habe etwas Geduld." + +# 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 "You'll have to wait %remainingTime% seconds until you can run this command again." +teleport-cooldown-message: "Warte noch %remainingTime% Sekunden." + +# 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 "You will be teleported in %delay% seconds." +teleport-promise-message: "Du wirst in %delay% Sekunden teleportiert." + +# Teleport random fail message will be sent when the spawn issue fails randomly (see fail-probability) +# defaults to: "Not today." +teleport-random-fail-message: "Ne, heute nicht." \ No newline at end of file