Skip to content

Commit

Permalink
Implement almost all of it? Except for data loading
Browse files Browse the repository at this point in the history
  • Loading branch information
cjburkey01 committed May 13, 2024
1 parent acb2d19 commit 8eb8392
Show file tree
Hide file tree
Showing 4 changed files with 368 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

import java.io.File;
import java.util.*;
import java.util.stream.Collectors;

/*
* I've actually just decided that we're gonna do it this way:
Expand All @@ -37,18 +38,22 @@ public class JournaledDataHandler implements IClaimChunkDataHandler {

@Getter private final File claimChunkDb;
private boolean init = false;
private HashMap<ChunkPos, DataChunk> claimedChunks;
private HashMap<UUID, FullPlayerData> joinedPlayers;
private HashMap<RegionPos, ClaimRegion> claimRegions;
private SqLiteWrapper sqLiteWrapper;

// Don't need this shit actually
// private HashMap<RegionPos, ClaimRegion> claimRegions;

public JournaledDataHandler(@NotNull File claimChunkDb) {
this.claimChunkDb = claimChunkDb;
}

@Override
public void init() {
joinedPlayers = new HashMap<>();
claimRegions = new HashMap<>();
// claimRegions = new HashMap<>();
claimedChunks = new HashMap<>();
sqLiteWrapper = new SqLiteWrapper(claimChunkDb);

init = true;
Expand All @@ -74,34 +79,40 @@ public void load() throws Exception {

@Override
public void addClaimedChunk(ChunkPos pos, UUID player) {
// TODO: mutating methods must call sqLiteWrapper methods
DataChunk chunk = new DataChunk(pos, player, new HashMap<>(), false);
claimedChunks.put(pos, chunk);
sqLiteWrapper.addClaimedChunk(chunk);
}

@Override
public void addClaimedChunks(DataChunk[] chunks) {
// TODO: mutating methods must call sqLiteWrapper methods
Arrays.stream(chunks).forEach(chunk -> addClaimedChunk(chunk.chunk, chunk.player));
}

@Override
public void removeClaimedChunk(ChunkPos pos) {
// TODO: mutating methods must call sqLiteWrapper methods
claimedChunks.remove(pos);
sqLiteWrapper.removeClaimedChunk(pos);
}

@Override
public boolean isChunkClaimed(ChunkPos pos) {
return false;
return claimedChunks.containsKey(pos);
}

@Override
public @Nullable UUID getChunkOwner(ChunkPos pos) {
return null;
DataChunk chunk = claimedChunks.get(pos);
return chunk == null ? null : chunk.player;
}

@Override
public DataChunk[] getClaimedChunks() {
return new DataChunk[0];
return claimedChunks.values().toArray(new DataChunk[0]);
}

// TODO: REMOVE

@Override
public boolean toggleTnt(ChunkPos pos) {
return false;
Expand All @@ -112,6 +123,14 @@ public boolean isTntEnabled(ChunkPos pos) {
return false;
}

// END TODO

@Override
public void addPlayer(FullPlayerData playerData) {
joinedPlayers.put(playerData.player, playerData);
sqLiteWrapper.addPlayer(playerData);
}

@Override
public void addPlayer(
UUID player,
Expand All @@ -120,19 +139,15 @@ public void addPlayer(
long lastOnlineTime,
boolean alerts,
int extraMaxClaims) {
joinedPlayers.put(
player,
addPlayer(
new FullPlayerData(
player, lastIgn, chunkName, lastOnlineTime, alerts, extraMaxClaims));

// TODO: mutating methods must call sqLiteWrapper methods
}

@Override
public void addPlayers(FullPlayerData[] players) {
Arrays.stream(players).forEach(player -> joinedPlayers.put(player.player, player));

// TODO: mutating methods must call sqLiteWrapper methods
// this::addPlayer calls SQLite mutation
Arrays.stream(players).forEach(this::addPlayer);
}

@Override
Expand All @@ -143,25 +158,24 @@ public void addPlayers(FullPlayerData[] players) {

@Override
public @Nullable UUID getPlayerUUID(String username) {
// TODO: THIS

for (FullPlayerData ply : joinedPlayers.values()) {
if (username.equals(ply.lastIgn)) return ply.player;
}
return null;
}

@Override
public void setPlayerLastOnline(UUID player, long time) {
FullPlayerData ply = joinedPlayers.get(player);
if (ply != null) ply.lastOnlineTime = time;

// TODO: mutating methods must call sqLiteWrapper methods
sqLiteWrapper.setPlayerLastOnline(player, time);
}

@Override
public void setPlayerChunkName(UUID player, @Nullable String name) {
FullPlayerData ply = joinedPlayers.get(player);
if (ply != null) ply.chunkName = name;

// TODO: mutating methods must call sqLiteWrapper methods
sqLiteWrapper.setPlayerChunkName(player, name);
}

@Override
Expand All @@ -171,63 +185,89 @@ public void setPlayerChunkName(UUID player, @Nullable String name) {
}

@Override
public void setPlayerReceiveAlerts(UUID player, boolean alerts) {
// TODO: mutating methods must call sqLiteWrapper methods
public void setPlayerReceiveAlerts(UUID player, boolean receiveAlerts) {
FullPlayerData ply = joinedPlayers.get(player);
if (ply != null) ply.alert = receiveAlerts;
sqLiteWrapper.setPlayerReceiveAlerts(player, receiveAlerts);
}

@Override
public boolean getPlayerReceiveAlerts(UUID player) {
return false;
FullPlayerData ply = joinedPlayers.get(player);
return ply != null && ply.alert;
}

@Override
public void setPlayerExtraMaxClaims(UUID player, int maxClaims) {
// TODO: mutating methods must call sqLiteWrapper methods
FullPlayerData ply = joinedPlayers.get(player);
if (ply != null) ply.extraMaxClaims = maxClaims;
sqLiteWrapper.setPlayerExtraMaxClaims(player, maxClaims);
}

@Override
public void addPlayerExtraMaxClaims(UUID player, int numToAdd) {
// TODO: mutating methods must call sqLiteWrapper methods
// This method executes database modification
setPlayerExtraMaxClaims(player, getPlayerExtraMaxClaims(player) + Math.abs(numToAdd));
}

@Override
public void takePlayerExtraMaxClaims(UUID player, int numToTake) {
// TODO: mutating methods must call sqLiteWrapper methods
// This method executes database modification
setPlayerExtraMaxClaims(player, Math.max(0, getPlayerExtraMaxClaims(player) - numToTake));
}

@Override
public int getPlayerExtraMaxClaims(UUID player) {
FullPlayerData ply = joinedPlayers.get(player);
if (ply != null) return ply.extraMaxClaims;
return 0;
}

@Override
public boolean hasPlayer(UUID player) {
return false;
return joinedPlayers.containsKey(player);
}

@Override
public Collection<SimplePlayerData> getPlayers() {
return null;
return joinedPlayers.values().stream()
.map(FullPlayerData::toSimplePlayer)
.collect(Collectors.toCollection(ArrayList::new));
}

@Override
public FullPlayerData[] getFullPlayerData() {
return new FullPlayerData[0];
return joinedPlayers.values().toArray(new FullPlayerData[0]);
}

@Override
public void givePlayerAccess(
ChunkPos chunk, UUID accessor, ChunkPlayerPermissions permissions) {
// TODO: mutating methods must call sqLiteWrapper methods
DataChunk chunkData = claimedChunks.get(chunk);
if (chunkData != null) {
ChunkPlayerPermissions previousPerms =
chunkData.playerPermissions.put(accessor, permissions);
if (previousPerms == null) {
// Player doesn't already have any access
sqLiteWrapper.addPlayerAccess(chunk, accessor, permissions.permissionFlags);
} else {
// Player has access, we're changing it
sqLiteWrapper.updatePlayerAccess(chunk, accessor, permissions.permissionFlags);
}
}
}

@Override
public void takePlayerAccess(ChunkPos chunk, UUID accessor) {
// TODO: mutating methods must call sqLiteWrapper methods
DataChunk chunkData = claimedChunks.get(chunk);
if (chunkData != null) chunkData.playerPermissions.remove(accessor);
sqLiteWrapper.removePlayerAccess(chunk, accessor);
}

@Override
public Map<UUID, ChunkPlayerPermissions> getPlayersWithAccess(ChunkPos chunk) {
DataChunk chunkData = claimedChunks.get(chunk);
if (chunkData != null) return chunkData.playerPermissions;
return null;
}
}
Loading

0 comments on commit 8eb8392

Please sign in to comment.