mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2025-12-30 20:39:10 +00:00
更正命名
This commit is contained in:
@@ -45,7 +45,7 @@ public abstract class AbstractItemManager<I> extends AbstractModelGenerator impl
|
||||
|
||||
private final ItemParser itemParser;
|
||||
private final EquipmentParser equipmentParser;
|
||||
protected final Map<String, ExternalItemProvider<I>> externalItemProviders = new HashMap<>();
|
||||
protected final Map<String, ExternalItemSource<I>> externalItemSources = new HashMap<>();
|
||||
protected final Map<String, Function<Object, ItemDataModifier<I>>> dataFunctions = new HashMap<>();
|
||||
protected final Map<Key, CustomItem<I>> customItems = new HashMap<>();
|
||||
protected final Map<Key, List<UniqueKey>> customItemTags = new HashMap<>();
|
||||
@@ -99,15 +99,15 @@ public abstract class AbstractItemManager<I> extends AbstractModelGenerator impl
|
||||
}
|
||||
|
||||
@Override
|
||||
public ExternalItemProvider<I> getExternalItemProvider(String name) {
|
||||
return this.externalItemProviders.get(name);
|
||||
public ExternalItemSource<I> getExternalItemSource(String name) {
|
||||
return this.externalItemSources.get(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean registerExternalItemProvider(ExternalItemProvider<I> externalItemProvider) {
|
||||
if (!ResourceLocation.isValidNamespace(externalItemProvider.plugin())) return false;
|
||||
if (this.externalItemProviders.containsKey(externalItemProvider.plugin())) return false;
|
||||
this.externalItemProviders.put(externalItemProvider.plugin(), externalItemProvider);
|
||||
public boolean registerExternalItemSource(ExternalItemSource<I> externalItemSource) {
|
||||
if (!ResourceLocation.isValidNamespace(externalItemSource.plugin())) return false;
|
||||
if (this.externalItemSources.containsKey(externalItemSource.plugin())) return false;
|
||||
this.externalItemSources.put(externalItemSource.plugin(), externalItemSource);
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -511,7 +511,7 @@ public abstract class AbstractItemManager<I> extends AbstractModelGenerator impl
|
||||
Map<String, Object> data = MiscUtils.castToMap(obj, false);
|
||||
String plugin = data.get("plugin").toString();
|
||||
String id = data.get("id").toString();
|
||||
ExternalItemProvider<I> provider = AbstractItemManager.this.getExternalItemProvider(plugin.toLowerCase(Locale.ENGLISH));
|
||||
ExternalItemSource<I> provider = AbstractItemManager.this.getExternalItemSource(plugin.toLowerCase(Locale.ENGLISH));
|
||||
return new ExternalModifier<>(id, Objects.requireNonNull(provider, "Item provider " + plugin + " not found"));
|
||||
}, "external");
|
||||
if (VersionHelper.isOrAbove1_20_5()) {
|
||||
|
||||
@@ -2,7 +2,7 @@ package net.momirealms.craftengine.core.item;
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public interface ExternalItemProvider<I> {
|
||||
public interface ExternalItemSource<I> {
|
||||
|
||||
String plugin();
|
||||
|
||||
@@ -61,9 +61,9 @@ public interface ItemManager<T> extends Manageable, ModelGenerator {
|
||||
|
||||
Key customItemId(T itemStack);
|
||||
|
||||
ExternalItemProvider<T> getExternalItemProvider(String name);
|
||||
ExternalItemSource<T> getExternalItemSource(String name);
|
||||
|
||||
boolean registerExternalItemProvider(ExternalItemProvider<T> externalItemProvider);
|
||||
boolean registerExternalItemSource(ExternalItemSource<T> externalItemSource);
|
||||
|
||||
Optional<Equipment> getEquipment(Key key);
|
||||
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
package net.momirealms.craftengine.core.item.modifier;
|
||||
|
||||
import net.momirealms.craftengine.core.item.ExternalItemProvider;
|
||||
import net.momirealms.craftengine.core.item.ExternalItemSource;
|
||||
import net.momirealms.craftengine.core.item.Item;
|
||||
import net.momirealms.craftengine.core.item.ItemBuildContext;
|
||||
import net.momirealms.craftengine.core.plugin.CraftEngine;
|
||||
|
||||
public class ExternalModifier<I> implements ItemDataModifier<I> {
|
||||
private final String id;
|
||||
private final ExternalItemProvider<I> provider;
|
||||
private final ExternalItemSource<I> provider;
|
||||
|
||||
public ExternalModifier(String id, ExternalItemProvider<I> provider) {
|
||||
public ExternalModifier(String id, ExternalItemSource<I> provider) {
|
||||
this.id = id;
|
||||
this.provider = provider;
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ import net.momirealms.craftengine.core.plugin.classpath.ClassPathAppender;
|
||||
import net.momirealms.craftengine.core.plugin.command.CraftEngineCommandManager;
|
||||
import net.momirealms.craftengine.core.plugin.command.sender.SenderFactory;
|
||||
import net.momirealms.craftengine.core.plugin.compatibility.CompatibilityManager;
|
||||
import net.momirealms.craftengine.core.plugin.compatibility.PluginTaskRegistry;
|
||||
import net.momirealms.craftengine.core.plugin.config.Config;
|
||||
import net.momirealms.craftengine.core.plugin.config.template.TemplateManager;
|
||||
import net.momirealms.craftengine.core.plugin.config.template.TemplateManagerImpl;
|
||||
@@ -37,6 +38,7 @@ import net.momirealms.craftengine.core.sound.SoundManager;
|
||||
import net.momirealms.craftengine.core.world.WorldManager;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.core.Logger;
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -73,6 +75,9 @@ public abstract class CraftEngine implements Plugin {
|
||||
protected GlobalVariableManager globalVariableManager;
|
||||
protected ProjectileManager projectileManager;
|
||||
|
||||
private final PluginTaskRegistry preLoadTaskRegistry = new PluginTaskRegistry();
|
||||
private final PluginTaskRegistry postLoadTaskRegistry = new PluginTaskRegistry();
|
||||
|
||||
private final Consumer<CraftEngine> reloadEventDispatcher;
|
||||
private boolean isReloading;
|
||||
private boolean isInitializing;
|
||||
@@ -205,6 +210,7 @@ public abstract class CraftEngine implements Plugin {
|
||||
this.commandManager.registerDefaultFeatures();
|
||||
// delay the reload so other plugins can register some custom parsers
|
||||
this.scheduler.sync().runDelayed(() -> {
|
||||
this.preLoadTaskRegistry.executeTasks();
|
||||
this.registerDefaultParsers();
|
||||
// hook external item plugins
|
||||
this.itemManager.delayedInit();
|
||||
@@ -231,6 +237,7 @@ public abstract class CraftEngine implements Plugin {
|
||||
// set up some platform extra tasks
|
||||
this.platformDelayedEnable();
|
||||
this.isInitializing = false;
|
||||
this.postLoadTaskRegistry.executeTasks();
|
||||
this.scheduler.executeAsync(() -> this.packManager.initCachedAssets());
|
||||
});
|
||||
}
|
||||
@@ -462,4 +469,14 @@ public abstract class CraftEngine implements Plugin {
|
||||
public Platform platform() {
|
||||
return platform;
|
||||
}
|
||||
|
||||
@ApiStatus.Experimental
|
||||
public PluginTaskRegistry preLoadTaskRegistry() {
|
||||
return preLoadTaskRegistry;
|
||||
}
|
||||
|
||||
@ApiStatus.Experimental
|
||||
public PluginTaskRegistry postLoadTaskRegistry() {
|
||||
return postLoadTaskRegistry;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import net.momirealms.craftengine.core.pack.PackManager;
|
||||
import net.momirealms.craftengine.core.plugin.classpath.ClassPathAppender;
|
||||
import net.momirealms.craftengine.core.plugin.command.sender.SenderFactory;
|
||||
import net.momirealms.craftengine.core.plugin.compatibility.CompatibilityManager;
|
||||
import net.momirealms.craftengine.core.plugin.compatibility.PluginTaskRegistry;
|
||||
import net.momirealms.craftengine.core.plugin.config.Config;
|
||||
import net.momirealms.craftengine.core.plugin.config.template.TemplateManager;
|
||||
import net.momirealms.craftengine.core.plugin.context.GlobalVariableManager;
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
package net.momirealms.craftengine.core.plugin.compatibility;
|
||||
|
||||
public final class PluginTask {
|
||||
private final Runnable task;
|
||||
private final Priority priority;
|
||||
private final String plugin;
|
||||
|
||||
private PluginTask(Runnable task, String plugin, Priority priority) {
|
||||
this.task = task;
|
||||
this.priority = priority;
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
public static PluginTask create(Runnable task, String plugin, Priority priority) {
|
||||
return new PluginTask(task, plugin, priority);
|
||||
}
|
||||
|
||||
public static PluginTask create(Runnable task, String plugin) {
|
||||
return new PluginTask(task, plugin, Priority.tail());
|
||||
}
|
||||
|
||||
public String plugin() {
|
||||
return plugin;
|
||||
}
|
||||
|
||||
public Priority priority() {
|
||||
return priority;
|
||||
}
|
||||
|
||||
public Runnable task() {
|
||||
return task;
|
||||
}
|
||||
|
||||
public static class Priority {
|
||||
public enum Position {
|
||||
BEFORE_PLUGIN,
|
||||
AFTER_PLUGIN,
|
||||
HEAD,
|
||||
TAIL
|
||||
}
|
||||
|
||||
private final Position position;
|
||||
private final String relativePlugin;
|
||||
|
||||
private Priority(Position position, String relativePlugin) {
|
||||
this.position = position;
|
||||
this.relativePlugin = relativePlugin;
|
||||
}
|
||||
|
||||
public static Priority before(String pluginName) {
|
||||
return new Priority(Position.BEFORE_PLUGIN, pluginName);
|
||||
}
|
||||
|
||||
public static Priority after(String pluginName) {
|
||||
return new Priority(Position.AFTER_PLUGIN, pluginName);
|
||||
}
|
||||
|
||||
public static Priority head() {
|
||||
return new Priority(Position.HEAD, null);
|
||||
}
|
||||
|
||||
public static Priority tail() {
|
||||
return new Priority(Position.TAIL, null);
|
||||
}
|
||||
|
||||
Position position() {
|
||||
return position;
|
||||
}
|
||||
|
||||
String relativePlugin() {
|
||||
return relativePlugin;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
package net.momirealms.craftengine.core.plugin.compatibility;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public final class PluginTaskRegistry {
|
||||
private static class Node {
|
||||
final PluginTask task;
|
||||
Node prev;
|
||||
Node next;
|
||||
|
||||
Node(PluginTask task) {
|
||||
this.task = task;
|
||||
}
|
||||
}
|
||||
|
||||
private final Node head = new Node(null); // 哨兵头节点
|
||||
private final Node tail = new Node(null); // 哨兵尾节点
|
||||
private final Map<String, Node> pluginNodeMap = new HashMap<>();
|
||||
|
||||
public PluginTaskRegistry() {
|
||||
head.next = tail;
|
||||
tail.prev = head;
|
||||
}
|
||||
|
||||
public void registerTask(PluginTask task) {
|
||||
PluginTask.Priority priority = task.priority();
|
||||
Node newNode = new Node(task);
|
||||
String pluginName = task.plugin();
|
||||
|
||||
if (this.pluginNodeMap.containsKey(pluginName)) {
|
||||
throw new IllegalArgumentException("Duplicate task for plugin: " + pluginName);
|
||||
}
|
||||
|
||||
switch (priority.position()) {
|
||||
case HEAD:
|
||||
insertAfter(this.head, newNode);
|
||||
break;
|
||||
case TAIL:
|
||||
insertBefore(this.tail, newNode);
|
||||
break;
|
||||
case BEFORE_PLUGIN:
|
||||
Node targetBefore = this.pluginNodeMap.get(priority.relativePlugin());
|
||||
if (targetBefore == null) {
|
||||
throw new IllegalArgumentException("Target plugin not found: " + priority.relativePlugin());
|
||||
}
|
||||
insertBefore(targetBefore, newNode);
|
||||
break;
|
||||
case AFTER_PLUGIN:
|
||||
Node targetAfter =this. pluginNodeMap.get(priority.relativePlugin());
|
||||
if (targetAfter == null) {
|
||||
throw new IllegalArgumentException("Target plugin not found: " + priority.relativePlugin());
|
||||
}
|
||||
insertAfter(targetAfter, newNode);
|
||||
break;
|
||||
}
|
||||
|
||||
this.pluginNodeMap.put(pluginName, newNode);
|
||||
}
|
||||
|
||||
private void insertAfter(Node existing, Node newNode) {
|
||||
newNode.next = existing.next;
|
||||
newNode.prev = existing;
|
||||
existing.next.prev = newNode;
|
||||
existing.next = newNode;
|
||||
}
|
||||
|
||||
private void insertBefore(Node existing, Node newNode) {
|
||||
newNode.prev = existing.prev;
|
||||
newNode.next = existing;
|
||||
existing.prev.next = newNode;
|
||||
existing.prev = newNode;
|
||||
}
|
||||
|
||||
public void executeTasks() {
|
||||
try {
|
||||
Node current = head.next;
|
||||
while (current != tail) {
|
||||
current.task.task().run();
|
||||
current = current.next;
|
||||
}
|
||||
} catch (Throwable ignored) {
|
||||
// 不要管其他插件的异常,应该他们自己处理
|
||||
}
|
||||
}
|
||||
|
||||
public String getExecutionOrder() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
Node current = head.next;
|
||||
while (current != tail) {
|
||||
sb.append(current.task.plugin());
|
||||
if (current.next != tail) sb.append(" -> ");
|
||||
current = current.next;
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user