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;
|
if (clicked == null) return;
|
||||||
Material type = clicked.getType();
|
Material type = clicked.getType();
|
||||||
if (type != Material.CAMPFIRE && type != Material.SOUL_CAMPFIRE) return;
|
if (type != Material.CAMPFIRE && type != Material.SOUL_CAMPFIRE) return;
|
||||||
if (!VersionHelper.isOrAbove1_21_2()) {
|
if (clicked.getState() instanceof Campfire campfire) {
|
||||||
if (clicked.getState() instanceof Campfire campfire) {
|
try {
|
||||||
try {
|
Object blockEntity = Reflections.field$CraftBlockEntityState$tileEntity.get(campfire);
|
||||||
Object blockEntity = Reflections.field$CraftBlockEntityState$tileEntity.get(campfire);
|
BukkitInjector.injectCookingBlockEntity(blockEntity);
|
||||||
BukkitInjector.injectCookingBlockEntity(blockEntity);
|
} catch (Exception e) {
|
||||||
} catch (Exception e) {
|
this.plugin.logger().warn("Failed to inject cooking block entity", e);
|
||||||
this.plugin.logger().warn("Failed to inject cooking block entity", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -704,59 +702,90 @@ public class RecipeEventListener implements Listener {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
Object mcRecipe = Reflections.field$CraftComplexRecipe$recipe.get(complexRecipe);
|
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);
|
inventory.setResult(null);
|
||||||
return;
|
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) {
|
} catch (Exception e) {
|
||||||
this.plugin.logger().warn("Failed to handle minecraft custom recipe", 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(
|
public static final Field field$CraftComplexRecipe$recipe = requireNonNull(
|
||||||
ReflectionUtils.getDeclaredField(
|
ReflectionUtils.getDeclaredField(
|
||||||
clazz$CraftComplexRecipe, clazz$CustomRecipe, 0
|
clazz$CraftComplexRecipe, clazz$CustomRecipe, 0
|
||||||
|
|||||||
@@ -27,6 +27,7 @@ public class ItemSettings {
|
|||||||
boolean renameable = true;
|
boolean renameable = true;
|
||||||
boolean canPlaceRelatedVanillaBlock = false;
|
boolean canPlaceRelatedVanillaBlock = false;
|
||||||
ProjectileMeta projectileMeta;
|
ProjectileMeta projectileMeta;
|
||||||
|
boolean dyeable = false;
|
||||||
|
|
||||||
private ItemSettings() {}
|
private ItemSettings() {}
|
||||||
|
|
||||||
@@ -56,6 +57,7 @@ public class ItemSettings {
|
|||||||
newSettings.renameable = settings.renameable;
|
newSettings.renameable = settings.renameable;
|
||||||
newSettings.canPlaceRelatedVanillaBlock = settings.canPlaceRelatedVanillaBlock;
|
newSettings.canPlaceRelatedVanillaBlock = settings.canPlaceRelatedVanillaBlock;
|
||||||
newSettings.projectileMeta = settings.projectileMeta;
|
newSettings.projectileMeta = settings.projectileMeta;
|
||||||
|
newSettings.dyeable = settings.dyeable;
|
||||||
return newSettings;
|
return newSettings;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -95,6 +97,10 @@ public class ItemSettings {
|
|||||||
return tags;
|
return tags;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean dyeable() {
|
||||||
|
return dyeable;
|
||||||
|
}
|
||||||
|
|
||||||
public List<AnvilRepairItem> repairItems() {
|
public List<AnvilRepairItem> repairItems() {
|
||||||
return anvilRepairItems;
|
return anvilRepairItems;
|
||||||
}
|
}
|
||||||
@@ -144,6 +150,11 @@ public class ItemSettings {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ItemSettings dyeable(boolean bool) {
|
||||||
|
this.dyeable = bool;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
@FunctionalInterface
|
@FunctionalInterface
|
||||||
public interface Modifier {
|
public interface Modifier {
|
||||||
|
|
||||||
@@ -217,6 +228,10 @@ public class ItemSettings {
|
|||||||
Quaternionf rotation = MiscUtils.getAsQuaternionf(ResourceConfigUtils.get(args, "rotation-left", "rotation"), "rotation-left");
|
Quaternionf rotation = MiscUtils.getAsQuaternionf(ResourceConfigUtils.get(args, "rotation-left", "rotation"), "rotation-left");
|
||||||
return settings -> settings.projectileMeta(new ProjectileMeta(customTridentItemId, displayType, scale, translation, rotation));
|
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) {
|
private static void registerFactory(String id, ItemSettings.Modifier.Factory factory) {
|
||||||
|
|||||||
Reference in New Issue
Block a user