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.

Featured Replies

Posted

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);
      }

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.

  • 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?

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.

  • 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)
            {
                 
            }
      }

}

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.

  • Author

I see... But I still need to get the entity from the event. How would I do that?

  • Author

Ok... Can you be a little bit more specific how and where I should do that?

 

  • 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?

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.

  • 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;
      }



}

  • 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 :P

  • 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);

                        }

                  }

            }


      }

}

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.

  • 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!

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.

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.