Jump to content

Recommended Posts

Posted

This is pretty self explanatory but i am trying to attach an inventory to one of my items. I know that i should be using capabilities for this but I'm not sure where i should put it for an item. Would it go inside of initCapabilities(), and if so how would i use that? I tried to take a look at how other mods do this but every mod seems to do it a different way and all of which require some kind of GUI(which I do not want). Of course there is the forge docs but their isn't enough documentations for doing so to items, it mostly explains for tile entities. Anything else i could find is too old to be any use with the complete API reworks basically every other version. Could someone help me with this? I was able to solve this issue last time i encountered it by simply adding the name of the stored item to the nbt but I'm afraid this wont work for this as I need to be able to have more than one slots.

Posted

You need a capability, and a capability provider.
the capability is just a class which will hold your arbitrary data, for an Inventory, you want an IItemHandler (which handles ItemStacks). forge already has a default implementation of IItemHandler called ItemStackHandler, however you can create your own for custom behaviour.

the capability provider is a class which implements ICapabilityProvider (orICapabilitySerializable<INBT> if you need save and load the data between sessions). In it's getCapability method, if the requested capability is your capability (or if it is just an inventory you can use CapabiltiyItemHandler.ITEM_HANDLER_CAPABILITY), and then return a lazy instance of it.

Posted

Ohhhhhhh, thats how you get access to those methods. I knew you used to get those in 1.12.2 but completely forgot how, that answers that! So now i simply add an IInventory to the item along with implementing the capability right?

Posted

 

public class ParcelItem extends Item implements ICapabilityProvider {
	LazyOptional<ItemStackHandler> inventoryHandler = LazyOptional.of(this);
	
	public ParcelItem(Properties properties) {
		super(properties);
	}

	@Override
	public <T> LazyOptional<T> getCapability(Capability<T> cap, Direction side) {
		if(cap == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
			return inventoryHandler.cast();
		}
		return null;
	}
}

Alright, i will make sure to use that instead, although, this is currently what I have. From the docs i understand i have to use a lazy optional and use the IItemHandler(im guessing replacing it with ItemStackHandler is fine consider it uses IItemHandler as a base). I thought i could pass the class as the supplier though but it seems i was mistaken. What exactly am i supposed to pass to it? Am i even supposed to do it like this?

Posted (edited)

Items can't be CapabilityProviders (unless you want all copies of your item to share that data). Item inventories belong to ItemStacks. Override initCapabilities.

Edited by Draco18s

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted
11 hours ago, Skelyvelocirap said:

How do i check the type of capability its returning for with this method though?

you don't.
You'll check the capability type in the provider;

And in your initCapabilities you'll return an instance of said provider. If your item needs more than one capability, just make a Provider which provides multiple capabilties depending on which one is requested.

Posted
35 minutes ago, Skelyvelocirap said:

I am really confused tbh. How exactly am I supposed to do that?

the job of the CapabilityProvider is too handle and expose all the capabilities for an object. In the getCapabilities method, it will check for which capability that is being requested, and provide it.

in the Item class, the point of the initCapabilities method, is just to say which CapabilityProvider will handle it's data.

so if what you need is an Inventory, you'd have an "InventoryProvider", which when the ITEM_STACK_HANDLER capability was requested, would return the Inventory. and in the item which has this inventory data, it would return this InventoryProvider in initCapabilities.

Here's an example:

public class BackpackCapabilityProvider implements ICapabilitySerializable<INBT> {

	// BackpackItemStackHandler is just an extension of ItemStackHandler which makes sure no Backpack can be put in the inventory
    private BackpackItemStackHandler backpackItemStackHandler;

	// This instantiates the Inventory only when it is first requested, and then caches it
    @Nonnull
    private BackpackItemStackHandler getCachedInventory() {
        if (backpackItemStackHandler == null) backpackItemStackHandler = new BackpackItemStackHandler();
        return backpackItemStackHandler;
    }

