99
1010import lombok .Setter ;
1111
12+ import org .bukkit .ChatColor ;
1213import org .bukkit .Material ;
1314import org .bukkit .block .Block ;
1415import org .bukkit .entity .Player ;
1819import io .github .thebusybiscuit .slimefun4 .api .items .SlimefunItemStack ;
1920import io .github .thebusybiscuit .slimefun4 .api .recipes .RecipeType ;
2021import io .github .thebusybiscuit .slimefun4 .libraries .dough .items .CustomItemStack ;
22+ import io .github .thebusybiscuit .slimefun4 .libraries .dough .items .ItemUtils ;
2123import io .github .thebusybiscuit .slimefun4 .utils .ChestMenuUtils ;
2224import me .mrCookieSlime .Slimefun .api .inventory .BlockMenu ;
2325import me .mrCookieSlime .Slimefun .api .inventory .BlockMenuPreset ;
2426import me .mrCookieSlime .Slimefun .api .inventory .DirtyChestMenu ;
2527
2628@ ParametersAreNonnullByDefault
27- public class CraftingBlock extends TickingMenuBlock {
29+ public class CraftingBlock extends MenuBlock {
2830
29- public static final ItemStack INVALID_RECIPE = new CustomItemStack (Material .RED_STAINED_GLASS_PANE , "&cInvalid Recipe!" );
3031 public static final ItemStack CLICK_TO_CRAFT = new CustomItemStack (Material .LIME_STAINED_GLASS_PANE , "&aClick To Craft!" );
3132
3233 @ Setter
@@ -37,90 +38,70 @@ public CraftingBlock(ItemGroup category, SlimefunItemStack item, RecipeType reci
3738 super (category , item , recipeType , recipe );
3839 }
3940
40- @ Nonnull
41- public final CraftingBlock addRecipe (ItemStack output , ItemStack ... inputs ) {
42- if (inputs .length == 0 ) {
43- throw new IllegalArgumentException ("Cannot add recipe with no input!" );
41+ protected void craft (Block b , BlockMenu menu , Player p ) {
42+ int [] slots = layout .inputSlots ();
43+ ItemStack [] input = new ItemStack [slots .length ];
44+ for (int i = 0 ; i < slots .length ; i ++) {
45+ input [i ] = menu .getItemInSlot (slots [i ]);
4446 }
45- CraftingBlockRecipe recipe = new CraftingBlockRecipe (output , inputs );
46- recipes .add (recipe );
47- return this ;
48- }
49-
50- @ Nonnull
51- public final CraftingBlock addRecipesFrom (MachineRecipeType recipeType ) {
52- recipeType .sendRecipesTo ((in , out ) -> addRecipe (out , in ));
53- return this ;
54- }
55-
56- @ Override
57- protected void setup (BlockMenuPreset preset ) {
58- preset .drawBackground (OUTPUT_BORDER , layout .outputBorder ());
59- preset .drawBackground (INPUT_BORDER , layout .inputBorder ());
60- preset .drawBackground (BACKGROUND_ITEM , layout .background ());
61- preset .addItem (layout .statusSlot (), INVALID_RECIPE , ChestMenuUtils .getEmptyClickHandler ());
62- }
6347
64- @ Override
65- protected void onNewInstance (BlockMenu menu , Block b ) {
66- menu .addMenuClickHandler (layout .statusSlot (), (player , i , itemStack , clickAction ) -> craft (b , menu , player ));
67- }
48+ CraftingBlockRecipe recipe = getOutput (input );
6849
69- protected final boolean craft (Block b , BlockMenu menu , Player p ) {
70- if (canCraft (b , p )) {
71- ItemStack [] input = getInput (menu );
72- CraftingBlockRecipe recipe = getOutput (b , input );
73- if (recipe != null ) {
74- if (recipe .check (p )) {
50+ if (recipe != null ) {
51+ if (recipe .check (p )) {
52+ if (menu .fits (recipe .output , layout .outputSlots ())) {
7553 ItemStack output = recipe .output .clone ();
76- if (output (b , menu , p , output )) {
77- onSuccessfulCraft (b , menu , p , output );
78- recipe .consume (input );
79- tick (b , menu );
80- }
54+ modifyOutput (menu , output );
55+ menu .pushItem (output , layout .outputSlots ());
56+ recipe .consume (input );
57+ p .sendMessage (ChatColor .GREEN + "Successfully Crafted: " + ItemUtils .getItemName (output ));
58+ } else {
59+ p .sendMessage (ChatColor .GOLD + "Not Enough Room!" );
8160 }
82- } else {
83- onInvalidRecipe (p );
8461 }
62+ } else {
63+ p .sendMessage (ChatColor .RED + "Invalid Recipe!" );
8564 }
86-
87- return false ;
8865 }
8966
90- @ Override
91- protected final int [] getInputSlots (DirtyChestMenu menu , ItemStack input ) {
92- return new int [0 ];
67+ protected void modifyOutput (BlockMenu menu , ItemStack toOutput ) {
68+
9369 }
9470
9571 @ Override
96- protected final int [] getInputSlots () {
97- return layout .inputSlots ();
72+ protected void setup (BlockMenuPreset preset ) {
73+ preset .drawBackground (OUTPUT_BORDER , layout .outputBorder ());
74+ preset .drawBackground (INPUT_BORDER , layout .inputBorder ());
75+ preset .drawBackground (BACKGROUND_ITEM , layout .background ());
76+ preset .addItem (layout .statusSlot (), CLICK_TO_CRAFT , ChestMenuUtils .getEmptyClickHandler ());
9877 }
9978
10079 @ Override
101- protected final int [] getOutputSlots () {
102- return layout .outputSlots ();
80+ protected void onNewInstance (BlockMenu menu , Block b ) {
81+ menu .addMenuClickHandler (layout .statusSlot (), (player , i , itemStack , clickAction ) -> {
82+ craft (b , menu , player );
83+ return false ;
84+ });
10385 }
10486
105- @ Override
106- protected final void tick ( Block b , BlockMenu menu ) {
107- if (menu . hasViewer () ) {
108- menu . replaceExistingItem ( layout . statusSlot (), getStatus ( b , menu ) );
87+ @ Nonnull
88+ public final CraftingBlock addRecipe ( ItemStack output , ItemStack ... inputs ) {
89+ if (inputs . length == 0 ) {
90+ throw new IllegalArgumentException ( "Cannot add recipe with no input!" );
10991 }
92+ CraftingBlockRecipe recipe = new CraftingBlockRecipe (output , inputs );
93+ recipes .add (recipe );
94+ return this ;
11095 }
11196
11297 @ Nonnull
113- final ItemStack [] getInput (BlockMenu menu ) {
114- int [] slots = layout .inputSlots ();
115- ItemStack [] input = new ItemStack [slots .length ];
116- for (int i = 0 ; i < slots .length ; i ++) {
117- input [i ] = menu .getItemInSlot (slots [i ]);
118- }
119- return input ;
98+ public final CraftingBlock addRecipesFrom (MachineRecipeType recipeType ) {
99+ recipeType .sendRecipesTo ((in , out ) -> addRecipe (out , in ));
100+ return this ;
120101 }
121102
122103 @ Nullable
123- final CraftingBlockRecipe getOutput (Block b , ItemStack [] input ) {
104+ protected final CraftingBlockRecipe getOutput (ItemStack [] input ) {
124105 for (CraftingBlockRecipe recipe : recipes ) {
125106 if (recipe .check (input )) {
126107 return recipe ;
@@ -129,39 +110,19 @@ final CraftingBlockRecipe getOutput(Block b, ItemStack[] input) {
129110 return null ;
130111 }
131112
132- @ Nonnull
133- protected ItemStack getStatus (Block b , BlockMenu menu ) {
134- if (getOutput (b , getInput (menu )) == null ) {
135- return INVALID_RECIPE ;
136- } else {
137- return CLICK_TO_CRAFT ;
138- }
139- }
140-
141- protected boolean output (Block b , BlockMenu menu , Player p , ItemStack output ) {
142- if (menu .fits (output , layout .outputSlots ())) {
143- menu .pushItem (output , layout .outputSlots ());
144- return true ;
145- } else {
146- onNoRoom (p );
147- return false ;
148- }
149- }
150-
151- protected boolean canCraft (Block b , Player p ) {
152- return true ;
153- }
154-
155- protected void onInvalidRecipe (Player p ) {
156-
113+ @ Override
114+ protected final int [] getInputSlots (DirtyChestMenu menu , ItemStack input ) {
115+ return new int [0 ];
157116 }
158117
159- protected void onNoRoom (Player p ) {
160-
118+ @ Override
119+ protected final int [] getInputSlots () {
120+ return layout .inputSlots ();
161121 }
162122
163- protected void onSuccessfulCraft (Block b , BlockMenu menu , Player p , ItemStack output ) {
164-
123+ @ Override
124+ protected final int [] getOutputSlots () {
125+ return layout .outputSlots ();
165126 }
166127
167128}
0 commit comments