Fixed chunk loaders can be placed next to each other and overlap chunks (#64)

This commit is contained in:
OmerBenGera 2023-01-13 16:48:36 +02:00
parent 6e0d1fa310
commit 22d8f0d1fb
5 changed files with 111 additions and 76 deletions

View File

@ -2,15 +2,18 @@ package com.bgsoftware.wildloaders.handlers;
import com.bgsoftware.wildloaders.WildLoadersPlugin;
import com.bgsoftware.wildloaders.api.loaders.LoaderData;
import com.bgsoftware.wildloaders.utils.ChunkLoaderChunks;
import com.bgsoftware.wildloaders.utils.ServerVersion;
import com.bgsoftware.wildloaders.utils.database.Database;
import com.bgsoftware.wildloaders.utils.locations.LocationUtils;
import com.bgsoftware.wildloaders.utils.threads.Executor;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.Material;
import java.io.File;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
@ -63,7 +66,10 @@ public final class DataHandler {
continue;
}
plugin.getLoaders().addChunkLoader(loaderData.get(), placer, location, timeLeft);
List<Chunk> chunksToLoad = ChunkLoaderChunks.calculateChunks(loaderData.get(), placer, location);
chunksToLoad.removeIf(chunk -> plugin.getLoaders().getChunkLoader(chunk).isPresent());
plugin.getLoaders().addChunkLoaderWithoutDBSave(loaderData.get(), placer, location, timeLeft, chunksToLoad);
}
});
}

View File

