From 5e6838bdc9f28b47c75e2e95e6a48b5580768fed Mon Sep 17 00:00:00 2001 From: cyberpwn Date: Thu, 9 Sep 2021 07:47:42 -0400 Subject: [PATCH] Registries --- api/build.gradle | 8 ++ .../java/com/volmit/iris/api/IrisAPI.java | 95 ++++++++++++++++++- build.gradle | 2 - .../volmit/iris/core/service/RegistrySVC.java | 39 ++++++++ .../iris/util/plugin/PluginRegistry.java | 65 +++++++++++++ .../iris/util/plugin/PluginRegistryGroup.java | 40 ++++++++ 6 files changed, 245 insertions(+), 4 deletions(-) create mode 100644 src/main/java/com/volmit/iris/core/service/RegistrySVC.java create mode 100644 src/main/java/com/volmit/iris/util/plugin/PluginRegistry.java create mode 100644 src/main/java/com/volmit/iris/util/plugin/PluginRegistryGroup.java diff --git a/api/build.gradle b/api/build.gradle index 73e5ec2cd..0fab76640 100644 --- a/api/build.gradle +++ b/api/build.gradle @@ -7,9 +7,17 @@ version '1.0.0' repositories { mavenCentral() + maven { + url 'https://hub.spigotmc.org/nexus/content/repositories/snapshots/' + content { + includeGroup 'org.spigotmc' + } + } } dependencies { + implementation rootProject + implementation 'org.spigotmc:spigot-api:1.17.1-R0.1-SNAPSHOT' testImplementation 'org.junit.jupiter:junit-jupiter-api:5.7.0' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.7.0' } diff --git a/api/src/main/java/com/volmit/iris/api/IrisAPI.java b/api/src/main/java/com/volmit/iris/api/IrisAPI.java index f68e5795c..ce1822816 100644 --- a/api/src/main/java/com/volmit/iris/api/IrisAPI.java +++ b/api/src/main/java/com/volmit/iris/api/IrisAPI.java @@ -18,7 +18,98 @@ package com.volmit.iris.api; -public interface IrisAPI -{ +import com.volmit.iris.Iris; +import com.volmit.iris.core.service.RegistrySVC; +import com.volmit.iris.core.tools.IrisToolbelt; +import com.volmit.iris.engine.data.cache.AtomicCache; +import com.volmit.iris.util.plugin.PluginRegistryGroup; +import org.bukkit.World; +import org.bukkit.block.data.BlockData; +import java.util.List; +import java.util.function.Supplier; + +public class IrisAPI +{ + private static final AtomicCache> customBlock = new AtomicCache<>(); + + /** + * Checks if the given world is an Iris World + * + * @param world the world + * @return true if it is an Iris world + */ + public static boolean isIrisWorld(World world) { + return IrisToolbelt.isIrisWorld(world); + } + + /** + * Checks if the given world is an Iris World with studio mode + * @param world the world + * @return true if it is an Iris World & is in Studio Mode + */ + public static boolean isIrisStudioWorld(World world) { + return IrisToolbelt.isIrisStudioWorld(world); + } + + /** + * Used for registering ids into custom blocks + * @return the registry + */ + public static RegistryHolder getCustomBlockRegistry() + { + return customBlock.aquire(RegistryHolder::new); + } + + public static class RegistryHolder + { + private Supplier> registry; + + /** + * Unregister a node + * @param namespace the namespace (your plugin id or something) + * @param id the identifier for the node + */ + public void unregister(String namespace, String id) + { + Iris.service(RegistrySVC.class).getCustomBlockRegistry() + .getRegistry(namespace).unregister(id); + } + + /** + * Unregister all nodes by namespace + * @param namespace the namespace (such as your plugin) + */ + public void unregisterAll(String namespace) + { + Iris.service(RegistrySVC.class).getCustomBlockRegistry() + .getRegistry(namespace).unregisterAll(); + } + + /** + * Register a node under a namespace & id + * such as myplugin:ruby_ore which should resolve to an object that + * could be used by the generator. + * + * @param namespace the namespace (of this plugin) (myplugin) + * @param id the identifier for this node (ruby_ore) + * @param block the provider for this node data (always return a new instance!) + */ + public void register(String namespace, String id, Supplier block) + { + Iris.service(RegistrySVC.class).getCustomBlockRegistry() + .getRegistry(namespace).register(id, block); + } + + /** + * Get all registered nodes under a namespace + * @param namespace the namespace such as your plugin + * @return the registry list (ids) + */ + public List getRegsitries(String namespace) + { + return Iris.service(RegistrySVC.class).getCustomBlockRegistry() + .getRegistry(namespace).getRegistries(); + } + } } diff --git a/build.gradle b/build.gradle index 3fe748939..9b648883d 100644 --- a/build.gradle +++ b/build.gradle @@ -146,7 +146,6 @@ shadowJar { include(dependency('io.papermc:paperlib')) include(dependency('com.dfsek:Paralithic')) include(dependency('net.kyori:')) - include(dependency('com.volmit.iris.api:')) } } @@ -182,7 +181,6 @@ dependencies { implementation "net.kyori:adventure-text-minimessage:4.2.0-SNAPSHOT" implementation "net.kyori:adventure-platform-bukkit:4.0.0-SNAPSHOT" implementation 'net.kyori:adventure-api:4.9.1' - implementation project(":api") // Dynamically Loaded implementation 'io.timeandspace:smoothie-map:2.0.2' diff --git a/src/main/java/com/volmit/iris/core/service/RegistrySVC.java b/src/main/java/com/volmit/iris/core/service/RegistrySVC.java new file mode 100644 index 000000000..f8bdc4576 --- /dev/null +++ b/src/main/java/com/volmit/iris/core/service/RegistrySVC.java @@ -0,0 +1,39 @@ +/* + * Iris is a World Generator for Minecraft Bukkit Servers + * Copyright (c) 2021 Arcane Arts (Volmit Software) + * + * 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 com.volmit.iris.core.service; + +import com.volmit.iris.util.plugin.IrisService; +import com.volmit.iris.util.plugin.PluginRegistryGroup; +import lombok.Data; +import org.bukkit.block.data.BlockData; + +@Data +public class RegistrySVC implements IrisService { + private PluginRegistryGroup customBlockRegistry; + + @Override + public void onEnable() { + customBlockRegistry = new PluginRegistryGroup<>(); + } + + @Override + public void onDisable() { + + } +} diff --git a/src/main/java/com/volmit/iris/util/plugin/PluginRegistry.java b/src/main/java/com/volmit/iris/util/plugin/PluginRegistry.java new file mode 100644 index 000000000..b8f0e0274 --- /dev/null +++ b/src/main/java/com/volmit/iris/util/plugin/PluginRegistry.java @@ -0,0 +1,65 @@ +/* + * Iris is a World Generator for Minecraft Bukkit Servers + * Copyright (c) 2021 Arcane Arts (Volmit Software) + * + * 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 com.volmit.iris.util.plugin; + +import com.volmit.iris.engine.object.annotations.Required; +import com.volmit.iris.util.collection.KList; +import com.volmit.iris.util.collection.KMap; +import lombok.Getter; +import lombok.RequiredArgsConstructor; +import org.bukkit.plugin.Plugin; + +import java.util.function.Supplier; + +@RequiredArgsConstructor +public class PluginRegistry { + private final KMap> registry = new KMap<>(); + @Getter + private final String namespace; + + public void unregisterAll() + { + registry.clear(); + } + + public KList getRegistries() + { + return registry.k(); + } + + public T get(String s) + { + if(!registry.containsKey(s)) + { + return null; + } + + return registry.get(s).get(); + } + + public void register(String s, Supplier t) + { + registry.put(s, t); + } + + public void unregister(String s) + { + registry.remove(s); + } +} diff --git a/src/main/java/com/volmit/iris/util/plugin/PluginRegistryGroup.java b/src/main/java/com/volmit/iris/util/plugin/PluginRegistryGroup.java new file mode 100644 index 000000000..d521adaaa --- /dev/null +++ b/src/main/java/com/volmit/iris/util/plugin/PluginRegistryGroup.java @@ -0,0 +1,40 @@ +/* + * Iris is a World Generator for Minecraft Bukkit Servers + * Copyright (c) 2021 Arcane Arts (Volmit Software) + * + * 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 com.volmit.iris.util.plugin; + +import com.volmit.iris.util.collection.KMap; + +public class PluginRegistryGroup { + private final KMap> registries = new KMap<>(); + + public void clearRegistries() + { + registries.clear(); + } + + public void removeRegistry(String namespace) + { + registries.remove(namespace); + } + + public PluginRegistry getRegistry(String namespace) + { + return registries.computeIfAbsent(namespace, PluginRegistry::new); + } +}