#How to modify vanilla items using mixin?

44 messages · Page 1 of 1 (latest)

past citrus
#

I want to modify some attributes and components of vanilla tools and weapons. How am I suppose to do that with mixin? I'm like 1 week into modding and I know very little

#

This is my code currently

@Mixin(Items.class)
public abstract class ItemsMixin {

    @Shadow
    public static final Item GOLDEN_SWORD = register("golden_sword", new Item.Settings().sword(ToolMaterial.GOLD, 5.0F, -2.4F));

}
#

1.21.8 btw

safe cave
#

not sure if you actually can change static final variables

golden galleon
#

The Shadow annotation does not allow you to change the definition* of a var. It only lets you use it

#

I'd suggest using a @Inject injecting into the register method

past citrus
#

uhmm this does not work

public abstract class ItemsMixin {
    @Shadow
    public static Item GOLDEN_SWORD;

    @Inject(method = "<clinit>", at = @At("TAIL"))
    public static void ModifyGoldenSword(CallbackInfo ci) {
        GOLDEN_SWORD = new Item.Settings().sword(ToolMaterial.GOLD, 5.0F, -2.4F);
    }
}

did I do anything wrong?

Edit: Added ModifyGoldenSword between void and (CallbackInfo ci), still not working

past citrus
left python
#

no, this is not good

#

don't reinitialize the item, this will override anything other mods would add.

#

you should do precise changes, in this case only to the item settings

#

that way for example if someone makes a sword edible, that wouldn't be lost

#

it will have both settings

past citrus
#

uhmmm

#

howd you do that?

left python
#
  1. use a @Slice to narrow down the search scope, generally you can easily target a constant value like "golden_sword"
  2. use a injector that lets you do precise changes: ModifyExpressionValue, ModifyArg
past citrus
#

can you provide some examples, because I'm still a beginner and those wiki pages don't make much sense to me?

unborn narwhal
#

why not just use the default item component event from fapi

#

you can use an accessor on ComponentMap$Builder to access its components which would let you modify them instead of overwriting (if needed)

left python
#

does it let you modify the tool material?

#

or is that not needed in OP's case?

#

wait tool material is a part of the component

past citrus
#

yes it did

#

all I did was

@Mixin(ToolMaterial.class)
public abstract class ToolMaterialMixin {

    @Shadow
    public static final ToolMaterial GOLD = new ToolMaterial(BlockTags.INCORRECT_FOR_GOLD_TOOL, 984, 8.0F, 0.0F, 22, ItemTags.GOLD_TOOL_MATERIALS);
    
}

and it worked perfectly fine

warm steeple
golden galleon
#

But it should never be used like that, so I pretend it doesn't work tiny_potato

warm steeple
#

It's quite common to do and actually quite useful

#

Saves an injector

golden galleon
#

but it's not stackable

warm steeple
#

If you use the original value it stacks just fine

past citrus
#

but when I used similar code for Items

@Mixin(Items.class)
public abstract class ItemsMixin {

    @Shadow
    public static final Item GOLDEN_SWORD = register("golden_sword", new Item.Settings().sword(ToolMaterial.GOLD, 5.0F, -2.4F));

}
#

it didn't work

unborn narwhal
#

or mixin into the register method

past citrus
unborn narwhal
#

Items.register

past citrus
#
@Mixin(Items.class)
public abstract class ItemMixin {

    @Shadow
    public static final Item GOLDEN_SWORD = Items.register("golden_sword", new Item.Settings().sword(ToolMaterial.GOLD, 9.0F, 4F));

}

still crashing

unborn narwhal
#

well thats not doing what I said

past citrus
#

so how would you do it?

unborn narwhal
#

either doing what diced said, or modifying the args passed to the other overload in the String, Item$Settings overload (which is less compatible with other injectors mixing into the same overload)

past citrus
#

I tried to read the wiki he sent and it just went over my head