Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

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

Featured Replies

Posted

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

  • Author

No this definitely should be server sided too. I got it figured out eventually with the packet system. I was just confused with creating some class that would be sending and receiving my packets. Can I somehow mark this issue as solved ?

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.

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.