Skip to content

Commit

Permalink
尝试修复铁砧配方的一些问题
Browse files Browse the repository at this point in the history
  • Loading branch information
YufiriaMazenta committed Jul 3, 2024
1 parent 3d1b4c6 commit 1bfd265
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 45 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.10.4"
version = "1.10.5-dev1"

plugins {
`java-library`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
import com.github.yufiriamazenta.craftorithm.item.ItemManager;
import com.github.yufiriamazenta.craftorithm.recipe.RecipeManager;
import com.github.yufiriamazenta.craftorithm.recipe.custom.AnvilRecipe;
import com.github.yufiriamazenta.craftorithm.util.CollectionsUtil;
import com.github.yufiriamazenta.craftorithm.util.ItemUtils;
import crypticlib.listener.BukkitListener;
import crypticlib.util.ItemUtil;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
Expand All @@ -18,9 +20,11 @@
import org.bukkit.event.inventory.PrepareAnvilEvent;
import org.bukkit.inventory.AnvilInventory;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@BukkitListener
public enum AnvilHandler implements Listener {
Expand Down Expand Up @@ -66,6 +70,25 @@ public void runConditions(PrepareAnvilEvent event) {
if (base.hasItemMeta())
result.setItemMeta(base.getItemMeta());
}
if (anvilRecipe.copyEnchantments()) {
if (base.hasItemMeta()) {
Map<Enchantment, Integer> baseEnchantments = base.getItemMeta().getEnchants();
ItemMeta resultMeta = result.getItemMeta();
Map<Enchantment, Integer> resultEnchantments = new HashMap<>(resultMeta.getEnchants());
CollectionsUtil.putAllIf(resultEnchantments, baseEnchantments, (type, level) -> {
if (resultEnchantments.containsKey(type)) {
return level > resultEnchantments.get(type);
} else {
return true;
}
});
resultMeta.removeEnchantments();
resultEnchantments.forEach((enchant, level) -> {
resultMeta.addEnchant(enchant, level, true);
});
result.setItemMeta(resultMeta);
}
}
event.getInventory().setRepairCost(anvilRecipe.costLevel());
//刷新物品
String resultId = ItemManager.INSTANCE.matchItemName(result, false);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.github.yufiriamazenta.craftorithm.recipe;

import com.github.yufiriamazenta.craftorithm.item.ItemManager;
import org.bukkit.inventory.meta.ItemMeta;

import java.util.HashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;

public class CopyNbtProcessor {

private Map<String, Function<ItemMeta, ItemMeta>> processors = new HashMap<>();

public CopyNbtProcessor() {

}

public ItemMeta process(ItemMeta meta) {

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -366,8 +366,14 @@ public static List<RecipeRegistry> newAnvilRecipe(YamlConfiguration config, Stri
ItemStack base = ItemManager.INSTANCE.matchItem(config.getString("source.base", ""));
ItemStack addition = ItemManager.INSTANCE.matchItem(config.getString("source.addition", ""));
int costLevel = config.getInt("source.cost_level", 0);
boolean copyNbt = config.getBoolean("source.copy_nbt", true);
RecipeRegistry recipeRegistry = new AnvilRecipeRegistry(key, namespacedKey, result).setBase(base).setAddition(addition).setCopyNbt(copyNbt).setCostLevel(costLevel);
boolean copyNbt = config.getBoolean("source.copy_nbt", false);
boolean copyEnchantments = config.getBoolean("source.copy_enchantments", true);
RecipeRegistry recipeRegistry = new AnvilRecipeRegistry(key, namespacedKey, result)
.setBase(base)
.setAddition(addition)
.setCopyNbt(copyNbt)
.setCopyEnchantments(copyEnchantments)
.setCostLevel(costLevel);
return Collections.singletonList(recipeRegistry);
}

Expand All @@ -382,8 +388,15 @@ public static List<RecipeRegistry> newMultipleAnvilRecipe(YamlConfiguration conf
ItemStack base = ItemManager.INSTANCE.matchItem((String) map.get("base"));
ItemStack addition = ItemManager.INSTANCE.matchItem((String) map.get("addition"));
int costLevel = map.containsKey("cost_level") ? (Integer) map.get("cost_level") : 0;
boolean copyNbt = map.containsKey("copy_nbt") ? (Boolean) map.get("copy_nbt") : true;
recipeRegistries.add(new AnvilRecipeRegistry(key, namespacedKey, result).setBase(base).setAddition(addition).setCopyNbt(copyNbt).setCostLevel(costLevel));
boolean copyNbt = map.containsKey("copy_nbt") ? (Boolean) map.get("copy_nbt") : false;
boolean copyEnchantments = map.containsKey("copy_enchantments") ? (Boolean) map.get("copy_enchantments") : true;
recipeRegistries.add(new AnvilRecipeRegistry(key, namespacedKey, result)
.setBase(base)
.setAddition(addition)
.setCopyNbt(copyNbt)
.setCopyEnchantments(copyEnchantments)
.setCostLevel(costLevel)
);
}
return recipeRegistries;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.github.yufiriamazenta.craftorithm.config.Languages;
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.custom.AnvilRecipe;
import com.github.yufiriamazenta.craftorithm.recipe.custom.CustomRecipe;
import com.github.yufiriamazenta.craftorithm.recipe.custom.PotionMixRecipe;
Expand Down Expand Up @@ -354,13 +355,17 @@ public int getRecipeGroupSortId(String recipeGroupName) {

@Nullable
public AnvilRecipe matchAnvilRecipe(ItemStack base, ItemStack addition) {
String baseId = ItemManager.INSTANCE.matchItemName(base, true);
String additionId = ItemManager.INSTANCE.matchItemName(addition, true);
for (Map.Entry<NamespacedKey, AnvilRecipe> anvilRecipeEntry : anvilRecipeMap.entrySet()) {
AnvilRecipe anvilRecipe = anvilRecipeEntry.getValue();
if (!anvilRecipe.base().isSimilar(base))
String recipeBaseId = ItemManager.INSTANCE.matchItemName(base, true);
String recipeAdditionId = ItemManager.INSTANCE.matchItemName(addition, true);
if (!Objects.equals(baseId, recipeBaseId))
continue;
if (base.getAmount() < anvilRecipe.base().getAmount())
continue;
if (!anvilRecipe.addition().isSimilar(addition))
if (!Objects.equals(additionId, recipeAdditionId))
continue;
if (addition.getAmount() < anvilRecipe.addition().getAmount())
continue;
Expand All @@ -370,45 +375,29 @@ public AnvilRecipe matchAnvilRecipe(ItemStack base, ItemStack addition) {
}

public RecipeType getRecipeType(Recipe recipe) {
if (recipe == null)
return RecipeType.UNKNOWN;
if (recipe instanceof ShapedRecipe)
return RecipeType.SHAPED;
else if (recipe instanceof ShapelessRecipe)
return RecipeType.SHAPELESS;
else if (recipe instanceof CookingRecipe)
return RecipeType.COOKING;
else if (recipe instanceof SmithingRecipe)
return RecipeType.SMITHING;
else if (recipe instanceof StonecuttingRecipe)
return RecipeType.STONE_CUTTING;
else if (recipe instanceof PotionMixRecipe)
return RecipeType.POTION;
else if (recipe instanceof AnvilRecipe)
return RecipeType.ANVIL;
else
return RecipeType.UNKNOWN;
return switch (recipe) {
case ShapedRecipe shapedRecipe -> RecipeType.SHAPED;
case ShapelessRecipe shapelessRecipe -> RecipeType.SHAPELESS;
case CookingRecipe<?> cookingRecipe -> RecipeType.COOKING;
case SmithingRecipe smithingRecipe -> RecipeType.SMITHING;
case StonecuttingRecipe stonecuttingRecipe -> RecipeType.STONE_CUTTING;
case PotionMixRecipe potionMixRecipe -> RecipeType.POTION;
case AnvilRecipe anvilRecipe -> RecipeType.ANVIL;
case null, default -> RecipeType.UNKNOWN;
};
}

public StringLangEntry getRecipeTypeName(RecipeType recipeType) {
switch (recipeType) {
case SHAPED:
return Languages.RECIPE_TYPE_NAME_SHAPED;
case SHAPELESS:
return Languages.RECIPE_TYPE_NAME_SHAPELESS;
case COOKING:
return Languages.RECIPE_TYPE_NAME_COOKING;
case SMITHING:
return Languages.RECIPE_TYPE_NAME_SMITHING;
case STONE_CUTTING:
return Languages.RECIPE_TYPE_NAME_STONE_CUTTING;
case POTION:
return Languages.RECIPE_TYPE_NAME_POTION;
case ANVIL:
return Languages.RECIPE_TYPE_NAME_ANVIL;
default:
return null;
}
return switch (recipeType) {
case SHAPED -> Languages.RECIPE_TYPE_NAME_SHAPED;
case SHAPELESS -> Languages.RECIPE_TYPE_NAME_SHAPELESS;
case COOKING -> Languages.RECIPE_TYPE_NAME_COOKING;
case SMITHING -> Languages.RECIPE_TYPE_NAME_SMITHING;
case STONE_CUTTING -> Languages.RECIPE_TYPE_NAME_STONE_CUTTING;
case POTION -> Languages.RECIPE_TYPE_NAME_POTION;
case ANVIL -> Languages.RECIPE_TYPE_NAME_ANVIL;
default -> null;
};
}

public void resetRecipes() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public class AnvilRecipe implements CustomRecipe {
private ItemStack base;
private ItemStack addition;
private int costLevel = 0;
private boolean copyNbt = true;
private boolean copyNbt = false;
private boolean copyEnchantments = true;

public AnvilRecipe(NamespacedKey key, ItemStack result, ItemStack base, ItemStack addition) {
this.key = key;
Expand Down Expand Up @@ -86,4 +87,12 @@ public AnvilRecipe setCopyNbt(boolean copyNbt) {
return this;
}

public boolean copyEnchantments() {
return copyEnchantments;
}

public AnvilRecipe setCopyEnchantments(boolean copyEnchantments) {
this.copyEnchantments = copyEnchantments;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
public class AnvilRecipeRegistry extends RecipeRegistry {

private ItemStack base, addition;
private boolean copyNbt;
private boolean copyNbt = false;
private int costLevel;
private boolean copyEnchantments = true;

public AnvilRecipeRegistry(@Nullable String group, @NotNull NamespacedKey namespacedKey, @NotNull ItemStack result) {
super(group, namespacedKey, result);
Expand All @@ -34,6 +35,7 @@ public void register() {
AnvilRecipe anvilRecipe = new AnvilRecipe(namespacedKey(), result(), base, addition);
anvilRecipe.setCopyNbt(copyNbt);
anvilRecipe.setCostLevel(costLevel);
anvilRecipe.setCopyEnchantments(copyEnchantments);
RecipeManager.INSTANCE.regRecipe(group(), anvilRecipe, RecipeType.ANVIL);
}

Expand Down Expand Up @@ -72,4 +74,14 @@ public AnvilRecipeRegistry setCostLevel(int costLevel) {
this.costLevel = costLevel;
return this;
}

public boolean copyEnchantments() {
return copyEnchantments;
}

public AnvilRecipeRegistry setCopyEnchantments(boolean copyEnchantments) {
this.copyEnchantments = copyEnchantments;
return this;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import java.util.List;
import java.util.Map;
import java.util.StringJoiner;
import java.util.function.BiFunction;
import java.util.function.Function;

public class CollectionsUtil {

Expand All @@ -26,4 +28,20 @@ public static String list2ArcencielBlock(List<String> list) {
return blockJoiner.toString();
}

/**
* 将一个map中所有符合条件的键值对插入另外一个map
* @param map 被插入的map
* @param otherMap 插入的map
* @param condition 筛选条件,两个参数都对应用于插入的map中的值
* @param <K> 用于插入的map的key
* @param <V> 用于插入的map的value
*/
public static <K, V> void putAllIf(Map<K, V> map, Map<K, V> otherMap, BiFunction<K, V, Boolean> condition) {
otherMap.forEach((k, v) -> {
if (condition.apply(k, v)) {
map.put(k, v);
}
});
}

}
3 changes: 3 additions & 0 deletions src/main/resources/recipes/example_anvil.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ source:
- base: bedrock
addition: command_block
copy_nbt: true
copy_enchantments: true
copy_damage: true
# merge_enchants: true #TODO 或许可以出
cost_level: 1

0 comments on commit 1bfd265

Please sign in to comment.