    private final LazyOptional<IItemHandler> lazyInventory = LazyOptional.of(this::getCachedInventory);

	// Provides the Inventory
    @Nonnull @Override
    public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, Direction side) {
        if (cap == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) return (LazyOptional<T>)(lazyInventory);
		// If we needed to provide more than one capability, we'd simply add another check:
		// if (cap == SOME_OTHER_CAPABILITY) return (otherCapability)

        return LazyOptional.empty();
    }

	// Saves the Inventory data to an NBT tag, so that it can be saved to disk
    @Override
    public INBT serializeNBT() {
        return CapabilityItemHandler.ITEM_HANDLER_CAPABILITY.writeNBT(getCachedInventory(), null);
    }
  
	// Reads the Inventory data from an NBT tag that was saved to disk
    @Override
    public void deserializeNBT(INBT nbt) {
        CapabilityItemHandler.ITEM_HANDLER_CAPABILITY.readNBT(getCachedInventory(), null, nbt);
    }
}

 

Posted
15 hours ago, Skelyvelocirap said:

How do i check the type of capability its returning for with this method though?

You don't. That's the point in time when you are saying "THIS ITEM HAS A CAPABILITY!" and it gets called once on itemstack creation.
The getCapability method that you need to check against is different.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted
On 7/18/2021 at 12:21 PM, kiou.23 said:

the job of the CapabilityProvider is too handle and expose all the capabilities for an object. In the getCapabilities method, it will check for which capability that is being requested, and provide it.

in the Item class, the point of the initCapabilities method, is just to say which CapabilityProvider will handle it's data.

so if what you need is an Inventory, you'd have an "InventoryProvider", which when the ITEM_STACK_HANDLER capability was requested, would return the Inventory. and in the item which has this inventory data, it would return this InventoryProvider in initCapabilities.

Here's an example:


public class BackpackCapabilityProvider implements ICapabilitySerializable<INBT> {

	// BackpackItemStackHandler is just an extension of ItemStackHandler which makes sure no Backpack can be put in the inventory
    private BackpackItemStackHandler backpackItemStackHandler;

	// This instantiates the Inventory only when it is first requested, and then caches it
    @Nonnull
    private BackpackItemStackHandler getCachedInventory() {
        if (backpackItemStackHandler == null) backpackItemStackHandler = new BackpackItemStackHandler();
        return backpackItemStackHandler;
    }

    private final LazyOptional<IItemHandler> lazyInventory = LazyOptional.of(this::getCachedInventory);

	// Provides the Inventory
    @Nonnull @Override
    public <T> LazyOptional<T> getCapability(@Nonnull Capability<T> cap, Direction side) {
        if (cap == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) return (LazyOptional<T>)(lazyInventory);
		// If we needed to provide more than one capability, we'd simply add another check:
		// if (cap == SOME_OTHER_CAPABILITY) return (otherCapability)

        return LazyOptional.empty();
    }

	// Saves the Inventory data to an NBT tag, so that it can be saved to disk
    @Override
    public INBT serializeNBT() {
        return CapabilityItemHandler.ITEM_HANDLER_CAPABILITY.writeNBT(getCachedInventory(), null);
    }
  
	// Reads the Inventory data from an NBT tag that was saved to disk
    @Override
    public void deserializeNBT(INBT nbt) {
        CapabilityItemHandler.ITEM_HANDLER_CAPABILITY.readNBT(getCachedInventory(), null, nbt);
    }
}

 

Alright so, ive gotten it to work but now im a bit confused as to how i add items to the inventory. Im guessing its not as simple as inventoryHandler#setStackInSlot() is it? Would this add it to the item or itemstack?

Posted

