Posted August 17, 20169 yr I'm trying to get the scrollwheel as input for my item, but what I currently have doesn't seem to work. I'm new to events so please tell me if I'm doing something completely wrong. This is the function that I have in my Item class: @SubscribeEvent public void scroll(MouseEvent event) { event.setCanceled(true); }
August 17, 20169 yr 1. Did you register event? 2. I don't think MouseEvent is cancelable (read docs). 3. MouseEvent (like any other input events) are client-sided. First of all they shouldn't be placed in common classes like Item. Second - you will need packets to do anything there (send packet to server from client when you want to do stuff). 1.7.10 is no longer supported by forge, you are on your own.
August 17, 20169 yr Author 1. No, I didn't. How do I do that? 2. I think it is cancellable, it says it in the MouseEvent class 3. Where should I put it then?
August 17, 20169 yr To register call MinecraftForge.EVENT_BUS.register(...); Doesn't matter right now, but it doesn't say it is or is not(at least for me). Make a new Class to put it in. VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect. Forge and vanilla BlockState generator.
August 18, 20169 yr Author Alright, I got the event sorta working. But how do I get the player from the event? And is this the correct way to make it client-side only?(probably not) I register the event in my main Classes Pre-Init. Is that the way to do it? This is my event class: public class MouseScroll { @SideOnly(Side.CLIENT) @SubscribeEvent public void scroll(MouseEvent event) { if(event.dwheel > 0) { } else if(event.dwheel < 0) { } } }
August 18, 20169 yr You should register it in init. What you need to do is use packets to send the data to the server. If you don't know how to use packets here is a link. http://jabelarminecraft.blogspot.com/p/minecraft-forge.html The data should contain something like a boolean or a byte with either 0 or 1 to determine > or < VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect. Forge and vanilla BlockState generator.
August 18, 20169 yr Author I see... But I still need to get the entity from the event. How would I do that?
August 18, 20169 yr You actually need to get the players UUID so Minecraft holds a client side player variable inside itself. VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect. Forge and vanilla BlockState generator.
August 18, 20169 yr Author Ok... Can you be a little bit more specific how and where I should do that?
August 18, 20169 yr I assume you have created a packet? VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect. Forge and vanilla BlockState generator.
August 18, 20169 yr Author Yes I have. This is where I send the packet: public class MouseScroll { @SideOnly(Side.CLIENT) @SubscribeEvent public void scroll(MouseEvent event) { if(event.dwheel > 0) { CreativePlus.network.sendToServer(new NoisePacket(1)); System.out.println("Packet 1 Sent"); } else if(event.dwheel < 0) { CreativePlus.network.sendToServer(new NoisePacket(2)); System.out.println("Packet 2 Sent"); } } } This is the Packet: public class NoisePacket implements IMessage{ private int upOrDown; public NoisePacket() { } public NoisePacket(int i) { this.upOrDown = i; } @Override public void fromBytes(ByteBuf buf) { upOrDown = ByteBufUtils.readVarInt(buf, 4); } @Override public void toBytes(ByteBuf buf) { ByteBufUtils.writeVarInt(buf, upOrDown, 4); } } This is the Packet Handler: public class NoisePacketHandler implements IMessageHandler<NoisePacket, IMessage> { @Override public IMessage onMessage(NoisePacket message, MessageContext ctx) { IThreadListener mainThread = Minecraft.getMinecraft(); mainThread.addScheduledTask(new Runnable(){ @Override public void run() { EntityPlayerSP player = Minecraft.getMinecraft().thePlayer; System.out.println("Packet Revieved"); if(player.getItemInUse().getItem() == CustomItems.noiseFill && player.isSneaking()) { ItemStack stack = player.getItemInUse(); NBTTagCompound subTag = stack.getTagCompound(); subTag.setDouble("percentage", subTag.getDouble("percentage") + 0.1); stack.setTagCompound(subTag); } } }); return null; } } I have registred the Packet on the client Side. How do I use the Packet Handler to change some nbt on the players currently held itemStack?
August 18, 20169 yr Don't use EntityPlayerSP for this if you are editing NBT or any data it must be handled by the server, which means use EntityPlayerMP and you can't use Minecraft...thePlayer. You should use... if (ctx.side == Side.SERVER) EntityPlayerMP player = ctx.getServerHandler().playerEntity; or something similar. VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect. Forge and vanilla BlockState generator.
August 18, 20169 yr Author Alright, the packet now gets recieved, but a nullPointerException is being thrown at the if(player.getItemInUse....) public class NoisePacketHandler implements IMessageHandler<NoisePacket, IMessage> { @Override public IMessage onMessage(NoisePacket message, final MessageContext ctx) { IThreadListener mainThread = (WorldServer)ctx.getServerHandler().playerEntity.worldObj; mainThread.addScheduledTask(new Runnable(){ EntityPlayerMP player = ctx.getServerHandler().playerEntity; @Override public void run() { System.out.println("Packet Recieved"); if(player.getItemInUse().getItem() == CustomItems.noiseFill && player.isSneaking()) { ItemStack stack = player.getItemInUse(); NBTTagCompound subTag = stack.getTagCompound(); subTag.setDouble("percentage", subTag.getDouble("percentage") + 0.1); stack.setTagCompound(subTag); } else { return; } } }); return null; } }
August 18, 20169 yr It is possible for the ItemStack to be null you need to check if it is != null VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect. Forge and vanilla BlockState generator.
August 18, 20169 yr Author It is possible for the ItemStack to be null you need to check if it is != null Oh your're right. Sometimes I derp
August 18, 20169 yr Author So I need to cancel the event, if the player is holding the Item and sneaking, but I don't know how to get the EntityPlayerMP object. public class MouseScroll { @SideOnly(Side.CLIENT) @SubscribeEvent public void scroll(MouseEvent event) { EntityPlayerMP player = //what do I put here? System.out.println(player.getItemInUse()); if(player.getItemInUse() != null) { if(player.getItemInUse().getItem() == CustomItems.noiseFill && player.isSneaking()) { if(event.dwheel > 0) { CreativePlus.network.sendToServer(new NoisePacket(1)); System.out.println("Packet 1 Sent"); event.setCanceled(true); } else if(event.dwheel < 0) { CreativePlus.network.sendToServer(new NoisePacket(2)); System.out.println("Packet 2 Sent"); event.setCanceled(true); } } } } }
August 18, 20169 yr I hope you mean is not sneaking or holding the item, but there since you are performing on the client again you should use EntityPlayerSP. VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect. Forge and vanilla BlockState generator.
August 18, 20169 yr Author Umm no I want to cancel the event, so that the scrolling doesn't switch to another Item, and I only want to do that if the player is sneaking EDIT: Nevermind me, I just noticed that getItemInUse is the wrong function to use EDIT2: It all works!
August 18, 20169 yr Umm no I want to cancel the event, so that the scrolling doesn't switch to another Item, and I only want to do that if the player is sneaking EDIT: Nevermind me, I just noticed that getItemInUse is the wrong function to use Then you may want to look at this in your message. if(player.getItemInUse().getItem() == CustomItems.noiseFill && player.isSneaking()) { VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect. Forge and vanilla BlockState generator.
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.