Skip to content

Commit

Permalink
Add extra max claim chunks to player data structure
Browse files Browse the repository at this point in the history
  • Loading branch information
cjburkey01 committed May 2, 2024
1 parent 21e5f85 commit c9f3526
Show file tree
Hide file tree
Showing 16 changed files with 367 additions and 131 deletions.
45 changes: 15 additions & 30 deletions src/main/java/com/cjburkey/claimchunk/ClaimChunk.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,7 @@
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.*;

// TODO: Split this plugin up into services that users can use
// Services:
Expand Down Expand Up @@ -89,14 +86,14 @@ public final class ClaimChunk extends JavaPlugin implements IClaimChunkPlugin {
// Whether the plugin should use an economy plugin
private boolean useEcon = false;
// An instance of the ClaimChunk economy handler
private Econ economy;
@Getter private Econ economy;

// An instance of the chunk handler
private ChunkHandler chunkHandler;
@Getter private ChunkHandler chunkHandler;
// An instance of the player handler
private PlayerHandler playerHandler;
@Getter private PlayerHandler playerHandler;
// An instance of the rank handler
private RankHandler rankHandler;
@Getter private RankHandler rankHandler;
// An instance of the world permissions manager
private ClaimChunkWorldProfileHandler profileManager;
// The main /chunk command
Expand All @@ -109,7 +106,7 @@ public final class ClaimChunk extends JavaPlugin implements IClaimChunkPlugin {
private FromPre0023 fromPre0023;

// An instance of the class responsible for handling all localized messages
private V2JsonMessages messages;
@Getter private V2JsonMessages messages;

// A list that contains all the players that are in admin mode.
@Getter private final AdminOverride adminOverrideHandler = new AdminOverride();
Expand Down Expand Up @@ -226,6 +223,7 @@ public void onEnable() {

chunkHandler = new ChunkHandler(dataHandler, this);
playerHandler = new PlayerHandler(dataHandler, this);

// As of version 0.0.23, the `ranks.json` file will be located in
// `/plugins/ClaimChunk` instead of `/plugins/ClaimChunk/data` to make
// it more accessible. The rank handler will automatically copy the
Expand Down Expand Up @@ -255,6 +253,7 @@ public void onEnable() {
} catch (Exception e) {
Utils.err("Failed to load the data handler, ClaimChunk will be disabled!");
Utils.err("Here is the error for reference:");
//noinspection CallToPrintStackTrace
e.printStackTrace();
disable();
return;
Expand All @@ -267,6 +266,7 @@ public void onEnable() {
} catch (Exception e) {
Utils.err("Failed to load ranks! No ranks will be loaded!");
Utils.err("Here is the error for reference:");
//noinspection CallToPrintStackTrace
e.printStackTrace();
}
Utils.debug("Loaded rank data.");
Expand Down Expand Up @@ -341,6 +341,7 @@ private void doUpdateCheck() {
}
} catch (Exception e) {
Utils.err("Failed to check for update");
//noinspection CallToPrintStackTrace
e.printStackTrace();
}
}
Expand Down Expand Up @@ -392,6 +393,7 @@ private boolean initDataHandler() {
Utils.err(
"Failed to initialize data storage system \"%s\", disabling ClaimChunk.",
dataHandler.getClass().getName());
//noinspection CallToPrintStackTrace
e.printStackTrace();
Utils.err("CLAIMCHUNK WILL NOT WORK WITHOUT A VALID DATA STORAGE SYSTEM!");
Utils.err(
Expand All @@ -407,6 +409,7 @@ private void initMessages() {
messages = V2JsonMessages.load(new File(getDataFolder(), "/messages.json"));
} catch (IOException e) {
Utils.err("Failed to load ClaimChunk/messages.json");
//noinspection CallToPrintStackTrace
e.printStackTrace();
}
}
Expand Down Expand Up @@ -567,6 +570,7 @@ private void taskSaveData() {
// Unload all of the world profiles so they'll be loaded next time they're needed
profileManager.unloadAllProfiles();
} catch (Exception e) {
//noinspection CallToPrintStackTrace
e.printStackTrace();
Utils.err("Couldn't reload data: \"%s\"", e.getMessage());
}
Expand All @@ -580,22 +584,6 @@ public ClaimChunkConfig getConfigHandler() {
return config;
}

public Econ getEconomy() {
return economy;
}

public PlayerHandler getPlayerHandler() {
return playerHandler;
}

public ChunkHandler getChunkHandler() {
return chunkHandler;
}

public RankHandler getRankHandler() {
return rankHandler;
}

public ClaimChunkWorldProfileHandler getProfileHandler() {
return profileManager;
}
Expand All @@ -604,10 +592,6 @@ public boolean useEconomy() {
return useEcon;
}

public V2JsonMessages getMessages() {
return messages;
}

public boolean isUpdateAvailable() {
return availableVersion != null && updateAvailable;
}
Expand Down Expand Up @@ -651,6 +635,7 @@ public void onDisable() {
dataHandler.exit();
Utils.debug("Cleaned up.");
} catch (Exception e) {
//noinspection CallToPrintStackTrace
e.printStackTrace();
}

Expand Down Expand Up @@ -689,7 +674,7 @@ public static ClaimChunk getInstance() {

public static class DataHandlerAlreadySetException extends Exception {

public static final long serialVersionUID = 49857948732L;
@Serial private static final long serialVersionUID = 49857948732L;

private DataHandlerAlreadySetException(
String newDataHandlerName, String existingDataHandlerName) {
Expand Down
13 changes: 6 additions & 7 deletions src/main/java/com/cjburkey/claimchunk/cmd/MainHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import com.cjburkey.claimchunk.chunk.ChunkHandler;
import com.cjburkey.claimchunk.chunk.ChunkOutlineHandler;
import com.cjburkey.claimchunk.chunk.ChunkPos;
import com.cjburkey.claimchunk.packet.ParticleHandler;
import com.cjburkey.claimchunk.player.PlayerHandler;
import com.cjburkey.claimchunk.rank.RankHandler;
import com.cjburkey.claimchunk.service.prereq.claim.*;
Expand All @@ -21,7 +20,6 @@

// TODO: DESTROY THIS CLASS ENTIRELY!

@SuppressWarnings("ClassCanBeRecord")
public final class MainHandler {

private final ClaimChunk claimChunk;
Expand Down Expand Up @@ -106,7 +104,7 @@ public void outlineChunk(ChunkPos chunk, Player showTo, int timeToShow) {
if (showTo.isOnline()) {
// If the player is still online, display the
// particles for them
ParticleHandler.spawnParticleForPlayers(
world.spawnParticle(
particle,
loc,
claimChunk
Expand Down Expand Up @@ -257,6 +255,7 @@ public boolean unclaimChunk(
Utils.err(
"Failed to unclaim chunk for player %s at %s,%s in %s",
p.getDisplayName(), x, z, world);
//noinspection CallToPrintStackTrace
e.printStackTrace();
}
return false;
Expand All @@ -275,7 +274,7 @@ private void accessChunk(
}

Player other = claimChunk.getServer().getPlayer(accessor);
UUID otherId = null;
UUID otherId;
if (other == null) {
otherId = claimChunk.getPlayerHandler().getUUID(accessor);
if (otherId == null) {
Expand Down Expand Up @@ -397,7 +396,7 @@ public void checkAccess(Player p, String playerToQuery) {
// Get permissions for all players with access
Map<UUID, Map<String, Boolean>> allPlayerPermissions =
claimChunk.getPlayerHandler().getAllPlayerPermissions(chunk);
if (allPlayerPermissions != null && allPlayerPermissions.size() != 0) {
if (allPlayerPermissions != null && !allPlayerPermissions.isEmpty()) {
for (Map.Entry<UUID, Map<String, Boolean>> e : allPlayerPermissions.entrySet()) {
String playerWithAccessUsername =
claimChunk.getPlayerHandler().getUsername(e.getKey());
Expand Down Expand Up @@ -439,7 +438,7 @@ public void revokeAccess(Player p, String[] playersToRevoke, boolean allChunks)
} else {
// Revoke access only to the chunk the executor is currently standing in
ChunkPos chunk = new ChunkPos(p.getLocation().getChunk());
if (claimChunk.getChunkHandler().getOwner(chunk).equals(p.getUniqueId())) {
if (p.getUniqueId().equals(claimChunk.getChunkHandler().getOwner(chunk))) {
chunks = new ChunkPos[] {chunk};
message = claimChunk.getMessages().revokeAccessCurrentChunk;
} else {
Expand Down Expand Up @@ -491,7 +490,7 @@ public void giveChunk(Player giver, Chunk chunk, String newOwner) {
// Get the receiving player's UUID
UUID given = givenPly.getUniqueId();

// Make sure the owner isn't trying to give the chunk to themself
// Make sure the owner isn't trying to give the chunk to themselves
if (giver.getUniqueId().equals(given)) {
Utils.toPlayer(giver, claimChunk.getMessages().giveNotYourself);
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,14 @@ public class ClaimChunkWorldProfileHandler {
+ " `false` will not.For blocks, the protections are: `B` for breaking, `P` for"
+ " placing, `I` for interacting, and `E` for exploding.\n"
+ "For entities, the protections are: `D` for damaging, `I` for interacting, and"
+ " `E` for exploding.\n" // hehe, "DIE" lol
+ "Note: These protections (except for exploding) are purely player-based.\n"
+ "I.e. `D` for damaging entities, when set to `D:false` will prevent players"
+ " from damaging the entity.\n\n"
+ "Examples:\n\n"
+ "To allow only interacting with all blocks in unclaimed chunks in this"
+ " world:\n\n"
+ "unclaimedChunks.blockAccesses:\n"
+ " "
+ " `E` for exploding.\n"
+ "Note: These protections (except for exploding) are purely player-based.\n"
+ "I.e. `D` for damaging entities, when set to `D:false` will prevent players from"
+ " damaging the entity.\n\n"
+ "Examples:\n\n"
+ "To allow only interacting with all blocks in unclaimed chunks in this world:\n\n"
+ "unclaimedChunks.blockAccesses:\n"
+ " "
+ ClaimChunkWorldProfile.DEFAULT
+ ": I:true B:false P:false E:false ;\n\n"
+ "(Note: the key `"
Expand All @@ -56,7 +55,6 @@ public class ClaimChunkWorldProfileHandler {
+ " defined here\")\n\n"
+ "Finally, the `_` label is for world properties. These will not vary between"
+ " unclaimed and claimed chunks.\n\n"
// TODO: MAKE WIKI PAGE
+ "More information will be available on the website:"
+ " https://claimchunk.cjburkey.com\n";

Expand All @@ -83,16 +81,6 @@ public ClaimChunkWorldProfileHandler(
this.writer = writer;
}

/**
* Merges the profiles from the given HashMap into this world manager, overriding any existing
* world profiles here.
*
* @param profiles The profiles to be merged.
*/
public void mergeProfiles(HashMap<String, ClaimChunkWorldProfile> profiles) {
this.profiles.putAll(profiles);
}

public @NotNull ClaimChunkWorldProfile getProfile(
@NotNull String worldName, @Nullable ClaimChunkWorldProfile defaultProfile) {
// Try to get the config from the ones already loaded
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.cjburkey.claimchunk.player.FullPlayerData;
import com.cjburkey.claimchunk.player.SimplePlayerData;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
Expand All @@ -23,8 +24,8 @@

/**
* Some servers keep MySQL on a separate host from the server. In this case, making a MySQL request
* is EXTREMELY slow. This data handler only makes requests when it initializes, saves data, and
* loads data. This handler uses a JSON handler so backups are possible.
* is (relatively) EXTREMELY slow. This data handler only makes requests when it initializes, saves
* data, and loads data. This handler uses a JSON handler so backups are possible.
*
* @param <T> The type of the backup data system.
* @since 0.0.16
Expand All @@ -36,11 +37,10 @@ public class BulkMySQLDataHandler<T extends IClaimChunkDataHandler> extends MySQ
private final boolean doBackups;
private final JsonDataHandler dataHandler;

@SuppressWarnings("WeakerAccess")
public BulkMySQLDataHandler(
ClaimChunk claimChunk,
File claimedChunksFile,
File joinedPlayersFile,
@NotNull ClaimChunk claimChunk,
@Nullable File claimedChunksFile,
@Nullable File joinedPlayersFile,
Supplier<T> oldDataHandler,
Consumer<T> onCleanOld) {
super(claimChunk, oldDataHandler, onCleanOld);
Expand Down Expand Up @@ -94,6 +94,7 @@ public void save() throws Exception {
}
} catch (Exception e) {
Utils.err("Failed to clear chunks table and accesses table");
//noinspection CallToPrintStackTrace
e.printStackTrace();
}
}
Expand All @@ -110,6 +111,7 @@ public void save() throws Exception {
statement.execute();
} catch (Exception e) {
Utils.err("Failed to clear players table");
//noinspection CallToPrintStackTrace
e.printStackTrace();
}
}
Expand Down Expand Up @@ -171,8 +173,13 @@ public boolean isTntEnabled(ChunkPos pos) {

@Override
public void addPlayer(
UUID player, String lastIgn, String chunkName, long lastOnlineTime, boolean alerts) {
dataHandler.addPlayer(player, lastIgn, chunkName, lastOnlineTime, alerts);
UUID player,
String lastIgn,
String chunkName,
long lastOnlineTime,
boolean alerts,
int maxClaims) {
dataHandler.addPlayer(player, lastIgn, chunkName, lastOnlineTime, alerts, maxClaims);
}

@Override
Expand Down Expand Up @@ -234,6 +241,27 @@ public boolean getPlayerReceiveAlerts(UUID player) {
return dataHandler.getPlayerReceiveAlerts(player);
}

@Override
public void setPlayerExtraMaxClaims(UUID player, int extraMaxClaims) {
dataHandler.setPlayerExtraMaxClaims(player, extraMaxClaims);
}

@Override
public void addPlayerExtraMaxClaims(UUID player, int numToAdd) {
// abs is performed in json data handler
dataHandler.addPlayerExtraMaxClaims(player, numToAdd);
}

@Override
public void takePlayerExtraMaxClaims(UUID player, int numToTake) {
dataHandler.takePlayerExtraMaxClaims(player, numToTake);
}

@Override
public int getPlayerExtraMaxClaims(UUID player) {
return dataHandler.getPlayerExtraMaxClaims(player);
}

@Override
public boolean hasPlayer(UUID player) {
return dataHandler.hasPlayer(player);
Expand Down
Loading

0 comments on commit c9f3526

Please sign in to comment.