flan/src/main/java/com/flemmli97/flan/claim/Claim.java

609 lines
23 KiB
Java
Raw Normal View History

2020-08-23 12:52:36 +00:00
package com.flemmli97.flan.claim;
import com.flemmli97.flan.api.ClaimPermission;
2021-03-08 20:42:12 +00:00
import com.flemmli97.flan.api.ClaimPermissionEvent;
import com.flemmli97.flan.api.PermissionRegistry;
import com.flemmli97.flan.config.ConfigHandler;
2020-08-23 12:52:36 +00:00
import com.flemmli97.flan.player.PlayerClaimData;
import com.google.common.collect.ImmutableList;
2021-05-02 16:24:46 +00:00
import com.google.common.collect.Lists;
2020-08-23 12:52:36 +00:00
import com.google.gson.JsonArray;
2020-08-24 19:03:06 +00:00
import com.google.gson.JsonElement;
2020-08-23 12:52:36 +00:00
import com.google.gson.JsonObject;
import com.mojang.authlib.GameProfile;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.network.ServerPlayerEntity;
2020-08-24 19:03:06 +00:00
import net.minecraft.server.world.ServerWorld;
import net.minecraft.text.LiteralText;
import net.minecraft.text.Text;
2021-03-08 20:42:12 +00:00
import net.minecraft.util.ActionResult;
import net.minecraft.util.Formatting;
2020-08-23 12:52:36 +00:00
import net.minecraft.util.math.BlockPos;
2021-05-02 16:06:56 +00:00
import java.util.ArrayList;
import java.util.Arrays;
2021-05-02 16:06:56 +00:00
import java.util.HashMap;
import java.util.HashSet;
2020-08-23 12:52:36 +00:00
import java.util.List;
import java.util.Map;
2020-08-24 20:32:29 +00:00
import java.util.Set;
2020-08-23 12:52:36 +00:00
import java.util.UUID;
public class Claim implements IPermissionContainer {
2020-08-23 12:52:36 +00:00
private boolean dirty;
private int minX, minZ, maxX, maxZ, minY;
private UUID owner;
2020-08-23 12:52:36 +00:00
private UUID claimID;
private LiteralText claimName;
2021-05-02 16:06:56 +00:00
private final Map<ClaimPermission, Boolean> globalPerm = new HashMap<>();
private final Map<String, Map<ClaimPermission, Boolean>> permissions = new HashMap<>();
2020-08-23 12:52:36 +00:00
2021-05-02 16:06:56 +00:00
private final Map<UUID, String> playersGroups = new HashMap<>();
2020-08-23 12:52:36 +00:00
2021-05-02 16:06:56 +00:00
private final List<Claim> subClaims = new ArrayList<>();
2020-08-23 12:52:36 +00:00
2020-08-24 19:03:06 +00:00
private UUID parent;
private Claim parentClaim;
2020-08-23 12:52:36 +00:00
/**
* Flag for players tracking this claim
*/
private boolean removed;
2020-08-24 19:03:06 +00:00
private final ServerWorld world;
private Claim(ServerWorld world) {
this.world = world;
2020-08-23 12:52:36 +00:00
}
public Claim(BlockPos pos1, BlockPos pos2, ServerPlayerEntity creator) {
this(pos1.getX(), pos2.getX(), pos1.getZ(), pos2.getZ(), Math.min(pos1.getY(), pos2.getY()), creator.getUuid(), creator.getServerWorld(), PlayerClaimData.get(creator).playerDefaultGroups().isEmpty());
PlayerClaimData.get(creator).playerDefaultGroups().forEach((s, m) -> m.forEach((perm, bool) -> this.editPerms(null, s, perm, bool ? 1 : 0)));
}
2020-08-24 19:03:06 +00:00
public Claim(BlockPos pos1, BlockPos pos2, UUID creator, ServerWorld world) {
this(pos1.getX(), pos2.getX(), pos1.getZ(), pos2.getZ(), Math.min(pos1.getY(), pos2.getY()), creator, world);
2020-08-23 12:52:36 +00:00
}
2020-08-24 19:03:06 +00:00
public Claim(int x1, int x2, int z1, int z2, int minY, UUID creator, ServerWorld world) {
this(x1, x2, z1, z2, minY, creator, world, true);
}
public Claim(int x1, int x2, int z1, int z2, int minY, UUID creator, ServerWorld world, boolean setDefaultGroups) {
2020-08-23 12:52:36 +00:00
this.minX = Math.min(x1, x2);
this.minZ = Math.min(z1, z2);
this.maxX = Math.max(x1, x2);
this.maxZ = Math.max(z1, z2);
this.minY = Math.max(0, minY);
this.owner = creator;
2020-08-24 19:03:06 +00:00
this.world = world;
this.setDirty(true);
2021-04-11 23:50:31 +00:00
PermissionRegistry.getPerms().stream().filter(perm -> perm.defaultVal).forEach(perm -> this.globalPerm.put(perm, true));
if (setDefaultGroups)
ConfigHandler.config.defaultGroups.forEach((s, m) -> m.forEach((perm, bool) -> this.editPerms(null, s, perm, bool ? 1 : 0)));
2020-08-23 12:52:36 +00:00
}
2020-08-24 19:03:06 +00:00
public static Claim fromJson(JsonObject obj, UUID owner, ServerWorld world) {
Claim claim = new Claim(world);
2020-08-23 12:52:36 +00:00
claim.readJson(obj, owner);
return claim;
}
public void setClaimID(UUID uuid) {
this.claimID = uuid;
this.setDirty(true);
2020-08-23 12:52:36 +00:00
}
2020-09-02 13:36:58 +00:00
public void extendDownwards(BlockPos pos) {
this.minY = pos.getY();
this.setDirty(true);
}
2020-08-23 12:52:36 +00:00
public UUID getClaimID() {
return this.claimID;
}
public LiteralText getClaimName() {
return this.claimName;
}
public void setClaimName(LiteralText name) {
this.claimName = name;
this.setDirty(true);
}
2020-08-23 12:52:36 +00:00
public UUID getOwner() {
return this.owner;
}
public ServerWorld getWorld() {
2020-10-30 14:09:57 +00:00
return this.world;
}
2020-08-25 17:43:52 +00:00
public Claim parentClaim() {
if (this.parent == null)
2020-08-24 19:03:06 +00:00
return null;
2020-08-25 17:43:52 +00:00
if (this.parentClaim == null) {
2020-08-24 19:03:06 +00:00
ClaimStorage storage = ClaimStorage.get(this.world);
2020-08-25 19:06:36 +00:00
this.parentClaim = storage.getFromUUID(this.parent);
2020-08-24 19:03:06 +00:00
}
return this.parentClaim;
}
2020-08-25 17:43:52 +00:00
public void copySizes(Claim claim) {
2020-08-24 19:03:06 +00:00
this.minX = claim.minX;
this.maxX = claim.maxX;
this.minZ = claim.minZ;
this.maxZ = claim.maxZ;
this.minY = claim.minY;
this.removed = false;
this.setDirty(true);
2020-08-24 19:03:06 +00:00
}
public void toggleAdminClaim(ServerPlayerEntity player, boolean flag) {
2020-09-02 13:36:58 +00:00
if (!flag)
2020-09-03 16:00:37 +00:00
this.transferOwner(player.getUuid());
else {
this.owner = null;
this.subClaims.forEach(claim -> claim.owner = null);
}
this.setDirty(true);
}
2020-09-02 13:36:58 +00:00
public boolean isAdminClaim() {
return this.owner == null;
}
2020-09-03 16:00:37 +00:00
public void transferOwner(UUID player) {
this.owner = player;
this.subClaims.forEach(claim -> claim.owner = player);
this.setDirty(true);
}
2020-08-23 12:52:36 +00:00
public int getPlane() {
return (this.maxX - this.minX + 1) * (this.maxZ - this.minZ + 1);
2020-08-23 12:52:36 +00:00
}
/**
* @return The claims dimension in order: x, X, z, Z, y
*/
public int[] getDimensions() {
return new int[]{this.minX, this.maxX, this.minZ, this.maxZ, this.minY};
}
public boolean insideClaim(BlockPos pos) {
return this.minX <= pos.getX() && this.maxX >= pos.getX() && this.minZ <= pos.getZ() && this.maxZ >= pos.getZ() && this.minY <= pos.getY();
}
public boolean intersects(Claim other) {
return this.minX <= other.maxX && this.maxX >= other.minX && this.minZ <= other.maxZ && this.maxZ >= other.minZ;
2020-08-23 12:52:36 +00:00
}
public boolean isCorner(BlockPos pos) {
return (pos.getX() == this.minX && pos.getZ() == this.minZ) || (pos.getX() == this.minX && pos.getZ() == this.maxZ)
|| (pos.getX() == this.maxX && pos.getZ() == this.minZ) || (pos.getX() == this.maxX && pos.getZ() == this.maxZ);
}
public void remove() {
this.removed = true;
}
public boolean isRemoved() {
return this.removed;
}
public boolean canInteract(ServerPlayerEntity player, ClaimPermission perm, BlockPos pos, boolean message) {
2021-03-08 20:42:12 +00:00
ActionResult res = ClaimPermissionEvent.CHECK.invoker().check(player, perm, pos);
if (res != ActionResult.PASS)
return res != ActionResult.FAIL;
if (perm != null) {
ClaimPermission.PermissionFlag flag = perm.test.test(this, player, pos);
if (flag != ClaimPermission.PermissionFlag.PASS) {
if (flag == ClaimPermission.PermissionFlag.NO) {
if (message)
player.sendMessage(PermHelper.simpleColoredText(ConfigHandler.lang.noPermissionSimple, Formatting.DARK_RED), true);
return false;
}
return true;
}
}
2020-12-11 15:21:46 +00:00
Boolean global = ConfigHandler.config.getGlobal(this.world, perm);
if (!this.isAdminClaim() && global != null) {
if (global || this.isAdminIgnore(player))
return true;
if (message)
player.sendMessage(PermHelper.simpleColoredText(ConfigHandler.lang.noPermissionSimple, Formatting.DARK_RED), true);
return false;
2020-10-30 14:09:57 +00:00
}
if (PermissionRegistry.globalPerms().contains(perm)) {
2020-08-23 12:52:36 +00:00
for (Claim claim : this.subClaims) {
if (claim.insideClaim(pos)) {
2020-08-24 19:03:06 +00:00
return claim.canInteract(player, perm, pos, message);
2020-08-23 12:52:36 +00:00
}
}
2020-08-25 17:43:52 +00:00
if (this.hasPerm(perm))
2020-08-24 19:03:06 +00:00
return true;
2020-08-25 17:43:52 +00:00
if (message)
2020-08-25 19:06:36 +00:00
player.sendMessage(PermHelper.simpleColoredText(ConfigHandler.lang.noPermissionSimple, Formatting.DARK_RED), true);
2020-08-24 19:03:06 +00:00
return false;
2020-08-23 12:52:36 +00:00
}
2020-10-30 14:09:57 +00:00
if (this.isAdminIgnore(player) || player.getUuid().equals(this.owner))
2020-08-23 12:52:36 +00:00
return true;
if (perm != PermissionRegistry.EDITCLAIM && perm != PermissionRegistry.EDITPERMS)
2020-09-03 16:00:37 +00:00
for (Claim claim : this.subClaims) {
if (claim.insideClaim(pos)) {
2020-08-24 19:03:06 +00:00
return claim.canInteract(player, perm, pos, message);
2020-09-03 16:00:37 +00:00
}
2020-08-23 12:52:36 +00:00
}
if (this.playersGroups.containsKey(player.getUuid())) {
Map<ClaimPermission, Boolean> map = this.permissions.get(this.playersGroups.get(player.getUuid()));
2020-08-24 19:03:06 +00:00
if (map != null && map.containsKey(perm)) {
2020-08-25 17:43:52 +00:00
if (map.get(perm))
2020-08-24 19:03:06 +00:00
return true;
2020-08-25 17:43:52 +00:00
if (message)
2020-08-25 19:06:36 +00:00
player.sendMessage(PermHelper.simpleColoredText(ConfigHandler.lang.noPermissionSimple, Formatting.DARK_RED), true);
2020-08-24 19:03:06 +00:00
return false;
}
2020-08-23 12:52:36 +00:00
}
2020-08-25 17:43:52 +00:00
if (this.hasPerm(perm))
2020-08-24 19:03:06 +00:00
return true;
2020-08-25 17:43:52 +00:00
if (message)
2020-08-25 19:06:36 +00:00
player.sendMessage(PermHelper.simpleColoredText(ConfigHandler.lang.noPermissionSimple, Formatting.DARK_RED), true);
2020-08-24 19:03:06 +00:00
return false;
}
private boolean isAdminIgnore(ServerPlayerEntity player) {
2020-10-30 14:09:57 +00:00
return player == null || ((this.isAdminClaim() && player.hasPermissionLevel(2)) || PlayerClaimData.get(player).isAdminIgnoreClaim());
}
2020-08-24 19:03:06 +00:00
/**
* @return -1 for default, 0 for false, 1 for true
*/
public int permEnabled(ClaimPermission perm) {
2020-08-25 17:43:52 +00:00
return !this.globalPerm.containsKey(perm) ? -1 : this.globalPerm.get(perm) ? 1 : 0;
2020-08-23 12:52:36 +00:00
}
private boolean hasPerm(ClaimPermission perm) {
2020-08-25 17:43:52 +00:00
if (this.parentClaim() == null)
2020-08-24 19:03:06 +00:00
return this.permEnabled(perm) == 1;
2020-08-25 17:43:52 +00:00
if (this.permEnabled(perm) == -1)
return this.parentClaim().permEnabled(perm) == 1;
2020-08-24 19:03:06 +00:00
return this.permEnabled(perm) == 1;
}
private UUID generateUUID() {
UUID uuid = UUID.randomUUID();
for (Claim claim : this.subClaims)
2020-08-25 17:43:52 +00:00
if (claim.claimID.equals(uuid)) {
2020-10-30 14:38:24 +00:00
return this.generateUUID();
2020-08-24 19:03:06 +00:00
}
return uuid;
2020-08-23 12:52:36 +00:00
}
2020-08-24 20:32:29 +00:00
public Set<Claim> tryCreateSubClaim(BlockPos pos1, BlockPos pos2) {
2020-08-24 19:03:06 +00:00
Claim sub = new Claim(pos1, new BlockPos(pos2.getX(), 0, pos2.getZ()), this.owner, this.world);
sub.setClaimID(this.generateUUID());
2021-05-02 16:06:56 +00:00
Set<Claim> conflicts = new HashSet<>();
2020-08-25 17:43:52 +00:00
for (Claim other : this.subClaims)
if (sub.intersects(other)) {
conflicts.add(other);
}
2020-08-25 17:43:52 +00:00
if (conflicts.isEmpty()) {
2020-08-24 20:32:29 +00:00
sub.parent = this.claimID;
sub.parentClaim = this;
this.subClaims.add(sub);
this.setDirty(true);
2020-08-24 20:32:29 +00:00
}
return conflicts;
2020-08-23 12:52:36 +00:00
}
2020-08-25 17:43:52 +00:00
public void addSubClaimGriefprevention(Claim claim) {
2020-08-24 19:03:06 +00:00
claim.setClaimID(this.generateUUID());
claim.parent = this.claimID;
claim.parentClaim = this;
this.subClaims.add(claim);
this.setDirty(true);
2020-08-24 19:03:06 +00:00
}
2020-08-23 12:52:36 +00:00
public Claim getSubClaim(BlockPos pos) {
for (Claim claim : this.subClaims)
if (claim.insideClaim(pos))
return claim;
return null;
}
2020-08-25 17:43:52 +00:00
public boolean deleteSubClaim(Claim claim) {
2020-08-25 19:50:41 +00:00
claim.remove();
this.setDirty(true);
2020-08-24 19:03:06 +00:00
return this.subClaims.remove(claim);
}
2020-08-25 17:43:52 +00:00
public List<Claim> getAllSubclaims() {
return ImmutableList.copyOf(this.subClaims);
}
2020-08-25 17:43:52 +00:00
public Set<Claim> resizeSubclaim(Claim claim, BlockPos from, BlockPos to) {
2020-08-24 19:03:06 +00:00
int[] dims = claim.getDimensions();
2020-08-25 17:43:52 +00:00
BlockPos opposite = new BlockPos(dims[0] == from.getX() ? dims[1] : dims[0], dims[4], dims[2] == from.getZ() ? dims[3] : dims[2]);
2020-08-24 19:03:06 +00:00
Claim newClaim = new Claim(opposite, to, claim.claimID, this.world);
2021-05-02 16:06:56 +00:00
Set<Claim> conflicts = new HashSet<>();
2020-08-25 17:43:52 +00:00
for (Claim other : this.subClaims)
2020-08-24 19:03:06 +00:00
if (!claim.equals(other) && newClaim.intersects(other))
2020-08-24 20:32:29 +00:00
conflicts.add(other);
2020-08-25 19:50:41 +00:00
if (conflicts.isEmpty()) {
2020-08-24 20:32:29 +00:00
claim.copySizes(newClaim);
this.setDirty(true);
2020-08-25 19:50:41 +00:00
}
2020-08-24 20:32:29 +00:00
return conflicts;
2020-08-24 19:03:06 +00:00
}
2020-08-23 12:52:36 +00:00
public boolean setPlayerGroup(UUID player, String group, boolean force) {
if (player.equals(this.owner))
return false;
2020-08-23 12:52:36 +00:00
if (group == null) {
this.playersGroups.remove(player);
this.setDirty(true);
2020-08-23 12:52:36 +00:00
return true;
}
if (!this.playersGroups.containsKey(player) || force) {
this.playersGroups.put(player, group);
this.setDirty(true);
2020-08-23 12:52:36 +00:00
return true;
}
return false;
}
public List<String> playersFromGroup(MinecraftServer server, String group) {
2021-05-02 16:06:56 +00:00
List<UUID> l = new ArrayList<>();
2020-08-23 12:52:36 +00:00
this.playersGroups.forEach((uuid, g) -> {
if (g.equals(group))
l.add(uuid);
});
2021-05-02 16:06:56 +00:00
List<String> names = new ArrayList<>();
2020-08-23 12:52:36 +00:00
l.forEach(uuid -> {
GameProfile prof = server.getUserCache().getByUuid(uuid);
if (prof != null)
names.add(prof.getName());
});
names.sort(null);
return names;
}
public boolean editGlobalPerms(ServerPlayerEntity player, ClaimPermission toggle, int mode) {
if ((player != null && !this.canInteract(player, PermissionRegistry.EDITPERMS, player.getBlockPos())) || (!this.isAdminClaim() && ConfigHandler.config.globallyDefined(this.world, toggle)))
2020-10-30 14:09:57 +00:00
return false;
2020-08-24 19:03:06 +00:00
if (mode > 1)
mode = -1;
if (mode == -1)
2020-08-23 12:52:36 +00:00
this.globalPerm.remove(toggle);
else
2020-08-24 19:03:06 +00:00
this.globalPerm.put(toggle, mode == 1);
this.setDirty(true);
2020-10-30 14:09:57 +00:00
return true;
2020-08-23 12:52:36 +00:00
}
public boolean editPerms(ServerPlayerEntity player, String group, ClaimPermission perm, int mode) {
return this.editPerms(player, group, perm, mode, false);
}
2020-09-02 13:36:58 +00:00
2020-08-23 12:52:36 +00:00
/**
* Edit the permissions for a group. If not defined for the group creates a new default permission map for that group
*
* @param mode -1 = makes it resort to the global perm, 0 = deny perm, 1 = allow perm
* @return If editing was successful or not
*/
public boolean editPerms(ServerPlayerEntity player, String group, ClaimPermission perm, int mode, boolean alwaysCan) {
if (PermissionRegistry.globalPerms().contains(perm) || (!this.isAdminClaim() && ConfigHandler.config.globallyDefined(this.world, perm)))
return false;
if (alwaysCan || this.canInteract(player, PermissionRegistry.EDITPERMS, player.getBlockPos())) {
2020-08-23 12:52:36 +00:00
if (mode > 1)
mode = -1;
2020-08-23 13:16:26 +00:00
boolean has = this.permissions.containsKey(group);
2021-05-02 16:06:56 +00:00
Map<ClaimPermission, Boolean> perms = has ? this.permissions.get(group) : new HashMap<>();
2020-08-23 12:52:36 +00:00
if (mode == -1)
perms.remove(perm);
else
perms.put(perm, mode == 1);
if (!has)
this.permissions.put(group, perms);
this.setDirty(true);
2020-08-23 12:52:36 +00:00
return true;
}
return false;
}
public boolean removePermGroup(ServerPlayerEntity player, String group) {
if (this.canInteract(player, PermissionRegistry.EDITPERMS, player.getBlockPos())) {
2020-08-23 12:52:36 +00:00
this.permissions.remove(group);
2021-05-02 16:06:56 +00:00
List<UUID> toRemove = new ArrayList<>();
2020-08-23 12:52:36 +00:00
this.playersGroups.forEach((uuid, g) -> {
if (g.equals(group))
toRemove.add(uuid);
});
2020-08-23 13:16:26 +00:00
toRemove.forEach(this.playersGroups::remove);
this.setDirty(true);
2020-08-23 12:52:36 +00:00
return true;
}
return false;
}
public int groupHasPerm(String rank, ClaimPermission perm) {
2020-08-23 12:52:36 +00:00
if (!this.permissions.containsKey(rank) || !this.permissions.get(rank).containsKey(perm))
return -1;
return this.permissions.get(rank).get(perm) ? 1 : 0;
}
public List<String> groups() {
2021-05-02 16:18:16 +00:00
List<String> l = new ArrayList<>(this.permissions.keySet());
2020-08-23 12:52:36 +00:00
l.sort(null);
return l;
}
2020-08-25 19:50:41 +00:00
/**
* Only marks non sub claims
*/
public void setDirty(boolean flag) {
2020-09-02 13:36:58 +00:00
if (this.parentClaim() != null)
this.parentClaim().setDirty(flag);
2020-08-25 19:50:41 +00:00
else
this.dirty = flag;
2020-08-23 12:52:36 +00:00
}
public boolean isDirty() {
return this.dirty;
}
public void readJson(JsonObject obj, UUID uuid) {
this.claimID = UUID.fromString(obj.get("ID").getAsString());
2020-08-23 12:52:36 +00:00
JsonArray pos = obj.getAsJsonArray("PosxXzZY");
this.minX = pos.get(0).getAsInt();
this.maxX = pos.get(1).getAsInt();
this.minZ = pos.get(2).getAsInt();
this.maxZ = pos.get(3).getAsInt();
this.minY = pos.get(4).getAsInt();
2020-09-03 16:00:37 +00:00
if (obj.has("AdminClaim") && obj.get("AdminClaim").getAsBoolean())
this.owner = null;
else
this.owner = uuid;
2020-08-23 12:52:36 +00:00
this.globalPerm.clear();
this.permissions.clear();
this.subClaims.clear();
2020-08-25 17:43:52 +00:00
if (obj.has("Parent"))
2020-08-24 19:03:06 +00:00
this.parent = UUID.fromString(obj.get("Parent").getAsString());
2020-08-23 12:52:36 +00:00
if (obj.has("GlobalPerms")) {
2020-08-25 17:43:52 +00:00
if (this.parent == null) {
obj.getAsJsonArray("GlobalPerms").forEach(perm -> this.globalPerm.put(PermissionRegistry.get(perm.getAsString()), true));
2020-08-25 17:43:52 +00:00
} else {
obj.getAsJsonObject("GlobalPerms").entrySet().forEach(entry -> this.globalPerm.put(PermissionRegistry.get(entry.getKey()), entry.getValue().getAsBoolean()));
2020-08-24 19:03:06 +00:00
}
2020-08-23 12:52:36 +00:00
}
if (obj.has("PermGroup")) {
JsonObject perms = obj.getAsJsonObject("PermGroup");
perms.entrySet().forEach(key -> {
2021-05-02 16:06:56 +00:00
Map<ClaimPermission, Boolean> map = new HashMap<>();
2020-08-23 12:52:36 +00:00
JsonObject group = key.getValue().getAsJsonObject();
group.entrySet().forEach(gkey -> map.put(PermissionRegistry.get(gkey.getKey()), gkey.getValue().getAsBoolean()));
2020-08-23 12:52:36 +00:00
this.permissions.put(key.getKey(), map);
});
}
if (obj.has("PlayerPerms")) {
JsonObject pl = obj.getAsJsonObject("PlayerPerms");
pl.entrySet().forEach(key -> this.playersGroups.put(UUID.fromString(key.getKey()), key.getValue().getAsString()));
}
if (obj.has("SubClaims")) {
2020-08-24 19:03:06 +00:00
obj.getAsJsonArray("SubClaims").forEach(sub -> this.subClaims.add(Claim.fromJson(sub.getAsJsonObject(), this.owner, this.world)));
2020-08-23 12:52:36 +00:00
}
}
public JsonObject toJson(JsonObject obj) {
obj.addProperty("ID", this.claimID.toString());
2020-08-23 12:52:36 +00:00
JsonArray pos = new JsonArray();
pos.add(this.minX);
pos.add(this.maxX);
pos.add(this.minZ);
pos.add(this.maxZ);
pos.add(this.minY);
obj.add("PosxXzZY", pos);
2020-08-25 17:43:52 +00:00
if (this.parent != null)
2020-08-24 19:03:06 +00:00
obj.addProperty("Parent", this.parent.toString());
2020-08-23 12:52:36 +00:00
if (!this.globalPerm.isEmpty()) {
2020-08-24 19:03:06 +00:00
JsonElement gPerm;
2020-08-25 17:43:52 +00:00
if (this.parent == null) {
2020-08-24 19:03:06 +00:00
gPerm = new JsonArray();
this.globalPerm.forEach((perm, bool) -> {
if (bool)
((JsonArray) gPerm).add(perm.id);
2020-08-24 19:03:06 +00:00
});
2020-08-25 17:43:52 +00:00
} else {
2020-08-24 19:03:06 +00:00
gPerm = new JsonObject();
this.globalPerm.forEach((perm, bool) -> ((JsonObject) gPerm).addProperty(perm.id, bool));
2020-08-24 19:03:06 +00:00
}
2020-08-23 12:52:36 +00:00
obj.add("GlobalPerms", gPerm);
}
if (!this.permissions.isEmpty()) {
JsonObject perms = new JsonObject();
this.permissions.forEach((s, pmap) -> {
JsonObject group = new JsonObject();
pmap.forEach((perm, bool) -> group.addProperty(perm.id, bool));
2020-08-23 12:52:36 +00:00
perms.add(s, group);
});
obj.add("PermGroup", perms);
}
if (!this.playersGroups.isEmpty()) {
JsonObject pl = new JsonObject();
this.playersGroups.forEach((uuid, s) -> pl.addProperty(uuid.toString(), s));
obj.add("PlayerPerms", pl);
}
if (!this.subClaims.isEmpty()) {
JsonArray list = new JsonArray();
this.subClaims.forEach(p -> list.add(p.toJson(new JsonObject())));
obj.add("SubClaims", list);
}
return obj;
}
@Override
public int hashCode() {
2020-08-25 17:43:52 +00:00
return this.claimID == null ? Arrays.hashCode(this.getDimensions()) : this.claimID.hashCode();
2020-08-23 12:52:36 +00:00
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj instanceof Claim) {
Claim other = (Claim) obj;
2020-08-25 17:43:52 +00:00
if (this.claimID == null && other.claimID == null)
return Arrays.equals(this.getDimensions(), ((Claim) obj).getDimensions());
2020-08-25 17:43:52 +00:00
if (this.claimID != null)
return this.claimID.equals(((Claim) obj).claimID);
}
2020-08-23 12:52:36 +00:00
return false;
}
@Override
public String toString() {
return String.format("Claim:[ID=%s, Owner=%s, from: [x=%d,z=%d], to: [x=%d,z=%d]", this.claimID != null ? this.claimID.toString() : "null", this.owner != null ? this.owner.toString() : "Admin", this.minX, this.minZ, this.maxX, this.maxZ);
2020-08-23 12:52:36 +00:00
}
public String formattedClaim() {
return String.format("[x=%d,z=%d] - [x=%d,z=%d]", this.minX, this.minZ, this.maxX, this.maxZ);
}
2020-08-25 17:43:52 +00:00
public List<Text> infoString(ServerPlayerEntity player) {
boolean perms = this.canInteract(player, PermissionRegistry.EDITPERMS, player.getBlockPos());
2021-05-02 16:06:56 +00:00
List<Text> l = new ArrayList<>();
2020-08-24 19:03:06 +00:00
l.add(PermHelper.simpleColoredText("=============================================", Formatting.GREEN));
2020-09-02 13:36:58 +00:00
GameProfile prof = this.owner != null ? player.getServer().getUserCache().getByUuid(this.owner) : null;
String ownerName = this.isAdminClaim() ? "Admin" : prof != null ? prof.getName() : "<UNKNOWN>";
2020-09-02 13:36:58 +00:00
if (this.parent == null)
2020-08-25 19:32:00 +00:00
l.add(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.claimBasicInfo, ownerName, this.minX, this.minZ, this.maxX, this.maxZ, this.subClaims.size()), Formatting.GOLD));
else
l.add(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.claimBasicInfoSub, ownerName, this.minX, this.minZ, this.maxX, this.maxZ), Formatting.GOLD));
2020-08-25 17:43:52 +00:00
if (perms) {
2020-08-24 19:03:06 +00:00
l.add(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.claimInfoPerms, this.globalPerm), Formatting.RED));
l.add(PermHelper.simpleColoredText(ConfigHandler.lang.claimGroupInfoHeader, Formatting.RED));
2021-05-02 16:06:56 +00:00
Map<String, List<String>> nameToGroup = new HashMap<>();
for (Map.Entry<UUID, String> e : this.playersGroups.entrySet()) {
GameProfile pgroup = player.getServer().getUserCache().getByUuid(e.getKey());
if (prof != null) {
2021-05-02 16:24:46 +00:00
nameToGroup.merge(e.getValue(), Lists.newArrayList(pgroup.getName()), (old, val) -> {
old.add(pgroup.getName());
return old;
});
}
}
for (Map.Entry<String, Map<ClaimPermission, Boolean>> e : this.permissions.entrySet()) {
2020-08-24 19:03:06 +00:00
l.add(PermHelper.simpleColoredText(String.format(" %s:", e.getKey()), Formatting.DARK_RED));
l.add(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.claimGroupPerms, e.getValue()), Formatting.RED));
2021-05-02 16:06:56 +00:00
l.add(PermHelper.simpleColoredText(String.format(ConfigHandler.lang.claimGroupPlayers, nameToGroup.getOrDefault(e.getKey(), new ArrayList<>())), Formatting.RED));
}
}
2020-08-24 19:03:06 +00:00
l.add(PermHelper.simpleColoredText("=============================================", Formatting.GREEN));
return l;
2020-08-23 12:52:36 +00:00
}
}