#How to make item equippable?

20 messages · Page 1 of 1 (latest)

broken estuary
#

if it's an existing item, mixin to LivingEntity$getPreferredEquipmentSlot, otherwise if adding a new one then have your item implement Equipment

broken estuary
#

don't understand the question, what are you asking?

#

if creating an equippable item, just make it so your class implements Equipment with public class yourClass implements Equipment {...}, if making an existing item equippable then the following is an example of a mixin that does so:

@Mixin(LivingEntity.class)
public class LivingEntityMixin {
    @Inject(method = "getPreferredEquipmentSlot", at = @At("HEAD"), cancellable = true)
    private static void moveToSlot(ItemStack stack, CallbackInfoReturnable<EquipmentSlot> cir) {
        if (<do your checks on the stack here>) cir.setReturnValue(<equipment slot to move to>);
    }
}
#

i don't know what exactly the problem is nor the exact mixin you're having a problem with

#

game version also makes a difference so you need to give me all of that information

#

are you using mojmap or yarn?

#

what version of the game are you modding for?

#

do you know what set of mappings you have?

#

adding this in a blank template mod, i can 100% verify that this works on 1.20.4 with the latest yarn mappings

#

oh also forgot to mention, you need to add an injector in Item$use, like so

@Mixin(Item.class)
public class ItemMixin {

    @Inject(method = "use", at = @At("HEAD"), cancellable = true)
    private void swapEquip(World world, PlayerEntity user, Hand hand, CallbackInfoReturnable<TypedActionResult<ItemStack>> cir) {
        if (<item check here>) {
            cir.setReturnValue(equipAndSwap(<your item type>, world, user, hand));
        }
    }

    @Unique private TypedActionResult<ItemStack> equipAndSwap(Item item, World world, PlayerEntity user, Hand hand) {
        ItemStack itemStack = user.getStackInHand(hand);
        EquipmentSlot equipmentSlot = MobEntity.getPreferredEquipmentSlot(itemStack);
        ItemStack itemStack2 = user.getEquippedStack(equipmentSlot);
        if (EnchantmentHelper.hasBindingCurse(itemStack2) || ItemStack.areEqual(itemStack, itemStack2)) {
            return TypedActionResult.fail(itemStack);
        }
        if (!world.isClient()) {
            user.incrementStat(Stats.USED.getOrCreateStat(item));
        }
        ItemStack itemStack3 = itemStack2.isEmpty() ? itemStack : itemStack2.copyAndEmpty();
        ItemStack itemStack4 = itemStack.copyAndEmpty();
        user.equipStack(equipmentSlot, itemStack4);
        return TypedActionResult.success(itemStack3, world.isClient());
    }

}

equipAndSwap is directly copied from the Equipment class

#

just push it to github if you can

#

alternatively go in your build.gradle and find the line that says mappings

#

oh alright

#

was it genSources?

#

oh alright, that would make sense

#

although from when i briefly looked at your repo it seems the items you were registering can just be registered as helmets to achieve the same deal

#

user.getStackInHand(hand).isOf(yourItem)

#

looking over it

#

have you added your mixins to the mixin configuration file?

#

you need this in your mod.mixins.json

  "mixins": [
    "ItemMixin",
    "LivingEntityMixin"
  ],