mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2025-12-19 15:09:15 +00:00
添加外部配方原料支持
This commit is contained in:
@@ -14,7 +14,7 @@ import java.util.Optional;
|
||||
public class CustomFishingProvider implements ExternalItemProvider<ItemStack> {
|
||||
@Override
|
||||
public String plugin() {
|
||||
return "CustomFishing";
|
||||
return "customfishing";
|
||||
}
|
||||
|
||||
@SuppressWarnings("UnstableApiUsage")
|
||||
|
||||
@@ -15,18 +15,22 @@ public class MMOItemsProvider implements ExternalItemProvider<ItemStack> {
|
||||
|
||||
@Override
|
||||
public String plugin() {
|
||||
return "MMOItems";
|
||||
return "mmoitems";
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable ItemStack build(String id, ItemBuildContext context) {
|
||||
String[] split = id.split(":", 2);
|
||||
if (split.length == 1) {
|
||||
split = split[0].split("_", 2);
|
||||
}
|
||||
if (split.length == 1) return new ItemStack(Material.AIR);
|
||||
MMOItem mmoItem = MMOItems.plugin.getMMOItem(Type.get(split[0]), split[1].toUpperCase());
|
||||
return mmoItem == null ? new ItemStack(Material.AIR) : requireNonNull(mmoItem.newBuilder().build());
|
||||
}
|
||||
|
||||
@Override
|
||||
public String id(ItemStack item) {
|
||||
return MMOItems.getType(item) + ":" + MMOItems.getID(item);
|
||||
return MMOItems.getType(item) + "_" + MMOItems.getID(item);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,7 +11,7 @@ public class MythicMobsProvider implements ExternalItemProvider<ItemStack> {
|
||||
|
||||
@Override
|
||||
public String plugin() {
|
||||
return "MythicMobs";
|
||||
return "mythicmobs";
|
||||
}
|
||||
|
||||
@Nullable
|
||||
|
||||
@@ -12,7 +12,7 @@ public class NeigeItemsProvider implements ExternalItemProvider<ItemStack> {
|
||||
|
||||
@Override
|
||||
public String plugin() {
|
||||
return "NeigeItems";
|
||||
return "neigeitems";
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -77,6 +77,21 @@ public class BukkitItemManager extends AbstractItemManager<ItemStack> {
|
||||
this.emptyUniqueItem = new UniqueIdItem<>(UniqueKey.AIR, this.emptyItem);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
@Override
|
||||
public void delayedLoad() {
|
||||
super.delayedLoad();
|
||||
List<ExternalItemProvider<ItemStack>> sources = new ArrayList<>();
|
||||
for (String externalSource : Config.recipeIngredientSources()) {
|
||||
String sourceId = externalSource.toLowerCase(Locale.ENGLISH);
|
||||
ExternalItemProvider<ItemStack> provider = getExternalItemProvider(sourceId);
|
||||
if (provider != null) {
|
||||
sources.add(provider);
|
||||
}
|
||||
}
|
||||
this.factory.resetRecipeIngredientSources(sources.isEmpty() ? null : sources.toArray(new ExternalItemProvider[0]));
|
||||
}
|
||||
|
||||
@Override
|
||||
public UniqueIdItem<ItemStack> uniqueEmptyItem() {
|
||||
return this.emptyUniqueItem;
|
||||
|
||||
@@ -6,22 +6,28 @@ import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.CoreReflect
|
||||
import net.momirealms.craftengine.bukkit.plugin.reflection.minecraft.MBuiltInRegistries;
|
||||
import net.momirealms.craftengine.bukkit.util.ItemTags;
|
||||
import net.momirealms.craftengine.bukkit.util.KeyUtils;
|
||||
import net.momirealms.craftengine.core.item.ExternalItemProvider;
|
||||
import net.momirealms.craftengine.core.item.ItemFactory;
|
||||
import net.momirealms.craftengine.core.item.ItemKeys;
|
||||
import net.momirealms.craftengine.core.item.ItemWrapper;
|
||||
import net.momirealms.craftengine.core.item.data.JukeboxPlayable;
|
||||
import net.momirealms.craftengine.core.item.setting.EquipmentData;
|
||||
import net.momirealms.craftengine.core.plugin.CraftEngine;
|
||||
import net.momirealms.craftengine.core.util.CharacterUtils;
|
||||
import net.momirealms.craftengine.core.util.Key;
|
||||
import net.momirealms.craftengine.core.util.StringUtils;
|
||||
import net.momirealms.craftengine.core.util.UniqueKey;
|
||||
import net.momirealms.sparrow.nbt.Tag;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.util.Locale;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
|
||||
public abstract class BukkitItemFactory<W extends ItemWrapper<ItemStack>> extends ItemFactory<W, ItemStack> {
|
||||
private boolean hasExternalRecipeSource = false;
|
||||
private ExternalItemProvider<ItemStack>[] recipeIngredientSources = null;
|
||||
|
||||
protected BukkitItemFactory(CraftEngine plugin) {
|
||||
super(plugin);
|
||||
@@ -52,6 +58,16 @@ public abstract class BukkitItemFactory<W extends ItemWrapper<ItemStack>> extend
|
||||
}
|
||||
}
|
||||
|
||||
public void resetRecipeIngredientSources(ExternalItemProvider<ItemStack>[] recipeIngredientSources) {
|
||||
if (recipeIngredientSources == null || recipeIngredientSources.length == 0) {
|
||||
this.recipeIngredientSources = null;
|
||||
this.hasExternalRecipeSource = false;
|
||||
} else {
|
||||
this.recipeIngredientSources = recipeIngredientSources;
|
||||
this.hasExternalRecipeSource = true;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
@Override
|
||||
protected byte[] toByteArray(W item) {
|
||||
@@ -82,6 +98,14 @@ public abstract class BukkitItemFactory<W extends ItemWrapper<ItemStack>> extend
|
||||
|
||||
@Override
|
||||
protected UniqueKey recipeIngredientID(W item) {
|
||||
if (this.hasExternalRecipeSource) {
|
||||
for (ExternalItemProvider<ItemStack> source : this.recipeIngredientSources) {
|
||||
String id = source.id(item.getItem());
|
||||
if (id != null) {
|
||||
return UniqueKey.create(Key.of(source.plugin(), StringUtils.toLowerCase(id)));
|
||||
}
|
||||
}
|
||||
}
|
||||
return UniqueKey.create(id(item));
|
||||
}
|
||||
|
||||
|
||||
@@ -227,7 +227,6 @@ public class BukkitCraftEngine extends CraftEngine {
|
||||
}
|
||||
}, 1, 1);
|
||||
}
|
||||
super.compatibilityManager().onDelayedEnable();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -296,6 +296,8 @@ recipe:
|
||||
# Selective recipe disabling (safer alternative to 'all: true')
|
||||
# Example: ["minecraft:wooden_sword", "minecraft:stone_hoe"]
|
||||
list: []
|
||||
# You can use items from other plugins by adding the supported plugin names here
|
||||
ingredient-sources: []
|
||||
|
||||
gui:
|
||||
browser:
|
||||
|
||||
@@ -105,6 +105,7 @@ public abstract class AbstractItemManager<I> extends AbstractModelGenerator impl
|
||||
|
||||
@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);
|
||||
return true;
|
||||
@@ -510,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);
|
||||
ExternalItemProvider<I> provider = AbstractItemManager.this.getExternalItemProvider(plugin.toLowerCase(Locale.ENGLISH));
|
||||
return new ExternalModifier<>(id, Objects.requireNonNull(provider, "Item provider " + plugin + " not found"));
|
||||
}, "external");
|
||||
if (VersionHelper.isOrAbove1_20_5()) {
|
||||
|
||||
@@ -217,6 +217,7 @@ public abstract class CraftEngine implements Plugin {
|
||||
this.fontManager.delayedInit();
|
||||
this.vanillaLootManager.delayedInit();
|
||||
this.advancementManager.delayedInit();
|
||||
this.compatibilityManager.onDelayedEnable();
|
||||
// reload the plugin
|
||||
try {
|
||||
this.reloadPlugin(Runnable::run, Runnable::run, true);
|
||||
|
||||
@@ -25,4 +25,18 @@ public final class StringUtils {
|
||||
result[index] = s.substring(start);
|
||||
return result;
|
||||
}
|
||||
|
||||
public static String toLowerCase(String str) {
|
||||
if (str == null) {
|
||||
return null;
|
||||
}
|
||||
char[] chars = str.toCharArray();
|
||||
for (int i = 0; i < chars.length; i++) {
|
||||
char c = chars[i];
|
||||
if (c >= 'A' && c <= 'Z') {
|
||||
chars[i] = (char) (c + 32);
|
||||
}
|
||||
}
|
||||
return new String(chars);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ org.gradle.jvmargs=-Xmx1G
|
||||
# Project settings
|
||||
# Rule: [major update].[feature update].[bug fix]
|
||||
project_version=0.0.60.1
|
||||
config_version=41
|
||||
config_version=42
|
||||
lang_version=22
|
||||
project_group=net.momirealms
|
||||
latest_supported_version=1.21.7
|
||||
|
||||
Reference in New Issue
Block a user