change global defined perms to modifiable and unmodifiable variant. indirectly closes #58
This commit is contained in:
parent
8086c96558
commit
5e30049ce7
@ -10,6 +10,7 @@ import io.github.flemmli97.flan.Flan;
|
||||
import io.github.flemmli97.flan.api.ClaimPermission;
|
||||
import io.github.flemmli97.flan.api.ClaimPermissionEvent;
|
||||
import io.github.flemmli97.flan.api.PermissionRegistry;
|
||||
import io.github.flemmli97.flan.config.Config;
|
||||
import io.github.flemmli97.flan.config.ConfigHandler;
|
||||
import io.github.flemmli97.flan.player.PlayerClaimData;
|
||||
import net.minecraft.server.MinecraftServer;
|
||||
@ -211,9 +212,9 @@ public class Claim implements IPermissionContainer {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Boolean global = ConfigHandler.config.getGlobal(this.world, perm);
|
||||
if (!this.isAdminClaim() && global != null) {
|
||||
if (global || this.isAdminIgnore(player))
|
||||
Config.GlobalType global = ConfigHandler.config.getGlobal(this.world, perm);
|
||||
if (!this.isAdminClaim() && !global.canModify()) {
|
||||
if (global.getValue() || this.isAdminIgnore(player))
|
||||
return true;
|
||||
if (message)
|
||||
player.sendMessage(PermHelper.simpleColoredText(ConfigHandler.lang.noPermissionSimple, Formatting.DARK_RED), true);
|
||||
|
@ -2,7 +2,9 @@ package io.github.flemmli97.flan.claim;
|
||||
|
||||
import io.github.flemmli97.flan.api.ClaimPermission;
|
||||
import io.github.flemmli97.flan.api.PermissionRegistry;
|
||||
import io.github.flemmli97.flan.config.Config;
|
||||
import io.github.flemmli97.flan.config.ConfigHandler;
|
||||
import io.github.flemmli97.flan.player.PlayerClaimData;
|
||||
import net.minecraft.server.network.ServerPlayerEntity;
|
||||
import net.minecraft.server.world.ServerWorld;
|
||||
import net.minecraft.util.Formatting;
|
||||
@ -18,9 +20,9 @@ public class GlobalClaim implements IPermissionContainer {
|
||||
|
||||
@Override
|
||||
public boolean canInteract(ServerPlayerEntity player, ClaimPermission perm, BlockPos pos, boolean message) {
|
||||
Boolean global = ConfigHandler.config.getGlobal(this.world, perm);
|
||||
if (global != null) {
|
||||
if (global)
|
||||
Config.GlobalType global = ConfigHandler.config.getGlobal(this.world, perm);
|
||||
if (global != Config.GlobalType.NONE && (player == null || !PlayerClaimData.get(player).isAdminIgnoreClaim())) {
|
||||
if (global.getValue())
|
||||
return true;
|
||||
if (message)
|
||||
player.sendMessage(PermHelper.simpleColoredText(ConfigHandler.lang.noPermissionSimple, Formatting.DARK_RED), true);
|
||||
|
@ -67,16 +67,15 @@ public class Config {
|
||||
}));
|
||||
});
|
||||
|
||||
private final Map<String, Map<ClaimPermission, Boolean>> globalDefaultPerms = createHashMap(map -> {
|
||||
private final Map<String, Map<ClaimPermission, GlobalType>> globalDefaultPerms = createHashMap(map -> {
|
||||
map.put("*", createHashMap(perms -> {
|
||||
perms.put(PermissionRegistry.FLIGHT, true);
|
||||
perms.put(PermissionRegistry.MOBSPAWN, false);
|
||||
perms.put(PermissionRegistry.FLIGHT, GlobalType.ALLTRUE);
|
||||
perms.put(PermissionRegistry.MOBSPAWN, GlobalType.ALLFALSE);
|
||||
}));
|
||||
});
|
||||
|
||||
public Config(MinecraftServer server) {
|
||||
File configDir = FabricLoader.getInstance().getConfigDir().resolve("flan").toFile();
|
||||
//.getSavePath(WorldSavePath.ROOT).resolve("config/claimConfigs").toFile();
|
||||
try {
|
||||
if (!configDir.exists())
|
||||
configDir.mkdirs();
|
||||
@ -132,11 +131,14 @@ public class Config {
|
||||
this.globalDefaultPerms.clear();
|
||||
JsonObject glob = ConfigHandler.fromJson(obj, "globalDefaultPerms");
|
||||
glob.entrySet().forEach(e -> {
|
||||
Map<ClaimPermission, Boolean> perms = new HashMap<>();
|
||||
Map<ClaimPermission, GlobalType> perms = new HashMap<>();
|
||||
if (e.getValue().isJsonObject()) {
|
||||
e.getValue().getAsJsonObject().entrySet().forEach(jperm -> {
|
||||
try {
|
||||
perms.put(PermissionRegistry.get(jperm.getKey()), jperm.getValue().getAsBoolean());
|
||||
if (jperm.getValue().isJsonPrimitive() && jperm.getValue().getAsJsonPrimitive().isBoolean())
|
||||
perms.put(PermissionRegistry.get(jperm.getKey()), jperm.getValue().getAsBoolean() ? GlobalType.ALLTRUE : GlobalType.ALLFALSE);
|
||||
else
|
||||
perms.put(PermissionRegistry.get(jperm.getKey()), GlobalType.valueOf(jperm.getValue().getAsString()));
|
||||
} catch (NullPointerException ex) {
|
||||
Flan.log("No permission with name {}", jperm.getKey());
|
||||
}
|
||||
@ -185,7 +187,7 @@ public class Config {
|
||||
JsonObject global = new JsonObject();
|
||||
this.globalDefaultPerms.forEach((key, value) -> {
|
||||
JsonObject perm = new JsonObject();
|
||||
value.forEach((key1, value1) -> perm.addProperty(key1.id, value1));
|
||||
value.forEach((key1, value1) -> perm.addProperty(key1.id, value1.toString()));
|
||||
global.add(key, perm);
|
||||
});
|
||||
obj.add("globalDefaultPerms", global);
|
||||
@ -202,15 +204,15 @@ public class Config {
|
||||
}
|
||||
|
||||
public boolean globallyDefined(ServerWorld world, ClaimPermission perm) {
|
||||
return getGlobal(world, perm) != null;
|
||||
return !getGlobal(world, perm).canModify();
|
||||
}
|
||||
|
||||
public Boolean getGlobal(ServerWorld world, ClaimPermission perm) {
|
||||
public GlobalType getGlobal(ServerWorld world, ClaimPermission perm) {
|
||||
//Update permission map if not done already
|
||||
Map<ClaimPermission, Boolean> allMap = ConfigHandler.config.globalDefaultPerms.get("*");
|
||||
Map<ClaimPermission, GlobalType> allMap = ConfigHandler.config.globalDefaultPerms.get("*");
|
||||
if (allMap != null) {
|
||||
world.getServer().getWorlds().forEach(w -> {
|
||||
Map<ClaimPermission, Boolean> wMap = ConfigHandler.config.globalDefaultPerms.getOrDefault(w.getRegistryKey().getValue().toString(), new HashMap<>());
|
||||
Map<ClaimPermission, GlobalType> wMap = ConfigHandler.config.globalDefaultPerms.getOrDefault(w.getRegistryKey().getValue().toString(), new HashMap<>());
|
||||
allMap.entrySet().forEach(e -> {
|
||||
if (!wMap.containsKey(e.getKey()))
|
||||
wMap.put(e.getKey(), e.getValue());
|
||||
@ -220,8 +222,8 @@ public class Config {
|
||||
ConfigHandler.config.globalDefaultPerms.remove("*");
|
||||
}
|
||||
|
||||
Map<ClaimPermission, Boolean> permMap = ConfigHandler.config.globalDefaultPerms.get(world.getRegistryKey().getValue().toString());
|
||||
return permMap == null ? null : permMap.getOrDefault(perm, null);
|
||||
Map<ClaimPermission, GlobalType> permMap = ConfigHandler.config.globalDefaultPerms.get(world.getRegistryKey().getValue().toString());
|
||||
return permMap == null ? GlobalType.NONE : permMap.getOrDefault(perm, GlobalType.NONE);
|
||||
}
|
||||
|
||||
private <V, K> Map<V, K> createHashMap(Consumer<Map<V, K>> cons) {
|
||||
@ -235,4 +237,21 @@ public class Config {
|
||||
cons.accept(map);
|
||||
return map;
|
||||
}
|
||||
|
||||
public enum GlobalType {
|
||||
|
||||
ALLTRUE,
|
||||
ALLFALSE,
|
||||
TRUE,
|
||||
FALSE,
|
||||
NONE;
|
||||
|
||||
public boolean getValue() {
|
||||
return this == ALLTRUE || this == TRUE;
|
||||
}
|
||||
|
||||
public boolean canModify() {
|
||||
return this.ordinal() > 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package io.github.flemmli97.flan.gui;
|
||||
import io.github.flemmli97.flan.api.ClaimPermission;
|
||||
import io.github.flemmli97.flan.claim.Claim;
|
||||
import io.github.flemmli97.flan.claim.PermHelper;
|
||||
import io.github.flemmli97.flan.config.Config;
|
||||
import io.github.flemmli97.flan.config.ConfigHandler;
|
||||
import io.github.flemmli97.flan.player.PlayerClaimData;
|
||||
import net.minecraft.item.ItemStack;
|
||||
@ -37,11 +38,11 @@ public class ServerScreenHelper {
|
||||
Text trans = ServerScreenHelper.coloredGuiText(pdesc, Formatting.YELLOW);
|
||||
lore.add(StringTag.of(Text.Serializer.toJson(trans)));
|
||||
}
|
||||
Boolean global = ConfigHandler.config.getGlobal(claim.getWorld(), perm);
|
||||
if (!claim.isAdminClaim() && global != null) {
|
||||
Config.GlobalType global = ConfigHandler.config.getGlobal(claim.getWorld(), perm);
|
||||
if (!claim.isAdminClaim() && !global.canModify()) {
|
||||
Text text = ServerScreenHelper.coloredGuiText(ConfigHandler.lang.screenUneditable, Formatting.DARK_RED);
|
||||
lore.add(StringTag.of(Text.Serializer.toJson(text)));
|
||||
String permFlag = global.toString();
|
||||
String permFlag = String.valueOf(global.getValue());
|
||||
Text text2 = ServerScreenHelper.coloredGuiText(String.format(ConfigHandler.lang.screenEnableText, permFlag), permFlag.equals("true") ? Formatting.GREEN : Formatting.RED);
|
||||
lore.add(StringTag.of(Text.Serializer.toJson(text2)));
|
||||
} else {
|
||||
@ -90,11 +91,11 @@ public class ServerScreenHelper {
|
||||
Text trans = ServerScreenHelper.coloredGuiText(pdesc, Formatting.YELLOW);
|
||||
lore.add(StringTag.of(Text.Serializer.toJson(trans)));
|
||||
}
|
||||
Boolean global = ConfigHandler.config.getGlobal(player.getServerWorld(), perm);
|
||||
if (global != null) {
|
||||
Config.GlobalType global = ConfigHandler.config.getGlobal(player.getServerWorld(), perm);
|
||||
if (!global.canModify()) {
|
||||
Text text = ServerScreenHelper.coloredGuiText(ConfigHandler.lang.screenUneditable, Formatting.DARK_RED);
|
||||
lore.add(StringTag.of(Text.Serializer.toJson(text)));
|
||||
String permFlag = global.toString();
|
||||
String permFlag = String.valueOf(global.getValue());
|
||||
Text text2 = ServerScreenHelper.coloredGuiText(String.format(ConfigHandler.lang.screenEnableText, permFlag), permFlag.equals("true") ? Formatting.GREEN : Formatting.RED);
|
||||
lore.add(StringTag.of(Text.Serializer.toJson(text2)));
|
||||
} else {
|
||||
|
Loading…
x
Reference in New Issue
Block a user