bunch of region improvements + new commands
All checks were successful
/ Auto-Build-App (push) Successful in 1m40s

This commit is contained in:
pietru 2024-10-29 22:06:36 +01:00
parent 9e048684c8
commit dcc35c5bec
11 changed files with 244 additions and 54 deletions

View file

@ -3,16 +3,27 @@ package net.pietru.cookie_utils.api;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.math.collision.BoundingBox;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Json;
import com.badlogic.gdx.utils.JsonValue;
import com.badlogic.gdx.utils.JsonWriter;
import finalforeach.cosmicreach.io.SaveLocation;
import net.pietru.cookie_utils.permissions.ObjectPermList;
import net.pietru.cookie_utils.permissions.PlayerAreaPerm;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import static net.pietru.cookie_utils.utils.directory_utils.get_path_string;
import static net.pietru.cookie_utils.utils.player_utils.is_plr_valid;
public class Area {
BoundingBox space = new BoundingBox();
public String name = "none";
public String parent = "";
public boolean enabled = true;
public boolean protect = false;
public ObjectPermList perms = new ObjectPermList();
public int piority = 0;
@ -37,7 +48,7 @@ public class Area {
}
public boolean get_action_bool(String action,String player_id){
if (!player_id.contains("offline"))
if (is_plr_valid(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)){
@ -48,11 +59,11 @@ public class Area {
}
public boolean is_user_type(String player_id,String type){
if (!player_id.contains("offline"))
if (is_plr_valid(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).type.equals(type);
return player_perms.get(i).type.equalsIgnoreCase(type);
}
}
return false;
@ -74,9 +85,26 @@ public class Area {
val.addChild("z2", new JsonValue(space.max.z));
val.addChild("enabled", new JsonValue(enabled));
val.addChild("protect", new JsonValue(protect));
val.addChild("parent", new JsonValue(parent));
perms.get_json(val);
val.addChild("piority", new JsonValue(piority));
return val;
}
public void save(boolean reload){
File area_file = new File(get_path_string(SaveLocation.getSaveFolder().getPath(),"regions",name));
try {
Json json = new Json();
json.setOutputType(JsonWriter.OutputType.json);
BufferedWriter writer = new BufferedWriter(new FileWriter(area_file.getPath()));
writer.write(json.prettyPrint(get_json().toString()));
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
if (reload)
Region.reload_regions();
}
}

View file

@ -44,7 +44,8 @@ public class Region {
boolean can = is_not_reloading;
if (is_not_reloading)
for (Area a : areas){
for (int i = 0; i< areas.size; i++){
Area a = areas.get(i);
if (a.enabled && a.does_intersect(g_position)){
can=a.get_action_bool(action);
break;
@ -58,7 +59,8 @@ public class Region {
boolean can = is_not_reloading;
if (is_not_reloading)
for (Area a : areas){
for (int i = 0; i< areas.size; i++){
Area a = areas.get(i);
if (a.enabled && a.does_intersect(position)){
can=a.get_action_bool(action,player_id);
break;
@ -72,7 +74,8 @@ public class Region {
boolean can = is_not_reloading;
if (is_not_reloading)
for (Area a : areas){
for (int i = 0; i< areas.size; i++){
Area a = areas.get(i);
if (a.enabled && a.does_intersect(position)){
can=a.is_user_type(player_id,type);
break;
@ -83,10 +86,9 @@ public class Region {
}
public static Area get_area_at_pos(Vector3 position){
boolean can = is_not_reloading;
if (is_not_reloading)
for (Area a : areas){
for (int i = 0; i< areas.size; i++){
Area a = areas.get(i);
if (a.enabled && a.does_intersect(position)){
return a;
}
@ -130,6 +132,7 @@ public class Region {
area.name=f.getName();
area.enabled=region.getBoolean("enabled",true);
area.protect=region.getBoolean("protect",false);
area.parent = region.getString("parent","");
if (area.parent.isEmpty()) {
@ -167,7 +170,14 @@ public class Region {
}
}
}
sort_areas();
is_not_reloading=true;
System.out.println("[REGIONS] Loaded %s areas.".replace("%s",String.valueOf(areas.size)));
}
public static void sort_areas(){
Sort sorter = Sort.instance();
sorter.sort(areas,(a,b)->{
@ -176,10 +186,6 @@ public class Region {
return pior;
return Math.toIntExact(a.size - b.size);
});
is_not_reloading=true;
System.out.println("[REGIONS] Loaded %s areas.".replace("%s",String.valueOf(areas.size)));
}
public static void reload_area_player_perms(String area_name){
@ -216,6 +222,7 @@ public class Region {
if (perm.player_id.isEmpty())
continue;
perm.filename=f.getName();
perm.type=perm_json.getString("type", "");
perm.enabled=perm_json.getBoolean("enabled", false);
@ -247,6 +254,6 @@ public class Region {
e.printStackTrace();
}
System.out.println("[REGIONS] Loaded %s permissions.".replace("%s",String.valueOf(areas.size)));
System.out.println("[AREA] Loaded %s permissions.".replace("%s",String.valueOf(areas.size)));
}
}

View file

@ -3,6 +3,7 @@ package net.pietru.cookie_utils.api;
import com.badlogic.gdx.math.Vector3;
import finalforeach.cosmicreach.accounts.Account;
import finalforeach.cosmicreach.entities.player.Player;
import finalforeach.cosmicreach.io.SaveLocation;
import finalforeach.cosmicreach.networking.NetworkIdentity;
import finalforeach.cosmicreach.networking.packets.MessagePacket;
import finalforeach.cosmicreach.networking.server.ServerSingletons;
@ -11,14 +12,16 @@ import io.netty.channel.ChannelHandlerContext;
import net.pietru.cookie_utils.permissions.Permissions;
import net.pietru.cookie_utils.setups.Setup;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import static net.pietru.cookie_utils.api.Region.reload_area_player_perms;
import static net.pietru.cookie_utils.api.Region.reload_regions;
import static net.pietru.cookie_utils.api.Region.*;
import static net.pietru.cookie_utils.permissions.Permissions.has_perm;
import static net.pietru.cookie_utils.permissions.Permissions.reload_perm_groups;
import static net.pietru.cookie_utils.utils.directory_utils.get_path_string;
import static net.pietru.cookie_utils.utils.player_utils.get_player_id;
public class TextCommands {
public static final Map<String, TriConsumer<String[], NetworkIdentity, ChannelHandlerContext>> commands = new HashMap<>();
@ -29,7 +32,7 @@ public class TextCommands {
if (!commands.containsKey(args[0]))
return;
Account account = ServerSingletons.getAccount(id);
if (!Permissions.has_perm(account.getUniqueId(),args[0]))
if (!has_perm(account.getUniqueId(),args[0]))
return;
TriConsumer<String[], NetworkIdentity, ChannelHandlerContext> cmd = commands.get(args[0]);
cmd.accept(args, id, ctx);
@ -60,6 +63,22 @@ public class TextCommands {
packet.playerUniqueId=account.getUniqueId();
packet.setupAndSend(channelHandlerContext);
});
commands.put("my_name",(args, networkIdentity, channelHandlerContext) -> {
Account account = ServerSingletons.getAccount(networkIdentity);
MessagePacket packet = new MessagePacket("Your name is "+account.getUsername());
packet.playerUniqueId=account.getUniqueId();
packet.setupAndSend(channelHandlerContext);
});
commands.put("my_area",(args, networkIdentity, channelHandlerContext) -> {
Account account = ServerSingletons.getAccount(networkIdentity);
Player plr = ServerSingletons.getPlayer(networkIdentity);
Area area = get_area_at_pos(plr.getPosition());
MessagePacket packet = new MessagePacket("You are in "+(area!=null?area.name : "no area atm..."));
packet.playerUniqueId=account.getUniqueId();
packet.setupAndSend(channelHandlerContext);
});
commands.put("setup",(args, networkIdentity, channelHandlerContext) -> {
Account account = ServerSingletons.getAccount(networkIdentity);
boolean started = false;
@ -83,6 +102,76 @@ public class TextCommands {
packet.setupAndSend(channelHandlerContext);
}
});
commands.put("region",(args, networkIdentity, channelHandlerContext) -> {
Account account = ServerSingletons.getAccount(networkIdentity);
MessagePacket packet = new MessagePacket("[Server] Usage :region area_name.json action value(optional)");
if (args.length>=3) {
String txt = args[1];
if (!txt.endsWith(".json"))
txt+=".json";
Area area = null;
for (int i = 0; i < areas.size; i++) {
if (areas.get(i).name.equals(txt)){
area=areas.get(i);
}
}
boolean a = has_perm(account.getUniqueId(), "region.*");
boolean b = has_perm(account.getUniqueId(), "region."+args[2]);
if (a && b && area!=null){
switch (args[2]){
case "enabled":
if (args.length==4){
try {
area.enabled=Boolean.parseBoolean(args[3]);
area.save(false);
} catch (Exception ignored) {}
packet = new MessagePacket("[Server] Area is now "+(area.enabled?"ENABLED":"DISABLED"));
} else
packet = new MessagePacket("[Server] Area is "+(area.enabled?"ENABLED":"DISABLED"));
break;
case "protect":
if (args.length==4){
try {
area.protect=Boolean.parseBoolean(args[3]);
area.save(false);
} catch (Exception ignored) {}
packet = new MessagePacket("[Server] Area protection is now "+(area.protect?"ENABLED":"DISABLED"));
} else
packet = new MessagePacket("[Server] Area protection is "+(area.protect?"ENABLED":"DISABLED"));
break;
case "priority", "piority":
if (args.length==4){
try {
area.piority=Integer.parseInt(args[3]);
area.save(false);
sort_areas();
} catch (Exception ignored) {}
packet = new MessagePacket("[Server] Area priority is now "+area.piority);
} else
packet = new MessagePacket("[Server] Area priority is "+area.piority);
break;
case "delete":
if (area.protect)
packet = new MessagePacket("[Server] Sorry but this area is protected...");
else {
File region = new File(get_path_string(SaveLocation.getSaveFolder().getPath(), "regions", area.name));
if (region.exists()) {
region.delete();
packet = new MessagePacket("[Server] Usage :region area_name.json ACTION value(if required)");
}
}
break;
}
} else {
packet = new MessagePacket("[Server] Insufficient permissions or wrong area...");
}
}
packet.playerUniqueId=account.getUniqueId();
packet.setupAndSend(channelHandlerContext);
});
commands.put("perm_code",(args, networkIdentity, channelHandlerContext) -> {
@ -106,13 +195,13 @@ public class TextCommands {
MessagePacket packet=null;
if (args.length==4) {
if (Objects.equals(args[1], "set")) {
boolean res = Permissions.set_group(args[2], args[3]);
packet = new MessagePacket(res ? "Group set for user..." : "Failed to set group for user...");
boolean res = Permissions.set_group(get_player_id(args[2]), args[3]);
packet = new MessagePacket(res ? "[Server] Group set for user..." : "[Server] Failed to set group for user...");
}
}
else if (args.length==3) {
if (Objects.equals(args[1], "clear")) {
Permissions.clear_group(args[2]);
Permissions.clear_group(get_player_id(args[2]));
packet = new MessagePacket("[Server] If user had perm it was cleared...");
}
}

View file

@ -0,0 +1,28 @@
package net.pietru.cookie_utils.mixins;
import com.llamalad7.mixinextras.sugar.Local;
import finalforeach.cosmicreach.accounts.Account;
import finalforeach.cosmicreach.networking.NetworkIdentity;
import finalforeach.cosmicreach.networking.packets.meta.LoginPacket;
import io.netty.channel.ChannelHandlerContext;
import net.pietru.cookie_utils.utils.player_utils;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import static net.pietru.cookie_utils.utils.player_utils.is_plr_valid;
@Mixin(LoginPacket.class)
public class LoginPacketMixin {
@Shadow
public Account account;
@Inject(method = "handle", at = @At("TAIL"))
private void event_player_data(CallbackInfo ci, @Local NetworkIdentity identity, @Local ChannelHandlerContext ctx){
if (is_plr_valid(account.getUniqueId())){
player_utils.usernames_to_ids.put(account.getUsername(), account.getUniqueId());
}
}
}

View file

@ -16,6 +16,7 @@ public class GroupPerms {
val.perms.add("gpos");
val.perms.add("my_zone");
val.perms.add("my_id");
val.perms.add("my_name");
val.perms.add("perm_code");
val.perms.add("perm_clear");
return val;

View file

@ -16,6 +16,7 @@ import java.util.Objects;
import java.util.Random;
import static net.pietru.cookie_utils.utils.directory_utils.get_path_string;
import static net.pietru.cookie_utils.utils.player_utils.is_plr_valid;
public class Permissions {
static Json json = new Json();
@ -110,11 +111,11 @@ public class Permissions {
if (player_group.containsKey(playerId))
if (groupPermsMap.containsKey(player_group.get(playerId)))
group=groupPermsMap.get(player_group.get(playerId));
return has_user_special_perm(playerId) || group.has_perm(perm);
return has_user_special_perm(playerId) || group.has_perm(perm) || group.has_perm("*");
}
public static boolean set_group(String playerId,String group){
if (!groupPermsMap.containsKey(group))
if (!groupPermsMap.containsKey(group) || !is_plr_valid(playerId))
return false;
player_group.put(playerId,group);
reload_player_groups(false);

View file

@ -3,6 +3,7 @@ package net.pietru.cookie_utils.permissions;
import com.badlogic.gdx.utils.JsonValue;
public class PlayerAreaPerm {
public String filename = "";
public String player_id = "";
public String type = "";
public boolean enabled = true;

View file

@ -2,6 +2,7 @@ package net.pietru.cookie_utils.setups;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Json;
import com.badlogic.gdx.utils.JsonValue;
import com.badlogic.gdx.utils.JsonWriter;
import finalforeach.cosmicreach.GameSingletons;
import finalforeach.cosmicreach.accounts.Account;
@ -20,6 +21,7 @@ import java.io.FileWriter;
import static net.pietru.cookie_utils.api.Region.get_area_at_pos;
import static net.pietru.cookie_utils.utils.directory_utils.get_path_string;
import static net.pietru.cookie_utils.utils.player_utils.*;
public class areaPermSetup extends Setup {
String mode="";
@ -41,6 +43,12 @@ public class areaPermSetup extends Setup {
steps.add(()->{});
steps.add(()->{});
steps.add(()->{
if (!is_plr_valid(usr_id) || !has_player(usr_id,area)) {
step_hints.add("Permission setup failed - invalid player id");
isActive = false;
return;
}
for (int i = 0; i < area.player_perms.size; i++) {
PlayerAreaPerm perm = area.player_perms.get(i);
if (perm.player_id.equals(usr_id)) {
@ -49,15 +57,11 @@ public class areaPermSetup extends Setup {
}
}
if (plr_perm==null) {
if (ServerSingletons.getAccountByUniqueId(usr_id)==null) {
step_hints.add("Permission setup failed - invalid player id");
isActive = false;
return;
}
plr_perm = new PlayerAreaPerm();
plr_perm.filename=usr_id.replace(":","_")+".json";
plr_perm.player_id=usr_id;
ObjectPermList.get_default_perms(plr_perm.perms);
area.player_perms.add(plr_perm);
}
step_hints.add("Permission setup complete");
@ -103,16 +107,15 @@ public class areaPermSetup extends Setup {
@Override
public void run_setup_finish() {
isActive=false;
File area_file = new File(get_path_string(SaveLocation.getSaveFolder().getPath(),"region_perms",area.name,usr_id+".json"));
File perm_file = new File(get_path_string(SaveLocation.getSaveFolder().getPath(),"region_perms",area.name,plr_perm.filename));
try {
if (!area_file.exists()) {
Json json = new Json();
JsonValue val = plr_perm.get_json();
json.setOutputType(JsonWriter.OutputType.json);
BufferedWriter writer = new BufferedWriter(new FileWriter(area_file.getPath()));
writer.write(json.prettyPrint(plr_perm.get_json().toString()));
BufferedWriter writer = new BufferedWriter(new FileWriter(perm_file.getPath()));
writer.write(json.prettyPrint(val.toString()));
writer.close();
Region.reload_regions();
}
Region.load_area_player_perms(area);
} catch (Exception e) {
e.printStackTrace();
}
@ -126,11 +129,11 @@ public class areaPermSetup extends Setup {
@Override
public boolean set_setup_text(String value) {
if (step==1)
mode=value;
mode=value.toLowerCase();
if (step==2)
selected_perm=value;
selected_perm=value.toLowerCase();
if (step==3)
usr_id=value;
usr_id=get_player_id(value);
return step==0 || step==1 || step==2 || step==3;
}

View file

@ -53,19 +53,12 @@ public class regionSetup extends Setup {
@Override
public void run_setup_finish() {
isActive=false;
File area_file = new File(get_path_string(SaveLocation.getSaveFolder().getPath(),"regions",name));
try {
if (!area_file.exists()) {
Json json = new Json();
json.setOutputType(JsonWriter.OutputType.json);
BufferedWriter writer = new BufferedWriter(new FileWriter(area_file.getPath()));
writer.write(json.prettyPrint(area.get_json().toString()));
writer.close();
Region.reload_regions();
}
} catch (Exception e) {
e.printStackTrace();
}
area.save(false);
Region.areas.add(area);
if (area.parent.isEmpty())
Region.load_area_player_perms(area);
}
@Override

View file

@ -0,0 +1,38 @@
package net.pietru.cookie_utils.utils;
import net.pietru.cookie_utils.api.Area;
import net.pietru.cookie_utils.permissions.PlayerAreaPerm;
import java.util.HashMap;
import java.util.Map;
public class player_utils {
public static Map<String,String> usernames_to_ids = new HashMap<>();
public static boolean has_player(String value){
return usernames_to_ids.containsKey(value) || usernames_to_ids.containsValue(value);
}
public static boolean has_player(String value, Area area){
String plr_id = get_player_id(value);
for (int i = 0; i < area.player_perms.size; i++) {
PlayerAreaPerm perm = area.player_perms.get(i);
if (perm.player_id.equals(plr_id)) {
return true;
}
}
return usernames_to_ids.containsKey(value) || usernames_to_ids.containsValue(value);
}
public static boolean is_plr_valid(String player_id){
return !player_id.contains("offline");
}
public static String get_player_id(String value){
if (usernames_to_ids.containsKey(value))
return usernames_to_ids.get(value);
if (usernames_to_ids.containsValue(value))
return value;
return "";
}
}

View file

@ -9,6 +9,7 @@
"BlockPlaceMixin",
"InteractBlockMixin",
"MessagePacketMixin",
"LoginPacketMixin",
"PlayerPositionMixin",
"ServerSingletonsMixin"
],