mirror of
https://github.com/Xiao-MoMi/craft-engine.git
synced 2025-12-22 16:39:28 +00:00
允许染色配方
This commit is contained in:
@@ -330,14 +330,12 @@ 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);
|
||||
BukkitInjector.injectCookingBlockEntity(blockEntity);
|
||||
} catch (Exception e) {
|
||||
this.plugin.logger().warn("Failed to inject cooking block entity", e);
|
||||
}
|
||||
if (clicked.getState() instanceof Campfire campfire) {
|
||||
try {
|
||||
Object blockEntity = Reflections.field$CraftBlockEntityState$tileEntity.get(campfire);
|
||||
BukkitInjector.injectCookingBlockEntity(blockEntity);
|
||||
} catch (Exception e) {
|
||||
this.plugin.logger().warn("Failed to inject cooking block entity", e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -704,59 +702,90 @@ public class RecipeEventListener implements Listener {
|
||||
|
||||
try {
|
||||
Object mcRecipe = Reflections.field$CraftComplexRecipe$recipe.get(complexRecipe);
|
||||
if (!Reflections.clazz$RepairItemRecipe.isInstance(mcRecipe)) {
|
||||
|
||||
// Repair recipe
|
||||
if (Reflections.clazz$RepairItemRecipe.isInstance(mcRecipe)) {
|
||||
// repair item
|
||||
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());
|
||||
if (!left.id().equals(right.id())) {
|
||||
inventory.setResult(null);
|
||||
return;
|
||||
}
|
||||
|
||||
int totalDamage = right.damage().orElse(0) + left.damage().orElse(0);
|
||||
int totalMaxDamage = left.maxDamage().get() + right.maxDamage().get();
|
||||
// should be impossible, but take care
|
||||
if (totalDamage >= totalMaxDamage) {
|
||||
inventory.setResult(null);
|
||||
return;
|
||||
}
|
||||
|
||||
Player player;
|
||||
try {
|
||||
player = (Player) Reflections.method$InventoryView$getPlayer.invoke(event.getView());
|
||||
} catch (ReflectiveOperationException e) {
|
||||
plugin.logger().warn("Failed to get inventory viewer", e);
|
||||
return;
|
||||
}
|
||||
|
||||
Optional<CustomItem<ItemStack>> customItemOptional = plugin.itemManager().getCustomItem(left.id());
|
||||
if (customItemOptional.isEmpty()) {
|
||||
inventory.setResult(null);
|
||||
return;
|
||||
}
|
||||
|
||||
CustomItem<ItemStack> customItem = customItemOptional.get();
|
||||
if (!customItem.settings().canRepair()) {
|
||||
inventory.setResult(null);
|
||||
return;
|
||||
}
|
||||
|
||||
Item<ItemStack> newItem = customItem.buildItem(ItemBuildContext.of(plugin.adapt(player)));
|
||||
int remainingDurability = totalMaxDamage - totalDamage;
|
||||
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;
|
||||
}
|
||||
|
||||
// repair item
|
||||
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());
|
||||
if (!left.id().equals(right.id())) {
|
||||
inventory.setResult(null);
|
||||
return;
|
||||
}
|
||||
|
||||
int totalDamage = right.damage().orElse(0) + left.damage().orElse(0);
|
||||
int totalMaxDamage = left.maxDamage().get() + right.maxDamage().get();
|
||||
// should be impossible, but take care
|
||||
if (totalDamage >= totalMaxDamage) {
|
||||
inventory.setResult(null);
|
||||
return;
|
||||
}
|
||||
|
||||
Player player;
|
||||
try {
|
||||
player = (Player) Reflections.method$InventoryView$getPlayer.invoke(event.getView());
|
||||
} catch (ReflectiveOperationException e) {
|
||||
plugin.logger().warn("Failed to get inventory viewer", e);
|
||||
return;
|
||||
}
|
||||
|
||||
Optional<CustomItem<ItemStack>> customItemOptional = plugin.itemManager().getCustomItem(left.id());
|
||||
if (customItemOptional.isEmpty()) {
|
||||
inventory.setResult(null);
|
||||
return;
|
||||
}
|
||||
|
||||
CustomItem<ItemStack> customItem = customItemOptional.get();
|
||||
if (!customItem.settings().canRepair()) {
|
||||
inventory.setResult(null);
|
||||
return;
|
||||
}
|
||||
|
||||
Item<ItemStack> newItem = customItem.buildItem(ItemBuildContext.of(plugin.adapt(player)));
|
||||
int remainingDurability = totalMaxDamage - totalDamage;
|
||||
int newItemDamage = Math.max(0, newItem.maxDamage().get() - remainingDurability);
|
||||
newItem.damage(newItemDamage);
|
||||
inventory.setResult(newItem.load());
|
||||
} catch (Exception e) {
|
||||
this.plugin.logger().warn("Failed to handle minecraft custom recipe", e);
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user