Jump to content

[1.8] {SOLVED} Key Press with Item in Hand?


mentin2

Recommended Posts

Hello everyone, I hope you can help me with this issue I've been having!

 

So a little background info, I started coding for minecraft 1.1 and 1.3.1 (2 years ago), and since then I've just been tinkering with it! However, I figured out I'd remake a mod I started on back then but I never got to finish or publish, and since I haven't seen anyone make it yet, I thought I'll just do it myself, and here I am... However, I'm in need of some help, since modding 1.8 is very different from older versions...

 

So following

I have setup a workspace (github), if it's badly setup please tell me so, or if you have any advices regarding how I've setup my workspace, I'm all ears!

 

So back to my issue, I want my "teleport_device" (com.isakviste.essentials.init.EssentialsItems.java) which is extending the "ItemTeleportCoordCache.java" (com.isakviste.essentials.items) to clear the NBTTagCompound when I press 'R' (KEY_R) (which could be changed in options, but unless that's simple, lets leave that for later!).

 

I already have it to clear the NBTTagCompound on right click if you are not crouched:

@Override
public ItemStack onItemRightClick(ItemStack stack, World worldIn, EntityPlayer playerIn)
{
if (!playerIn.isSneaking())
{
	if (stack.getTagCompound() != null)
	{
		// removes the NBTTag from the items data, basically removes the
		// coordinate data stored
		stack.getTagCompound().removeTag("coords");
		stack.clearCustomName();
	}
}
return stack;
}

However, that is just temporary until I find out how to do it when the 'R' key is pressed, as I want to have the teleport code on the right click. (I also need this for more items later: guns, grenades...)

 

I consider myself pretty new to coding (even though I love tinkering around it and figuring things out by myself, as well as reading documentations and other forums) so please try to explain as simple as possible and not leaving steps out! I have to say, it was only now after several hours of research with little progress that I thought it was time to ask you guys. I found a few similar threads/posts but either I didn't feel like they explained enough and skipped steps, or that it wasn't the same issue!

 

~Isak Viste

 

SOLVED:

 

With the help of this example: https://github.com/TheGreyGhost/MinecraftByExample

I managed to make what I wanted, you can check that out here: https://github.com/Mentin002/RedstoneGuns

Link to comment
Share on other sites

You need to subscribe to the KeyInputEvent (search for KeyHandler, KeyBinding, that kind of thing and you'll find tutorials on that), then when the key is pressed, which happens CLIENT side, you must send a packet to the server to let it know the key was pressed, and the server then decides what to do (e.g. clear the NBT data of the currently held item).

 

If you are unfamiliar with packets, diesieben has a tutorial here in the Tutorials section that should get on the right path.

Link to comment
Share on other sites

You need to subscribe to the KeyInputEvent [...]

 

Okay will definitely check that out thanks!

I was looking at the forge wiki and I saw some things about KeyBindings and KeyInputHandler, as you can see on my github workspace, and I managed to get a print screen to work with the r button, however, I did not figure out how to make it only work when that specific item is in the current slot bar...

I will have a look at those tutorials and see if I can figure it out. Thank you for the help!

 

EDIT: Not really sure what it does, yes I did read it, but I find it confusing... Oh well, I guess I'll just have to tinker with it some more!

Link to comment
Share on other sites

Current as in the player's currently held item? Since user input is all handled client side, you are safe to use Minecraft.getMinecraft().thePlayer for the current client side player (i.e. the one who pressed the key).

 

From there, simply get player.getHeldItem() and check if it is the item you want, but really I would just send the packet right away to the server saying 'player A pressed button B, now what?' and let the server handle all the logic. Unless whatever you want to do is client side only.

Link to comment
Share on other sites

Current as in the player's currently held item? Since user input is [...]

 

Yes current as in currently held item.

 

What I want is to make a mod, a mod I've been waiting for a long time but that has never been made, so I'm taking matters into my own hands. However, I'm not too good with modding or coding, so honestly, I'm a bit lost here with all the client/server side stuff going on...

I have to say, it was easier to mod minecraft 1.3.1, but I guess you have more freedom now than back then...

So if you could point me in the right direction here, I'd be grateful!

 

I might not have been clear enough, but what I want is right now, is just figuring out how I will make my mod, and to do so, I'm working on this small grenade/teleport mod, just to figure out how things work. And right now I have problems with the key binding... I want it so that when you have the teleport item in hand if you shift+right click a block, it gets the coordinates of that block and in which dimension you are, and stores it in the item <- I already have that. What I now want, is that when you hold that item in hand, and press 'R', the NBTag stored on the item is cleared/removed... I have managed to do this with just right click, but I want it now to do it when I press 'R', and that's where I'm having problems.

Also all this client/server side stuff is just making me confused, I checked out Diesieben07's tutorial on packets but it just made me even more confused...

 

So again, if you could point me in the right direction here, it'd be nice, also if you know any nice tutorials for beginners, that would help as well! Like I said, I'm currently in a learning phase, so the simpler, the better.

Link to comment
Share on other sites

Current as in the player's currently held item? Since user input is [...]

 

Yes current as in currently held item.

 

What I want is to make a mod, a mod I've been waiting for a long time but that has never been made, so I'm taking matters into my own hands. However, I'm not too good with modding or coding, so honestly, I'm a bit lost here with all the client/server side stuff going on...

I have to say, it was easier to mod minecraft 1.3.1, but I guess you have more freedom now than back then...

So if you could point me in the right direction here, I'd be grateful!

 

I might not have been clear enough, but what I want is right now, is just figuring out how I will make my mod, and to do so, I'm working on this small grenade/teleport mod, just to figure out how things work. And right now I have problems with the key binding... I want it so that when you have the teleport item in hand if you shift+right click a block, it gets the coordinates of that block and in which dimension you are, and stores it in the item <- I already have that. What I now want, is that when you hold that item in hand, and press 'R', the NBTag stored on the item is cleared/removed... I have managed to do this with just right click, but I want it now to do it when I press 'R', and that's where I'm having problems.

Also all this client/server side stuff is just making me confused, I checked out Diesieben07's tutorial on packets but it just made me even more confused...

 

So again, if you could point me in the right direction here, it'd be nice, also if you know any nice tutorials for beginners, that would help as well! Like I said, I'm currently in a learning phase, so the simpler, the better.

Let me explain you server-client stuff a bit:

- Server is a logical part of the game, all logic, calculation, processing must happen here.

  All logic must happen on server side, because if it would happen on client, it would open straight road for hackers & cheaters.

- Client is a graphicall part of the game: rendering and input goes here.

 

Let's split 3 minecraft game cases in to sides:

1) Multiplayer: server machine is a server and all connected players are clients

2) Singleplayer: your computer is both of these: server one and client one. It can be represented like private server hosted on your computer with only 1 player.

