Add hack to make Location flags work with unloaded worlds.

This commit is contained in:
sk89q 2014-12-29 16:35:22 -08:00
parent 7bd69bebf4
commit 53fdff1d9b
2 changed files with 94 additions and 28 deletions

View File

@ -0,0 +1,71 @@
/*
* WorldGuard, a suite of tools for Minecraft
* Copyright (C) sk89q <http://www.sk89q.com>
* Copyright (C) WorldGuard team and contributors
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU Lesser 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 Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.sk89q.worldguard.protection.flags;
import com.sk89q.worldedit.LocalWorld;
import com.sk89q.worldedit.Location;
import com.sk89q.worldedit.Vector;
import com.sk89q.worldedit.bukkit.BukkitUtil;
import org.bukkit.Bukkit;
import javax.annotation.Nullable;
class LazyLocation extends Location {
private final String worldName;
@Nullable
private static LocalWorld findWorld(String worldName) {
return BukkitUtil.getLocalWorld(Bukkit.getServer().getWorld(worldName));
}
public LazyLocation(String worldName, Vector position, float yaw, float pitch) {
super(findWorld(worldName), position, yaw, pitch);
this.worldName = worldName;
}
public LazyLocation(String worldName, Vector position) {
super(findWorld(worldName), position);
this.worldName = worldName;
}
public String getWorldName() {
return worldName;
}
public LazyLocation setAngles(float yaw, float pitch) {
return new LazyLocation(worldName, getPosition(), yaw, pitch);
}
public LazyLocation setPosition(Vector position) {
return new LazyLocation(worldName, position, getYaw(), getPitch());
}
public LazyLocation add(Vector other) {
return this.setPosition(getPosition().add(other));
}
public LazyLocation add(double x, double y, double z) {
return this.setPosition(getPosition().add(x, y, z));
}
}

View File

@ -19,25 +19,18 @@
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;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import java.util.HashMap;
import java.util.Map;
/**
*
* @author sk89q
*/
public class LocationFlag extends Flag<Location> {
public LocationFlag(String name, RegionGroup defaultGroup) {
@ -60,7 +53,7 @@ public Location parseInput(WorldGuardPlugin plugin, CommandSender sender, String
}
if ("here".equalsIgnoreCase(input)) {
return BukkitUtil.toLocation(player.getLocation());
return toLazyLocation(player.getLocation());
} else if ("none".equalsIgnoreCase(input)) {
return null;
} else {
@ -74,15 +67,7 @@ public Location parseInput(WorldGuardPlugin plugin, CommandSender sender, String
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
);
return new LazyLocation(world.getName(), new Vector(x, y, z), yaw, pitch);
} catch (NumberFormatException ignored) {
}
}
@ -91,10 +76,14 @@ public Location parseInput(WorldGuardPlugin plugin, CommandSender sender, String
}
}
private Location toLazyLocation(org.bukkit.Location location) {
return new LazyLocation(location.getWorld().getName(), BukkitUtil.toVector(location), location.getYaw(), location.getPitch());
}
@Override
public Location unmarshal(Object o) {
if (o instanceof Map<?, ?>) {
Map<?, ?> map = (Map<?, ?>) o;
Map<?, ?> map = (Map<?, ?>) o;
Object rawWorld = map.get("world");
if (rawWorld == null) return null;
@ -114,13 +103,11 @@ public Location unmarshal(Object o) {
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 new LazyLocation(String.valueOf(rawWorld), position, yaw, pitch);
}
return null;
@ -130,7 +117,15 @@ public Location unmarshal(Object o) {
public Object marshal(Location o) {
Vector position = o.getPosition();
Map<String, Object> vec = new HashMap<String, Object>();
vec.put("world", o.getWorld().getName());
if (o instanceof LazyLocation) {
vec.put("world", ((LazyLocation) o).getWorldName());
} else {
try {
vec.put("world", o.getWorld().getName());
} catch (NullPointerException e) {
return null;
}
}
vec.put("x", position.getX());
vec.put("y", position.getY());
vec.put("z", position.getZ());