start on conversions
This commit is contained in:
parent
1f0a24a64f
commit
0418c25f5f
6 changed files with 281 additions and 0 deletions
|
@ -0,0 +1,10 @@
|
||||||
|
package lol.pyr.znpcsplus.conversion;
|
||||||
|
|
||||||
|
import lol.pyr.znpcsplus.npc.NpcEntryImpl;
|
||||||
|
|
||||||
|
import java.util.Collection;
|
||||||
|
|
||||||
|
public interface DataImporter {
|
||||||
|
Collection<NpcEntryImpl> importData();
|
||||||
|
boolean isValid();
|
||||||
|
}
|
|
@ -0,0 +1,19 @@
|
||||||
|
package lol.pyr.znpcsplus.conversion;
|
||||||
|
|
||||||
|
import lol.pyr.znpcsplus.util.LazyLoader;
|
||||||
|
|
||||||
|
public enum DataImporterType {
|
||||||
|
ZNPCS(() -> null), // TODO
|
||||||
|
LEGACY_ZNPCS_PLUS(() -> null), // TODO
|
||||||
|
CITIZENS(() -> null); // TODO
|
||||||
|
|
||||||
|
private final LazyLoader<DataImporter> importerLoader;
|
||||||
|
|
||||||
|
DataImporterType(LazyLoader.ObjectProvider<DataImporter> provider) {
|
||||||
|
this.importerLoader = LazyLoader.of(provider);
|
||||||
|
}
|
||||||
|
|
||||||
|
DataImporter getImporter() {
|
||||||
|
return importerLoader.get();
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,130 @@
|
||||||
|
package lol.pyr.znpcsplus.conversion.znpcs;
|
||||||
|
|
||||||
|
import com.google.gson.Gson;
|
||||||
|
import com.google.gson.stream.JsonReader;
|
||||||
|
import lol.pyr.znpcsplus.api.interaction.InteractionType;
|
||||||
|
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.interaction.InteractionAction;
|
||||||
|
import lol.pyr.znpcsplus.interaction.consolecommand.ConsoleCommandAction;
|
||||||
|
import lol.pyr.znpcsplus.interaction.message.MessageAction;
|
||||||
|
import lol.pyr.znpcsplus.interaction.playerchat.PlayerChatAction;
|
||||||
|
import lol.pyr.znpcsplus.interaction.playercommand.PlayerCommandAction;
|
||||||
|
import lol.pyr.znpcsplus.interaction.switchserver.SwitchServerAction;
|
||||||
|
import lol.pyr.znpcsplus.npc.NpcEntryImpl;
|
||||||
|
import lol.pyr.znpcsplus.npc.NpcImpl;
|
||||||
|
import lol.pyr.znpcsplus.npc.NpcTypeRegistryImpl;
|
||||||
|
import lol.pyr.znpcsplus.packets.PacketFactory;
|
||||||
|
import lol.pyr.znpcsplus.scheduling.TaskScheduler;
|
||||||
|
import lol.pyr.znpcsplus.util.BungeeConnector;
|
||||||
|
import lol.pyr.znpcsplus.util.NpcLocation;
|
||||||
|
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
|
||||||
|
import net.kyori.adventure.text.Component;
|
||||||
|
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
|
|
||||||
|
public class ZNpcsLoader implements DataImporter {
|
||||||
|
private final ConfigManager configManager;
|
||||||
|
private final BukkitAudiences adventure;
|
||||||
|
private final BungeeConnector bungeeConnector;
|
||||||
|
private final TaskScheduler taskScheduler;
|
||||||
|
private final PacketFactory packetFactory;
|
||||||
|
private final LegacyComponentSerializer textSerializer;
|
||||||
|
private final NpcTypeRegistryImpl typeRegistry;
|
||||||
|
private final File folder;
|
||||||
|
private final File dataFile;
|
||||||
|
private final Gson gson;
|
||||||
|
|
||||||
|
public ZNpcsLoader(ConfigManager configManager, BukkitAudiences adventure, BungeeConnector bungeeConnector, TaskScheduler taskScheduler, PacketFactory packetFactory, LegacyComponentSerializer textSerializer, NpcTypeRegistryImpl typeRegistry, File pluginsFolder) {
|
||||||
|
this.configManager = configManager;
|
||||||
|
this.adventure = adventure;
|
||||||
|
this.bungeeConnector = bungeeConnector;
|
||||||
|
this.taskScheduler = taskScheduler;
|
||||||
|
this.packetFactory = packetFactory;
|
||||||
|
this.textSerializer = textSerializer;
|
||||||
|
this.typeRegistry = typeRegistry;
|
||||||
|
folder = new File(pluginsFolder, "ServersNPC");
|
||||||
|
dataFile = new File(folder, "data.json");
|
||||||
|
gson = new Gson();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Collection<NpcEntryImpl> importData() {
|
||||||
|
ZNpcsModel[] models;
|
||||||
|
try (FileReader fileReader = new FileReader(dataFile)) {
|
||||||
|
models = gson.fromJson(new JsonReader(fileReader), ZNpcsModel[].class);
|
||||||
|
} catch (IOException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
if (models == null) return Collections.emptyList();
|
||||||
|
ArrayList<NpcEntryImpl> entries = new ArrayList<>();
|
||||||
|
for (ZNpcsModel model : models) {
|
||||||
|
String type = model.getNpcType();
|
||||||
|
if (type.equalsIgnoreCase("mushroom_cow")) type = "mooshroom";
|
||||||
|
else if (type.equalsIgnoreCase("snowman")) type = "snow_golem";
|
||||||
|
|
||||||
|
|
||||||
|
ZNpcsLocation oldLoc = model.getLocation();
|
||||||
|
NpcLocation location = new NpcLocation(oldLoc.getX(), oldLoc.getY(), oldLoc.getZ(), oldLoc.getYaw(), oldLoc.getPitch());
|
||||||
|
NpcImpl npc = new NpcImpl(configManager, packetFactory, textSerializer, oldLoc.getWorld(), typeRegistry.getByName(type), location);
|
||||||
|
|
||||||
|
for (String raw : model.getHologramLines()) {
|
||||||
|
Component line = textSerializer.deserialize(raw);
|
||||||
|
npc.getHologram().addLineComponent(line);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (ZNpcsAction action : model.getClickActions()) {
|
||||||
|
InteractionType t = adaptClickType(action.getActionType());
|
||||||
|
// TODO
|
||||||
|
}
|
||||||
|
|
||||||
|
NpcEntryImpl entry = new NpcEntryImpl(String.valueOf(model.getId()), npc);
|
||||||
|
entry.enableEverything();
|
||||||
|
entries.add(entry);
|
||||||
|
}
|
||||||
|
return entries;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isValid() {
|
||||||
|
return folder.isDirectory() && dataFile.isFile();
|
||||||
|
}
|
||||||
|
|
||||||
|
private InteractionType adaptClickType(String clickType) {
|
||||||
|
switch (clickType.toLowerCase()) {
|
||||||
|
case "default":
|
||||||
|
return InteractionType.ANY_CLICK;
|
||||||
|
case "left":
|
||||||
|
return InteractionType.LEFT_CLICK;
|
||||||
|
case "right":
|
||||||
|
return InteractionType.RIGHT_CLICK;
|
||||||
|
}
|
||||||
|
throw new IllegalArgumentException("Couldn't adapt znpcs click type: " + clickType);
|
||||||
|
}
|
||||||
|
|
||||||
|
private InteractionAction adaptAction(String type, InteractionType clickType, String parameter, int delay) {
|
||||||
|
switch (type.toLowerCase()) {
|
||||||
|
case "cmd":
|
||||||
|
return new PlayerCommandAction(taskScheduler, parameter, clickType, delay * 1000L);
|
||||||
|
case "console":
|
||||||
|
return new ConsoleCommandAction(taskScheduler, parameter, clickType, delay * 1000L);
|
||||||
|
case "chat":
|
||||||
|
return new PlayerChatAction(parameter, clickType, delay * 1000L);
|
||||||
|
case "message":
|
||||||
|
return new MessageAction(adventure, parameter, clickType, textSerializer, delay * 1000L);
|
||||||
|
case "server":
|
||||||
|
return new SwitchServerAction(bungeeConnector, parameter, clickType, delay * 1000L);
|
||||||
|
}
|
||||||
|
throw new IllegalArgumentException("Couldn't adapt znpcs click action: " + type);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,24 @@
|
||||||
|
package lol.pyr.znpcsplus.conversion.znpcs.model;
|
||||||
|
|
||||||
|
public class ZNpcsAction {
|
||||||
|
private String actionType;
|
||||||
|
private String clickType;
|
||||||
|
private String action;
|
||||||
|
private double delay;
|
||||||
|
|
||||||
|
public String getActionType() {
|
||||||
|
return actionType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getClickType() {
|
||||||
|
return clickType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getAction() {
|
||||||
|
return action;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getDelay() {
|
||||||
|
return delay;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,34 @@
|
||||||
|
package lol.pyr.znpcsplus.conversion.znpcs.model;
|
||||||
|
|
||||||
|
public class ZNpcsLocation {
|
||||||
|
private String world;
|
||||||
|
private double x;
|
||||||
|
private double y;
|
||||||
|
private double z;
|
||||||
|
private float yaw;
|
||||||
|
private float pitch;
|
||||||
|
|
||||||
|
public String getWorld() {
|
||||||
|
return world;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getX() {
|
||||||
|
return x;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getY() {
|
||||||
|
return y;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getZ() {
|
||||||
|
return z;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getYaw() {
|
||||||
|
return yaw;
|
||||||
|
}
|
||||||
|
|
||||||
|
public float getPitch() {
|
||||||
|
return pitch;
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,64 @@
|
||||||
|
package lol.pyr.znpcsplus.conversion.znpcs.model;
|
||||||
|
|
||||||
|
import org.bukkit.inventory.ItemStack;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
|
public class ZNpcsModel {
|
||||||
|
private int id;
|
||||||
|
private double hologramHeight;
|
||||||
|
private String skin;
|
||||||
|
private String signature;
|
||||||
|
private String glowName;
|
||||||
|
private ZNpcsLocation location;
|
||||||
|
private String npcType;
|
||||||
|
private List<String> hologramLines;
|
||||||
|
private List<ZNpcsAction> clickActions;
|
||||||
|
private Map<String, ItemStack> npcEquip;
|
||||||
|
private Map<String, String[]> customizationMap;
|
||||||
|
|
||||||
|
public int getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getHologramHeight() {
|
||||||
|
return hologramHeight;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSkin() {
|
||||||
|
return skin;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getSignature() {
|
||||||
|
return signature;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getGlowName() {
|
||||||
|
return glowName;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ZNpcsLocation getLocation() {
|
||||||
|
return location;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getNpcType() {
|
||||||
|
return npcType;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> getHologramLines() {
|
||||||
|
return hologramLines;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<ZNpcsAction> getClickActions() {
|
||||||
|
return clickActions;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, ItemStack> getNpcEquip() {
|
||||||
|
return npcEquip;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Map<String, String[]> getCustomizationMap() {
|
||||||
|
return customizationMap;
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue