diff --git a/README.md b/README.md index 8b61e67c..aff700a6 100644 --- a/README.md +++ b/README.md @@ -48,7 +48,7 @@ Here are some plugins that either have addons for or integrations with ClaimChun Building [![Build Status](https://travis-ci.com/cjburkey01/ClaimChunk.svg?branch=master)](https://travis-ci.com/cjburkey01/ClaimChunk) -------- -[![Version Info](https://img.shields.io/static/v1?label=Repository%20Version&message=0.0.23-prev4&color=ff5555&style=for-the-badge)](https://github.com/cjburkey01/ClaimChunk/archive/master.zip) +[![Version Info](https://img.shields.io/static/v1?label=Repository%20Version&message=0.1.0-SNAPSHOT&color=ff5555&style=for-the-badge)](https://github.com/cjburkey01/ClaimChunk/archive/refs/heads/v0.1.0.zip) If you want to obtain a version of the plugin that isn't available yet (like a snapshot), you can do so by asking on the Discord or building it yourself. Here's how to build it yourself: diff --git a/build.gradle b/build.gradle index 4d0e9349..0e7d30e4 100644 --- a/build.gradle +++ b/build.gradle @@ -8,7 +8,7 @@ plugins { // Plugin information group 'com.cjburkey' -version '0.0.23-prev4' +version '0.1.0-SNAPSHOT' project.ext.liveVersion = '0.0.22' project.archivesBaseName = 'claimchunk' project.ext.pluginName = 'ClaimChunk' diff --git a/src/main/java/com/cjburkey/claimchunk/ClaimChunk.java b/src/main/java/com/cjburkey/claimchunk/ClaimChunk.java index 81a20124..c8ccc0ed 100644 --- a/src/main/java/com/cjburkey/claimchunk/ClaimChunk.java +++ b/src/main/java/com/cjburkey/claimchunk/ClaimChunk.java @@ -29,7 +29,7 @@ import java.io.InputStreamReader; import java.nio.file.Files; import java.nio.file.StandardCopyOption; -import java.util.Objects; +import java.util.Collection; // TODO: Split this plugin up into services that users can use // Services: @@ -454,12 +454,12 @@ private void handleAutoUnclaim() { if (playerJoinedSinceTimeRecordUpdate && playerBeenOfflineTooLong) { // Get a list of all the player's chunks - ChunkPos[] claimedChunks = chunkHandler.getClaimedChunks(player.player); + Collection claimedChunks = chunkHandler.getClaimedChunks(player.player); - if (claimedChunks.length > 0) { + if (!claimedChunks.isEmpty()) { // Unclaim all of the player's chunks for (ChunkPos chunk : claimedChunks) { - chunkHandler.unclaimChunk(getServer().getWorld(chunk.getWorld()), chunk.getX(), chunk.getZ()); + chunkHandler.setOwner(chunk, null); } Utils.log("Unclaimed all chunks of player \"%s\" (%s)", player.lastIgn, player.player); diff --git a/src/main/java/com/cjburkey/claimchunk/chunk/ChunkHandler.java b/src/main/java/com/cjburkey/claimchunk/chunk/ChunkHandler.java index 1a672fb8..47e82d26 100644 --- a/src/main/java/com/cjburkey/claimchunk/chunk/ChunkHandler.java +++ b/src/main/java/com/cjburkey/claimchunk/chunk/ChunkHandler.java @@ -1,15 +1,19 @@ package com.cjburkey.claimchunk.chunk; import com.cjburkey.claimchunk.ClaimChunk; +import com.cjburkey.claimchunk.data.hyperdrive.chunk.ICCChunkHandler; import com.cjburkey.claimchunk.data.newdata.IClaimChunkDataHandler; -import java.util.HashSet; -import java.util.Set; -import java.util.UUID; +import java.util.*; + +import org.bukkit.Bukkit; import org.bukkit.Chunk; import org.bukkit.World; import org.bukkit.entity.Player; +import org.jetbrains.annotations.Nullable; + +import javax.annotation.Nonnull; -public final class ChunkHandler { +public final class ChunkHandler implements ICCChunkHandler { private final IClaimChunkDataHandler dataHandler; private final ClaimChunk claimChunk; @@ -29,8 +33,10 @@ public ChunkHandler(IClaimChunkDataHandler dataHandler, ClaimChunk claimChunk) { * @param x The chunk x-coord. * @param z The chunk z-coord. * @param player The player for whom to claim the chunk. - * @return The chunk position variable or {@code null} if the chuk is already claimed + * @return The chunk position variable or {@code null} if the chunk is already claimed + * @deprecated Use methods defined in {@link com.cjburkey.claimchunk.data.hyperdrive.chunk.ICCChunkHandler} */ + @Deprecated public ChunkPos claimChunk(String world, int x, int z, UUID player) { if (isClaimed(world, x, z)) { // If the chunk is already claimed, return null @@ -57,8 +63,10 @@ public ChunkPos claimChunk(String world, int x, int z, UUID player) { * @param x The chunk x-coord. * @param z The chunk z-coord. * @param player The player for whom to claim the chunk. - * @return The chunk position variable or {@code null} if the chuk is already claimed + * @return The chunk position variable or {@code null} if the chunk is already claimed + * @deprecated Use methods defined in {@link com.cjburkey.claimchunk.data.hyperdrive.chunk.ICCChunkHandler} */ + @Deprecated public ChunkPos claimChunk(World world, int x, int z, UUID player) { return claimChunk(world.getName(), x, z, player); } @@ -72,7 +80,9 @@ public ChunkPos claimChunk(World world, int x, int z, UUID player) { * @param world The current world. * @param x The chunk x-coord. * @param z The chunk z-coord. + * @deprecated Use methods defined in {@link com.cjburkey.claimchunk.data.hyperdrive.chunk.ICCChunkHandler} */ + @Deprecated public void unclaimChunk(World world, int x, int z) { if (isClaimed(world, x, z)) { // If the chunk is claimed, remove it from the claimed chunks list @@ -89,7 +99,10 @@ public void unclaimChunk(World world, int x, int z) { * @param world The current world name. * @param x The chunk x-coord. * @param z The chunk z-coord. + * @deprecated Use methods defined in {@link com.cjburkey.claimchunk.data.hyperdrive.chunk.ICCChunkHandler} */ + @SuppressWarnings("DeprecatedIsStillUsed") + @Deprecated public void unclaimChunk(String world, int x, int z) { if (isClaimed(world, x, z)) { // If the chunk is claimed, remove it from the claimed chunks list @@ -102,7 +115,10 @@ public void unclaimChunk(String world, int x, int z) { * * @param ply The UUID of the player. * @return The integer count of chunks this player has claimed in total across worlds. + * @deprecated Use methods defined in {@link com.cjburkey.claimchunk.data.hyperdrive.chunk.ICCChunkHandler} */ + @SuppressWarnings("DeprecatedIsStillUsed") + @Deprecated public int getClaimed(UUID ply) { int count = 0; @@ -115,24 +131,46 @@ public int getClaimed(UUID ply) { return count; } - /** - * Creates an array with all claimed chunks that the provided player has. - * - * @param ply The UUID of the player. - * @return An array containing chunk positions for all this player's claimed chunks. - */ - public ChunkPos[] getClaimedChunks(UUID ply) { + // Fucky business until I implement the new data handlers + + @Override + public @Nonnull Optional setOwner(@Nonnull ChunkPos chunkPos, @Nullable UUID newOwner) { + Optional oldOwner = getOwner(chunkPos); + if (oldOwner.isPresent()) unclaimChunk(chunkPos.getWorld(), chunkPos.getX(), chunkPos.getZ()); + if (newOwner != null) claimChunk(chunkPos.getWorld(), chunkPos.getX(), chunkPos.getZ(), newOwner); + return oldOwner; + } + + @Override + public boolean getHasOwner(@Nonnull ChunkPos chunkPos) { + return getOwner(chunkPos).isPresent(); + } + + @Override + public @Nonnull Optional getOwner(@Nonnull ChunkPos chunkPos) { + return Optional.ofNullable(getOwner(Objects.requireNonNull(Bukkit.getWorld(chunkPos.getWorld())), + chunkPos.getX(), + chunkPos.getZ())); + } + + @Override + public int getClaimedChunksCount(@Nonnull UUID owner) { + return getClaimed(owner); + } + + @Override + public @Nonnull Collection getClaimedChunks(@Nonnull UUID ply) { // Create a set for the chunks Set chunks = new HashSet<>(); - // Loop throug all chunks + // Loop through all chunks for (DataChunk chunk : dataHandler.getClaimedChunks()) { // Add chunks that are owned by this player if (chunk.player.equals(ply)) chunks.add(chunk.chunk); } // Convert the set into an array - return chunks.toArray(new ChunkPos[0]); + return chunks; } /** @@ -184,7 +222,9 @@ public boolean getHasAllFreeChunks(UUID ply, int count) { * @param x The x-coordinate (in chunk coordinates) of the chunk. * @param z The z-coordinate (in chunk coordinates) of the chunk. * @return Whether this chunk is currently claimed. + * @deprecated Use methods defined in {@link com.cjburkey.claimchunk.data.hyperdrive.chunk.ICCChunkHandler} */ + @Deprecated public boolean isClaimed(World world, int x, int z) { return dataHandler.isChunkClaimed(new ChunkPos(world.getName(), x, z)); } @@ -196,7 +236,9 @@ public boolean isClaimed(World world, int x, int z) { * @param x The x-coordinate (in chunk coordinates) of the chunk. * @param z The z-coordinate (in chunk coordinates) of the chunk. * @return Whether this chunk is currently claimed. + * @deprecated Use methods defined in {@link com.cjburkey.claimchunk.data.hyperdrive.chunk.ICCChunkHandler} */ + @Deprecated public boolean isClaimed(String world, int x, int z) { return dataHandler.isChunkClaimed(new ChunkPos(world, x, z)); } @@ -206,7 +248,9 @@ public boolean isClaimed(String world, int x, int z) { * * @param chunk The Spigot chunk position. * @return Whether this chunk is currently claimed. + * @deprecated Use methods defined in {@link com.cjburkey.claimchunk.data.hyperdrive.chunk.ICCChunkHandler} */ + @Deprecated public boolean isClaimed(Chunk chunk) { return isClaimed(chunk.getWorld(), chunk.getX(), chunk.getZ()); } @@ -219,7 +263,9 @@ public boolean isClaimed(Chunk chunk) { * @param z The z-coordinate (in chunk coordinates) of the chunk. * @param ply The UUID of the player. * @return Whether this player owns this chunk. + * @deprecated Use methods defined in {@link com.cjburkey.claimchunk.data.hyperdrive.chunk.ICCChunkHandler} */ + @Deprecated public boolean isOwner(World world, int x, int z, UUID ply) { ChunkPos pos = new ChunkPos(world.getName(), x, z); UUID owner = dataHandler.getChunkOwner(pos); @@ -234,7 +280,9 @@ public boolean isOwner(World world, int x, int z, UUID ply) { * @param z The z-coordinate (in chunk coordinates) of the chunk. * @param ply The player. * @return Whether this player owns this chunk. + * @deprecated Use methods defined in {@link com.cjburkey.claimchunk.data.hyperdrive.chunk.ICCChunkHandler} */ + @Deprecated public boolean isOwner(World world, int x, int z, Player ply) { return isOwner(world, x, z, ply.getUniqueId()); } @@ -245,7 +293,9 @@ public boolean isOwner(World world, int x, int z, Player ply) { * @param chunk The Spigot chunk position. * @param ply The UUID of the player. * @return Whether this player owns this chunk. + * @deprecated Use methods defined in {@link com.cjburkey.claimchunk.data.hyperdrive.chunk.ICCChunkHandler} */ + @Deprecated public boolean isOwner(Chunk chunk, UUID ply) { return isOwner(chunk.getWorld(), chunk.getX(), chunk.getZ(), ply); } @@ -256,7 +306,9 @@ public boolean isOwner(Chunk chunk, UUID ply) { * @param chunk The Spigot chunk position. * @param ply The player. * @return Whether this player owns this chunk. + * @deprecated Use methods defined in {@link com.cjburkey.claimchunk.data.hyperdrive.chunk.ICCChunkHandler} */ + @Deprecated public boolean isOwner(Chunk chunk, Player ply) { return isOwner(chunk.getWorld(), chunk.getX(), chunk.getZ(), ply); } @@ -268,7 +320,10 @@ public boolean isOwner(Chunk chunk, Player ply) { * @param x The x-coordinate (in chunk coordinates) of the chunk. * @param z The z-coordinate (in chunk coordinates) of the chunk. * @return The UUID of the owner of this chunk. + * @deprecated Use methods defined in {@link com.cjburkey.claimchunk.data.hyperdrive.chunk.ICCChunkHandler} */ + @SuppressWarnings("DeprecatedIsStillUsed") + @Deprecated public UUID getOwner(World world, int x, int z) { ChunkPos pos = new ChunkPos(world.getName(), x, z); return !dataHandler.isChunkClaimed(pos) ? null : dataHandler.getChunkOwner(pos); @@ -280,18 +335,25 @@ public UUID getOwner(World world, int x, int z) { * * @param chunk The Spigot chunk position. * @return The UUID of the owner of this chunk. + * @deprecated Use methods defined in {@link com.cjburkey.claimchunk.data.hyperdrive.chunk.ICCChunkHandler} */ + @Deprecated public UUID getOwner(Chunk chunk) { ChunkPos pos = new ChunkPos(chunk); return !dataHandler.isChunkClaimed(pos) ? null : dataHandler.getChunkOwner(pos); } + // TODO: MOVE TNT TO PER-PLAYER/PER-CHUNK SETTING HANDLER WHEN THAT COMES + // TO FRUITION. + /** * Toggles whether TNT is enabled in the provided chunk. * * @param chunk The Spigot chunk position. * @return Whether TNT is now (after the toggle) enabled in this chunk. + * @deprecated TODO */ + @Deprecated public boolean toggleTnt(Chunk chunk) { return dataHandler.toggleTnt(new ChunkPos(chunk)); } @@ -301,7 +363,9 @@ public boolean toggleTnt(Chunk chunk) { * * @param chunk The Spigot chunk position. * @return Whether TNT is currently enabled in this chunk. + * @deprecated TODO */ + @Deprecated public boolean isTntEnabled(Chunk chunk) { return dataHandler.isTntEnabled(new ChunkPos(chunk)); } diff --git a/src/main/java/com/cjburkey/claimchunk/chunk/ChunkPos.java b/src/main/java/com/cjburkey/claimchunk/chunk/ChunkPos.java index 79ab9d3f..c879d610 100644 --- a/src/main/java/com/cjburkey/claimchunk/chunk/ChunkPos.java +++ b/src/main/java/com/cjburkey/claimchunk/chunk/ChunkPos.java @@ -3,10 +3,26 @@ import java.util.Objects; import org.bukkit.Chunk; +import javax.annotation.Nonnull; + +/** + * A class representing the position of some chunk in a given world. + */ public final class ChunkPos { + /** + * The name of the world that contains this chunk + */ private final String world; + + /** + * The x-coordinate (in chunk coordinates) of this chunk. + */ private final int x; + + /** + * The z-coordinate (in chunk coordinates) of this chunk. + */ private final int z; /** @@ -16,7 +32,7 @@ public final class ChunkPos { * @param x The x-coordinate of this chunk (in chunk coordinates). * @param z The y-coordinate of this chunk (in chunk coordinates). */ - public ChunkPos(String world, int x, int z) { + public ChunkPos(@Nonnull String world, int x, int z) { this.world = world; this.x = x; this.z = z; @@ -28,12 +44,13 @@ public ChunkPos(String world, int x, int z) { * * @param chunk The Spigot chunk representation. */ - public ChunkPos(Chunk chunk) { - this(chunk.getWorld().getName(), chunk.getX(), chunk.getZ()); + public ChunkPos(@Nonnull Chunk chunk) { + // Make sure + this(Objects.requireNonNull(chunk.getWorld()).getName(), chunk.getX(), chunk.getZ()); } /** - * Get the name of the world that this chunk is in. + * Get the name of the world that contains this chunk. * * @return The world name of this chunk. */ @@ -42,7 +59,7 @@ public String getWorld() { } /** - * Get the x-coordinate of this chunk. + * Get the x-coordinate of this chunk in chunk coordinates. * * @return The x-coordinate of this chunk (in chunk coordinates). */ @@ -51,7 +68,7 @@ public int getX() { } /** - * Get the y-coordinate of this chunk. + * Get the y-coordinate of this chunk in chunk coordinates. * * @return The y-coordinate of this chunk (in chunk coordinates). */ @@ -61,7 +78,7 @@ public int getZ() { @Override public String toString() { - return String.format("%s, %s in %s", x, z, world); + return String.format("Chunk { %s,%s in %s }", x, z, world); } @Override diff --git a/src/main/java/com/cjburkey/claimchunk/cmd/MainHandler.java b/src/main/java/com/cjburkey/claimchunk/cmd/MainHandler.java index 2669211f..fe997390 100644 --- a/src/main/java/com/cjburkey/claimchunk/cmd/MainHandler.java +++ b/src/main/java/com/cjburkey/claimchunk/cmd/MainHandler.java @@ -11,7 +11,6 @@ import com.cjburkey.claimchunk.service.prereq.claim.*; import org.bukkit.*; import org.bukkit.entity.Player; - import java.util.ArrayList; import java.util.List; import java.util.UUID; @@ -142,19 +141,10 @@ public void claimChunk(Player p, Chunk loc) { PREREQ.check(new PrereqClaimData(CLAIM_CHUNK, loc, p.getUniqueId(), p), CLAIM_CHUNK.getMessages().claimSuccess.replace("%%PRICE%%", CLAIM_CHUNK.getMessages().claimNoCost), errorMsg -> errorMsg.ifPresent(msg -> Utils.toPlayer(p, msg)), successMsg -> { + ChunkPos chunkPos = new ChunkPos(loc); + // Claim the chunk if nothing is wrong - ChunkPos pos = CHUNK_HANDLE.claimChunk(loc.getWorld(), loc.getX(), loc.getZ(), p.getUniqueId()); - - // Error check, though it *shouldn't* occur - if (pos == null) { - Utils.err( - "Failed to claim chunk (%s, %s) in world %s for player %s. The data handler returned " - + "a null position?", - loc.getX(), loc.getZ(), loc.getWorld() - .getName(), p.getName() - ); - return; - } + CHUNK_HANDLE.setOwner(chunkPos, p.getUniqueId()); // Send the success message to the player if it's present (it should be) successMsg.ifPresent(msg -> Utils.toPlayer(p, msg)); @@ -199,7 +189,7 @@ public boolean unclaimChunk(boolean adminOverride, boolean hideTitle, Player p, Utils.err("Failed to locate world %s", world); return false; } - if (!ch.isClaimed(w, x, z)) { + if (!ch.getHasOwner(new ChunkPos(w.getName(), x, z))) { if (!hideTitle) { Utils.toPlayer(p, claimChunk.getMessages().unclaimNotOwned); } @@ -207,7 +197,7 @@ public boolean unclaimChunk(boolean adminOverride, boolean hideTitle, Player p, } // Check if the unclaiming player is the owner or admin override is enable - if (!adminOverride && !ch.isOwner(w, x, z, p)) { + if (!adminOverride && !ch.isOwner(new ChunkPos(w.getName(), x, z), p.getUniqueId())) { if (!hideTitle) { Utils.toPlayer(p, claimChunk.getMessages().unclaimNotOwner); } @@ -219,7 +209,7 @@ public boolean unclaimChunk(boolean adminOverride, boolean hideTitle, Player p, if (!adminOverride && claimChunk.useEconomy() - && ch.getClaimed(p.getUniqueId()) > claimChunk.chConfig().getFirstFreeChunks()) { + && ch.getClaimedChunksCount(p.getUniqueId()) > claimChunk.chConfig().getFirstFreeChunks()) { Econ e = claimChunk.getEconomy(); double reward = claimChunk.chConfig().getUnclaimReward(); if (reward > 0) { @@ -232,7 +222,7 @@ public boolean unclaimChunk(boolean adminOverride, boolean hideTitle, Player p, } // Unclaim the chunk - ch.unclaimChunk(w, x, z); + ch.setOwner(new ChunkPos(w.getName(), x, z), null); if (!refund && !hideTitle) { Utils.toPlayer(p, claimChunk.getMessages().unclaimSuccess); } @@ -331,7 +321,7 @@ public void giveChunk(Player giver, Chunk chunk, String newOwner) { final RankHandler rankHandler = claimChunk.getRankHandler(); // Check if this player owns this chunk - if (!chunkHandler.isOwner(chunk, giver)) { + if (!chunkHandler.isOwner(new ChunkPos(chunk), giver.getUniqueId())) { Utils.toPlayer(giver, claimChunk.getMessages().giveNotYourChunk); return; } @@ -353,25 +343,16 @@ public void giveChunk(Player giver, Chunk chunk, String newOwner) { } // Make sure the receiving player doesn't have too many chunks already - if (chunkHandler.getClaimed(given) >= rankHandler.getMaxClaimsForPlayer(givenPly)) { + if (chunkHandler.getClaimedChunksCount(given) >= rankHandler.getMaxClaimsForPlayer(givenPly)) { Utils.toPlayer(giver, claimChunk.getMessages().giveChunksFull.replace("%%PLAYER%%", newOwner)); return; } - // Unclaim the chunk from the old owner - chunkHandler.unclaimChunk(chunk.getWorld(), chunk.getX(), chunk.getZ()); - - // Claim the chunk for the new owner - ChunkPos newChunk = chunkHandler.claimChunk(chunk.getWorld(), chunk.getX(), chunk.getZ(), given); + ChunkPos chunkPos = new ChunkPos(chunk); - // Error check (it should never happen) - if (newChunk == null) { - Utils.toPlayer(giver, claimChunk.getMessages().giveError); - Utils.err("Failed to give %s the chunk (%s, %s) in world %s from player %s", newOwner, chunk.getX(), - chunk.getZ(), chunk.getWorld().getName(), giver.getDisplayName() - ); - return; - } + // Unclaim the chunk from the old owner and claim the chunk for the + // new owner + chunkHandler.setOwner(chunkPos, given); // Tell the player they have given their chunk Utils.toPlayer(giver, claimChunk.getMessages().gaveChunk.replace("%%PLAYER%%", newOwner)); diff --git a/src/main/java/com/cjburkey/claimchunk/cmds/CmdAdminUnclaimAll.java b/src/main/java/com/cjburkey/claimchunk/cmds/CmdAdminUnclaimAll.java index d9bb76b0..cf2ba65c 100644 --- a/src/main/java/com/cjburkey/claimchunk/cmds/CmdAdminUnclaimAll.java +++ b/src/main/java/com/cjburkey/claimchunk/cmds/CmdAdminUnclaimAll.java @@ -6,9 +6,9 @@ import com.cjburkey.claimchunk.chunk.ChunkPos; import com.cjburkey.claimchunk.cmd.Argument; import com.cjburkey.claimchunk.cmd.ICommand; -import java.util.UUID; import org.bukkit.command.CommandSender; import org.bukkit.entity.Player; +import java.util.UUID; public class CmdAdminUnclaimAll implements ICommand { @@ -52,11 +52,10 @@ public boolean onCall(ClaimChunk claimChunk, String cmdUsed, Player executor, St UUID ply = claimChunk.getPlayerHandler().getUUID(args[0]); if (ply != null) { - ChunkPos[] claimedChunks = chunkHandler.getClaimedChunks(ply); int unclaimed = 0; - for (ChunkPos chunk : claimedChunks) { + for (ChunkPos chunk : chunkHandler.getClaimedChunks(ply)) { if (allWorlds || executor.getWorld().getName().equals(chunk.getWorld())) { - chunkHandler.unclaimChunk(chunk.getWorld(), chunk.getX(), chunk.getZ()); + chunkHandler.setOwner(chunk, null); unclaimed++; } } diff --git a/src/main/java/com/cjburkey/claimchunk/cmds/CmdInfo.java b/src/main/java/com/cjburkey/claimchunk/cmds/CmdInfo.java index f40b81c0..b68cb359 100644 --- a/src/main/java/com/cjburkey/claimchunk/cmds/CmdInfo.java +++ b/src/main/java/com/cjburkey/claimchunk/cmds/CmdInfo.java @@ -2,9 +2,12 @@ import com.cjburkey.claimchunk.ClaimChunk; import com.cjburkey.claimchunk.Utils; +import com.cjburkey.claimchunk.chunk.ChunkPos; import com.cjburkey.claimchunk.cmd.Argument; import com.cjburkey.claimchunk.cmd.ICommand; import com.cjburkey.claimchunk.player.PlayerHandler; + +import java.util.Optional; import java.util.UUID; import org.bukkit.Chunk; import org.bukkit.command.CommandSender; @@ -46,16 +49,14 @@ public int getRequiredArguments(ClaimChunk claimChunk) { public boolean onCall(ClaimChunk claimChunk, String cmdUsed, Player executor, String[] args) { PlayerHandler playerHandler = claimChunk.getPlayerHandler(); Chunk chunk = executor.getLocation().getChunk(); - UUID owner = claimChunk.getChunkHandler().getOwner(chunk); + Optional owner = claimChunk.getChunkHandler().getOwner(new ChunkPos(chunk)); - String ownerName = ((owner == null) - ? null - : playerHandler.getUsername(owner)); + String ownerName = owner.map(playerHandler::getUsername).orElse(null); if (ownerName == null) ownerName = claimChunk.getMessages().infoOwnerUnknown; - String ownerDisplay = ((owner == null || !playerHandler.hasChunkName(owner)) + String ownerDisplay = ((!owner.isPresent() || !playerHandler.hasChunkName(owner.get())) ? null - : playerHandler.getChunkName(owner)); + : playerHandler.getChunkName(owner.get())); if (ownerDisplay == null) ownerDisplay = claimChunk.getMessages().infoNameNone; Utils.msg(executor, String.format("%s&l--- [ %s ] ---", diff --git a/src/main/java/com/cjburkey/claimchunk/cmds/CmdList.java b/src/main/java/com/cjburkey/claimchunk/cmds/CmdList.java index 392b4a0a..d449ad45 100644 --- a/src/main/java/com/cjburkey/claimchunk/cmds/CmdList.java +++ b/src/main/java/com/cjburkey/claimchunk/cmds/CmdList.java @@ -52,7 +52,7 @@ public boolean onCall(ClaimChunk claimChunk, String cmdUsed, Player executor, St String ownerName = playerHandler.getUsername(executor.getUniqueId()); if (ownerName == null) ownerName = claimChunk.getMessages().infoOwnerUnknown; - ChunkPos[] chunks = chunkHandler.getClaimedChunks(ply); + ChunkPos[] chunks = chunkHandler.getClaimedChunks(ply).toArray(new ChunkPos[0]); int page = 0; final int maxPerPage = Utils.clamp(claimChunk.chConfig().getMaxPerListPage(), 2, 10); final int maxPage = Integer.max(0, (chunks.length - 1) / maxPerPage); diff --git a/src/main/java/com/cjburkey/claimchunk/cmds/CmdUnclaimAll.java b/src/main/java/com/cjburkey/claimchunk/cmds/CmdUnclaimAll.java index 6b409857..e573d6ff 100644 --- a/src/main/java/com/cjburkey/claimchunk/cmds/CmdUnclaimAll.java +++ b/src/main/java/com/cjburkey/claimchunk/cmds/CmdUnclaimAll.java @@ -46,7 +46,7 @@ public boolean onCall(ClaimChunk claimChunk, String cmdUsed, Player executor, St boolean allWorlds = (args.length == 1 && Boolean.parseBoolean(args[0])); ChunkHandler chunkHandler = claimChunk.getChunkHandler(); - ChunkPos[] claimedChunks = chunkHandler.getClaimedChunks(executor.getUniqueId()); + ChunkPos[] claimedChunks = chunkHandler.getClaimedChunks(executor.getUniqueId()).toArray(new ChunkPos[0]); int unclaimed = 0; for (ChunkPos chunk : claimedChunks) { if ((allWorlds diff --git a/src/main/java/com/cjburkey/claimchunk/data/hyperdrive/chunk/ICCChunkHandler.java b/src/main/java/com/cjburkey/claimchunk/data/hyperdrive/chunk/ICCChunkHandler.java new file mode 100644 index 00000000..d877435b --- /dev/null +++ b/src/main/java/com/cjburkey/claimchunk/data/hyperdrive/chunk/ICCChunkHandler.java @@ -0,0 +1,80 @@ +package com.cjburkey.claimchunk.data.hyperdrive.chunk; + +import com.cjburkey.claimchunk.chunk.ChunkPos; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.util.Collection; +import java.util.Objects; +import java.util.Optional; +import java.util.UUID; + +/** + * An interface representing a ClaimChunk chunk handler. + * + * @since 0.1.0 + */ +@SuppressWarnings({"UnusedReturnValue", "BooleanMethodIsAlwaysInverted"}) +public interface ICCChunkHandler { + + /** + * Sets or updates the given chunk's owner to the new provided owner. + * + * @param chunkPos The chunk to update (will not be null). + * @param newOwner The UUID of the new owner of the chunk; if this is {@code null}, the chunk should be marked as unclaimed. + * @return An optional with the given chunk's previous owner's UUID, or empty if the chunk was previously unclaimed. + * @since 0.1.0 + */ + @Nonnull Optional setOwner(@Nonnull ChunkPos chunkPos, @Nullable UUID newOwner); + + /** + * Checks if the given chunk has an owner. + * + * @param chunkPos The chunk to query (will not be null). + * @return Whether the given chunk currently has an owner (whether this chunk is claimed). + * @since 0.1.0 + */ + boolean getHasOwner(@Nonnull ChunkPos chunkPos); + + /** + * Retrieves the current owner of the given chunk. + * + * @param chunkPos The chunk to query (will not be null). + * @return An optional with the given chunk's owner's UUID, or empty if the chunk is unclaimed. + * @since 0.1.0 + */ + @Nonnull Optional getOwner(@Nonnull ChunkPos chunkPos); + + /** + * Checks whether the given player is the owner of the given chunk. + * + * @param chunkPos The chunk to check (will not be null). + * @param player The player (will not be null). + * @return Whether the given player has claimed the given chunk. + * @since 0.1.0 + */ + default boolean isOwner(@Nonnull ChunkPos chunkPos, @Nonnull UUID player) { + return Objects.equals(getOwner(chunkPos).orElse(null), player); + } + + /** + * Retrieves the total number of claims across all worlds that the given + * player has. + * + * @param owner The chunk(s) owner (will not be null). + * @return An integer representing the number of claims the given player has. + * @since 0.1.0 + */ + int getClaimedChunksCount(@Nonnull UUID owner); + + /** + * Retrieves a collection of the positions of all of the chunks that the + * given player has claimed across all worlds. + * + * @param owner The chunk(s) owner (will not be null). + * @return A non-null collection of the claimed chunks' positions. + * @since 0.1.0 + */ + @Nonnull Collection getClaimedChunks(@Nonnull UUID owner); + +} diff --git a/src/main/java/com/cjburkey/claimchunk/data/hyperdrive/ply/ICCJoinedPlayer.java b/src/main/java/com/cjburkey/claimchunk/data/hyperdrive/ply/ICCJoinedPlayer.java new file mode 100644 index 00000000..cabf76b8 --- /dev/null +++ b/src/main/java/com/cjburkey/claimchunk/data/hyperdrive/ply/ICCJoinedPlayer.java @@ -0,0 +1,39 @@ +package com.cjburkey.claimchunk.data.hyperdrive.ply; + +import javax.annotation.Nonnull; +import java.util.UUID; + +/** + * A class representing a player that has been entered into a ClaimChunk player + * handler. + * + * @since 0.1.0 + */ +@SuppressWarnings("unused") +public interface ICCJoinedPlayer { + + /** + * Gets the unique ID for this player. + * + * @return The UUID of this player. + * @since 0.1.0 + */ + @Nonnull UUID getUUID(); + + /** + * Retrieves the last in-game name recorded for this player. + * + * @return The username for a given player. + * @since 0.1.0 + */ + @Nonnull String getLastIngameName(); + + /** + * Gets the last time this player was online. + * + * @return The Unix timestamp for when this player last joined, in milliseconds since the start of the epoch. + * @since 0.1.0 + */ + long getLastJoinTime(); + +} diff --git a/src/main/java/com/cjburkey/claimchunk/data/hyperdrive/ply/ICCPlayerHandler.java b/src/main/java/com/cjburkey/claimchunk/data/hyperdrive/ply/ICCPlayerHandler.java new file mode 100644 index 00000000..dbc91567 --- /dev/null +++ b/src/main/java/com/cjburkey/claimchunk/data/hyperdrive/ply/ICCPlayerHandler.java @@ -0,0 +1,172 @@ +package com.cjburkey.claimchunk.data.hyperdrive.ply; + +import com.cjburkey.claimchunk.data.hyperdrive.ply.ICCJoinedPlayer; +import org.bukkit.entity.Player; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.util.Collection; +import java.util.List; +import java.util.Optional; +import java.util.UUID; +import java.util.stream.Collectors; + +/** + * An interface representing all the methods a PlayerHandler should implement + * for ClaimChunk + * + * @since 0.1.0 + */ +@SuppressWarnings("unused") +public interface ICCPlayerHandler { + + /** + * Adds a given player to this player handler. This method will be called + * by ClaimChunk when a new player joins. + * + * @param player The player to add to this player handler (will not be null). + * @return {@code true} if the player was just added to this player handler, or {@code false} if the player has already joined. + * @since 0.1.0 + */ + boolean onPlayerJoin(@Nonnull Player player); + + /** + * Retrieves the complete collection of all players who are registered with + * this player handler. + * + * @return A non-null collection of all the players stored in this player handler + * @since 0.1.0 + */ + @Nonnull Collection getJoinedPlayers(); + + /** + * Performs a query for usernames of players in this data handler that + * start with a provided partial username. + * + * @param partialUsername The partial username to search (will not be null). + * @return A non-null list of usernames that begin with the provided partial name. + * @since 0.1.0 + */ + default @Nonnull List getJoinedPlayersByUsername(@Nonnull String partialUsername) { + return getJoinedPlayers().stream() + .map(ICCJoinedPlayer::getLastIngameName) + .filter(name -> name.startsWith(partialUsername)) + .collect(Collectors.toList()); + } + + /** + * Toggles access for the given accessor in the given owner's chunks. + * + * @param owner The person whose chunks for which access will be toggled (will not be null). + * @param accessor The person for whom access is toggled (will not be null). + * @return Whether the given accessor now has access. + * @since 0.1.0 + */ + boolean toggleAccess(@Nonnull UUID owner, @Nonnull UUID accessor); + + /** + * Checks if the given accessor has access to the given owner's chunks. + * + * @param owner The person whose chunks for which accessibility will be determined (will not be null). + * @param accessor The person for whom access is being queried (will not be null). + * @return Whether the given accessor has access to the given owner's chunks. + * @since 0.1.0 + */ + boolean hasAccess(@Nonnull UUID owner, @Nonnull UUID accessor); + + /** + * Retrieves a collection of the UUIDs of all players who have access to + * the given owner's chunks. + * + * @param owner The person for whom we wish to find accessors (will not be null). + * @return A collection of UUIDs for accessors of the given owner's chunks. + * @since 0.1.0 + */ + @Nonnull Collection getAccessPermitted(@Nonnull UUID owner); + + /** + * Toggles whether the given player will receive alerts when another player + * enters their territory. + * + * @param player The player whose alerts are to be toggled (will not be null). + * @return Whether the given player now has alerts enabled. + * @since 0.1.0 + */ + boolean toggleAlerts(@Nonnull UUID player); + + /** + * Checks whether the given player has alerts enabled. + * + * @param player The player whose alert status is to be queried (will not be null). + * @return Whether the given player has alerts enabled. + * @since 0.1.0 + */ + boolean hasAlerts(@Nonnull UUID player); + + /** + * Changes the given player's chunk display name, or resets it to their + * username if the given name is {@code null}. + * + * @param player The player whose chunk display name to modify (will not be null). + * @param name The new name for the given owner's chunks, or {@code null} to clear the current name. + * @since 0.1.0 + */ + void setChunkName(@Nonnull UUID player, @Nullable String name); + + /** + * Checks whether the given player has a custom chunk name set. + * + * @param player The player (will not be null). + * @return Whether this player has a custom chunk name. + * @since 0.1.0 + */ + boolean hasChunkName(@Nonnull UUID player); + + /** + * Tries to retrieve the chunk name for the given player. + * + * @param player The player (will not be null). + * @return An optional representing the chunk name if one is present, or empty if the player hasn't joined or hasn't set a custom chunk display name. + * @since 0.1.0 + */ + @Nonnull Optional getChunkName(@Nonnull UUID player); + + /** + * Retrieves the username for a given player + * + * @param player The player (will not be null). + * @return The given player's username, or {@code null} if the player has not joined. + * @since 0.1.0 + */ + @Nullable String getUsername(@Nonnull UUID player); + + /** + * Retrieves a player's unique ID based on their username. + * + * @param username The given player's username (will not be null). + * @return The player's UUID, or {@code null} if the given username has no UUID mapping. + * @since 0.1.0 + */ + @Nullable UUID getUUID(@Nonnull String username); + + /** + * Gets the display name to be shown. If the given player has a custom + * chunk name, that will be shown, otherwise, their username will be. + * + * @param player The player (will not be null). + * @return An optional representing the player's display name or username if present, otherwise empty if the given player has not joined. + * @since 0.1.0 + */ + default Optional getChunkDisplayName(@Nonnull UUID player) { + return Optional.ofNullable(getChunkName(player).orElseGet(() -> getUsername(player))); + } + + /** + * Updates the given player's last joined time to the given time. + * + * @param player The player (will not be null). + * @param time The Unix timestamp representing when the given player last joined, in milliseconds, since the start of the epoch. + * @since 0.1.0 + */ + void setLastJoinedTime(@Nonnull UUID player, long time); + +} diff --git a/src/main/java/com/cjburkey/claimchunk/event/PlayerMovementHandler.java b/src/main/java/com/cjburkey/claimchunk/event/PlayerMovementHandler.java index db949318..07bf33a7 100644 --- a/src/main/java/com/cjburkey/claimchunk/event/PlayerMovementHandler.java +++ b/src/main/java/com/cjburkey/claimchunk/event/PlayerMovementHandler.java @@ -4,7 +4,10 @@ import com.cjburkey.claimchunk.Utils; import com.cjburkey.claimchunk.chunk.AutoClaimHandler; import com.cjburkey.claimchunk.chunk.ChunkHandler; +import com.cjburkey.claimchunk.chunk.ChunkPos; import com.cjburkey.claimchunk.player.PlayerHandler; + +import java.util.Optional; import java.util.UUID; import org.bukkit.Bukkit; import org.bukkit.Chunk; @@ -41,19 +44,21 @@ public void onPlayerMove(PlayerMoveEvent e) { } ChunkHandler ch = claimChunk.getChunkHandler(); + ChunkPos prevChunkPos = new ChunkPos(prev); + ChunkPos toChunkPos = new ChunkPos(to); // Check if the previous chunk was already claimed - boolean lastClaimed = ch.isClaimed(prev.getWorld(), prev.getX(), prev.getZ()); + boolean lastClaimed = ch.getHasOwner(new ChunkPos(prev)); // Check if the new chunk is already claimed - if (ch.isClaimed(to.getWorld(), to.getX(), to.getZ())) { + if (ch.getHasOwner(new ChunkPos(to))) { // If the new chunk and the previous chunk were claimed, check if the owners differ if (lastClaimed) { - UUID prevOwner = ch.getOwner(prev.getWorld(), prev.getX(), prev.getZ()); - UUID newOwner = ch.getOwner(to.getWorld(), to.getX(), to.getZ()); + Optional prevOwner = ch.getOwner(prevChunkPos); + Optional newOwner = ch.getOwner(toChunkPos); // Only display the new chunk's owner if they differ from the previous chunk's owner - if ((prevOwner == null && newOwner == null) || (prevOwner != null && !prevOwner.equals(newOwner))) { + if (!prevOwner.equals(newOwner)) { showTitle(e.getPlayer(), to); } } else { @@ -63,10 +68,13 @@ public void onPlayerMove(PlayerMoveEvent e) { } else { // The player entered an unclaimed chunk from a claimed chunk if (lastClaimed) { - UUID lastOwner = ch.getOwner(prev.getWorld(), prev.getX(), prev.getZ()); - String name = claimChunk.getPlayerHandler().getChunkName(lastOwner); + Optional lastOwner = ch.getOwner(prevChunkPos); + // Error? + if (!lastOwner.isPresent()) return; + + String name = claimChunk.getPlayerHandler().getChunkName(lastOwner.get()); String msg; - if (e.getPlayer().getUniqueId().equals(lastOwner)) { + if (e.getPlayer().getUniqueId().equals(lastOwner.get())) { msg = claimChunk.getMessages().chunkLeaveSelf; } else if (name == null) { msg = claimChunk.getMessages().chunkLeaveUnknown; @@ -82,22 +90,21 @@ public void onPlayerMove(PlayerMoveEvent e) { private void showTitle(Player player, Chunk newChunk) { // Get the UUID of the new chunk owner - UUID newOwner = claimChunk.getChunkHandler().getOwner(newChunk.getWorld(), newChunk.getX(), - newChunk.getZ()); + Optional newOwner = claimChunk.getChunkHandler().getOwner(new ChunkPos(newChunk)); // Check if this player doesn't own the new chunk - if (newOwner != null && !player.getUniqueId().equals(newOwner)) { + if (newOwner.isPresent() && !player.getUniqueId().equals(newOwner.get())) { // Get the name of the chunks for the owner of this chunk and display it PlayerHandler ph = claimChunk.getPlayerHandler(); - String newName = ph.getChunkName(newOwner); + String newName = ph.getChunkName(newOwner.get()); String text = ((newName == null) ? claimChunk.getMessages().unknownChunkOwner // Something probably went wrong with the PlayerHandler : claimChunk.getMessages().chunkOwner.replace("%%PLAYER%%", newName)); showTitleRaw(true, player, text); // Send a message to the chunk owner if possible - if (!Utils.hasPerm(player, false, "invis") && ph.hasAlerts(newOwner)) { - Player owner = Bukkit.getPlayer(newOwner); + if (!Utils.hasPerm(player, false, "invis") && ph.hasAlerts(newOwner.get())) { + Player owner = Bukkit.getPlayer(newOwner.get()); if (owner != null) { if (owner.canSee(player) || !claimChunk.chConfig().getHideAlertsForVanishedPlayers()) { diff --git a/src/main/java/com/cjburkey/claimchunk/event/WorldProfileEventHandler.java b/src/main/java/com/cjburkey/claimchunk/event/WorldProfileEventHandler.java index 1d13c34a..8b9a5654 100644 --- a/src/main/java/com/cjburkey/claimchunk/event/WorldProfileEventHandler.java +++ b/src/main/java/com/cjburkey/claimchunk/event/WorldProfileEventHandler.java @@ -4,6 +4,7 @@ import com.cjburkey.claimchunk.Messages; import com.cjburkey.claimchunk.Utils; import com.cjburkey.claimchunk.chunk.ChunkHandler; +import com.cjburkey.claimchunk.chunk.ChunkPos; import com.cjburkey.claimchunk.config.*; import com.cjburkey.claimchunk.config.access.BlockAccess; import com.cjburkey.claimchunk.config.access.EntityAccess; @@ -157,10 +158,13 @@ public void onBlockBreak(BlockBreakEvent event) { public void onBlockPlace(BlockPlaceEvent event) { if (event == null || event.isCancelled()) return; - // Check to make sure this block doesn't connect to any blocks in a claim - onBlockAdjacentCheck(() -> event.setCancelled(true), - event.getPlayer(), - event.getBlock()); + // Check to make sure this chest doesn't connect to a chest in a claim + // TODO: MAKE THIS A CUSTOMIZABLE LIST + if (event.getBlock().getType() == Material.CHEST) { + onBlockAdjacentCheck(() -> event.setCancelled(true), + event.getPlayer(), + event.getBlock()); + } // Make sure the event wasn't cancelled by the adjacent check if (event.isCancelled()) return; @@ -590,13 +594,11 @@ private void onEntityEvent(@Nonnull Runnable cancel, @Nonnull Player player, @Nonnull Entity entity, @Nonnull EntityAccess.EntityAccessType accessType) { - // Get the profile for this world ClaimChunkWorldProfile profile = claimChunk.getProfileManager().getProfile(entity.getWorld().getName()); // check if the world profile is enabled if(profile.enabled) { - // Get necessary information final UUID ply = player.getUniqueId(); final UUID chunkOwner = claimChunk.getChunkHandler().getOwner(entity.getLocation().getChunk()); @@ -629,7 +631,7 @@ private void onBlockAdjacentCheck(@Nonnull Runnable cancel, // Get necessary information final UUID ply = player.getUniqueId(); - final UUID chunkOwner = claimChunk.getChunkHandler().getOwner(block.getChunk()); + final Optional chunkOwner = claimChunk.getChunkHandler().getOwner(new ChunkPos(block.getChunk())); // Loop through adjacent horizontal neighbors for (int x = -1; x <= 1; x++) { @@ -638,17 +640,17 @@ private void onBlockAdjacentCheck(@Nonnull Runnable cancel, if (x != z) { // Get neighbor info final Block neighbor = block.getRelative(x, 0, z); - final UUID neighborOwner = claimChunk.getChunkHandler().getOwner(neighbor.getChunk()); + final Optional neighborOwner = claimChunk.getChunkHandler().getOwner(new ChunkPos(neighbor.getChunk())); // Make sure the neighbor block is the same type and is owned by someone other than the owner // for the chunk in which they're building, or this person has access to build in that owner's // chunk - final boolean isOwner = (chunkOwner != null && chunkOwner.equals(ply)); + final boolean isOwner = (chunkOwner.isPresent() && chunkOwner.get().equals(ply)); final boolean isOwnerOrAccess = isOwner - || (chunkOwner != null && claimChunk.getPlayerHandler().hasAccess(chunkOwner, ply)); + || (chunkOwner.isPresent() && claimChunk.getPlayerHandler().hasAccess(chunkOwner.get(), ply)); if (neighbor.getType() == block.getType() - && neighborOwner != null - && neighborOwner != chunkOwner + && neighborOwner.isPresent() + && !neighborOwner.equals(chunkOwner) && !isOwnerOrAccess) { // check if the player has AdminOverride @@ -658,7 +660,7 @@ private void onBlockAdjacentCheck(@Nonnull Runnable cancel, cancel.run(); // Send cancellation message - final String ownerName = claimChunk.getPlayerHandler().getChunkName(neighborOwner); + final String ownerName = claimChunk.getPlayerHandler().getChunkName(neighborOwner.get()); Utils.toPlayer(player, Messages.replaceLocalizedMsg( player, @@ -682,7 +684,6 @@ private void onBlockEvent(@Nonnull Runnable cancel, @Nonnull Material blockType, @Nonnull Block block, @Nonnull BlockAccess.BlockAccessType accessType) { - // Get the profile for this world ClaimChunkWorldProfile profile = claimChunk.getProfileManager().getProfile(block.getWorld().getName()); @@ -707,7 +708,6 @@ private void onBlockEvent(@Nonnull Runnable cancel, Messages.sendAccessDeniedBlockMessage(player, claimChunk, blockType.getKey(), accessType, chunkOwner); } } - } private void onExplosionForEntityEvent(@Nonnull Runnable cancel, @@ -720,7 +720,7 @@ private void onExplosionForEntityEvent(@Nonnull Runnable cancel, // Delegate event cancellation to the world profile if (profile.enabled - && !profile.getEntityAccess(claimChunk.getChunkHandler().isClaimed(entity.getLocation().getChunk()), + && !profile.getEntityAccess(claimChunk.getChunkHandler().getHasOwner(new ChunkPos(entity.getLocation().getChunk())), worldName, entity.getType()).allowExplosion) { cancel.run(); @@ -751,13 +751,13 @@ private void onExplosionEvent(@Nonnull World world, // Check if this type of block should be protected if (cancelChunks.computeIfAbsent(chunk, c -> - !worldProfile.getBlockAccess(chunkHandler.isClaimed(c), + !worldProfile.getBlockAccess(chunkHandler.getHasOwner(new ChunkPos(c)), worldName, block.getType()).allowExplosion)) { // Try to remove the block from the explosion list if (!blockList.remove(block)) { - Utils.err("Failed to remove block of type \"%s\" at %s,%s,%s in world %s", + Utils.err("Failed to remove block of type \"%s\" from explosion at %s,%s,%s in world %s", block.getType(), block.getLocation().getBlockX(), block.getLocation().getBlockY(), @@ -778,8 +778,8 @@ private void onSpreadEvent(@Nonnull Runnable cancel, Chunk newChunk = newBlock.getChunk(); // Get the owners of the chunks - UUID sourceOwner = claimChunk.getChunkHandler().getOwner(sourceChunk); - UUID newOwner = claimChunk.getChunkHandler().getOwner(newChunk); + Optional sourceOwner = claimChunk.getChunkHandler().getOwner(new ChunkPos(sourceChunk)); + Optional newOwner = claimChunk.getChunkHandler().getOwner(new ChunkPos(newChunk)); // Get the profile for this world ClaimChunkWorldProfile profile = claimChunk.getProfileManager().getProfile(sourceChunk.getWorld().getName()); @@ -800,7 +800,7 @@ private void onSpreadEvent(@Nonnull Runnable cancel, } // Check if this needs to be cancelled - if (spreadProfile.getShouldCancel(sourceOwner, newOwner)) { + if (spreadProfile.getShouldCancel(sourceOwner.orElse(null), newOwner.orElse(null))) { cancel.run(); } } @@ -815,7 +815,7 @@ private void onPistonAction(@Nonnull Runnable cancel, if (profile.enabled) { // Get the source and target chunks - UUID sourceChunkOwner = claimChunk.getChunkHandler().getOwner(piston.getChunk()); + Optional sourceChunkOwner = claimChunk.getChunkHandler().getOwner(new ChunkPos(piston.getChunk())); HashMap targetChunksOwners = new HashMap<>(); // Keep a set of all blocks that will be moved, including future // positions of the moving blocks @@ -828,11 +828,11 @@ private void onPistonAction(@Nonnull Runnable cancel, // chunks for (Block block : allBlocks) { targetChunksOwners.computeIfAbsent(block.getChunk(), - (chunk) -> claimChunk.getChunkHandler().getOwner(chunk)); + (chunk) -> claimChunk.getChunkHandler().getOwner(new ChunkPos(chunk)).orElse(null)); } // Check if unclaimed to claimed piston actions are protected - if (sourceChunkOwner == null && !profile.pistonExtend.fromUnclaimedIntoClaimed) { + if (!sourceChunkOwner.isPresent() && !profile.pistonExtend.fromUnclaimedIntoClaimed) { for (UUID owner : targetChunksOwners.values()) { if (owner != null) { cancel.run(); @@ -842,7 +842,7 @@ private void onPistonAction(@Nonnull Runnable cancel, } // Check if claimed to unclaimed piston actions are protected - if (sourceChunkOwner != null && !profile.pistonExtend.fromClaimedIntoUnclaimed) { + if (sourceChunkOwner.isPresent() && !profile.pistonExtend.fromClaimedIntoUnclaimed) { for (UUID owner : targetChunksOwners.values()) { if (owner == null) { cancel.run(); @@ -852,11 +852,11 @@ private void onPistonAction(@Nonnull Runnable cancel, } // Check if claimed to claimed piston actions are protected - if (sourceChunkOwner != null && !profile.pistonExtend.fromClaimedIntoDiffClaimed) { + if (sourceChunkOwner.isPresent() && !profile.pistonExtend.fromClaimedIntoDiffClaimed) { for (UUID owner : targetChunksOwners.values()) { if (owner != null - && !owner.equals(sourceChunkOwner) - && !claimChunk.getPlayerHandler().hasAccess(owner, sourceChunkOwner)) { + && !owner.equals(sourceChunkOwner.get()) + && !claimChunk.getPlayerHandler().hasAccess(owner, sourceChunkOwner.get())) { cancel.run(); return; } @@ -869,7 +869,7 @@ private void onCommand(@Nonnull Runnable cancel, @Nonnull Player player, @Nonnull PluginCommand command) { final UUID ply = player.getUniqueId(); - final UUID chunkOwner = claimChunk.getChunkHandler().getOwner(player.getLocation().getChunk()); + final Optional chunkOwner = claimChunk.getChunkHandler().getOwner(new ChunkPos(player.getLocation().getChunk())); // Get the profile for this world ClaimChunkWorldProfile profile = claimChunk.getProfileManager().getProfile(player.getWorld().getName()); @@ -879,9 +879,9 @@ private void onCommand(@Nonnull Runnable cancel, // Determine which list of blocked commands to check HashMap cmds = profile.blockedCmdsInOwnClaimed; - if (chunkOwner == null) { + if (!chunkOwner.isPresent()) { cmds = profile.blockedCmdsInUnclaimed; - } else if (!chunkOwner.equals(ply)) { + } else if (!chunkOwner.get().equals(ply)) { cmds = profile.blockedCmdsInDiffClaimed; } // Cancel if necessary diff --git a/src/main/java/com/cjburkey/claimchunk/placeholder/ClaimChunkPlaceholders.java b/src/main/java/com/cjburkey/claimchunk/placeholder/ClaimChunkPlaceholders.java index 16349684..f902af59 100644 --- a/src/main/java/com/cjburkey/claimchunk/placeholder/ClaimChunkPlaceholders.java +++ b/src/main/java/com/cjburkey/claimchunk/placeholder/ClaimChunkPlaceholders.java @@ -1,6 +1,7 @@ package com.cjburkey.claimchunk.placeholder; import com.cjburkey.claimchunk.ClaimChunk; +import com.cjburkey.claimchunk.chunk.ChunkPos; import me.clip.placeholderapi.expansion.PlaceholderExpansion; import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; @@ -38,7 +39,7 @@ public ClaimChunkPlaceholders(ClaimChunk claimChunk) { // This player's total number of claimed chunks offlinePlayerPlaceholders.put("my_claims", - ply -> claimChunk.getChunkHandler().getClaimed(ply.getUniqueId())); + ply -> claimChunk.getChunkHandler().getClaimedChunksCount(ply.getUniqueId())); /* Online player placeholders */ @@ -130,9 +131,9 @@ public String onPlaceholderRequest(Player onlinePlayer, @NotNull String identifi } // Get the owner of the chunk in which `onlinePlayer` is standing - UUID chunkOwner = claimChunk.getChunkHandler().getOwner(onlinePlayer.getLocation().getChunk()); + Optional chunkOwner = claimChunk.getChunkHandler().getOwner(new ChunkPos(onlinePlayer.getLocation().getChunk())); return Optional.ofNullable(playerOwnerPlaceholders.get(identifier)) - .map(f -> f.apply(onlinePlayer, Optional.ofNullable(chunkOwner))) + .map(f -> f.apply(onlinePlayer, chunkOwner)) .map(Object::toString).orElse(null); } } diff --git a/src/main/java/com/cjburkey/claimchunk/service/prereq/claim/PrereqClaimData.java b/src/main/java/com/cjburkey/claimchunk/service/prereq/claim/PrereqClaimData.java index 980afd05..68a68f1f 100644 --- a/src/main/java/com/cjburkey/claimchunk/service/prereq/claim/PrereqClaimData.java +++ b/src/main/java/com/cjburkey/claimchunk/service/prereq/claim/PrereqClaimData.java @@ -28,7 +28,7 @@ public PrereqClaimData(@Nonnull ClaimChunk claimChunk, this.player = player; this.claimedBefore = claimChunk.getChunkHandler() - .getClaimed(playerId); + .getClaimedChunksCount(playerId); this.maxClaimed = claimChunk.getRankHandler() .getMaxClaimsForPlayer(player); this.freeClaims = claimChunk.chConfig().getFirstFreeChunks(); diff --git a/src/main/java/com/cjburkey/claimchunk/service/prereq/claim/UnclaimedPrereq.java b/src/main/java/com/cjburkey/claimchunk/service/prereq/claim/UnclaimedPrereq.java index 0a149c6e..61068f0f 100644 --- a/src/main/java/com/cjburkey/claimchunk/service/prereq/claim/UnclaimedPrereq.java +++ b/src/main/java/com/cjburkey/claimchunk/service/prereq/claim/UnclaimedPrereq.java @@ -1,5 +1,6 @@ package com.cjburkey.claimchunk.service.prereq.claim; +import com.cjburkey.claimchunk.chunk.ChunkPos; import org.jetbrains.annotations.NotNull; import java.util.Optional; @@ -14,7 +15,7 @@ public int getWeight() { @Override public boolean getPassed(@Nonnull @NotNull PrereqClaimData data) { - return !data.claimChunk.getChunkHandler().isClaimed(data.chunk); + return !data.claimChunk.getChunkHandler().getHasOwner(new ChunkPos(data.chunk)); } @Override diff --git a/unbuilt_readme.md b/unbuilt_readme.md index d589be51..53a7856e 100644 --- a/unbuilt_readme.md +++ b/unbuilt_readme.md @@ -48,7 +48,7 @@ Here are some plugins that either have addons for or integrations with ClaimChun Building [![Build Status](https://travis-ci.com/cjburkey01/ClaimChunk.svg?branch=master)](https://travis-ci.com/cjburkey01/ClaimChunk) -------- -[![Version Info](https://img.shields.io/static/v1?label=Repository%20Version&message=@PLUGIN_VERSION@&color=ff5555&style=for-the-badge)](https://github.com/cjburkey01/ClaimChunk/archive/master.zip) +[![Version Info](https://img.shields.io/static/v1?label=Repository%20Version&message=@PLUGIN_VERSION@&color=ff5555&style=for-the-badge)](https://github.com/cjburkey01/ClaimChunk/archive/refs/heads/v0.1.0.zip) If you want to obtain a version of the plugin that isn't available yet (like a snapshot), you can do so by asking on the Discord or building it yourself. Here's how to build it yourself: