Skip to content

Commit

Permalink
Added ArchitectTool durability
Browse files Browse the repository at this point in the history
  • Loading branch information
kill05 committed Jun 17, 2024
1 parent 220e92a commit e7f3108
Show file tree
Hide file tree
Showing 11 changed files with 76 additions and 23 deletions.
9 changes: 1 addition & 8 deletions src/main/java/com/github/kill05/items/tool/ArchitectAxe.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

import com.github.kill05.items.part.ArchitectPart;
import com.github.kill05.items.part.statistics.PartStatistic;
import net.minecraft.core.block.Block;
import net.minecraft.core.block.tag.BlockTags;
import net.minecraft.core.item.ItemStack;

public class ArchitectAxe extends ArchitectTool {

public ArchitectAxe() {
super("axe");
addPart(ArchitectPart.AXE_HEAD, "axe/head");
addPart(ArchitectPart.AXE_HEAD, "axe/head", "axe/broken_head");
addPart(ArchitectPart.TOOL_BINDING, "axe/binding").renderPriority(-1);
addPart(ArchitectPart.TOOL_ROD, "pickaxe/handle");

Expand All @@ -19,9 +17,4 @@ public ArchitectAxe() {

addMineableTags(BlockTags.MINEABLE_BY_AXE);
}

@Override
public boolean canHarvestBlock(ItemStack item, Block block) {
return block.hasTag(BlockTags.MINEABLE_BY_AXE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class ArchitectMattock extends ArchitectTool {
public ArchitectMattock() {
super("mattock");

addPart(ArchitectPart.AXE_HEAD, "mattock/head");
addPart(ArchitectPart.AXE_HEAD, "mattock/head", "mattock/broken_head");
addPart(ArchitectPart.SHOVEL_HEAD, "mattock/back");
addPart(ArchitectPart.TOOL_ROD, "mattock/handle").renderPriority(1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class ArchitectPickaxe extends ArchitectTool {

public ArchitectPickaxe() {
super("pickaxe");
addPart(ArchitectPart.PICKAXE_HEAD, "pickaxe/head");
addPart(ArchitectPart.PICKAXE_HEAD, "pickaxe/head", "pickaxe/broken_head");
addPart(ArchitectPart.TOOL_BINDING, "pickaxe/binding").renderPriority(-1);
addPart(ArchitectPart.TOOL_ROD, "pickaxe/handle");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class ArchitectShortsword extends ArchitectTool {
public ArchitectShortsword() {
super("shortsword");

addPart(ArchitectPart.SWORD_BLADE, "shortsword/blade");
addPart(ArchitectPart.SWORD_BLADE, "shortsword/blade", "shortsword/broken_blade");
addPart(ArchitectPart.SWORD_GUARD, "shortsword/guard");
addPart(ArchitectPart.TOOL_ROD, "shortsword/handle").renderPriority(1);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class ArchitectShovel extends ArchitectTool {

public ArchitectShovel() {
super("shovel");
addPart(ArchitectPart.SHOVEL_HEAD, "shovel/head");
addPart(ArchitectPart.SHOVEL_HEAD, "shovel/head", "shovel/broken_head");
addPart(ArchitectPart.TOOL_ROD, "shovel/handle");
addPart(ArchitectPart.TOOL_BINDING, "shovel/binding");

Expand Down
15 changes: 13 additions & 2 deletions src/main/java/com/github/kill05/items/tool/ArchitectTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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 @@ -92,6 +93,7 @@ public int getMaxDamage() {

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

Expand All @@ -105,6 +107,7 @@ public boolean canHarvestBlock(ItemStack itemStack, Block block) {
}

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

Expand Down Expand Up @@ -193,12 +196,20 @@ protected ToolPartInfo addPart(ToolPartInfo part) {
return part;
}

protected ToolPartInfo addPart(ArchitectPart part, PartType type, String texture, String brokenTexture) {
return addPart(new ToolPartInfo(part, type, texture, brokenTexture));
}

protected ToolPartInfo addPart(ArchitectPart part, PartType type, String texture) {
return addPart(new ToolPartInfo(part, type, texture));
return addPart(part, type, texture, texture);
}

protected ToolPartInfo addPart(ArchitectPart part, String texture, String brokenTexture) {
return addPart(part, part.getValidTypes().get(0), texture, brokenTexture);
}

protected ToolPartInfo addPart(ArchitectPart part, String texture) {
return addPart(part, part.getValidTypes().get(0), texture);
return addPart(part, texture, texture);
}

@SafeVarargs
Expand Down
10 changes: 8 additions & 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,6 +5,7 @@
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 All @@ -15,6 +16,7 @@ public class ToolPartInfo {
private final ArchitectPart part;
private final PartType type;
private final String texture;
private final String brokenTexture;
private int renderPriority;
private int renderOffsetX;
private int renderOffsetY;
Expand All @@ -23,17 +25,21 @@ public class ToolPartInfo {
private int index;
private int partIndex;

public ToolPartInfo(ArchitectPart part, PartType type, String texture) {
public ToolPartInfo(ArchitectPart part, PartType type, String texture, String brokenTexture) {
if(!part.getValidTypes().contains(type))
throw new IllegalArgumentException(String.format("Invalid type '%s' for part '%s'.", type, part.id));

this.part = part;
this.type = type;
this.texture = texture;
this.brokenTexture = brokenTexture;
this.index = -1;
this.partIndex = -1;
}

public ToolPartInfo(ArchitectPart part, PartType type, String texture) {
this(part, type, texture, texture);
}

public void setTool(ArchitectTool tool) {
if(this.tool != null) throw new IllegalStateException("Part already has a tool!");
Expand All @@ -51,7 +57,7 @@ public void setTool(ArchitectTool tool) {

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

public ArchitectPart part() {
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/com/github/kill05/mixins/ItemModelStandardMixin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Redirect;

@Mixin(
value = ItemModelStandard.class,
remap = false
)
public 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();
}
}
25 changes: 18 additions & 7 deletions src/main/java/com/github/kill05/mixins/ItemStackMixin.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
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 All @@ -10,6 +11,7 @@
import org.spongepowered.asm.mixin.Unique;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

//todo: remove once 7.2_pre2 comes out
Expand All @@ -19,16 +21,13 @@ public abstract class ItemStackMixin {
@Shadow
public abstract Item getItem();

@Shadow
public int itemID;

@Inject(
method = "canHarvestBlock",
at = @At(value = "HEAD"),
cancellable = true
)
public void canHarvestBlockMixin(Block block, CallbackInfoReturnable<Boolean> cir) {
if(getItem() instanceof ArchitectTool tool) {
if (getItem() instanceof ArchitectTool tool) {
cir.setReturnValue(tool.canHarvestBlock(getThis(), block));
}
}
Expand All @@ -39,7 +38,7 @@ public void canHarvestBlockMixin(Block block, CallbackInfoReturnable<Boolean> ci
cancellable = true
)
public void getMaxDamageMixin(CallbackInfoReturnable<Integer> cir) {
if(getItem() instanceof ArchitectTool tool) {
if (getItem() instanceof ArchitectTool tool) {
cir.setReturnValue(tool.getMaxDamage(getThis()));
}
}
Expand All @@ -50,7 +49,19 @@ public void getMaxDamageMixin(CallbackInfoReturnable<Integer> cir) {
cancellable = true
)
public void isItemStackDamageableMixin(CallbackInfoReturnable<Boolean> cir) {
if(getItem() instanceof ArchitectTool) cir.setReturnValue(true);
if (getItem() instanceof ArchitectTool && !ItemUtils.isBroken(getThis())) {
cir.setReturnValue(true);
}
}

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

@Inject(
Expand All @@ -59,7 +70,7 @@ public void isItemStackDamageableMixin(CallbackInfoReturnable<Boolean> cir) {
cancellable = true
)
public void getDamageVsEntityMixin(Entity entity, CallbackInfoReturnable<Integer> cir) {
if(getItem() instanceof ArchitectTool item) {
if (getItem() instanceof ArchitectTool item) {
cir.setReturnValue(item.getDamageVsEntity(entity, getThis()));
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/github/kill05/utils/ItemUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,10 @@ 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();
}

}
1 change: 1 addition & 0 deletions src/main/resources/mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
"plugin": "com.github.kill05.MixinConfigPlugin",
"mixins": [
"CreativeEntryMixin",
"ItemModelStandardMixin",
"ItemStackMixin",
"StringTagMixin",
"disablevanilla.ItemToolMixin",
Expand Down

0 comments on commit e7f3108

Please sign in to comment.