Merge pull request #119 from MineFact/2.X

Fix migration issues
This commit is contained in:
Pyr 2024-01-31 05:22:30 +00:00 committed by GitHub
commit 35ad35fb57
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 121 additions and 4 deletions

@ -8,9 +8,7 @@ import lol.pyr.znpcsplus.api.interaction.InteractionType;
import lol.pyr.znpcsplus.api.skin.SkinDescriptor;
import lol.pyr.znpcsplus.config.ConfigManager;
import lol.pyr.znpcsplus.conversion.DataImporter;
import lol.pyr.znpcsplus.conversion.znpcs.model.ZNpcsAction;
import lol.pyr.znpcsplus.conversion.znpcs.model.ZNpcsLocation;
import lol.pyr.znpcsplus.conversion.znpcs.model.ZNpcsModel;
import lol.pyr.znpcsplus.conversion.znpcs.model.*;
import lol.pyr.znpcsplus.entity.EntityPropertyImpl;
import lol.pyr.znpcsplus.entity.EntityPropertyRegistryImpl;
import lol.pyr.znpcsplus.hologram.HologramImpl;
@ -56,6 +54,7 @@ public class ZNpcImporter implements DataImporter {
private final EntityPropertyRegistryImpl propertyRegistry;
private final MojangSkinCache skinCache;
private final File dataFile;
private final File conversationFile;
private final Gson gson;
private final BungeeConnector bungeeConnector;
@ -72,6 +71,7 @@ public class ZNpcImporter implements DataImporter {
this.propertyRegistry = propertyRegistry;
this.skinCache = skinCache;
this.dataFile = dataFile;
this.conversationFile = new File(dataFile.getParentFile(), "conversations.json");
this.bungeeConnector = bungeeConnector;
gson = new GsonBuilder()
.create();
@ -88,6 +88,19 @@ public class ZNpcImporter implements DataImporter {
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);
for (ZNpcsModel model : models) {
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);
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();
hologram.setOffset(model.getHologramHeight());
for (String raw : model.getHologramLines()) {
@ -132,7 +180,7 @@ public class ZNpcImporter implements DataImporter {
npc.setProperty(propertyRegistry.getByName("skin", SkinDescriptor.class), new PrefetchedDescriptor(new SkinImpl(model.getSkin(), model.getSignature())));
}
Map<String, Object> toggleValues = model.getNpcToggleValues();
Map<String, Object> toggleValues = model.getNpcToggleValues() == null ? model.getNpcFunctions() : model.getNpcToggleValues();
if (toggleValues != null) {
if (toggleValues.containsKey("look")) {
npc.setProperty(propertyRegistry.getByName("look", LookType.class), LookType.CLOSEST_PLAYER);

@ -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,12 +14,15 @@ public class ZNpcsModel {
private String signature;
private String glowName;
private ZNpcsConversation conversation;
private ZNpcsLocation location;
private String npcType;
private List<String> hologramLines;
private List<ZNpcsAction> clickActions;
private Map<String, String> npcEquip;
private Map<String, Object> npcToggleValues;
private Map<String, Object> npcFunctions;
private Map<String, String[]> customizationMap;
public int getId() {
@ -38,6 +41,10 @@ public class ZNpcsModel {
return skinName;
}
public ZNpcsConversation getConversation() {
return conversation;
}
public ZNpcsLocation getLocation() {
return location;
}
@ -62,6 +69,10 @@ public class ZNpcsModel {
return npcToggleValues;
}
public Map<String, Object> getNpcFunctions() {
return npcFunctions;
}
public Map<String, String[]> getCustomizationMap() {
return customizationMap;
}

@ -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;
}
}