FluffyDiscord Posted September 20, 2020 Posted September 20, 2020 (edited) 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 September 20, 2020 by FluffyDiscord Quote
FluffyDiscord Posted September 20, 2020 Author Posted September 20, 2020 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 ? Quote
Alpvax Posted September 23, 2020 Posted September 23, 2020 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. Quote
Recommended Posts
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.