3) Lan server: your computer "splits" into 2 "instances": server and client, where all players, including computer client "instance", are connected to server to yours computer server "instance". This can be represented like server hosted on your computer, where you play on your computer (where you play on same machine)...

 

Server does not have graphic classes, so using them on server will crash it...

 

Server neither have keyboard, mouse or other input devices, so clients needs to send packets to server, containing information about user input (ex: player X has pressed button 'G')

in order to proceed... Then server processes packets information and decides what to do.

 

Few warnings:

 

- USE @SIDEONLY ANNOTATED FIELDS ONLY ON CONVINIENT SIDE (USING CLIENT FIELDS WILL CRASH SERVER)

- NEVER PROCESS INPUT INFORMATION ON CLIENT AND SEND WHAT TO DO TO SERVER!!!

 

Link to comment
Share on other sites

How good are you with Java?

 

Not Too good I'm afraid...

I can read and understand the basics

 

Let me explain you server-client stuff a bit [...]

 

Okay thanks, I think I start to understand how it works, however, I am not sure how I am to use this in my code.

So I need the client to check for input, then tell the server that the Player X pressed this key. Then the server figures out what to do, then tells the client what to do?

 

Example of how I understood it:

- CLIENT [Player12]: (send to SERVER) [Player12] pressed R

- SERVER: Pressing R means to open the custom crafting gui

- SERVER: (send to CLIENT) Open the custom crafting gui for [Player12]

- CLIENT [Player12]: Open custom crafting gui = draw the gui on [Player12] screen

 

Did I understand it correctly?

Also where am I supposed to write this code? do you know of any github with a good example that I can have a look at?

Link to comment
Share on other sites

How good are you with Java?

 

Not Too good I'm afraid...

I can read and understand the basics

 

Let me explain you server-client stuff a bit [...]

 

Okay thanks, I think I start to understand how it works, however, I am not sure how I am to use this in my code.

So I need the client to check for input, then tell the server that the Player X pressed this key. Then the server figures out what to do, then tells the client what to do?

 

Example of how I understood it:

