55import net .minestom .server .ServerProcess ;
66import net .minestom .server .event .Event ;
77import net .minestom .server .event .EventNode ;
8+ import net .minestom .server .event .inventory .InventoryCloseEvent ;
89import net .minestom .server .event .inventory .InventoryItemChangeEvent ;
910import net .minestom .server .event .inventory .InventoryPreClickEvent ;
1011import net .minestom .server .inventory .AbstractInventory ;
@@ -27,36 +28,19 @@ public record CraftingRecipes(@NotNull CraftingFeature.Recipes recipes, @NotNull
2728 public EventNode <Event > init () {
2829 EventNode <Event > node = EventNode .all ("vri:recipes-inventory" );
2930
30- node .addListener (InventoryItemChangeEvent .class , event -> {
31- if (event .getInventory () instanceof PlayerInventory inv ) {
32- var crafting = Views .player ().crafting ();
33-
34- InventoryView input = crafting .input ();
35- InventoryView .Singular output = crafting .output ();
36-
37- if (!crafting .isValidExternal (event .getSlot ())) return ;
38-
39- Recipe .Crafting recipe = searchRecipe (2 , 2 , input .collect (inv ));
40-
41- output .set (inv , recipe != null ? recipe .result () : ItemStack .AIR );
42- } else if (event .getInventory () instanceof Inventory inv && inv .getInventoryType () == InventoryType .CRAFTING ) {
43- var crafting = Views .craftingTable ();
44-
45- InventoryView input = crafting .input ();
46- InventoryView .Singular output = crafting .output ();
47-
48- if (!crafting .isValidExternal (event .getSlot ())) return ;
49-
50- Recipe .Crafting recipe = searchRecipe (3 , 3 , input .collect (inv ));
51-
52- output .set (inv , recipe != null ? recipe .result () : ItemStack .AIR );
53- }
54- }).addListener (InventoryPreClickEvent .class , addOutputSlot (
31+ node .addListener (InventoryItemChangeEvent .class , addSquareInputSlots (
5532 inv -> inv instanceof PlayerInventory ,
56- Views .player ().crafting ().input (),
57- Views .player ().crafting ().output ()
58- ))
59- .addListener (InventoryPreClickEvent .class , addOutputSlot (
33+ Views .player ().crafting ().input (),
34+ Views .player ().crafting ().output ()
35+ )).addListener (InventoryItemChangeEvent .class , addSquareInputSlots (
36+ inv -> inv instanceof Inventory crafting && crafting .getInventoryType () == InventoryType .CRAFTING ,
37+ Views .craftingTable ().input (),
38+ Views .craftingTable ().output ()
39+ )).addListener (InventoryPreClickEvent .class , addOutputSlot (
40+ inv -> inv instanceof PlayerInventory ,
41+ Views .player ().crafting ().input (),
42+ Views .player ().crafting ().output ()
43+ )).addListener (InventoryPreClickEvent .class , addOutputSlot (
6044 inv -> inv instanceof Inventory crafting && crafting .getInventoryType () == InventoryType .CRAFTING ,
6145 Views .craftingTable ().input (),
6246 Views .craftingTable ().output ()
@@ -65,7 +49,23 @@ public EventNode<Event> init() {
6549 return node ;
6650 }
6751
68- private static @ NotNull Consumer <InventoryPreClickEvent > addOutputSlot (@ NotNull Predicate <AbstractInventory > predicate , @ NotNull InventoryView input , @ NotNull InventoryView .Singular output ) {
52+ // Assumes that the input region is square.
53+ private @ NotNull Consumer <InventoryItemChangeEvent > addSquareInputSlots (@ NotNull Predicate <AbstractInventory > predicate , @ NotNull InventoryView input , @ NotNull InventoryView .Singular output ) {
54+ return event -> {
55+ final AbstractInventory inv = event .getInventory ();
56+
57+ if (!predicate .test (inv )) return ;
58+ if (!input .isValidExternal (event .getSlot ()) && !output .isValidExternal (event .getSlot ())) return ;
59+
60+ int length = (int ) Math .round (Math .sqrt (input .size ()));
61+
62+ Recipe .Crafting recipe = searchRecipe (length , length , input .collect (inv ));
63+
64+ output .set (inv , recipe != null ? recipe .result () : ItemStack .AIR );
65+ };
66+ }
67+
68+ private @ NotNull Consumer <InventoryPreClickEvent > addOutputSlot (@ NotNull Predicate <AbstractInventory > predicate , @ NotNull InventoryView input , @ NotNull InventoryView .Singular output ) {
6969 return event -> {
7070 final AbstractInventory inv = event .getInventory ();
7171
0 commit comments