ItemStackHandler#insert and ItemStackHandler#Extract

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Im trying to build my mod using shade since i use the luaj library however i keep getting this error Reason: Task ':reobfJar' uses this output of task ':shadowJar' without declaring an explicit or implicit dependency. This can lead to incorrect results being produced, depending on what order the tasks are executed. So i try adding reobfJar.dependsOn shadowJar  Could not get unknown property 'reobfJar' for object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler. my gradle file plugins { id 'eclipse' id 'idea' id 'maven-publish' id 'net.minecraftforge.gradle' version '[6.0,6.2)' id 'com.github.johnrengelman.shadow' version '7.1.2' id 'org.spongepowered.mixin' version '0.7.+' } apply plugin: 'net.minecraftforge.gradle' apply plugin: 'org.spongepowered.mixin' apply plugin: 'com.github.johnrengelman.shadow' version = mod_version group = mod_group_id base { archivesName = mod_id } // Mojang ships Java 17 to end users in 1.18+, so your mod should target Java 17. java.toolchain.languageVersion = JavaLanguageVersion.of(17) //jarJar.enable() println "Java: ${System.getProperty 'java.version'}, JVM: ${System.getProperty 'java.vm.version'} (${System.getProperty 'java.vendor'}), Arch: ${System.getProperty 'os.arch'}" minecraft { mappings channel: mapping_channel, version: mapping_version copyIdeResources = true runs { configureEach { workingDirectory project.file('run') property 'forge.logging.markers', 'REGISTRIES' property 'forge.logging.console.level', 'debug' arg "-mixin.config=derp.mixin.json" mods { "${mod_id}" { source sourceSets.main } } } client { // Comma-separated list of namespaces to load gametests from. Empty = all namespaces. property 'forge.enabledGameTestNamespaces', mod_id } server { property 'forge.enabledGameTestNamespaces', mod_id args '--nogui' } gameTestServer { property 'forge.enabledGameTestNamespaces', mod_id } data { workingDirectory project.file('run-data') args '--mod', mod_id, '--all', '--output', file('src/generated/resources/'), '--existing', file('src/main/resources/') } } } sourceSets.main.resources { srcDir 'src/generated/resources' } repositories { flatDir { dirs './libs' } maven { url = "https://jitpack.io" } } configurations { shade implementation.extendsFrom shade } dependencies { minecraft "net.minecraftforge:forge:${minecraft_version}-${forge_version}" implementation 'org.luaj:luaj-jse-3.0.2' implementation fg.deobf("com.github.Virtuoel:Pehkui:${pehkui_version}") annotationProcessor 'org.spongepowered:mixin:0.8.5:processor' minecraftLibrary 'luaj:luaj-jse:3.0.2' shade 'luaj:luaj-jse:3.0.2' } // Example for how to get properties into the manifest for reading at runtime. tasks.named('jar', Jar).configure { manifest { attributes([ 'Specification-Title' : mod_id, 'Specification-Vendor' : mod_authors, 'Specification-Version' : '1', // We are version 1 of ourselves 'Implementation-Title' : project.name, 'Implementation-Version' : project.jar.archiveVersion, 'Implementation-Vendor' : mod_authors, 'Implementation-Timestamp': new Date().format("yyyy-MM-dd'T'HH:mm:ssZ"), "TweakClass" : "org.spongepowered.asm.launch.MixinTweaker", "TweakOrder" : 0, "MixinConfigs" : "derp.mixin.json" ]) } rename 'mixin.refmap.json', 'derp.mixin-refmap.json' } shadowJar { archiveClassifier = '' configurations = [project.configurations.shade] finalizedBy 'reobfShadowJar' } assemble.dependsOn shadowJar reobf { re shadowJar {} } publishing { publications { mavenJava(MavenPublication) { artifact jar } } repositories { maven { url "file://${project.projectDir}/mcmodsrepo" } } }  
    • All versions of Minecraft Forge suddenly black screen even without mods (tried reinstalling original Minecraft, Java, updating drivers doesn't work)
    • When i join minecraft all ok, when i join world all working fine, but when i open indentity menu, i get this The game crashed whilst unexpected error Error: java.lang.NullPointerException: Cannot invoke "top.ribs.scguns.common.Gun$Projectile.getDamage()" because "this.projectile" is null crash report here https://paste.ee/p/0vKaf
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.