Jump to content

Recommended Posts

Posted

I currently have a custom inventory that has two problems:

  • Client side doesn't allow items to enter the custom slots (as it should), but server side allows items to enter the slots
  • Opening the custom inventory will move items around and sometimes dupe things as well.

I think the second problem is possibly coming from how I add the slots are being added in the container, but I'm not exactly sure why beyond that.

 

Container class

Spoiler

package mod.mythusteam.mythus.inventory;

import mod.mythusteam.mythus.capabilities.interfaces.ICoreStorage;
import mod.mythusteam.mythus.init.MythusContainers;
import mod.mythusteam.mythus.inventory.slots.MythusSlotCores;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.container.ClickType;
import net.minecraft.inventory.container.Container;
import net.minecraft.inventory.container.Slot;
import net.minecraft.item.ItemStack;
import org.apache.commons.lang3.tuple.ImmutablePair;

import java.util.ArrayList;
import java.util.List;

public class CoreStorageContainer extends Container
{
    private final int CORES_SIZE;
    private static final List<ImmutablePair<Integer,Integer>> slotCoords = new ArrayList<ImmutablePair<Integer, Integer>>(5)
    {{
        //(x,y)
        add(new ImmutablePair<>(80, 17));
        add(new ImmutablePair<>(44, 44));
        add(new ImmutablePair<>(116, 44));
        add(new ImmutablePair<>(61, 84));
        add(new ImmutablePair<>(101, 84));
    }};

    public CoreStorageContainer(int id, PlayerInventory inventory, ICoreStorage cores)
    {
        super(MythusContainers.CORE_STORAGE_CONTAINER.get(), id);

        CORES_SIZE = cores.getSlots();

        //TODO Change texture locations
        for(int i = 0; i < cores.getSlots(); i++)
        {
            this.addSlot(new MythusSlotCores(cores, i, slotCoords.get(i).left, slotCoords.get(i).right));
        }

        for (int i = 0; i < 3; ++i)
        {
            for (int j = 0; j < 9; ++j)
            {
                this.addSlot(new Slot(inventory, j + i * 9 + 9, 8 + j * 18, 132 + i * 18));
            }
        }

        for (int i = 0; i < 9; ++i)
        {
            this.addSlot(new Slot(inventory, i, 8 + i * 18, 190));
        }
    }

    @Override
    public boolean canInteractWith(PlayerEntity playerIn) {
        return true;
    }

    @Override
    public ItemStack slotClick(int slotId, int dragType, ClickType clickTypeIn, PlayerEntity player) {
        return super.slotClick(slotId, dragType, clickTypeIn, player);
    }

    @Override
    public ItemStack transferStackInSlot(PlayerEntity playerIn, int index)
    {
        ItemStack itemstack = null;
        Slot slot = this.inventorySlots.get(index);

        if(slot != null && slot.getHasStack())
        {
            ItemStack itemstack1 = slot.getStack();
            itemstack = itemstack1.copy();
            if(index >= 0 && index < CORES_SIZE)
            {
                if (!this.mergeItemStack(itemstack1, CORES_SIZE+1, CORES_SIZE+36+1, true))
                {
                    return null;
                }

                slot.onSlotChange(itemstack1, itemstack);
            }
            else if(index >= CORES_SIZE)
            {
                if(!this.mergeItemStack(itemstack1, 0, CORES_SIZE, false))
                {
                    return null;
                }

                slot.onSlotChange(itemstack1, itemstack);
            }

            if (itemstack1.getCount() == 0)
            {
                slot.putStack(null);
            }
            else
            {
                slot.onSlotChanged();
            }

            if (itemstack1.getCount() == itemstack.getCount())
            {
                return null;
            }

        }

        return itemstack;
    }
}
 

  

Capability Instance Class

Spoiler

package mod.mythusteam.mythus.capabilities.instances;

import mod.mythusteam.mythus.capabilities.interfaces.ICoreStorage;
import mod.mythusteam.mythus.items.cores.MythusCore;
import net.minecraft.item.ItemStack;
import net.minecraftforge.items.ItemStackHandler;

import javax.annotation.Nonnull;

public class CoreStorageInstance extends ItemStackHandler implements ICoreStorage
{
    int level;

    public CoreStorageInstance()
    {
        super(5); //TODO Change?
    }

    @Override
    public int getLevel() {
        return level;
    }

    @Override
    public void setLevel(int level) {
        this.level = level;
    }

    @Override
    public boolean isItemValid(int slot, @Nonnull ItemStack stack) {
        return stack.getItem() instanceof MythusCore;
    }

