-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1dd6517
commit 68d76ff
Showing
15 changed files
with
165 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
src/main/java/com/github/yufiriamazenta/craftorithm/recipe/RecipeGroup.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
src/main/java/com/github/yufiriamazenta/craftorithm/recipe/loader/RecipeGroupLoader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} |
3 changes: 1 addition & 2 deletions
3
...pe/registry/impl/AnvilRecipeRegistry.java → .../recipe/registry/AnvilRecipeRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
.../registry/impl/CookingRecipeRegistry.java → ...ecipe/registry/CookingRecipeRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
...egistry/impl/PotionMixRecipeRegistry.java → ...ipe/registry/PotionMixRecipeRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...try/impl/RandomCookingRecipeRegistry.java → ...registry/RandomCookingRecipeRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
...e/registry/impl/ShapedRecipeRegistry.java → ...recipe/registry/ShapedRecipeRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
...egistry/impl/ShapelessRecipeRegistry.java → ...ipe/registry/ShapelessRecipeRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
...registry/impl/SmithingRecipeRegistry.java → ...cipe/registry/SmithingRecipeRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
...stry/impl/StoneCuttingRecipeRegistry.java → .../registry/StoneCuttingRecipeRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...egistry/impl/XSmithingRecipeRegistry.java → ...ipe/registry/XSmithingRecipeRegistry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: | ||
其他键: |