Skip to content

Commit

Permalink
Fixes and change to JEI behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
M-W-K committed Nov 8, 2024
1 parent 5628363 commit 03c55bc
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 26 deletions.
91 changes: 65 additions & 26 deletions src/main/java/gregtech/api/items/toolitem/ItemGTToolbelt.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import gregtech.api.unification.material.properties.PropertyKey;
import gregtech.api.unification.material.properties.ToolProperty;
import gregtech.api.util.LocalizationUtils;
import gregtech.common.items.behaviors.ColorSprayBehaviour;
import gregtech.core.network.packets.PacketToolbeltSelectionChange;

import net.minecraft.block.state.IBlockState;
Expand Down Expand Up @@ -269,7 +270,7 @@ public double getDurabilityForDisplay(@NotNull ItemStack stack) {
if (selected != null) {
double dis = selected.getItem().getDurabilityForDisplay(selected);
// vanillaesque tools need to be inverted
if (!(selected.getItem() instanceof IGTTool)) dis = 1 - dis;
if (selected.getItem() instanceof ItemTool) dis = 1 - dis;
return dis;
} else return definition$getDurabilityForDisplay(stack);
}
Expand Down Expand Up @@ -323,17 +324,18 @@ public boolean hasContainerItem(@NotNull ItemStack stack) {

@Override
public @NotNull ItemStack getContainerItem(@NotNull ItemStack stack) {
Ingredient nextCraftIngredient = getHandler(stack).nextCraftIngredient;
if (nextCraftIngredient != null) {
if (getHandler(stack).dealCraftDamageToSelected()) {
stack = stack.copy();
this.craftDamageTools(stack, nextCraftIngredient);
return stack;
}
return super.getContainerItem(stack);
}

public void setOnCraftIngredient(ItemStack stack, Ingredient ingredient) {
getHandler(stack).nextCraftIngredient = ingredient;
Integer match = getHandler(stack).checkIngredientAgainstTools(ingredient);
if (match != null) {
setSelectedTool(match, stack);
}
}

public boolean damageAgainstMaintenanceProblem(ItemStack stack, String toolClass,
Expand All @@ -342,15 +344,11 @@ public boolean damageAgainstMaintenanceProblem(ItemStack stack, String toolClass
}

public boolean supportsIngredient(ItemStack stack, Ingredient ingredient) {
return getHandler(stack).checkIngredientAgainstTools(ingredient, false);
return getHandler(stack).checkIngredientAgainstTools(ingredient) != null;
}

public boolean supportsTool(ItemStack stack, ItemStack tool) {
return getHandler(stack).checkToolAgainstTools(tool, false);
}

public void craftDamageTools(ItemStack stack, Ingredient ingredient) {
getHandler(stack).checkIngredientAgainstTools(ingredient, true);
return getHandler(stack).checkToolAgainstTools(tool) != null;
}

private ToolStackHandler getHandler(ItemStack stack) {
Expand Down Expand Up @@ -400,6 +398,23 @@ public void setSelectedTool(@Nullable Integer slot, ItemStack stack) {
else return result;
}

@Override
public @NotNull EnumActionResult onItemUse(@NotNull EntityPlayer player, @NotNull World world,
@NotNull BlockPos pos, @NotNull EnumHand hand,
@NotNull EnumFacing facing, float hitX, float hitY, float hitZ) {
ToolStackHandler handler = getHandler(player.getHeldItem(hand));
ItemStack selected = handler.getSelectedStack();
if (selected != null) {
ColorSprayBehaviour spray = ColorSprayBehaviour.getBehavior(selected);
if (spray != null) {
EnumActionResult result = spray.useFromToolbelt(player, world, pos, hand, facing, hitX, hitY, hitZ,
selected);
if (result != EnumActionResult.PASS) return result;
}
}
return super.onItemUse(player, world, pos, hand, facing, hitX, hitY, hitZ);
}

@Override
public int getColor(ItemStack stack, int tintIndex) {
if (tintIndex == 0) {
Expand Down Expand Up @@ -432,7 +447,7 @@ public static boolean checkIngredientAgainstToolbelt(@NotNull ItemStack input, @
}

public static boolean checkToolAgainstToolbelt(@NotNull ItemStack toolbelt, @NotNull ItemStack tool) {
if (toolbelt.getItem() instanceof ItemGTToolbelt belt && ToolHelper.isTool(tool)) {
if (toolbelt.getItem() instanceof ItemGTToolbelt belt && ToolHelper.isUtilityItem(tool)) {
return belt.supportsTool(toolbelt, tool);
}
return false;
Expand All @@ -450,7 +465,7 @@ public ToolbeltCapabilityProvider(ItemStack stack) {
String string = toolTag.getString(MATERIAL_KEY);
Material material = GregTechAPI.materialManager.getMaterial(string);
if (material == null) {
toolTag.setString(MATERIAL_KEY, (material = Materials.Iron).toString());
toolTag.setString(MATERIAL_KEY, (material = Materials.Iron).getRegistryName());
}
ToolProperty toolProperty = material.getProperty(PropertyKey.TOOL);
return (int) (toolProperty == null ? 5 : toolProperty.getToolHarvestLevel() * 5.4f);
Expand Down Expand Up @@ -489,6 +504,22 @@ protected ToolStackHandler getHandler(int minsize) {
}
}

@Override
public NBTTagCompound getNBTShareTag(ItemStack stack) {
NBTTagCompound tag = new NBTTagCompound();
if (stack.getTagCompound() != null) {
tag.setTag("NBT", stack.getTagCompound());
}
tag.setTag("Cap", getHandler(stack).serializeNBT());
return tag;
}

@Override
public void readNBTShareTag(ItemStack stack, NBTTagCompound nbt) {
// cap syncing is handled separately, we only need it on the share tag so that changes are detected properly.
stack.setTagCompound(nbt == null ? null : (nbt.hasKey("NBT") ? nbt.getCompoundTag("NBT") : null));
}

protected static class ToolStackHandler extends ItemStackHandler {

private Ingredient nextCraftIngredient;
Expand Down Expand Up @@ -561,7 +592,7 @@ public Set<String> getToolClasses(boolean defaultEmpty) {
public boolean isItemValid(int slot, @NotNull ItemStack stack) {
Item item = stack.getItem();
if (item instanceof ItemGTToolbelt) return false;
return ToolHelper.isTool(stack);
return ToolHelper.isUtilityItem(stack);
}

@Override
Expand Down Expand Up @@ -636,30 +667,38 @@ public boolean checkMaintenanceAgainstTools(String toolClass, boolean doCrafting
return false;
}

public boolean checkIngredientAgainstTools(Ingredient ingredient, boolean doCraftingDamage) {
@Nullable
public Integer checkIngredientAgainstTools(Ingredient ingredient) {
for (int i = 0; i < this.getSlots(); i++) {
ItemStack stack = this.getStackInSlot(i);
if (ingredient.test(stack)) {
if (doCraftingDamage && stack.getItem().hasContainerItem(stack)) {
this.setStackInSlot(i, stack.getItem().getContainerItem(stack));
}
return true;
return i;
}
}
return false;
return null;
}

public boolean checkToolAgainstTools(ItemStack tool, boolean doCraftingDamage) {
public void dealCraftDamageToSlot(int slot) {
ItemStack stack = this.getStackInSlot(slot);
this.setStackInSlot(slot, stack.getItem().getContainerItem(stack));
}

public boolean dealCraftDamageToSelected() {
if (selectedSlot != null) {
dealCraftDamageToSlot(selectedSlot);
return true;
} else return false;
}

@Nullable
public Integer checkToolAgainstTools(ItemStack tool) {
for (int i = 0; i < this.getSlots(); i++) {
ItemStack stack = this.getStackInSlot(i);
if (OreDictionary.itemMatches(stack, tool, false)) {
if (doCraftingDamage && stack.getItem().hasContainerItem(stack)) {
this.setStackInSlot(i, stack.getItem().getContainerItem(stack));
}
return true;
return i;
}
}
return false;
return null;
}
}
}
17 changes: 17 additions & 0 deletions src/main/java/gregtech/api/items/toolitem/ToolHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import gregtech.api.GTValues;
import gregtech.api.capability.GregtechCapabilities;
import gregtech.api.capability.IElectricItem;
import gregtech.api.items.metaitem.MetaItem;
import gregtech.api.items.toolitem.aoe.AoESymmetrical;
import gregtech.api.recipes.Recipe;
import gregtech.api.recipes.RecipeMaps;
Expand All @@ -15,6 +16,7 @@
import gregtech.api.util.function.QuintFunction;
import gregtech.common.ConfigHolder;
import gregtech.common.items.MetaItems;
import gregtech.common.items.behaviors.ColorSprayBehaviour;
import gregtech.tools.enchants.EnchantmentHardHammer;

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

/**
* @return if the itemstack should be considered a utility item and thus can be put into toolbelts.
*/
public static boolean isUtilityItem(ItemStack utility) {
return isTool(utility) || isSpraycan(utility);
}

/**
* @return if the itemstack should be considered a tool
*/
public static boolean isTool(ItemStack tool) {
return tool.getItem() instanceof ItemTool || tool.getItem() instanceof IGTTool;
}

/**
* @return if the itemstack should be considered a spraycan
*/
public static boolean isSpraycan(ItemStack spraycan) {
return spraycan.getItem() instanceof MetaItem<?>meta &&
meta.getBehaviours(spraycan).stream().anyMatch(b -> b instanceof ColorSprayBehaviour);
}

/**
* Return if all the specified tool classes exists in the tool
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package gregtech.common.items.behaviors;

import gregtech.api.items.metaitem.MetaItem;
import gregtech.api.items.metaitem.stats.IItemBehaviour;
import gregtech.api.items.metaitem.stats.IItemDurabilityManager;
import gregtech.api.metatileentity.MetaTileEntity;
import gregtech.api.metatileentity.interfaces.IGregTechTileEntity;
Expand Down Expand Up @@ -68,6 +70,29 @@ public ActionResult<ItemStack> onItemUse(EntityPlayer player, World world, Block
return ActionResult.newResult(EnumActionResult.SUCCESS, player.getHeldItem(hand));
}

@Nullable
public static ColorSprayBehaviour getBehavior(ItemStack spraycan) {
if (!(spraycan.getItem() instanceof MetaItem<?>meta)) return null;
for (IItemBehaviour behaviour : meta.getBehaviours(spraycan)) {
if (behaviour instanceof ColorSprayBehaviour spray) return spray;
}
return null;
}

public EnumActionResult useFromToolbelt(EntityPlayer player, World world, BlockPos pos, EnumHand hand,
EnumFacing facing, float hitX, float hitY, float hitZ, ItemStack spraycan) {
if (!player.canPlayerEdit(pos, facing, spraycan)) {
return EnumActionResult.FAIL;
}
if (!tryPaintBlock(player, world, pos, facing)) {
return EnumActionResult.PASS;
}
useItemDurability(player, hand, spraycan, empty.copy());
world.playSound(null, player.posX, player.posY, player.posZ, GTSoundEvents.SPRAY_CAN_TOOL,
SoundCategory.PLAYERS, 1.0f, 1.0f);
return EnumActionResult.SUCCESS;
}

private boolean tryPaintBlock(EntityPlayer player, World world, BlockPos pos, EnumFacing side) {
IBlockState blockState = world.getBlockState(pos);
Block block = blockState.getBlock();
Expand Down

0 comments on commit 03c55bc

Please sign in to comment.