    @Nonnull
    @Override
    public ItemStack insertItem(int slot, @Nonnull ItemStack stack, boolean simulate) {
        if(stack.getItem() instanceof MythusCore)
            return super.insertItem(slot, stack, simulate);
        else
            return ItemStack.EMPTY;
    }
}
 

 

The transferStackInSlot method is screwed up, but I'll try to fix it after I get these two pretty serious problems out of the way

Posted
14 minutes ago, Nozzomi said:

publicpublic ItemStack insertItem(int slot, @Nonnull ItemStack stack, boolean simulate) {
        if(stack.getItem() instanceof MythusCore)
            return super.insertItem(slot, stack, simulate);
        else
            return ItemStack.EMPTY;
    }

So, if it is the wrong item, destroy it?

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

@Draco18s Oops right, I think I just put that there just to see if checking for the right item there would stop server side from allowing it into the slot

Posted

I open the gui in the container by using ClientTickEvent

Spoiler

@SubscribeEvent
    public static void onClientTickEvent(final TickEvent.ClientTickEvent event)
    {
        if (event.phase != TickEvent.Phase.END) return;

        if(Mythus.keyBindings[0].isPressed())
        {
            MythusPacketHandler.INSTANCE.sendToServer(new MythusCoreStorageGUIPacket());
        }
    }

Then in the packet I just use the NetworkHooks#openGui

Spoiler

public static void handle(MythusCoreStorageGUIPacket pkt, Supplier<NetworkEvent.Context> ctx)
    {
        if(ctx.get().getDirection().getReceptionSide().isServer())
        {
            ctx.get().enqueueWork(() -> {
                PlayerEntity player = ctx.get().getSender();
                player.getCapability(CapabilityCoreStorage.CORE_STORAGE_CAPABILITY).ifPresent(cap -> {
                    NetworkHooks.openGui((ServerPlayerEntity) player, new CoreStorageContainerProvider());
                });
            });
            ctx.get().setPacketHandled(true);
        }
    }

 

 

And uhhh, I just register the container type using the DeferredRegister

public static final RegistryObject<ContainerType<CoreStorageContainer>> CORE_STORAGE_CONTAINER = CONTAINERS.register("core_storage_container",
            () -> new ContainerType<CoreStorageContainer>((id, inv) -> new CoreStorageContainer(id, inv, new CoreStorageInstance())));

 

Posted
11 minutes ago, diesieben07 said:

You're given a PlayerInventory, which has the PlayerEntity.

I tried using the capability, but the result is the same. The inventory is saving properly though, but so at least that's not a problem.

 

This is what I have now instead

public static final RegistryObject<ContainerType<CoreStorageContainer>> CORE_STORAGE_CONTAINER = CONTAINERS.register("core_storage_container",
            () -> new ContainerType<>((id, inv) -> new CoreStorageContainer(id, inv, inv.player.getCapability(CapabilityCoreStorage.CORE_STORAGE_CAPABILITY).orElse(new CoreStorageInstance()))));

 

Posted
5 minutes ago, diesieben07 said:

Both your IItemHandler and your slot (the latter unnecessarily, because the item handler already does it) check for instanceof MythusCore. An item with that class is never created. Thus, nothing can ever be put in the slots (client or server side).

I was not able to reproduce any duping or moving of items in the survival inventory that's shown below. I was not able to put any items into the upper slots (for the reason explained above).

 

Please clarify what your issue is and how to reproduce it.

Here's a video of me showing what I'm having problems with.

 

First, I'm showing the clicking the item into the slot that's doesn't allow it, then opening and closing that inventory, where the item ends up in the slot. After that, I'm switching between the vanilla inventory and the custom inventory quickly and the items eventually start to pile up in my inventory. 

 

Posted

That usually happens when you screw up your slot indexes.

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
20 minutes ago, Draco18s said:

That usually happens when you screw up your slot indexes.

for (int i = 0; i < 3; ++i)
{
    for (int j = 0; j < 9; ++j)
    {
        Mythus.LOG.info(j + i * 9 + 9);
        this.addSlot(new Slot(inventory, j + i * 9 + 9, 8 + j * 18, 132 + i * 18));
    }
}

for (int i = 0; i < 9; ++i)
{
        Mythus.LOG.info(i);
        this.addSlot(new Slot(inventory, i, 8 + i * 18, 190));
}

What would be wrong with this? No numbers are repeating in the logs I think

Posted

Those aren't your only slots, are they.

 

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
Just now, Draco18s said:

Those aren't your only slots, are they.

 

No, they aren't

