Jump to content

Recommended Posts

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

Posted

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.

Posted

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.

Posted

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

}

Posted

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.

Posted

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?

Posted

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.

Posted

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



}

Posted

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

                        }

                  }

            }


      }

}

Posted

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!

Posted

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

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

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