[Integration] Get backpack and backpack content?
Insane96 opened this issue · 8 comments
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
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.
Wait, am I missing something? IBackpack implements getData()
but BackpackDataItems doesn't. It implements getItems(world, player)
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);
The problem is that with reflection I can't return a type from your mod unless I set it as dependance
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 aBackpackDataItems
by comparing the type name. - Call
getItems(world, player)
using reflection on the object returned bygetData()
.
It's similar to what I do in IntermodUtils
.