add saving and loading npcs
All checks were successful
/ Auto-Build-App (push) Successful in 4m12s

This commit is contained in:
pietru 2025-04-29 20:53:06 +02:00
parent dab6d8fa11
commit 4a05528b36
4 changed files with 143 additions and 0 deletions

View file

@ -1,12 +1,22 @@
package net.pietru.cookie_utils.commands;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Json;
import com.badlogic.gdx.utils.JsonWriter;
import finalforeach.cosmicreach.entities.player.Player;
import finalforeach.cosmicreach.io.SaveLocation;
import finalforeach.cosmicreach.networking.NetworkIdentity;
import finalforeach.cosmicreach.networking.packets.MessagePacket;
import io.netty.channel.ChannelHandlerContext;
import net.pietru.cookie_utils.api.Region;
import net.pietru.cookie_utils.npcs.NpcDef;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import static net.pietru.cookie_utils.utils.directory_utils.get_path_string;
public class npc_command extends BaseCommand{
@Override
public String get_command_key() {
@ -100,6 +110,32 @@ public class npc_command extends BaseCommand{
}
packet.message = msg.toString();
break;
case "save":
File npc_config = new File(get_path_string(SaveLocation.getSaveFolder().getPath(),"config", "cookie-npcs.json"));
if (!npc_config.getParentFile().exists())
npc_config.getParentFile().mkdirs();
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(npc_config.getPath()));
for(NpcDef npc : NpcDef.registered_npc_list){
writer.write(npc.get_json().toJson(JsonWriter.OutputType.json));
writer.newLine();
}
writer.close();
packet.message = "[Server] Saved npcs...";
} catch (Exception e) {
e.printStackTrace();
packet.message = "[Server] Failed to save npcs...";
}
break;
case "load":
try {
NpcDef.load_npcs();
packet.message = "[Server] Loaded npcs...";
} catch (Exception e) {
e.printStackTrace();
packet.message = "[Server] Failed to load npcs...";
}
break;
}
}

View file

@ -10,8 +10,10 @@ import io.netty.channel.ChannelHandlerContext;
import net.pietru.cookie_utils.CookieUtils;
import net.pietru.cookie_utils.npcs.NpcDef;
import net.pietru.cookie_utils.utils.player_utils;
import net.pietru.cookie_utils.utils.prop;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
@ -27,6 +29,7 @@ public class LoginPacketMixin {
@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());
player_utils.plr_last_area.put(account.getUniqueId(),"");
@ -34,6 +37,9 @@ public class LoginPacketMixin {
CookieUtils.delayer.schedule(new TimerTask() {
@Override
public void run() {
if (prop.load_npcs && !NpcDef.were_npcs_loaded){
NpcDef.load_npcs();
}
for (NpcDef def : NpcDef.registered_npc_list){
def.send_to(ctx);
}

View file

@ -1,18 +1,27 @@
package net.pietru.cookie_utils.npcs;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.JsonReader;
import com.badlogic.gdx.utils.JsonValue;
import finalforeach.cosmicreach.GameSingletons;
import finalforeach.cosmicreach.accounts.AccountOffline;
import finalforeach.cosmicreach.entities.Entity;
import finalforeach.cosmicreach.entities.player.Player;
import finalforeach.cosmicreach.entities.player.PlayerEntity;
import finalforeach.cosmicreach.io.SaveLocation;
import finalforeach.cosmicreach.networking.packets.entities.PlayerPacket;
import finalforeach.cosmicreach.networking.packets.entities.PlayerPositionPacket;
import finalforeach.cosmicreach.networking.packets.meta.RemovedPlayerPacket;
import finalforeach.cosmicreach.networking.server.ServerSingletons;
import finalforeach.cosmicreach.world.Zone;
import io.netty.channel.ChannelHandlerContext;
import net.pietru.cookie_utils.utils.prop;
import java.io.File;
import java.util.ArrayList;
import java.util.Scanner;
import static net.pietru.cookie_utils.utils.directory_utils.get_path_string;
public class NpcDef {
public final static ArrayList<NpcDef> registered_npc_list = new ArrayList<>();
@ -139,4 +148,91 @@ public class NpcDef {
zone.removeEntity(playerEntity);
return this;
}
public NpcDef load(JsonValue data){
playerEntity.setPosition(
data.getFloat("x1"),
data.getFloat("y1"),
data.getFloat("z1")
);
playerEntity.viewDirection.set(
data.getFloat("x2"),
data.getFloat("y2"),
data.getFloat("z2")
);
playerEntity.viewPositionOffset.set(
data.getFloat("x3"),
data.getFloat("y3"),
data.getFloat("z3")
);;
account.setDisplayName(data.getString("npc_name"));
player.zoneId=data.getString("zoneId");
zone = GameSingletons.world.getZoneIfExists(data.getString("zoneId"));
if (data.getBoolean("added"))
show_npc();
else
hide_npc();
return this;
}
public JsonValue get_json(){
JsonValue val = new JsonValue(JsonValue.ValueType.object);
Vector3 v1 = playerEntity.getPosition();
Vector3 v2 = playerEntity.viewDirection;
Vector3 v3 = playerEntity.viewPositionOffset;
val.addChild("x1", new JsonValue(v1.x));
val.addChild("y1", new JsonValue(v1.y));
val.addChild("z1", new JsonValue(v1.z));
val.addChild("x2", new JsonValue(v2.x));
val.addChild("y2", new JsonValue(v2.y));
val.addChild("z2", new JsonValue(v2.z));
val.addChild("x3", new JsonValue(v3.x));
val.addChild("y3", new JsonValue(v3.y));
val.addChild("z3", new JsonValue(v3.z));
val.addChild("npc_name", new JsonValue(account.getDisplayName()));
val.addChild("zoneId", new JsonValue(player.zoneId));
val.addChild("added", new JsonValue(added));
return val;
}
public static boolean were_npcs_loaded = false;
public static JsonReader jsonReader = new JsonReader();
public static void load_npcs(){
were_npcs_loaded=true;
File npc_config = new File(get_path_string(SaveLocation.getSaveFolder().getPath(),"config", "cookie-npcs.json"));
if (!npc_config.getParentFile().exists())
npc_config.getParentFile().mkdirs();
if (!npc_config.exists())
return;
for (NpcDef npc : registered_npc_list){
npc.unregister();
}
registered_npc_list.clear();
try {
Scanner scanner = new Scanner(npc_config);
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
try {
JsonValue npc_data = jsonReader.parse(line);
new NpcDef("LoadingData...", Vector3.Zero, null).load(npc_data).register();
} catch (Exception ignored){
if (prop.log_level>0)
System.err.printf("[NPCS] Encountered exception while loading npc data of a npc -> %s\n", line);
}
}
scanner.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

View file

@ -16,6 +16,7 @@ public class prop {
static File config_file = new File(get_path_string(SaveLocation.getSaveFolder().getPath(),"config", "cookie.config"));
public static Properties panel_prop;
public static int log_level = 1;
public static boolean load_npcs = false;
public static ArrayList<String> get_server_property_array(String key, Object defaultValue) throws IOException {
String txt = (String) get_server_property(key,defaultValue);
@ -62,6 +63,10 @@ public class prop {
if (!panel_prop.containsKey("chat_shadow_delay")){
panel_prop.put("chat_shadow_delay","500"); save=true;}
if (!panel_prop.containsKey("npc_load_on_start")){
panel_prop.put("npc_load_on_start","false"); save=true;}
load_npcs = Boolean.parseBoolean((String) panel_prop.getOrDefault("log_level","false"));
/*
* level 2 - sudo_code info error debug debug+