From 1229a7de3e8617a776093783a27ff15ec206ab33 Mon Sep 17 00:00:00 2001 From: WolfyScript Date: Tue, 28 Dec 2021 17:01:48 +0100 Subject: [PATCH 01/30] Started work on extension packs --- .../utilities/extensions/ExtensionPack.java | 58 +++++++++++++++ .../utilities/extensions/PackMetaFile.java | 71 +++++++++++++++++++ .../examples/extensionpack/pack.json | 8 +++ 3 files changed, 137 insertions(+) create mode 100644 core/src/main/java/me/wolfyscript/utilities/extensions/ExtensionPack.java create mode 100644 core/src/main/java/me/wolfyscript/utilities/extensions/PackMetaFile.java create mode 100644 wolfyutilities/src/main/resources/examples/extensionpack/pack.json diff --git a/core/src/main/java/me/wolfyscript/utilities/extensions/ExtensionPack.java b/core/src/main/java/me/wolfyscript/utilities/extensions/ExtensionPack.java new file mode 100644 index 000000000..a6362b5ef --- /dev/null +++ b/core/src/main/java/me/wolfyscript/utilities/extensions/ExtensionPack.java @@ -0,0 +1,58 @@ +/* + * WolfyUtilities, APIs and Utilities for Minecraft Spigot plugins + * Copyright (C) 2021 WolfyScript + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package me.wolfyscript.utilities.extensions; + +import me.wolfyscript.utilities.util.json.jackson.JacksonUtil; + +import java.io.File; +import java.io.IOException; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; + +public class ExtensionPack { + + private static final int VERSION = 1; + + private final ZipFile file; + + ExtensionPack (ZipFile file) { + this.file = file; + } + + public static ExtensionPack readPack(String path) throws IOException { + File file = new File(path); + if (file.getName().endsWith(".zip")) { + ZipFile zipFile = new ZipFile(path); + ZipEntry packInfoEntry = zipFile.getEntry("pack.json"); + if(packInfoEntry != null && !packInfoEntry.isDirectory()) { + PackMetaFile metaFile = JacksonUtil.getObjectMapper().readValue(zipFile.getInputStream(packInfoEntry), PackMetaFile.class); + if (metaFile.getPack().getVersion() != VERSION) { + //Invalid version! + return null; + } + + + + return new ExtensionPack(zipFile); + } + } + return null; + } + +} diff --git a/core/src/main/java/me/wolfyscript/utilities/extensions/PackMetaFile.java b/core/src/main/java/me/wolfyscript/utilities/extensions/PackMetaFile.java new file mode 100644 index 000000000..c720905b5 --- /dev/null +++ b/core/src/main/java/me/wolfyscript/utilities/extensions/PackMetaFile.java @@ -0,0 +1,71 @@ +/* + * WolfyUtilities, APIs and Utilities for Minecraft Spigot plugins + * Copyright (C) 2021 WolfyScript + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package me.wolfyscript.utilities.extensions; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.util.List; + +public class PackMetaFile { + + private final PackInfo pack; + + @JsonCreator + PackMetaFile(@JsonProperty("pack") PackInfo pack) { + this.pack = pack; + } + + public PackInfo getPack() { + return pack; + } + + public static class PackInfo { + + private final int version; + private final String namespace; + private final List authors; + private final String description; + + @JsonCreator + PackInfo(@JsonProperty("version") int version, @JsonProperty("namespace") String namespace, @JsonProperty("authors") List authors, @JsonProperty("description") String description) { + this.version = version; + this.namespace = namespace; + this.authors = List.copyOf(authors); + this.description = description; + } + + public int getVersion() { + return version; + } + + public String getNamespace() { + return namespace; + } + + public List getAuthors() { + return authors; + } + + public String getDescription() { + return description; + } + } + +} diff --git a/wolfyutilities/src/main/resources/examples/extensionpack/pack.json b/wolfyutilities/src/main/resources/examples/extensionpack/pack.json new file mode 100644 index 000000000..65b49c573 --- /dev/null +++ b/wolfyutilities/src/main/resources/examples/extensionpack/pack.json @@ -0,0 +1,8 @@ +{ + "pack": { + "version": 1, + "namespace": " (Usually your lowercase plugin name. Spaces are not allowed! Must be unique from other packs, so make sure to pick a good name!)", + "authors": [], + "description": "" + } +} \ No newline at end of file From 0914fd909f342738a4bd25cb56546317fa3badf9 Mon Sep 17 00:00:00 2001 From: WolfyScript Date: Thu, 6 Jan 2022 14:58:48 +0100 Subject: [PATCH 02/30] Renamed extension to expansion --- .../ExtensionPack.java => expansions/ExpansionPack.java} | 4 ++-- .../utilities/{extensions => expansions}/PackMetaFile.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) rename core/src/main/java/me/wolfyscript/utilities/{extensions/ExtensionPack.java => expansions/ExpansionPack.java} (96%) rename core/src/main/java/me/wolfyscript/utilities/{extensions => expansions}/PackMetaFile.java (97%) diff --git a/core/src/main/java/me/wolfyscript/utilities/extensions/ExtensionPack.java b/core/src/main/java/me/wolfyscript/utilities/expansions/ExpansionPack.java similarity index 96% rename from core/src/main/java/me/wolfyscript/utilities/extensions/ExtensionPack.java rename to core/src/main/java/me/wolfyscript/utilities/expansions/ExpansionPack.java index a6362b5ef..e6306d5cd 100644 --- a/core/src/main/java/me/wolfyscript/utilities/extensions/ExtensionPack.java +++ b/core/src/main/java/me/wolfyscript/utilities/expansions/ExpansionPack.java @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -package me.wolfyscript.utilities.extensions; +package me.wolfyscript.utilities.expansions; import me.wolfyscript.utilities.util.json.jackson.JacksonUtil; @@ -25,7 +25,7 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipFile; -public class ExtensionPack { +public class ExpansionPack { private static final int VERSION = 1; diff --git a/core/src/main/java/me/wolfyscript/utilities/extensions/PackMetaFile.java b/core/src/main/java/me/wolfyscript/utilities/expansions/PackMetaFile.java similarity index 97% rename from core/src/main/java/me/wolfyscript/utilities/extensions/PackMetaFile.java rename to core/src/main/java/me/wolfyscript/utilities/expansions/PackMetaFile.java index c720905b5..8ff8b894d 100644 --- a/core/src/main/java/me/wolfyscript/utilities/extensions/PackMetaFile.java +++ b/core/src/main/java/me/wolfyscript/utilities/expansions/PackMetaFile.java @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -package me.wolfyscript.utilities.extensions; +package me.wolfyscript.utilities.expansions; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; From 7829f1e688d1e61a3cc30e172bf0877ad9340156 Mon Sep 17 00:00:00 2001 From: WolfyScript Date: Thu, 6 Jan 2022 14:59:10 +0100 Subject: [PATCH 03/30] Added ResourceLoader that can be registered to load specific resources from expansion packs. --- .../utilities/expansions/ResourceLoader.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 core/src/main/java/me/wolfyscript/utilities/expansions/ResourceLoader.java diff --git a/core/src/main/java/me/wolfyscript/utilities/expansions/ResourceLoader.java b/core/src/main/java/me/wolfyscript/utilities/expansions/ResourceLoader.java new file mode 100644 index 000000000..9336337cb --- /dev/null +++ b/core/src/main/java/me/wolfyscript/utilities/expansions/ResourceLoader.java @@ -0,0 +1,52 @@ +/* + * WolfyUtilities, APIs and Utilities for Minecraft Spigot plugins + * Copyright (C) 2021 WolfyScript + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package me.wolfyscript.utilities.expansions; + +import com.google.common.base.Preconditions; +import me.wolfyscript.utilities.api.WolfyUtilCore; +import me.wolfyscript.utilities.util.Keyed; +import me.wolfyscript.utilities.util.NamespacedKey; +import org.bukkit.plugin.Plugin; + +import java.util.Locale; +import java.util.zip.ZipEntry; + +public abstract class ResourceLoader implements Keyed { + + private final Plugin plugin; + private final NamespacedKey key; + + protected ResourceLoader(Plugin plugin, NamespacedKey namespacedKey) { + this.plugin = plugin; + String namespace = plugin.getName().toLowerCase(Locale.ROOT); + Preconditions.checkArgument(namespacedKey.getNamespace().equals(namespace), "The namespace must be equal to your plugin name! Expected " + namespace + " but got " + namespace); + this.key = namespacedKey; + } + + public Plugin getPlugin() { + return plugin; + } + + public abstract void load(ZipEntry entry, WolfyUtilCore core); + + @Override + public NamespacedKey getNamespacedKey() { + return key; + } +} From c886b2bda5bb342b639e71ba72f84e09f5767a71 Mon Sep 17 00:00:00 2001 From: WolfyScript Date: Thu, 6 Jan 2022 14:59:30 +0100 Subject: [PATCH 04/30] Added ResourceLoader registry --- .../utilities/registry/Registries.java | 9 ++++ .../registry/RegistryResourceLoader.java | 51 +++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 core/src/main/java/me/wolfyscript/utilities/registry/RegistryResourceLoader.java diff --git a/core/src/main/java/me/wolfyscript/utilities/registry/Registries.java b/core/src/main/java/me/wolfyscript/utilities/registry/Registries.java index e1b313b90..498a041e5 100644 --- a/core/src/main/java/me/wolfyscript/utilities/registry/Registries.java +++ b/core/src/main/java/me/wolfyscript/utilities/registry/Registries.java @@ -102,6 +102,9 @@ public > V getByKeyOfType(NamespacedKey key, Class reg private final TypeRegistry particleTimer; private final TypeRegistry customItemNbtChecks; + //Expansions + private final RegistryResourceLoader expansionLoaders; + public Registries(WolfyUtilCore core) { this.core = core; @@ -116,6 +119,8 @@ public Registries(WolfyUtilCore core) { particleShapes = new TypeRegistrySimple<>(new NamespacedKey(core, "particles/shapes"), this); particleTimer = new TypeRegistrySimple<>(new NamespacedKey(core, "particle_timers"), this); customItemNbtChecks = new TypeRegistrySimple<>(new NamespacedKey(core, "custom_item_nbt_checks"), this); + + expansionLoaders = new RegistryResourceLoader(this); } public WolfyUtilCore getCore() { @@ -197,4 +202,8 @@ public TypeRegistry getParticleAnimators() { public TypeRegistry getParticleTimer() { return particleTimer; } + + public RegistryResourceLoader getExpansionResourceLoaders() { + return expansionLoaders; + } } diff --git a/core/src/main/java/me/wolfyscript/utilities/registry/RegistryResourceLoader.java b/core/src/main/java/me/wolfyscript/utilities/registry/RegistryResourceLoader.java new file mode 100644 index 000000000..30e417975 --- /dev/null +++ b/core/src/main/java/me/wolfyscript/utilities/registry/RegistryResourceLoader.java @@ -0,0 +1,51 @@ +/* + * WolfyUtilities, APIs and Utilities for Minecraft Spigot plugins + * Copyright (C) 2021 WolfyScript + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package me.wolfyscript.utilities.registry; + +import me.wolfyscript.utilities.expansions.ResourceLoader; +import me.wolfyscript.utilities.util.NamespacedKey; + +import java.util.LinkedList; +import java.util.List; + +public class RegistryResourceLoader extends RegistrySimple { + + private final List registerOrder = new LinkedList<>(); + + public RegistryResourceLoader(Registries registries) { + super(new NamespacedKey(registries.getCore(), "expansions/loaders"), registries); + } + + @Override + public void register(ResourceLoader value) { + super.register(value); + if (!registerOrder.contains(value.getNamespacedKey())) { + registerOrder.add(value.getNamespacedKey()); + } + } + + @Override + public void register(NamespacedKey namespacedKey, ResourceLoader value) { + super.register(namespacedKey, value); + } + + public List getRegisterOrder() { + return List.copyOf(registerOrder); + } +} From 3b740e63d7108f0ff5035a018508ecd3ad8de938 Mon Sep 17 00:00:00 2001 From: WolfyScript Date: Thu, 6 Jan 2022 15:00:17 +0100 Subject: [PATCH 05/30] Added ResourceLoaderParticleEffects as a prototype to load particle effects. --- .../main/ResourceLoaderParticleEffects.java | 37 +++++++++++++++++++ .../wolfyscript/utilities/main/WUPlugin.java | 4 ++ 2 files changed, 41 insertions(+) create mode 100644 wolfyutilities/src/main/java/me/wolfyscript/utilities/main/ResourceLoaderParticleEffects.java diff --git a/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/ResourceLoaderParticleEffects.java b/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/ResourceLoaderParticleEffects.java new file mode 100644 index 000000000..d37b5760e --- /dev/null +++ b/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/ResourceLoaderParticleEffects.java @@ -0,0 +1,37 @@ +/* + * WolfyUtilities, APIs and Utilities for Minecraft Spigot plugins + * Copyright (C) 2021 WolfyScript + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package me.wolfyscript.utilities.main; + +import me.wolfyscript.utilities.api.WolfyUtilCore; +import me.wolfyscript.utilities.expansions.ResourceLoader; +import me.wolfyscript.utilities.util.NamespacedKey; + +import java.util.zip.ZipEntry; + +public class ResourceLoaderParticleEffects extends ResourceLoader { + + protected ResourceLoaderParticleEffects(WUPlugin plugin) { + super(plugin, new NamespacedKey(plugin, "particles/effects")); + } + + @Override + public void load(ZipEntry entry, WolfyUtilCore core) { + core.getLogger().info("effect: " + entry.getName()); + } +} diff --git a/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/WUPlugin.java b/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/WUPlugin.java index ddc6f0b6e..f8174bbc9 100644 --- a/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/WUPlugin.java +++ b/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/WUPlugin.java @@ -45,6 +45,7 @@ import me.wolfyscript.utilities.api.language.Language; import me.wolfyscript.utilities.compatibility.CompatibilityManager; import me.wolfyscript.utilities.compatibility.CompatibilityManagerImpl; +import me.wolfyscript.utilities.expansions.ExpansionManager; import me.wolfyscript.utilities.main.commands.ChatActionCommand; import me.wolfyscript.utilities.main.commands.InputCommand; import me.wolfyscript.utilities.main.commands.SpawnParticleAnimationCommand; @@ -218,6 +219,9 @@ public void onLoad() { KeyedTypeIdResolver.registerTypeRegistry(Animator.class, particleAnimators); KeyedTypeIdResolver.registerTypeRegistry(Shape.class, particleShapes); KeyedTypeIdResolver.registerTypeRegistry(Timer.class, particleTimers); + + var expansionLoaders = getRegistries().getExpansionResourceLoaders(); + expansionLoaders.register(new ResourceLoaderParticleEffects(this)); } @Override From 905cfaba2590920c863eee91db858cb4e3f10cd0 Mon Sep 17 00:00:00 2001 From: WolfyScript Date: Thu, 6 Jan 2022 15:01:59 +0100 Subject: [PATCH 06/30] ExpansionPack#load, loads the data from the pack using the registered ResourceLoaders. The register order is preserved and the resources loaded accordingly. --- .../utilities/expansions/ExpansionPack.java | 58 ++++++++++++------- 1 file changed, 38 insertions(+), 20 deletions(-) diff --git a/core/src/main/java/me/wolfyscript/utilities/expansions/ExpansionPack.java b/core/src/main/java/me/wolfyscript/utilities/expansions/ExpansionPack.java index e6306d5cd..b5f1e2ca4 100644 --- a/core/src/main/java/me/wolfyscript/utilities/expansions/ExpansionPack.java +++ b/core/src/main/java/me/wolfyscript/utilities/expansions/ExpansionPack.java @@ -18,41 +18,59 @@ package me.wolfyscript.utilities.expansions; -import me.wolfyscript.utilities.util.json.jackson.JacksonUtil; +import me.wolfyscript.utilities.api.WolfyUtilCore; +import me.wolfyscript.utilities.registry.RegistryResourceLoader; +import me.wolfyscript.utilities.util.NamespacedKey; import java.io.File; import java.io.IOException; -import java.util.zip.ZipEntry; +import java.util.List; import java.util.zip.ZipFile; public class ExpansionPack { - private static final int VERSION = 1; + static final int VERSION = 1; - private final ZipFile file; + private final WolfyUtilCore core; + private final File file; + private final List zipEntryNames; + private final String rootDir; - ExtensionPack (ZipFile file) { + ExpansionPack(File file, List entryNames, WolfyUtilCore core) { this.file = file; + this.core = core; + this.zipEntryNames = entryNames; + this.rootDir = entryNames.get(0); } - public static ExtensionPack readPack(String path) throws IOException { - File file = new File(path); - if (file.getName().endsWith(".zip")) { - ZipFile zipFile = new ZipFile(path); - ZipEntry packInfoEntry = zipFile.getEntry("pack.json"); - if(packInfoEntry != null && !packInfoEntry.isDirectory()) { - PackMetaFile metaFile = JacksonUtil.getObjectMapper().readValue(zipFile.getInputStream(packInfoEntry), PackMetaFile.class); - if (metaFile.getPack().getVersion() != VERSION) { - //Invalid version! - return null; + public void load(RegistryResourceLoader registry, List loaderOrder) throws IOException { + var zipFile = new ZipFile(file); + try (zipFile) { + for (NamespacedKey namespacedKey : loaderOrder) { + var loader = registry.get(namespacedKey); + if (loader != null) { + String loaderRoot = rootDir + namespacedKey.getNamespace() + "/" + namespacedKey.getKey() + "/"; //Get the root of the loader + int rootIndex = zipEntryNames.indexOf(loaderRoot); // Get the index of that root in the zip file. + if (rootIndex == -1) { + continue; // The loader root is not in the zip file. + } + for (int i = rootIndex; i < zipEntryNames.size(); i++) { + String entryName = zipEntryNames.get(i); + if (!entryName.startsWith(loaderRoot)) { + break; + } + var entry = zipFile.getEntry(entryName); + if (entry != null) { + // Call the loader specific load method + loader.load(entry, core); + } + } } - - - - return new ExtensionPack(zipFile); } } - return null; } + public File getFile() { + return file; + } } From a58ea20f80b77d37fc053312b553e55d3299e77f Mon Sep 17 00:00:00 2001 From: WolfyScript Date: Thu, 6 Jan 2022 15:02:40 +0100 Subject: [PATCH 07/30] Added ExpansionManager that initializes, loads and manages the expansions. --- .../expansions/ExpansionManager.java | 99 +++++++++++++++++++ .../wolfyscript/utilities/main/WUPlugin.java | 6 ++ 2 files changed, 105 insertions(+) create mode 100644 core/src/main/java/me/wolfyscript/utilities/expansions/ExpansionManager.java diff --git a/core/src/main/java/me/wolfyscript/utilities/expansions/ExpansionManager.java b/core/src/main/java/me/wolfyscript/utilities/expansions/ExpansionManager.java new file mode 100644 index 000000000..b6dc3c093 --- /dev/null +++ b/core/src/main/java/me/wolfyscript/utilities/expansions/ExpansionManager.java @@ -0,0 +1,99 @@ +/* + * WolfyUtilities, APIs and Utilities for Minecraft Spigot plugins + * Copyright (C) 2021 WolfyScript + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package me.wolfyscript.utilities.expansions; + +import me.wolfyscript.utilities.api.WolfyUtilCore; +import me.wolfyscript.utilities.util.NamespacedKey; +import me.wolfyscript.utilities.util.json.jackson.JacksonUtil; + +import java.io.File; +import java.io.IOException; +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; + +public class ExpansionManager { + + private final WolfyUtilCore core; + private final File packsFolder; + + private List packs; + + public ExpansionManager(WolfyUtilCore core) { + this.core = core; + this.packsFolder = new File(core.getDataFolder(), "expansion_packs"); + this.packs = new LinkedList<>(); + } + + public void initPacks() { + core.getLogger().info("Initiating expansion packs..."); + File[] files = packsFolder.listFiles((dir, name) -> name.endsWith(".zip")); + if (files != null) { + this.packs = Arrays.stream(files).map(file -> { + try { + return initPack(file); + } catch (IOException e) { + core.getLogger().warning("Failed to read expansion zip file " + file.getName() + ": " + e.getMessage()); + } + return null; + }).filter(Objects::nonNull).collect(Collectors.toCollection(LinkedList::new)); + } + } + + public void loadPacks() { + core.getLogger().info("Loading expansion packs..."); + var registry = core.getRegistries().getExpansionResourceLoaders(); + List order = registry.getRegisterOrder(); + for (ExpansionPack pack : packs) { + try { + pack.load(registry, order); + } catch (IOException e) { + core.getLogger().warning("Failed to read expansion zip file " + pack.getFile().getName() + ": " + e.getMessage()); + } + } + } + + public ExpansionPack initPack(File file) throws IOException { + ZipFile zipFile = new ZipFile(file); + try (zipFile) { + if (zipFile.entries().hasMoreElements()) { + List entryNames = zipFile.stream().map(ZipEntry::getName).toList(); + ZipEntry packInfoEntry = zipFile.getEntry(entryNames.get(0) + "pack.json"); + if (packInfoEntry != null && !packInfoEntry.isDirectory()) { + PackMetaFile metaFile = JacksonUtil.getObjectMapper().readValue(zipFile.getInputStream(packInfoEntry), PackMetaFile.class); + if (metaFile.getPack().getVersion() != ExpansionPack.VERSION) { + //Invalid version! + return null; + } + core.getLogger().info("Initiated expansion pack " + file.getName()); + return new ExpansionPack(file, entryNames, core); + } + } + } + return null; + } + + public List getPacks() { + return List.copyOf(packs); + } +} diff --git a/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/WUPlugin.java b/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/WUPlugin.java index f8174bbc9..b00d01663 100644 --- a/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/WUPlugin.java +++ b/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/WUPlugin.java @@ -114,6 +114,7 @@ public final class WUPlugin extends WolfyUtilCore { private final MessageHandler messageHandler; private final MessageFactory messageFactory; private final CompatibilityManagerImpl compatibilityManager; + private final ExpansionManager expansionManager; public WUPlugin() { super(); @@ -124,6 +125,7 @@ public WUPlugin() { this.messageHandler = new MessageHandler(this); this.messageFactory = new MessageFactory(this); this.compatibilityManager = new CompatibilityManagerImpl(this); + this.expansionManager = new ExpansionManager(this); } private WUPlugin(JavaPluginLoader loader, PluginDescriptionFile description, File dataFolder, File file) { @@ -135,6 +137,7 @@ private WUPlugin(JavaPluginLoader loader, PluginDescriptionFile description, Fil this.messageHandler = new MessageHandler(this); this.messageFactory = new MessageFactory(this); this.compatibilityManager = new CompatibilityManagerImpl(this); + this.expansionManager = new ExpansionManager(this); } @Deprecated @@ -237,6 +240,9 @@ public void onEnable() { registerAPIReference(new VanillaRef.Parser()); registerAPIReference(new WolfyUtilitiesRef.Parser()); + expansionManager.initPacks(); + expansionManager.loadPacks(); + var languageAPI = api.getLanguageAPI(); saveResource("lang/en_US.json", true); languageAPI.setActiveLanguage(new Language(this, "en_US")); From ef7bce53c567ef15d60febb83609a23ce4c756dd Mon Sep 17 00:00:00 2001 From: WolfyScript Date: Thu, 6 Jan 2022 19:05:54 +0100 Subject: [PATCH 08/30] ExpansionManager will ignore duplicate packs. --- .../utilities/expansions/ExpansionManager.java | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/me/wolfyscript/utilities/expansions/ExpansionManager.java b/core/src/main/java/me/wolfyscript/utilities/expansions/ExpansionManager.java index b6dc3c093..c545f4a2b 100644 --- a/core/src/main/java/me/wolfyscript/utilities/expansions/ExpansionManager.java +++ b/core/src/main/java/me/wolfyscript/utilities/expansions/ExpansionManager.java @@ -49,14 +49,17 @@ public void initPacks() { core.getLogger().info("Initiating expansion packs..."); File[] files = packsFolder.listFiles((dir, name) -> name.endsWith(".zip")); if (files != null) { - this.packs = Arrays.stream(files).map(file -> { + this.packs = new LinkedList<>(); + for (File file : files) { try { - return initPack(file); + var pack = initPack(file); + if (pack != null) { + packs.add(pack); + } } catch (IOException e) { core.getLogger().warning("Failed to read expansion zip file " + file.getName() + ": " + e.getMessage()); } - return null; - }).filter(Objects::nonNull).collect(Collectors.toCollection(LinkedList::new)); + } } } @@ -85,8 +88,13 @@ public ExpansionPack initPack(File file) throws IOException { //Invalid version! return null; } + ExpansionPack pack = new ExpansionPack(metaFile, file, entryNames, core); + if (packs.contains(pack)) { + core.getLogger().warning("Expansion Pack already initialised: \"" + metaFile.getPack().getNamespace() +" \" in file " + file.getName()); + return null; + } core.getLogger().info("Initiated expansion pack " + file.getName()); - return new ExpansionPack(file, entryNames, core); + return pack; } } } From e17f50daa1a1c12cef7dce19a6227f49b6b53c2b Mon Sep 17 00:00:00 2001 From: WolfyScript Date: Thu, 6 Jan 2022 19:06:54 +0100 Subject: [PATCH 09/30] Added more arguments to ResourceLoader#load --- .../utilities/expansions/ExpansionPack.java | 33 +++++++++-- .../utilities/expansions/ResourceLoader.java | 5 +- .../main/ResourceLoaderParticleEffects.java | 37 ------------ .../ResourceLoaderParticleEffects.java | 56 +++++++++++++++++++ 4 files changed, 89 insertions(+), 42 deletions(-) delete mode 100644 wolfyutilities/src/main/java/me/wolfyscript/utilities/main/ResourceLoaderParticleEffects.java create mode 100644 wolfyutilities/src/main/java/me/wolfyscript/utilities/main/expansions/ResourceLoaderParticleEffects.java diff --git a/core/src/main/java/me/wolfyscript/utilities/expansions/ExpansionPack.java b/core/src/main/java/me/wolfyscript/utilities/expansions/ExpansionPack.java index b5f1e2ca4..cff999459 100644 --- a/core/src/main/java/me/wolfyscript/utilities/expansions/ExpansionPack.java +++ b/core/src/main/java/me/wolfyscript/utilities/expansions/ExpansionPack.java @@ -21,6 +21,7 @@ import me.wolfyscript.utilities.api.WolfyUtilCore; import me.wolfyscript.utilities.registry.RegistryResourceLoader; import me.wolfyscript.utilities.util.NamespacedKey; +import org.jetbrains.annotations.NotNull; import java.io.File; import java.io.IOException; @@ -32,15 +33,17 @@ public class ExpansionPack { static final int VERSION = 1; private final WolfyUtilCore core; + private final PackMetaFile meta; private final File file; private final List zipEntryNames; private final String rootDir; - ExpansionPack(File file, List entryNames, WolfyUtilCore core) { + ExpansionPack(@NotNull PackMetaFile meta, File file, List entryNames, WolfyUtilCore core) { this.file = file; this.core = core; this.zipEntryNames = entryNames; this.rootDir = entryNames.get(0); + this.meta = meta; } public void load(RegistryResourceLoader registry, List loaderOrder) throws IOException { @@ -49,12 +52,13 @@ public void load(RegistryResourceLoader registry, List loaderOrde for (NamespacedKey namespacedKey : loaderOrder) { var loader = registry.get(namespacedKey); if (loader != null) { - String loaderRoot = rootDir + namespacedKey.getNamespace() + "/" + namespacedKey.getKey() + "/"; //Get the root of the loader + String loaderRoot = rootDir + loader.folderPath; //Get the root of the loader int rootIndex = zipEntryNames.indexOf(loaderRoot); // Get the index of that root in the zip file. if (rootIndex == -1) { continue; // The loader root is not in the zip file. } - for (int i = rootIndex; i < zipEntryNames.size(); i++) { + //We start one index later, as the first one is always the root folder + for (int i = rootIndex + 1; i < zipEntryNames.size(); i++) { String entryName = zipEntryNames.get(i); if (!entryName.startsWith(loaderRoot)) { break; @@ -62,7 +66,12 @@ public void load(RegistryResourceLoader registry, List loaderOrde var entry = zipFile.getEntry(entryName); if (entry != null) { // Call the loader specific load method - loader.load(entry, core); + try { + loader.load(this, zipFile, rootDir, entry, core); + } catch (Exception e) { + // We don't want a faulty loader to crash the whole loading process and/or plugin! + e.printStackTrace(); + } } } } @@ -70,7 +79,23 @@ public void load(RegistryResourceLoader registry, List loaderOrde } } + public PackMetaFile getMeta() { + return meta; + } + public File getFile() { return file; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof ExpansionPack pack)) return false; + return meta.getPack().getNamespace().equals(pack.meta.getPack().getNamespace()); + } + + @Override + public int hashCode() { + return meta.getPack().getNamespace().hashCode(); + } } diff --git a/core/src/main/java/me/wolfyscript/utilities/expansions/ResourceLoader.java b/core/src/main/java/me/wolfyscript/utilities/expansions/ResourceLoader.java index 9336337cb..40394cde5 100644 --- a/core/src/main/java/me/wolfyscript/utilities/expansions/ResourceLoader.java +++ b/core/src/main/java/me/wolfyscript/utilities/expansions/ResourceLoader.java @@ -26,24 +26,27 @@ import java.util.Locale; import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; public abstract class ResourceLoader implements Keyed { private final Plugin plugin; private final NamespacedKey key; + protected final String folderPath; protected ResourceLoader(Plugin plugin, NamespacedKey namespacedKey) { this.plugin = plugin; String namespace = plugin.getName().toLowerCase(Locale.ROOT); Preconditions.checkArgument(namespacedKey.getNamespace().equals(namespace), "The namespace must be equal to your plugin name! Expected " + namespace + " but got " + namespace); this.key = namespacedKey; + this.folderPath = key.toString("/") + "/"; } public Plugin getPlugin() { return plugin; } - public abstract void load(ZipEntry entry, WolfyUtilCore core); + public abstract void load(ExpansionPack pack, ZipFile zipFile, String root, ZipEntry entry, WolfyUtilCore core); @Override public NamespacedKey getNamespacedKey() { diff --git a/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/ResourceLoaderParticleEffects.java b/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/ResourceLoaderParticleEffects.java deleted file mode 100644 index d37b5760e..000000000 --- a/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/ResourceLoaderParticleEffects.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * WolfyUtilities, APIs and Utilities for Minecraft Spigot plugins - * Copyright (C) 2021 WolfyScript - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package me.wolfyscript.utilities.main; - -import me.wolfyscript.utilities.api.WolfyUtilCore; -import me.wolfyscript.utilities.expansions.ResourceLoader; -import me.wolfyscript.utilities.util.NamespacedKey; - -import java.util.zip.ZipEntry; - -public class ResourceLoaderParticleEffects extends ResourceLoader { - - protected ResourceLoaderParticleEffects(WUPlugin plugin) { - super(plugin, new NamespacedKey(plugin, "particles/effects")); - } - - @Override - public void load(ZipEntry entry, WolfyUtilCore core) { - core.getLogger().info("effect: " + entry.getName()); - } -} diff --git a/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/expansions/ResourceLoaderParticleEffects.java b/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/expansions/ResourceLoaderParticleEffects.java new file mode 100644 index 000000000..f8f224ccb --- /dev/null +++ b/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/expansions/ResourceLoaderParticleEffects.java @@ -0,0 +1,56 @@ +/* + * WolfyUtilities, APIs and Utilities for Minecraft Spigot plugins + * Copyright (C) 2021 WolfyScript + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package me.wolfyscript.utilities.main.expansions; + +import me.wolfyscript.utilities.api.WolfyUtilCore; +import me.wolfyscript.utilities.expansions.ExpansionPack; +import me.wolfyscript.utilities.expansions.ResourceLoader; +import me.wolfyscript.utilities.main.WUPlugin; +import me.wolfyscript.utilities.util.NamespacedKey; +import me.wolfyscript.utilities.util.json.jackson.JacksonUtil; +import me.wolfyscript.utilities.util.particles.ParticleEffect; + +import java.io.BufferedInputStream; +import java.io.IOException; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; + +public class ResourceLoaderParticleEffects extends ResourceLoader { + + public ResourceLoaderParticleEffects(WUPlugin plugin) { + super(plugin, new NamespacedKey(plugin, "particles/effects")); + } + + @Override + public void load(ExpansionPack pack, ZipFile zipFile, String root, ZipEntry entry, WolfyUtilCore core) { + if (entry.isDirectory()) { + //We can't read from a directory! + return; + } + String path = entry.getName().replace(root + folderPath, "").replace(".json", ""); // This the key of the effect. + try (var stream = new BufferedInputStream(zipFile.getInputStream(entry))){ + var effect = JacksonUtil.getObjectMapper().readValue(stream, ParticleEffect.class); + if (effect != null) { + core.getRegistries().getParticleEffects().register(new NamespacedKey(pack.getMeta().getPack().getNamespace(), path), effect); + } + } catch (IOException e) { + core.getLogger().info("Failed to load effect \"" + path + "\"! Cause:" + e.getMessage()); + } + } +} From 541fd72d2e5c02a2e3c3cc599b38e39b2bf52c8f Mon Sep 17 00:00:00 2001 From: WolfyScript Date: Thu, 6 Jan 2022 19:07:21 +0100 Subject: [PATCH 10/30] Added ResourceLoaderParticleAnimations to load particle animations. --- .../wolfyscript/utilities/main/WUPlugin.java | 3 + .../ResourceLoaderParticleAnimations.java | 58 +++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 wolfyutilities/src/main/java/me/wolfyscript/utilities/main/expansions/ResourceLoaderParticleAnimations.java diff --git a/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/WUPlugin.java b/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/WUPlugin.java index b00d01663..2e35f505e 100644 --- a/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/WUPlugin.java +++ b/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/WUPlugin.java @@ -51,6 +51,8 @@ import me.wolfyscript.utilities.main.commands.SpawnParticleAnimationCommand; import me.wolfyscript.utilities.main.commands.SpawnParticleEffectCommand; import me.wolfyscript.utilities.main.configs.WUConfig; +import me.wolfyscript.utilities.main.expansions.ResourceLoaderParticleAnimations; +import me.wolfyscript.utilities.main.expansions.ResourceLoaderParticleEffects; import me.wolfyscript.utilities.main.listeners.BlockListener; import me.wolfyscript.utilities.main.listeners.EquipListener; import me.wolfyscript.utilities.main.listeners.GUIInventoryListener; @@ -225,6 +227,7 @@ public void onLoad() { var expansionLoaders = getRegistries().getExpansionResourceLoaders(); expansionLoaders.register(new ResourceLoaderParticleEffects(this)); + expansionLoaders.register(new ResourceLoaderParticleAnimations(this)); } @Override diff --git a/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/expansions/ResourceLoaderParticleAnimations.java b/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/expansions/ResourceLoaderParticleAnimations.java new file mode 100644 index 000000000..b09a3d745 --- /dev/null +++ b/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/expansions/ResourceLoaderParticleAnimations.java @@ -0,0 +1,58 @@ +/* + * WolfyUtilities, APIs and Utilities for Minecraft Spigot plugins + * Copyright (C) 2021 WolfyScript + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package me.wolfyscript.utilities.main.expansions; + +import me.wolfyscript.utilities.api.WolfyUtilCore; +import me.wolfyscript.utilities.expansions.ExpansionPack; +import me.wolfyscript.utilities.expansions.ResourceLoader; +import me.wolfyscript.utilities.main.WUPlugin; +import me.wolfyscript.utilities.util.NamespacedKey; +import me.wolfyscript.utilities.util.json.jackson.JacksonUtil; +import me.wolfyscript.utilities.util.particles.ParticleAnimation; +import me.wolfyscript.utilities.util.particles.ParticleEffect; + +import java.io.BufferedInputStream; +import java.io.IOException; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; + +public class ResourceLoaderParticleAnimations extends ResourceLoader { + + public ResourceLoaderParticleAnimations(WUPlugin plugin) { + super(plugin, new NamespacedKey(plugin, "particles/animations")); + } + + @Override + public void load(ExpansionPack pack, ZipFile zipFile, String root, ZipEntry entry, WolfyUtilCore core) { + if (entry.isDirectory()) { + //We can't read from a directory! + return; + } + String path = entry.getName().replace(root + folderPath, ""); + path = path.substring(0, path.lastIndexOf(".")); // This the key of the effect. + try (var stream = new BufferedInputStream(zipFile.getInputStream(entry))){ + var effect = JacksonUtil.getObjectMapper().readValue(stream, ParticleAnimation.class); + if (effect != null) { + core.getRegistries().getParticleAnimations().register(new NamespacedKey(pack.getMeta().getPack().getNamespace(), path), effect); + } + } catch (IOException e) { + core.getLogger().info("Failed to load animation \"" + path + "\"! Cause:" + e.getMessage()); + } + } +} From 0a03fc9c1e06d9d1305ace9e784356ec970b32f6 Mon Sep 17 00:00:00 2001 From: WolfyScript Date: Thu, 6 Jan 2022 19:41:10 +0100 Subject: [PATCH 11/30] Added ResourceLoaderJson to make it easier to load data from json zip entries. --- .../expansions/ResourceLoaderJson.java | 60 +++++++++++++++++++ .../utilities/registry/Registries.java | 1 - 2 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 core/src/main/java/me/wolfyscript/utilities/expansions/ResourceLoaderJson.java diff --git a/core/src/main/java/me/wolfyscript/utilities/expansions/ResourceLoaderJson.java b/core/src/main/java/me/wolfyscript/utilities/expansions/ResourceLoaderJson.java new file mode 100644 index 000000000..c29ede4a4 --- /dev/null +++ b/core/src/main/java/me/wolfyscript/utilities/expansions/ResourceLoaderJson.java @@ -0,0 +1,60 @@ +/* + * WolfyUtilities, APIs and Utilities for Minecraft Spigot plugins + * Copyright (C) 2021 WolfyScript + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package me.wolfyscript.utilities.expansions; + +import me.wolfyscript.utilities.api.WolfyUtilCore; +import me.wolfyscript.utilities.util.Keyed; +import me.wolfyscript.utilities.util.NamespacedKey; +import me.wolfyscript.utilities.util.json.jackson.JacksonUtil; +import org.bukkit.plugin.Plugin; + +import java.io.BufferedInputStream; +import java.io.IOException; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; + +public abstract class ResourceLoaderJson extends ResourceLoader { + + private final Class type; + + protected ResourceLoaderJson(Plugin plugin, NamespacedKey namespacedKey, Class type) { + super(plugin, namespacedKey); + this.type = type; + } + + public abstract void register(NamespacedKey namespacedKey, T value, WolfyUtilCore core); + + @Override + public void load(ExpansionPack pack, ZipFile zipFile, String root, ZipEntry entry, WolfyUtilCore core) { + if (entry.isDirectory()) { + //We can't read from a directory! + return; + } + String path = entry.getName().replace(root + folderPath, ""); + path = path.substring(0, path.lastIndexOf(".")); // This the key of the effect. + try (var stream = new BufferedInputStream(zipFile.getInputStream(entry))) { + var item = JacksonUtil.getObjectMapper().readValue(stream, type); + if (item != null) { + this.register(new NamespacedKey(pack.getMeta().getPack().getNamespace(), path), item, core); + } + } catch (IOException e) { + core.getLogger().info("Failed to load animation \"" + path + "\"! Cause:" + e.getMessage()); + } + } +} diff --git a/core/src/main/java/me/wolfyscript/utilities/registry/Registries.java b/core/src/main/java/me/wolfyscript/utilities/registry/Registries.java index 498a041e5..fad1e95ef 100644 --- a/core/src/main/java/me/wolfyscript/utilities/registry/Registries.java +++ b/core/src/main/java/me/wolfyscript/utilities/registry/Registries.java @@ -101,7 +101,6 @@ public > V getByKeyOfType(NamespacedKey key, Class reg private final TypeRegistry particleShapes; private final TypeRegistry particleTimer; private final TypeRegistry customItemNbtChecks; - //Expansions private final RegistryResourceLoader expansionLoaders; From 6780dc4c0c9ace8e489074f44ca670195e1a75d8 Mon Sep 17 00:00:00 2001 From: WolfyScript Date: Thu, 6 Jan 2022 19:41:46 +0100 Subject: [PATCH 12/30] Updated existing resource loaders to resourceLoaderJson. Renamed package --- .../wolfyscript/utilities/main/WUPlugin.java | 4 +- .../ResourceLoaderParticleAnimations.java | 58 ------------------- .../ResourceLoaderParticleAnimations.java | 37 ++++++++++++ .../ResourceLoaderParticleEffects.java | 23 ++------ 4 files changed, 45 insertions(+), 77 deletions(-) delete mode 100644 wolfyutilities/src/main/java/me/wolfyscript/utilities/main/expansions/ResourceLoaderParticleAnimations.java create mode 100644 wolfyutilities/src/main/java/me/wolfyscript/utilities/main/resource_loader/ResourceLoaderParticleAnimations.java rename wolfyutilities/src/main/java/me/wolfyscript/utilities/main/{expansions => resource_loader}/ResourceLoaderParticleEffects.java (63%) diff --git a/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/WUPlugin.java b/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/WUPlugin.java index 2e35f505e..1daadb96c 100644 --- a/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/WUPlugin.java +++ b/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/WUPlugin.java @@ -51,8 +51,8 @@ import me.wolfyscript.utilities.main.commands.SpawnParticleAnimationCommand; import me.wolfyscript.utilities.main.commands.SpawnParticleEffectCommand; import me.wolfyscript.utilities.main.configs.WUConfig; -import me.wolfyscript.utilities.main.expansions.ResourceLoaderParticleAnimations; -import me.wolfyscript.utilities.main.expansions.ResourceLoaderParticleEffects; +import me.wolfyscript.utilities.main.resource_loader.ResourceLoaderParticleAnimations; +import me.wolfyscript.utilities.main.resource_loader.ResourceLoaderParticleEffects; import me.wolfyscript.utilities.main.listeners.BlockListener; import me.wolfyscript.utilities.main.listeners.EquipListener; import me.wolfyscript.utilities.main.listeners.GUIInventoryListener; diff --git a/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/expansions/ResourceLoaderParticleAnimations.java b/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/expansions/ResourceLoaderParticleAnimations.java deleted file mode 100644 index b09a3d745..000000000 --- a/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/expansions/ResourceLoaderParticleAnimations.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * WolfyUtilities, APIs and Utilities for Minecraft Spigot plugins - * Copyright (C) 2021 WolfyScript - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package me.wolfyscript.utilities.main.expansions; - -import me.wolfyscript.utilities.api.WolfyUtilCore; -import me.wolfyscript.utilities.expansions.ExpansionPack; -import me.wolfyscript.utilities.expansions.ResourceLoader; -import me.wolfyscript.utilities.main.WUPlugin; -import me.wolfyscript.utilities.util.NamespacedKey; -import me.wolfyscript.utilities.util.json.jackson.JacksonUtil; -import me.wolfyscript.utilities.util.particles.ParticleAnimation; -import me.wolfyscript.utilities.util.particles.ParticleEffect; - -import java.io.BufferedInputStream; -import java.io.IOException; -import java.util.zip.ZipEntry; -import java.util.zip.ZipFile; - -public class ResourceLoaderParticleAnimations extends ResourceLoader { - - public ResourceLoaderParticleAnimations(WUPlugin plugin) { - super(plugin, new NamespacedKey(plugin, "particles/animations")); - } - - @Override - public void load(ExpansionPack pack, ZipFile zipFile, String root, ZipEntry entry, WolfyUtilCore core) { - if (entry.isDirectory()) { - //We can't read from a directory! - return; - } - String path = entry.getName().replace(root + folderPath, ""); - path = path.substring(0, path.lastIndexOf(".")); // This the key of the effect. - try (var stream = new BufferedInputStream(zipFile.getInputStream(entry))){ - var effect = JacksonUtil.getObjectMapper().readValue(stream, ParticleAnimation.class); - if (effect != null) { - core.getRegistries().getParticleAnimations().register(new NamespacedKey(pack.getMeta().getPack().getNamespace(), path), effect); - } - } catch (IOException e) { - core.getLogger().info("Failed to load animation \"" + path + "\"! Cause:" + e.getMessage()); - } - } -} diff --git a/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/resource_loader/ResourceLoaderParticleAnimations.java b/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/resource_loader/ResourceLoaderParticleAnimations.java new file mode 100644 index 000000000..cf6503b22 --- /dev/null +++ b/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/resource_loader/ResourceLoaderParticleAnimations.java @@ -0,0 +1,37 @@ +/* + * WolfyUtilities, APIs and Utilities for Minecraft Spigot plugins + * Copyright (C) 2021 WolfyScript + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package me.wolfyscript.utilities.main.resource_loader; + +import me.wolfyscript.utilities.api.WolfyUtilCore; +import me.wolfyscript.utilities.expansions.ResourceLoaderJson; +import me.wolfyscript.utilities.main.WUPlugin; +import me.wolfyscript.utilities.util.NamespacedKey; +import me.wolfyscript.utilities.util.particles.ParticleAnimation; + +public class ResourceLoaderParticleAnimations extends ResourceLoaderJson { + + public ResourceLoaderParticleAnimations(WUPlugin plugin) { + super(plugin, new NamespacedKey(plugin, "particles/animations"), ParticleAnimation.class); + } + + @Override + public void register(NamespacedKey namespacedKey, ParticleAnimation value, WolfyUtilCore core) { + core.getRegistries().getParticleAnimations().register(namespacedKey, value); + } +} diff --git a/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/expansions/ResourceLoaderParticleEffects.java b/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/resource_loader/ResourceLoaderParticleEffects.java similarity index 63% rename from wolfyutilities/src/main/java/me/wolfyscript/utilities/main/expansions/ResourceLoaderParticleEffects.java rename to wolfyutilities/src/main/java/me/wolfyscript/utilities/main/resource_loader/ResourceLoaderParticleEffects.java index f8f224ccb..db4f0e7a0 100644 --- a/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/expansions/ResourceLoaderParticleEffects.java +++ b/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/resource_loader/ResourceLoaderParticleEffects.java @@ -16,11 +16,12 @@ * along with this program. If not, see . */ -package me.wolfyscript.utilities.main.expansions; +package me.wolfyscript.utilities.main.resource_loader; import me.wolfyscript.utilities.api.WolfyUtilCore; import me.wolfyscript.utilities.expansions.ExpansionPack; import me.wolfyscript.utilities.expansions.ResourceLoader; +import me.wolfyscript.utilities.expansions.ResourceLoaderJson; import me.wolfyscript.utilities.main.WUPlugin; import me.wolfyscript.utilities.util.NamespacedKey; import me.wolfyscript.utilities.util.json.jackson.JacksonUtil; @@ -31,26 +32,14 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipFile; -public class ResourceLoaderParticleEffects extends ResourceLoader { +public class ResourceLoaderParticleEffects extends ResourceLoaderJson { public ResourceLoaderParticleEffects(WUPlugin plugin) { - super(plugin, new NamespacedKey(plugin, "particles/effects")); + super(plugin, new NamespacedKey(plugin, "particles/effects"), ParticleEffect.class); } @Override - public void load(ExpansionPack pack, ZipFile zipFile, String root, ZipEntry entry, WolfyUtilCore core) { - if (entry.isDirectory()) { - //We can't read from a directory! - return; - } - String path = entry.getName().replace(root + folderPath, "").replace(".json", ""); // This the key of the effect. - try (var stream = new BufferedInputStream(zipFile.getInputStream(entry))){ - var effect = JacksonUtil.getObjectMapper().readValue(stream, ParticleEffect.class); - if (effect != null) { - core.getRegistries().getParticleEffects().register(new NamespacedKey(pack.getMeta().getPack().getNamespace(), path), effect); - } - } catch (IOException e) { - core.getLogger().info("Failed to load effect \"" + path + "\"! Cause:" + e.getMessage()); - } + public void register(NamespacedKey namespacedKey, ParticleEffect value, WolfyUtilCore core) { + core.getRegistries().getParticleEffects().register(namespacedKey, value); } } From 8144d2addf4a540d0b5731473780c1027737bae3 Mon Sep 17 00:00:00 2001 From: WolfyScript Date: Thu, 6 Jan 2022 19:42:32 +0100 Subject: [PATCH 13/30] Added ResourceLoaderCustomItems to load custom items from expansions --- .../ResourceLoaderCustomItems.java | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 wolfyutilities/src/main/java/me/wolfyscript/utilities/main/resource_loader/ResourceLoaderCustomItems.java diff --git a/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/resource_loader/ResourceLoaderCustomItems.java b/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/resource_loader/ResourceLoaderCustomItems.java new file mode 100644 index 000000000..4b6e1695a --- /dev/null +++ b/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/resource_loader/ResourceLoaderCustomItems.java @@ -0,0 +1,38 @@ +/* + * WolfyUtilities, APIs and Utilities for Minecraft Spigot plugins + * Copyright (C) 2021 WolfyScript + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package me.wolfyscript.utilities.main.resource_loader; + +import me.wolfyscript.utilities.api.WolfyUtilCore; +import me.wolfyscript.utilities.api.inventory.custom_items.CustomItem; +import me.wolfyscript.utilities.expansions.ResourceLoaderJson; +import me.wolfyscript.utilities.main.WUPlugin; +import me.wolfyscript.utilities.util.NamespacedKey; + +public class ResourceLoaderCustomItems extends ResourceLoaderJson { + + public ResourceLoaderCustomItems(WUPlugin plugin) { + super(plugin, new NamespacedKey(plugin, "items/items"), CustomItem.class); + } + + @Override + public void register(NamespacedKey namespacedKey, CustomItem value, WolfyUtilCore core) { + core.getRegistries().getCustomItems().register(namespacedKey, value); + } + +} From 6311e16897cccdf6e676250e975d918baa9b5340 Mon Sep 17 00:00:00 2001 From: WolfyScript Date: Thu, 6 Jan 2022 19:43:05 +0100 Subject: [PATCH 14/30] Added prefix to ExpansionManager log messages --- .../utilities/expansions/ExpansionManager.java | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/core/src/main/java/me/wolfyscript/utilities/expansions/ExpansionManager.java b/core/src/main/java/me/wolfyscript/utilities/expansions/ExpansionManager.java index c545f4a2b..2b2bfd634 100644 --- a/core/src/main/java/me/wolfyscript/utilities/expansions/ExpansionManager.java +++ b/core/src/main/java/me/wolfyscript/utilities/expansions/ExpansionManager.java @@ -24,16 +24,15 @@ import java.io.File; import java.io.IOException; -import java.util.Arrays; import java.util.LinkedList; import java.util.List; -import java.util.Objects; -import java.util.stream.Collectors; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; public class ExpansionManager { + private final String LOG_PREFIX = "[Expansions] "; + private final WolfyUtilCore core; private final File packsFolder; @@ -46,7 +45,7 @@ public ExpansionManager(WolfyUtilCore core) { } public void initPacks() { - core.getLogger().info("Initiating expansion packs..."); + core.getLogger().info(LOG_PREFIX + "Initializing packs..."); File[] files = packsFolder.listFiles((dir, name) -> name.endsWith(".zip")); if (files != null) { this.packs = new LinkedList<>(); @@ -57,21 +56,21 @@ public void initPacks() { packs.add(pack); } } catch (IOException e) { - core.getLogger().warning("Failed to read expansion zip file " + file.getName() + ": " + e.getMessage()); + core.getLogger().warning(LOG_PREFIX + "Failed to read expansion zip file " + file.getName() + ": " + e.getMessage()); } } } } public void loadPacks() { - core.getLogger().info("Loading expansion packs..."); + core.getLogger().info(LOG_PREFIX + "Loading packs..."); var registry = core.getRegistries().getExpansionResourceLoaders(); List order = registry.getRegisterOrder(); for (ExpansionPack pack : packs) { try { pack.load(registry, order); } catch (IOException e) { - core.getLogger().warning("Failed to read expansion zip file " + pack.getFile().getName() + ": " + e.getMessage()); + core.getLogger().warning(LOG_PREFIX + "Failed to read expansion zip file " + pack.getFile().getName() + ": " + e.getMessage()); } } } @@ -90,10 +89,9 @@ public ExpansionPack initPack(File file) throws IOException { } ExpansionPack pack = new ExpansionPack(metaFile, file, entryNames, core); if (packs.contains(pack)) { - core.getLogger().warning("Expansion Pack already initialised: \"" + metaFile.getPack().getNamespace() +" \" in file " + file.getName()); + core.getLogger().warning(LOG_PREFIX + "Pack already initialised: \"" + metaFile.getPack().getNamespace() +"\" in file " + file.getName()); return null; } - core.getLogger().info("Initiated expansion pack " + file.getName()); return pack; } } From f855354c16777bd9be6c379bb6ce9cd920bab37e Mon Sep 17 00:00:00 2001 From: WolfyScript Date: Thu, 6 Jan 2022 19:49:16 +0100 Subject: [PATCH 15/30] Register ResourceLoaderCustomItems --- .../src/main/java/me/wolfyscript/utilities/main/WUPlugin.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/WUPlugin.java b/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/WUPlugin.java index 1daadb96c..e07e7e8ec 100644 --- a/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/WUPlugin.java +++ b/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/WUPlugin.java @@ -51,6 +51,7 @@ import me.wolfyscript.utilities.main.commands.SpawnParticleAnimationCommand; import me.wolfyscript.utilities.main.commands.SpawnParticleEffectCommand; import me.wolfyscript.utilities.main.configs.WUConfig; +import me.wolfyscript.utilities.main.resource_loader.ResourceLoaderCustomItems; import me.wolfyscript.utilities.main.resource_loader.ResourceLoaderParticleAnimations; import me.wolfyscript.utilities.main.resource_loader.ResourceLoaderParticleEffects; import me.wolfyscript.utilities.main.listeners.BlockListener; @@ -228,6 +229,7 @@ public void onLoad() { var expansionLoaders = getRegistries().getExpansionResourceLoaders(); expansionLoaders.register(new ResourceLoaderParticleEffects(this)); expansionLoaders.register(new ResourceLoaderParticleAnimations(this)); + expansionLoaders.register(new ResourceLoaderCustomItems(this)); } @Override From 300bd0ca1d94be373ac89e1f85ab10c64110fba2 Mon Sep 17 00:00:00 2001 From: WolfyScript Date: Tue, 28 Dec 2021 17:01:48 +0100 Subject: [PATCH 16/30] Started work on extension packs --- .../utilities/extensions/ExtensionPack.java | 58 +++++++++++++++ .../utilities/extensions/PackMetaFile.java | 71 +++++++++++++++++++ .../examples/extensionpack/pack.json | 8 +++ 3 files changed, 137 insertions(+) create mode 100644 core/src/main/java/me/wolfyscript/utilities/extensions/ExtensionPack.java create mode 100644 core/src/main/java/me/wolfyscript/utilities/extensions/PackMetaFile.java create mode 100644 wolfyutilities/src/main/resources/examples/extensionpack/pack.json diff --git a/core/src/main/java/me/wolfyscript/utilities/extensions/ExtensionPack.java b/core/src/main/java/me/wolfyscript/utilities/extensions/ExtensionPack.java new file mode 100644 index 000000000..a6362b5ef --- /dev/null +++ b/core/src/main/java/me/wolfyscript/utilities/extensions/ExtensionPack.java @@ -0,0 +1,58 @@ +/* + * WolfyUtilities, APIs and Utilities for Minecraft Spigot plugins + * Copyright (C) 2021 WolfyScript + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package me.wolfyscript.utilities.extensions; + +import me.wolfyscript.utilities.util.json.jackson.JacksonUtil; + +import java.io.File; +import java.io.IOException; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; + +public class ExtensionPack { + + private static final int VERSION = 1; + + private final ZipFile file; + + ExtensionPack (ZipFile file) { + this.file = file; + } + + public static ExtensionPack readPack(String path) throws IOException { + File file = new File(path); + if (file.getName().endsWith(".zip")) { + ZipFile zipFile = new ZipFile(path); + ZipEntry packInfoEntry = zipFile.getEntry("pack.json"); + if(packInfoEntry != null && !packInfoEntry.isDirectory()) { + PackMetaFile metaFile = JacksonUtil.getObjectMapper().readValue(zipFile.getInputStream(packInfoEntry), PackMetaFile.class); + if (metaFile.getPack().getVersion() != VERSION) { + //Invalid version! + return null; + } + + + + return new ExtensionPack(zipFile); + } + } + return null; + } + +} diff --git a/core/src/main/java/me/wolfyscript/utilities/extensions/PackMetaFile.java b/core/src/main/java/me/wolfyscript/utilities/extensions/PackMetaFile.java new file mode 100644 index 000000000..c720905b5 --- /dev/null +++ b/core/src/main/java/me/wolfyscript/utilities/extensions/PackMetaFile.java @@ -0,0 +1,71 @@ +/* + * WolfyUtilities, APIs and Utilities for Minecraft Spigot plugins + * Copyright (C) 2021 WolfyScript + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package me.wolfyscript.utilities.extensions; + +import com.fasterxml.jackson.annotation.JsonCreator; +import com.fasterxml.jackson.annotation.JsonProperty; + +import java.util.List; + +public class PackMetaFile { + + private final PackInfo pack; + + @JsonCreator + PackMetaFile(@JsonProperty("pack") PackInfo pack) { + this.pack = pack; + } + + public PackInfo getPack() { + return pack; + } + + public static class PackInfo { + + private final int version; + private final String namespace; + private final List authors; + private final String description; + + @JsonCreator + PackInfo(@JsonProperty("version") int version, @JsonProperty("namespace") String namespace, @JsonProperty("authors") List authors, @JsonProperty("description") String description) { + this.version = version; + this.namespace = namespace; + this.authors = List.copyOf(authors); + this.description = description; + } + + public int getVersion() { + return version; + } + + public String getNamespace() { + return namespace; + } + + public List getAuthors() { + return authors; + } + + public String getDescription() { + return description; + } + } + +} diff --git a/wolfyutilities/src/main/resources/examples/extensionpack/pack.json b/wolfyutilities/src/main/resources/examples/extensionpack/pack.json new file mode 100644 index 000000000..65b49c573 --- /dev/null +++ b/wolfyutilities/src/main/resources/examples/extensionpack/pack.json @@ -0,0 +1,8 @@ +{ + "pack": { + "version": 1, + "namespace": " (Usually your lowercase plugin name. Spaces are not allowed! Must be unique from other packs, so make sure to pick a good name!)", + "authors": [], + "description": "" + } +} \ No newline at end of file From acea3896be5995f8686dfa14cd1fe5148ef9671e Mon Sep 17 00:00:00 2001 From: WolfyScript Date: Thu, 6 Jan 2022 14:58:48 +0100 Subject: [PATCH 17/30] Renamed extension to expansion --- .../ExtensionPack.java => expansions/ExpansionPack.java} | 4 ++-- .../utilities/{extensions => expansions}/PackMetaFile.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) rename core/src/main/java/me/wolfyscript/utilities/{extensions/ExtensionPack.java => expansions/ExpansionPack.java} (96%) rename core/src/main/java/me/wolfyscript/utilities/{extensions => expansions}/PackMetaFile.java (97%) diff --git a/core/src/main/java/me/wolfyscript/utilities/extensions/ExtensionPack.java b/core/src/main/java/me/wolfyscript/utilities/expansions/ExpansionPack.java similarity index 96% rename from core/src/main/java/me/wolfyscript/utilities/extensions/ExtensionPack.java rename to core/src/main/java/me/wolfyscript/utilities/expansions/ExpansionPack.java index a6362b5ef..e6306d5cd 100644 --- a/core/src/main/java/me/wolfyscript/utilities/extensions/ExtensionPack.java +++ b/core/src/main/java/me/wolfyscript/utilities/expansions/ExpansionPack.java @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -package me.wolfyscript.utilities.extensions; +package me.wolfyscript.utilities.expansions; import me.wolfyscript.utilities.util.json.jackson.JacksonUtil; @@ -25,7 +25,7 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipFile; -public class ExtensionPack { +public class ExpansionPack { private static final int VERSION = 1; diff --git a/core/src/main/java/me/wolfyscript/utilities/extensions/PackMetaFile.java b/core/src/main/java/me/wolfyscript/utilities/expansions/PackMetaFile.java similarity index 97% rename from core/src/main/java/me/wolfyscript/utilities/extensions/PackMetaFile.java rename to core/src/main/java/me/wolfyscript/utilities/expansions/PackMetaFile.java index c720905b5..8ff8b894d 100644 --- a/core/src/main/java/me/wolfyscript/utilities/extensions/PackMetaFile.java +++ b/core/src/main/java/me/wolfyscript/utilities/expansions/PackMetaFile.java @@ -16,7 +16,7 @@ * along with this program. If not, see . */ -package me.wolfyscript.utilities.extensions; +package me.wolfyscript.utilities.expansions; import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; From 59e643c1355a0c93a37d2e8cc9443eb47aa922cb Mon Sep 17 00:00:00 2001 From: WolfyScript Date: Thu, 6 Jan 2022 14:59:10 +0100 Subject: [PATCH 18/30] Added ResourceLoader that can be registered to load specific resources from expansion packs. --- .../utilities/expansions/ResourceLoader.java | 52 +++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 core/src/main/java/me/wolfyscript/utilities/expansions/ResourceLoader.java diff --git a/core/src/main/java/me/wolfyscript/utilities/expansions/ResourceLoader.java b/core/src/main/java/me/wolfyscript/utilities/expansions/ResourceLoader.java new file mode 100644 index 000000000..9336337cb --- /dev/null +++ b/core/src/main/java/me/wolfyscript/utilities/expansions/ResourceLoader.java @@ -0,0 +1,52 @@ +/* + * WolfyUtilities, APIs and Utilities for Minecraft Spigot plugins + * Copyright (C) 2021 WolfyScript + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package me.wolfyscript.utilities.expansions; + +import com.google.common.base.Preconditions; +import me.wolfyscript.utilities.api.WolfyUtilCore; +import me.wolfyscript.utilities.util.Keyed; +import me.wolfyscript.utilities.util.NamespacedKey; +import org.bukkit.plugin.Plugin; + +import java.util.Locale; +import java.util.zip.ZipEntry; + +public abstract class ResourceLoader implements Keyed { + + private final Plugin plugin; + private final NamespacedKey key; + + protected ResourceLoader(Plugin plugin, NamespacedKey namespacedKey) { + this.plugin = plugin; + String namespace = plugin.getName().toLowerCase(Locale.ROOT); + Preconditions.checkArgument(namespacedKey.getNamespace().equals(namespace), "The namespace must be equal to your plugin name! Expected " + namespace + " but got " + namespace); + this.key = namespacedKey; + } + + public Plugin getPlugin() { + return plugin; + } + + public abstract void load(ZipEntry entry, WolfyUtilCore core); + + @Override + public NamespacedKey getNamespacedKey() { + return key; + } +} From 57499a79dd83bdced8af06fdf9fae63f69580691 Mon Sep 17 00:00:00 2001 From: WolfyScript Date: Thu, 6 Jan 2022 14:59:30 +0100 Subject: [PATCH 19/30] Added ResourceLoader registry --- .../utilities/registry/Registries.java | 9 ++++ .../registry/RegistryResourceLoader.java | 51 +++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 core/src/main/java/me/wolfyscript/utilities/registry/RegistryResourceLoader.java diff --git a/core/src/main/java/me/wolfyscript/utilities/registry/Registries.java b/core/src/main/java/me/wolfyscript/utilities/registry/Registries.java index 610388766..82cb1c27f 100644 --- a/core/src/main/java/me/wolfyscript/utilities/registry/Registries.java +++ b/core/src/main/java/me/wolfyscript/utilities/registry/Registries.java @@ -86,6 +86,9 @@ public class Registries { private final TypeRegistry> valueProviders; private final TypeRegistry operators; + //Expansions + private final RegistryResourceLoader expansionLoaders; + public Registries(WolfyUtilCore core) { this.core = core; @@ -102,6 +105,8 @@ public Registries(WolfyUtilCore core) { particleShapes = new TypeRegistrySimple<>(new NamespacedKey(core, "particles/shapes"), this); particleTimer = new TypeRegistrySimple<>(new NamespacedKey(core, "particle_timers"), this); customItemNbtChecks = new TypeRegistrySimple<>(new NamespacedKey(core, "custom_item_nbt_checks"), this); + + expansionLoaders = new RegistryResourceLoader(this); customItemActions = new TypeRegistrySimple<>(ITEM_ACTION_TYPES, this); customItemEvents = new TypeRegistrySimple<>(ITEM_EVENT_TYPES, this); valueProviders = new TypeRegistrySimple<>(new NamespacedKey(core, "value_providers"), this); @@ -229,6 +234,10 @@ public TypeRegistry getParticleTimer() { return particleTimer; } + public RegistryResourceLoader getExpansionResourceLoaders() { + return expansionLoaders; + } + public TypeRegistry> getCustomItemActions() { return customItemActions; } diff --git a/core/src/main/java/me/wolfyscript/utilities/registry/RegistryResourceLoader.java b/core/src/main/java/me/wolfyscript/utilities/registry/RegistryResourceLoader.java new file mode 100644 index 000000000..30e417975 --- /dev/null +++ b/core/src/main/java/me/wolfyscript/utilities/registry/RegistryResourceLoader.java @@ -0,0 +1,51 @@ +/* + * WolfyUtilities, APIs and Utilities for Minecraft Spigot plugins + * Copyright (C) 2021 WolfyScript + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package me.wolfyscript.utilities.registry; + +import me.wolfyscript.utilities.expansions.ResourceLoader; +import me.wolfyscript.utilities.util.NamespacedKey; + +import java.util.LinkedList; +import java.util.List; + +public class RegistryResourceLoader extends RegistrySimple { + + private final List registerOrder = new LinkedList<>(); + + public RegistryResourceLoader(Registries registries) { + super(new NamespacedKey(registries.getCore(), "expansions/loaders"), registries); + } + + @Override + public void register(ResourceLoader value) { + super.register(value); + if (!registerOrder.contains(value.getNamespacedKey())) { + registerOrder.add(value.getNamespacedKey()); + } + } + + @Override + public void register(NamespacedKey namespacedKey, ResourceLoader value) { + super.register(namespacedKey, value); + } + + public List getRegisterOrder() { + return List.copyOf(registerOrder); + } +} From a85851c0aebaf17353003754cae80962d6b5b00c Mon Sep 17 00:00:00 2001 From: WolfyScript Date: Thu, 6 Jan 2022 15:00:17 +0100 Subject: [PATCH 20/30] Added ResourceLoaderParticleEffects as a prototype to load particle effects. --- .../main/ResourceLoaderParticleEffects.java | 37 +++++++++++++++++++ .../wolfyscript/utilities/main/WUPlugin.java | 4 ++ 2 files changed, 41 insertions(+) create mode 100644 wolfyutilities/src/main/java/me/wolfyscript/utilities/main/ResourceLoaderParticleEffects.java diff --git a/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/ResourceLoaderParticleEffects.java b/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/ResourceLoaderParticleEffects.java new file mode 100644 index 000000000..d37b5760e --- /dev/null +++ b/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/ResourceLoaderParticleEffects.java @@ -0,0 +1,37 @@ +/* + * WolfyUtilities, APIs and Utilities for Minecraft Spigot plugins + * Copyright (C) 2021 WolfyScript + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package me.wolfyscript.utilities.main; + +import me.wolfyscript.utilities.api.WolfyUtilCore; +import me.wolfyscript.utilities.expansions.ResourceLoader; +import me.wolfyscript.utilities.util.NamespacedKey; + +import java.util.zip.ZipEntry; + +public class ResourceLoaderParticleEffects extends ResourceLoader { + + protected ResourceLoaderParticleEffects(WUPlugin plugin) { + super(plugin, new NamespacedKey(plugin, "particles/effects")); + } + + @Override + public void load(ZipEntry entry, WolfyUtilCore core) { + core.getLogger().info("effect: " + entry.getName()); + } +} diff --git a/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/WUPlugin.java b/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/WUPlugin.java index ec15ae5bd..3f9a3046a 100644 --- a/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/WUPlugin.java +++ b/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/WUPlugin.java @@ -59,6 +59,7 @@ import me.wolfyscript.utilities.api.inventory.custom_items.references.WolfyUtilitiesRef; import me.wolfyscript.utilities.compatibility.CompatibilityManager; import me.wolfyscript.utilities.compatibility.CompatibilityManagerImpl; +import me.wolfyscript.utilities.expansions.ExpansionManager; import me.wolfyscript.utilities.main.commands.ChatActionCommand; import me.wolfyscript.utilities.main.commands.InputCommand; import me.wolfyscript.utilities.main.commands.SpawnParticleAnimationCommand; @@ -316,6 +317,9 @@ public void onLoad() { KeyedTypeIdResolver.registerTypeRegistry((Class>)(Object) Event.class, customItemEvents); KeyedTypeIdResolver.registerTypeRegistry(Operator.class, operators); KeyedTypeIdResolver.registerTypeRegistry((Class>) (Object)ValueProvider.class, valueProviders); + + var expansionLoaders = getRegistries().getExpansionResourceLoaders(); + expansionLoaders.register(new ResourceLoaderParticleEffects(this)); } @Override From bd4954b8331b89af6ce1ef0effc94130b24518ec Mon Sep 17 00:00:00 2001 From: WolfyScript Date: Thu, 6 Jan 2022 15:01:59 +0100 Subject: [PATCH 21/30] ExpansionPack#load, loads the data from the pack using the registered ResourceLoaders. The register order is preserved and the resources loaded accordingly. --- .../utilities/expansions/ExpansionPack.java | 58 ++++++++++++------- 1 file changed, 38 insertions(+), 20 deletions(-) diff --git a/core/src/main/java/me/wolfyscript/utilities/expansions/ExpansionPack.java b/core/src/main/java/me/wolfyscript/utilities/expansions/ExpansionPack.java index e6306d5cd..b5f1e2ca4 100644 --- a/core/src/main/java/me/wolfyscript/utilities/expansions/ExpansionPack.java +++ b/core/src/main/java/me/wolfyscript/utilities/expansions/ExpansionPack.java @@ -18,41 +18,59 @@ package me.wolfyscript.utilities.expansions; -import me.wolfyscript.utilities.util.json.jackson.JacksonUtil; +import me.wolfyscript.utilities.api.WolfyUtilCore; +import me.wolfyscript.utilities.registry.RegistryResourceLoader; +import me.wolfyscript.utilities.util.NamespacedKey; import java.io.File; import java.io.IOException; -import java.util.zip.ZipEntry; +import java.util.List; import java.util.zip.ZipFile; public class ExpansionPack { - private static final int VERSION = 1; + static final int VERSION = 1; - private final ZipFile file; + private final WolfyUtilCore core; + private final File file; + private final List zipEntryNames; + private final String rootDir; - ExtensionPack (ZipFile file) { + ExpansionPack(File file, List entryNames, WolfyUtilCore core) { this.file = file; + this.core = core; + this.zipEntryNames = entryNames; + this.rootDir = entryNames.get(0); } - public static ExtensionPack readPack(String path) throws IOException { - File file = new File(path); - if (file.getName().endsWith(".zip")) { - ZipFile zipFile = new ZipFile(path); - ZipEntry packInfoEntry = zipFile.getEntry("pack.json"); - if(packInfoEntry != null && !packInfoEntry.isDirectory()) { - PackMetaFile metaFile = JacksonUtil.getObjectMapper().readValue(zipFile.getInputStream(packInfoEntry), PackMetaFile.class); - if (metaFile.getPack().getVersion() != VERSION) { - //Invalid version! - return null; + public void load(RegistryResourceLoader registry, List loaderOrder) throws IOException { + var zipFile = new ZipFile(file); + try (zipFile) { + for (NamespacedKey namespacedKey : loaderOrder) { + var loader = registry.get(namespacedKey); + if (loader != null) { + String loaderRoot = rootDir + namespacedKey.getNamespace() + "/" + namespacedKey.getKey() + "/"; //Get the root of the loader + int rootIndex = zipEntryNames.indexOf(loaderRoot); // Get the index of that root in the zip file. + if (rootIndex == -1) { + continue; // The loader root is not in the zip file. + } + for (int i = rootIndex; i < zipEntryNames.size(); i++) { + String entryName = zipEntryNames.get(i); + if (!entryName.startsWith(loaderRoot)) { + break; + } + var entry = zipFile.getEntry(entryName); + if (entry != null) { + // Call the loader specific load method + loader.load(entry, core); + } + } } - - - - return new ExtensionPack(zipFile); } } - return null; } + public File getFile() { + return file; + } } From b83fec9166ef32d266c4b59da5a637cb86e39c0d Mon Sep 17 00:00:00 2001 From: WolfyScript Date: Thu, 6 Jan 2022 15:02:40 +0100 Subject: [PATCH 22/30] Added ExpansionManager that initializes, loads and manages the expansions. --- .../expansions/ExpansionManager.java | 99 +++++++++++++++++++ .../wolfyscript/utilities/main/WUPlugin.java | 6 ++ 2 files changed, 105 insertions(+) create mode 100644 core/src/main/java/me/wolfyscript/utilities/expansions/ExpansionManager.java diff --git a/core/src/main/java/me/wolfyscript/utilities/expansions/ExpansionManager.java b/core/src/main/java/me/wolfyscript/utilities/expansions/ExpansionManager.java new file mode 100644 index 000000000..b6dc3c093 --- /dev/null +++ b/core/src/main/java/me/wolfyscript/utilities/expansions/ExpansionManager.java @@ -0,0 +1,99 @@ +/* + * WolfyUtilities, APIs and Utilities for Minecraft Spigot plugins + * Copyright (C) 2021 WolfyScript + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package me.wolfyscript.utilities.expansions; + +import me.wolfyscript.utilities.api.WolfyUtilCore; +import me.wolfyscript.utilities.util.NamespacedKey; +import me.wolfyscript.utilities.util.json.jackson.JacksonUtil; + +import java.io.File; +import java.io.IOException; +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; +import java.util.Objects; +import java.util.stream.Collectors; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; + +public class ExpansionManager { + + private final WolfyUtilCore core; + private final File packsFolder; + + private List packs; + + public ExpansionManager(WolfyUtilCore core) { + this.core = core; + this.packsFolder = new File(core.getDataFolder(), "expansion_packs"); + this.packs = new LinkedList<>(); + } + + public void initPacks() { + core.getLogger().info("Initiating expansion packs..."); + File[] files = packsFolder.listFiles((dir, name) -> name.endsWith(".zip")); + if (files != null) { + this.packs = Arrays.stream(files).map(file -> { + try { + return initPack(file); + } catch (IOException e) { + core.getLogger().warning("Failed to read expansion zip file " + file.getName() + ": " + e.getMessage()); + } + return null; + }).filter(Objects::nonNull).collect(Collectors.toCollection(LinkedList::new)); + } + } + + public void loadPacks() { + core.getLogger().info("Loading expansion packs..."); + var registry = core.getRegistries().getExpansionResourceLoaders(); + List order = registry.getRegisterOrder(); + for (ExpansionPack pack : packs) { + try { + pack.load(registry, order); + } catch (IOException e) { + core.getLogger().warning("Failed to read expansion zip file " + pack.getFile().getName() + ": " + e.getMessage()); + } + } + } + + public ExpansionPack initPack(File file) throws IOException { + ZipFile zipFile = new ZipFile(file); + try (zipFile) { + if (zipFile.entries().hasMoreElements()) { + List entryNames = zipFile.stream().map(ZipEntry::getName).toList(); + ZipEntry packInfoEntry = zipFile.getEntry(entryNames.get(0) + "pack.json"); + if (packInfoEntry != null && !packInfoEntry.isDirectory()) { + PackMetaFile metaFile = JacksonUtil.getObjectMapper().readValue(zipFile.getInputStream(packInfoEntry), PackMetaFile.class); + if (metaFile.getPack().getVersion() != ExpansionPack.VERSION) { + //Invalid version! + return null; + } + core.getLogger().info("Initiated expansion pack " + file.getName()); + return new ExpansionPack(file, entryNames, core); + } + } + } + return null; + } + + public List getPacks() { + return List.copyOf(packs); + } +} diff --git a/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/WUPlugin.java b/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/WUPlugin.java index 3f9a3046a..af60489f0 100644 --- a/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/WUPlugin.java +++ b/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/WUPlugin.java @@ -154,6 +154,7 @@ public final class WUPlugin extends WolfyUtilCore { private final MessageFactory messageFactory; private final CompatibilityManagerImpl compatibilityManager; private BukkitAudiences adventure; + private final ExpansionManager expansionManager; /** * Constructor invoked by Spigot when the plugin is loaded. @@ -167,6 +168,7 @@ public WUPlugin() { this.messageHandler = new MessageHandler(this); this.messageFactory = new MessageFactory(this); this.compatibilityManager = new CompatibilityManagerImpl(this); + this.expansionManager = new ExpansionManager(this); } /** @@ -181,6 +183,7 @@ private WUPlugin(JavaPluginLoader loader, PluginDescriptionFile description, Fil this.messageHandler = new MessageHandler(this); this.messageFactory = new MessageFactory(this); this.compatibilityManager = new CompatibilityManagerImpl(this); + this.expansionManager = new ExpansionManager(this); } @Deprecated @@ -336,6 +339,9 @@ public void onEnable() { registerAPIReference(new VanillaRef.Parser()); registerAPIReference(new WolfyUtilitiesRef.Parser()); + expansionManager.initPacks(); + expansionManager.loadPacks(); + //Load Language api.getLanguageAPI().loadLangFile("en_US"); From d2ee4390bb4910a7fb3652d508f5770384a6a7c7 Mon Sep 17 00:00:00 2001 From: WolfyScript Date: Thu, 6 Jan 2022 19:05:54 +0100 Subject: [PATCH 23/30] ExpansionManager will ignore duplicate packs. --- .../utilities/expansions/ExpansionManager.java | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/me/wolfyscript/utilities/expansions/ExpansionManager.java b/core/src/main/java/me/wolfyscript/utilities/expansions/ExpansionManager.java index b6dc3c093..c545f4a2b 100644 --- a/core/src/main/java/me/wolfyscript/utilities/expansions/ExpansionManager.java +++ b/core/src/main/java/me/wolfyscript/utilities/expansions/ExpansionManager.java @@ -49,14 +49,17 @@ public void initPacks() { core.getLogger().info("Initiating expansion packs..."); File[] files = packsFolder.listFiles((dir, name) -> name.endsWith(".zip")); if (files != null) { - this.packs = Arrays.stream(files).map(file -> { + this.packs = new LinkedList<>(); + for (File file : files) { try { - return initPack(file); + var pack = initPack(file); + if (pack != null) { + packs.add(pack); + } } catch (IOException e) { core.getLogger().warning("Failed to read expansion zip file " + file.getName() + ": " + e.getMessage()); } - return null; - }).filter(Objects::nonNull).collect(Collectors.toCollection(LinkedList::new)); + } } } @@ -85,8 +88,13 @@ public ExpansionPack initPack(File file) throws IOException { //Invalid version! return null; } + ExpansionPack pack = new ExpansionPack(metaFile, file, entryNames, core); + if (packs.contains(pack)) { + core.getLogger().warning("Expansion Pack already initialised: \"" + metaFile.getPack().getNamespace() +" \" in file " + file.getName()); + return null; + } core.getLogger().info("Initiated expansion pack " + file.getName()); - return new ExpansionPack(file, entryNames, core); + return pack; } } } From a0434c1725236c62c47608c864b003d134b6055d Mon Sep 17 00:00:00 2001 From: WolfyScript Date: Thu, 6 Jan 2022 19:06:54 +0100 Subject: [PATCH 24/30] Added more arguments to ResourceLoader#load --- .../utilities/expansions/ExpansionPack.java | 33 +++++++++-- .../utilities/expansions/ResourceLoader.java | 5 +- .../main/ResourceLoaderParticleEffects.java | 37 ------------ .../ResourceLoaderParticleEffects.java | 56 +++++++++++++++++++ 4 files changed, 89 insertions(+), 42 deletions(-) delete mode 100644 wolfyutilities/src/main/java/me/wolfyscript/utilities/main/ResourceLoaderParticleEffects.java create mode 100644 wolfyutilities/src/main/java/me/wolfyscript/utilities/main/expansions/ResourceLoaderParticleEffects.java diff --git a/core/src/main/java/me/wolfyscript/utilities/expansions/ExpansionPack.java b/core/src/main/java/me/wolfyscript/utilities/expansions/ExpansionPack.java index b5f1e2ca4..cff999459 100644 --- a/core/src/main/java/me/wolfyscript/utilities/expansions/ExpansionPack.java +++ b/core/src/main/java/me/wolfyscript/utilities/expansions/ExpansionPack.java @@ -21,6 +21,7 @@ import me.wolfyscript.utilities.api.WolfyUtilCore; import me.wolfyscript.utilities.registry.RegistryResourceLoader; import me.wolfyscript.utilities.util.NamespacedKey; +import org.jetbrains.annotations.NotNull; import java.io.File; import java.io.IOException; @@ -32,15 +33,17 @@ public class ExpansionPack { static final int VERSION = 1; private final WolfyUtilCore core; + private final PackMetaFile meta; private final File file; private final List zipEntryNames; private final String rootDir; - ExpansionPack(File file, List entryNames, WolfyUtilCore core) { + ExpansionPack(@NotNull PackMetaFile meta, File file, List entryNames, WolfyUtilCore core) { this.file = file; this.core = core; this.zipEntryNames = entryNames; this.rootDir = entryNames.get(0); + this.meta = meta; } public void load(RegistryResourceLoader registry, List loaderOrder) throws IOException { @@ -49,12 +52,13 @@ public void load(RegistryResourceLoader registry, List loaderOrde for (NamespacedKey namespacedKey : loaderOrder) { var loader = registry.get(namespacedKey); if (loader != null) { - String loaderRoot = rootDir + namespacedKey.getNamespace() + "/" + namespacedKey.getKey() + "/"; //Get the root of the loader + String loaderRoot = rootDir + loader.folderPath; //Get the root of the loader int rootIndex = zipEntryNames.indexOf(loaderRoot); // Get the index of that root in the zip file. if (rootIndex == -1) { continue; // The loader root is not in the zip file. } - for (int i = rootIndex; i < zipEntryNames.size(); i++) { + //We start one index later, as the first one is always the root folder + for (int i = rootIndex + 1; i < zipEntryNames.size(); i++) { String entryName = zipEntryNames.get(i); if (!entryName.startsWith(loaderRoot)) { break; @@ -62,7 +66,12 @@ public void load(RegistryResourceLoader registry, List loaderOrde var entry = zipFile.getEntry(entryName); if (entry != null) { // Call the loader specific load method - loader.load(entry, core); + try { + loader.load(this, zipFile, rootDir, entry, core); + } catch (Exception e) { + // We don't want a faulty loader to crash the whole loading process and/or plugin! + e.printStackTrace(); + } } } } @@ -70,7 +79,23 @@ public void load(RegistryResourceLoader registry, List loaderOrde } } + public PackMetaFile getMeta() { + return meta; + } + public File getFile() { return file; } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof ExpansionPack pack)) return false; + return meta.getPack().getNamespace().equals(pack.meta.getPack().getNamespace()); + } + + @Override + public int hashCode() { + return meta.getPack().getNamespace().hashCode(); + } } diff --git a/core/src/main/java/me/wolfyscript/utilities/expansions/ResourceLoader.java b/core/src/main/java/me/wolfyscript/utilities/expansions/ResourceLoader.java index 9336337cb..40394cde5 100644 --- a/core/src/main/java/me/wolfyscript/utilities/expansions/ResourceLoader.java +++ b/core/src/main/java/me/wolfyscript/utilities/expansions/ResourceLoader.java @@ -26,24 +26,27 @@ import java.util.Locale; import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; public abstract class ResourceLoader implements Keyed { private final Plugin plugin; private final NamespacedKey key; + protected final String folderPath; protected ResourceLoader(Plugin plugin, NamespacedKey namespacedKey) { this.plugin = plugin; String namespace = plugin.getName().toLowerCase(Locale.ROOT); Preconditions.checkArgument(namespacedKey.getNamespace().equals(namespace), "The namespace must be equal to your plugin name! Expected " + namespace + " but got " + namespace); this.key = namespacedKey; + this.folderPath = key.toString("/") + "/"; } public Plugin getPlugin() { return plugin; } - public abstract void load(ZipEntry entry, WolfyUtilCore core); + public abstract void load(ExpansionPack pack, ZipFile zipFile, String root, ZipEntry entry, WolfyUtilCore core); @Override public NamespacedKey getNamespacedKey() { diff --git a/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/ResourceLoaderParticleEffects.java b/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/ResourceLoaderParticleEffects.java deleted file mode 100644 index d37b5760e..000000000 --- a/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/ResourceLoaderParticleEffects.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * WolfyUtilities, APIs and Utilities for Minecraft Spigot plugins - * Copyright (C) 2021 WolfyScript - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package me.wolfyscript.utilities.main; - -import me.wolfyscript.utilities.api.WolfyUtilCore; -import me.wolfyscript.utilities.expansions.ResourceLoader; -import me.wolfyscript.utilities.util.NamespacedKey; - -import java.util.zip.ZipEntry; - -public class ResourceLoaderParticleEffects extends ResourceLoader { - - protected ResourceLoaderParticleEffects(WUPlugin plugin) { - super(plugin, new NamespacedKey(plugin, "particles/effects")); - } - - @Override - public void load(ZipEntry entry, WolfyUtilCore core) { - core.getLogger().info("effect: " + entry.getName()); - } -} diff --git a/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/expansions/ResourceLoaderParticleEffects.java b/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/expansions/ResourceLoaderParticleEffects.java new file mode 100644 index 000000000..f8f224ccb --- /dev/null +++ b/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/expansions/ResourceLoaderParticleEffects.java @@ -0,0 +1,56 @@ +/* + * WolfyUtilities, APIs and Utilities for Minecraft Spigot plugins + * Copyright (C) 2021 WolfyScript + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package me.wolfyscript.utilities.main.expansions; + +import me.wolfyscript.utilities.api.WolfyUtilCore; +import me.wolfyscript.utilities.expansions.ExpansionPack; +import me.wolfyscript.utilities.expansions.ResourceLoader; +import me.wolfyscript.utilities.main.WUPlugin; +import me.wolfyscript.utilities.util.NamespacedKey; +import me.wolfyscript.utilities.util.json.jackson.JacksonUtil; +import me.wolfyscript.utilities.util.particles.ParticleEffect; + +import java.io.BufferedInputStream; +import java.io.IOException; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; + +public class ResourceLoaderParticleEffects extends ResourceLoader { + + public ResourceLoaderParticleEffects(WUPlugin plugin) { + super(plugin, new NamespacedKey(plugin, "particles/effects")); + } + + @Override + public void load(ExpansionPack pack, ZipFile zipFile, String root, ZipEntry entry, WolfyUtilCore core) { + if (entry.isDirectory()) { + //We can't read from a directory! + return; + } + String path = entry.getName().replace(root + folderPath, "").replace(".json", ""); // This the key of the effect. + try (var stream = new BufferedInputStream(zipFile.getInputStream(entry))){ + var effect = JacksonUtil.getObjectMapper().readValue(stream, ParticleEffect.class); + if (effect != null) { + core.getRegistries().getParticleEffects().register(new NamespacedKey(pack.getMeta().getPack().getNamespace(), path), effect); + } + } catch (IOException e) { + core.getLogger().info("Failed to load effect \"" + path + "\"! Cause:" + e.getMessage()); + } + } +} From 8768cdaaa57feb8752828341410aa0e333b79bef Mon Sep 17 00:00:00 2001 From: WolfyScript Date: Thu, 6 Jan 2022 19:07:21 +0100 Subject: [PATCH 25/30] Added ResourceLoaderParticleAnimations to load particle animations. --- .../wolfyscript/utilities/main/WUPlugin.java | 3 + .../ResourceLoaderParticleAnimations.java | 58 +++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 wolfyutilities/src/main/java/me/wolfyscript/utilities/main/expansions/ResourceLoaderParticleAnimations.java diff --git a/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/WUPlugin.java b/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/WUPlugin.java index af60489f0..1218b92e5 100644 --- a/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/WUPlugin.java +++ b/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/WUPlugin.java @@ -65,6 +65,8 @@ import me.wolfyscript.utilities.main.commands.SpawnParticleAnimationCommand; import me.wolfyscript.utilities.main.commands.SpawnParticleEffectCommand; import me.wolfyscript.utilities.main.configs.WUConfig; +import me.wolfyscript.utilities.main.expansions.ResourceLoaderParticleAnimations; +import me.wolfyscript.utilities.main.expansions.ResourceLoaderParticleEffects; import me.wolfyscript.utilities.main.listeners.BlockListener; import me.wolfyscript.utilities.main.listeners.EquipListener; import me.wolfyscript.utilities.main.listeners.GUIInventoryListener; @@ -323,6 +325,7 @@ public void onLoad() { var expansionLoaders = getRegistries().getExpansionResourceLoaders(); expansionLoaders.register(new ResourceLoaderParticleEffects(this)); + expansionLoaders.register(new ResourceLoaderParticleAnimations(this)); } @Override diff --git a/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/expansions/ResourceLoaderParticleAnimations.java b/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/expansions/ResourceLoaderParticleAnimations.java new file mode 100644 index 000000000..b09a3d745 --- /dev/null +++ b/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/expansions/ResourceLoaderParticleAnimations.java @@ -0,0 +1,58 @@ +/* + * WolfyUtilities, APIs and Utilities for Minecraft Spigot plugins + * Copyright (C) 2021 WolfyScript + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package me.wolfyscript.utilities.main.expansions; + +import me.wolfyscript.utilities.api.WolfyUtilCore; +import me.wolfyscript.utilities.expansions.ExpansionPack; +import me.wolfyscript.utilities.expansions.ResourceLoader; +import me.wolfyscript.utilities.main.WUPlugin; +import me.wolfyscript.utilities.util.NamespacedKey; +import me.wolfyscript.utilities.util.json.jackson.JacksonUtil; +import me.wolfyscript.utilities.util.particles.ParticleAnimation; +import me.wolfyscript.utilities.util.particles.ParticleEffect; + +import java.io.BufferedInputStream; +import java.io.IOException; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; + +public class ResourceLoaderParticleAnimations extends ResourceLoader { + + public ResourceLoaderParticleAnimations(WUPlugin plugin) { + super(plugin, new NamespacedKey(plugin, "particles/animations")); + } + + @Override + public void load(ExpansionPack pack, ZipFile zipFile, String root, ZipEntry entry, WolfyUtilCore core) { + if (entry.isDirectory()) { + //We can't read from a directory! + return; + } + String path = entry.getName().replace(root + folderPath, ""); + path = path.substring(0, path.lastIndexOf(".")); // This the key of the effect. + try (var stream = new BufferedInputStream(zipFile.getInputStream(entry))){ + var effect = JacksonUtil.getObjectMapper().readValue(stream, ParticleAnimation.class); + if (effect != null) { + core.getRegistries().getParticleAnimations().register(new NamespacedKey(pack.getMeta().getPack().getNamespace(), path), effect); + } + } catch (IOException e) { + core.getLogger().info("Failed to load animation \"" + path + "\"! Cause:" + e.getMessage()); + } + } +} From af40f4b10b21479885c6b52cee07908685304d60 Mon Sep 17 00:00:00 2001 From: WolfyScript Date: Thu, 6 Jan 2022 19:41:10 +0100 Subject: [PATCH 26/30] Added ResourceLoaderJson to make it easier to load data from json zip entries. --- .../expansions/ResourceLoaderJson.java | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 core/src/main/java/me/wolfyscript/utilities/expansions/ResourceLoaderJson.java diff --git a/core/src/main/java/me/wolfyscript/utilities/expansions/ResourceLoaderJson.java b/core/src/main/java/me/wolfyscript/utilities/expansions/ResourceLoaderJson.java new file mode 100644 index 000000000..c29ede4a4 --- /dev/null +++ b/core/src/main/java/me/wolfyscript/utilities/expansions/ResourceLoaderJson.java @@ -0,0 +1,60 @@ +/* + * WolfyUtilities, APIs and Utilities for Minecraft Spigot plugins + * Copyright (C) 2021 WolfyScript + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package me.wolfyscript.utilities.expansions; + +import me.wolfyscript.utilities.api.WolfyUtilCore; +import me.wolfyscript.utilities.util.Keyed; +import me.wolfyscript.utilities.util.NamespacedKey; +import me.wolfyscript.utilities.util.json.jackson.JacksonUtil; +import org.bukkit.plugin.Plugin; + +import java.io.BufferedInputStream; +import java.io.IOException; +import java.util.zip.ZipEntry; +import java.util.zip.ZipFile; + +public abstract class ResourceLoaderJson extends ResourceLoader { + + private final Class type; + + protected ResourceLoaderJson(Plugin plugin, NamespacedKey namespacedKey, Class type) { + super(plugin, namespacedKey); + this.type = type; + } + + public abstract void register(NamespacedKey namespacedKey, T value, WolfyUtilCore core); + + @Override + public void load(ExpansionPack pack, ZipFile zipFile, String root, ZipEntry entry, WolfyUtilCore core) { + if (entry.isDirectory()) { + //We can't read from a directory! + return; + } + String path = entry.getName().replace(root + folderPath, ""); + path = path.substring(0, path.lastIndexOf(".")); // This the key of the effect. + try (var stream = new BufferedInputStream(zipFile.getInputStream(entry))) { + var item = JacksonUtil.getObjectMapper().readValue(stream, type); + if (item != null) { + this.register(new NamespacedKey(pack.getMeta().getPack().getNamespace(), path), item, core); + } + } catch (IOException e) { + core.getLogger().info("Failed to load animation \"" + path + "\"! Cause:" + e.getMessage()); + } + } +} From 691c5e5ab9036ed24206a60b0b47dfee2581a94c Mon Sep 17 00:00:00 2001 From: WolfyScript Date: Thu, 6 Jan 2022 19:41:46 +0100 Subject: [PATCH 27/30] Updated existing resource loaders to resourceLoaderJson. Renamed package --- .../wolfyscript/utilities/main/WUPlugin.java | 4 +- .../ResourceLoaderParticleAnimations.java | 58 ------------------- .../ResourceLoaderParticleAnimations.java | 37 ++++++++++++ .../ResourceLoaderParticleEffects.java | 23 ++------ 4 files changed, 45 insertions(+), 77 deletions(-) delete mode 100644 wolfyutilities/src/main/java/me/wolfyscript/utilities/main/expansions/ResourceLoaderParticleAnimations.java create mode 100644 wolfyutilities/src/main/java/me/wolfyscript/utilities/main/resource_loader/ResourceLoaderParticleAnimations.java rename wolfyutilities/src/main/java/me/wolfyscript/utilities/main/{expansions => resource_loader}/ResourceLoaderParticleEffects.java (63%) diff --git a/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/WUPlugin.java b/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/WUPlugin.java index 1218b92e5..61c77342c 100644 --- a/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/WUPlugin.java +++ b/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/WUPlugin.java @@ -65,8 +65,8 @@ import me.wolfyscript.utilities.main.commands.SpawnParticleAnimationCommand; import me.wolfyscript.utilities.main.commands.SpawnParticleEffectCommand; import me.wolfyscript.utilities.main.configs.WUConfig; -import me.wolfyscript.utilities.main.expansions.ResourceLoaderParticleAnimations; -import me.wolfyscript.utilities.main.expansions.ResourceLoaderParticleEffects; +import me.wolfyscript.utilities.main.resource_loader.ResourceLoaderParticleAnimations; +import me.wolfyscript.utilities.main.resource_loader.ResourceLoaderParticleEffects; import me.wolfyscript.utilities.main.listeners.BlockListener; import me.wolfyscript.utilities.main.listeners.EquipListener; import me.wolfyscript.utilities.main.listeners.GUIInventoryListener; diff --git a/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/expansions/ResourceLoaderParticleAnimations.java b/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/expansions/ResourceLoaderParticleAnimations.java deleted file mode 100644 index b09a3d745..000000000 --- a/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/expansions/ResourceLoaderParticleAnimations.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * WolfyUtilities, APIs and Utilities for Minecraft Spigot plugins - * Copyright (C) 2021 WolfyScript - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - */ - -package me.wolfyscript.utilities.main.expansions; - -import me.wolfyscript.utilities.api.WolfyUtilCore; -import me.wolfyscript.utilities.expansions.ExpansionPack; -import me.wolfyscript.utilities.expansions.ResourceLoader; -import me.wolfyscript.utilities.main.WUPlugin; -import me.wolfyscript.utilities.util.NamespacedKey; -import me.wolfyscript.utilities.util.json.jackson.JacksonUtil; -import me.wolfyscript.utilities.util.particles.ParticleAnimation; -import me.wolfyscript.utilities.util.particles.ParticleEffect; - -import java.io.BufferedInputStream; -import java.io.IOException; -import java.util.zip.ZipEntry; -import java.util.zip.ZipFile; - -public class ResourceLoaderParticleAnimations extends ResourceLoader { - - public ResourceLoaderParticleAnimations(WUPlugin plugin) { - super(plugin, new NamespacedKey(plugin, "particles/animations")); - } - - @Override - public void load(ExpansionPack pack, ZipFile zipFile, String root, ZipEntry entry, WolfyUtilCore core) { - if (entry.isDirectory()) { - //We can't read from a directory! - return; - } - String path = entry.getName().replace(root + folderPath, ""); - path = path.substring(0, path.lastIndexOf(".")); // This the key of the effect. - try (var stream = new BufferedInputStream(zipFile.getInputStream(entry))){ - var effect = JacksonUtil.getObjectMapper().readValue(stream, ParticleAnimation.class); - if (effect != null) { - core.getRegistries().getParticleAnimations().register(new NamespacedKey(pack.getMeta().getPack().getNamespace(), path), effect); - } - } catch (IOException e) { - core.getLogger().info("Failed to load animation \"" + path + "\"! Cause:" + e.getMessage()); - } - } -} diff --git a/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/resource_loader/ResourceLoaderParticleAnimations.java b/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/resource_loader/ResourceLoaderParticleAnimations.java new file mode 100644 index 000000000..cf6503b22 --- /dev/null +++ b/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/resource_loader/ResourceLoaderParticleAnimations.java @@ -0,0 +1,37 @@ +/* + * WolfyUtilities, APIs and Utilities for Minecraft Spigot plugins + * Copyright (C) 2021 WolfyScript + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package me.wolfyscript.utilities.main.resource_loader; + +import me.wolfyscript.utilities.api.WolfyUtilCore; +import me.wolfyscript.utilities.expansions.ResourceLoaderJson; +import me.wolfyscript.utilities.main.WUPlugin; +import me.wolfyscript.utilities.util.NamespacedKey; +import me.wolfyscript.utilities.util.particles.ParticleAnimation; + +public class ResourceLoaderParticleAnimations extends ResourceLoaderJson { + + public ResourceLoaderParticleAnimations(WUPlugin plugin) { + super(plugin, new NamespacedKey(plugin, "particles/animations"), ParticleAnimation.class); + } + + @Override + public void register(NamespacedKey namespacedKey, ParticleAnimation value, WolfyUtilCore core) { + core.getRegistries().getParticleAnimations().register(namespacedKey, value); + } +} diff --git a/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/expansions/ResourceLoaderParticleEffects.java b/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/resource_loader/ResourceLoaderParticleEffects.java similarity index 63% rename from wolfyutilities/src/main/java/me/wolfyscript/utilities/main/expansions/ResourceLoaderParticleEffects.java rename to wolfyutilities/src/main/java/me/wolfyscript/utilities/main/resource_loader/ResourceLoaderParticleEffects.java index f8f224ccb..db4f0e7a0 100644 --- a/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/expansions/ResourceLoaderParticleEffects.java +++ b/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/resource_loader/ResourceLoaderParticleEffects.java @@ -16,11 +16,12 @@ * along with this program. If not, see . */ -package me.wolfyscript.utilities.main.expansions; +package me.wolfyscript.utilities.main.resource_loader; import me.wolfyscript.utilities.api.WolfyUtilCore; import me.wolfyscript.utilities.expansions.ExpansionPack; import me.wolfyscript.utilities.expansions.ResourceLoader; +import me.wolfyscript.utilities.expansions.ResourceLoaderJson; import me.wolfyscript.utilities.main.WUPlugin; import me.wolfyscript.utilities.util.NamespacedKey; import me.wolfyscript.utilities.util.json.jackson.JacksonUtil; @@ -31,26 +32,14 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipFile; -public class ResourceLoaderParticleEffects extends ResourceLoader { +public class ResourceLoaderParticleEffects extends ResourceLoaderJson { public ResourceLoaderParticleEffects(WUPlugin plugin) { - super(plugin, new NamespacedKey(plugin, "particles/effects")); + super(plugin, new NamespacedKey(plugin, "particles/effects"), ParticleEffect.class); } @Override - public void load(ExpansionPack pack, ZipFile zipFile, String root, ZipEntry entry, WolfyUtilCore core) { - if (entry.isDirectory()) { - //We can't read from a directory! - return; - } - String path = entry.getName().replace(root + folderPath, "").replace(".json", ""); // This the key of the effect. - try (var stream = new BufferedInputStream(zipFile.getInputStream(entry))){ - var effect = JacksonUtil.getObjectMapper().readValue(stream, ParticleEffect.class); - if (effect != null) { - core.getRegistries().getParticleEffects().register(new NamespacedKey(pack.getMeta().getPack().getNamespace(), path), effect); - } - } catch (IOException e) { - core.getLogger().info("Failed to load effect \"" + path + "\"! Cause:" + e.getMessage()); - } + public void register(NamespacedKey namespacedKey, ParticleEffect value, WolfyUtilCore core) { + core.getRegistries().getParticleEffects().register(namespacedKey, value); } } From c4b4c92a779e3e485e73bf8d544ac0777cce9ab1 Mon Sep 17 00:00:00 2001 From: WolfyScript Date: Thu, 6 Jan 2022 19:42:32 +0100 Subject: [PATCH 28/30] Added ResourceLoaderCustomItems to load custom items from expansions --- .../ResourceLoaderCustomItems.java | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 wolfyutilities/src/main/java/me/wolfyscript/utilities/main/resource_loader/ResourceLoaderCustomItems.java diff --git a/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/resource_loader/ResourceLoaderCustomItems.java b/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/resource_loader/ResourceLoaderCustomItems.java new file mode 100644 index 000000000..4b6e1695a --- /dev/null +++ b/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/resource_loader/ResourceLoaderCustomItems.java @@ -0,0 +1,38 @@ +/* + * WolfyUtilities, APIs and Utilities for Minecraft Spigot plugins + * Copyright (C) 2021 WolfyScript + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package me.wolfyscript.utilities.main.resource_loader; + +import me.wolfyscript.utilities.api.WolfyUtilCore; +import me.wolfyscript.utilities.api.inventory.custom_items.CustomItem; +import me.wolfyscript.utilities.expansions.ResourceLoaderJson; +import me.wolfyscript.utilities.main.WUPlugin; +import me.wolfyscript.utilities.util.NamespacedKey; + +public class ResourceLoaderCustomItems extends ResourceLoaderJson { + + public ResourceLoaderCustomItems(WUPlugin plugin) { + super(plugin, new NamespacedKey(plugin, "items/items"), CustomItem.class); + } + + @Override + public void register(NamespacedKey namespacedKey, CustomItem value, WolfyUtilCore core) { + core.getRegistries().getCustomItems().register(namespacedKey, value); + } + +} From cdfc65bd3346856956e8bbcfeb0df2ce6171740a Mon Sep 17 00:00:00 2001 From: WolfyScript Date: Thu, 6 Jan 2022 19:43:05 +0100 Subject: [PATCH 29/30] Added prefix to ExpansionManager log messages --- .../utilities/expansions/ExpansionManager.java | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/core/src/main/java/me/wolfyscript/utilities/expansions/ExpansionManager.java b/core/src/main/java/me/wolfyscript/utilities/expansions/ExpansionManager.java index c545f4a2b..2b2bfd634 100644 --- a/core/src/main/java/me/wolfyscript/utilities/expansions/ExpansionManager.java +++ b/core/src/main/java/me/wolfyscript/utilities/expansions/ExpansionManager.java @@ -24,16 +24,15 @@ import java.io.File; import java.io.IOException; -import java.util.Arrays; import java.util.LinkedList; import java.util.List; -import java.util.Objects; -import java.util.stream.Collectors; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; public class ExpansionManager { + private final String LOG_PREFIX = "[Expansions] "; + private final WolfyUtilCore core; private final File packsFolder; @@ -46,7 +45,7 @@ public ExpansionManager(WolfyUtilCore core) { } public void initPacks() { - core.getLogger().info("Initiating expansion packs..."); + core.getLogger().info(LOG_PREFIX + "Initializing packs..."); File[] files = packsFolder.listFiles((dir, name) -> name.endsWith(".zip")); if (files != null) { this.packs = new LinkedList<>(); @@ -57,21 +56,21 @@ public void initPacks() { packs.add(pack); } } catch (IOException e) { - core.getLogger().warning("Failed to read expansion zip file " + file.getName() + ": " + e.getMessage()); + core.getLogger().warning(LOG_PREFIX + "Failed to read expansion zip file " + file.getName() + ": " + e.getMessage()); } } } } public void loadPacks() { - core.getLogger().info("Loading expansion packs..."); + core.getLogger().info(LOG_PREFIX + "Loading packs..."); var registry = core.getRegistries().getExpansionResourceLoaders(); List order = registry.getRegisterOrder(); for (ExpansionPack pack : packs) { try { pack.load(registry, order); } catch (IOException e) { - core.getLogger().warning("Failed to read expansion zip file " + pack.getFile().getName() + ": " + e.getMessage()); + core.getLogger().warning(LOG_PREFIX + "Failed to read expansion zip file " + pack.getFile().getName() + ": " + e.getMessage()); } } } @@ -90,10 +89,9 @@ public ExpansionPack initPack(File file) throws IOException { } ExpansionPack pack = new ExpansionPack(metaFile, file, entryNames, core); if (packs.contains(pack)) { - core.getLogger().warning("Expansion Pack already initialised: \"" + metaFile.getPack().getNamespace() +" \" in file " + file.getName()); + core.getLogger().warning(LOG_PREFIX + "Pack already initialised: \"" + metaFile.getPack().getNamespace() +"\" in file " + file.getName()); return null; } - core.getLogger().info("Initiated expansion pack " + file.getName()); return pack; } } From d6bd25e6ab628ca0bf8c4b8e371dfe388975d2cb Mon Sep 17 00:00:00 2001 From: WolfyScript Date: Thu, 6 Jan 2022 19:49:16 +0100 Subject: [PATCH 30/30] Register ResourceLoaderCustomItems --- .../src/main/java/me/wolfyscript/utilities/main/WUPlugin.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/WUPlugin.java b/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/WUPlugin.java index 61c77342c..962432be4 100644 --- a/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/WUPlugin.java +++ b/wolfyutilities/src/main/java/me/wolfyscript/utilities/main/WUPlugin.java @@ -65,6 +65,7 @@ import me.wolfyscript.utilities.main.commands.SpawnParticleAnimationCommand; import me.wolfyscript.utilities.main.commands.SpawnParticleEffectCommand; import me.wolfyscript.utilities.main.configs.WUConfig; +import me.wolfyscript.utilities.main.resource_loader.ResourceLoaderCustomItems; import me.wolfyscript.utilities.main.resource_loader.ResourceLoaderParticleAnimations; import me.wolfyscript.utilities.main.resource_loader.ResourceLoaderParticleEffects; import me.wolfyscript.utilities.main.listeners.BlockListener; @@ -326,6 +327,7 @@ public void onLoad() { var expansionLoaders = getRegistries().getExpansionResourceLoaders(); expansionLoaders.register(new ResourceLoaderParticleEffects(this)); expansionLoaders.register(new ResourceLoaderParticleAnimations(this)); + expansionLoaders.register(new ResourceLoaderCustomItems(this)); } @Override