This commit is contained in:
parent
b22dcc6d50
commit
9e048684c8
10 changed files with 242 additions and 6 deletions
|
@ -3,6 +3,7 @@ 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;
|
import com.badlogic.gdx.utils.Array;
|
||||||
|
import com.badlogic.gdx.utils.JsonValue;
|
||||||
import net.pietru.cookie_utils.permissions.ObjectPermList;
|
import net.pietru.cookie_utils.permissions.ObjectPermList;
|
||||||
import net.pietru.cookie_utils.permissions.PlayerAreaPerm;
|
import net.pietru.cookie_utils.permissions.PlayerAreaPerm;
|
||||||
|
|
||||||
|
@ -46,8 +47,36 @@ public class Area {
|
||||||
return perms.get_bool_perm(action,false);
|
return perms.get_bool_perm(action,false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean is_user_type(String player_id,String type){
|
||||||
|
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).type.equals(type);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
public boolean get_action_bool(String action){
|
public boolean get_action_bool(String action){
|
||||||
return perms.get_bool_perm(action,false);
|
return perms.get_bool_perm(action,false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public JsonValue get_json(){
|
||||||
|
JsonValue val = new JsonValue(JsonValue.ValueType.object);
|
||||||
|
|
||||||
|
val.addChild("x1", new JsonValue(space.min.x));
|
||||||
|
val.addChild("y1", new JsonValue(space.min.y));
|
||||||
|
val.addChild("z1", new JsonValue(space.min.z));
|
||||||
|
|
||||||
|
val.addChild("x2", new JsonValue(space.max.x));
|
||||||
|
val.addChild("y2", new JsonValue(space.max.y));
|
||||||
|
val.addChild("z2", new JsonValue(space.max.z));
|
||||||
|
|
||||||
|
val.addChild("enabled", new JsonValue(enabled));
|
||||||
|
perms.get_json(val);
|
||||||
|
val.addChild("piority", new JsonValue(piority));
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -68,6 +68,33 @@ public class Region {
|
||||||
return can;
|
return can;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static boolean is_area_user_type(Vector3 position,String player_id,String type){
|
||||||
|
boolean can = is_not_reloading;
|
||||||
|
|
||||||
|
if (is_not_reloading)
|
||||||
|
for (Area a : areas){
|
||||||
|
if (a.enabled && a.does_intersect(position)){
|
||||||
|
can=a.is_user_type(player_id,type);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return can;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Area get_area_at_pos(Vector3 position){
|
||||||
|
boolean can = is_not_reloading;
|
||||||
|
|
||||||
|
if (is_not_reloading)
|
||||||
|
for (Area a : areas){
|
||||||
|
if (a.enabled && a.does_intersect(position)){
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
public static void reload_regions(){
|
public static void reload_regions(){
|
||||||
is_not_reloading=false;
|
is_not_reloading=false;
|
||||||
|
|
||||||
|
@ -189,6 +216,7 @@ public class Region {
|
||||||
if (perm.player_id.isEmpty())
|
if (perm.player_id.isEmpty())
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
perm.type=perm_json.getString("type", "");
|
||||||
perm.enabled=perm_json.getBoolean("enabled", false);
|
perm.enabled=perm_json.getBoolean("enabled", false);
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -64,8 +64,9 @@ public class TextCommands {
|
||||||
Account account = ServerSingletons.getAccount(networkIdentity);
|
Account account = ServerSingletons.getAccount(networkIdentity);
|
||||||
boolean started = false;
|
boolean started = false;
|
||||||
if (args.length==2) {
|
if (args.length==2) {
|
||||||
if (has_perm(account.getUniqueId(), Setup.get_perm(args[1])) ||
|
boolean a = has_perm(account.getUniqueId(), Setup.get_perm(args[1]));
|
||||||
has_perm(account.getUniqueId(), "setup.*"))
|
boolean b = has_perm(account.getUniqueId(), "setup.*");
|
||||||
|
if (a || b)
|
||||||
started = Setup.start(account.getUniqueId(), args[1]);
|
started = Setup.start(account.getUniqueId(), args[1]);
|
||||||
if (args[1].equals("cancel"))
|
if (args[1].equals("cancel"))
|
||||||
Setup.cancel(account.getUniqueId());
|
Setup.cancel(account.getUniqueId());
|
||||||
|
|
|
@ -1,9 +1,11 @@
|
||||||
package net.pietru.cookie_utils.mixins;
|
package net.pietru.cookie_utils.mixins;
|
||||||
|
|
||||||
import com.llamalad7.mixinextras.sugar.Local;
|
import com.llamalad7.mixinextras.sugar.Local;
|
||||||
|
import finalforeach.cosmicreach.accounts.Account;
|
||||||
import finalforeach.cosmicreach.networking.NetworkIdentity;
|
import finalforeach.cosmicreach.networking.NetworkIdentity;
|
||||||
import finalforeach.cosmicreach.networking.NetworkSide;
|
import finalforeach.cosmicreach.networking.NetworkSide;
|
||||||
import finalforeach.cosmicreach.networking.packets.MessagePacket;
|
import finalforeach.cosmicreach.networking.packets.MessagePacket;
|
||||||
|
import finalforeach.cosmicreach.networking.server.ServerSingletons;
|
||||||
import io.netty.channel.ChannelHandlerContext;
|
import io.netty.channel.ChannelHandlerContext;
|
||||||
import net.pietru.cookie_utils.api.TextCommands;
|
import net.pietru.cookie_utils.api.TextCommands;
|
||||||
import net.pietru.cookie_utils.setups.Setup;
|
import net.pietru.cookie_utils.setups.Setup;
|
||||||
|
@ -22,20 +24,21 @@ public class MessagePacketMixin {
|
||||||
@Inject(method = "handle", at = @At("HEAD"), cancellable = true)
|
@Inject(method = "handle", at = @At("HEAD"), cancellable = true)
|
||||||
private void event_on_message(CallbackInfo ci, @Local NetworkIdentity identity, @Local ChannelHandlerContext ctx){
|
private void event_on_message(CallbackInfo ci, @Local NetworkIdentity identity, @Local ChannelHandlerContext ctx){
|
||||||
if (identity.getSide() != NetworkSide.CLIENT) {
|
if (identity.getSide() != NetworkSide.CLIENT) {
|
||||||
|
Account account = ServerSingletons.getAccount(identity);
|
||||||
if (message.startsWith(":")) {
|
if (message.startsWith(":")) {
|
||||||
String cmd = message.replaceFirst(":","");
|
String cmd = message.replaceFirst(":","");
|
||||||
String[] data = cmd.split(" ");
|
String[] data = cmd.split(" ");
|
||||||
TextCommands.run(data,identity,ctx);
|
TextCommands.run(data,identity,ctx);
|
||||||
ci.cancel();
|
ci.cancel();
|
||||||
}else if (Setup.setups.containsKey(playerUniqueId)){
|
} else if (Setup.setups.containsKey(account.getUniqueId())){
|
||||||
Setup setup = Setup.setups.get(playerUniqueId);
|
Setup setup = Setup.setups.get(account.getUniqueId());
|
||||||
if (setup.set_setup_text(message.replace(" ","_"))) {
|
if (setup.set_setup_text(message.replace(" ","_"))) {
|
||||||
ci.cancel();
|
ci.cancel();
|
||||||
|
|
||||||
setup.run_step();
|
setup.run_step();
|
||||||
|
|
||||||
MessagePacket packet = new MessagePacket("[Server] " + setup.get_step_hint());
|
MessagePacket packet = new MessagePacket("[Server] " + setup.get_step_hint());
|
||||||
packet.playerUniqueId = playerUniqueId;
|
packet.playerUniqueId = account.getUniqueId();
|
||||||
packet.setupAndSend(ctx);
|
packet.setupAndSend(ctx);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -24,6 +24,11 @@ public class ObjectPermList {
|
||||||
return bool_perms.getOrDefault(key,def_val);
|
return bool_perms.getOrDefault(key,def_val);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void get_json(JsonValue val){
|
||||||
|
for (String key : bool_perms.keySet())
|
||||||
|
val.addChild(key, new JsonValue(bool_perms.get(key)));
|
||||||
|
}
|
||||||
|
|
||||||
public static void get_default_perms(JsonValue val){
|
public static void get_default_perms(JsonValue val){
|
||||||
val.addChild("canPlace", new JsonValue(false));
|
val.addChild("canPlace", new JsonValue(false));
|
||||||
val.addChild("canBreak", new JsonValue(false));
|
val.addChild("canBreak", new JsonValue(false));
|
||||||
|
|
|
@ -4,6 +4,7 @@ import com.badlogic.gdx.utils.JsonValue;
|
||||||
|
|
||||||
public class PlayerAreaPerm {
|
public class PlayerAreaPerm {
|
||||||
public String player_id = "";
|
public String player_id = "";
|
||||||
|
public String type = "";
|
||||||
public boolean enabled = true;
|
public boolean enabled = true;
|
||||||
public ObjectPermList perms = new ObjectPermList();
|
public ObjectPermList perms = new ObjectPermList();
|
||||||
|
|
||||||
|
@ -11,9 +12,20 @@ public class PlayerAreaPerm {
|
||||||
return perms.get_bool_perm(action,false);
|
return perms.get_bool_perm(action,false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public JsonValue get_json(){
|
||||||
|
JsonValue val = new JsonValue(JsonValue.ValueType.object);
|
||||||
|
|
||||||
|
val.addChild("player_id", new JsonValue(player_id));
|
||||||
|
val.addChild("type", new JsonValue(type));
|
||||||
|
val.addChild("enabled", new JsonValue(enabled));
|
||||||
|
perms.get_json(val);
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
|
||||||
public static JsonValue get_default_perms(){
|
public static JsonValue get_default_perms(){
|
||||||
JsonValue val = new JsonValue(JsonValue.ValueType.object);
|
JsonValue val = new JsonValue(JsonValue.ValueType.object);
|
||||||
val.addChild("player_id", new JsonValue(""));
|
val.addChild("player_id", new JsonValue(""));
|
||||||
|
val.addChild("type", new JsonValue(""));
|
||||||
val.addChild("enabled", new JsonValue(true));
|
val.addChild("enabled", new JsonValue(true));
|
||||||
ObjectPermList.get_default_perms(val);
|
ObjectPermList.get_default_perms(val);
|
||||||
return val;
|
return val;
|
||||||
|
|
|
@ -17,6 +17,9 @@ public abstract class Setup {
|
||||||
Setup setup = setupCreator.get(setupId);
|
Setup setup = setupCreator.get(setupId);
|
||||||
if (setup==null)
|
if (setup==null)
|
||||||
return false;
|
return false;
|
||||||
|
if (!setup.can_start_setup(playerId))
|
||||||
|
return false;
|
||||||
|
setup.creatorId=playerId;
|
||||||
setups.put(playerId,setup);
|
setups.put(playerId,setup);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -41,10 +44,15 @@ public abstract class Setup {
|
||||||
|
|
||||||
|
|
||||||
public boolean isActive = true;
|
public boolean isActive = true;
|
||||||
|
public String creatorId;
|
||||||
public int step = 0;
|
public int step = 0;
|
||||||
public ArrayList<Runnable> steps = new ArrayList<>();
|
public ArrayList<Runnable> steps = new ArrayList<>();
|
||||||
public ArrayList<String> step_hints = new ArrayList<>();
|
public ArrayList<String> step_hints = new ArrayList<>();
|
||||||
|
|
||||||
|
public boolean can_start_setup(String playerId){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
public void run_step(){
|
public void run_step(){
|
||||||
if (!isActive)
|
if (!isActive)
|
||||||
return;
|
return;
|
||||||
|
|
146
src/main/java/net/pietru/cookie_utils/setups/areaPermSetup.java
Normal file
146
src/main/java/net/pietru/cookie_utils/setups/areaPermSetup.java
Normal file
|
@ -0,0 +1,146 @@
|
||||||
|
package net.pietru.cookie_utils.setups;
|
||||||
|
|
||||||
|
import com.badlogic.gdx.math.Vector3;
|
||||||
|
import com.badlogic.gdx.utils.Json;
|
||||||
|
import com.badlogic.gdx.utils.JsonWriter;
|
||||||
|
import finalforeach.cosmicreach.GameSingletons;
|
||||||
|
import finalforeach.cosmicreach.accounts.Account;
|
||||||
|
import finalforeach.cosmicreach.blocks.BlockPosition;
|
||||||
|
import finalforeach.cosmicreach.entities.player.Player;
|
||||||
|
import finalforeach.cosmicreach.io.SaveLocation;
|
||||||
|
import finalforeach.cosmicreach.networking.server.ServerSingletons;
|
||||||
|
import net.pietru.cookie_utils.api.Area;
|
||||||
|
import net.pietru.cookie_utils.api.Region;
|
||||||
|
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.api.Region.get_area_at_pos;
|
||||||
|
import static net.pietru.cookie_utils.utils.directory_utils.get_path_string;
|
||||||
|
|
||||||
|
public class areaPermSetup extends Setup {
|
||||||
|
String mode="";
|
||||||
|
String selected_perm="";
|
||||||
|
String usr_id="";
|
||||||
|
|
||||||
|
Area area;
|
||||||
|
PlayerAreaPerm plr_perm;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean can_start_setup(String playerId) {
|
||||||
|
Player player = GameSingletons.getPlayerFromUniqueId(playerId);
|
||||||
|
area=get_area_at_pos(player.getPosition());
|
||||||
|
return area!=null&&Region.is_area_user_type(player.getPosition(),playerId,"Owner");
|
||||||
|
}
|
||||||
|
|
||||||
|
public areaPermSetup() {
|
||||||
|
steps.add(()->{});
|
||||||
|
steps.add(()->{});
|
||||||
|
steps.add(()->{});
|
||||||
|
steps.add(()->{
|
||||||
|
for (int i = 0; i < area.player_perms.size; i++) {
|
||||||
|
PlayerAreaPerm perm = area.player_perms.get(i);
|
||||||
|
if (perm.player_id.equals(usr_id)) {
|
||||||
|
plr_perm = perm;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
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();
|
||||||
|
ObjectPermList.get_default_perms(plr_perm.perms);
|
||||||
|
}
|
||||||
|
step_hints.add("Permission setup complete");
|
||||||
|
|
||||||
|
boolean target = mode.equalsIgnoreCase("grant") || mode.equalsIgnoreCase("g");
|
||||||
|
|
||||||
|
switch (selected_perm){
|
||||||
|
case "all","a":
|
||||||
|
plr_perm.perms.set_bool_perm("canPlace", target);
|
||||||
|
plr_perm.perms.set_bool_perm("canBreak", target);
|
||||||
|
plr_perm.perms.set_bool_perm("canInteract", target);
|
||||||
|
plr_perm.perms.set_bool_perm("canEnter", target);
|
||||||
|
break;
|
||||||
|
case "edit","e":
|
||||||
|
plr_perm.perms.set_bool_perm("canPlace", target);
|
||||||
|
plr_perm.perms.set_bool_perm("canBreak", target);
|
||||||
|
break;
|
||||||
|
case "interact","i":
|
||||||
|
plr_perm.perms.set_bool_perm("canInteract", target);
|
||||||
|
break;
|
||||||
|
case "enter","m":
|
||||||
|
plr_perm.perms.set_bool_perm("canEnter", target);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
run_setup_finish();
|
||||||
|
});
|
||||||
|
|
||||||
|
step_hints.add("Interactive setup via chat, please type anything to continue...");
|
||||||
|
step_hints.add("Please select mode - Grant/Revoke (G/R)");
|
||||||
|
step_hints.add("Please select permission: edit(e),interact(i),enter(m),all(a).");
|
||||||
|
step_hints.add("Please enter player id...");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void register() {
|
||||||
|
setupCreator.registerSetupCreator("perm", areaPermSetup::new);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String get_setup_perm() {
|
||||||
|
return "setup.area_perm";
|
||||||
|
}
|
||||||
|
|
||||||
|
@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"));
|
||||||
|
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(plr_perm.get_json().toString()));
|
||||||
|
writer.close();
|
||||||
|
Region.reload_regions();
|
||||||
|
}
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void run_setup_canceled() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean set_setup_text(String value) {
|
||||||
|
if (step==1)
|
||||||
|
mode=value;
|
||||||
|
if (step==2)
|
||||||
|
selected_perm=value;
|
||||||
|
if (step==3)
|
||||||
|
usr_id=value;
|
||||||
|
return step==0 || step==1 || step==2 || step==3;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean set_setup_vector3(Vector3 value) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean set_setup_block_pos(BlockPosition value) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
|
@ -6,6 +6,7 @@ import com.badlogic.gdx.utils.JsonWriter;
|
||||||
import finalforeach.cosmicreach.blocks.BlockPosition;
|
import finalforeach.cosmicreach.blocks.BlockPosition;
|
||||||
import finalforeach.cosmicreach.io.SaveLocation;
|
import finalforeach.cosmicreach.io.SaveLocation;
|
||||||
import net.pietru.cookie_utils.api.Area;
|
import net.pietru.cookie_utils.api.Area;
|
||||||
|
import net.pietru.cookie_utils.api.Region;
|
||||||
import net.pietru.cookie_utils.permissions.ObjectPermList;
|
import net.pietru.cookie_utils.permissions.ObjectPermList;
|
||||||
|
|
||||||
import java.io.BufferedWriter;
|
import java.io.BufferedWriter;
|
||||||
|
@ -37,6 +38,7 @@ public class regionSetup extends Setup {
|
||||||
step_hints.add("Please select first position...");
|
step_hints.add("Please select first position...");
|
||||||
step_hints.add("Please select second position...");
|
step_hints.add("Please select second position...");
|
||||||
step_hints.add("Please enter region name (single word)...");
|
step_hints.add("Please enter region name (single word)...");
|
||||||
|
step_hints.add("Region creation complete");
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void register() {
|
public static void register() {
|
||||||
|
@ -57,8 +59,9 @@ public class regionSetup extends Setup {
|
||||||
Json json = new Json();
|
Json json = new Json();
|
||||||
json.setOutputType(JsonWriter.OutputType.json);
|
json.setOutputType(JsonWriter.OutputType.json);
|
||||||
BufferedWriter writer = new BufferedWriter(new FileWriter(area_file.getPath()));
|
BufferedWriter writer = new BufferedWriter(new FileWriter(area_file.getPath()));
|
||||||
writer.write(json.prettyPrint(area));
|
writer.write(json.prettyPrint(area.get_json().toString()));
|
||||||
writer.close();
|
writer.close();
|
||||||
|
Region.reload_regions();
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
|
|
|
@ -7,6 +7,7 @@ public interface setupCreator {
|
||||||
|
|
||||||
static void registerSetupCreators() {
|
static void registerSetupCreators() {
|
||||||
regionSetup.register();
|
regionSetup.register();
|
||||||
|
areaPermSetup.register();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void registerSetupCreator(String setupId, setupCreator creator) {
|
static void registerSetupCreator(String setupId, setupCreator creator) {
|
||||||
|
|
Loading…
Reference in a new issue