From 0f7916e5a7cb217d6535de483765cd90d388e5c1 Mon Sep 17 00:00:00 2001 From: shedaniel Date: Tue, 16 Apr 2024 03:30:29 +0900 Subject: [PATCH 1/5] Fix collapsible entry configure button not rendering the icon at the correct location --- .../client/gui/screen/collapsible/CollapsibleEntryWidget.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/screen/collapsible/CollapsibleEntryWidget.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/screen/collapsible/CollapsibleEntryWidget.java index 88596a0f7..f7bca78e2 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/screen/collapsible/CollapsibleEntryWidget.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/screen/collapsible/CollapsibleEntryWidget.java @@ -121,7 +121,7 @@ public CollapsibleEntryWidget(boolean custom, ResourceLocation id, Component com public void render(PoseStack matrices, int mouseX, int mouseY, float delta) { super.render(matrices, mouseX, mouseY, delta); RenderSystem.setShaderTexture(0, InternalTextures.CHEST_GUI_TEXTURE); - blit(matrices, x + 3, y + 3, 0, 0, 14, 14); + blit(matrices, getX() + 3, getY() + 3, 0, 0, 14, 14); } }; } else { From f0ada921ddb51d9ed80640b88cded9043d0f418d Mon Sep 17 00:00:00 2001 From: shedaniel Date: Mon, 1 Apr 2024 22:26:52 +0900 Subject: [PATCH 2/5] Add display validator, Fix #1610 --- .../screen/CompositeDisplayViewingScreen.java | 4 +- .../screen/DefaultDisplayViewingScreen.java | 3 +- .../registry/display/DisplayRegistryImpl.java | 8 ++- .../registry/display/DisplayValidator.java | 54 +++++++++++++++++++ .../assets/roughlyenoughitems/lang/en_us.json | 1 + 5 files changed, 67 insertions(+), 3 deletions(-) create mode 100644 runtime/src/main/java/me/shedaniel/rei/impl/client/registry/display/DisplayValidator.java diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/screen/CompositeDisplayViewingScreen.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/screen/CompositeDisplayViewingScreen.java index f92275424..793903364 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/screen/CompositeDisplayViewingScreen.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/screen/CompositeDisplayViewingScreen.java @@ -39,6 +39,7 @@ import me.shedaniel.rei.api.client.registry.category.ButtonArea; import me.shedaniel.rei.api.client.registry.category.CategoryRegistry; import me.shedaniel.rei.api.client.registry.display.DisplayCategory; +import me.shedaniel.rei.api.client.registry.display.DisplayRegistry; import me.shedaniel.rei.api.client.view.ViewSearchBuilder; import me.shedaniel.rei.api.common.category.CategoryIdentifier; import me.shedaniel.rei.api.common.display.Display; @@ -47,6 +48,7 @@ import me.shedaniel.rei.impl.client.gui.widget.EntryWidget; import me.shedaniel.rei.impl.client.gui.widget.InternalWidgets; import me.shedaniel.rei.impl.display.DisplaySpec; +import net.minecraft.ChatFormatting; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.components.events.GuiEventListener; import net.minecraft.network.chat.Component; @@ -179,7 +181,7 @@ public void init() { this.widgets.add(Widgets.createClickableLabel(new Point(bounds.x + 4 + scrollListBounds.width / 2, bounds.y + 6), categories.get(selectedCategoryIndex).getTitle(), label -> { ViewSearchBuilder.builder().addAllCategories().open(); - }).tooltip(Component.translatable("text.rei.view_all_categories")).noShadow().color(0xFF404040, 0xFFBBBBBB).hoveredColor(0xFF0041FF, 0xFFFFBD4D)); + }).tooltip(Component.translatable("text.rei.view_all_categories"), Component.translatable("text.rei.view_all_categories.tooltip", CategoryRegistry.getInstance().stream().filter(config -> !DisplayRegistry.getInstance().get(config.getCategoryIdentifier()).isEmpty()).count()).withStyle(ChatFormatting.DARK_GRAY)).noShadow().color(0xFF404040, 0xFFBBBBBB).hoveredColor(0xFF0041FF, 0xFFFFBD4D)); this.widgets.add(new ButtonListWidget(buttonList)); this.children().addAll(widgets); diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/screen/DefaultDisplayViewingScreen.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/screen/DefaultDisplayViewingScreen.java index b47c9d825..c3593008e 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/screen/DefaultDisplayViewingScreen.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/screen/DefaultDisplayViewingScreen.java @@ -47,6 +47,7 @@ import me.shedaniel.rei.api.client.registry.category.ButtonArea; import me.shedaniel.rei.api.client.registry.category.CategoryRegistry; import me.shedaniel.rei.api.client.registry.display.DisplayCategory; +import me.shedaniel.rei.api.client.registry.display.DisplayRegistry; import me.shedaniel.rei.api.client.view.ViewSearchBuilder; import me.shedaniel.rei.api.common.category.CategoryIdentifier; import me.shedaniel.rei.api.common.display.Display; @@ -168,7 +169,7 @@ public void init() { .onClick(button -> previousCategory()).tooltipLine(Component.translatable("text.rei.previous_category"))); this.widgets.add(Widgets.createClickableLabel(new Point(bounds.getCenterX(), bounds.getY() + 7), getCurrentCategory().getTitle(), clickableLabelWidget -> { ViewSearchBuilder.builder().addAllCategories().open(); - }).tooltip(Component.translatable("text.rei.view_all_categories"))); + }).tooltip(Component.translatable("text.rei.view_all_categories"), Component.translatable("text.rei.view_all_categories.tooltip", CategoryRegistry.getInstance().stream().filter(config -> !DisplayRegistry.getInstance().get(config.getCategoryIdentifier()).isEmpty()).count()).withStyle(ChatFormatting.DARK_GRAY))); this.widgets.add(categoryNext = Widgets.createButton(new Rectangle(bounds.getCenterX() + guiWidth / 2 - 17, bounds.getY() + 5, 12, 12), Component.literal("")) .onClick(button -> nextCategory()).tooltipLine(Component.translatable("text.rei.next_category"))); this.categoryBack.setEnabled(categories.size() > 1); diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/display/DisplayRegistryImpl.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/display/DisplayRegistryImpl.java index 655466211..277ba64d7 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/display/DisplayRegistryImpl.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/display/DisplayRegistryImpl.java @@ -79,7 +79,9 @@ public void add(Display display, @Nullable Object origin) { lastAddWarning = System.currentTimeMillis(); } - this.displaysHolder.add(display, origin); + if (DisplayValidator.validate(display)) { + this.displaysHolder.add(display, origin); + } } @Override @@ -199,6 +201,10 @@ public void endReload() { } } + for (List displays : getAll().values()) { + displays.removeIf(display -> !DisplayValidator.validate(display)); + } + this.displaysHolder.endReload(); InternalLogger.getInstance().debug("Registered %d displays", displaySize()); diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/display/DisplayValidator.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/display/DisplayValidator.java new file mode 100644 index 000000000..6ebd4dbf0 --- /dev/null +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/display/DisplayValidator.java @@ -0,0 +1,54 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021, 2022, 2023 shedaniel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.shedaniel.rei.impl.client.registry.display; + +import me.shedaniel.rei.api.client.registry.category.CategoryRegistry; +import me.shedaniel.rei.api.client.registry.display.DisplayCategory; +import me.shedaniel.rei.api.common.category.CategoryIdentifier; +import me.shedaniel.rei.api.common.display.Display; +import me.shedaniel.rei.impl.common.InternalLogger; + +public class DisplayValidator { + public static boolean validate(Display display) { + CategoryIdentifier identifier = display.getCategoryIdentifier(); + + try { + CategoryRegistry.CategoryConfiguration configuration = CategoryRegistry.getInstance().get(identifier); + + try { + DisplayCategory category = (DisplayCategory) configuration.getCategory(); + category.getDisplayWidth(display); + return true; + } catch (ClassCastException exception) { + InternalLogger.getInstance().error("Display [%s] %s failed validation check for category [%s] %s", display.getDisplayLocation().orElse(null), display, identifier, configuration.getCategory()); + return false; + } + } catch (NullPointerException exception) { + return true; + } catch (Throwable throwable) { + InternalLogger.getInstance().error("Display [%s] %s failed validation check for category [%s]".formatted(display.getDisplayLocation().orElse(null), display, identifier), throwable); + return false; + } + } +} diff --git a/runtime/src/main/resources/assets/roughlyenoughitems/lang/en_us.json b/runtime/src/main/resources/assets/roughlyenoughitems/lang/en_us.json index 73bcb103a..fa6f3dbd4 100755 --- a/runtime/src/main/resources/assets/roughlyenoughitems/lang/en_us.json +++ b/runtime/src/main/resources/assets/roughlyenoughitems/lang/en_us.json @@ -137,6 +137,7 @@ "text.rei.left_arrow": "<", "text.rei.right_arrow": ">", "text.rei.view_all_categories": "View All Categories", + "text.rei.view_all_categories.tooltip": "%d categories", "text.rei.go_back_first_page": "Back to Page 1", "text.rei.choose_page": "Choose Page", "text.rei.shift_click_to": "Shift-Click to %s", From 3d3796d684e70c38ec46c28d18051ee6c94c278f Mon Sep 17 00:00:00 2001 From: shedaniel Date: Tue, 16 Apr 2024 18:39:08 +0900 Subject: [PATCH 3/5] Retain displays order --- .../java/me/shedaniel/rei/impl/client/view/ViewsImpl.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/view/ViewsImpl.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/view/ViewsImpl.java index aebdbb6c1..34bff7be2 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/view/ViewsImpl.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/view/ViewsImpl.java @@ -31,7 +31,7 @@ import it.unimi.dsi.fastutil.longs.Long2LongOpenHashMap; import it.unimi.dsi.fastutil.objects.Object2IntMap; import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap; -import it.unimi.dsi.fastutil.objects.ReferenceOpenHashSet; +import it.unimi.dsi.fastutil.objects.ReferenceLinkedOpenHashSet; import me.shedaniel.rei.api.client.REIRuntime; import me.shedaniel.rei.api.client.config.ConfigObject; import me.shedaniel.rei.api.client.registry.category.CategoryRegistry; @@ -281,7 +281,7 @@ public static Set getOrPutEmptyLinkedSet(Map> map, A key) { if (b != null) { return b; } - map.put(key, new ReferenceOpenHashSet<>()); + map.put(key, new ReferenceLinkedOpenHashSet<>()); return map.get(key); } From da47c76268e89abc8bb95f02964ca9ae3550d5a9 Mon Sep 17 00:00:00 2001 From: shedaniel Date: Tue, 16 Apr 2024 18:39:40 +0900 Subject: [PATCH 4/5] Fix compositing category not showing chance in tooltips --- .../plugin/client/categories/DefaultCompostingCategory.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/default-plugin/src/main/java/me/shedaniel/rei/plugin/client/categories/DefaultCompostingCategory.java b/default-plugin/src/main/java/me/shedaniel/rei/plugin/client/categories/DefaultCompostingCategory.java index 6f5c942c2..c8a94ecc9 100644 --- a/default-plugin/src/main/java/me/shedaniel/rei/plugin/client/categories/DefaultCompostingCategory.java +++ b/default-plugin/src/main/java/me/shedaniel/rei/plugin/client/categories/DefaultCompostingCategory.java @@ -34,6 +34,7 @@ import me.shedaniel.rei.api.client.registry.display.DisplayCategory; import me.shedaniel.rei.api.common.category.CategoryIdentifier; import me.shedaniel.rei.api.common.entry.EntryIngredient; +import me.shedaniel.rei.api.common.entry.type.VanillaEntryTypes; import me.shedaniel.rei.api.common.util.EntryStacks; import me.shedaniel.rei.plugin.common.BuiltinPlugin; import me.shedaniel.rei.plugin.common.displays.DefaultCompostingDisplay; @@ -94,8 +95,8 @@ public List setupDisplay(DefaultCompostingDisplay display, Rectangle bou for (int y = 0; y < 5; y++) for (int x = 0; x < 7; x++) { EntryIngredient entryIngredient = stacks.size() > i ? stacks.get(i) : EntryIngredient.empty(); - if (!entryIngredient.isEmpty()) { - ItemStack firstStack = (ItemStack) entryIngredient.get(0).getValue(); + if (!entryIngredient.isEmpty() && entryIngredient.get(0).getType() == VanillaEntryTypes.ITEM) { + ItemStack firstStack = entryIngredient.get(0).castValue(); float chance = ComposterBlock.COMPOSTABLES.getFloat(firstStack.getItem()); if (chance > 0.0f) { entryIngredient = entryIngredient.map(stack -> stack.copy().tooltip(Component.translatable("text.rei.composting.chance", Mth.clamp(Mth.fastFloor(chance * 100), 0, 100)).withStyle(ChatFormatting.YELLOW))); From 6cc0322aacd36bbf325d3e08b7fa0b76d2eb0e5a Mon Sep 17 00:00:00 2001 From: shedaniel Date: Tue, 16 Apr 2024 18:32:47 +0900 Subject: [PATCH 5/5] Add anvil recipes, Close #1381 --- .../plugin/client/DefaultClientPlugin.java | 63 +++++++++++++++++++ .../anvil/DefaultAnvilCategory.java | 16 ++++- .../common/displays/anvil/AnvilRecipe.java | 11 ++++ .../displays/anvil/DefaultAnvilDisplay.java | 55 ++++++++++++++-- 4 files changed, 137 insertions(+), 8 deletions(-) diff --git a/default-plugin/src/main/java/me/shedaniel/rei/plugin/client/DefaultClientPlugin.java b/default-plugin/src/main/java/me/shedaniel/rei/plugin/client/DefaultClientPlugin.java index 239b95e2e..b3b94b9e3 100644 --- a/default-plugin/src/main/java/me/shedaniel/rei/plugin/client/DefaultClientPlugin.java +++ b/default-plugin/src/main/java/me/shedaniel/rei/plugin/client/DefaultClientPlugin.java @@ -96,6 +96,7 @@ import net.minecraft.world.item.alchemy.PotionBrewing; import net.minecraft.world.item.alchemy.PotionUtils; import net.minecraft.world.item.crafting.*; +import net.minecraft.world.item.enchantment.EnchantmentInstance; import net.minecraft.world.level.GameType; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.ComposterBlock; @@ -104,11 +105,13 @@ import net.minecraft.world.level.block.state.BlockState; import net.minecraft.world.level.material.Fluid; import net.minecraft.world.level.material.FluidState; +import org.apache.commons.lang3.tuple.Pair; import org.jetbrains.annotations.ApiStatus; import java.util.*; import java.util.function.Supplier; import java.util.function.UnaryOperator; +import java.util.stream.IntStream; import java.util.stream.Stream; @Environment(EnvType.CLIENT) @@ -355,6 +358,66 @@ public void registerDisplays(DisplayRegistry registry) { registerForgePotions(registry, this); } + for (Item item : Registry.ITEM) { + ItemStack stack = item.getDefaultInstance(); + if (!stack.isDamageableItem()) continue; + EntryIngredient repairMaterialBase = null; + if (item instanceof TieredItem tieredItem) { + Tier tier = tieredItem.getTier(); + repairMaterialBase = EntryIngredients.ofIngredient(tier.getRepairIngredient()); + } else if (item instanceof ArmorItem armorItem) { + ArmorMaterial material = armorItem.getMaterial(); + repairMaterialBase = EntryIngredients.ofIngredient(material.getRepairIngredient()); + } else if (item instanceof ShieldItem shieldItem) { + repairMaterialBase = EntryIngredients.ofItemTag(ItemTags.PLANKS); + repairMaterialBase.filter(s -> shieldItem.isValidRepairItem(stack, s.castValue())); + } else if (item instanceof ElytraItem elytraItem) { + repairMaterialBase = EntryIngredients.of(Items.PHANTOM_MEMBRANE); + repairMaterialBase.filter(s -> elytraItem.isValidRepairItem(stack, s.castValue())); + } + if (repairMaterialBase == null || repairMaterialBase.isEmpty()) continue; + for (int[] i = {1}; i[0] <= 4; i[0]++) { + ItemStack baseStack = item.getDefaultInstance(); + int toRepair = i[0] == 4 ? item.getMaxDamage() : baseStack.getMaxDamage() / 4 * i[0]; + baseStack.setDamageValue(toRepair); + EntryIngredient repairMaterial = repairMaterialBase.map(s -> { + EntryStack newStack = s.copy(); + newStack.castValue().setCount(i[0]); + return newStack; + }); + Optional> output = DefaultAnvilDisplay.calculateOutput(baseStack, repairMaterial.get(0).castValue()); + if (output.isEmpty()) continue; + registry.add(new DefaultAnvilDisplay(List.of(EntryIngredients.of(baseStack), repairMaterial), + Collections.singletonList(EntryIngredients.of(output.get().getLeft())), Optional.empty(), OptionalInt.of(output.get().getRight()))); + } + } + List> enchantmentBooks = Registry.ENCHANTMENT.stream() + .flatMap(enchantment -> { + if (enchantment.getMaxLevel() - enchantment.getMinLevel() >= 10) { + return IntStream.of(enchantment.getMinLevel(), enchantment.getMaxLevel()) + .mapToObj(lvl -> new EnchantmentInstance(enchantment, lvl)); + } else { + return IntStream.rangeClosed(enchantment.getMinLevel(), enchantment.getMaxLevel()) + .mapToObj(lvl -> new EnchantmentInstance(enchantment, lvl)); + } + }) + .map(instance -> { + return Pair.of(instance, EnchantedBookItem.createForEnchantment(instance)); + }) + .toList(); + EntryRegistry.getInstance().getEntryStacks().forEach(stack -> { + if (stack.getType() != VanillaEntryTypes.ITEM) return; + ItemStack itemStack = stack.castValue(); + if (!itemStack.isEnchantable()) return; + for (Pair pair : enchantmentBooks) { + if (!pair.getKey().enchantment.canEnchant(itemStack)) continue; + Optional> output = DefaultAnvilDisplay.calculateOutput(itemStack, pair.getValue()); + if (output.isEmpty()) continue; + registry.add(new DefaultAnvilDisplay(List.of(EntryIngredients.of(itemStack), EntryIngredients.of(pair.getValue())), + Collections.singletonList(EntryIngredients.of(output.get().getLeft())), Optional.empty(), OptionalInt.of(output.get().getRight()))); + } + }); + for (Registry reg : Registry.REGISTRY) { reg.getTags().forEach(tagPair -> registry.add(tagPair.getFirst())); } diff --git a/default-plugin/src/main/java/me/shedaniel/rei/plugin/client/categories/anvil/DefaultAnvilCategory.java b/default-plugin/src/main/java/me/shedaniel/rei/plugin/client/categories/anvil/DefaultAnvilCategory.java index f8c16278e..efcd41b1f 100644 --- a/default-plugin/src/main/java/me/shedaniel/rei/plugin/client/categories/anvil/DefaultAnvilCategory.java +++ b/default-plugin/src/main/java/me/shedaniel/rei/plugin/client/categories/anvil/DefaultAnvilCategory.java @@ -34,6 +34,9 @@ import me.shedaniel.rei.api.common.util.EntryStacks; import me.shedaniel.rei.plugin.common.BuiltinPlugin; import me.shedaniel.rei.plugin.common.displays.anvil.DefaultAnvilDisplay; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.Font; +import net.minecraft.client.gui.GuiComponent; import net.minecraft.network.chat.Component; import net.minecraft.world.level.block.Blocks; @@ -57,7 +60,7 @@ public Renderer getIcon() { @Override public List setupDisplay(DefaultAnvilDisplay display, Rectangle bounds) { - Point startPoint = new Point(bounds.getCenterX() - 31, bounds.getCenterY() - 13); + Point startPoint = new Point(bounds.getCenterX() - 31, bounds.getCenterY() - (display.getCost().isPresent() ? 20 : 13)); List widgets = Lists.newArrayList(); widgets.add(Widgets.createRecipeBase(bounds)); widgets.add(Widgets.createArrow(new Point(startPoint.x + 27, startPoint.y + 4))); @@ -65,11 +68,20 @@ public List setupDisplay(DefaultAnvilDisplay display, Rectangle bounds) widgets.add(Widgets.createSlot(new Point(startPoint.x + 4 - 22, startPoint.y + 5)).entries(display.getInputEntries().get(0)).markInput()); widgets.add(Widgets.createSlot(new Point(startPoint.x + 4, startPoint.y + 5)).entries(display.getInputEntries().get(1)).markInput()); widgets.add(Widgets.createSlot(new Point(startPoint.x + 61, startPoint.y + 5)).entries(display.getOutputEntries().get(0)).disableBackground().markOutput()); + if (display.getCost().isPresent()) { + widgets.add(Widgets.createDrawableWidget((helper, matrices, mouseX, mouseY, delta) -> { + Font font = Minecraft.getInstance().font; + Component component = Component.translatable("container.repair.cost", display.getCost().getAsInt()); + int x = startPoint.x + 102 - font.width(component) - 2; + GuiComponent.fill(matrices, x - 2, startPoint.y + 28, startPoint.x + 102, startPoint.y + 28 + 12, 0x4f000000); + font.drawShadow(matrices, component, x, startPoint.y + 28 + 2, 0x80ff20); + })); + } return widgets; } @Override public int getDisplayHeight() { - return 36; + return 48; } } diff --git a/default-plugin/src/main/java/me/shedaniel/rei/plugin/common/displays/anvil/AnvilRecipe.java b/default-plugin/src/main/java/me/shedaniel/rei/plugin/common/displays/anvil/AnvilRecipe.java index ba4f70458..694868145 100644 --- a/default-plugin/src/main/java/me/shedaniel/rei/plugin/common/displays/anvil/AnvilRecipe.java +++ b/default-plugin/src/main/java/me/shedaniel/rei/plugin/common/displays/anvil/AnvilRecipe.java @@ -28,6 +28,7 @@ import org.jetbrains.annotations.Nullable; import java.util.List; +import java.util.OptionalInt; public class AnvilRecipe { @Nullable @@ -35,12 +36,18 @@ public class AnvilRecipe { private final List leftInput; private final List rightInputs; private final List outputs; + private final OptionalInt cost; public AnvilRecipe(@Nullable ResourceLocation id, List leftInput, List rightInputs, List outputs) { + this(id, leftInput, rightInputs, outputs, OptionalInt.empty()); + } + + public AnvilRecipe(@Nullable ResourceLocation id, List leftInput, List rightInputs, List outputs, OptionalInt cost) { this.id = id; this.leftInput = leftInput; this.rightInputs = rightInputs; this.outputs = outputs; + this.cost = cost; } public ResourceLocation getId() { @@ -58,4 +65,8 @@ public List getRightInputs() { public List getOutputs() { return outputs; } + + public OptionalInt getCost() { + return cost; + } } diff --git a/default-plugin/src/main/java/me/shedaniel/rei/plugin/common/displays/anvil/DefaultAnvilDisplay.java b/default-plugin/src/main/java/me/shedaniel/rei/plugin/common/displays/anvil/DefaultAnvilDisplay.java index 0c2b5e655..befc8d143 100644 --- a/default-plugin/src/main/java/me/shedaniel/rei/plugin/common/displays/anvil/DefaultAnvilDisplay.java +++ b/default-plugin/src/main/java/me/shedaniel/rei/plugin/common/displays/anvil/DefaultAnvilDisplay.java @@ -28,14 +28,20 @@ import me.shedaniel.rei.api.common.entry.EntryIngredient; import me.shedaniel.rei.api.common.util.EntryIngredients; import me.shedaniel.rei.plugin.common.BuiltinPlugin; +import net.minecraft.client.Minecraft; +import net.minecraft.nbt.CompoundTag; import net.minecraft.resources.ResourceLocation; +import net.minecraft.world.entity.player.Inventory; +import net.minecraft.world.inventory.AnvilMenu; +import net.minecraft.world.item.ItemStack; +import org.apache.commons.lang3.tuple.Pair; +import org.jetbrains.annotations.ApiStatus; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; -import java.util.Optional; +import java.util.*; public class DefaultAnvilDisplay extends BasicDisplay { + private final OptionalInt cost; + public DefaultAnvilDisplay(AnvilRecipe recipe) { this( Arrays.asList( @@ -43,12 +49,22 @@ public DefaultAnvilDisplay(AnvilRecipe recipe) { EntryIngredients.ofItemStacks(recipe.getRightInputs()) ), Collections.singletonList(EntryIngredients.ofItemStacks(recipe.getOutputs())), - Optional.ofNullable(recipe.getId()) + Optional.ofNullable(recipe.getId()), + recipe.getCost() ); } public DefaultAnvilDisplay(List inputs, List outputs, Optional location) { + this(inputs, outputs, location, OptionalInt.empty()); + } + + public DefaultAnvilDisplay(List inputs, List outputs, Optional location, CompoundTag tag) { + this(inputs, outputs, location, tag.contains("Cost") ? OptionalInt.of(tag.getInt("Cost")) : OptionalInt.empty()); + } + + public DefaultAnvilDisplay(List inputs, List outputs, Optional location, OptionalInt cost) { super(inputs, outputs, location); + this.cost = cost; } @Override @@ -56,7 +72,34 @@ public CategoryIdentifier getCategoryIdentifier() { return BuiltinPlugin.ANVIL; } + public OptionalInt getCost() { + return cost; + } + public static BasicDisplay.Serializer serializer() { - return BasicDisplay.Serializer.ofSimple(DefaultAnvilDisplay::new); + return BasicDisplay.Serializer.of(DefaultAnvilDisplay::new, (display, tag) -> { + if (display.getCost().isPresent()) { + tag.putInt("Cost", display.getCost().getAsInt()); + } + }); + } + + @ApiStatus.Experimental + @ApiStatus.Internal + public static Optional> calculateOutput(ItemStack left, ItemStack right) { + try { + if (Minecraft.getInstance().player == null) return Optional.empty(); + AnvilMenu menu = new AnvilMenu(0, new Inventory(Minecraft.getInstance().player)); + menu.setItem(0, menu.incrementStateId(), left); + menu.setItem(1, menu.incrementStateId(), right); + ItemStack output = menu.getSlot(2).getItem().copy(); + if (!output.isEmpty()) { + return Optional.of(Pair.of(output, menu.getCost())); + } else { + return Optional.empty(); + } + } catch (Throwable ignored) { + return Optional.empty(); + } } }