Finalize immutable locations and add a platform world implementation

This commit is contained in:
Alexander Söderberg
2020-07-10 12:21:29 +02:00
parent cf1b027db9
commit 5341015cb1
51 changed files with 503 additions and 246 deletions

View File

@ -50,6 +50,7 @@ import com.plotsquared.bukkit.util.BukkitRegionManager;
import com.plotsquared.bukkit.util.BukkitSetupUtils;
import com.plotsquared.bukkit.util.BukkitTaskManager;
import com.plotsquared.bukkit.util.BukkitUtil;
import com.plotsquared.bukkit.util.BukkitWorld;
import com.plotsquared.bukkit.util.SetGenCB;
import com.plotsquared.bukkit.util.UpdateUtility;
import com.plotsquared.bukkit.uuid.BungeePermsUUIDService;
@ -1168,4 +1169,8 @@ public final class BukkitPlatform extends JavaPlugin implements Listener, PlotPl
return this.playerManager;
}
@Override @NotNull public com.plotsquared.core.location.World<?> getPlatformWorld(@NotNull final String worldName) {
return BukkitWorld.of(worldName);
}
}

View File

@ -150,23 +150,23 @@ public class ChunkListener implements Listener {
int z = chunkZ << 4;
int x2 = x + 15;
int z2 = z + 15;
Plot plot = new Location(world, x, 1, z).getOwnedPlotAbs();
Plot plot = Location.at(world, x, 1, z).getOwnedPlotAbs();
if (plot != null && plot.hasOwner()) {
return true;
}
plot = new Location(world, x2, 1, z2).getOwnedPlotAbs();
plot = Location.at(world, x2, 1, z2).getOwnedPlotAbs();
if (plot != null && plot.hasOwner()) {
return true;
}
plot = new Location(world, x2, 1, z).getOwnedPlotAbs();
plot = Location.at(world, x2, 1, z).getOwnedPlotAbs();
if (plot != null && plot.hasOwner()) {
return true;
}
plot = new Location(world, x, 1, z2).getOwnedPlotAbs();
plot = Location.at(world, x, 1, z2).getOwnedPlotAbs();
if (plot != null && plot.hasOwner()) {
return true;
}
plot = new Location(world, x + 7, 1, z + 7).getOwnedPlotAbs();
plot = Location.at(world, x + 7, 1, z + 7).getOwnedPlotAbs();
return plot != null && plot.hasOwner();
}

View File

