make importer ids lower case and add base for citizens importer

This commit is contained in:
Pyrbu 2023-06-19 00:01:38 +02:00
parent f8f5877619
commit 9e920ecab1
3 changed files with 76 additions and 8 deletions

@ -28,14 +28,16 @@ public class DataImporterRegistry {
register("znpcs", LazyLoader.of(() -> new ZNpcImporter(configManager, adventure, bungeeConnector, taskScheduler, register("znpcs", LazyLoader.of(() -> new ZNpcImporter(configManager, adventure, bungeeConnector, taskScheduler,
packetFactory, textSerializer, typeRegistry, propertyRegistry, skinCache, new File(pluginsFolder, "ServersNPC/data.json")))); packetFactory, textSerializer, typeRegistry, propertyRegistry, skinCache, new File(pluginsFolder, "ServersNPC/data.json"))));
/* register("citizens", LazyLoader.of(() -> new CitizensImporter(configManager, adventure, bungeeConnector, taskScheduler,
packetFactory, textSerializer, typeRegistry, propertyRegistry, skinCache, new File(pluginsFolder, "Citizens/saves.yml")))); */
} }
private void register(String id, LazyLoader<DataImporter> loader) { private void register(String id, LazyLoader<DataImporter> loader) {
importers.put(id.toUpperCase(), loader); importers.put(id.toLowerCase(), loader);
} }
public DataImporter getImporter(String id) { public DataImporter getImporter(String id) {
id = id.toUpperCase(); id = id.toLowerCase();
return importers.containsKey(id) ? importers.get(id).get() : null; return importers.containsKey(id) ? importers.get(id).get() : null;
} }

@ -0,0 +1,60 @@
package lol.pyr.znpcsplus.conversion.citizens;
import lol.pyr.znpcsplus.config.ConfigManager;
import lol.pyr.znpcsplus.conversion.DataImporter;
import lol.pyr.znpcsplus.entity.EntityPropertyRegistryImpl;
import lol.pyr.znpcsplus.npc.NpcEntryImpl;
import lol.pyr.znpcsplus.npc.NpcTypeRegistryImpl;
import lol.pyr.znpcsplus.packets.PacketFactory;
import lol.pyr.znpcsplus.scheduling.TaskScheduler;
import lol.pyr.znpcsplus.skin.cache.SkinCache;
import lol.pyr.znpcsplus.util.BungeeConnector;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.configuration.file.YamlConfiguration;
import java.io.File;
import java.util.Collection;
import java.util.Collections;
@SuppressWarnings("FieldCanBeLocal")
public class CitizensImporter implements DataImporter {
private final ConfigManager configManager;
private final BukkitAudiences adventure;
private final BungeeConnector bungeeConnector;
private final TaskScheduler scheduler;
private final PacketFactory packetFactory;
private final LegacyComponentSerializer textSerializer;
private final NpcTypeRegistryImpl typeRegistry;
private final EntityPropertyRegistryImpl propertyRegistry;
private final SkinCache skinCache;
private final File dataFile;
public CitizensImporter(ConfigManager configManager, BukkitAudiences adventure, BungeeConnector bungeeConnector,
TaskScheduler taskScheduler, PacketFactory packetFactory, LegacyComponentSerializer textSerializer,
NpcTypeRegistryImpl typeRegistry, EntityPropertyRegistryImpl propertyRegistry, SkinCache skinCache,
File dataFile) {
this.configManager = configManager;
this.adventure = adventure;
this.bungeeConnector = bungeeConnector;
this.scheduler = taskScheduler;
this.packetFactory = packetFactory;
this.textSerializer = textSerializer;
this.typeRegistry = typeRegistry;
this.propertyRegistry = propertyRegistry;
this.skinCache = skinCache;
this.dataFile = dataFile;
}
@Override
public Collection<NpcEntryImpl> importData() {
YamlConfiguration config = YamlConfiguration.loadConfiguration(dataFile);
// TODO
return Collections.emptyList();
}
@Override
public boolean isValid() {
return dataFile.isFile();
}
}

@ -1,6 +1,7 @@
package lol.pyr.znpcsplus.util; package lol.pyr.znpcsplus.util;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.util.io.BukkitObjectInputStream; import org.bukkit.util.io.BukkitObjectInputStream;
import org.bukkit.util.io.BukkitObjectOutputStream; import org.bukkit.util.io.BukkitObjectOutputStream;
@ -10,20 +11,21 @@ import java.io.IOException;
import java.util.Base64; import java.util.Base64;
public class ItemSerializationUtil { public class ItemSerializationUtil {
public static byte[] itemToBytes(ItemStack item) { public static byte[] objectToBytes(Object obj) {
ByteArrayOutputStream bout = new ByteArrayOutputStream(); ByteArrayOutputStream bout = new ByteArrayOutputStream();
try { try {
new BukkitObjectOutputStream(bout).writeObject(item); new BukkitObjectOutputStream(bout).writeObject(obj);
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
return bout.toByteArray(); return bout.toByteArray();
} }
public static ItemStack itemFromBytes(byte[] bytes) { @SuppressWarnings({"unchecked", "unused"})
public static <T> T objectFromBytes(byte[] bytes, Class<T> clazz) {
ByteArrayInputStream bin = new ByteArrayInputStream(bytes); ByteArrayInputStream bin = new ByteArrayInputStream(bytes);
try { try {
return (ItemStack) new BukkitObjectInputStream(bin).readObject(); return (T) new BukkitObjectInputStream(bin).readObject();
} catch (IOException | ClassNotFoundException e) { } catch (IOException | ClassNotFoundException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
@ -31,11 +33,15 @@ public class ItemSerializationUtil {
public static String itemToB64(ItemStack item) { public static String itemToB64(ItemStack item) {
if (item == null) return null; if (item == null) return null;
return Base64.getEncoder().encodeToString(itemToBytes(item)); return Base64.getEncoder().encodeToString(objectToBytes(item));
} }
public static ItemStack itemFromB64(String str) { public static ItemStack itemFromB64(String str) {
if (str == null) return null; if (str == null) return null;
return itemFromBytes(Base64.getDecoder().decode(str)); return objectFromBytes(Base64.getDecoder().decode(str), ItemStack.class);
}
public static ItemMeta metaFromB64(String str) {
return objectFromBytes(Base64.getDecoder().decode(str), ItemMeta.class);
} }
} }