pre10 update - added player perms
All checks were successful
/ Auto-Build-App (push) Successful in 1m36s

This commit is contained in:
pietru 2024-10-26 15:29:52 +02:00
parent 44003f57c2
commit 8f9957921b
9 changed files with 181 additions and 32 deletions

View file

@ -9,10 +9,10 @@ group=net.pietru
id=cookie_utils id=cookie_utils
# Dependency Versions # Dependency Versions
cosmic_reach_version=0.3.2-pre9 cosmic_reach_version=0.3.2-pre10
# If unspecified, will use the version above # If unspecified, will use the version above
cosmic_reach_server_version=0.3.2-pre9 cosmic_reach_server_version=0.3.2-pre10
cosmic_quilt_version=2.3.0 cosmic_quilt_version=2.3.1
#2.2.0 #2.2.0
# modmenu_version=1.0.7 # modmenu_version=1.0.7

View file

@ -2,19 +2,18 @@ package net.pietru.cookie_utils.api;
import com.badlogic.gdx.math.Vector3; import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.math.collision.BoundingBox; import com.badlogic.gdx.math.collision.BoundingBox;
import com.badlogic.gdx.utils.Array;
public class Area { public class Area {
BoundingBox space = new BoundingBox(); BoundingBox space = new BoundingBox();
public String name = "none";
public String parent = "";
public boolean enabled = true; public boolean enabled = true;
public ObjectPermList perms = new ObjectPermList();
public boolean canPlace = false;
public boolean canBreak = false;
public boolean canInteract = false;
public boolean canExplode = false;
public boolean canEnter = true;
public int piority = 0; public int piority = 0;
public Array<PlayerAreaPerm> player_perms = new Array<>();
public BoundingBox getBox() { public BoundingBox getBox() {
return space; return space;
@ -32,15 +31,18 @@ public class Area {
return space.contains(c); return space.contains(c);
} }
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);
}
}
return perms.get_bool_perm(action,false);
}
public boolean get_action_bool(String action){ public boolean get_action_bool(String action){
return switch (action) { return perms.get_bool_perm(action,false);
case "place" -> canPlace;
case "break" -> canBreak;
case "interact" -> canInteract;
case "explode" -> canExplode;
case "move" -> canEnter;
default -> false;
};
} }
} }

View file

@ -0,0 +1,38 @@
package net.pietru.cookie_utils.api;
import com.badlogic.gdx.utils.Json;
import com.badlogic.gdx.utils.JsonValue;
import java.util.HashMap;
import java.util.Map;
public class ObjectPermList {
public Map<String,Boolean> bool_perms = new HashMap<>();
// switch (action) {
// case "place" -> canPlace;
// case "break" -> canBreak;
// case "interact" -> canInteract;
// case "explode" -> canExplode;
// case "move" -> canEnter;
// default -> false;
// };
public void set_bool_perm(String key, boolean val){
bool_perms.put(key,val);
}
public boolean get_bool_perm(String key, boolean def_val){
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);
val.writeValue("canExplode", false);
val.writeValue("canEnter", true);
return val;
}
}

View file

@ -0,0 +1,20 @@
package net.pietru.cookie_utils.api;
import com.badlogic.gdx.utils.Json;
public class PlayerAreaPerm {
public String player_id = "";
public boolean enabled = true;
public ObjectPermList perms = new ObjectPermList();
public boolean get_action_bool(String action){
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);
return val;
}
}

View file

