add one region reload, example perm file for areas, fixes
All checks were successful
/ Auto-Build-App (push) Successful in 1m37s

This commit is contained in:
pietru 2024-10-26 16:56:25 +02:00
parent 8f9957921b
commit 87894d545e
5 changed files with 76 additions and 37 deletions

View file

@ -32,12 +32,13 @@ public class Area {
}
public boolean get_action_bool(String action,String player_id){
for (int i = 0; i < player_perms.size; i++) {
PlayerAreaPerm p = player_perms.get(i);
if (p.enabled && !p.player_id.isEmpty() && p.player_id.equals(player_id)){
return player_perms.get(i).get_action_bool(action);
if (!player_id.contains("offline"))
for (int i = 0; i < player_perms.size; i++) {
PlayerAreaPerm p = player_perms.get(i);
if (p.enabled && !p.player_id.isEmpty() && p.player_id.equals(player_id)){
return player_perms.get(i).get_action_bool(action);
}
}
}
return perms.get_bool_perm(action,false);
}

View file

@ -25,14 +25,13 @@ public class ObjectPermList {
return bool_perms.getOrDefault(key,def_val);
}
public static Json get_default_perms(){
Json val = new Json();
val.writeValue("canPlace", false);
val.writeValue("canBreak", false);
val.writeValue("canInteract", false);
public static JsonValue get_default_perms(JsonValue val){
val.addChild("canPlace", new JsonValue(false));
val.addChild("canBreak", new JsonValue(false));
val.addChild("canInteract", new JsonValue(false));
val.writeValue("canExplode", false);
val.writeValue("canEnter", true);
val.addChild("canExplode", new JsonValue(false));
val.addChild("canEnter", new JsonValue(true));
return val;
}
}

View file

@ -1,6 +1,7 @@
package net.pietru.cookie_utils.api;
import com.badlogic.gdx.utils.Json;
import com.badlogic.gdx.utils.JsonValue;
public class PlayerAreaPerm {
public String player_id = "";
@ -11,10 +12,11 @@ public class PlayerAreaPerm {
return perms.get_bool_perm(action,false);
}
public static Json get_default_perms(){
Json val = ObjectPermList.get_default_perms();
val.writeValue("player_id", "");
val.writeValue("enabled", true);
public static JsonValue get_default_perms(){
JsonValue val = new JsonValue(JsonValue.ValueType.object);
val.addChild("player_id", new JsonValue(""));
val.addChild("enabled", new JsonValue(true));
ObjectPermList.get_default_perms(val);
return val;
}
}

View file

@ -113,7 +113,7 @@ public class Region {
perms.set_bool_perm("interact", region.getBoolean("canInteract", false));
perms.set_bool_perm("explode", region.getBoolean("canExplode", false));
perms.set_bool_perm("move", region.getBoolean("canEnter", false));
perms.set_bool_perm("move", region.getBoolean("canEnter", true));
}
@ -121,6 +121,9 @@ public class Region {
areas.add(area);
if (area.parent.isEmpty())
load_area_player_perms(area);
System.out.println("[REGIONS] Loaded region file \"%s\"".replace("%s", area.name));
}
}
@ -132,6 +135,7 @@ public class Region {
Area parent_area = areas.get(j);
if (parent_area.name.equals(area.parent)) {
area.perms = parent_area.perms;
area.player_perms = parent_area.player_perms;
break;
}
}
@ -146,53 +150,70 @@ public class Region {
System.out.println("[REGIONS] Loaded %s areas.".replace("%s",String.valueOf(areas.size)));
}
public static void reload_regions(Area area){
public static void reload_area_player_perms(String area_name){
for (int i = 0; i < areas.size; i++) {
Area area = areas.get(i);
if (area.name.equals(area_name)) {
load_area_player_perms(area);
break;
}
}
}
public static void load_area_player_perms(Area area){
area.player_perms.clear();
File perms_folder = new File(get_path_string(SaveLocation.getSaveFolder().getPath(),"region_perms", area.name));
if (!perms_folder.exists()) {
perms_folder.mkdirs();
try {
Json def = PlayerAreaPerm.get_default_perms();
BufferedWriter writer = new BufferedWriter(new FileWriter(get_path_string(perms_folder.getPath(),"example.json")));
writer.write(def.prettyPrint(JsonWriter.OutputType.json));
} catch (Exception ignored) {}
return;
}
File[] perms = perms_folder.listFiles(((dir, name) -> name.endsWith(".json")));
if (perms!=null) {
System.out.println("[AREA] Trying to load %s perms.".replace("%s",String.valueOf(perms.length)));
for (File f : perms) {
JsonValue region;
JsonValue perm_json;
try {
region=jsonReader.parse(Gdx.files.absolute(f.getPath()));
perm_json=jsonReader.parse(Gdx.files.absolute(f.getPath()));
} catch (Exception ignored){
System.out.println("[AREA] Perms file \"%s\" had encountered exception while loading...".replace("%s", f.getPath()));
continue;
}
PlayerAreaPerm perm = new PlayerAreaPerm();
perm.player_id=region.getString("player_id", "");
perm.player_id=perm_json.getString("player_id", "");
if (perm.player_id.isEmpty())
continue;
perm.enabled=region.getBoolean("enabled", false);
perm.enabled=perm_json.getBoolean("enabled", false);
ObjectPermList player_perms = perm.perms;
player_perms.set_bool_perm("place", region.getBoolean("canPlace", false));
player_perms.set_bool_perm("break", region.getBoolean("canBreak", false));
player_perms.set_bool_perm("interact", region.getBoolean("canInteract", false));
player_perms.set_bool_perm("place", perm_json.getBoolean("canPlace", false));
player_perms.set_bool_perm("break", perm_json.getBoolean("canBreak", false));
player_perms.set_bool_perm("interact", perm_json.getBoolean("canInteract", false));
player_perms.set_bool_perm("explode", region.getBoolean("canExplode", false));
player_perms.set_bool_perm("move", region.getBoolean("canEnter", false));
player_perms.set_bool_perm("explode", perm_json.getBoolean("canExplode", false));
player_perms.set_bool_perm("move", perm_json.getBoolean("canEnter", false));
area.player_perms.add(perm);
}
}
try {
Json json = new Json();
json.setOutputType(JsonWriter.OutputType.json);
JsonValue def = PlayerAreaPerm.get_default_perms();
String txt = def.toString();
BufferedWriter writer = new BufferedWriter(new FileWriter(get_path_string(perms_folder.getPath(),"example.json")));
writer.write(json.prettyPrint(txt));
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("[REGIONS] Loaded %s permissions.".replace("%s",String.valueOf(areas.size)));
}
}

View file

@ -12,6 +12,7 @@ import io.netty.channel.ChannelHandlerContext;
import java.util.HashMap;
import java.util.Map;
import static net.pietru.cookie_utils.api.Region.reload_area_player_perms;
import static net.pietru.cookie_utils.api.Region.reload_regions;
public class TextCommands {
@ -40,15 +41,30 @@ public class TextCommands {
Player player = ServerSingletons.getPlayer(networkIdentity);
Account account = ServerSingletons.getAccount(networkIdentity);
Vector3 pos = player.getPosition();
MessagePacket packet = new MessagePacket("You are in "+player.zoneId);
packet.playerUniqueId=account.getUniqueId();
packet.setupAndSend(channelHandlerContext);
});
commands.put("reload_regions",(args, networkIdentity, channelHandlerContext) -> {
reload_regions();
commands.put("my_id",(args, networkIdentity, channelHandlerContext) -> {
Account account = ServerSingletons.getAccount(networkIdentity);
MessagePacket packet = new MessagePacket("Your id is "+account.getUniqueId());
packet.playerUniqueId=account.getUniqueId();
packet.setupAndSend(channelHandlerContext);
});
commands.put("reload_regions",(args, networkIdentity, channelHandlerContext) -> {
MessagePacket packet = null;
if (args.length==1) {
reload_regions();
packet = new MessagePacket("Reloaded regions...");
}
if (args.length==2) {
reload_area_player_perms(args[1]);
packet = new MessagePacket("Reloaded %s area...".replace("%s", args[1]));
}
if (packet==null)
return;
Account account = ServerSingletons.getAccount(networkIdentity);
MessagePacket packet = new MessagePacket("Reloaded regions...");
packet.playerUniqueId=account.getUniqueId();
packet.setupAndSend(channelHandlerContext);
});