Skip to content

Commit

Permalink
Merge pull request #119 from MineFact/2.X
Browse files Browse the repository at this point in the history
Fix migration issues
  • Loading branch information
Pyrbu authored Jan 31, 2024
2 parents 624a3c9 + 7ebb456 commit 35ad35f
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
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;
Expand Down Expand Up @@ -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;

Expand All @@ -72,6 +71,7 @@ public ZNpcImporter(ConfigManager configManager, BukkitAudiences adventure,
this.propertyRegistry = propertyRegistry;
this.skinCache = skinCache;
this.dataFile = dataFile;
this.conversationFile = new File(dataFile.getParentFile(), "conversations.json");
this.bungeeConnector = bungeeConnector;
gson = new GsonBuilder()
.create();
Expand All @@ -88,6 +88,19 @@ public Collection<NpcEntryImpl> importData() {
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();
Expand All @@ -107,6 +120,41 @@ public Collection<NpcEntryImpl> importData() {
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()) {
Expand All @@ -132,7 +180,7 @@ else if (model.getSkin() != null && model.getSignature() != null) {
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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -38,6 +41,10 @@ public String getSkinName() {
return skinName;
}

public ZNpcsConversation getConversation() {
return conversation;
}

public ZNpcsLocation getLocation() {
return location;
}
Expand All @@ -62,6 +69,10 @@ public Map<String, Object> getNpcToggleValues() {
return npcToggleValues;
}

public Map<String, Object> getNpcFunctions() {
return npcFunctions;
}

public Map<String, String[]> getCustomizationMap() {
return customizationMap;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit 35ad35f

Please sign in to comment.