From 679e43758bf40b43b26ed78194cfdbc52dcff321 Mon Sep 17 00:00:00 2001 From: TomyLobo Date: Mon, 12 Mar 2012 06:50:35 +0100 Subject: [PATCH] BREAKING CHANGE: Added a LocationFlag class and made the teleport and spawn flags use it. This means all teleport and spawn flags are cleared. This also means that world, yaw and pitch are now saved with these flags. --- .../bukkit/WorldGuardPlayerListener.java | 4 +- .../bukkit/commands/RegionCommands.java | 7 +- .../protection/flags/DefaultFlag.java | 4 +- .../protection/flags/LocationFlag.java | 154 ++++++++++++++++++ 4 files changed, 162 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/sk89q/worldguard/protection/flags/LocationFlag.java diff --git a/src/main/java/com/sk89q/worldguard/bukkit/WorldGuardPlayerListener.java b/src/main/java/com/sk89q/worldguard/bukkit/WorldGuardPlayerListener.java index 46ad5abb..c9d9c87d 100644 --- a/src/main/java/com/sk89q/worldguard/bukkit/WorldGuardPlayerListener.java +++ b/src/main/java/com/sk89q/worldguard/bukkit/WorldGuardPlayerListener.java @@ -920,10 +920,10 @@ public void onPlayerRespawn(PlayerRespawnEvent event) { ApplicableRegionSet set = mgr.getApplicableRegions(pt); LocalPlayer localPlayer = plugin.wrapPlayer(player); - Vector spawn = set.getFlag(DefaultFlag.SPAWN_LOC, localPlayer); + com.sk89q.worldedit.Location spawn = set.getFlag(DefaultFlag.SPAWN_LOC, localPlayer); if (spawn != null) { - event.setRespawnLocation(BukkitUtil.toLocation(player.getWorld(), spawn)); + event.setRespawnLocation(com.sk89q.worldedit.bukkit.BukkitUtil.toLocation(spawn)); } } } diff --git a/src/main/java/com/sk89q/worldguard/bukkit/commands/RegionCommands.java b/src/main/java/com/sk89q/worldguard/bukkit/commands/RegionCommands.java index f5d42730..d32c6e4b 100644 --- a/src/main/java/com/sk89q/worldguard/bukkit/commands/RegionCommands.java +++ b/src/main/java/com/sk89q/worldguard/bukkit/commands/RegionCommands.java @@ -35,6 +35,7 @@ import com.sk89q.minecraft.util.commands.CommandPermissions; import com.sk89q.minecraft.util.commands.CommandPermissionsException; import com.sk89q.worldedit.BlockVector; +import com.sk89q.worldedit.Location; import com.sk89q.worldedit.Vector; import com.sk89q.worldedit.bukkit.BukkitUtil; import com.sk89q.worldedit.bukkit.WorldEditPlugin; @@ -1071,13 +1072,13 @@ public void teleport(CommandContext args, CommandSender sender) throws CommandEx throw new CommandException("A region with ID '" + id + "' doesn't exist."); } - final Vector teleportLocation = region.getFlag(DefaultFlag.TELE_LOC); + final Location teleportLocation = region.getFlag(DefaultFlag.TELE_LOC); if (teleportLocation == null) { throw new CommandException("The region has no teleport point associated."); } - player.teleport(BukkitUtil.toLocation(player.getWorld(), teleportLocation)); - + player.teleport(BukkitUtil.toLocation(teleportLocation)); + sender.sendMessage("Teleported you to the region '" + id + "'."); } } diff --git a/src/main/java/com/sk89q/worldguard/protection/flags/DefaultFlag.java b/src/main/java/com/sk89q/worldguard/protection/flags/DefaultFlag.java index f8e2b527..361ed7f8 100644 --- a/src/main/java/com/sk89q/worldguard/protection/flags/DefaultFlag.java +++ b/src/main/java/com/sk89q/worldguard/protection/flags/DefaultFlag.java @@ -74,8 +74,8 @@ public final class DefaultFlag { public static final IntegerFlag FEED_AMOUNT = new IntegerFlag("feed-amount", RegionGroup.ALL); public static final IntegerFlag MIN_FOOD = new IntegerFlag("feed-min-hunger", RegionGroup.ALL); public static final IntegerFlag MAX_FOOD = new IntegerFlag("feed-max-hunger", RegionGroup.ALL); - public static final VectorFlag TELE_LOC = new VectorFlag("teleport", RegionGroup.MEMBERS); - public static final VectorFlag SPAWN_LOC = new VectorFlag("spawn", RegionGroup.MEMBERS); + public static final LocationFlag TELE_LOC = new LocationFlag("teleport", RegionGroup.MEMBERS); + public static final LocationFlag SPAWN_LOC = new LocationFlag("spawn", RegionGroup.MEMBERS); public static final BooleanFlag BUYABLE = new BooleanFlag("buyable"); public static final DoubleFlag PRICE = new DoubleFlag("price"); public static final SetFlag BLOCKED_CMDS = new SetFlag("blocked-cmds", RegionGroup.ALL, new CommandStringFlag(null)); diff --git a/src/main/java/com/sk89q/worldguard/protection/flags/LocationFlag.java b/src/main/java/com/sk89q/worldguard/protection/flags/LocationFlag.java new file mode 100644 index 00000000..d6b9b311 --- /dev/null +++ b/src/main/java/com/sk89q/worldguard/protection/flags/LocationFlag.java @@ -0,0 +1,154 @@ +// $Id$ +/* + * WorldGuard + * Copyright (C) 2010 sk89q + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.sk89q.worldguard.protection.flags; + +import java.util.HashMap; +import java.util.Map; + +import org.bukkit.Bukkit; +import org.bukkit.World; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + +import com.sk89q.minecraft.util.commands.CommandException; +import com.sk89q.worldedit.LocalWorld; +import com.sk89q.worldedit.Location; +import com.sk89q.worldedit.Vector; +import com.sk89q.worldedit.bukkit.BukkitUtil; +import com.sk89q.worldguard.bukkit.WorldGuardPlugin; + +/** + * + * @author sk89q + */ +public class LocationFlag extends Flag { + + public LocationFlag(String name, RegionGroup defaultGroup) { + super(name, defaultGroup); + } + + public LocationFlag(String name) { + super(name); + } + + @Override + public Location parseInput(WorldGuardPlugin plugin, CommandSender sender, String input) throws InvalidFlagFormat { + input = input.trim(); + + final Player player; + try { + player = plugin.checkPlayer(sender); + } catch (CommandException e) { + throw new InvalidFlagFormat(e.getMessage()); + } + + if ("here".equalsIgnoreCase(input)) { + return BukkitUtil.toLocation(player.getLocation()); + } else if ("none".equalsIgnoreCase(input)) { + return null; + } else { + String[] split = input.split(","); + if (split.length >= 3) { + try { + final World world = player.getWorld(); + final double x = Double.parseDouble(split[0]); + final double y = Double.parseDouble(split[1]); + final double z = Double.parseDouble(split[2]); + final float yaw = split.length < 4 ? 0 : Float.parseFloat(split[3]); + final float pitch = split.length < 5 ? 0 : Float.parseFloat(split[4]); + + return new Location( + BukkitUtil.getLocalWorld(world), + new Vector( + x, + y, + z + ), + yaw, pitch + ); + } catch (NumberFormatException e) { + } + } + + throw new InvalidFlagFormat("Expected 'here' or x,y,z."); + } + } + + @Override + public Location unmarshal(Object o) { + if (o instanceof Map) { + Map map = (Map) o; + + Object rawWorld = map.get("world"); + if (rawWorld == null) return null; + + Object rawX = map.get("x"); + if (rawX == null) return null; + + Object rawY = map.get("y"); + if (rawY == null) return null; + + Object rawZ = map.get("z"); + if (rawZ == null) return null; + + Object rawYaw = map.get("yaw"); + if (rawYaw == null) return null; + + Object rawPitch = map.get("pitch"); + if (rawPitch == null) return null; + + World bukkitWorld = Bukkit.getServer().getWorld((String) rawWorld); + LocalWorld world = BukkitUtil.getLocalWorld(bukkitWorld); + Vector position = new Vector(toNumber(rawX), toNumber(rawY), toNumber(rawZ)); + float yaw = (float) toNumber(rawYaw); + float pitch = (float) toNumber(rawPitch); + + return new Location(world, position, yaw, pitch); + } + + return null; + } + + @Override + public Object marshal(Location o) { + Vector position = o.getPosition(); + Map vec = new HashMap(); + vec.put("world", o.getWorld().getName()); + vec.put("x", position.getX()); + vec.put("y", position.getY()); + vec.put("z", position.getZ()); + vec.put("yaw", o.getYaw()); + vec.put("pitch", o.getPitch()); + return vec; + } + + private double toNumber(Object o) { + if (o instanceof Integer) { + return (Integer) o; + } else if (o instanceof Long) { + return (Long) o; + } else if (o instanceof Float) { + return (Float) o; + } else if (o instanceof Double) { + return (Double) o; + } else { + return 0; + } + } +}