9
0
mirror of https://github.com/Xiao-MoMi/craft-engine.git synced 2025-12-22 16:39:28 +00:00

允许染色配方

This commit is contained in:
XiaoMoMi
2025-05-11 15:44:33 +08:00
parent 8edd7c46e5
commit e253c31e93
3 changed files with 109 additions and 58 deletions

View File

@@ -330,7 +330,6 @@ public class RecipeEventListener implements Listener {
if (clicked == null) return;
Material type = clicked.getType();
if (type != Material.CAMPFIRE && type != Material.SOUL_CAMPFIRE) return;
if (!VersionHelper.isOrAbove1_21_2()) {
if (clicked.getState() instanceof Campfire campfire) {
try {
Object blockEntity = Reflections.field$CraftBlockEntityState$tileEntity.get(campfire);
@@ -339,7 +338,6 @@ public class RecipeEventListener implements Listener {
this.plugin.logger().warn("Failed to inject cooking block entity", e);
}
}
}
ItemStack itemStack = event.getItem();
if (ItemUtils.isEmpty(itemStack)) return;
@@ -704,11 +702,9 @@ public class RecipeEventListener implements Listener {
try {
Object mcRecipe = Reflections.field$CraftComplexRecipe$recipe.get(complexRecipe);
if (!Reflections.clazz$RepairItemRecipe.isInstance(mcRecipe)) {
inventory.setResult(null);
return;
}
// Repair recipe
if (Reflections.clazz$RepairItemRecipe.isInstance(mcRecipe)) {
// repair item
ItemStack[] itemStacks = inventory.getMatrix();
Pair<ItemStack, ItemStack> onlyTwoItems = getTheOnlyTwoItem(itemStacks);
@@ -757,6 +753,39 @@ public class RecipeEventListener implements Listener {
int newItemDamage = Math.max(0, newItem.maxDamage().get() - remainingDurability);
newItem.damage(newItemDamage);
inventory.setResult(newItem.load());
} else if (Reflections.clazz$ArmorDyeRecipe.isInstance(mcRecipe)) {
ItemStack[] itemStacks = inventory.getMatrix();
Pair<ItemStack, ItemStack> onlyTwoItems = getTheOnlyTwoItem(itemStacks);
if (onlyTwoItems.left() == null || onlyTwoItems.right() == null) {
inventory.setResult(null);
return;
}
Item<ItemStack> left = plugin.itemManager().wrap(onlyTwoItems.left());
Item<ItemStack> right = plugin.itemManager().wrap(onlyTwoItems.right());
// can't be two custom items
if (left.isCustomItem() && right.isCustomItem()) {
inventory.setResult(null);
return;
}
Optional<CustomItem<ItemStack>> customLeftItem = left.getCustomItem();
if (customLeftItem.isPresent()) {
if (!customLeftItem.get().settings().dyeable()) {
inventory.setResult(null);
}
} else {
Optional<CustomItem<ItemStack>> customRightItem = right.getCustomItem();
if (customRightItem.isPresent()) {
if (!customRightItem.get().settings().dyeable()) {
inventory.setResult(null);
}
}
}
} else {
inventory.setResult(null);
return;
}
} catch (Exception e) {
this.plugin.logger().warn("Failed to handle minecraft custom recipe", e);
}

View File

@@ -5269,6 +5269,13 @@ public class Reflections {
)
);
public static final Class<?> clazz$ArmorDyeRecipe = requireNonNull(
BukkitReflectionUtils.findReobfOrMojmapClass(
"world.item.crafting.RecipeArmorDye",
"world.item.crafting.ArmorDyeRecipe"
)
);
public static final Field field$CraftComplexRecipe$recipe = requireNonNull(
ReflectionUtils.getDeclaredField(
clazz$CraftComplexRecipe, clazz$CustomRecipe, 0

View File

@@ -27,6 +27,7 @@ public class ItemSettings {
boolean renameable = true;
boolean canPlaceRelatedVanillaBlock = false;
ProjectileMeta projectileMeta;
boolean dyeable = false;
private ItemSettings() {}
@@ -56,6 +57,7 @@ public class ItemSettings {
newSettings.renameable = settings.renameable;
newSettings.canPlaceRelatedVanillaBlock = settings.canPlaceRelatedVanillaBlock;
newSettings.projectileMeta = settings.projectileMeta;
newSettings.dyeable = settings.dyeable;
return newSettings;
}
@@ -95,6 +97,10 @@ public class ItemSettings {
return tags;
}
public boolean dyeable() {
return dyeable;
}
public List<AnvilRepairItem> repairItems() {
return anvilRepairItems;
}
@@ -144,6 +150,11 @@ public class ItemSettings {
return this;
}
public ItemSettings dyeable(boolean bool) {
this.dyeable = bool;
return this;
}
@FunctionalInterface
public interface Modifier {
@@ -217,6 +228,10 @@ public class ItemSettings {
Quaternionf rotation = MiscUtils.getAsQuaternionf(ResourceConfigUtils.get(args, "rotation-left", "rotation"), "rotation-left");
return settings -> settings.projectileMeta(new ProjectileMeta(customTridentItemId, displayType, scale, translation, rotation));
}));
registerFactory("dyeable", (value -> {
boolean bool = (boolean) value;
return settings -> settings.dyeable(bool);
}));
}
private static void registerFactory(String id, ItemSettings.Modifier.Factory factory) {