- CLIENT [Player12]: (send to SERVER) [Player12] pressed R

- SERVER: Pressing R means to open the custom crafting gui

- SERVER: (send to CLIENT) Open the custom crafting gui for [Player12]

- CLIENT [Player12]: Open custom crafting gui = draw the gui on [Player12] screen

 

Did I understand it correctly?

Also where am I supposed to write this code? do you know of any github with a good example that I can have a look at?

Yes, nearly:

Well, in case of minecraft it is way simplier with guis: you don't need to send packets to player to open it. Using forge, create a gui handler, GuiContainer and Container. They will automatically take care of syncing. There are a lot of tutorials for that. It's also written in these tutorials how to open these guis.

 

Container is server element that is not drawn and just represents data, logicall and syncing methods...

GuiContainer is client element, gui, that is "attached" to server container element, which is continuously synced with it...

 

There are tons and tons of tutorials about guis, containers and gui handlers...

 

 

Link to comment
Share on other sites

Yes, nearly [...]

 

Well I used GUI as an example, but I'll definitely check some tutorials out.

 

In my case I wanted Key Binding, and I haven't found any clear tutorials, which explains it in a comprehensive manner (or that is updated) for someone who isn't too familiar/comfortable/good at java/minecraft modding....

 

I consider what I want really simple, yet it's not that simple to make it (unless you're familiar with it):

What I believe I need is that the player presses a button, it tells the server that a button was pressed, it then checks if the player has the teleport item in hand/currently used slot, then tells the client to 'reload' that item/to remove the NBTTag from it.

 

However, I do not know where I am supposed to put it... Do I need more/new classes? Do I have to do something with my ClientProxy.java and CommonProxy.java since this is between the client and the server (or am i completely of track here?). This might be easy/basic/comprehensive for others, but I'm a bit lost, and I need guidance!

 

Anyhow, I appreciate all the help, but I'm not sure we are on the same page here! (Or maybe I'm just to stupid to see that they are related...)

 

Link to comment
Share on other sites

Yes, nearly [...]

 

Well I used GUI as an example, but I'll definitely check some tutorials out.

 

In my case I wanted Key Binding, and I haven't found any clear tutorials, which explains it in a comprehensive manner (or that is updated) for someone who isn't too familiar/comfortable/good at java/minecraft modding....

 

I consider what I want really simple, yet it's not that simple to make it (unless you're familiar with it):

What I believe I need is that the player presses a button, it tells the server that a button was pressed, it then checks if the player has the teleport item in hand/currently used slot, then tells the client to 'reload' that item/to remove the NBTTag from it.

 

However, I do not know where I am supposed to put it... Do I need more/new classes? Do I have to do something with my ClientProxy.java and CommonProxy.java since this is between the client and the server (or am i completely of track here?). This might be easy/basic/comprehensive for others, but I'm a bit lost, and I need guidance!

 

Anyhow, I appreciate all the help, but I'm not sure we are on the same page here! (Or maybe I'm just to stupid to see that they are related...)

NBT is logic, so it must happen on server... Always modifiy nbt on server! If it's inventory item (other cases exists), server will automatically sync item (&nbt) to client!!!

Link to comment
Share on other sites

NBT is logic, so it must happen on server... Always modifiy nbt on server! If it's inventory item (other cases exists), server will automatically sync item (&nbt) to client!!!

 

Okay stupid question then, how do i make it do it on the server?

 

Would it be adding

@SideOnly(Side.SERVER)

as you would add

@SideOnly(Side.CLIENT)

Link to comment
Share on other sites

You can't (and shouldn't!) just add @SideOnly to cause something to happen on one side or the other - that annotation actually strips the code below it from the jar when it is on the other side, so (Side.CLIENT) stuff doesn't even exist in the server jar. Side.SERVER is ONLY for the dedicated server, not even the integrated server (which is what is used in single player), and I haven't seen a use case yet in my own mods where it is ever needed.

 

Long story short: NEVER use @SideOnly annotation. Use world.isRemote to check for which side your code is on if you don't already know - when it is true, you are on the client side, when it is false, you are on the server side.

 

Here is some more information on packets.

Here is some information on using events (e.g. KeyInputEvent).

Here is an example of sending a packet to the server. Be sure to also check out the actual packet class for how it processes, and keep in mind that the server and client proxies must both implement a few methods to return the player / main thread, as explained in the packet tutorial linked earlier.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
×
×
  • Create New...

Important Information

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