convert npc conversations from conversations.json as well

This commit is contained in:
MineFact 2024-01-12 17:55:22 +01:00
parent 0f17060b49
commit 7ebb456e1b
5 changed files with 115 additions and 3 deletions

@ -8,9 +8,7 @@ import lol.pyr.znpcsplus.api.interaction.InteractionType;
import lol.pyr.znpcsplus.api.skin.SkinDescriptor; import lol.pyr.znpcsplus.api.skin.SkinDescriptor;
import lol.pyr.znpcsplus.config.ConfigManager; import lol.pyr.znpcsplus.config.ConfigManager;
import lol.pyr.znpcsplus.conversion.DataImporter; import lol.pyr.znpcsplus.conversion.DataImporter;
import lol.pyr.znpcsplus.conversion.znpcs.model.ZNpcsAction; import lol.pyr.znpcsplus.conversion.znpcs.model.*;
import lol.pyr.znpcsplus.conversion.znpcs.model.ZNpcsLocation;
import lol.pyr.znpcsplus.conversion.znpcs.model.ZNpcsModel;
import lol.pyr.znpcsplus.entity.EntityPropertyImpl; import lol.pyr.znpcsplus.entity.EntityPropertyImpl;
import lol.pyr.znpcsplus.entity.EntityPropertyRegistryImpl; import lol.pyr.znpcsplus.entity.EntityPropertyRegistryImpl;
import lol.pyr.znpcsplus.hologram.HologramImpl; import lol.pyr.znpcsplus.hologram.HologramImpl;
@ -56,6 +54,7 @@ public class ZNpcImporter implements DataImporter {
private final EntityPropertyRegistryImpl propertyRegistry; private final EntityPropertyRegistryImpl propertyRegistry;
private final MojangSkinCache skinCache; private final MojangSkinCache skinCache;
private final File dataFile; private final File dataFile;
private final File conversationFile;
private final Gson gson; private final Gson gson;
private final BungeeConnector bungeeConnector; private final BungeeConnector bungeeConnector;
@ -72,6 +71,7 @@ public class ZNpcImporter implements DataImporter {
this.propertyRegistry = propertyRegistry; this.propertyRegistry = propertyRegistry;
this.skinCache = skinCache; this.skinCache = skinCache;
this.dataFile = dataFile; this.dataFile = dataFile;
this.conversationFile = new File(dataFile.getParentFile(), "conversations.json");
this.bungeeConnector = bungeeConnector; this.bungeeConnector = bungeeConnector;
gson = new GsonBuilder() gson = new GsonBuilder()
.create(); .create();
@ -88,6 +88,19 @@ public class ZNpcImporter implements DataImporter {
return Collections.emptyList(); return Collections.emptyList();
} }
if (models == null) return Collections.emptyList(); if (models == null) return Collections.emptyList();
ZnpcsConversations[] conversations;
try (BufferedReader fileReader = Files.newBufferedReader(conversationFile.toPath())) {
JsonElement element = JsonParser.parseReader(fileReader);
conversations = gson.fromJson(element, ZnpcsConversations[].class);
} catch (IOException e) {
e.printStackTrace();
return Collections.emptyList();
}
if (conversations == null) return Collections.emptyList();
ArrayList<NpcEntryImpl> entries = new ArrayList<>(models.length); ArrayList<NpcEntryImpl> entries = new ArrayList<>(models.length);
for (ZNpcsModel model : models) { for (ZNpcsModel model : models) {
String type = model.getNpcType(); String type = model.getNpcType();
@ -107,6 +120,41 @@ public class ZNpcImporter implements DataImporter {
NpcImpl npc = new NpcImpl(uuid, propertyRegistry, configManager, packetFactory, textSerializer, oldLoc.getWorld(), typeRegistry.getByName(type), location); NpcImpl npc = new NpcImpl(uuid, propertyRegistry, configManager, packetFactory, textSerializer, oldLoc.getWorld(), typeRegistry.getByName(type), location);
npc.getType().applyDefaultProperties(npc); npc.getType().applyDefaultProperties(npc);
// Convert the conversations from each NPC
ZNpcsConversation conversation = model.getConversation();
if (conversation != null) {
// Loop through all conversations in the conversations.json file
for (ZnpcsConversations conv : conversations) {
// If the conversation name matches the conversation name in the data.json file, proceed
if (conv.getName().equalsIgnoreCase(conversation.getConversationName())) {
int totalDelay = 0;
// Loop through all texts in the conversation
for(ZNpcsConversationText text : conv.getTexts()) {
// Add the delay in ticks to the total delay
totalDelay += text.getDelay() * 20;
// Get the lines of text from the conversation
String[] lines = text.getLines();
// Loop through all lines of text
for (String line : lines) {
// Create a new message action for each line of text
InteractionActionImpl action = new MessageAction(adventure, line, InteractionType.ANY_CLICK, textSerializer, 0, totalDelay);
npc.addAction(action);
}
}
}
}
}
HologramImpl hologram = npc.getHologram(); HologramImpl hologram = npc.getHologram();
hologram.setOffset(model.getHologramHeight()); hologram.setOffset(model.getHologramHeight());
for (String raw : model.getHologramLines()) { for (String raw : model.getHologramLines()) {

@ -0,0 +1,16 @@
package lol.pyr.znpcsplus.conversion.znpcs.model;
@SuppressWarnings("unused")
public class ZNpcsConversation {
private String conversationName;
private String conversationType;
public String getConversationName() {
return conversationName;
}
public String getConversationType() {
return conversationType;
}
}

@ -0,0 +1,19 @@
package lol.pyr.znpcsplus.conversion.znpcs.model;
@SuppressWarnings("unused")
public class ZNpcsConversationText {
private String[] lines;
private ZNpcsAction[] actions;
private int delay;
public String[] getLines() {
return lines;
}
public ZNpcsAction[] getActions() {
return actions;
}
public int getDelay() {
return delay;
}
}

@ -14,6 +14,8 @@ public class ZNpcsModel {
private String signature; private String signature;
private String glowName; private String glowName;
private ZNpcsConversation conversation;
private ZNpcsLocation location; private ZNpcsLocation location;
private String npcType; private String npcType;
private List<String> hologramLines; private List<String> hologramLines;
@ -39,6 +41,10 @@ public class ZNpcsModel {
return skinName; return skinName;
} }
public ZNpcsConversation getConversation() {
return conversation;
}
public ZNpcsLocation getLocation() { public ZNpcsLocation getLocation() {
return location; return location;
} }

@ -0,0 +1,23 @@
package lol.pyr.znpcsplus.conversion.znpcs.model;
@SuppressWarnings("unused")
public class ZnpcsConversations {
private String name;
private ZNpcsConversationText[] texts;
private int radius;
private int delay;
public String getName() {
return name;
}
public ZNpcsConversationText[] getTexts() {
return texts;
}
public int getRadius() {
return radius;
}
public int getDelay() {
return delay;
}
}