Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
1229a7d
Started work on extension packs
WolfyScript Dec 28, 2021
a1ae914
Merge branch 'master' into extension_packs
WolfyScript Jan 5, 2022
0914fd9
Renamed extension to expansion
WolfyScript Jan 6, 2022
7829f1e
Added ResourceLoader that can be registered to load specific resource…
WolfyScript Jan 6, 2022
c886b2b
Added ResourceLoader registry
WolfyScript Jan 6, 2022
3b740e6
Added ResourceLoaderParticleEffects as a prototype to load particle e…
WolfyScript Jan 6, 2022
905cfab
ExpansionPack#load, loads the data from the pack using the registered…
WolfyScript Jan 6, 2022
a58ea20
Added ExpansionManager that initializes, loads and manages the expans…
WolfyScript Jan 6, 2022
ef7bce5
ExpansionManager will ignore duplicate packs.
WolfyScript Jan 6, 2022
e17f50d
Added more arguments to ResourceLoader#load
WolfyScript Jan 6, 2022
541fd72
Added ResourceLoaderParticleAnimations to load particle animations.
WolfyScript Jan 6, 2022
0a03fc9
Added ResourceLoaderJson to make it easier to load data from json zip…
WolfyScript Jan 6, 2022
6780dc4
Updated existing resource loaders to resourceLoaderJson.
WolfyScript Jan 6, 2022
8144d2a
Added ResourceLoaderCustomItems to load custom items from expansions
WolfyScript Jan 6, 2022
6311e16
Added prefix to ExpansionManager log messages
WolfyScript Jan 6, 2022
f855354
Register ResourceLoaderCustomItems
WolfyScript Jan 6, 2022
f6d4363
Merge branch 'master' into extension_packs
WolfyScript Jan 26, 2022
7e74baa
Merge branch 'master' into extension_packs
WolfyScript Feb 17, 2022
300bd0c
Started work on extension packs
WolfyScript Dec 28, 2021
acea389
Renamed extension to expansion
WolfyScript Jan 6, 2022
59e643c
Added ResourceLoader that can be registered to load specific resource…
WolfyScript Jan 6, 2022
57499a7
Added ResourceLoader registry
WolfyScript Jan 6, 2022
a85851c
Added ResourceLoaderParticleEffects as a prototype to load particle e…
WolfyScript Jan 6, 2022
bd4954b
ExpansionPack#load, loads the data from the pack using the registered…
WolfyScript Jan 6, 2022
b83fec9
Added ExpansionManager that initializes, loads and manages the expans…
WolfyScript Jan 6, 2022
d2ee439
ExpansionManager will ignore duplicate packs.
WolfyScript Jan 6, 2022
a0434c1
Added more arguments to ResourceLoader#load
WolfyScript Jan 6, 2022
8768cda
Added ResourceLoaderParticleAnimations to load particle animations.
WolfyScript Jan 6, 2022
af40f4b
Added ResourceLoaderJson to make it easier to load data from json zip…
WolfyScript Jan 6, 2022
691c5e5
Updated existing resource loaders to resourceLoaderJson.
WolfyScript Jan 6, 2022
c4b4c92
Added ResourceLoaderCustomItems to load custom items from expansions
WolfyScript Jan 6, 2022
cdfc65b
Added prefix to ExpansionManager log messages
WolfyScript Jan 6, 2022
d6bd25e
Register ResourceLoaderCustomItems
WolfyScript Jan 6, 2022
53434c9
Merge remote-tracking branch 'origin/extension_packs' into extension_…
WolfyScript Mar 8, 2022
f3302f1
Merge branch 'master' into extension_packs
WolfyScript Mar 9, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* 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 <https://www.gnu.org/licenses/>.
*/

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.LinkedList;
import java.util.List;
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;

private List<ExpansionPack> 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(LOG_PREFIX + "Initializing packs...");
File[] files = packsFolder.listFiles((dir, name) -> name.endsWith(".zip"));
if (files != null) {
this.packs = new LinkedList<>();
for (File file : files) {
try {
var pack = initPack(file);
if (pack != null) {
packs.add(pack);
}
} catch (IOException e) {
core.getLogger().warning(LOG_PREFIX + "Failed to read expansion zip file " + file.getName() + ": " + e.getMessage());
}
}
}
}

public void loadPacks() {
core.getLogger().info(LOG_PREFIX + "Loading packs...");
var registry = core.getRegistries().getExpansionResourceLoaders();
List<NamespacedKey> order = registry.getRegisterOrder();
for (ExpansionPack pack : packs) {
try {
pack.load(registry, order);
} catch (IOException e) {
core.getLogger().warning(LOG_PREFIX + "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<String> 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;
}
ExpansionPack pack = new ExpansionPack(metaFile, file, entryNames, core);
if (packs.contains(pack)) {
core.getLogger().warning(LOG_PREFIX + "Pack already initialised: \"" + metaFile.getPack().getNamespace() +"\" in file " + file.getName());
return null;
}
return pack;
}
}
}
return null;
}

public List<ExpansionPack> getPacks() {
return List.copyOf(packs);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
/*
* 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 <https://www.gnu.org/licenses/>.
*/

package me.wolfyscript.utilities.expansions;

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;
import java.util.List;
import java.util.zip.ZipFile;

public class ExpansionPack {

static final int VERSION = 1;

private final WolfyUtilCore core;
private final PackMetaFile meta;
private final File file;
private final List<String> zipEntryNames;
private final String rootDir;

ExpansionPack(@NotNull PackMetaFile meta, File file, List<String> 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<NamespacedKey> 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 + 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.
}
//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;
}
var entry = zipFile.getEntry(entryName);
if (entry != null) {
// Call the loader specific load method
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();
}
}
}
}
}
}
}

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();
}
}
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/

package me.wolfyscript.utilities.expansions;

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<String> authors;
private final String description;

@JsonCreator
PackInfo(@JsonProperty("version") int version, @JsonProperty("namespace") String namespace, @JsonProperty("authors") List<String> 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<String> getAuthors() {
return authors;
}

public String getDescription() {
return description;
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* 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 <https://www.gnu.org/licenses/>.
*/

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;
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(ExpansionPack pack, ZipFile zipFile, String root, ZipEntry entry, WolfyUtilCore core);

@Override
public NamespacedKey getNamespacedKey() {
return key;
}
}
Original file line number Diff line number Diff line change
@@ -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 <https://www.gnu.org/licenses/>.
*/

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<T extends Keyed> extends ResourceLoader {

private final Class<T> type;

protected ResourceLoaderJson(Plugin plugin, NamespacedKey namespacedKey, Class<T> 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());
}
}
}
Loading