Skip to content

Commit

Permalink
[1.9.0-dev1]准备重构配方配置整体结构
Browse files Browse the repository at this point in the history
  • Loading branch information
YufiriaMazenta committed Jan 7, 2024
1 parent 1dd6517 commit 68d76ff
Show file tree
Hide file tree
Showing 15 changed files with 165 additions and 20 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import java.text.SimpleDateFormat
version = "1.8.7"
version = "1.9.0-dev1"

plugins {
`java-library`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.github.yufiriamazenta.craftorithm.item.ItemManager;
import com.github.yufiriamazenta.craftorithm.menu.display.RecipeGroupListMenu;
import com.github.yufiriamazenta.craftorithm.recipe.RecipeGroup;
import com.github.yufiriamazenta.craftorithm.recipe.registry.impl.CookingRecipeRegistry;
import com.github.yufiriamazenta.craftorithm.recipe.registry.CookingRecipeRegistry;
import com.github.yufiriamazenta.craftorithm.util.ItemUtils;
import crypticlib.chat.TextProcessor;
import crypticlib.conversation.Conversation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
import com.github.yufiriamazenta.craftorithm.config.PluginConfigs;
import com.github.yufiriamazenta.craftorithm.exception.UnsupportedVersionException;
import com.github.yufiriamazenta.craftorithm.item.ItemManager;
import com.github.yufiriamazenta.craftorithm.recipe.registry.RecipeRegistry;
import com.github.yufiriamazenta.craftorithm.recipe.registry.impl.*;
import com.github.yufiriamazenta.craftorithm.recipe.registry.*;
import crypticlib.CrypticLib;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
package com.github.yufiriamazenta.craftorithm.recipe;

import com.github.yufiriamazenta.craftorithm.config.PluginConfigs;
import com.github.yufiriamazenta.craftorithm.recipe.registry.RecipeRegistry;
import crypticlib.config.ConfigWrapper;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.Recipe;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

//TODO 重构RecipeGroup
public class RecipeGroup {

private String groupName;
private List<NamespacedKey> groupRecipeKeys = new CopyOnWriteArrayList<>();
private Map<NamespacedKey, RecipeRegistry> groupRegistrys = new ConcurrentHashMap<>();
private final RecipeType recipeType;
private ConfigWrapper recipeGroupConfig;
private int sortId;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package com.github.yufiriamazenta.craftorithm.recipe.loader;

import com.github.yufiriamazenta.craftorithm.Craftorithm;
import com.github.yufiriamazenta.craftorithm.item.ItemManager;
import com.github.yufiriamazenta.craftorithm.recipe.RecipeGroup;
import com.github.yufiriamazenta.craftorithm.recipe.RecipeType;
import com.github.yufiriamazenta.craftorithm.recipe.registry.RecipeRegistry;
import com.github.yufiriamazenta.craftorithm.recipe.registry.ShapedRecipeRegistry;
import crypticlib.config.ConfigWrapper;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.Tag;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.RecipeChoice;

import java.lang.reflect.Field;
import java.util.*;
import java.util.function.BiFunction;
import java.util.function.Function;

public class RecipeGroupLoader {

public static final String TYPE_KEY = "type", RESULT_KEY = "result";
public static final List<String> GLOBAL_KEYS = Arrays.asList(TYPE_KEY, RESULT_KEY);
public final Map<RecipeType, BiFunction<String, ConfigurationSection, RecipeRegistry>> RECIPE_LOAD_MAP = new HashMap<>();
protected ConfigWrapper configWrapper;
protected RecipeType globalType;
protected ItemStack globalResult;
protected String groupName;

public RecipeGroupLoader(String groupName, ConfigWrapper configWrapper) {
this.configWrapper = configWrapper;
this.groupName = groupName;
String globalTypeStr = configWrapper.config().getString(TYPE_KEY);
if (globalTypeStr != null)
this.globalType = RecipeType.valueOf(globalTypeStr.toUpperCase());
String globalResultStr = configWrapper.config().getString(RESULT_KEY);
if (globalResultStr != null)
this.globalResult = ItemManager.INSTANCE.matchItem(globalResultStr);
loadDefRecipeLoadMap();
}

public RecipeGroup load() {
YamlConfiguration config = configWrapper.config();
List<String> recipeKeys = new ArrayList<>(config.getKeys(false));
recipeKeys.removeAll(GLOBAL_KEYS);
for (String recipeKey : recipeKeys) {
if (!config.isConfigurationSection(recipeKey))
continue;
ConfigurationSection recipeCfgSection = config.getConfigurationSection(recipeKey);
RecipeType recipeType;
String recipeTypeStr = recipeCfgSection.getString(TYPE_KEY);
if (recipeTypeStr == null)
recipeType = globalType;
else
recipeType = RecipeType.valueOf(recipeTypeStr.toUpperCase());
//TODO 加载配方注册器
}
return null;
}

private void loadDefRecipeLoadMap() {
RECIPE_LOAD_MAP.put(RecipeType.SHAPED, (subName, configSection) -> {
ItemStack result;
String resultStr = configSection.getString(RESULT_KEY);
if (resultStr == null)
result = globalResult;
else
result = ItemManager.INSTANCE.matchItem(resultStr);
List<String> shapeList = configSection.getStringList("source.shape");
Map<Character, RecipeChoice> ingredientMap = new HashMap<>();
ConfigurationSection ingredientCfgSection = configSection.getConfigurationSection("source.ingredient_map");
if (ingredientCfgSection == null)
throw new IllegalArgumentException("Ingredient map cannot be null");
for (String key : ingredientCfgSection.getKeys(false)) {
String ingredientStr = ingredientCfgSection.getString(key);
if (ingredientStr == null)
ingredientStr = Material.AIR.getKey().toString();
ingredientStr = ingredientStr.split(" ")[0];
RecipeChoice recipeChoice = matchRecipeChoice(ingredientStr);
ingredientMap.put(key.charAt(0), recipeChoice);
}
NamespacedKey namespacedKey = new NamespacedKey(Craftorithm.instance(), groupName + "." + subName);
String[] shape = new String[shapeList.size()];
return new ShapedRecipeRegistry(groupName, namespacedKey, result)
.setShape(shapeList.toArray(shape))
.setRecipeChoiceMap(ingredientMap);
});
}

public RecipeChoice matchRecipeChoice(String ingredientName) {
if (!ingredientName.contains(":")) {
Material material = Material.matchMaterial(ingredientName);
if (material == null) {
throw new IllegalArgumentException(ingredientName + " is a not exist item type");
}
return new RecipeChoice.MaterialChoice(material);
}
int index = ingredientName.indexOf(":");
String namespace = ingredientName.substring(0, index);
namespace = namespace.toLowerCase();
switch (namespace) {
case "minecraft":
Material material = Material.matchMaterial(ingredientName);
if (material == null) {
throw new IllegalArgumentException(ingredientName + " is a not exist item type");
}
return new RecipeChoice.MaterialChoice(material);
case "tag":
String tagStr = ingredientName.substring(4).toUpperCase(Locale.ROOT);
Tag<Material> materialTag;
try {
Field field = Tag.class.getField(tagStr);
materialTag = (Tag<Material>) field.get(null);
} catch (NoSuchFieldException | IllegalAccessException e) {
throw new RuntimeException(e);
}
return new RecipeChoice.MaterialChoice(materialTag);
default:
ItemStack item = ItemManager.INSTANCE.matchItem(ingredientName);
return new RecipeChoice.ExactChoice(item);
}
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.github.yufiriamazenta.craftorithm.recipe.registry.impl;
package com.github.yufiriamazenta.craftorithm.recipe.registry;

import com.github.yufiriamazenta.craftorithm.recipe.RecipeManager;
import com.github.yufiriamazenta.craftorithm.recipe.RecipeType;
import com.github.yufiriamazenta.craftorithm.recipe.custom.AnvilRecipe;
import com.github.yufiriamazenta.craftorithm.recipe.registry.RecipeRegistry;
import com.google.common.base.Preconditions;
import crypticlib.util.ItemUtil;
import org.bukkit.NamespacedKey;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.github.yufiriamazenta.craftorithm.recipe.registry.impl;
package com.github.yufiriamazenta.craftorithm.recipe.registry;

import com.github.yufiriamazenta.craftorithm.recipe.RecipeManager;
import com.github.yufiriamazenta.craftorithm.recipe.RecipeType;
import com.github.yufiriamazenta.craftorithm.recipe.registry.RecipeRegistry;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.*;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package com.github.yufiriamazenta.craftorithm.recipe.registry.impl;
package com.github.yufiriamazenta.craftorithm.recipe.registry;

import com.github.yufiriamazenta.craftorithm.recipe.RecipeManager;
import com.github.yufiriamazenta.craftorithm.recipe.RecipeType;
import com.github.yufiriamazenta.craftorithm.recipe.custom.PotionMixRecipe;
import com.github.yufiriamazenta.craftorithm.recipe.registry.RecipeRegistry;
import io.papermc.paper.potion.PotionMix;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.ItemStack;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.yufiriamazenta.craftorithm.recipe.registry.impl;
package com.github.yufiriamazenta.craftorithm.recipe.registry;

import com.github.yufiriamazenta.craftorithm.recipe.RecipeManager;
import com.github.yufiriamazenta.craftorithm.recipe.RecipeType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.github.yufiriamazenta.craftorithm.recipe.registry.impl;
package com.github.yufiriamazenta.craftorithm.recipe.registry;

import com.github.yufiriamazenta.craftorithm.recipe.RecipeManager;
import com.github.yufiriamazenta.craftorithm.recipe.RecipeType;
import com.github.yufiriamazenta.craftorithm.recipe.registry.RecipeRegistry;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.RecipeChoice;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.github.yufiriamazenta.craftorithm.recipe.registry.impl;
package com.github.yufiriamazenta.craftorithm.recipe.registry;

import com.github.yufiriamazenta.craftorithm.recipe.RecipeManager;
import com.github.yufiriamazenta.craftorithm.recipe.RecipeType;
import com.github.yufiriamazenta.craftorithm.recipe.registry.RecipeRegistry;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.RecipeChoice;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.github.yufiriamazenta.craftorithm.recipe.registry.impl;
package com.github.yufiriamazenta.craftorithm.recipe.registry;

import com.github.yufiriamazenta.craftorithm.recipe.RecipeManager;
import com.github.yufiriamazenta.craftorithm.recipe.RecipeType;
import com.github.yufiriamazenta.craftorithm.recipe.registry.RecipeRegistry;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.RecipeChoice;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package com.github.yufiriamazenta.craftorithm.recipe.registry.impl;
package com.github.yufiriamazenta.craftorithm.recipe.registry;

import com.github.yufiriamazenta.craftorithm.recipe.RecipeManager;
import com.github.yufiriamazenta.craftorithm.recipe.RecipeType;
import com.github.yufiriamazenta.craftorithm.recipe.registry.RecipeRegistry;
import org.bukkit.NamespacedKey;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.RecipeChoice;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.github.yufiriamazenta.craftorithm.recipe.registry.impl;
package com.github.yufiriamazenta.craftorithm.recipe.registry;

import com.github.yufiriamazenta.craftorithm.recipe.RecipeManager;
import com.github.yufiriamazenta.craftorithm.recipe.RecipeType;
Expand Down
20 changes: 20 additions & 0 deletions src/main/resources/recipe_groups/example.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
type: shaped
result: minecraft:bedrock
sort_id: 1
recipe1:
type: shaped
result: minecraft:bedrock
source:
shape:
- 'aaa'
- 'bbb'
- 'ccc'
ingredient_map:
a: minecraft:command_block
b: minecraft:barrier
c: minecraft:chain_command_block
unlock: true

recipe2:
type:
其他键:

0 comments on commit 68d76ff

Please sign in to comment.