Jump to content

(solved) [1.16] setInventorySlotContents(), but on the server side


FluffyDiscord

Recommended Posts

Hello,

I would like to make a mod, where player can swap items he is holding when he scrolls with mouse wheel and holds the right item from a predefined list in hand. So far I have made a prototype which works - kinda - it switches items on the client side as I understand this correctly.

 

My question is, how do I send the item swap request to the server to validate (if it's a valid swap request) and then update player's inventory according to it ?

 

I have found this page https://mcforge.readthedocs.io/en/1.15.x/networking/simpleimpl/ but I am kinda confused - what do I do with that information ?

 

package com.mossshine.blocks.init;

import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.player.ClientPlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.client.event.InputEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;

@Mod.EventBusSubscriber(Dist.CLIENT)
public class Events {

    private static final double SCROLL_DOWN = -1.0;
    private static final double SCROLL_UP = 1.0;

    @SubscribeEvent
    public static void mouseScrolled(InputEvent.MouseScrollEvent event) {
        ClientPlayerEntity player = Minecraft.getInstance().player;
        if (player != null) {
            ItemStack currentItem = player.getHeldItemMainhand();
            int index = BlocksList.LOG_ARRAY_LIST.indexOf(currentItem.getItem());

            if (event.getScrollDelta() == SCROLL_DOWN && index != -1) {
                event.setCanceled(true);
                int slot = player.inventory.currentItem;
                Item prevItem;

                index--;
                if (index > -1) {
                    prevItem = BlocksList.LOG_ARRAY_LIST.get(index);
                } else {
                    prevItem = BlocksList.LOG_ARRAY_LIST.get(BlocksList.LOG_ARRAY_LIST.size() - 1);
                }
                ItemStack newItem = new ItemStack(prevItem, currentItem.getCount());

                player.inventory.setInventorySlotContents(slot, newItem);
                player.container.detectAndSendChanges();
            }
        }
    }
}

 

Edited by FluffyDiscord
Link to comment
Share on other sites

On 9/20/2020 at 6:16 PM, diesieben07 said:

In your case I would only send "player scrolled X amount", not e.g. "move stack A from A to B".

I'm not fully convinced of that. You should send "player scrolled X slots", that way the actual scroll amount per slot can be configured client-side.

Link to comment
Share on other sites

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.



×
×
  • Create New...

Important Information

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