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

  Quote

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
  On 8/18/2016 at 11:04 AM, Animefan8888 said:

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

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
  On 8/18/2016 at 12:22 PM, Tschipp said:

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Hello all. I'm currently grappling with the updateShape method in a custom class extending Block.  My code currently looks like this: The conditionals in CheckState are there to switch blockstate properties, which is working fine, as it functions correctly every time in getStateForPlacement.  The problem I'm running into is that when I update a state, the blocks seem to call CheckState with the position of the block which was changed updated last.  If I build a wall I can see the same change propagate across. My question thus is this: is updateShape sending its return to the neighbouring block?  Is each block not independently executing the updateShape method, thus inserting its own current position?  The first statement appears to be true, and the second false (each block is not independently executing the method). I have tried to fix this by saving the block's own position to a variable myPos at inception, and then feeding this in as CheckState(myPos) but this causes a worse outcome, where all blocks take the update of the first modified block, rather than just their neighbour.  This raises more questions than it answers, obviously: how is a different instance's variable propagating here?  I also tried changing it so that CheckState did not take a BlockPos, but had myPos built into the body - same problem. I have previously looked at neighbourUpdate and onNeighbourUpdate, but could not find a way to get this to work at all.  One post on here about updatePostPlacement and other methods has proven itself long superceded.  All other sources on the net seem to be out of date. Many thanks in advance for any help you might offer me, it's been several days now of trying to get this work and several weeks of generally trying to get round this roadblock.  - Sandermall
    • sorry, I might be stupid, but how do I open it? because the only options I have are too X out, copy it, which doesn't work and send crash report, which doesn't show it to me, also, sorry for taking so long.
    • Can you reproduce this with version 55.0.21? A whole lot of plant placement issues were just fixed in this PR.
    • Necro'ing that thread to ask if you found a solution ? I'm encountering the same crash on loading the world. I created the world in Creative to test my MP, went into survival to test combat, died, crashed on respawn and since then crash on loading the world. Deactivating Oculus isn't fixing it either, and I don't have Optifine (Twilight forest is incompatible)
  • Topics

×
×
  • Create New...

Important Information

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