for(int i = 0; i < cores.getSlots(); i++)
{
    this.addSlot(new SlotItemHandler(cores, i, slotCoords.get(i).left, slotCoords.get(i).right));
}

I didn't think that they'd affect the player inventory because they're using the indices for a different IItemHandler...it seems I might be wrong though...?

Posted
48 minutes ago, Nozzomi said:

No, they aren't


for(int i = 0; i < cores.getSlots(); i++)
{
    this.addSlot(new SlotItemHandler(cores, i, slotCoords.get(i).left, slotCoords.get(i).right));
}

I didn't think that they'd affect the player inventory because they're using the indices for a different IItemHandler...it seems I might be wrong though...?

Had to check.

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
1 hour ago, diesieben07 said:

I'm unable to reproduce the effect you showed in your video.

I clicked the slot with an item like a madman, the item does not go in.

 

I also smashed ^ and e in succession like a mad man to quickly switch between the inventory and your GUI. No duplication could be observed.

That's super weird, I guess the only option now is to...reinstantiate (is that the correct term or even a word?!?!) the forge project and will see what happens

39 minutes ago, Draco18s said:

Had to check.

Was there anything that might've royally screwed me over in terms of adding the inventory?

Posted
18 minutes ago, Nozzomi said:

Was there anything that might've royally screwed me over in terms of adding the inventory?

If you had inadvertently linked the player inventory instead of your container inventory.

When that happens, you get a very similar effect to your video.

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

Is it possible for me to extend on the player's regular ItemStackHandler capability? Maybe if I add the container through an already existing capability, it'll go more smoothly. How would I extend upon that capability?

Posted
1 hour ago, diesieben07 said:

Since I can't reproduce this on my end, you need to debug this yourself.

Put a breakpoing in insertItem (and setStackInSlot) of your item handler. Then reproduce the "wrong item in slot" bug and see where the method is called from.

Then report your findings here.

I think what I've found is that inserItem and setStackInSlot isn't called when I try to ghost insert my item in the custom inventory, but the item somehow still ends up in the slot it's not supposed to be in. I also tried sticking an extra isItemValid() check in the setStackInSlot method before the super, but it ended up not showing the items, but if I clicked on the slot, the item would still be there, just no longer showing.

Posted
27 minutes ago, diesieben07 said:

That's... not possible...

Video

 

The sword is placed in according to the server but the breakpoint doesn't stop me when I click with the sword. The setStackInSlot part is because the item is already recognized as in there...? (It's definitely your word over mine, I'm a little lost)

Posted
4 hours ago, diesieben07 said:

Did some more digging. You never attach CapabilityCoreStorage.CORE_STORAGE_CAPABILITY to the player. Which is why getCapability for it always returns an empty optional. Which you ignore and just create an empty one. This is not how you handle errors. If you expect the capability to be there, throw an error if it's absent! This is how you get hard to debug problems like this. "Oh, something strange happend that I didn't expect? Oh well, carry on."

This is not PHP.

Wait, but don't I? I have

e.addCapability(CapabilityCoreStorage.LOCATION, new CapabilityCoreStorage());

where LOCATION is just the ResourceLocation needed to attach the capability, right?

Posted (edited)

Also, I changed the orElse into an orElseThrow and it hasn't given me any errors (though I'm  not sure whether or not to be happy since the problem isn't any clearer...)

public static final RegistryObject<ContainerType<CoreStorageContainer>> CORE_STORAGE_CONTAINER = CONTAINERS.register("core_storage_container",
    () -> new ContainerType<>((id, inv) -> new CoreStorageContainer(id, inv, inv.player.getCapability(CapabilityCoreStorage.CORE_STORAGE_CAPABILITY).orElseThrow(() -> new NullPointerException("Core Storage Capability not found!")))));

 

Edited by Nozzomi
Posted
2 hours ago, diesieben07 said:

Please clone your own repo in a separate folder and check if you have the error there as well.

Huh, I couldn't reproduce it either...

 

I ended up fixing the problem in the original code by creating a new world since I figured that if it worked in the git clone, it must've had something to do with the run folder. In hindsight, I probably should've created a new world in the first place since I was attaching a capability to the player. Thank you for helping me through!

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

    • that happens every time I enter a new dimension.
    • This is the last line before the crash: [ebwizardry]: Synchronising spell emitters for PixelTraveler But I have no idea what this means
    • What in particular? I barely used that mod this time around, and it's never been a problem in the past.
    • 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" } } } my entire project:https://github.com/kevin051606/DERP-Mod/tree/Derp-1.0-1.20
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

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