@ -305,7 +305,7 @@ public class PaperListener implements Listener {
return;
}
Location location = BukkitUtil.getLocation(entity);
if (!PlotSquared.get().getPlotAreaManager().hasPlotArea(location.getWorld())) {
if (!PlotSquared.get().getPlotAreaManager().hasPlotArea(location.getWorldName())) {
return;
}
PlotPlayer<?> pp = BukkitUtil.getPlayer((Player) shooter);

View File

@ -256,7 +256,7 @@ public class PlayerEvents extends PlotListener implements Listener {
for (final PlotPlayer<?> player : PlotSquared.platform().getPlayerManager().getPlayers()) {
Location location = player.getLocation();
if (location.getWorld().equals(world)) {
if (location.getWorldName().equals(world)) {
if (16 * Math.abs(location.getX() - x) / 16 > distance
|| 16 * Math.abs(location.getZ() - z) / 16 > distance) {
continue;
@ -455,16 +455,16 @@ public class PlayerEvents extends PlotListener implements Listener {
}
switch (piston.getFacing()) {
case EAST:
location.setX(location.getX() + 1);
location = location.add(1, 0, 0);
break;
case SOUTH:
location.setX(location.getX() - 1);
location = location.add(-1, 0, 0);
break;
case WEST:
location.setZ(location.getZ() + 1);
location = location.add(0, 0, 1);
break;
case NORTH:
location.setZ(location.getZ() - 1);
location = location.add(0, 0, -1);
break;
}
Plot newPlot = area.getOwnedPlotAbs(location);
@ -490,7 +490,7 @@ public class PlayerEvents extends PlotListener implements Listener {
return;
}
Location location = BukkitUtil.getLocation(entity);
if (!PlotSquared.get().getPlotAreaManager().hasPlotArea(location.getWorld())) {
if (!PlotSquared.get().getPlotAreaManager().hasPlotArea(location.getWorldName())) {
return;
}
PlotPlayer<Player> pp = BukkitUtil.getPlayer((Player) shooter);
@ -504,7 +504,7 @@ public class PlayerEvents extends PlotListener implements Listener {
@EventHandler public boolean onProjectileHit(ProjectileHitEvent event) {
Projectile entity = event.getEntity();
Location location = BukkitUtil.getLocation(entity);
if (!PlotSquared.get().getPlotAreaManager().hasPlotArea(location.getWorld())) {
if (!PlotSquared.get().getPlotAreaManager().hasPlotArea(location.getWorldName())) {
return true;
}
PlotArea area = location.getPlotArea();
@ -1078,7 +1078,7 @@ public class PlayerEvents extends PlotListener implements Listener {
PlotArea area = location.getPlotArea();
boolean plotArea = location.isPlotArea();
if (!plotArea) {
if (!PlotSquared.get().getPlotAreaManager().hasPlotArea(location.getWorld())) {
if (!PlotSquared.get().getPlotAreaManager().hasPlotArea(location.getWorldName())) {
return;
}
return;
@ -1505,7 +1505,7 @@ public class PlayerEvents extends PlotListener implements Listener {
Vector relative = new Vector(face.getModX(), face.getModY(), face.getModZ());
PlotArea area = location.getPlotArea();
if (area == null) {
if (!PlotSquared.get().getPlotAreaManager().hasPlotArea(location.getWorld())) {
if (!PlotSquared.get().getPlotAreaManager().hasPlotArea(location.getWorldName())) {
return;
}
for (Block block1 : event.getBlocks()) {
@ -1542,7 +1542,7 @@ public class PlayerEvents extends PlotListener implements Listener {
Location location = BukkitUtil.getLocation(block.getLocation());
PlotArea area = location.getPlotArea();
if (area == null) {
if (!PlotSquared.get().getPlotAreaManager().hasPlotArea(location.getWorld())) {
if (!PlotSquared.get().getPlotAreaManager().hasPlotArea(location.getWorldName())) {
return;
}
if (this.pistonBlocks) {
@ -1840,7 +1840,7 @@ public class PlayerEvents extends PlotListener implements Listener {
public void onPotionSplash(LingeringPotionSplashEvent event) {
Projectile entity = event.getEntity();
Location location = BukkitUtil.getLocation(entity);
if (!PlotSquared.get().getPlotAreaManager().hasPlotArea(location.getWorld())) {
if (!PlotSquared.get().getPlotAreaManager().hasPlotArea(location.getWorldName())) {
return;
}
if (!this.onProjectileHit(event)) {
@ -1907,7 +1907,7 @@ public class PlayerEvents extends PlotListener implements Listener {
public void onBigBoom(BlockExplodeEvent event) {
Block block = event.getBlock();
Location location = BukkitUtil.getLocation(block.getLocation());
String world = location.getWorld();
String world = location.getWorldName();
if (!PlotSquared.get().getPlotAreaManager().hasPlotArea(world)) {
return;
}
@ -2707,7 +2707,7 @@ public class PlayerEvents extends PlotListener implements Listener {
public void onPotionSplash(PotionSplashEvent event) {
ThrownPotion damager = event.getPotion();
Location location = BukkitUtil.getLocation(damager);
if (!PlotSquared.get().getPlotAreaManager().hasPlotArea(location.getWorld())) {
if (!PlotSquared.get().getPlotAreaManager().hasPlotArea(location.getWorldName())) {
return;
}
int count = 0;
@ -2737,7 +2737,7 @@ public class PlayerEvents extends PlotListener implements Listener {
public void onEntityDamageByEntityEvent(EntityDamageByEntityEvent event) {
Entity damager = event.getDamager();
Location location = BukkitUtil.getLocation(damager);
if (!PlotSquared.get().getPlotAreaManager().hasPlotArea(location.getWorld())) {
if (!PlotSquared.get().getPlotAreaManager().hasPlotArea(location.getWorldName())) {
return;
}
Entity victim = event.getEntity();

View File

@ -241,7 +241,7 @@ public class BukkitPlayer extends PlotPlayer<Player> {
return;
}
final org.bukkit.Location bukkitLocation =
new org.bukkit.Location(BukkitUtil.getWorld(location.getWorld()), location.getX() + 0.5,
new org.bukkit.Location(BukkitUtil.getWorld(location.getWorldName()), location.getX() + 0.5,
location.getY(), location.getZ() + 0.5, location.getYaw(), location.getPitch());
PaperLib.teleportAsync(player, bukkitLocation, getTeleportCause(cause));
}
@ -259,7 +259,7 @@ public class BukkitPlayer extends PlotPlayer<Player> {
@Override public void setCompassTarget(Location location) {
this.player.setCompassTarget(
new org.bukkit.Location(BukkitUtil.getWorld(location.getWorld()), location.getX(),
new org.bukkit.Location(BukkitUtil.getWorld(location.getWorldName()), location.getX(),
location.getY(), location.getZ()));
}

View File

@ -62,7 +62,7 @@ public class GenChunk extends ScopedLocalBlockQueue {
@Getter @Setter private ChunkData chunkData = null;
public GenChunk() {
super(null, new Location(null, 0, 0, 0), new Location(null, 15, 255, 15));
super(null, Location.at("", 0, 0, 0), Location.at("", 15, 255, 15));
this.biomes = Biome.values();
}
@ -196,11 +196,11 @@ public class GenChunk extends ScopedLocalBlockQueue {
}
@Override public Location getMax() {
return new Location(getWorld(), 15 + (getX() << 4), 255, 15 + (getZ() << 4));
return Location.at(getWorld(), 15 + (getX() << 4), 255, 15 + (getZ() << 4));
}
@Override public Location getMin() {
return new Location(getWorld(), getX() << 4, 0, getZ() << 4);
return Location.at(getWorld(), getX() << 4, 0, getZ() << 4);
}
public GenChunk clone() {

View File

@ -198,9 +198,9 @@ public class BukkitRegionManager extends RegionManager {
final CuboidRegion region =
RegionUtil.createRegion(pos1.getX(), pos2.getX(), pos1.getZ(), pos2.getZ());
final World oldWorld = Bukkit.getWorld(pos1.getWorld());
final World oldWorld = Bukkit.getWorld(pos1.getWorldName());
final BukkitWorld oldBukkitWorld = new BukkitWorld(oldWorld);
final World newWorld = Bukkit.getWorld(newPos.getWorld());
final World newWorld = Bukkit.getWorld(newPos.getWorldName());
assert newWorld != null;
assert oldWorld != null;
final String newWorldName = newWorld.getName();
@ -248,7 +248,7 @@ public class BukkitRegionManager extends RegionManager {
@Override
public boolean regenerateRegion(final Location pos1, final Location pos2,
final boolean ignoreAugment, final Runnable whenDone) {
final String world = pos1.getWorld();
final String world = pos1.getWorldName();
final int p1x = pos1.getX();
final int p1z = pos1.getZ();
@ -401,7 +401,7 @@ public class BukkitRegionManager extends RegionManager {
}
@Override public void clearAllEntities(Location pos1, Location pos2) {
String world = pos1.getWorld();
String world = pos1.getWorldName();
List<Entity> entities = BukkitUtil.getEntities(world);
int bx = pos1.getX();
int bz = pos1.getZ();
@ -428,8 +428,8 @@ public class BukkitRegionManager extends RegionManager {
RegionUtil.createRegion(bot1.getX(), top1.getX(), bot1.getZ(), top1.getZ());
CuboidRegion region2 =
RegionUtil.createRegion(bot2.getX(), top2.getX(), bot2.getZ(), top2.getZ());
final World world1 = Bukkit.getWorld(bot1.getWorld());
final World world2 = Bukkit.getWorld(bot2.getWorld());
final World world1 = Bukkit.getWorld(bot1.getWorldName());
final World world2 = Bukkit.getWorld(bot2.getWorldName());
checkNotNull(world1, "Critical error during swap.");
checkNotNull(world2, "Critical error during swap.");
int relX = bot2.getX() - bot1.getX();
@ -456,9 +456,9 @@ public class BukkitRegionManager extends RegionManager {
@Override
public void setBiome(final CuboidRegion region, final int extendBiome, final BiomeType biome,
final String world, final Runnable whenDone) {
Location pos1 = new Location(world, region.getMinimumPoint().getX() - extendBiome,
Location pos1 = Location.at(world, region.getMinimumPoint().getX() - extendBiome,
region.getMinimumPoint().getY(), region.getMinimumPoint().getZ() - extendBiome);
Location pos2 = new Location(world, region.getMaximumPoint().getX() + extendBiome,
Location pos2 = Location.at(world, region.getMaximumPoint().getX() + extendBiome,
region.getMaximumPoint().getY(), region.getMaximumPoint().getZ() + extendBiome);
final LocalBlockQueue queue = GlobalBlockQueue.IMP.getNewQueue(world, false);

View File

@ -276,18 +276,18 @@ public class BukkitUtil extends WorldUtil {
}
public static Location getLocation(@NonNull final org.bukkit.Location location) {
return new Location(location.getWorld().getName(), MathMan.roundInt(location.getX()),
MathMan.roundInt(location.getY()), MathMan.roundInt(location.getZ()));
return Location.at(com.plotsquared.bukkit.util.BukkitWorld.of(location.getWorld()),
MathMan.roundInt(location.getX()), MathMan.roundInt(location.getY()), MathMan.roundInt(location.getZ()));
}
public static Location getLocationFull(@NonNull final org.bukkit.Location location) {
return new Location(location.getWorld().getName(), MathMan.roundInt(location.getX()),
MathMan.roundInt(location.getY()), MathMan.roundInt(location.getZ()), location.getYaw(),
return Location.at(com.plotsquared.bukkit.util.BukkitWorld.of(location.getWorld()),
MathMan.roundInt(location.getX()), MathMan.roundInt(location.getY()), MathMan.roundInt(location.getZ()), location.getYaw(),
location.getPitch());
}
public static org.bukkit.Location getLocation(@NonNull final Location location) {
return new org.bukkit.Location(getWorld(location.getWorld()), location.getX(),
return new org.bukkit.Location((World) location.getWorld().getPlatformWorld(), location.getX(),
location.getY(), location.getZ());
}
@ -311,13 +311,13 @@ public class BukkitUtil extends WorldUtil {
public static Location getLocation(@NonNull final Entity entity) {
final org.bukkit.Location location = entity.getLocation();
String world = location.getWorld().getName();
return new Location(world, location.getBlockX(), location.getBlockY(),
return Location.at(world, location.getBlockX(), location.getBlockY(),
location.getBlockZ());
}
@NotNull public static Location getLocationFull(@NonNull final Entity entity) {
final org.bukkit.Location location = entity.getLocation();
return new Location(location.getWorld().getName(), MathMan.roundInt(location.getX()),
return Location.at(location.getWorld().getName(), MathMan.roundInt(location.getX()),
MathMan.roundInt(location.getY()), MathMan.roundInt(location.getZ()), location.getYaw(),
location.getPitch());
}
@ -409,7 +409,7 @@ public class BukkitUtil extends WorldUtil {
}
@Override @Nullable public String[] getSignSynchronous(@NonNull final Location location) {
Block block = getWorld(location.getWorld())
Block block = getWorld(location.getWorldName())
.getBlockAt(location.getX(), location.getY(), location.getZ());
return TaskManager.IMP.sync(new RunnableVal<String[]>() {
@Override public void run(String[] value) {
@ -423,12 +423,12 @@ public class BukkitUtil extends WorldUtil {
@Override public Location getSpawn(@NonNull final String world) {
final org.bukkit.Location temp = getWorld(world).getSpawnLocation();
return new Location(world, temp.getBlockX(), temp.getBlockY(), temp.getBlockZ(),
return Location.at(world, temp.getBlockX(), temp.getBlockY(), temp.getBlockZ(),
temp.getYaw(), temp.getPitch());
}
@Override public void setSpawn(@NonNull final Location location) {
final World world = getWorld(location.getWorld());
final World world = getWorld(location.getWorldName());
if (world != null) {
world.setSpawnLocation(location.getX(), location.getY(), location.getZ());
}
@ -526,14 +526,14 @@ public class BukkitUtil extends WorldUtil {
@Override
public void getBlock(@NonNull final Location location, final Consumer<BlockState> result) {
ensureLoaded(location, chunk -> {
final World world = getWorld(location.getWorld());
final World world = getWorld(location.getWorldName());
final Block block = world.getBlockAt(location.getX(), location.getY(), location.getZ());
result.accept(BukkitAdapter.asBlockType(block.getType()).getDefaultState());
});
}
@Override public BlockState getBlockSynchronous(@NonNull final Location location) {
final World world = getWorld(location.getWorld());
final World world = getWorld(location.getWorldName());
final Block block = world.getBlockAt(location.getX(), location.getY(), location.getZ());
return BukkitAdapter.asBlockType(block.getType()).getDefaultState();
}

View File

@ -0,0 +1,85 @@
/*
* _____ _ _ _____ _
* | __ \| | | | / ____| | |
* | |__) | | ___ | |_| (___ __ _ _ _ __ _ _ __ ___ __| |
* | ___/| |/ _ \| __|\___ \ / _` | | | |/ _` | '__/ _ \/ _` |
* | | | | (_) | |_ ____) | (_| | |_| | (_| | | | __/ (_| |
* |_| |_|\___/ \__|_____/ \__, |\__,_|\__,_|_| \___|\__,_|
* | |
* |_|
* PlotSquared plot management system for Minecraft
* Copyright (C) 2020 IntellectualSites
*
* 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 <http://www.gnu.org/licenses/>.
*/
package com.plotsquared.bukkit.util;
import com.google.common.collect.Maps;
import com.plotsquared.core.location.World;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import org.bukkit.Bukkit;
import org.jetbrains.annotations.NotNull;
import java.util.Map;
@EqualsAndHashCode @ToString public class BukkitWorld implements World<org.bukkit.World> {
private static final Map<String, BukkitWorld> worldMap = Maps.newHashMap();
private final org.bukkit.World world;
private BukkitWorld(@NotNull final org.bukkit.World world) {
this.world = world;
}
/**
* Get a new {@link BukkitWorld} from a world name
*
* @param worldName World name
* @return World instance
*/
@NotNull public static BukkitWorld of(@NotNull final String worldName) {
final org.bukkit.World bukkitWorld = Bukkit.getWorld(worldName);
if (bukkitWorld == null) {
throw new IllegalArgumentException(String.format("There is no world with the name '%s'", worldName));
}
return of(bukkitWorld);
}
/**
* Get a new {@link BukkitWorld} from a Bukkit world
*
* @param world Bukkit world
* @return World instance
*/
@NotNull public static BukkitWorld of(@NotNull final org.bukkit.World world) {
BukkitWorld bukkitWorld = worldMap.get(world.getName());
if (bukkitWorld != null && bukkitWorld.getPlatformWorld().equals(world)) {
return bukkitWorld;
}
bukkitWorld = new BukkitWorld(world);
worldMap.put(world.getName(), bukkitWorld);
return bukkitWorld;
}
@NotNull @Override public org.bukkit.World getPlatformWorld() {
return this.world;
}
@Override public @NotNull String getName() {
return this.world.getName();
}
}