@ -2,15 +2,14 @@ package net.pietru.cookie_utils.api;
import com.badlogic.gdx.Gdx; import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.Vector3; import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Array; import com.badlogic.gdx.utils.*;
import com.badlogic.gdx.utils.JsonReader;
import com.badlogic.gdx.utils.JsonValue;
import com.badlogic.gdx.utils.Sort;
import finalforeach.cosmicreach.blocks.BlockPosition; import finalforeach.cosmicreach.blocks.BlockPosition;
import finalforeach.cosmicreach.io.SaveLocation; import finalforeach.cosmicreach.io.SaveLocation;
import finalforeach.cosmicreach.world.Zone; import finalforeach.cosmicreach.world.Zone;
import java.io.BufferedWriter;
import java.io.File; import java.io.File;
import java.io.FileWriter;
public class Region { public class Region {
@ -19,6 +18,22 @@ public class Region {
public static boolean is_not_reloading = true; public static boolean is_not_reloading = true;
public static boolean can_edit_block(BlockPosition position,String action,String player_id){
Zone z = position.getZone();
Vector3 g_position = new Vector3(position.getGlobalX(),position.getGlobalY(),position.getGlobalZ());
boolean can = is_not_reloading;
if (is_not_reloading)
for (Area a : areas){
if (a.enabled && a.does_intersect(g_position)){
can=a.get_action_bool(action,player_id);
break;
}
}
return can;
}
public static boolean can_edit_block(BlockPosition position,String action){ public static boolean can_edit_block(BlockPosition position,String action){
Zone z = position.getZone(); Zone z = position.getZone();
Vector3 g_position = new Vector3(position.getGlobalX(),position.getGlobalY(),position.getGlobalZ()); Vector3 g_position = new Vector3(position.getGlobalX(),position.getGlobalY(),position.getGlobalZ());
@ -35,13 +50,13 @@ public class Region {
return can; return can;
} }
public static boolean can_enter_position(Vector3 position,String action){ public static boolean can_enter_position(Vector3 position,String action,String player_id){
boolean can = is_not_reloading; boolean can = is_not_reloading;
if (is_not_reloading) if (is_not_reloading)
for (Area a : areas){ for (Area a : areas){
if (a.enabled && a.does_intersect(position)){ if (a.enabled && a.does_intersect(position)){
can=a.get_action_bool(action); can=a.get_action_bool(action,player_id);
break; break;
} }
} }
@ -49,6 +64,10 @@ public class Region {
return can; return can;
} }
public static String get_path_string(String ...path_parts){
return String.join(File.separator, path_parts);
}
public static void reload_regions(){ public static void reload_regions(){
is_not_reloading=false; is_not_reloading=false;
@ -82,19 +101,39 @@ public class Region {
area.setCorners(a, b); area.setCorners(a, b);
area.name=f.getName();
area.enabled=region.getBoolean("enabled",true); area.enabled=region.getBoolean("enabled",true);
area.canPlace=region.getBoolean("canPlace",false); area.parent = region.getString("parent","");
area.canBreak=region.getBoolean("canBreak",false); if (area.parent.isEmpty()) {
area.canInteract=region.getBoolean("canInteract",false); ObjectPermList perms = area.perms;
area.canExplode=region.getBoolean("canExplode",false); perms.set_bool_perm("place", region.getBoolean("canPlace", false));
area.canEnter=region.getBoolean("canEnter",true); perms.set_bool_perm("break", region.getBoolean("canBreak", false));
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));
}
area.piority=region.getInt("piority",0); area.piority=region.getInt("piority",0);
areas.add(area); areas.add(area);
System.out.println("[REGIONS] Loaded region file \"%s\"".replace("%s", area.name));
}
}
for (int i = 0; i < areas.size; i++) {
Area area = areas.get(i);
if (area.parent.isEmpty())
continue;
for (int j = 0; j < areas.size; j++) {
Area parent_area = areas.get(j);
if (parent_area.name.equals(area.parent)) {
area.perms = parent_area.perms;
break;
}
} }
} }
@ -106,4 +145,54 @@ public class Region {
System.out.println("[REGIONS] Loaded %s areas.".replace("%s",String.valueOf(areas.size))); System.out.println("[REGIONS] Loaded %s areas.".replace("%s",String.valueOf(areas.size)));
} }
public static void reload_regions(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;
try {
region=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", "");
if (perm.player_id.isEmpty())
continue;
perm.enabled=region.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("explode", region.getBoolean("canExplode", false));
player_perms.set_bool_perm("move", region.getBoolean("canEnter", false));
area.player_perms.add(perm);
}
}
System.out.println("[REGIONS] Loaded %s permissions.".replace("%s",String.valueOf(areas.size)));
}
} }

View file

