22
33import io .github .mooy1 .infinityexpansion .lists .Categories ;
44import io .github .mooy1 .infinityexpansion .lists .Items ;
5+ import io .github .mooy1 .infinitylib .items .StackUtils ;
56import io .github .mooy1 .infinitylib .objects .AbstractContainer ;
67import io .github .mooy1 .infinitylib .player .MessageUtils ;
78import io .github .mooy1 .infinitylib .presets .MenuPreset ;
1415import me .mrCookieSlime .Slimefun .api .inventory .BlockMenu ;
1516import me .mrCookieSlime .Slimefun .api .inventory .BlockMenuPreset ;
1617import me .mrCookieSlime .Slimefun .api .item_transport .ItemTransportFlow ;
17- import me .mrCookieSlime .Slimefun .cscorelib2 .inventory . ItemUtils ;
18+ import me .mrCookieSlime .Slimefun .cscorelib2 .collections . Pair ;
1819import me .mrCookieSlime .Slimefun .cscorelib2 .item .CustomItem ;
1920import org .bukkit .Material ;
2021import org .bukkit .block .Block ;
2425import javax .annotation .Nullable ;
2526import java .util .ArrayList ;
2627import java .util .List ;
28+ import java .util .Locale ;
29+
2730/**
2831 * Machine that changes the material of gear and tools
2932 *
@@ -52,7 +55,7 @@ public class GearTransformer extends AbstractContainer implements EnergyNetCompo
5255 private static final int STATUS_SLOT = MenuPreset .slot2 ;
5356
5457 public GearTransformer () {
55- super (Categories .ADVANCED_MACHINES , Items .GEAR_TRANSFORMER , RecipeType .ENHANCED_CRAFTING_TABLE , new ItemStack []{
58+ super (Categories .ADVANCED_MACHINES , Items .GEAR_TRANSFORMER , RecipeType .ENHANCED_CRAFTING_TABLE , new ItemStack [] {
5659 Items .MAGSTEEL_PLATE , Items .MACHINE_CIRCUIT , Items .MAGSTEEL_PLATE ,
5760 Items .MACHINE_CIRCUIT , new ItemStack (Material .SMITHING_TABLE ), Items .MACHINE_CIRCUIT ,
5861 Items .MAGSTEEL_PLATE , Items .MACHINE_CIRCUIT , Items .MAGSTEEL_PLATE
@@ -85,29 +88,23 @@ public void tick(@Nonnull Block b, @Nonnull BlockMenu inv) {
8588
8689 if (inputItem == null ) { //no input
8790
88- inv .replaceExistingItem (STATUS_SLOT , new CustomItem (Material .BLUE_STAINED_GLASS_PANE , "&9Input a tool" ));
91+ inv .replaceExistingItem (STATUS_SLOT , new CustomItem (Material .BLUE_STAINED_GLASS_PANE , "&9Input a tool or piece of gear " ));
8992 return ;
9093
9194 }
92-
93- String inputToolType ;
94-
95- if (getToolType (inputItem ) != null ) {
96- inputToolType = getToolType (inputItem );
97- } else {
98- inputToolType = getArmorType (inputItem );
95+
96+ if (StackUtils .getItemID (inputItem , false ) != null ) {
97+ inv .replaceExistingItem (STATUS_SLOT , new CustomItem (Material .RED_STAINED_GLASS_PANE , "&cSlimefun items may not have their material changed!" ));
98+ return ;
9999 }
100100
101+ String inputToolType = getType (inputItem );
102+
101103 if (inputToolType == null ) { //invalid input
102104
103- inv .replaceExistingItem (STATUS_SLOT , new CustomItem (Material .BARRIER , "&cNot a tool or armor !" ));
105+ inv .replaceExistingItem (STATUS_SLOT , new CustomItem (Material .BARRIER , "&cNot a tool or piece of gear !" ));
104106 return ;
105107
106- /*if (inv.getItemInSlot(OUTPUT_SLOTS[0]) == null) {
107- inv.pushItem(inputItem, OUTPUT_SLOTS);
108- inv.consumeItem(INPUT_SLOT1, inputItem.getAmount());
109- }*/
110-
111108 }
112109
113110 ItemStack inputMaterial = inv .getItemInSlot (INPUT_SLOT2 );
@@ -119,9 +116,9 @@ public void tick(@Nonnull Block b, @Nonnull BlockMenu inv) {
119116
120117 }
121118
122- Material outputMaterial = getOutput (inputMaterial , inputToolType );
119+ Pair < Material , Integer > pair = getOutput (inputMaterial , inputToolType );
123120
124- if (outputMaterial == null ) { //invalid material
121+ if (pair == null ) { //invalid material
125122
126123 inv .replaceExistingItem (STATUS_SLOT , new CustomItem (Material .BARRIER , "&cInvalid Materials!" ));
127124 return ;
@@ -138,13 +135,13 @@ public void tick(@Nonnull Block b, @Nonnull BlockMenu inv) {
138135 //output
139136 removeCharge (b .getLocation (), ENERGY );
140137
141- MessageUtils .messagePlayersInInv (inv , "Transformed into: " + ItemUtils . getItemName ( new ItemStack ( outputMaterial ) ));
138+ MessageUtils .messagePlayersInInv (inv , "Transformed into: " + pair . getFirstValue (). toString (). toUpperCase ( Locale . ROOT ));
142139
143- inputItem .setType (outputMaterial );
140+ inputItem .setType (pair . getFirstValue () );
144141 inv .pushItem (inputItem , OUTPUT_SLOTS );
145142
146- inv .consumeItem (INPUT_SLOT1 );
147- inv .consumeItem (INPUT_SLOT2 , getAmount ( inputMaterial , inputToolType ));
143+ inv .replaceExistingItem (INPUT_SLOT1 , null );
144+ inv .consumeItem (INPUT_SLOT2 , pair . getSecondValue ( ));
148145
149146 inv .replaceExistingItem (STATUS_SLOT , new CustomItem (Material .LIME_STAINED_GLASS_PANE , "&aTool Transformed!" ));
150147 }
@@ -181,97 +178,65 @@ public int[] getTransportSlots(@Nonnull ItemTransportFlow flow) {
181178 return new int [0 ];
182179 }
183180 }
184-
181+
185182 /**
186183 * This method gets the output from the input material and input tool
187- *
184+ *
188185 * @param inputMaterial material
189186 * @param inputToolType tools type
187+ *
190188 * @return output if any
191189 */
192190 @ Nullable
193- private Material getOutput (ItemStack inputMaterial , String inputToolType ) {
191+ private Pair < Material , Integer > getOutput (ItemStack inputMaterial , String inputToolType ) {
194192
195193 for (String toolType : TOOL_TYPES ) {
196194 if (inputToolType .equals (toolType )) { //make sure its a tool
197195
198- for (int i = 0 ; i < TOOL_RECIPE .length ; i ++) { //compare to each recipe
196+ for (int i = 0 ; i < TOOL_RECIPE .length ; i ++) { //compare to each recipe
199197 ItemStack recipe = TOOL_RECIPE [i ];
200198
201199 if (inputMaterial .getType () == recipe .getType () && inputMaterial .getAmount () >= recipe .getAmount ()) {
202- return Material .getMaterial (TOOL_MATERIALS [i ] + toolType );
200+
201+ return new Pair <>(Material .getMaterial (TOOL_MATERIALS [i ] + toolType ), recipe .getAmount ());
203202 }
204203 }
204+
205+ break ;
205206 }
206207 }
207208
208209 for (String armorType : ARMOR_TYPES ) {
209210 if (inputToolType .equals (armorType )) { //make sure its a armor
210211
211- for (int i = 0 ; i < ARMOR_RECIPE .length ; i ++) { //compare to each recipe
212+ for (int i = 0 ; i < ARMOR_RECIPE .length ; i ++) { //compare to each recipe
212213 ItemStack recipe = ARMOR_RECIPE [i ];
213214
214215 if (inputMaterial .getType () == recipe .getType () && inputMaterial .getAmount () >= recipe .getAmount ()) {
215216
216- return Material .getMaterial (ARMOR_MATERIALS [i ] + armorType );
217+ return new Pair <>( Material .getMaterial (ARMOR_MATERIALS [i ] + armorType ), recipe . getAmount () );
217218 }
218219 }
220+
221+ break ;
219222 }
220223 }
221224
222225 return null ;
223226 }
224227
225- /**
226- * This method gets the amount of material required to transform and item
227- *
228- * @param inputMaterial material input type
229- * @param inputToolType tool input type
230- * @return amount needed
231- */
232- private int getAmount (ItemStack inputMaterial , String inputToolType ) {
233-
234- for (String toolType : TOOL_TYPES ) {
235-
236- if (inputToolType .equals (toolType )) {
237-
238- for (ItemStack input : TOOL_RECIPE ) {
239-
240- if (inputMaterial .getType () == input .getType () && inputMaterial .getAmount () >= input .getAmount ()) {
241-
242- return input .getAmount ();
243- }
244- }
245- }
246- }
228+ @ Nullable
229+ private String getType (ItemStack item ) {
230+ Material material = item .getType ();
247231
248232 for (String armorType : ARMOR_TYPES ) {
249233
250- if (inputToolType .equals (armorType )) {
251-
252- for (ItemStack input : ARMOR_RECIPE ) {
253-
254- if (inputMaterial .getType () == input .getType () && inputMaterial .getAmount () >= input .getAmount ()) {
234+ for (String armorMaterial : ARMOR_MATERIALS ) {
255235
256- return input .getAmount ();
257- }
258- }
236+ if (material == Material .getMaterial (armorMaterial + armorType )) return armorType ;
259237 }
260238 }
261239
262- return 0 ;
263- }
264-
265- /**
266- * This method gets the type of tool that an item is
267- *
268- * @param item item to check
269- * @return type of tool if any
270- */
271- @ Nullable
272- private String getToolType (ItemStack item ) {
273- Material material = item .getType ();
274-
275240 for (String toolType : TOOL_TYPES ) {
276241
277242 for (String toolMaterial : TOOL_MATERIALS ) {
@@ -280,26 +245,7 @@ private String getToolType(ItemStack item) {
280245 }
281246 }
282247 return null ;
283- }
284-
285- /**
286- * This method gets the type of armor that an item is
287- *
288- * @param item item to check
289- * @return type of armor if any
290- */
291- @ Nullable
292- private String getArmorType (ItemStack item ) {
293- Material material = item .getType ();
294-
295- for (String armorType : ARMOR_TYPES ) {
296-
297- for (String armorMaterial : ARMOR_MATERIALS ) {
298248
299- if (material == Material .getMaterial (armorMaterial + armorType )) return armorType ;
300- }
301- }
302- return null ;
303249 }
304250
305251 private static final String [] ARMOR_TYPES = {
@@ -348,10 +294,7 @@ public int getCapacity() {
348294 public List <ItemStack > getDisplayRecipes () {
349295 List <ItemStack > items = new ArrayList <>();
350296
351- items .add (new CustomItem (Material .DIAMOND_PICKAXE , "&7For Tools >>>" ));
352- items .add (new CustomItem (Material .DIAMOND_CHESTPLATE , "&7For Armor >>>" ));
353-
354- for (int i = 0 ; i < TOOL_RECIPE .length ; i ++) {
297+ for (int i = 0 ; i < TOOL_RECIPE .length ; i ++) {
355298 items .add (TOOL_RECIPE [i ]);
356299 items .add (ARMOR_RECIPE [i ]);
357300 }
@@ -376,4 +319,5 @@ public List<ItemStack> getDisplayRecipes() {
376319 new ItemStack (Material .DIAMOND , 9 ),
377320 new ItemStack (Material .NETHERITE_INGOT , 2 )
378321 };
322+
379323}
0 commit comments