From 2191b94b9a66e1cb615824f79deff5fae90d250b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Bro=C4=8Dko?= Date: Thu, 6 Sep 2018 11:25:49 +0200 Subject: [PATCH] RandomTeleport --- deploy.sh | 4 +- src/cz/marwland/mc/core/MarwCore.java | 2 + .../essentials/features/RandomTeleport.java | 78 +++++++++++++++++++ 3 files changed, 82 insertions(+), 2 deletions(-) create mode 100644 src/cz/marwland/mc/essentials/features/RandomTeleport.java diff --git a/deploy.sh b/deploy.sh index 751d2e9..359f868 100755 --- a/deploy.sh +++ b/deploy.sh @@ -1,8 +1,8 @@ #!/bin/bash -if [[ "Eriks-MacBook-Air.local" == `hostname` ]]; then +if [[ "Darwin" == `uname` ]]; then MC_PATH="../../server" - + cp "$1" "../../server/plugins/$2.jar" "$MC_PATH/cmd.sh" "plugman reload $2" else diff --git a/src/cz/marwland/mc/core/MarwCore.java b/src/cz/marwland/mc/core/MarwCore.java index 8c97fe8..66e4d53 100644 --- a/src/cz/marwland/mc/core/MarwCore.java +++ b/src/cz/marwland/mc/core/MarwCore.java @@ -9,6 +9,7 @@ import cz.marwland.mc.core.features.Base; import cz.marwland.mc.core.features.Feature; import cz.marwland.mc.essentials.features.Borders; import cz.marwland.mc.essentials.features.ChatNotifier; +import cz.marwland.mc.essentials.features.RandomTeleport; public class MarwCore extends JavaPlugin { @@ -24,6 +25,7 @@ public class MarwCore extends JavaPlugin { this.addFeature(new Borders(this)); this.addFeature(new ChatNotifier(this)); + this.addFeature(new RandomTeleport(this)); configManager.load(); this.features.forEach((k, v) -> v.onEnable()); diff --git a/src/cz/marwland/mc/essentials/features/RandomTeleport.java b/src/cz/marwland/mc/essentials/features/RandomTeleport.java new file mode 100644 index 0000000..47215ec --- /dev/null +++ b/src/cz/marwland/mc/essentials/features/RandomTeleport.java @@ -0,0 +1,78 @@ +package cz.marwland.mc.essentials.features; + +import java.util.Arrays; + +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.command.CommandSender; +import org.bukkit.command.ConsoleCommandSender; +import org.bukkit.command.defaults.BukkitCommand; +import org.bukkit.entity.Player; + +import cz.marwland.mc.core.MarwCore; +import cz.marwland.mc.core.features.Feature; +import net.md_5.bungee.api.ChatColor; + +public class RandomTeleport extends Feature { + + public RandomTeleport(MarwCore plugin) { + super(plugin, null); + this.addCommand(new BukkitCommand( + "rtp", + "Teleport player to random location in the current world.", + "/rtp [player]", + Arrays.asList("randomtp", "randomteleport") ) { + @Override + public boolean execute(CommandSender sender, String commandLabel, String[] args) { + if (!permissionMissingCheck(sender, this.getPermission())) + return true; + + if (sender instanceof ConsoleCommandSender && args.length < 1) { + sender.sendMessage(ChatColor.RED + "You need to specify the world! " + ChatColor.YELLOW + "/rtp [world]"); + return true; + } + + // Should never happen. + if (!(sender instanceof Player)) + return false; + + Player p = (Player) sender; + if (args.length >= 1) { + p = plugin.getServer().getPlayer(args[0]); + if (p == null) { + sender.sendMessage(ChatColor.RED + "Player not found!"); + return true; + } + } + World w = p.getWorld(); + randomTeleport(w, p); + + return true; + } + + @Override + public String getPermission() { + return getPermissionPath() + "." + this.getName().toLowerCase(); + } + }); + } + + public static void randomTeleport(World w, Player p) { + int wboff = 16; + Location loc = w.getWorldBorder().getCenter(); + int radius = (int) w.getWorldBorder().getSize() / 2; + int minx = loc.getBlockX() - radius; + int minz = loc.getBlockZ() - radius; + int maxx = loc.getBlockX() + radius; + int maxz = loc.getBlockZ() + radius; + if (wboff >= radius) + wboff = 0; + int rangex = (maxx - minx) - wboff * 2; + int rangez = (maxz - minz) - wboff * 2; + int finx = minx + (int) (Math.random() * rangex) + wboff; + int finz = minz + (int) (Math.random() * rangez) + wboff; + Location targetloc = w.getHighestBlockAt(finx, finz).getLocation(); + p.teleport(targetloc); +; } + +}