9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2026-01-06 15:52:03 +00:00

framework for external items

This commit is contained in:
XiaoMoMi
2025-03-22 21:13:17 +08:00
parent 951a9c321e
commit 2e6eddf17a
20 changed files with 170 additions and 13 deletions

View File

@@ -280,7 +280,13 @@ public class AbstractItem<W extends ItemWrapper<I>, I> implements Item<I> {
@SuppressWarnings({"unchecked", "rawtypes"})
@Override
public Item<I> merge(Item<?> another) {
return new AbstractItem<>(this.factory, this.factory.merge(this.item, ((AbstractItem) another).item));
public Item<I> mergeCopy(Item<?> another) {
return new AbstractItem<>(this.factory, this.factory.mergeCopy(this.item, ((AbstractItem) another).item));
}
@SuppressWarnings({"unchecked", "rawtypes"})
@Override
public void merge(Item<I> another) {
this.factory.merge(this.item, ((AbstractItem) another).item);
}
}

View File

@@ -23,6 +23,7 @@ public abstract class AbstractItemManager<I> extends AbstractModelGenerator impl
protected static final List<Key> VANILLA_ITEMS = new ArrayList<>();
protected static final Map<Key, List<Holder<Key>>> VANILLA_ITEM_TAGS = new HashMap<>();
protected final Map<String, ExternalItemProvider<I>> externalItemProviders = new HashMap<>();
protected final Map<String, Function<Object, ItemModifier<I>>> dataFunctions = new HashMap<>();
protected final Map<Key, CustomItem<I>> customItems = new HashMap<>();
protected final Map<Key, List<Holder<Key>>> customItemTags;
@@ -47,6 +48,18 @@ public abstract class AbstractItemManager<I> extends AbstractModelGenerator impl
}
}
@Override
public ExternalItemProvider<I> getExternalItemProvider(String name) {
return this.externalItemProviders.get(name);
}
@Override
public boolean registerExternalItemProvider(ExternalItemProvider<I> externalItemProvider) {
if (this.externalItemProviders.containsKey(externalItemProvider.plugin())) return false;
this.externalItemProviders.put(externalItemProvider.plugin(), externalItemProvider);
return true;
}
@Override
public void unload() {
super.clearModelsToGenerate();
@@ -165,6 +178,13 @@ public abstract class AbstractItemManager<I> extends AbstractModelGenerator impl
}
private void registerFunctions() {
registerDataFunction((obj) -> {
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);
return new ExternalModifier<>(id, Objects.requireNonNull(provider, "Item provider " + plugin + " not found"));
}, "external");
registerDataFunction((obj) -> {
String name = obj.toString();
return new DisplayNameModifier<>(name);

View File

@@ -0,0 +1,11 @@
package net.momirealms.craftengine.core.item;
import org.jetbrains.annotations.Nullable;
public interface ExternalItemProvider<I> {
String plugin();
@Nullable
I build(String id, ItemBuildContext context);
}

View File

@@ -117,5 +117,7 @@ public interface Item<I> {
Object getLiteralObject();
Item<I> merge(Item<?> another);
Item<I> mergeCopy(Item<?> another);
void merge(Item<I> another);
}

View File

@@ -112,5 +112,7 @@ public abstract class ItemFactory<P extends Plugin, W extends ItemWrapper<I>, I>
protected abstract Optional<Integer> repairCost(ItemWrapper<I> item);
protected abstract ItemWrapper<I> merge(ItemWrapper<I> item1, ItemWrapper<I> item2);
protected abstract ItemWrapper<I> mergeCopy(ItemWrapper<I> item1, ItemWrapper<I> item2);
protected abstract void merge(ItemWrapper<I> item1, ItemWrapper<I> item2);
}

View File

@@ -51,6 +51,10 @@ public interface ItemManager<T> extends Reloadable, ModelGenerator, ConfigSectio
Key customItemId(T itemStack);
ExternalItemProvider<T> getExternalItemProvider(String name);
boolean registerExternalItemProvider(ExternalItemProvider<T> externalItemProvider);
Optional<CustomItem<T>> getCustomItem(Key key);
Optional<List<ItemBehavior>> getItemBehavior(Key key);

View File

@@ -0,0 +1,33 @@
package net.momirealms.craftengine.core.item.modifier;
import net.momirealms.craftengine.core.item.ExternalItemProvider;
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 ItemModifier<I> {
private final String id;
private final ExternalItemProvider<I> provider;
public ExternalModifier(String id, ExternalItemProvider<I> provider) {
this.id = id;
this.provider = provider;
}
@Override
public String name() {
return "external";
}
@SuppressWarnings("unchecked")
@Override
public void apply(Item<I> item, ItemBuildContext context) {
I another = this.provider.build(id, context);
if (another == null) {
CraftEngine.instance().logger().warn("'" + id + "' could not be found in " + provider.plugin());
return;
}
Item<I> anotherWrapped = (Item<I>) CraftEngine.instance().itemManager().wrap(another);
item.merge(anotherWrapped);
}
}

View File

@@ -96,7 +96,7 @@ public class CustomSmithingTransformRecipe<T> implements Recipe<T> {
Item<T> wrappedResult = (Item<T>) CraftEngine.instance().itemManager().wrap(result);
Item<T> finalResult = wrappedResult;
if (this.mergeComponents) {
finalResult = base.merge(wrappedResult);
finalResult = base.mergeCopy(wrappedResult);
}
for (ItemDataProcessor processor : this.processors) {
processor.accept(base, wrappedResult, finalResult);

View File

@@ -127,11 +127,11 @@ public abstract class CraftEngine implements Plugin {
// delay the reload so other plugins can register some parsers
this.scheduler.sync().runDelayed(() -> {
this.registerParsers();
this.itemManager.delayedInit();
this.reload();
this.guiManager.delayedInit();
this.recipeManager.delayedInit();
this.blockManager.delayedInit();
this.itemManager.delayedInit();
this.worldManager.delayedInit();
this.packManager.delayedInit();
this.furnitureManager.delayedInit();