From 58f9a4d34952523a196e775547fee09e4fc8f262 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Bro=C4=8Dko?= Date: Sat, 8 Sep 2018 18:04:09 +0200 Subject: [PATCH] WorldGuard dependency --- .classpath | 8 +++ pom.xml | 16 ++++++ resources/plugin.yml | 1 + .../essentials/features/RandomTeleport.java | 56 +++++++++++++++---- 4 files changed, 71 insertions(+), 10 deletions(-) diff --git a/.classpath b/.classpath index ab5fb2e..b866b22 100644 --- a/.classpath +++ b/.classpath @@ -17,4 +17,12 @@ + + + + + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index ea2100d..9b143a6 100644 --- a/pom.xml +++ b/pom.xml @@ -71,6 +71,18 @@ 1.6.4 provided + + com.sk89q.worldedit + worldedit-core + 6.0.0-SNAPSHOT + provided + + + com.sk89q.worldguard + worldguard-legacy + 6.2 + provided + @@ -80,5 +92,9 @@ spigot-repo https://hub.spigotmc.org/nexus/content/repositories/snapshots/ + + sk89q-repo + http://maven.sk89q.com/repo/ + \ No newline at end of file diff --git a/resources/plugin.yml b/resources/plugin.yml index 96dd17e..014be65 100644 --- a/resources/plugin.yml +++ b/resources/plugin.yml @@ -4,6 +4,7 @@ version: 0.0.1 api-version: 1.12 author: ericek111 website: https://git.lixko.eu/MarwLand/MarwCore +softdepend: [WorldGuard] commands: first: description: First command. diff --git a/src/cz/marwland/mc/essentials/features/RandomTeleport.java b/src/cz/marwland/mc/essentials/features/RandomTeleport.java index 73f1637..6fe81f1 100644 --- a/src/cz/marwland/mc/essentials/features/RandomTeleport.java +++ b/src/cz/marwland/mc/essentials/features/RandomTeleport.java @@ -2,22 +2,25 @@ package cz.marwland.mc.essentials.features; import java.util.Arrays; +import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.World; +import org.bukkit.block.Block; import org.bukkit.command.CommandSender; -import org.bukkit.command.ConsoleCommandSender; import org.bukkit.command.defaults.BukkitCommand; import org.bukkit.entity.Player; +import org.bukkit.event.block.BlockCanBuildEvent; import org.bukkit.event.player.PlayerTeleportEvent.TeleportCause; -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); + private int MAX_TRIES = 100; + + public RandomTeleport() { + super(); this.addCommand(new BukkitCommand( "rtp", "Teleport player to random location in the current world.", @@ -36,7 +39,7 @@ public class RandomTeleport extends Feature { Player p = (Player) sender; if (args.length >= 1) { - p = plugin.getServer().getPlayer(args[0]); + p = Bukkit.getServer().getPlayer(args[0]); if (p != sender && !permissionMissingCheck(sender, this.getPermission() + ".others")) { return true; } else if (p == null) { @@ -47,7 +50,7 @@ public class RandomTeleport extends Feature { World w = p.getWorld(); if (args.length >= 2) { - w = plugin.getServer().getWorld(args[1]); + w = Bukkit.getServer().getWorld(args[1]); if (w != p.getWorld() && !permissionMissingCheck(sender, this.getPermission() + ".world." + w.getName()) ) { return true; } else if (w == null) { @@ -56,7 +59,6 @@ public class RandomTeleport extends Feature { } } - sender.sendMessage("teleporting..."); randomTeleport(w, p); return true; @@ -64,12 +66,24 @@ public class RandomTeleport extends Feature { @Override public String getPermission() { - return getPermissionPath() + "." + this.getName().toLowerCase(); + return getPermissionPath(); } }); } - public static void randomTeleport(World w, Player p) { + public void randomTeleport(World w, Player p) { + Location loc = null; + for (int i = 0; i < MAX_TRIES && loc == null; i++) { + p.sendMessage("Generating #" + i); + loc = generateLoc(w); + if (!canTeleportHere(loc, p)) + loc = null; + } + if (loc != null) + p.teleport(loc, TeleportCause.COMMAND); + } + + public Location generateLoc(World w) { int wboff = 16; Location loc = w.getWorldBorder().getCenter(); int radius = (int) w.getWorldBorder().getSize() / 2; @@ -86,7 +100,29 @@ public class RandomTeleport extends Feature { Location targetloc = w.getHighestBlockAt(finx, finz).getLocation(); targetloc.setYaw((float) (Math.random() * 360f)); targetloc.add(0.5d, 1d, 0.5d); - p.teleport(targetloc, TeleportCause.COMMAND); + + return targetloc; + } + + public boolean canTeleportHere(Location loc, Player p) { + Block block = loc.getWorld().getBlockAt(loc); + p.sendMessage(block.getType().name()); + BlockCanBuildEvent canBuildEvent = new BlockCanBuildEvent(block, block.getData(), true); + Bukkit.getServer().getPluginManager().callEvent(canBuildEvent); + if(!canBuildEvent.isBuildable()) + return false; + + try { + p.sendMessage("trying"); + if (!com.sk89q.worldguard.bukkit.WGBukkit.getPlugin().canBuild(p, block)) { + p.sendMessage("cannot"); + return false; + } + } catch (NoClassDefFoundError e) { + } + + p.sendMessage("can"); + return true; } }