copygirl's Wearable Backpacks

copygirl's Wearable Backpacks

15M Downloads

[Integration] Get backpack and backpack content?

Insane96 opened this issue · 8 comments

commented

I'm trying to add integration to my IguanaTweaks mod. I would like the read the content of the backpack (when worn) and change the weight of the player accordingly.

Seems like that to get the backpack I can use getBackpack(Entity): https://github.com/copygirl/WearableBackpacks/blob/master/src/main/java/net/mcft/copy/backpacks/api/BackpackHelper.java#L49

commented

You might have to reference the non-API class BackpackDataItems or directly call its getItems(world, player) method via reflection on the IBackpackData return by IBackpack.getData(). (Currently only BackpackDataItems implements IBackpackData, but there was plans for different types of backpacks.)


... Note to self: There is a possible NPE when calling getItems() without the world argument when lootable backpacks have not generated their loot yet. Huh.

commented

Wait, am I missing something? IBackpack implements getData() but BackpackDataItems doesn't. It implements getItems(world, player)

commented
IBackpack backpack = BackpackHelper.getBackpack(player);
if (backpack == null) return;

IBackpackData data = backpack.getData();
if (!(data instanceof BackpackDataItems)) return;

BackpackDataItems dataItems = (BackpackDataItems)data;
ItemStackHandler items = dataItems.getItems(world, player);
commented

The problem is that with reflection I can't return a type from your mod unless I set it as dependance

commented

ItemStackHandler is a Forge thing.

commented
commented

I don't have Java stuff set up to write any actual code but there's two things you should do.

  • Call getData() and make sure it's a BackpackDataItems by comparing the type name.
  • Call getItems(world, player) using reflection on the object returned by getData().

It's similar to what I do in IntermodUtils.

commented

Closing due to lack of activity.