@ -34,7 +34,7 @@ public class BlockBreakMixin {
private void event_block_break(CallbackInfo ci, @Local NetworkIdentity identity, @Local ChannelHandlerContext ctx){ private void event_block_break(CallbackInfo ci, @Local NetworkIdentity identity, @Local ChannelHandlerContext ctx){
if (identity.getSide() != NetworkSide.CLIENT) { if (identity.getSide() != NetworkSide.CLIENT) {
Account account = ServerSingletons.getAccount(identity); Account account = ServerSingletons.getAccount(identity);
if (!Permissions.has_user_special_perm(account.getUniqueId()) && !can_edit_block(blockPos,"break")) { if (!Permissions.has_user_special_perm(account.getUniqueId()) && !can_edit_block(blockPos,"break",account.getUniqueId())) {
ci.cancel(); ci.cancel();
MessagePacket packet = new MessagePacket("[Server] " + (is_not_reloading ? "Sorry, but this area is under protection." : "Sorry but you can't do this action right now. [Config Reload In Progress]")); MessagePacket packet = new MessagePacket("[Server] " + (is_not_reloading ? "Sorry, but this area is under protection." : "Sorry but you can't do this action right now. [Config Reload In Progress]"));

View file

@ -34,7 +34,7 @@ public class BlockPlaceMixin {
private void event_block_place(CallbackInfo ci, @Local NetworkIdentity identity, @Local ChannelHandlerContext ctx){ private void event_block_place(CallbackInfo ci, @Local NetworkIdentity identity, @Local ChannelHandlerContext ctx){
if (identity.getSide() != NetworkSide.CLIENT) { if (identity.getSide() != NetworkSide.CLIENT) {
Account account = ServerSingletons.getAccount(identity); Account account = ServerSingletons.getAccount(identity);
if (!Permissions.has_user_special_perm(account.getUniqueId()) && !can_edit_block(blockPos,"place")) { if (!Permissions.has_user_special_perm(account.getUniqueId()) && !can_edit_block(blockPos,"place",account.getUniqueId())) {
ci.cancel(); ci.cancel();
MessagePacket packet = new MessagePacket("[Server] " + (is_not_reloading ? "Sorry, but this area is under protection." : "Sorry but you can't do this action right now. [Config Reload In Progress]")); MessagePacket packet = new MessagePacket("[Server] " + (is_not_reloading ? "Sorry, but this area is under protection." : "Sorry but you can't do this action right now. [Config Reload In Progress]"));

View file

@ -34,7 +34,7 @@ public class InteractBlockMixin {
private void event_block_interact(CallbackInfo ci, @Local NetworkIdentity identity, @Local ChannelHandlerContext ctx){ private void event_block_interact(CallbackInfo ci, @Local NetworkIdentity identity, @Local ChannelHandlerContext ctx){
if (identity.getSide() != NetworkSide.CLIENT) { if (identity.getSide() != NetworkSide.CLIENT) {
Account account = ServerSingletons.getAccount(identity); Account account = ServerSingletons.getAccount(identity);
if (!Permissions.has_user_special_perm(account.getUniqueId()) && !can_edit_block(blockPos,"interact")) { if (!Permissions.has_user_special_perm(account.getUniqueId()) && !can_edit_block(blockPos,"interact",account.getUniqueId())) {
ci.cancel(); ci.cancel();
MessagePacket packet = new MessagePacket("[Server] " + (is_not_reloading ? "Sorry, but this area is under protection." : "Sorry but you can't do this action right now. [Config Reload In Progress]")); MessagePacket packet = new MessagePacket("[Server] " + (is_not_reloading ? "Sorry, but this area is under protection." : "Sorry but you can't do this action right now. [Config Reload In Progress]"));

View file

@ -37,7 +37,7 @@ public class PlayerPositionMixin {
private void event_player_move(CallbackInfo ci, @Local NetworkIdentity identity, @Local ChannelHandlerContext ctx){ private void event_player_move(CallbackInfo ci, @Local NetworkIdentity identity, @Local ChannelHandlerContext ctx){
if (identity.getSide() != NetworkSide.CLIENT) { if (identity.getSide() != NetworkSide.CLIENT) {
Account account = ServerSingletons.getAccount(identity); Account account = ServerSingletons.getAccount(identity);
if (!Permissions.has_user_special_perm(account.getUniqueId()) && !can_enter_position(position,"move")) { if (!Permissions.has_user_special_perm(account.getUniqueId()) && !can_enter_position(position,"move",playerUniqueId)) {
ci.cancel(); ci.cancel();
Player player = ServerSingletons.getPlayer(identity); Player player = ServerSingletons.getPlayer(identity);