Skip to content

Commit 03c55bc

Browse files
committed
Fixes and change to JEI behavior
1 parent 5628363 commit 03c55bc

File tree

3 files changed

+107
-26
lines changed

3 files changed

+107
-26
lines changed

src/main/java/gregtech/api/items/toolitem/ItemGTToolbelt.java

Lines changed: 65 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import gregtech.api.unification.material.properties.PropertyKey;
1212
import gregtech.api.unification.material.properties.ToolProperty;
1313
import gregtech.api.util.LocalizationUtils;
14+
import gregtech.common.items.behaviors.ColorSprayBehaviour;
1415
import gregtech.core.network.packets.PacketToolbeltSelectionChange;
1516

1617
import 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
}

src/main/java/gregtech/api/items/toolitem/ToolHelper.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import gregtech.api.GTValues;
44
import gregtech.api.capability.GregtechCapabilities;
55
import gregtech.api.capability.IElectricItem;
6+
import gregtech.api.items.metaitem.MetaItem;
67
import gregtech.api.items.toolitem.aoe.AoESymmetrical;
78
import gregtech.api.recipes.Recipe;
89
import gregtech.api.recipes.RecipeMaps;
@@ -15,6 +16,7 @@
1516
import gregtech.api.util.function.QuintFunction;
1617
import gregtech.common.ConfigHolder;
1718
import gregtech.common.items.MetaItems;
19+
import gregtech.common.items.behaviors.ColorSprayBehaviour;
1820
import gregtech.tools.enchants.EnchantmentHardHammer;
1921

2022
import net.minecraft.advancements.CriteriaTriggers;
@@ -389,13 +391,28 @@ public static boolean isTool(ItemStack tool, String... toolClasses) {
389391
return false;
390392
}
391393

394+
/**
395+
* @return if the itemstack should be considered a utility item and thus can be put into toolbelts.
396+
*/
397+
public static boolean isUtilityItem(ItemStack utility) {
398+
return isTool(utility) || isSpraycan(utility);
399+
}
400+
392401
/**
393402
* @return if the itemstack should be considered a tool
394403
*/
395404
public static boolean isTool(ItemStack tool) {
396405
return tool.getItem() instanceof ItemTool || tool.getItem() instanceof IGTTool;
397406
}
398407

408+
/**
409+
* @return if the itemstack should be considered a spraycan
410+
*/
411+
public static boolean isSpraycan(ItemStack spraycan) {
412+
return spraycan.getItem() instanceof MetaItem<?>meta &&
413+
meta.getBehaviours(spraycan).stream().anyMatch(b -> b instanceof ColorSprayBehaviour);
414+
}
415+
399416
/**
400417
* Return if all the specified tool classes exists in the tool
401418
*/

src/main/java/gregtech/common/items/behaviors/ColorSprayBehaviour.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package gregtech.common.items.behaviors;
22

3+
import gregtech.api.items.metaitem.MetaItem;
4+
import gregtech.api.items.metaitem.stats.IItemBehaviour;
35
import gregtech.api.items.metaitem.stats.IItemDurabilityManager;
46
import gregtech.api.metatileentity.MetaTileEntity;
57
import gregtech.api.metatileentity.interfaces.IGregTechTileEntity;
@@ -68,6 +70,29 @@ public ActionResult<ItemStack> onItemUse(EntityPlayer player, World world, Block
6870
return ActionResult.newResult(EnumActionResult.SUCCESS, player.getHeldItem(hand));
6971
}
7072

73+
@Nullable
74+
public static ColorSprayBehaviour getBehavior(ItemStack spraycan) {
75+
if (!(spraycan.getItem() instanceof MetaItem<?>meta)) return null;
76+
for (IItemBehaviour behaviour : meta.getBehaviours(spraycan)) {
77+
if (behaviour instanceof ColorSprayBehaviour spray) return spray;
78+
}
79+
return null;
80+
}
81+
82+
public EnumActionResult useFromToolbelt(EntityPlayer player, World world, BlockPos pos, EnumHand hand,
83+
EnumFacing facing, float hitX, float hitY, float hitZ, ItemStack spraycan) {
84+
if (!player.canPlayerEdit(pos, facing, spraycan)) {
85+
return EnumActionResult.FAIL;
86+
}
87+
if (!tryPaintBlock(player, world, pos, facing)) {
88+
return EnumActionResult.PASS;
89+
}
90+
useItemDurability(player, hand, spraycan, empty.copy());
91+
world.playSound(null, player.posX, player.posY, player.posZ, GTSoundEvents.SPRAY_CAN_TOOL,
92+
SoundCategory.PLAYERS, 1.0f, 1.0f);
93+
return EnumActionResult.SUCCESS;
94+
}
95+
7196
private boolean tryPaintBlock(EntityPlayer player, World world, BlockPos pos, EnumFacing side) {
7297
IBlockState blockState = world.getBlockState(pos);
7398
Block block = blockState.getBlock();

0 commit comments

Comments
 (0)