Skip to content

Commit

Permalink
Added LazyWorldsProvider used for getting info about worlds without l…
Browse files Browse the repository at this point in the history
…oading them
  • Loading branch information
OmerBenGera committed Oct 17, 2022
1 parent 0649d47 commit ebcc661
Show file tree
Hide file tree
Showing 23 changed files with 411 additions and 100 deletions.
Expand Up @@ -7,10 +7,12 @@
import com.bgsoftware.superiorskyblock.api.factory.PlayersFactory;
import com.bgsoftware.superiorskyblock.api.island.Island;
import com.bgsoftware.superiorskyblock.api.island.bank.BankTransaction;
import com.bgsoftware.superiorskyblock.api.world.WorldInfo;
import com.bgsoftware.superiorskyblock.api.wrappers.BlockOffset;
import com.bgsoftware.superiorskyblock.api.wrappers.BlockPosition;
import com.bgsoftware.superiorskyblock.api.wrappers.SuperiorPlayer;
import org.bukkit.Location;
import org.bukkit.World;

import javax.annotation.Nullable;
import java.math.BigDecimal;
Expand Down Expand Up @@ -141,4 +143,12 @@ public interface FactoriesManager {
BankTransaction createTransaction(@Nullable UUID player, BankAction action, int position,
long time, String failureReason, BigDecimal amount);

/**
* Create a new world info.
*
* @param worldName The name of the world.
* @param environment The environment of the world.
*/
WorldInfo createWorldInfo(String worldName, World.Environment environment);

}
Expand Up @@ -5,6 +5,7 @@
import com.bgsoftware.superiorskyblock.api.island.IslandPreview;
import com.bgsoftware.superiorskyblock.api.island.SortingType;
import com.bgsoftware.superiorskyblock.api.island.container.IslandsContainer;
import com.bgsoftware.superiorskyblock.api.world.WorldInfo;
import com.bgsoftware.superiorskyblock.api.world.algorithm.IslandCreationAlgorithm;
import com.bgsoftware.superiorskyblock.api.wrappers.SuperiorPlayer;
import org.bukkit.Chunk;
Expand Down Expand Up @@ -244,6 +245,28 @@ void createIsland(SuperiorPlayer superiorPlayer, String schemName, BigDecimal bo
@Nullable
World getIslandsWorld(Island island, World.Environment environment);

/**
* Get the {@link WorldInfo} of the world of an island by the environment.
* The world might not be loaded at the time of calling this method.
*
* @param island The island to check.
* @param environment The world environment.
* @return The world info for the given environment, or null if this environment is not enabled.
*/
@Nullable
WorldInfo getIslandsWorldInfo(Island island, World.Environment environment);

/**
* Get the {@link WorldInfo} of the world of an island by its name.
* The world might not be loaded at the time of calling this method.
*
* @param island The island to check.
* @param worldName The name of the world.
* @return The world info for the given name, or null if this name is not an islands world.
*/
@Nullable
WorldInfo getIslandsWorldInfo(Island island, String worldName);

/**
* Checks if the given world is an islands world.
* Can be the normal world, the nether world (if enabled in config) or the end world (if enabled in config)
Expand Down
@@ -0,0 +1,33 @@
package com.bgsoftware.superiorskyblock.api.hooks;

import com.bgsoftware.superiorskyblock.api.island.Island;
import com.bgsoftware.superiorskyblock.api.world.WorldInfo;
import org.bukkit.World;

import javax.annotation.Nullable;

public interface LazyWorldsProvider extends WorldsProvider {

/**
* Get the {@link WorldInfo} of the world of an island by the environment.
* The world does not have to be loaded.
*
* @param island The island to check.
* @param environment The world environment.
* @return The world info for the given environment, or null if this environment is not enabled.
*/
@Nullable
WorldInfo getIslandsWorldInfo(Island island, World.Environment environment);

/**
* Get the {@link WorldInfo} of the world of an island by its name.
* The world does not have to be loaded.
*
* @param island The island to check.
* @param worldName The name of the world.
* @return The world info for the given name, or null if this name is not an islands world.
*/
@Nullable
WorldInfo getIslandsWorldInfo(Island island, String worldName);

}
Expand Up @@ -16,6 +16,7 @@ public interface WorldsProvider {

/**
* Get the world of an island by the environment.
* If the world is not loaded, this method should load the world before returning.
*
* @param environment The world environment.
* @param island The island to check.
Expand Down
Expand Up @@ -783,6 +783,11 @@ public boolean isChunkDirty(World world, int chunkX, int chunkZ) {
return this.handle.isChunkDirty(world, chunkX, chunkZ);
}

@Override
public boolean isChunkDirty(String worldName, int chunkX, int chunkZ) {
return this.handle.isChunkDirty(worldName, chunkX, chunkZ);
}

@Override
public void markChunkDirty(World world, int chunkX, int chunkZ, boolean save) {
this.handle.markChunkDirty(world, chunkX, chunkZ, save);
Expand Down
Expand Up @@ -1061,6 +1061,15 @@ public interface Island extends Comparable<Island>, IMissionsHolder, IPersistent
*/
boolean isChunkDirty(World world, int chunkX, int chunkZ);

/**
* Check whether a chunk has blocks inside it.
*
* @param worldName The name of the world of the chunk.
* @param chunkX The x-coords of the chunk.
* @param chunkZ The z-coords of the chunk.
*/
boolean isChunkDirty(String worldName, int chunkX, int chunkZ);

/**
* Mark a chunk as it has blocks inside it.
*
Expand Down
@@ -0,0 +1,42 @@
package com.bgsoftware.superiorskyblock.api.world;

import com.bgsoftware.superiorskyblock.api.SuperiorSkyblockAPI;
import com.google.common.base.Preconditions;
import org.bukkit.World;

public interface WorldInfo {

/**
* Get the name of this world.
*/
String getName();

/**
* Get the environment of this world.
*/
World.Environment getEnvironment();

/**
* Create a new world info.
*
* @param world The world.
*/
static WorldInfo of(World world) {
Preconditions.checkNotNull(world, "world parameter cannot be null");
Preconditions.checkArgument(SuperiorSkyblockAPI.getGrid().isIslandsWorld(world), "World must be an islands world.");
return of(world.getName(), world.getEnvironment());
}

/**
* Create a new world info.
*
* @param worldName The name of the world.
* @param environment The environment of the world.
*/
static WorldInfo of(String worldName, World.Environment environment) {
Preconditions.checkNotNull(worldName, "worldName parameter cannot be null");
Preconditions.checkNotNull(environment, "environment parameter cannot be null");
return SuperiorSkyblockAPI.getFactory().createWorldInfo(worldName, environment);
}

}
Expand Up @@ -2,6 +2,7 @@

