1111import gregtech .api .unification .material .properties .PropertyKey ;
1212import gregtech .api .unification .material .properties .ToolProperty ;
1313import gregtech .api .util .LocalizationUtils ;
14+ import gregtech .common .items .behaviors .ColorSprayBehaviour ;
1415import gregtech .core .network .packets .PacketToolbeltSelectionChange ;
1516
1617import net .minecraft .block .state .IBlockState ;
@@ -269,7 +270,7 @@ public double getDurabilityForDisplay(@NotNull ItemStack stack) {
269270 if (selected != null ) {
270271 double dis = selected .getItem ().getDurabilityForDisplay (selected );
271272 // vanillaesque tools need to be inverted
272- if (!( selected .getItem () instanceof IGTTool ) ) dis = 1 - dis ;
273+ if (selected .getItem () instanceof ItemTool ) dis = 1 - dis ;
273274 return dis ;
274275 } else return definition$getDurabilityForDisplay (stack );
275276 }
@@ -323,17 +324,18 @@ public boolean hasContainerItem(@NotNull ItemStack stack) {
323324
324325 @ Override
325326 public @ NotNull ItemStack getContainerItem (@ NotNull ItemStack stack ) {
326- Ingredient nextCraftIngredient = getHandler (stack ).nextCraftIngredient ;
327- if (nextCraftIngredient != null ) {
327+ if (getHandler (stack ).dealCraftDamageToSelected ()) {
328328 stack = stack .copy ();
329- this .craftDamageTools (stack , nextCraftIngredient );
330329 return stack ;
331330 }
332331 return super .getContainerItem (stack );
333332 }
334333
335334 public void setOnCraftIngredient (ItemStack stack , Ingredient ingredient ) {
336- getHandler (stack ).nextCraftIngredient = ingredient ;
335+ Integer match = getHandler (stack ).checkIngredientAgainstTools (ingredient );
336+ if (match != null ) {
337+ setSelectedTool (match , stack );
338+ }
337339 }
338340
339341 public boolean damageAgainstMaintenanceProblem (ItemStack stack , String toolClass ,
@@ -342,15 +344,11 @@ public boolean damageAgainstMaintenanceProblem(ItemStack stack, String toolClass
342344 }
343345
344346 public boolean supportsIngredient (ItemStack stack , Ingredient ingredient ) {
345- return getHandler (stack ).checkIngredientAgainstTools (ingredient , false ) ;
347+ return getHandler (stack ).checkIngredientAgainstTools (ingredient ) != null ;
346348 }
347349
348350 public boolean supportsTool (ItemStack stack , ItemStack tool ) {
349- return getHandler (stack ).checkToolAgainstTools (tool , false );
350- }
351-
352- public void craftDamageTools (ItemStack stack , Ingredient ingredient ) {
353- getHandler (stack ).checkIngredientAgainstTools (ingredient , true );
351+ return getHandler (stack ).checkToolAgainstTools (tool ) != null ;
354352 }
355353
356354 private ToolStackHandler getHandler (ItemStack stack ) {
@@ -400,6 +398,23 @@ public void setSelectedTool(@Nullable Integer slot, ItemStack stack) {
400398 else return result ;
401399 }
402400
401+ @ Override
402+ public @ NotNull EnumActionResult onItemUse (@ NotNull EntityPlayer player , @ NotNull World world ,
403+ @ NotNull BlockPos pos , @ NotNull EnumHand hand ,
404+ @ NotNull EnumFacing facing , float hitX , float hitY , float hitZ ) {
405+ ToolStackHandler handler = getHandler (player .getHeldItem (hand ));
406+ ItemStack selected = handler .getSelectedStack ();
407+ if (selected != null ) {
408+ ColorSprayBehaviour spray = ColorSprayBehaviour .getBehavior (selected );
409+ if (spray != null ) {
410+ EnumActionResult result = spray .useFromToolbelt (player , world , pos , hand , facing , hitX , hitY , hitZ ,
411+ selected );
412+ if (result != EnumActionResult .PASS ) return result ;
413+ }
414+ }
415+ return super .onItemUse (player , world , pos , hand , facing , hitX , hitY , hitZ );
416+ }
417+
403418 @ Override
404419 public int getColor (ItemStack stack , int tintIndex ) {
405420 if (tintIndex == 0 ) {
@@ -432,7 +447,7 @@ public static boolean checkIngredientAgainstToolbelt(@NotNull ItemStack input, @
432447 }
433448
434449 public static boolean checkToolAgainstToolbelt (@ NotNull ItemStack toolbelt , @ NotNull ItemStack tool ) {
435- if (toolbelt .getItem () instanceof ItemGTToolbelt belt && ToolHelper .isTool (tool )) {
450+ if (toolbelt .getItem () instanceof ItemGTToolbelt belt && ToolHelper .isUtilityItem (tool )) {
436451 return belt .supportsTool (toolbelt , tool );
437452 }
438453 return false ;
@@ -450,7 +465,7 @@ public ToolbeltCapabilityProvider(ItemStack stack) {
450465 String string = toolTag .getString (MATERIAL_KEY );
451466 Material material = GregTechAPI .materialManager .getMaterial (string );
452467 if (material == null ) {
453- toolTag .setString (MATERIAL_KEY , (material = Materials .Iron ).toString ());
468+ toolTag .setString (MATERIAL_KEY , (material = Materials .Iron ).getRegistryName ());
454469 }
455470 ToolProperty toolProperty = material .getProperty (PropertyKey .TOOL );
456471 return (int ) (toolProperty == null ? 5 : toolProperty .getToolHarvestLevel () * 5.4f );
@@ -489,6 +504,22 @@ protected ToolStackHandler getHandler(int minsize) {
489504 }
490505 }
491506
507+ @ Override
508+ public NBTTagCompound getNBTShareTag (ItemStack stack ) {
509+ NBTTagCompound tag = new NBTTagCompound ();
510+ if (stack .getTagCompound () != null ) {
511+ tag .setTag ("NBT" , stack .getTagCompound ());
512+ }
513+ tag .setTag ("Cap" , getHandler (stack ).serializeNBT ());
514+ return tag ;
515+ }
516+
517+ @ Override
518+ public void readNBTShareTag (ItemStack stack , NBTTagCompound nbt ) {
519+ // cap syncing is handled separately, we only need it on the share tag so that changes are detected properly.
520+ stack .setTagCompound (nbt == null ? null : (nbt .hasKey ("NBT" ) ? nbt .getCompoundTag ("NBT" ) : null ));
521+ }
522+
492523 protected static class ToolStackHandler extends ItemStackHandler {
493524
494525 private Ingredient nextCraftIngredient ;
@@ -561,7 +592,7 @@ public Set<String> getToolClasses(boolean defaultEmpty) {
561592 public boolean isItemValid (int slot , @ NotNull ItemStack stack ) {
562593 Item item = stack .getItem ();
563594 if (item instanceof ItemGTToolbelt ) return false ;
564- return ToolHelper .isTool (stack );
595+ return ToolHelper .isUtilityItem (stack );
565596 }
566597
567598 @ Override
@@ -636,30 +667,38 @@ public boolean checkMaintenanceAgainstTools(String toolClass, boolean doCrafting
636667 return false ;
637668 }
638669
639- public boolean checkIngredientAgainstTools (Ingredient ingredient , boolean doCraftingDamage ) {
670+ @ Nullable
671+ public Integer checkIngredientAgainstTools (Ingredient ingredient ) {
640672 for (int i = 0 ; i < this .getSlots (); i ++) {
641673 ItemStack stack = this .getStackInSlot (i );
642674 if (ingredient .test (stack )) {
643- if (doCraftingDamage && stack .getItem ().hasContainerItem (stack )) {
644- this .setStackInSlot (i , stack .getItem ().getContainerItem (stack ));
645- }
646- return true ;
675+ return i ;
647676 }
648677 }
649- return false ;
678+ return null ;
650679 }
651680
652- public boolean checkToolAgainstTools (ItemStack tool , boolean doCraftingDamage ) {
681+ public void dealCraftDamageToSlot (int slot ) {
682+ ItemStack stack = this .getStackInSlot (slot );
683+ this .setStackInSlot (slot , stack .getItem ().getContainerItem (stack ));
684+ }
685+
686+ public boolean dealCraftDamageToSelected () {
687+ if (selectedSlot != null ) {
688+ dealCraftDamageToSlot (selectedSlot );
689+ return true ;
690+ } else return false ;
691+ }
692+
693+ @ Nullable
694+ public Integer checkToolAgainstTools (ItemStack tool ) {
653695 for (int i = 0 ; i < this .getSlots (); i ++) {
654696 ItemStack stack = this .getStackInSlot (i );
655697 if (OreDictionary .itemMatches (stack , tool , false )) {
656- if (doCraftingDamage && stack .getItem ().hasContainerItem (stack )) {
657- this .setStackInSlot (i , stack .getItem ().getContainerItem (stack ));
658- }
659- return true ;
698+ return i ;
660699 }
661700 }
662- return false ;
701+ return null ;
663702 }
664703 }
665704}
0 commit comments