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

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now


×
×
  • Create New...

Important Information

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