@ -1,11 +1,12 @@
package com.bgsoftware.wildloaders.handlers;
import com.bgsoftware.wildloaders.WildLoadersPlugin;
import com.bgsoftware.wildloaders.api.managers.LoadersManager;
import com.bgsoftware.wildloaders.api.loaders.ChunkLoader;
import com.bgsoftware.wildloaders.api.loaders.LoaderData;
import com.bgsoftware.wildloaders.api.managers.LoadersManager;
import com.bgsoftware.wildloaders.loaders.WChunkLoader;
import com.bgsoftware.wildloaders.loaders.WLoaderData;
import com.bgsoftware.wildloaders.utils.ChunkLoaderChunks;
import com.bgsoftware.wildloaders.utils.chunks.ChunkPosition;
import com.bgsoftware.wildloaders.utils.database.Query;
import com.google.common.collect.Maps;
@ -59,7 +60,8 @@ public final class LoadersHandler implements LoadersManager {
@Override
public ChunkLoader addChunkLoader(LoaderData loaderData, Player whoPlaced, Location location, long timeLeft) {
WChunkLoader chunkLoader = addChunkLoader(loaderData, whoPlaced.getUniqueId(), location, timeLeft);
WChunkLoader chunkLoader = addChunkLoaderWithoutDBSave(loaderData, whoPlaced.getUniqueId(), location, timeLeft,
ChunkLoaderChunks.calculateChunks(loaderData, whoPlaced.getUniqueId(), location));
Query.INSERT_CHUNK_LOADER.insertParameters()
.setLocation(location)
@ -71,8 +73,8 @@ public final class LoadersHandler implements LoadersManager {
return chunkLoader;
}
public WChunkLoader addChunkLoader(LoaderData loaderData, UUID placer, Location location, long timeLeft){
WChunkLoader chunkLoader = new WChunkLoader(loaderData, placer, location, timeLeft);
public WChunkLoader addChunkLoaderWithoutDBSave(LoaderData loaderData, UUID placer, Location location, long timeLeft, List<Chunk> loadedChunks) {
WChunkLoader chunkLoader = new WChunkLoader(loaderData, placer, location, loadedChunks.toArray(new Chunk[0]), timeLeft);
chunkLoaders.put(location, chunkLoader);
for (Chunk loadedChunk : chunkLoader.getLoadedChunks()) {
chunkLoadersByChunks.put(ChunkPosition.of(loadedChunk), chunkLoader);

View File

@ -4,8 +4,10 @@ import com.bgsoftware.wildloaders.Locale;
import com.bgsoftware.wildloaders.WildLoadersPlugin;
import com.bgsoftware.wildloaders.api.loaders.ChunkLoader;
import com.bgsoftware.wildloaders.api.loaders.LoaderData;
import com.bgsoftware.wildloaders.utils.ChunkLoaderChunks;
import com.bgsoftware.wildloaders.utils.chunks.ChunkPosition;
import com.bgsoftware.wildloaders.utils.legacy.Materials;
import org.bukkit.Chunk;
import org.bukkit.GameMode;
import org.bukkit.Location;
import org.bukkit.block.Block;
@ -45,13 +47,15 @@ public final class BlocksListener implements Listener {
return;
}
if(plugin.getLoaders().getChunkLoader(e.getBlock().getLocation().getChunk()).isPresent()){
LoaderData loaderData = optionalLoaderData.get();
for (Chunk chunk : ChunkLoaderChunks.calculateChunks(loaderData, e.getPlayer().getUniqueId(), e.getBlock().getLocation())) {
if (plugin.getLoaders().getChunkLoader(chunk).isPresent()) {
e.setCancelled(true);
Locale.ALREADY_LOADED.send(e.getPlayer());
return;
}
LoaderData loaderData = optionalLoaderData.get();
}
long timeLeft = plugin.getNMSAdapter().getTag(e.getItemInHand(), "loader-time", loaderData.getTimeLeft());
@ -100,7 +104,8 @@ public final class BlocksListener implements Listener {
return;
}
}
} catch (Throwable ignored) {}
} catch (Throwable ignored) {
}
}
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
@ -112,7 +117,8 @@ public final class BlocksListener implements Listener {
return;
}
}
} catch (Throwable ignored) {}
} catch (Throwable ignored) {
}
}
private boolean handleLoaderBreak(Block block, boolean dropItem) {

View File

@ -5,6 +5,7 @@ import com.bgsoftware.wildloaders.api.holograms.Hologram;
import com.bgsoftware.wildloaders.api.loaders.ChunkLoader;
import com.bgsoftware.wildloaders.api.loaders.LoaderData;
import com.bgsoftware.wildloaders.api.npc.ChunkLoaderNPC;
import com.bgsoftware.wildloaders.utils.ChunkLoaderChunks;
import com.bgsoftware.wildloaders.utils.database.Query;
import com.bgsoftware.wildloaders.utils.threads.Executor;
import org.bukkit.Bukkit;
@ -14,7 +15,6 @@ import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.inventory.ItemStack;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
@ -33,11 +33,11 @@ public final class WChunkLoader implements ChunkLoader {
private boolean active = true;
private long timeLeft;
public WChunkLoader(LoaderData loaderData, UUID whoPlaced, Location location, long timeLeft) {
public WChunkLoader(LoaderData loaderData, UUID whoPlaced, Location location, Chunk[] loadedChunks, long timeLeft) {
this.loaderName = loaderData.getName();
this.whoPlaced = whoPlaced;
this.location = location.clone();
this.loadedChunks = calculateChunks(loaderData, whoPlaced, this.location);
this.loadedChunks = loadedChunks;
this.timeLeft = timeLeft;
this.tileEntityChunkLoader = plugin.getNMSAdapter().createLoader(this);
}
@ -125,39 +125,5 @@ public final class WChunkLoader implements ChunkLoader {
return isInfinite() ? plugin.getSettings().infiniteHologramLines : plugin.getSettings().hologramLines;
}
private static Chunk[] calculateChunks(LoaderData loaderData, UUID whoPlaced, Location original) {
List<Chunk> chunkList = new ArrayList<>();
if (loaderData.isChunksSpread()) {
calculateClaimChunks(original.getChunk(), whoPlaced, chunkList);
}
if (chunkList.isEmpty()) {
int chunkX = original.getBlockX() >> 4, chunkZ = original.getBlockZ() >> 4;
for (int x = -loaderData.getChunksRadius(); x <= loaderData.getChunksRadius(); x++)
for (int z = -loaderData.getChunksRadius(); z <= loaderData.getChunksRadius(); z++)
chunkList.add(original.getWorld().getChunkAt(chunkX + x, chunkZ + z));
}
return chunkList.toArray(new Chunk[0]);
}
private static void calculateClaimChunks(Chunk originalChunk, UUID whoPlaced, List<Chunk> chunkList) {
if (!plugin.getProviders().hasChunkAccess(whoPlaced, originalChunk))
return;
chunkList.add(originalChunk);
int chunkX = originalChunk.getX(), chunkZ = originalChunk.getZ();
for (int x = -1; x <= 1; x++) {
for (int z = -1; z <= 1; z++) {
if (x != 0 || z != 0) // We don't want to add the originalChunk again.
calculateClaimChunks(originalChunk.getWorld().getChunkAt(chunkX + x, chunkZ + z), whoPlaced, chunkList);
}
}
}
}

View File

@ -0,0 +1,55 @@
package com.bgsoftware.wildloaders.utils;
import com.bgsoftware.wildloaders.WildLoadersPlugin;
import com.bgsoftware.wildloaders.api.loaders.LoaderData;
import org.bukkit.Chunk;
import org.bukkit.Location;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;
public class ChunkLoaderChunks {
private static final WildLoadersPlugin plugin = WildLoadersPlugin.getPlugin();
private ChunkLoaderChunks() {
}
public static List<Chunk> calculateChunks(LoaderData loaderData, UUID whoPlaced, Location original) {
List<Chunk> chunkList = new LinkedList<>();
if (loaderData.isChunksSpread()) {
calculateClaimChunksRecursive(original.getChunk(), whoPlaced, chunkList);
}
if (chunkList.isEmpty()) {
int chunkX = original.getBlockX() >> 4, chunkZ = original.getBlockZ() >> 4;
for (int x = -loaderData.getChunksRadius(); x <= loaderData.getChunksRadius(); x++)
for (int z = -loaderData.getChunksRadius(); z <= loaderData.getChunksRadius(); z++)
chunkList.add(original.getWorld().getChunkAt(chunkX + x, chunkZ + z));
}
return chunkList;
}
private static void calculateClaimChunksRecursive(Chunk originalChunk, UUID whoPlaced, List<Chunk> chunkList) {
if (!plugin.getProviders().hasChunkAccess(whoPlaced, originalChunk))
return;
chunkList.add(originalChunk);
int chunkX = originalChunk.getX(), chunkZ = originalChunk.getZ();
for (int x = -1; x <= 1; x++) {
for (int z = -1; z <= 1; z++) {
if (x != 0 || z != 0) // We don't want to add the originalChunk again.
calculateClaimChunksRecursive(originalChunk.getWorld().getChunkAt(chunkX + x, chunkZ + z), whoPlaced, chunkList);
}
}
}
}