Skip to content

Commit

Permalink
feat: spawn command now functional
Browse files Browse the repository at this point in the history
values like cooldown and delay are currently set via constant variables
  • Loading branch information
Maloschnikow committed Oct 13, 2024
1 parent d278b89 commit f89d757
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,25 +1,79 @@
package io.maloschnikow.spawncmdplugin;

import org.bukkit.Location;
import org.bukkit.World;

import java.util.Dictionary;
import java.util.Hashtable;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import io.papermc.paper.command.brigadier.BasicCommand;
import io.papermc.paper.command.brigadier.CommandSourceStack;

public class SpawnCommand implements BasicCommand {

private Dictionary<UUID, Long> playerLastUse; //Holds unix time in seconds of the last command use of each player
private final Long COOLDOWN_TIME_SEC = 60L;
private final int FAIL_PROBABILITY = 1000; // 1 to x (e.g. 1 to 1000) (kind of)
private final long TELEPORT_DELAY_SEC = 6;

public SpawnCommand() {

playerLastUse = new Hashtable<>();

}

@Override
public void execute(@NotNull CommandSourceStack stack, @NotNull String[] args) {
CommandSender sender = stack.getSender();
if (!(sender instanceof Player)) {
return;
}

//Get player and world info
Player player = (Player)stack.getSender();
UUID uuid = player.getUniqueId();
World spawnWorld = player.getServer().getWorld("world");
Location spawnLocation = spawnWorld.getSpawnLocation();

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

//Sets date and time of command use
this.playerLastUse.put(uuid, Long.valueOf(System.currentTimeMillis() / 1000L));

//Decides on randomness if player is teleported
Random rand = new Random();
int n = rand.nextInt(1000);
int n = rand.nextInt(FAIL_PROBABILITY + 1);
if (n > 0) {
stack.getSender().sendRichMessage("<green>Du wirst gleich teleportiert.</green>");
player.sendRichMessage("<green>Du wirst in <bold><dark_green>" + TELEPORT_DELAY_SEC +"</dark_green></bold> Sekunden teleportiert.</green>");

//Teleport player with delay
final ScheduledExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
executorService.schedule(new Runnable() {
@Override
public void run() {
player.teleportAsync(spawnLocation);
}
}, TELEPORT_DELAY_SEC, TimeUnit.SECONDS);

}
else {
stack.getSender().sendRichMessage("<red>Ne, heute nicht.</red>");
player.sendRichMessage("<red>Ne, heute nicht.</red>");
}

}

}
4 changes: 2 additions & 2 deletions spawncmdplugin/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: SpawnCMDPlugin
version: 0.0.1
name: spawncmdplugin
version: 1.0.0
main: io.maloschnikow.spawncmdplugin.SpawnCMDPlugin
description: Adds a "/spawn" command to teleport the player back to the spawn.
author: Maloschnikow
Expand Down

0 comments on commit f89d757

Please sign in to comment.