import com.bgsoftware.superiorskyblock.SuperiorSkyblockPlugin;
import com.bgsoftware.superiorskyblock.api.island.Island;
import com.bgsoftware.superiorskyblock.api.world.WorldInfo;
import com.bgsoftware.superiorskyblock.api.wrappers.SuperiorPlayer;
import com.bgsoftware.superiorskyblock.commands.arguments.CommandArguments;
import com.bgsoftware.superiorskyblock.core.logging.Log;
Expand Down Expand Up @@ -105,7 +106,7 @@ public void execute(SuperiorSkyblockPlugin plugin, CommandSender sender, Superio
}

// Resetting the chunks
List<ChunkPosition> chunkPositions = IslandUtils.getChunkCoords(island, world, true, true);
List<ChunkPosition> chunkPositions = IslandUtils.getChunkCoords(island, WorldInfo.of(world), true, true);
IslandUtils.deleteChunks(island, chunkPositions, () -> island.calcIslandWorth(null));

island.setSchematicGenerate(environment, false);
Expand Down
@@ -1,5 +1,6 @@
package com.bgsoftware.superiorskyblock.core;

import com.bgsoftware.superiorskyblock.api.world.WorldInfo;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.Location;
Expand All @@ -10,52 +11,48 @@

public class ChunkPosition {

private final String worldName;
private final WorldInfo worldInfo;
private final int x;
private final int z;

private long pairedXZ = -1;

private ChunkPosition(String worldName, int x, int z) {
this.worldName = worldName;
private ChunkPosition(WorldInfo worldInfo, int x, int z) {
this.worldInfo = worldInfo;
this.x = x;
this.z = z;
}

public static ChunkPosition of(Block block) {
return of(block.getLocation());
return of(WorldInfo.of(block.getWorld()), block.getX() >> 4, block.getZ() >> 4);
}

public static ChunkPosition of(Location location) {
return of(LazyWorldLocation.getWorldName(location), location.getBlockX() >> 4, location.getBlockZ() >> 4);
return of(WorldInfo.of(location.getWorld()), location.getBlockX() >> 4, location.getBlockZ() >> 4);
}

public static ChunkPosition of(Chunk chunk) {
return of(chunk.getWorld().getName(), chunk.getX(), chunk.getZ());
}

public static ChunkPosition of(SBlockPosition blockPosition) {
return of(blockPosition.getWorldName(), blockPosition.getX() >> 4, blockPosition.getZ() >> 4);
return of(WorldInfo.of(chunk.getWorld()), chunk.getX(), chunk.getZ());
}

public static ChunkPosition of(World world, int x, int z) {
return of(world.getName(), x, z);
return of(WorldInfo.of(world), x, z);
}

public static ChunkPosition of(String worldName, int x, int z) {
return new ChunkPosition(worldName, x, z);
public static ChunkPosition of(WorldInfo worldInfo, int x, int z) {
return new ChunkPosition(worldInfo, x, z);
}

public Chunk loadChunk() {
return getWorld().getChunkAt(x, z);
public World getWorld() {
return Bukkit.getWorld(getWorldName());
}

public World getWorld() {
return Bukkit.getWorld(worldName);
public WorldInfo getWorldsInfo() {
return this.worldInfo;
}

public String getWorldName() {
return worldName;
return this.worldInfo.getName();
}

public int getX() {
Expand All @@ -74,12 +71,13 @@ public long asPair() {
}

public boolean isInsideChunk(Location location) {
return location.getWorld().getName().equals(worldName) && location.getBlockX() >> 4 == x && location.getBlockZ() >> 4 == z;
return location.getWorld().getName().equals(worldInfo.getName()) &&
location.getBlockX() >> 4 == x && location.getBlockZ() >> 4 == z;
}

@Override
public int hashCode() {
return Objects.hash(worldName, x, z);
return Objects.hash(worldInfo.getName(), x, z);
}

@Override
Expand All @@ -89,12 +87,12 @@ public boolean equals(Object o) {
ChunkPosition that = (ChunkPosition) o;
return x == that.x &&
z == that.z &&
worldName.equals(that.worldName);
worldInfo.equals(that.worldInfo);
}

@Override
public String toString() {
return worldName + ", " + x + ", " + z;
return worldInfo.getName() + ", " + x + ", " + z;
}

}
42 changes: 42 additions & 0 deletions src/main/java/com/bgsoftware/superiorskyblock/core/DirtyChunk.java
@@ -0,0 +1,42 @@
package com.bgsoftware.superiorskyblock.core;

import java.util.Objects;

public class DirtyChunk {

private final String worldName;
private final int x;
private final int z;

public DirtyChunk(String worldName, int x, int z) {
this.worldName = worldName;
this.x = x;
this.z = z;
}

public String getWorldName() {
return worldName;
}

public int getX() {
return x;
}

public int getZ() {
return z;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
DirtyChunk that = (DirtyChunk) o;
return x == that.x && z == that.z && worldName.equals(that.worldName);
}

@Override
public int hashCode() {
return Objects.hash(worldName, x, z);
}

}
@@ -0,0 +1,41 @@
package com.bgsoftware.superiorskyblock.core;

import com.bgsoftware.superiorskyblock.api.world.WorldInfo;
import org.bukkit.World;

import java.util.Objects;

public class WorldInfoImpl implements WorldInfo {

private final String worldName;
private final World.Environment environment;

public WorldInfoImpl(String worldName, World.Environment environment) {
this.worldName = worldName;
this.environment = environment;
}

@Override
public String getName() {
return this.worldName;
}

@Override
public World.Environment getEnvironment() {
return this.environment;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
WorldInfoImpl worldInfo = (WorldInfoImpl) o;
return worldName.equals(worldInfo.worldName) && environment == worldInfo.environment;
}

@Override
public int hashCode() {
return Objects.hash(worldName, environment);
}

}

0 comments on commit ebcc661

Please sign in to comment.