CraftTweaker

CraftTweaker

151M Downloads

[1.16.5] MCEnchantment.canApply() not taking into account other enchantments on item

blockgoblin31 opened this issue · 15 comments

commented

Issue description

I have this code

    var level = random.nextInt(5, 19);
    var curLevel = 0;
    var result = <item:mekanismtools:steel_helmet>;
    while curLevel < level {
        var enchant = enchantsHelm[random.nextInt(enchantsHelm.length as int)];
        if enchant.canApply(result) {
            var enchLvl = random.nextInt(1, enchant.maxLevel);
            curLevel += enchLvl;
            if curLevel > level {
                enchLvl -= (curLevel - level);
                curLevel = level;
            }
            result = result.withEnchantment(enchant, enchLvl);
        }
    }
    return new MCMerchantOffer(<item:minecraft:emerald> * random.nextInt(13, 27), result, 3, 15, 0.2);
});

where enchantsHelm is a List of all the enchantments in the game that can apply to a steel helmet. When I reload with this code and check just enough resources to test that it worked, the first trade jer comes up with has both protection 4 and fire protection 2. (I just tested this println(<enchantment:minecraft:fire_protection>.canApply(<item:minecraft:wooden_sword>.withEnchantment(<enchantment:minecraft:protection>, 1)));, which works as expected and prints false? Am I missing something? Maybe its something with JER?

also as seen in the imgur link below the method enchant.canVillagerTrade returns true for curses, which it definitely shouldn't. Same goes for mending(also found on the item) and frost walker. Source: MC fandom wiki, specifically the villager trades page and enchanting page.

Steps to reproduce

Not sure apparently...

Script used

https://gist.github.com/tuc56/6df50b80b1323b990fac577d7a1d5358

The crafttweaker.log file

https://gist.github.com/tuc56/08c0f59ee79f5f4358cded30c1d076c8

Minecraft version

1.16

Modloader

Forge

Modloader version

36.2.39

CraftTweaker version

7.1.2.520

Other relevant information

Screenshot of the erroring item(jer apparently refreshes the trade whenever you close out and reopen the veiwer): https://i.imgur.com/5e2MW6w.png

Installed mods:

    [PneumaticCraft: Repressurized (by desht_08)](https://www.curseforge.com/minecraft/mc-mods/pneumaticcraft-repressurized)
    [Nature's Aura (by Ellpeck)](https://www.curseforge.com/minecraft/mc-mods/natures-aura)
    [Interactio - In-World Crafting with Datapacks! (by MaxNeedsSnacks)](https://www.curseforge.com/minecraft/mc-mods/interactio)
    [FTB Teams (Forge) (by FTB)](https://www.curseforge.com/minecraft/mc-mods/ftb-teams-forge)
    [ConnectedTexturesMod (by tterrag1098)](https://www.curseforge.com/minecraft/mc-mods/ctm)
    [Open Loader (by DarkhaxDev)](https://www.curseforge.com/minecraft/mc-mods/open-loader)
    [Immersive Petroleum (by Flaxbeard)](https://www.curseforge.com/minecraft/mc-mods/immersive-petroleum)
    [Mouse Tweaks (by YaLTeR)](https://www.curseforge.com/minecraft/mc-mods/mouse-tweaks)
    [ModernFix (by embeddedt)](https://www.curseforge.com/minecraft/mc-mods/modernfix)
    [FerriteCore (Forge) (by malte0811)](https://www.curseforge.com/minecraft/mc-mods/ferritecore)
    [Better End Potato Edition (by potato_____boy)](https://www.curseforge.com/minecraft/mc-mods/better-end-potato-edition)
    [AppleSkin (by squeek502)](https://www.curseforge.com/minecraft/mc-mods/appleskin)
    [Quests Additions (Forge) (by NaturaSpell)](https://www.curseforge.com/minecraft/mc-mods/quests-additions)
    [KubeJS Mekanism (by LatvianModder)](https://www.curseforge.com/minecraft/mc-mods/kubejs-mekanism)
    [The One Probe (by McJty)](https://www.curseforge.com/minecraft/mc-mods/the-one-probe)
    [FTB Library (Forge) (by FTB)](https://www.curseforge.com/minecraft/mc-mods/ftb-library-forge)
    [JER Integration (by AlasDiablo)](https://www.curseforge.com/minecraft/mc-mods/jer-integration)
    [CoFH Core (by TeamCoFH)](https://www.curseforge.com/minecraft/mc-mods/cofh-core)
    [Botania (by Vazkii)](https://www.curseforge.com/minecraft/mc-mods/botania)
    [Mekanism Additions (by bradyaidanc)](https://www.curseforge.com/minecraft/mc-mods/mekanism-additions)
    [Simple Storage Network (by Lothrazar)](https://www.curseforge.com/minecraft/mc-mods/simple-storage-network)
    [More Overlays Updated (by RiDGo8)](https://www.curseforge.com/minecraft/mc-mods/more-overlays-updated)
    [Block Swap (by Corgi_Taco)](https://www.curseforge.com/minecraft/mc-mods/block-swap)
    [Iron Chests (by ProgWML6)](https://www.curseforge.com/minecraft/mc-mods/iron-chests)
    [KubeJS Thermal (by LatvianModder)](https://www.curseforge.com/minecraft/mc-mods/kubejs-thermal)
    [Nature's Aura Tweaker (by Mrthomas20121)](https://www.curseforge.com/minecraft/mc-mods/naturesaura_tweaker)
    [Astral Sorcery (by HellFirePvP)](https://www.curseforge.com/minecraft/mc-mods/astral-sorcery)
    [ContentTweaker (by Jaredlll08)](https://www.curseforge.com/minecraft/mc-mods/contenttweaker)
    [Immersive Engineering (by BluSunrize)](https://www.curseforge.com/minecraft/mc-mods/immersive-engineering)
    [Create Deco (by talrey)](https://www.curseforge.com/minecraft/mc-mods/create-deco)
    [KubeJS Immersive Engineering (by LatvianModder)](https://www.curseforge.com/minecraft/mc-mods/kubejs-immersive-engineering)
    [Item Filters (by LatvianModder)](https://www.curseforge.com/minecraft/mc-mods/item-filters)
    [Curios API (Forge) (by TheIllusiveC4)](https://www.curseforge.com/minecraft/mc-mods/curios)
    [Applied Energistics 2 (by thetechnici4n)](https://www.curseforge.com/minecraft/mc-mods/applied-energistics-2)
    [AgriCraft (by InfinityRaider)](https://www.curseforge.com/minecraft/mc-mods/agricraft)
    [Initial Inventory (by Jaredlll08)](https://www.curseforge.com/minecraft/mc-mods/initial-inventory)
    [AutoRegLib (by Vazkii)](https://www.curseforge.com/minecraft/mc-mods/autoreglib)
    [More Dragon Eggs (by Darkere)](https://www.curseforge.com/minecraft/mc-mods/more-dragon-eggs)
    [Thermal Foundation (by TeamCoFH)](https://www.curseforge.com/minecraft/mc-mods/thermal-foundation)
    [Flywheel (by jozufozu)](https://www.curseforge.com/minecraft/mc-mods/flywheel)
    [JEITweaker (by Jaredlll08)](https://www.curseforge.com/minecraft/mc-mods/jeitweaker)
    [Create (by simibubi)](https://www.curseforge.com/minecraft/mc-mods/create)
    [Just Enough Items (JEI) (by mezz)](https://www.curseforge.com/minecraft/mc-mods/jei)
    [Valhelsia Core (by ValhelsiaTeam)](https://www.curseforge.com/minecraft/mc-mods/valhelsia-core)
    [Farmer's Delight (by vectorwing)](https://www.curseforge.com/minecraft/mc-mods/farmers-delight)
    [LootJS: KubeJS Addon (by Lytho)](https://www.curseforge.com/minecraft/mc-mods/lootjs)
    [JourneyMap (by techbrew)](https://www.curseforge.com/minecraft/mc-mods/journeymap)
    [Ex Nihilo: Sequentia (by NovaMachina)](https://www.curseforge.com/minecraft/mc-mods/ex-nihilo-sequentia)
    [InfinityLib (by InfinityRaider)](https://www.curseforge.com/minecraft/mc-mods/infinitylib)
    [CraftTweaker (by Jaredlll08)](https://www.curseforge.com/minecraft/mc-mods/crafttweaker)
    [Just Enough Calculation (by towdium)](https://www.curseforge.com/minecraft/mc-mods/just-enough-calculation)
    [Lazy DataFixerUpper(LazyDFU) [FORGE] (by Corgi_Taco)](https://www.curseforge.com/minecraft/mc-mods/lazy-dfu-forge)
    [WorldEditCUI Forge Edition 3 (by hexosse)](https://www.curseforge.com/minecraft/mc-mods/worldeditcui-forge-edition-3)
    [Construction Wand (by ThetaDev)](https://www.curseforge.com/minecraft/mc-mods/construction-wand)
    [CreateTweaker (by Jaredlll08)](https://www.curseforge.com/minecraft/mc-mods/createtweaker)
    [Patchouli (by Vazkii)](https://www.curseforge.com/minecraft/mc-mods/patchouli)
    [Architectury API (Fabric/Forge) (by shedaniel)](https://www.curseforge.com/minecraft/mc-mods/architectury-api)
    [Eating Animation [Forge] (by matyrobbrt)](https://www.curseforge.com/minecraft/mc-mods/eating-animation-forge)
    [FTB Quests (Forge) (by FTB)](https://www.curseforge.com/minecraft/mc-mods/ftb-quests-forge)
    [Quark (by Vazkii)](https://www.curseforge.com/minecraft/mc-mods/quark)
    [Mekanism Tools (by bradyaidanc)](https://www.curseforge.com/minecraft/mc-mods/mekanism-tools)
    [Starter Structure (by Serilum)](https://www.curseforge.com/minecraft/mc-mods/starter-structure)
    [ObserverLib (by HellFirePvP)](https://www.curseforge.com/minecraft/mc-mods/observerlib)
    [Thermal Cultivation (by TeamCoFH)](https://www.curseforge.com/minecraft/mc-mods/thermal-cultivation)
    [Tinkers Construct (by mDiyo)](https://www.curseforge.com/minecraft/mc-mods/tinkers-construct)
    [Clumps (by Jaredlll08)](https://www.curseforge.com/minecraft/mc-mods/clumps)
    [AppliedEnergisticsTweaker (by Jaredlll08)](https://www.curseforge.com/minecraft/mc-mods/appliedenergisticstweaker)
    [Collective (by Serilum)](https://www.curseforge.com/minecraft/mc-mods/collective)
    [Controlling (by Jaredlll08)](https://www.curseforge.com/minecraft/mc-mods/controlling)
    [Mantle (by mDiyo)](https://www.curseforge.com/minecraft/mc-mods/mantle)
    [Pretty Pipes (by Ellpeck)](https://www.curseforge.com/minecraft/mc-mods/pretty-pipes)
    [Multiblocked (by KilaBash)](https://www.curseforge.com/minecraft/mc-mods/multiblocked)
    [KubeJS (by LatvianModder)](https://www.curseforge.com/minecraft/mc-mods/kubejs)
    [TerraForged (by won_ton_)](https://www.curseforge.com/minecraft/mc-mods/terraforged)
    [KubeJS Additions (by ILIKEPIEFOO2)](https://www.curseforge.com/minecraft/mc-mods/kubejs-additions)
    [Just Enough Resources (JER) (by way2muchnoise)](https://www.curseforge.com/minecraft/mc-mods/just-enough-resources-jer)
    [LDLib (by KilaBash)](https://www.curseforge.com/minecraft/mc-mods/ldlib)
    [Neat (by Vazkii)](https://www.curseforge.com/minecraft/mc-mods/neat)
    [Custom Machinery (by frinn38)](https://www.curseforge.com/minecraft/mc-mods/custom-machinery)
    [Rubidium (by Asek3)](https://www.curseforge.com/minecraft/mc-mods/rubidium)
    [Mekanism Generators (by bradyaidanc)](https://www.curseforge.com/minecraft/mc-mods/mekanism-generators)
    [Mekanism (by bradyaidanc)](https://www.curseforge.com/minecraft/mc-mods/mekanism)
    [Ore Tweaker (by EwyBoy)](https://www.curseforge.com/minecraft/mc-mods/ore-tweaker)
    [Fast Leaf Decay (by olafskiii)](https://www.curseforge.com/minecraft/mc-mods/fast-leaf-decay)
    [Rhino (by LatvianModder)](https://www.curseforge.com/minecraft/mc-mods/rhino)
    [Refined Pipes (by raoulvdberge)](https://www.curseforge.com/minecraft/mc-mods/refined-pipes)

The latest.log file

https://gist.github.com/tuc56/ddecbcb90936dc06e55a3c883e9da1a5

commented

So I really should do a test with just crafttweaker, mekanism tools, and jer to narrow down the problem. That’ll be tonight

commented

Actually !enchant.treasure might work fine as far as I know, I was editing the wrong file…(!enchant.treasure works fine)

commented

can 100% confirm enchant.canVillagerTrade doesnt work for sorting out stuff that could come on gear, maybe it means on an enchanted book?

commented

ok it looks to me like .canVillagerTrade calls the vanilla .isTradable method, which always returns true

commented

so I'll use !enchantment.treasure instead

commented

no thats just me failing at reading code(edit: it was, but wasnt, that method is never overridden as far as I can see)

commented

that is... also not working

commented

reproduced with an actual villager btw, not just a jer thing.

commented

So this really seems to be two separate issues I discovered at the same time. The first is my for loop, with enchant.canVillagerTrade swapped for !enchant.treasure, is still letting curses and mending through, and the second is that .canApply doesnt work under certain circumstances(I cant find it in the vanilla code anywhere...)

commented

ok this println(<enchantment:minecraft:fire_protection>.canApply(<item:minecraft:diamond_helmet>.withEnchantment(<enchantment:minecraft:protection>, 1))); does return true

commented

ok I can confirm that it happens with vanilla too, exact same script as above, just substituting mekanismtools:steel with minecraft:diamond(just crafttweaker installed)

commented

I specifically got a fire protection 1, balst protection 3, unbreaking 1 helmet

commented

oh btw I need to redo that println, I just realized I was checking if you can put protection on a sword

commented

Okay so:

enchant.canApply(result)

That just checks if the enchantment can go on the item, it doesn't check for incompatible enchantments.

What you would need is code like this:

val canApplyEnchantment = (item as IItemStack, newEnchantment as MCEnchantment) => {
    var valid = true;
    for enchantment, level in item.enchantments {
        if !enchantment.isCompatibleWith(newEnchantment) {
            valid = false;
        }
    }
    return valid;
};

then replace line 39 with

 if canApplyEnchantment(result, enchant) {

Just a note, when I tested this it caused the server to freeze because of this line https://gist.github.com/tuc56/6df50b80b1323b990fac577d7a1d5358#file-villagers-zs-L37
I don't think there are enough compatible enchantments in the game to satisfy that condition, so you will need to change that.

Changing it to:

for i in 0 .. 10 {
        var enchant = enchantsHelm[random.nextInt(enchantsHelm.length as int)];
        if canApplyEnchantment(result, enchant) {
            var enchLvl = random.nextInt(1, enchant.maxLevel);
            result = result.withEnchantment(enchant, enchLvl);
        }
    }

Does show that it works as intended (at-least for compatible enchants)

As for

enchant.canVillagerTrade

This method is only overriden by the SoulSpeedEnchantment.

As far as I can tell, there is nothing stopping curses from being traded by a villager, mending 100% can be traded by a villager, that mechanic is the main way to get mending books to put on whatever item you want.

If you want to exclude those enchantments you will have to do it manually.

I hope I got all the questions answered, let me know if I missed one

commented

Issue has been confirmed as fixed via discord.