Jump to content

[1.10.2] How to set up keybindings?


RealTheUnderTaker11

Recommended Posts

I've been looking around and from everything I've seen there used to be a forge wiki page on it but there no long is. Do I have to set up a whole packet handler like this http://www.minecraftforge.net/forum/index.php?topic=20135.0 and all the keyhandler stuff, or is there an easier way to do it?

Looking around the closest thing I could find was this http://www.minecraftforge.net/forum/index.php?topic=25751.msg131395#msg131395 but like I said, I'm not sure.

 

Basically my question is how should I make a keybinding and all needed things for that in 1.10.2?

My IGN is TheUnderTaker11, but when I tried to sign up for this site it would not send the email... So yea now I have to use this account.

Link to comment
Share on other sites

Its actually pretty simple, you can see how im doing it here to enable jetpack flight

https://github.com/abused/Tech-Expansion/blob/master/src/main/java/abused_master/TechExpansion/registry/KeybindHandler.java

and then i call

KeybindHandler.init();

in the preInit in my client proxy, using it is easy too how im doing it is checking if its being used as so:

if(KeybindHandler.keybind.isKeyDown()) {
        }

Link to comment
Share on other sites

Okay thank you, whew I was hoping I wasn't going to have to go through all that packet handler stuff just to have a keybind!

 

[EDIT] So question, what situation could I call that in though? It seems like it would need a point of reference, so do I have to call it on the clientside? Or could I call it somewhere like in the PlayerTick event?

My IGN is TheUnderTaker11, but when I tried to sign up for this site it would not send the email... So yea now I have to use this account.

Link to comment
Share on other sites

Okay thank you, whew I was hoping I wasn't going to have to go through all that packet handler stuff just to have a keybind!

You usually need to use packets with keybinds. Keybinds are only client-side, so if you want to affect something on the server you need to send a packet.

 

[EDIT] So question, what situation could I call that in though? It seems like it would need a point of reference, so do I have to call it on the clientside? Or could I call it somewhere like in the PlayerTick event?

 

Keybinds should be registered client-side only during preinit. You call

Keybinding#isKeyDown()

on the client whenever you want to know if the bound key/s are pressed.

 

@abused_master

This will not work on a dedicated server, you need to use packets to turn the jetpack on/off server-side.

Link to comment
Share on other sites

Okay so now I'm really confused. So I got two questions now.

 

import io.netty.buffer.ByteBuf;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.util.IThreadListener;
import net.minecraft.util.text.TextComponentString;
import net.minecraft.world.WorldServer;
import net.minecraftforge.fml.common.network.ByteBufUtils;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;

public class SendTeleportPlayer implements IMessage{

private int Distance;

public SendTeleportPlayer() { }

    public SendTeleportPlayer(int distance) {
        this.Distance = distance;
    }
@Override
public void fromBytes(ByteBuf buf)
{
	Distance = buf.readInt();
}

@Override
public void toBytes(ByteBuf buf)
{
	buf.writeInt(Distance);
}

public static class Handler implements IMessageHandler<SendTeleportPlayer, IMessage> {
    
    @Override
    public IMessage onMessage(SendTeleportPlayer message, MessageContext ctx) {
        IThreadListener mainThread = (WorldServer) ctx.getServerHandler().playerEntity.worldObj; // or Minecraft.getMinecraft() on the client
        mainThread.addScheduledTask(new Runnable() {
            @Override
            public void run() {
            	EntityPlayerMP serverPlayer = ctx.getServerHandler().playerEntity;
            	serverPlayer.addChatMessage(new TextComponentString("*Sigh*, "+message.Distance));
            }
        });
        return null; // no response
    }
}
}

 

 

My first question is, when it says "//or Minecraft.getMinecraft() on the client" does it mean when I'm sending it from the client, or when it's being run on the client? It never really said.

 

Second question is, where do I even put the code I want to actually do stuff? When it is in the run() like I have it now, I just get an error saying

Cannot refer to the non-final local variable ctx defined in an enclosing scope

and the same thing for the message#Distance.

 

For context my end goal is for this to "teleport" or move the player in the direction he is looking when he hits a keybind. Sure I could change the distance if I wanted but the actual info in the packet sends is arbitrary for what I want. I just need it to know a player hit the key and check a capability on the player.

My IGN is TheUnderTaker11, but when I tried to sign up for this site it would not send the email... So yea now I have to use this account.

Link to comment
Share on other sites

My first question is, when it says "//or Minecraft.getMinecraft() on the client" does it mean when I'm sending it from the client, or when it's being run on the client? It never really said.

 

"Use Minecraft.getMinecraft() on the client."

"Minecraft is a Client-Side-Only class."

 

Hmmmmmmmm

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

My first question is, when it says "//or Minecraft.getMinecraft() on the client" does it mean when I'm sending it from the client, or when it's being run on the client? It never really said.

 

"Use Minecraft.getMinecraft() on the client."

"Minecraft is a Client-Side-Only class."

 

Hmmmmmmmm

What I am saying is that the packet, since it is a keybind, will be sent from the client to the server. There could be situations when you send a packet from server to client. I am asking for clarification on which one I should use. Does the context come from the client that sent it, hence I would have to use Minecraft.getMinecraft(), or is the context gotten from the server where the actual stuff should happen, and I'm supposed to assume the server already knows what player sent it?

My IGN is TheUnderTaker11, but when I tried to sign up for this site it would not send the email... So yea now I have to use this account.

Link to comment
Share on other sites

Minecraft is a client side only class, you CANNOT use it on the server, not even in a packet handler.

 

The code in the example is a server side packet handler, which is what you want. The comment is for going the other way.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

A final method parameter is just one that you agree not to modify.  It's perfectly safe to make cxt final.

https://github.com/Draco18s/ReasonableRealism/blob/master/src/main/java/com/draco18s/ores/networking/ServerOreCartHandler.java#L30

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

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.