From f36be35e30ae227eb0cd48e2471f6347fbafe290 Mon Sep 17 00:00:00 2001 From: Algent Date: Mon, 26 Oct 2026 23:42:34 +0100 Subject: [PATCH] Fix part swap stats using rebuilt tool diff (#42) --- dependencies.gradle | 3 +- .../IguanaTweaksTConstruct.java | 2 +- .../OldToolConversionHandler.java | 23 +- .../ToolUpdateRecipe.java | 9 +- .../reference/Config.java | 6 - .../replacing/IguanaToolPartReplacing.java | 9 - .../replacing/ModPartReplacement.java | 31 +- .../replacing/ReplacementLogic.java | 376 +++++++++--------- 8 files changed, 224 insertions(+), 235 deletions(-) diff --git a/dependencies.gradle b/dependencies.gradle index f23c610..f0acd8e 100644 --- a/dependencies.gradle +++ b/dependencies.gradle @@ -1,7 +1,7 @@ // Add your dependencies here dependencies { - api("com.github.GTNewHorizons:TinkersConstruct:1.14.91-GTNH:dev") + api("com.github.GTNewHorizons:TinkersConstruct:1.14.121-GTNH:dev") api("com.github.GTNewHorizons:NotEnoughItems:2.8.102-GTNH:dev") compileOnly("com.github.GTNewHorizons:TinkersGregworks:GTNH-1.0.35:dev") compileOnly("com.github.GTNewHorizons:Railcraft:9.17.26:dev") @@ -13,7 +13,6 @@ dependencies { } compileOnly rfg.deobf("curse.maven:biomes-o-plenty-220318:2499612") - compileOnly rfg.deobf("curse.maven:extra-utilities-225561:2264384") compileOnly("thaumcraft:Thaumcraft:1.7.10-4.2.3.5:dev") { transitive = false diff --git a/src/main/java/iguanaman/iguanatweakstconstruct/IguanaTweaksTConstruct.java b/src/main/java/iguanaman/iguanatweakstconstruct/IguanaTweaksTConstruct.java index b3b49d9..34554a2 100644 --- a/src/main/java/iguanaman/iguanatweakstconstruct/IguanaTweaksTConstruct.java +++ b/src/main/java/iguanaman/iguanatweakstconstruct/IguanaTweaksTConstruct.java @@ -45,7 +45,7 @@ import mantle.pulsar.pulse.PulseMeta; name = Reference.MOD_NAME, version = "GRADLETOKEN_VERSION", dependencies = "required-after:" + Reference.TCON_MOD_ID - + "@[1.7.10-1.8.3,);after:ForgeMultipart@[1.1.1.321,);after:*") + + "@[1.14.121-GTNH,);after:ForgeMultipart@[1.1.1.321,);after:*") public class IguanaTweaksTConstruct { // The instance of your mod that Forge uses. diff --git a/src/main/java/iguanaman/iguanatweakstconstruct/OldToolConversionHandler.java b/src/main/java/iguanaman/iguanatweakstconstruct/OldToolConversionHandler.java index 1688986..29c8bde 100644 --- a/src/main/java/iguanaman/iguanatweakstconstruct/OldToolConversionHandler.java +++ b/src/main/java/iguanaman/iguanatweakstconstruct/OldToolConversionHandler.java @@ -148,18 +148,25 @@ public class OldToolConversionHandler { // reduction int oldXpPenality = Config.partReplacementXpPenality; int oldBoostXpPenality = Config.partReplacementBoostXpPenality; + boolean oldRemoveMobHead = Config.removeMobHeadOnPartReplacement; Config.partReplacementXpPenality = 0; Config.partReplacementBoostXpPenality = 0; + // same head, so the mob head stays too + Config.removeMobHeadOnPartReplacement = false; - ReplacementLogic.exchangeToolPart(tool, tags, ReplacementLogic.PartTypes.HEAD, newHead, itemStack); - // re-add gem boost, since it's not an actual part replacement - if (gemBoost > 0) { - tags.setInteger("GemBoost", gemBoost); - tags.setInteger("HarvestLevel", tags.getInteger("HarvestLevel") + gemBoost); + try { + ReplacementLogic.exchangeToolPart(tool, tags, ReplacementLogic.PartTypes.HEAD, newHead, itemStack); + // re-add gem boost, since it's not an actual part replacement + // still there if the tool couldn't be rebuilt, nothing was changed then + if (gemBoost > 0 && !tags.hasKey("GemBoost")) { + tags.setInteger("GemBoost", gemBoost); + tags.setInteger("HarvestLevel", tags.getInteger("HarvestLevel") + gemBoost); + } + } finally { + Config.partReplacementXpPenality = oldXpPenality; + Config.partReplacementBoostXpPenality = oldBoostXpPenality; + Config.removeMobHeadOnPartReplacement = oldRemoveMobHead; } - - Config.partReplacementXpPenality = oldXpPenality; - Config.partReplacementBoostXpPenality = oldBoostXpPenality; Log.debug("Updated Tool " + itemStack.getDisplayName()); } diff --git a/src/main/java/iguanaman/iguanatweakstconstruct/ToolUpdateRecipe.java b/src/main/java/iguanaman/iguanatweakstconstruct/ToolUpdateRecipe.java index 17d585e..3dc8023 100644 --- a/src/main/java/iguanaman/iguanatweakstconstruct/ToolUpdateRecipe.java +++ b/src/main/java/iguanaman/iguanatweakstconstruct/ToolUpdateRecipe.java @@ -29,11 +29,10 @@ public class ToolUpdateRecipe implements IRecipe { // empty slot if (slot == null) continue; - // is it the tool? - if (slot.getItem() instanceof ToolCore) { - if (tool != null) return false; - tool = slot; - } + // only the tool is allowed, anything else would be consumed + if (!(slot.getItem() instanceof ToolCore)) return false; + if (tool != null) return false; + tool = slot; } // no tool found? if (tool == null) return false; diff --git a/src/main/java/iguanaman/iguanatweakstconstruct/reference/Config.java b/src/main/java/iguanaman/iguanatweakstconstruct/reference/Config.java index 37f84f4..871c705 100644 --- a/src/main/java/iguanaman/iguanatweakstconstruct/reference/Config.java +++ b/src/main/java/iguanaman/iguanatweakstconstruct/reference/Config.java @@ -57,7 +57,6 @@ public class Config { public static boolean removeMobHeadOnPartReplacement; public static int partReplacementXpPenality; public static int partReplacementBoostXpPenality; - public static boolean partReplacementHandleMagicalWood; // heads public static int baseHeadDropChance; @@ -330,11 +329,6 @@ public class Config { 0, 100, "How much of the current XP% to the next mining level shall be removed when replacing the pickaxe head. Useful to remove the mining level boost on part replacement."); - partReplacementHandleMagicalWood = configfile.getBoolean( - "HandleMagicalWood", - CATEGORY_PartReplacement, - true, - "If the bonus modifiers from magical wood parts should be properly removed or added from tool part swapping. If a uniform magical wood tool no longer becomes uniform, it will lose the +8/+11 bonus modifiers and vice versa."); /** MobHeads **/ configfile.setCategoryComment(CATEGORY_Heads, "Mob Head Module: Adds additional Mob heads and drops"); diff --git a/src/main/java/iguanaman/iguanatweakstconstruct/replacing/IguanaToolPartReplacing.java b/src/main/java/iguanaman/iguanatweakstconstruct/replacing/IguanaToolPartReplacing.java index b9d73f1..660ad5c 100644 --- a/src/main/java/iguanaman/iguanatweakstconstruct/replacing/IguanaToolPartReplacing.java +++ b/src/main/java/iguanaman/iguanatweakstconstruct/replacing/IguanaToolPartReplacing.java @@ -2,10 +2,8 @@ package iguanaman.iguanatweakstconstruct.replacing; import net.minecraftforge.common.MinecraftForge; -import cpw.mods.fml.common.Loader; import cpw.mods.fml.common.event.FMLInitializationEvent; import cpw.mods.fml.common.event.FMLPostInitializationEvent; -import cpw.mods.fml.common.event.FMLPreInitializationEvent; import iguanaman.iguanatweakstconstruct.reference.Reference; import mantle.pulsar.pulse.Handler; import mantle.pulsar.pulse.Pulse; @@ -17,13 +15,6 @@ import tconstruct.library.crafting.ModifyBuilder; @Pulse(id = Reference.PULSE_REPLACING, description = "Replace parts of tools") public class IguanaToolPartReplacing { - public static boolean extraUtilsLoaded; - - @Handler - public void preInit(FMLPreInitializationEvent event) { - extraUtilsLoaded = Loader.isModLoaded("ExtraUtilities"); - } - @Handler public void init(FMLInitializationEvent event) { ModifyBuilder.registerModifier(new ModPartReplacement()); diff --git a/src/main/java/iguanaman/iguanatweakstconstruct/replacing/ModPartReplacement.java b/src/main/java/iguanaman/iguanatweakstconstruct/replacing/ModPartReplacement.java index c9d9701..a5dbb64 100644 --- a/src/main/java/iguanaman/iguanatweakstconstruct/replacing/ModPartReplacement.java +++ b/src/main/java/iguanaman/iguanatweakstconstruct/replacing/ModPartReplacement.java @@ -7,9 +7,11 @@ import static iguanaman.iguanatweakstconstruct.replacing.ReplacementLogic.PartTy import static iguanaman.iguanatweakstconstruct.replacing.ReplacementLogic.PartTypes.HEAD; import static iguanaman.iguanatweakstconstruct.replacing.ReplacementLogic.detectAdditionalPartType; import static iguanaman.iguanatweakstconstruct.replacing.ReplacementLogic.exchangeToolPart; -import static iguanaman.iguanatweakstconstruct.replacing.ReplacementLogic.getResultingBoltModifierCount; +import static iguanaman.iguanatweakstconstruct.replacing.ReplacementLogic.getBuildPartType; import static iguanaman.iguanatweakstconstruct.replacing.ReplacementLogic.getResultingModifierCount; import static iguanaman.iguanatweakstconstruct.replacing.ReplacementLogic.getToolPartMaterial; +import static iguanaman.iguanatweakstconstruct.replacing.ReplacementLogic.hasSameParts; +import static iguanaman.iguanatweakstconstruct.replacing.ReplacementLogic.rebuildTool; import net.minecraft.init.Items; import net.minecraft.item.Item; @@ -119,7 +121,6 @@ public class ModPartReplacement extends ItemModifier { // determine materials int newMatId = ToolBuilder.instance.getMaterialID(parts[partIndex]); int oldMatId = getToolPartMaterial(tags, partType); - int modifiers = tags.getInteger("Modifiers"); // detect possible secondary position for part (e.g. hammer has 2 plates, etc.) // index 0 = handle @@ -140,20 +141,34 @@ public class ModPartReplacement extends ItemModifier { boolean sameMaterial = newMatId == oldMatId; - // do we have enough modifiers left if we exchange this part? // Special case for bolts, which have two materials. if (tool == TinkerWeaponry.boltAmmo && replacementPartItem instanceof DualMaterialToolPart) { int newHeadId = ((DualMaterialToolPart) replacementPartItem).getMaterialID2(parts[partIndex]); int oldHeadId = getToolPartMaterial(tags, HEAD); - modifiers = getResultingBoltModifierCount(tool, modifiers, tags, oldHeadId, newHeadId, oldMatId, newMatId); sameMaterial = sameMaterial && newHeadId == oldHeadId; - } else { - modifiers = getResultingModifierCount(tool, modifiers, tags, oldMatId, newMatId, partType); } - if (modifiers < 0) return false; + if (sameMaterial) return false; - return !sameMaterial; + // can the tool be built with the new part, and do we have enough modifiers left if we exchange it? + PartTypes buildPartType = getBuildPartType(tool, partType); + ItemStack[] rebuilt = rebuildTool(tool, tags, buildPartType, parts[partIndex]); + if (rebuilt == null) return false; + + NBTTagCompound originalTags = rebuilt[0].getTagCompound().getCompoundTag("InfiTool"); + NBTTagCompound newTags = rebuilt[1].getTagCompound().getCompoundTag("InfiTool"); + // vanilla items like sticks only turn into their material once built + if (hasSameParts(originalTags, newTags)) return false; + + int modifiers = getResultingModifierCount(tags, originalTags, newTags); + + // replacing the head gives back the modifier used by the mob head boost + if (buildPartType == HEAD && Config.removeMobHeadOnPartReplacement + && Config.mobHeadRequiresModifier + && tags.hasKey("Mining Level Boost")) + modifiers++; + + return modifiers >= 0; } @Override diff --git a/src/main/java/iguanaman/iguanatweakstconstruct/replacing/ReplacementLogic.java b/src/main/java/iguanaman/iguanatweakstconstruct/replacing/ReplacementLogic.java index feb1959..e56d82e 100644 --- a/src/main/java/iguanaman/iguanatweakstconstruct/replacing/ReplacementLogic.java +++ b/src/main/java/iguanaman/iguanatweakstconstruct/replacing/ReplacementLogic.java @@ -6,19 +6,14 @@ import static iguanaman.iguanatweakstconstruct.replacing.ReplacementLogic.PartTy import static iguanaman.iguanatweakstconstruct.replacing.ReplacementLogic.PartTypes.HEAD; import java.util.Arrays; -import java.util.EnumMap; -import java.util.Map.Entry; import net.minecraft.init.Items; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NBTTagCompound; -import com.rwtema.extrautils.ExtraUtils; - import iguanaman.iguanatweakstconstruct.IguanaTweaksTConstruct; import iguanaman.iguanatweakstconstruct.leveling.LevelingLogic; -import iguanaman.iguanatweakstconstruct.leveling.modifiers.ModXpAwareRedstone; import iguanaman.iguanatweakstconstruct.reference.Config; import iguanaman.iguanatweakstconstruct.reference.Reference; import iguanaman.iguanatweakstconstruct.util.HarvestLevels; @@ -33,9 +28,8 @@ import tconstruct.library.tools.DualMaterialToolPart; import tconstruct.library.tools.ToolCore; import tconstruct.library.weaponry.IAmmo; import tconstruct.modifiers.tools.ModAttack; -import tconstruct.modifiers.tools.ModRedstone; +import tconstruct.modifiers.tools.ModWindup; import tconstruct.tools.TinkerTools; -import tconstruct.tools.TinkerTools.MaterialID; import tconstruct.util.config.PHConstruct; import tconstruct.weaponry.TinkerWeaponry; @@ -45,52 +39,14 @@ public final class ReplacementLogic { public static void exchangeToolPart(ToolCore tool, NBTTagCompound tags, PartTypes type, ItemStack partStack, ItemStack toolStack) { - // create a new tool that'd be the old one with the new part exchanged - ItemStack headStack = null; - ItemStack handleStack = null; - ItemStack accessoryStack = null; - ItemStack extraStack = null; - - if (getPart(tool, HEAD) != null) - headStack = new ItemStack(getPart(tool, HEAD), 1, getToolPartMaterial(tags, HEAD)); - if (getPart(tool, PartTypes.HANDLE) != null) - handleStack = new ItemStack(getPart(tool, HANDLE), 1, getToolPartMaterial(tags, HANDLE)); - if (getPart(tool, ACCESSORY) != null) - accessoryStack = new ItemStack(getPart(tool, ACCESSORY), 1, getToolPartMaterial(tags, ACCESSORY)); - if (getPart(tool, EXTRA) != null) - extraStack = new ItemStack(getPart(tool, EXTRA), 1, getToolPartMaterial(tags, EXTRA)); - - // extra hack for bolt crafting.. - if (tool == TinkerWeaponry.boltAmmo) { - headStack = DualMaterialToolPart.createDualMaterial( - TinkerWeaponry.partBolt, - getToolPartMaterial(tags, HANDLE), - getToolPartMaterial(tags, HEAD)); - handleStack = accessoryStack; - accessoryStack = null; - if (type == ACCESSORY) type = HANDLE; - else type = HEAD; - } - - ItemStack originalTool = ToolBuilder.instance - .buildTool(headStack, handleStack, accessoryStack, extraStack, "Original Tool"); - if (originalTool == null) { - Log.error("Tool to modify is impossible?"); + type = getBuildPartType(tool, type); + ItemStack[] rebuilt = rebuildTool(tool, tags, type, partStack); + if (rebuilt == null) { + Log.warn("Couldn't rebuild " + toolStack.getDisplayName() + " to replace its part, nothing was changed."); return; } - - if (type == HEAD && headStack != null) headStack = partStack; - if (type == HANDLE && handleStack != null) handleStack = partStack; - if (type == ACCESSORY && accessoryStack != null) accessoryStack = partStack; - if (type == EXTRA && extraStack != null) extraStack = partStack; - - ItemStack newTool = ToolBuilder.instance - .buildTool(headStack, handleStack, accessoryStack, extraStack, "Modified Tool"); - if (newTool == null) { - Log.debug("External source prevents creation of tool with replaced parts."); - return; - } - NBTTagCompound newTags = newTool.getTagCompound().getCompoundTag("InfiTool"); + NBTTagCompound originalTags = rebuilt[0].getTagCompound().getCompoundTag("InfiTool"); + NBTTagCompound newTags = rebuilt[1].getTagCompound().getCompoundTag("InfiTool"); int partMaterialId = getToolPartMaterial(newTags, type); int oldMaterialId = getToolPartMaterial(tags, type); @@ -109,25 +65,9 @@ public final class ReplacementLogic { long oldRequiredXp = LevelingLogic.getRequiredXp(toolStack, tags); long oldRequiredBoostXp = LevelingLogic.getRequiredBoostXp(toolStack); - int modifiers = tags.getInteger("Modifiers"); - // special case for bolts - if (tool == TinkerWeaponry.boltAmmo && partStack.getItem() instanceof DualMaterialToolPart) { - int oldHandleId = getToolPartMaterial(tags, HANDLE); - int newHandleId = ((DualMaterialToolPart) partStack.getItem()).getMaterialID(partStack); - - modifiers = getResultingBoltModifierCount( - tool, - modifiers, - tags, - oldMaterialId, - partMaterialId, - oldHandleId, - newHandleId); - } else { - modifiers = getResultingModifierCount(tool, modifiers, tags, oldMaterialId, partMaterialId, type); - } - // Shouldn't happen, but just in case - tags.setInteger("Modifiers", Math.max(modifiers, 0)); + // can be negative until the mob head refund below. Not clamped, ModifyBuilder rejects the swap if still + // negative, e.g. when TinkersGregworks' looser check let it through + tags.setInteger("Modifiers", getResultingModifierCount(tags, originalTags, newTags)); // update part materials and rendering updateTag(newTags, tags, "Head"); @@ -139,12 +79,17 @@ public final class ReplacementLogic { updateTag(newTags, tags, "Extra"); updateTag(newTags, tags, "RenderExtra"); + // max ammo comes from the durability, check it before updating + boolean fullAmmo = tool instanceof IAmmo + && ((IAmmo) tool).getAmmoCount(toolStack) >= ((IAmmo) tool).getMaxAmmo(toolStack); + // update durability int base = newTags.getInteger("BaseDurability"); int bonus = tags.getInteger("BonusDurability"); // not changed through material, modifier float modDur = tags.getFloat("ModDurability"); // not changed through material, modifier - int total = Math.max((int) ((base + bonus) * (modDur + 1f)), 1); + // long so maxed out durabilities don't wrap around, float multiply like tcon's ModDurability + int total = Math.max(ToolBuilder.clampDurability((long) (((long) base + bonus) * (modDur + 1f))), 1); tags.setInteger("BaseDurability", base); tags.setInteger("TotalDurability", total); @@ -152,11 +97,11 @@ public final class ReplacementLogic { updateTag(newTags, tags, "Attack"); updateTag(newTags, tags, "BaseAttack"); - // update mining speed - updateTag(newTags, tags, "MiningSpeed"); - updateTag(newTags, tags, "MiningSpeed2"); - updateTag(newTags, tags, "MiningSpeedHandle"); - updateTag(newTags, tags, "MiningSpeedExtra"); + // update mining speed, keeping what was added after crafting (redstone, silk touch) + updateMiningSpeed(tool, tags, originalTags, newTags, "MiningSpeed", true); + updateMiningSpeed(tool, tags, originalTags, newTags, "MiningSpeed2", true); + updateMiningSpeed(tool, tags, originalTags, newTags, "MiningSpeedHandle", false); + updateMiningSpeed(tool, tags, originalTags, newTags, "MiningSpeedExtra", false); // update harvest level (boosting/xp will be applied later) updateTag(newTags, tags, "HarvestLevel"); @@ -172,49 +117,53 @@ public final class ReplacementLogic { updateTag(newTags, tags, "BreakChance"); updateTag(newTags, tags, "Accuracy"); - // stonebound. Shoddy is always present and never changed, we can simply update it. - updateTag(newTags, tags, "Shoddy"); + // stonebound/jagged. Leveling bonuses add to the material value, so keep what was added after crafting + float shoddy = tags.getFloat("Shoddy") + newTags.getFloat("Shoddy") - originalTags.getFloat("Shoddy"); + // float rounding can leave a tiny value instead of 0, which would block jagged/stonebound level up bonuses + if (Math.abs(shoddy) < 0.0001f) shoddy = 0f; + tags.setFloat("Shoddy", shoddy); - // update ammo + // update ammo, full ammo stays full like an undamaged tool keeps full durability if (tool instanceof IAmmo) { IAmmo ammo = (IAmmo) tool; - if (ammo.getAmmoCount(toolStack) > ammo.getMaxAmmo(toolStack)) + if (fullAmmo || ammo.getAmmoCount(toolStack) > ammo.getMaxAmmo(toolStack)) ammo.setAmmo(ammo.getMaxAmmo(toolStack), toolStack); } // reinforced is kinda complicated, since the actual level you get out of the materials is complicated // simply calculate difference between current and newly built tool to know how much has been added afterwards int currentReinforced = tags.getInteger("Unbreaking"); - int oldReinforced = originalTool.getTagCompound().getCompoundTag("InfiTool").getInteger("Unbreaking"); + int oldReinforced = originalTags.getInteger("Unbreaking"); int newReinforced = newTags.getInteger("Unbreaking"); newReinforced += currentReinforced - oldReinforced; tags.setInteger("Unbreaking", newReinforced); // reinforced tooltip is handled internally by tcon automagically - // fiery blaze arrows has to be handled separately.. meh - if (tool == TinkerWeaponry.arrowAmmo && type == HANDLE) if (oldMaterialId == 3 && partMaterialId != 3) { - // remove fiery - if (tags.getInteger("Fiery") > 5) tags.setInteger("Fiery", tags.getInteger("Fiery") - 5); - else tags.removeTag("Fiery"); - } else if (partMaterialId == 3 && oldMaterialId != 3) { - // add fiery - tags.setInteger("Fiery", 5); - } + // fiery from blaze arrow shafts, keep what the blaze modifier added after crafting + int fiery = tags.getInteger("Fiery") + newTags.getInteger("Fiery") - originalTags.getInteger("Fiery"); + if (fiery > 0) tags.setInteger("Fiery", fiery); + else tags.removeTag("Fiery"); // material tooltips are handled by tcon internally - // redstone modifier - reapplyRedstone(tags, toolStack); + // redstone modifier on bows + reapplyWindup(tags, toolStack); // quartz/attack modifier reapplyAttack(tags, toolStack); // silktouch reapplySilktouch(tags, toolStack); - if (Config.removeMobHeadOnPartReplacement && type == HEAD) removeMobHeadModifier(tags); + boolean mobHeadRemoved = Config.removeMobHeadOnPartReplacement && type == HEAD + && tags.hasKey("Mining Level Boost"); + if (mobHeadRemoved) removeMobHeadModifier(tags); - // if boosted, remove boost tag - // you're removing the head that had the reinforced tips or w/e after all - if (tags.hasKey("GemBoost")) tags.removeTag("GemBoost"); + if (tags.hasKey("GemBoost")) { + // if boosted, remove boost tag + // you're removing the head that had the reinforced tips or w/e after all + if (type == HEAD) tags.removeTag("GemBoost"); + // the harvest level only comes from the head, so reapply the boost on top of the rebuilt one + else tags.setInteger("HarvestLevel", tags.getInteger("HarvestLevel") + tags.getInteger("GemBoost")); + } // if boosted by vanilla diamond modifier, then we have to respect that if vanilla tcon allows harvestlevel // change @@ -251,26 +200,30 @@ public final class ReplacementLogic { } // handle boost leveling/xp - if (LevelingLogic.hasBoostXp(tags) && Config.levelingPickaxeBoost) { - float newRequiredBoostXp = LevelingLogic.getRequiredBoostXp(toolStack); - float xp = LevelingLogic.getBoostXp(tags); + if (LevelingLogic.hasBoostXp(tags)) { + if (Config.levelingPickaxeBoost) { + float newRequiredBoostXp = LevelingLogic.getRequiredBoostXp(toolStack); + float xp = LevelingLogic.getBoostXp(tags); - // apply transition - xp *= newRequiredBoostXp / (float) oldRequiredBoostXp; + // apply transition + xp *= newRequiredBoostXp / (float) oldRequiredBoostXp; - // xp penality? - if (Config.partReplacementBoostXpPenality > 0 && type == HEAD) { - if (LevelingLogic.isBoosted(tags)) { - tags.setBoolean(LevelingLogic.TAG_IS_BOOSTED, false); - xp = newRequiredBoostXp; + // xp penality? + if (Config.partReplacementBoostXpPenality > 0 && type == HEAD) { + if (LevelingLogic.isBoosted(tags)) { + tags.setBoolean(LevelingLogic.TAG_IS_BOOSTED, false); + xp = newRequiredBoostXp; + } + + xp *= (100.0f - Config.partReplacementBoostXpPenality) / 100.0f; } - xp *= (100.0f - Config.partReplacementBoostXpPenality) / 100.0f; + tags.setLong(LevelingLogic.TAG_BOOST_EXP, Math.round(xp)); } + // without boost xp the boost can only come from the mob head, so it goes with it and can be reapplied + else if (mobHeadRemoved) tags.setBoolean(LevelingLogic.TAG_IS_BOOSTED, false); - tags.setLong(LevelingLogic.TAG_BOOST_EXP, Math.round(xp)); - - // already full xp? + // already full xp, or boosted by a mob head? if (LevelingLogic.isBoosted(tags)) tags.setInteger("HarvestLevel", tags.getInteger("HarvestLevel") + 1); } // The original tool did not have mining XP boost. Check if the new part should have it. @@ -294,22 +247,55 @@ public final class ReplacementLogic { // update tag if it already exists. // this saves us lots of hasFoo code,since there should be no general situation where a tag doesn't exist on the // target + // tags the rebuilt tool doesn't have are removed, like the DrawSpeed older versions added to non-bows. + // copying them would store null, which crashes when the tool is copied or saved private static void updateTag(NBTTagCompound from, NBTTagCompound to, String tag) { - if (to.hasKey(tag)) to.setTag(tag, from.getTag(tag)); + if (!to.hasKey(tag)) return; + if (from.hasKey(tag)) to.setTag(tag, from.getTag(tag)); + else to.removeTag(tag); } - // strips the redstone modifier off a tool and reapplies it (to get the correct mining level modification) - private static void reapplyRedstone(NBTTagCompound tags, ItemStack itemStack) { + // keeps what was added to a mining speed after crafting + // redstone's bonus depends on how much was added at once, so it can't be reapplied exactly + private static void updateMiningSpeed(ToolCore tool, NBTTagCompound tags, NBTTagCompound originalTags, + NBTTagCompound newTags, String key, boolean silkTouchAffected) { + if (!tags.hasKey(key)) return; + + int added = tags.getInteger(key) - originalTags.getInteger(key); + // silk touch can't go below 0, so on slow materials the redstone bonus may be partly hidden. + // estimate it as if it was added one by one instead + if (silkTouchAffected && tags.hasKey("Silk Touch") && originalTags.getInteger(key) < 300) + added = getRedstoneSpeedBonus(tool, tags) - 300; + + tags.setInteger(key, Math.max(newTags.getInteger(key) + added, 0)); + } + + // mining speed added by the redstone modifier if it was added one by one, see ModRedstone + private static int getRedstoneSpeedBonus(ToolCore tool, NBTTagCompound tags) { + if (!tags.hasKey("Redstone")) return 0; + + int partBonus = 0; + if (tool.durabilityTypeHandle() == 2) partBonus += 2; + if (tool.durabilityTypeAccessory() == 2) partBonus += 2; + if (tool.durabilityTypeExtra() == 2) partBonus += 2; + + int redstone = tags.getIntArray("Redstone")[0]; + int bonus = 0; + for (int i = 1; i <= redstone; i++) bonus += 8 + (i - 1) / 50 * 2 + partBonus; + return bonus; + } + + // strips the windup (redstone on bows) modifier off a tool and reapplies it (to get the correct draw speed) + private static void reapplyWindup(NBTTagCompound tags, ItemStack itemStack) { // only do if we actually have redstone if (!tags.hasKey("Redstone")) return; int modifiers = tags.getInteger("Modifiers"); // backup modifiers - // find the redstone modifier - for (ItemModifier mod : ModifyBuilder.instance.itemModifiers) if (mod instanceof ModRedstone) { - ModRedstone modRedstone = (ModRedstone) mod; - if (modRedstone instanceof ModXpAwareRedstone) - modRedstone = ((ModXpAwareRedstone) modRedstone).originalModifier; + // find the windup modifier, tools share the Redstone key but their speed is kept by updateMiningSpeed + for (ItemModifier mod : ModifyBuilder.instance.itemModifiers) if (mod instanceof ModWindup) { + ModWindup modWindup = (ModWindup) mod; + if (!modWindup.validType((ToolCore) itemStack.getItem())) continue; int[] keyPair = tags.getIntArray("Redstone"); // get amount of redstone applied int rLvl = keyPair[0]; @@ -322,17 +308,16 @@ public final class ReplacementLogic { tags.removeTag("ModifierTip" + tipIndex); // reapply redstone - while (rLvl-- > 0) modRedstone.modify(new ItemStack[] { new ItemStack(Items.redstone) }, itemStack); // tags - // belong - // to - // oldTool + // tags belong to oldTool + while (rLvl-- > 0) modWindup.modify(new ItemStack[] { new ItemStack(Items.redstone) }, itemStack); + break; } // restore modifiers tags.setInteger("Modifiers", modifiers); } - // same as reapplyRedstone but for Attack modifier + // same as reapplyWindup but for Attack modifier private static void reapplyAttack(NBTTagCompound tags, ItemStack itemStack) { // only do if we actually have attack modifier if (!tags.hasKey("ModAttack")) return; @@ -364,7 +349,7 @@ public final class ReplacementLogic { tags.setInteger("Modifiers", modifiers); } - // Reapplies the changes to stats done by the silktouch modifier + // Reapplies the attack change done by the silktouch modifier, mining speed is kept by updateMiningSpeed private static void reapplySilktouch(NBTTagCompound tags, ItemStack itemStack) { // only do if we actually have attack modifier if (!tags.hasKey("Silk Touch")) return; @@ -373,18 +358,6 @@ public final class ReplacementLogic { attack -= 3; if (attack < 0) attack = 0; tags.setInteger("Attack", attack); - - int miningSpeed = tags.getInteger("MiningSpeed"); - miningSpeed -= 300; - if (miningSpeed < 0) miningSpeed = 0; - tags.setInteger("MiningSpeed", miningSpeed); - - if (tags.hasKey("MiningSpeed2")) { - int miningSpeed2 = tags.getInteger("MiningSpeed2"); - miningSpeed2 -= 300; - if (miningSpeed2 < 0) miningSpeed2 = 0; - tags.setInteger("MiningSpeed2", miningSpeed2); - } } // removes the mobhead modifier and rendering @@ -413,8 +386,8 @@ public final class ReplacementLogic { } int effectId = tags.getInteger(effect); - // mobheads occupy id's 20 to 26. see IguanaToolLeveling.registerBoostModifiers - if (effectId >= 20 && effectId <= 26) { + // mobheads and the nether star occupy id's 20 to 28. see IguanaToolLeveling.registerBoostModifiers + if (effectId >= 20 && effectId <= 28) { tags.removeTag(effect); removed = true; } @@ -452,77 +425,88 @@ public final class ReplacementLogic { } /** - * Checks if the material has an extra modifier. + * Bolts are built from the dual material bolt core (head) and the fletching (handle). */ - public static boolean hasExtraModifier(int materialId) { - return materialId == TinkerTools.MaterialID.Paper || materialId == MaterialID.Thaumium; + public static PartTypes getBuildPartType(ToolCore tool, PartTypes type) { + if (tool != TinkerWeaponry.boltAmmo) return type; + return type == ACCESSORY ? HANDLE : HEAD; } /** - * Returns true if the given material ids are valid for an "all magic wood" tool + * Builds the tool from its current parts, and with the given part exchanged. Building fires the regular craft + * events, so material bonuses (paper, thaumium, magical wood, ...) are the same as on a freshly crafted tool. + * + * @param type Where the new part goes, as returned by {@link #getBuildPartType} + * @return The original and the new tool, or null if either can't be built */ - public static boolean isAllMagicWood(EnumMap map) { - if (!IguanaToolPartReplacing.extraUtilsLoaded) return false; - int magicWoodId = ExtraUtils.tcon_magical_wood_id; - for (Entry entry : map.entrySet()) { - boolean allowsMat0 = entry.getKey() == ACCESSORY || entry.getKey() == EXTRA; - int value = entry.getValue(); - boolean isValid = value == magicWoodId || (value <= 0 && allowsMat0); - if (!isValid) return false; + public static ItemStack[] rebuildTool(ToolCore tool, NBTTagCompound tags, PartTypes type, ItemStack partStack) { + ItemStack headStack = null; + ItemStack handleStack = null; + ItemStack accessoryStack = null; + ItemStack extraStack = null; + + if (getPart(tool, HEAD) != null) + headStack = new ItemStack(getPart(tool, HEAD), 1, getToolPartMaterial(tags, HEAD)); + if (getPart(tool, HANDLE) != null) + handleStack = new ItemStack(getPart(tool, HANDLE), 1, getToolPartMaterial(tags, HANDLE)); + if (getPart(tool, ACCESSORY) != null) + accessoryStack = new ItemStack(getPart(tool, ACCESSORY), 1, getToolPartMaterial(tags, ACCESSORY)); + if (getPart(tool, EXTRA) != null) + extraStack = new ItemStack(getPart(tool, EXTRA), 1, getToolPartMaterial(tags, EXTRA)); + + // extra hack for bolt crafting.. + if (tool == TinkerWeaponry.boltAmmo) { + headStack = DualMaterialToolPart.createDualMaterial( + TinkerWeaponry.partBolt, + getToolPartMaterial(tags, HANDLE), + getToolPartMaterial(tags, HEAD)); + handleStack = accessoryStack; + accessoryStack = null; } + + ItemStack originalTool = ToolBuilder.instance + .buildTool(headStack, handleStack, accessoryStack, extraStack, "Original Tool"); + if (originalTool == null) return null; + + if (type == HEAD && headStack != null) headStack = partStack; + if (type == HANDLE && handleStack != null) handleStack = partStack; + if (type == ACCESSORY && accessoryStack != null) accessoryStack = partStack; + if (type == EXTRA && extraStack != null) extraStack = partStack; + + ItemStack newTool = ToolBuilder.instance + .buildTool(headStack, handleStack, accessoryStack, extraStack, "Modified Tool"); + if (newTool == null) return null; + + return new ItemStack[] { originalTool, newTool }; + } + + /** + * Returns true if both tools are made of the same materials, e.g. a stick replacing a wooden tool rod. + */ + public static boolean hasSameParts(NBTTagCompound tags, NBTTagCompound otherTags) { + for (PartTypes type : PartTypes.values()) + if (getToolPartMaterial(tags, type) != getToolPartMaterial(otherTags, type)) return false; return true; } - public static int getResultingModifierCount(ToolCore tool, int modifiers, NBTTagCompound tags, int oldMatId, - int newMatId, PartTypes partType) { - if (oldMatId == newMatId) return modifiers; - if (hasExtraModifier(oldMatId)) modifiers--; - if (hasExtraModifier(newMatId)) modifiers++; - - // Handle magic wood modifiers - if (IguanaToolPartReplacing.extraUtilsLoaded && Config.partReplacementHandleMagicalWood) { - int magicWoodId = ExtraUtils.tcon_magical_wood_id; - int extraModifiers = tool == TinkerTools.battlesign ? 11 : 8; - EnumMap map = new EnumMap<>(PartTypes.class); - int oldToolMagicWoodCount = 0; - for (PartTypes type : PartTypes.values()) { - int partId = getToolPartMaterial(tags, type); - if (partId == magicWoodId) oldToolMagicWoodCount++; - map.put(type, partId); - } - boolean wasUniformMagicalWood = isAllMagicWood(map); // The past - map.put(partType, newMatId); - boolean isUniformMagicalWood = isAllMagicWood(map); // The present - - if (wasUniformMagicalWood == isUniformMagicalWood) { - // If it's a full magical wood tool, don't change the modifier count - if (!wasUniformMagicalWood) { - if (oldMatId == magicWoodId) { - modifiers--; - } else if (newMatId == magicWoodId) { - modifiers++; - } - } - // Head or handle changed - } else if (wasUniformMagicalWood) { - modifiers -= extraModifiers - oldToolMagicWoodCount; - if (oldMatId == magicWoodId) modifiers--; - } else { - modifiers += extraModifiers - oldToolMagicWoodCount; - } - } - return modifiers; + /** + * Checks if the material has an extra modifier. + * + * @deprecated Only covers paper and thaumium, kept because TinkersGregworks still calls it. Use + * {@link #rebuildTool} and {@link #getResultingModifierCount} instead. + */ + @Deprecated + public static boolean hasExtraModifier(int materialId) { + return materialId == TinkerTools.MaterialID.Paper || materialId == TinkerTools.MaterialID.Thaumium; } /** - * Bolts swap head and handle at once, so the handle lookup has to see the new head. + * Keeps the modifiers spent or gained since crafting, and applies the difference in crafting bonuses between the + * original and the new tool. */ - public static int getResultingBoltModifierCount(ToolCore tool, int modifiers, NBTTagCompound tags, int oldHeadId, - int newHeadId, int oldHandleId, int newHandleId) { - modifiers = getResultingModifierCount(tool, modifiers, tags, oldHeadId, newHeadId, HEAD); - NBTTagCompound swapped = (NBTTagCompound) tags.copy(); - swapped.setInteger("Head", newHeadId); - return getResultingModifierCount(tool, modifiers, swapped, oldHandleId, newHandleId, HANDLE); + public static int getResultingModifierCount(NBTTagCompound tags, NBTTagCompound originalTags, + NBTTagCompound newTags) { + return tags.getInteger("Modifiers") + newTags.getInteger("Modifiers") - originalTags.getInteger("Modifiers"); } /**