Skip to content

Commit

Permalink
Fixed stack overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
kill05 committed Jun 17, 2024
1 parent e7f3108 commit 4625cf3
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 19 deletions.
12 changes: 9 additions & 3 deletions src/main/java/com/github/kill05/items/tool/ArchitectTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import com.github.kill05.items.part.statistics.PartStatistic;
import com.github.kill05.items.part.statistics.PartStatistics;
import com.github.kill05.materials.ArchitectMaterial;
import com.github.kill05.utils.ItemUtils;
import net.minecraft.core.block.Block;
import net.minecraft.core.data.tag.Tag;
import net.minecraft.core.entity.Entity;
Expand Down Expand Up @@ -70,6 +69,11 @@ public ArchitectTool(String toolId) {
}


public static boolean isToolBroken(ItemStack item) {
return item.getMetadata() >= item.getMaxDamage();
}


//todo: remove once 7.2-pre2 comes out (need itemstack argument)
@Override
public boolean canHarvestBlock(Block block) {
Expand All @@ -93,12 +97,14 @@ public int getMaxDamage() {

@Override
public float getStrVsBlock(ItemStack itemStack, Block block) {
if(ItemUtils.isBroken(itemStack)) return super.getStrVsBlock(itemStack, block);
if(isToolBroken(itemStack)) return super.getStrVsBlock(itemStack, block);
return canHarvestBlock(itemStack, block) ? getStatistic(itemStack, PartStatistic.MINING_SPEED) : super.getStrVsBlock(itemStack, block);
}

//todo: replace once 7.2-pre2 comes out (need itemstack argument)
public boolean canHarvestBlock(ItemStack itemStack, Block block) {
if(isToolBroken(itemStack)) return false;

for (Tag<Block> mineableTag : mineableTags) {
if(block.hasTag(mineableTag)) return true;
}
Expand All @@ -107,7 +113,7 @@ public boolean canHarvestBlock(ItemStack itemStack, Block block) {
}

public int getDamageVsEntity(Entity entity, ItemStack itemStack) {
if(ItemUtils.isBroken(itemStack)) return super.getDamageVsEntity(entity);
if(isToolBroken(itemStack)) return super.getDamageVsEntity(entity);
return getStatistic(itemStack, PartStatistic.ENTITY_DAMAGE).intValue();
}

Expand Down
3 changes: 1 addition & 2 deletions src/main/java/com/github/kill05/items/tool/ToolPartInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import com.github.kill05.items.part.PartType;
import com.github.kill05.materials.ArchitectMaterial;
import com.github.kill05.materials.MaterialType;
import com.github.kill05.utils.ItemUtils;
import net.minecraft.client.render.stitcher.IconCoordinate;
import net.minecraft.core.item.ItemStack;

Expand Down Expand Up @@ -57,7 +56,7 @@ public void setTool(ArchitectTool tool) {

public IconCoordinate getIcon(ItemStack itemStack) {
ArchitectMaterial material = ArchitectTools.getToolPart(itemStack, this);
return MaterialType.getTexture(ItemUtils.isBroken(itemStack) ? brokenTexture : texture, material);
return MaterialType.getTexture(ArchitectTool.isToolBroken(itemStack) ? brokenTexture : texture, material);
}

public ArchitectPart part() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.kill05.mixins;

import com.github.kill05.items.tool.ArchitectTool;
import com.github.kill05.utils.ItemUtils;
import net.minecraft.client.render.item.model.ItemModelStandard;
import net.minecraft.core.item.ItemStack;
import org.spongepowered.asm.mixin.Mixin;
Expand All @@ -12,14 +11,14 @@
value = ItemModelStandard.class,
remap = false
)
public class ItemModelStandardMixin {
public abstract class ItemModelStandardMixin {

@Redirect(
method = "renderItemOverlayIntoGUI",
at = @At(value = "INVOKE", target = "Lnet/minecraft/core/item/ItemStack;isItemDamaged()Z")
)
public boolean redirectRenderItemOverlay(ItemStack item) {
if(!(item.getItem() instanceof ArchitectTool)) return item.isItemDamaged();
return !ItemUtils.isBroken(item) && item.isItemDamaged();
return !ArchitectTool.isToolBroken(item) && item.isItemDamaged();
}
}
17 changes: 12 additions & 5 deletions src/main/java/com/github/kill05/mixins/ItemStackMixin.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.github.kill05.mixins;

import com.github.kill05.items.tool.ArchitectTool;
import com.github.kill05.utils.ItemUtils;
import net.minecraft.core.block.Block;
import net.minecraft.core.entity.Entity;
import net.minecraft.core.item.Item;
Expand Down Expand Up @@ -49,17 +48,25 @@ public void getMaxDamageMixin(CallbackInfoReturnable<Integer> cir) {
cancellable = true
)
public void isItemStackDamageableMixin(CallbackInfoReturnable<Boolean> cir) {
if (getItem() instanceof ArchitectTool && !ItemUtils.isBroken(getThis())) {
cir.setReturnValue(true);
}
if (getItem() instanceof ArchitectTool) cir.setReturnValue(true);
}

@Inject(
method = "damageItem",
at = @At(value = "FIELD", target = "Lnet/minecraft/core/item/ItemStack;stackSize:I", ordinal = 1),
at = @At(value = "FIELD", target = "Lnet/minecraft/core/item/ItemStack;metadata:I", ordinal = 0, shift = At.Shift.BEFORE),
cancellable = true
)
public void injectDamageItem(int i, Entity entity, CallbackInfo ci) {
if (!(getItem() instanceof ArchitectTool)) return;
if(ArchitectTool.isToolBroken(getThis())) ci.cancel();
}

@Inject(
method = "damageItem",
at = @At(value = "FIELD", target = "Lnet/minecraft/core/item/ItemStack;stackSize:I", ordinal = 1),
cancellable = true
)
public void injectBreakItem(int i, Entity entity, CallbackInfo ci) {
if (!(getItem() instanceof ArchitectTool)) return;
ci.cancel();
}
Expand Down
6 changes: 0 additions & 6 deletions src/main/java/com/github/kill05/utils/ItemUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,4 @@ public static String getDisabledDescription() {
§ethen set 'DisableVanillaTools' to 'false'.""";
}


public static boolean isBroken(ItemStack item) {
if(!item.isItemStackDamageable()) return false;
return item.getMetadata() > item.getMaxDamage();
}

}

0 comments on commit 4625cf3

Please sign in to comment.