Fix some issues I caused with Guice

This commit is contained in:
dordsor21 2020-07-24 12:18:36 +01:00
parent 2cfb646065
commit d906a85095
No known key found for this signature in database
GPG Key ID: 1E53E88969FFCF0B
9 changed files with 181 additions and 203 deletions

View File

@ -27,7 +27,7 @@
<dependency>
<groupId>com.plotsquared</groupId>
<artifactId>PlotSquared-Core</artifactId>
<version>5.12.5</version>
<version>6.0.0-SUPER-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>

View File

@ -27,7 +27,6 @@ package com.plotsquared.bukkit.util;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.plotsquared.bukkit.BukkitPlatform;
import com.plotsquared.core.generator.AugmentedUtils;
import com.plotsquared.core.location.Location;
import com.plotsquared.core.location.PlotLoc;
@ -40,13 +39,10 @@ import com.plotsquared.core.queue.ScopedQueueCoordinator;
import com.plotsquared.core.util.ChunkManager;
import com.plotsquared.core.util.RegionManager;
import com.plotsquared.core.util.RegionUtil;
import com.plotsquared.core.util.WorldUtil;
import com.plotsquared.core.util.entity.EntityCategories;
import com.plotsquared.core.util.task.RunnableVal;
import com.sk89q.worldedit.bukkit.BukkitAdapter;
import com.sk89q.worldedit.bukkit.BukkitWorld;
import com.sk89q.worldedit.math.BlockVector2;
import com.sk89q.worldedit.math.BlockVector3;
import com.sk89q.worldedit.regions.CuboidRegion;
import com.sk89q.worldedit.world.block.BaseBlock;
import com.sk89q.worldedit.world.block.BlockTypes;
@ -62,9 +58,7 @@ import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.Semaphore;
import static com.plotsquared.core.util.entity.EntityCategories.CAP_ANIMAL;
import static com.plotsquared.core.util.entity.EntityCategories.CAP_ENTITY;
@ -78,38 +72,8 @@ public class BukkitRegionManager extends RegionManager {
private static final Logger logger =
LoggerFactory.getLogger("P2/" + BukkitRegionManager.class.getSimpleName());
@Inject private WorldUtil worldUtil;
@Inject private GlobalBlockQueue blockQueue;
@Override public Set<BlockVector2> getChunkChunks(String world) {
Set<BlockVector2> chunks = super.getChunkChunks(world);
if (Bukkit.isPrimaryThread()) {
for (Chunk chunk : Objects.requireNonNull(Bukkit.getWorld(world)).getLoadedChunks()) {
BlockVector2 loc = BlockVector2.at(chunk.getX() >> 5, chunk.getZ() >> 5);
chunks.add(loc);
}
} else {
final Semaphore semaphore = new Semaphore(1);
try {
semaphore.acquire();
Bukkit.getScheduler()
.runTask(BukkitPlatform.getPlugin(BukkitPlatform.class), () -> {
for (Chunk chunk : Objects.requireNonNull(Bukkit.getWorld(world))
.getLoadedChunks()) {
BlockVector2 loc =
BlockVector2.at(chunk.getX() >> 5, chunk.getZ() >> 5);
chunks.add(loc);
}
semaphore.release();
});
semaphore.acquireUninterruptibly();
} catch (final Exception e) {
e.printStackTrace();
}
}
return chunks;
}
@Override public boolean handleClear(Plot plot, Runnable whenDone, PlotManager manager) {
return false;
}
@ -189,7 +153,7 @@ public class BukkitRegionManager extends RegionManager {
return count;
}
@Override @Inject public boolean regenerateRegion(final Location pos1, final Location pos2,
@Override public boolean regenerateRegion(final Location pos1, final Location pos2,
final boolean ignoreAugment, final Runnable whenDone) {
final BukkitWorld world = new BukkitWorld((World) pos1.getWorld());

View File

@ -25,7 +25,6 @@
*/
package com.plotsquared.bukkit.util;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.plotsquared.bukkit.BukkitPlatform;
import com.plotsquared.bukkit.player.BukkitPlayer;
@ -36,7 +35,6 @@ import com.plotsquared.core.player.PlotPlayer;
import com.plotsquared.core.util.BlockUtil;
import com.plotsquared.core.util.MathMan;
import com.plotsquared.core.util.PlayerManager;
import com.plotsquared.core.util.RegionManager;
import com.plotsquared.core.util.StringComparison;
import com.plotsquared.core.util.WorldUtil;
import com.plotsquared.core.util.task.TaskManager;
@ -102,6 +100,7 @@ import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.Semaphore;
import java.util.function.Consumer;
import java.util.function.IntConsumer;
import java.util.stream.Stream;
@ -110,13 +109,10 @@ import java.util.stream.Stream;
@Singleton
public class BukkitUtil extends WorldUtil {
private static final Logger logger = LoggerFactory.getLogger("P2/" + BukkitUtil.class.getSimpleName());
private static final Logger logger =
LoggerFactory.getLogger("P2/" + BukkitUtil.class.getSimpleName());
private final Collection<BlockType> tileEntityTypes = new HashSet<>();
@Inject public BukkitUtil(@Nonnull final RegionManager regionManager) {
super(regionManager);
}
/**
* Get a {@link PlotPlayer} from an {@link OfflinePlayer}. If the player is
* online, it returns a complete player. If the player is offline, it creates
@ -156,7 +152,8 @@ public class BukkitUtil extends WorldUtil {
*/
@Nonnull public static Location adapt(@Nonnull final org.bukkit.Location location) {
return Location.at(com.plotsquared.bukkit.util.BukkitWorld.of(location.getWorld()),
MathMan.roundInt(location.getX()), MathMan.roundInt(location.getY()), MathMan.roundInt(location.getZ()));
MathMan.roundInt(location.getX()), MathMan.roundInt(location.getY()),
MathMan.roundInt(location.getZ()));
}
/**
@ -168,8 +165,8 @@ public class BukkitUtil extends WorldUtil {
*/
@Nonnull public static Location adaptComplete(@Nonnull final org.bukkit.Location location) {
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());
MathMan.roundInt(location.getX()), MathMan.roundInt(location.getY()),
MathMan.roundInt(location.getZ()), location.getYaw(), location.getPitch());
}
/**
@ -180,8 +177,8 @@ public class BukkitUtil extends WorldUtil {
* @return Bukkit location
*/
@Nonnull public static org.bukkit.Location adapt(@Nonnull final Location location) {
return new org.bukkit.Location((World) location.getWorld().getPlatformWorld(), location.getX(),
location.getY(), location.getZ());
return new org.bukkit.Location((World) location.getWorld().getPlatformWorld(),
location.getX(), location.getY(), location.getZ());
}
/**
@ -196,12 +193,12 @@ public class BukkitUtil extends WorldUtil {
private static void ensureLoaded(@Nonnull final String world, final int x, final int z,
@Nonnull final Consumer<Chunk> chunkConsumer) {
PaperLib.getChunkAtAsync(Objects.requireNonNull(getWorld(world)),
x >> 4, z >> 4, true)
PaperLib.getChunkAtAsync(Objects.requireNonNull(getWorld(world)), x >> 4, z >> 4, true)
.thenAccept(chunk -> ensureMainThread(chunkConsumer, chunk));
}
private static void ensureLoaded(@Nonnull final Location location, @Nonnull final Consumer<Chunk> chunkConsumer) {
private static void ensureLoaded(@Nonnull final Location location,
@Nonnull final Consumer<Chunk> chunkConsumer) {
PaperLib.getChunkAtAsync(adapt(location), true)
.thenAccept(chunk -> ensureMainThread(chunkConsumer, chunk));
}
@ -211,8 +208,8 @@ public class BukkitUtil extends WorldUtil {
if (Bukkit.isPrimaryThread()) {
consumer.accept(value);
} else {
Bukkit.getScheduler()
.runTask(BukkitPlatform.getPlugin(BukkitPlatform.class), () -> consumer.accept(value));
Bukkit.getScheduler().runTask(BukkitPlatform.getPlugin(BukkitPlatform.class),
() -> consumer.accept(value));
}
}
@ -228,8 +225,8 @@ public class BukkitUtil extends WorldUtil {
return PlotPlayer.wrap(Bukkit.getOfflinePlayer(uuid));
}
@Override public boolean isBlockSame(@Nonnull final BlockState block1,
@Nonnull final BlockState block2) {
@Override
public boolean isBlockSame(@Nonnull final BlockState block1, @Nonnull final BlockState block2) {
if (block1.equals(block2)) {
return true;
}
@ -242,19 +239,18 @@ public class BukkitUtil extends WorldUtil {
return getWorld(worldName) != null;
}
@Override public void getBiome(@Nonnull final String world, final int x,
final int z, @Nonnull final Consumer<BiomeType> result) {
@Override public void getBiome(@Nonnull final String world, final int x, final int z,
@Nonnull final Consumer<BiomeType> result) {
ensureLoaded(world, x, z,
chunk -> result.accept(BukkitAdapter.adapt(getWorld(world).getBiome(x, z))));
}
@Override @Nonnull public BiomeType getBiomeSynchronous(@Nonnull final String world,
final int x, final int z) {
@Override @Nonnull
public BiomeType getBiomeSynchronous(@Nonnull final String world, final int x, final int z) {
return BukkitAdapter.adapt(Objects.requireNonNull(getWorld(world)).getBiome(x, z));
}
@Override
public void getHighestBlock(@Nonnull final String world, final int x, final int z,
@Override public void getHighestBlock(@Nonnull final String world, final int x, final int z,
@Nonnull final IntConsumer result) {
ensureLoaded(world, x, z, chunk -> {
final World bukkitWorld = Objects.requireNonNull(getWorld(world));
@ -282,8 +278,7 @@ public class BukkitUtil extends WorldUtil {
}
@Override @Nonnegative
public int getHighestBlockSynchronous(@Nonnull final String world,
final int x, final int z) {
public int getHighestBlockSynchronous(@Nonnull final String world, final int x, final int z) {
final World bukkitWorld = Objects.requireNonNull(getWorld(world));
// Skip top and bottom block
int air = 1;
@ -305,8 +300,7 @@ public class BukkitUtil extends WorldUtil {
return bukkitWorld.getMaxHeight() - 1;
}
@Override @Nonnull
public String[] getSignSynchronous(@Nonnull final Location location) {
@Override @Nonnull public String[] getSignSynchronous(@Nonnull final Location location) {
Block block = Objects.requireNonNull(getWorld(location.getWorldName()))
.getBlockAt(location.getX(), location.getY(), location.getZ());
try {
@ -323,8 +317,7 @@ public class BukkitUtil extends WorldUtil {
return new String[0];
}
@Override @Nonnull
public Location getSpawn(@Nonnull final String world) {
@Override @Nonnull public Location getSpawn(@Nonnull final String world) {
final org.bukkit.Location temp = getWorld(world).getSpawnLocation();
return Location
.at(world, temp.getBlockX(), temp.getBlockY(), temp.getBlockZ(), temp.getYaw(),
@ -392,8 +385,7 @@ public class BukkitUtil extends WorldUtil {
}
@Override
public void setBiomes(@Nonnull final String worldName,
@Nonnull final CuboidRegion region,
public void setBiomes(@Nonnull final String worldName, @Nonnull final CuboidRegion region,
@Nonnull final BiomeType biomeType) {
final World world = getWorld(worldName);
if (world == null) {
@ -412,7 +404,8 @@ public class BukkitUtil extends WorldUtil {
}
}
@Override @Nonnull public com.sk89q.worldedit.world.World getWeWorld(@Nonnull final String world) {
@Override @Nonnull
public com.sk89q.worldedit.world.World getWeWorld(@Nonnull final String world) {
return new BukkitWorld(Bukkit.getWorld(world));
}
@ -420,15 +413,14 @@ public class BukkitUtil extends WorldUtil {
Bukkit.getWorld(world).refreshChunk(x, z);
}
@Override
public void getBlock(@Nonnull final Location location,
@Override public void getBlock(@Nonnull final Location location,
@Nonnull final Consumer<BlockState> result) {
ensureLoaded(location, chunk -> {
final World world = getWorld(location.getWorldName());
final Block block = Objects.requireNonNull(world)
.getBlockAt(location.getX(), location.getY(), location.getZ());
result.accept(Objects.requireNonNull(BukkitAdapter
.asBlockType(block.getType())).getDefaultState());
result.accept(Objects.requireNonNull(BukkitAdapter.asBlockType(block.getType()))
.getDefaultState());
});
}
@ -436,24 +428,20 @@ public class BukkitUtil extends WorldUtil {
final World world = getWorld(location.getWorldName());
final Block block = Objects.requireNonNull(world)
.getBlockAt(location.getX(), location.getY(), location.getZ());
return Objects.requireNonNull(BukkitAdapter
.asBlockType(block.getType())).getDefaultState();
return Objects.requireNonNull(BukkitAdapter.asBlockType(block.getType())).getDefaultState();
}
@Override @Nonnegative public double getHealth(@Nonnull final PlotPlayer player) {
return Objects.requireNonNull(Bukkit
.getPlayer(player.getUUID())).getHealth();
return Objects.requireNonNull(Bukkit.getPlayer(player.getUUID())).getHealth();
}
@Override @Nonnegative public int getFoodLevel(@Nonnull final PlotPlayer<?> player) {
return Objects.requireNonNull(Bukkit.getPlayer(player.getUUID()))
.getFoodLevel();
return Objects.requireNonNull(Bukkit.getPlayer(player.getUUID())).getFoodLevel();
}
@Override public void setHealth(@Nonnull final PlotPlayer<?> player,
@Nonnegative final double health) {
Objects.requireNonNull(Bukkit.getPlayer(player.getUUID()))
.setHealth(health);
@Override
public void setHealth(@Nonnull final PlotPlayer<?> player, @Nonnegative final double health) {
Objects.requireNonNull(Bukkit.getPlayer(player.getUUID())).setHealth(health);
}
@Override public void setFoodLevel(@Nonnull final PlotPlayer<?> player,
@ -461,8 +449,7 @@ public class BukkitUtil extends WorldUtil {
Bukkit.getPlayer(player.getUUID()).setFoodLevel(foodLevel);
}
@Override @Nonnull
public Set<com.sk89q.worldedit.world.entity.EntityType> getTypesInCategory(
@Override @Nonnull public Set<com.sk89q.worldedit.world.entity.EntityType> getTypesInCategory(
@Nonnull final String category) {
final Collection<Class<?>> allowedInterfaces = new HashSet<>();
switch (category) {
@ -567,11 +554,38 @@ public class BukkitUtil extends WorldUtil {
}
@Override @Nonnegative
public int getTileEntityCount(@Nonnull final String world,
@Nonnull final BlockVector2 chunk) {
public int getTileEntityCount(@Nonnull final String world, @Nonnull final BlockVector2 chunk) {
return Objects.requireNonNull(getWorld(world)).
getChunkAt(chunk.getBlockX(), chunk.getBlockZ())
.getTileEntities().length;
getChunkAt(chunk.getBlockX(), chunk.getBlockZ()).getTileEntities().length;
}
@Override public Set<BlockVector2> getChunkChunks(String world) {
Set<BlockVector2> chunks = super.getChunkChunks(world);
if (Bukkit.isPrimaryThread()) {
for (Chunk chunk : Objects.requireNonNull(Bukkit.getWorld(world)).getLoadedChunks()) {
BlockVector2 loc = BlockVector2.at(chunk.getX() >> 5, chunk.getZ() >> 5);
chunks.add(loc);
}
} else {
final Semaphore semaphore = new Semaphore(1);
try {
semaphore.acquire();
Bukkit.getScheduler()
.runTask(BukkitPlatform.getPlugin(BukkitPlatform.class), () -> {
for (Chunk chunk : Objects.requireNonNull(Bukkit.getWorld(world))
.getLoadedChunks()) {
BlockVector2 loc =
BlockVector2.at(chunk.getX() >> 5, chunk.getZ() >> 5);
chunks.add(loc);
}
semaphore.release();
});
semaphore.acquireUninterruptibly();
} catch (final Exception e) {
e.printStackTrace();
}
}
return chunks;
}
}

View File

@ -33,6 +33,7 @@ import com.plotsquared.core.plot.world.PlotAreaManager;
import com.plotsquared.core.util.MainUtil;
import com.plotsquared.core.util.RegionManager;
import com.plotsquared.core.util.StringMan;
import com.plotsquared.core.util.WorldUtil;
import com.plotsquared.core.util.entity.EntityCategories;
import com.plotsquared.core.util.entity.EntityCategory;
import com.plotsquared.core.util.query.PlotQuery;
@ -57,12 +58,12 @@ public class Debug extends SubCommand {
private static final Logger logger = LoggerFactory.getLogger("P2/" + Debug.class.getSimpleName());
private final PlotAreaManager plotAreaManager;
private final RegionManager regionManager;
private final WorldUtil worldUtil;
@Inject public Debug(@Nonnull final PlotAreaManager plotAreaManager,
@Nonnull final RegionManager regionManager) {
@Nonnull final WorldUtil worldUtil) {
this.plotAreaManager = plotAreaManager;
this.regionManager = regionManager;
this.worldUtil = worldUtil;
}
@Override public boolean onCommand(PlotPlayer<?> player, String[] args) {
@ -78,7 +79,7 @@ public class Debug extends SubCommand {
final long start = System.currentTimeMillis();
MainUtil.sendMessage(player, "Fetching loaded chunks...");
TaskManager.runTaskAsync(() -> MainUtil.sendMessage(player,
"Loaded chunks: " + this.regionManager.getChunkChunks(player.getLocation().getWorldName()).size() + "(" + (
"Loaded chunks: " + this.worldUtil.getChunkChunks(player.getLocation().getWorldName()).size() + "(" + (
System.currentTimeMillis() - start) + "ms) using thread: " + Thread
.currentThread().getName()));
return true;

View File

@ -98,7 +98,7 @@ public class Trim extends SubCommand {
if (ExpireManager.IMP != null) {
plots.removeAll(ExpireManager.IMP.getPendingExpired());
}
result.value1 = new HashSet<>(PlotSquared.platform().getRegionManager().getChunkChunks(world));
result.value1 = new HashSet<>(PlotSquared.platform().getWorldUtil().getChunkChunks(world));
result.value2 = new HashSet<>();
MainUtil.sendMessage(null, " - MCA #: " + result.value1.size());
MainUtil.sendMessage(null, " - CHUNKS: " + (result.value1.size() * 1024) + " (max)");

View File

@ -406,7 +406,7 @@ public class HybridUtils {
return false;
}
HybridUtils.UPDATE = true;
Set<BlockVector2> regions = this.regionManager.getChunkChunks(area.getWorldName());
Set<BlockVector2> regions = this.worldUtil.getChunkChunks(area.getWorldName());
return scheduleRoadUpdate(area, regions, extend, new HashSet<>());
}

View File

@ -78,31 +78,6 @@ public abstract class RegionManager {
*/
public abstract int[] countEntities(Plot plot);
public Set<BlockVector2> getChunkChunks(String world) {
File folder =
new File(PlotSquared.platform().getWorldContainer(), world + File.separator + "region");
File[] regionFiles = folder.listFiles();
if (regionFiles == null) {
throw new RuntimeException(
"Could not find worlds folder: " + folder + " ? (no read access?)");
}
HashSet<BlockVector2> chunks = new HashSet<>();
for (File file : regionFiles) {
String name = file.getName();
if (name.endsWith("mca")) {
String[] split = name.split("\\.");
try {
int x = Integer.parseInt(split[1]);
int z = Integer.parseInt(split[2]);
BlockVector2 loc = BlockVector2.at(x, z);
chunks.add(loc);
} catch (NumberFormatException ignored) {
}
}
}
return chunks;
}
public void deleteRegionFiles(final String world, final Collection<BlockVector2> chunks,
final Runnable whenDone) {
TaskManager.runTaskAsync(() -> {

View File

@ -53,6 +53,7 @@ import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.util.Collection;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
@ -65,12 +66,6 @@ import java.util.zip.ZipOutputStream;
public abstract class WorldUtil {
private final RegionManager regionManager;
public WorldUtil(@Nonnull final RegionManager regionManager) {
this.regionManager = regionManager;
}
/**
* Set the biome in a region
*
@ -196,8 +191,8 @@ public abstract class WorldUtil {
* @return Result
* @deprecated Use {@link #getHighestBlock(String, int, int, IntConsumer)}
*/
@Deprecated @Nonnegative
public abstract int getHighestBlockSynchronous(@Nonnull String world, int x, int z);
@Deprecated @Nonnegative public abstract int getHighestBlockSynchronous(@Nonnull String world,
int x, int z);
/**
* Set the text in a sign
@ -240,7 +235,8 @@ public abstract class WorldUtil {
public void upload(@Nonnull final Plot plot, @Nullable final UUID uuid,
@Nullable final String file, @Nonnull final RunnableVal<URL> whenDone) {
plot.getHome(home -> SchematicHandler.upload(uuid, file, "zip", new RunnableVal<OutputStream>() {
plot.getHome(
home -> SchematicHandler.upload(uuid, file, "zip", new RunnableVal<OutputStream>() {
@Override public void run(OutputStream output) {
try (final ZipOutputStream zos = new ZipOutputStream(output)) {
File dat = getDat(plot.getWorldName());
@ -275,7 +271,7 @@ public abstract class WorldUtil {
int brz = bot.getZ() >> 9;
int trx = top.getX() >> 9;
int trz = top.getZ() >> 9;
Set<BlockVector2> files = regionManager.getChunkChunks(bot.getWorldName());
Set<BlockVector2> files = getChunkChunks(bot.getWorldName());
for (BlockVector2 mca : files) {
if (mca.getX() >= brx && mca.getX() <= trx && mca.getZ() >= brz
&& mca.getZ() <= trz) {
@ -328,6 +324,32 @@ public abstract class WorldUtil {
return null;
}
public Set<BlockVector2> getChunkChunks(String world) {
File folder =
new File(PlotSquared.platform().getWorldContainer(), world + File.separator + "region");
File[] regionFiles = folder.listFiles();
if (regionFiles == null) {
throw new RuntimeException(
"Could not find worlds folder: " + folder + " ? (no read access?)");
}
HashSet<BlockVector2> chunks = new HashSet<>();
for (File file : regionFiles) {
String name = file.getName();
if (name.endsWith("mca")) {
String[] split = name.split("\\.");
try {
int x = Integer.parseInt(split[1]);
int z = Integer.parseInt(split[2]);
BlockVector2 loc = BlockVector2.at(x, z);
chunks.add(loc);
} catch (NumberFormatException ignored) {
}
}
}
return chunks;
}
/**
* Check if two blocks are the same type)
*

View File

@ -31,7 +31,7 @@ ext {
}
def ver = "6.0.0"
def versuffix = "SUPER-SNAPSHOT"
def versuffix = "-SUPER-SNAPSHOT"
ext {
if (project.hasProperty("versionsuffix")) {
versuffix = "-$versionsuffix"
@ -125,6 +125,8 @@ subprojects { subproject ->
dependencies {
include(dependency("org.json:json:20200518"))
include(dependency("net.kyori:text-api:3.0.2"))
include(dependency("javax.inject:javax.inject:1"))
include(dependency("aopalliance:aopalliance:1.0"))
}
relocate("io.papermc.lib", "com.plotsquared.bukkit.paperlib")
relocate("org.json", "com.plotsquared.json") {