some code cleanups and make zone creation cmd save zones to load...
Some checks failed
/ Auto-Build-App (push) Has been cancelled

This commit is contained in:
pietru2004 2024-11-08 09:11:59 +01:00
parent fdbe58fc9a
commit 7c4921843b
3 changed files with 33 additions and 21 deletions

View file

@ -1,17 +1,11 @@
package net.pietru.cookie_utils;
import com.badlogic.gdx.Gdx;
import finalforeach.cosmicreach.GameAssetLoader;
import finalforeach.cosmicreach.GameSingletons;
import finalforeach.cosmicreach.ZoneLoaders;
import finalforeach.cosmicreach.blockevents.BlockEvents;
import finalforeach.cosmicreach.blocks.Block;
import dev.crmodders.cosmicquilt.api.entrypoint.ModInitializer;
import finalforeach.cosmicreach.world.Zone;
import net.pietru.cookie_utils.api.Region;
import net.pietru.cookie_utils.permissions.Permissions;
import net.pietru.cookie_utils.setups.setupCreator;
import net.pietru.cookie_utils.utils.prop;
import org.quiltmc.loader.api.ModContainer;
import java.io.IOException;

View file

@ -16,6 +16,8 @@ import net.pietru.cookie_utils.permissions.Permissions;
import net.pietru.cookie_utils.setups.Setup;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
@ -26,6 +28,8 @@ 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;
import static net.pietru.cookie_utils.utils.player_utils.parseAsPlayer;
import static net.pietru.cookie_utils.utils.prop.get_server_property_array;
import static net.pietru.cookie_utils.utils.prop.set_server_property;
public class TextCommands {
public static final Map<String, TriConsumer<String[], NetworkIdentity, ChannelHandlerContext>> commands = new HashMap<>();
@ -44,7 +48,6 @@ public class TextCommands {
static {
commands.put("help",(args, networkIdentity, channelHandlerContext) -> {
Player player = ServerSingletons.getPlayer(networkIdentity);
Account account = ServerSingletons.getAccount(networkIdentity);
MessagePacket packet;
packet = new MessagePacket("You have access to following commands:");
@ -176,7 +179,7 @@ public class TextCommands {
else {
File region = new File(get_path_string(SaveLocation.getSaveFolder().getPath(), "regions", area.name));
if (region.exists()) {
region.delete();
boolean ignored = region.delete();
packet = new MessagePacket("[Server] Usage :region area_name.json ACTION value(if required)");
}
}
@ -194,18 +197,37 @@ public class TextCommands {
commands.put("make_zone",(args, networkIdentity, channelHandlerContext) -> {
boolean created = false;
ArrayList<String> zones;
try {
zones = get_server_property_array("zones","[]");
} catch (IOException e) {
e.printStackTrace();
zones = new ArrayList<>();
}
if (args.length==2){
String zoneId = args[1];
created = GameSingletons.world.getZoneIfExists(zoneId)==null;
if(created)
if(created) {
GameSingletons.world.addNewZone(zoneId, ZoneGenerator.getZoneGenerator(zoneId));
zones.add(zoneId);
}
}
if (args.length>=3){
String zoneId = args[1];
String genId = args[2];
created = GameSingletons.world.getZoneIfExists(zoneId)==null;
if(created)
if(created) {
GameSingletons.world.addNewZone(zoneId, ZoneGenerator.getZoneGenerator(genId));
zones.add(zoneId);
}
}
if (created){
try {
set_server_property("zones",zones);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
MessagePacket packet = new MessagePacket(created ? "[Server] Created new zone..." : "[Server] Could not create new zone...");
@ -214,7 +236,6 @@ public class TextCommands {
commands.put("send",(args, networkIdentity, channelHandlerContext) -> {
Player player = ServerSingletons.getPlayer(networkIdentity);
Account account = ServerSingletons.getAccount(networkIdentity);
String txt = "[Server] Could not send plr to zone...";
if (args.length>=2){
String zoneId = args[1];
@ -294,7 +315,7 @@ public class TextCommands {
});
commands.put("reload_groups",(args, networkIdentity, channelHandlerContext) -> {
MessagePacket packet = new MessagePacket("Reloaded groups...");;
MessagePacket packet = new MessagePacket("Reloaded groups...");
reload_perm_groups();
Account account = ServerSingletons.getAccount(networkIdentity);
packet.playerUniqueId=account.getUniqueId();

View file

@ -8,7 +8,6 @@ import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import static net.pietru.cookie_utils.utils.directory_utils.get_path_string;
@ -21,14 +20,14 @@ public class prop {
String txt = (String) get_server_property(key,defaultValue);
String replace = txt.replace("[","");
String replace1 = replace.replace("]","");
return new ArrayList<String>(Arrays.asList(replace1.split(",")));
return new ArrayList<>(Arrays.asList(replace1.split(",")));
}
public static Object get_server_property(String key, Object defaultValue) throws IOException {
if(!config_file.exists())
config_file.createNewFile();
Properties prop = new Properties();
prop.load(new FileInputStream(file.toString()));
prop.load(new FileInputStream(config_file.toString()));
return prop.getOrDefault(key,defaultValue);
}
public static void set_server_property(String key, Object value) throws IOException {
@ -37,28 +36,26 @@ public class prop {
if(!config_file.exists())
config_file.createNewFile();
Properties prop = new Properties();
prop.load(new FileInputStream(file.toString()));
prop.load(new FileInputStream(config_file.toString()));
prop.put(key,value);
// Storing the properties in the file with a heading comment.
prop.store(new FileWriter(file.toString()), "This file is CookieServerUtils config file.");
prop.store(new FileWriter(config_file.toString()), "This file is CookieServerUtils config file.");
}
public static void load_panel_config() throws IOException {
if(!config_file.exists())
config_file.createNewFile();
panel_prop = new Properties();
panel_prop.load(new FileInputStream(file.toString()));
panel_prop.load(new FileInputStream(config_file.toString()));
boolean save = false;
if (!panel_prop.containsKey("zones")){
panel_prop.put("zones",new ArrayList<String>()); save=true;}
if (!panel_prop.containsKey("passwd")){
panel_prop.put("passwd","1234"); save=true;}
if (save)
panel_prop.store(new FileWriter(file.toString()), "This file is CookieServerUtils config file.");
panel_prop.store(new FileWriter(config_file.toString()), "This file is CookieServerUtils config file.");
}
}