Jump to content

[SOLVED]Checking item in hand and onRightClick


Rainfur
 Share

Recommended Posts

So, coming from a ModLoader background, I've discovered my previous method from 1.2.5 doesn't work at all. How would I:

 

1) Check what item is in the player's hand

2) How can I implement the Forge equivalent of ML's onRightClick

3) How can I get the block right clicked so I can modify it, replace it, etc.

 

 

Link to comment
Share on other sites

From 1.2.5 ?! It won't be easy for you to get used to everything with Forge now...

 

ALL of those are not a part of forge as far as I'm aware, they are a part of MC it self.

 

Modloader "equivalent" of "onRightClick":

Function: onItemRightClick, From: Item.class


public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
    {
        return par1ItemStack;
    }

 

Check what item is in the player's hand:

 

You need a function with acces to EntityPlayer/Minecraft, after you found something that suits you:

 

thePlayerInstance.getCurrentEquippedItem()

 

Getting a block that is right click:

Function: boolean onBlockActivated(World par1World, int par2, int par3, int par4, EntityPlayer par5EntityPlayer, int par6, float par7, float par8, float par9)

Found in: Block.class

 

Get the block in the position the player right-clicked in and then do whatever you want.

Link to comment
Share on other sites

Didn't help a whole lot, maybe this will help:

 

I need to check what item is in the player's hand. If it is (insert item here), check if it is right clicked on a block. If it is, then check to see if the block is (insert block here). If it is, replace said block with (insert block here).

 

 

Link to comment
Share on other sites

Look at the ItemHoe class. It seems like you are trying to do just about the same thing that the hoe does, by checking to see if the block clicked on is dirt, and if it is, it turns it into tilled earth. Just a question, is the item that you are clicking with an item you made or a vanilla item?

If you really want help, give that modder a thank you.

 

Modders LOVE thank yous.

Link to comment
Share on other sites

I need to check what item is in the player's hand. If it is (insert whatever item here), check if it is right clicked on a block. If it is, then check to see if the block is (insert whatever block here). If it is, replace said block with (insert whatever block here).

 

 

Link to comment
Share on other sites

I'm not sure how to do this without editing base files if your item is vanilla, but if the item is an item you made yourself, then just use the vanilla code from the hoe file. Do you have any code so far in your class?

If you really want help, give that modder a thank you.

 

Modders LOVE thank yous.

Link to comment
Share on other sites

What I thought I could do is setup a MouseListener for right clicks, then check the item in the player's hand. (mc.thePlayer.something perhaps). Not sure how I'd get the block right clicked though. Any suggestions? Or the block replacement.

Link to comment
Share on other sites

So, coming from a ModLoader background, I've discovered my previous method from 1.2.5 doesn't work at all. How would I:

 

1) Check what item is in the player's hand

2) How can I implement the Forge equivalent of ML's onRightClick

3) How can I get the block right clicked so I can modify it, replace it, etc.

 

This is quite easy to do, if you look into Item.java then you see some methods which every item inherits like this one:

/**
     * Called whenever this item is equipped and the right mouse button is pressed. Args: itemStack, world, entityPlayer
     */
    public ItemStack onItemRightClick(ItemStack itemStack, World world, EntityPlayer player)
    {
        return itemStack;
    }

 

There you have access to the player, so you can see all the info about him and the item being used.

The last question you can do from the clientside by grabbing the mouseOverObject from the minecraft.java which is the result of a raytrace to find what the player is looking at returning the information as a MovingObject(...) (forgot the whole name) which will let you see all the info about it (x,y,z, block ID, sideHit etc.)

 

That should be all you need assuming the rest of your setup is updated to 1.3.X+ standards. If you are not familiar with them you should first start by looking into the basics of them, I recommend this article for that purpose:  http://www.minecraftforge.net/wiki/Basic_Modding

 

 

If you guys dont get it.. then well ya.. try harder...

Link to comment
Share on other sites

That helped, but my item(s) are vanilla. I would like to just use a MouseListener from OpenGL, would that work and just check the item when it detects something then?

 

Could you also explain a bit more on the ray tracing method? Didn't follow very well there.

Link to comment
Share on other sites

Inside Minecraft.java you will find the above mentioned field it contains whatever the player is looking towards which is within his range. You will understand once you see the field and what info it contains. Also you may want to open it's call hierarchy and see the get function for it to see how it's made :)

 

As long as you got access to a player object, you can find out which item he is holding by a simple method call, you will find this inside the player's class.

 

As long as you can get the player object then sure.

Since you are already on client side, you can use the Minecraft.java's thePlayer field to get the player object.

 

 

If you guys dont get it.. then well ya.. try harder...

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

 Share



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • I am using a modified MeleeAttackGoal to make an entity pick a random button, go to it and press it. But after a while it just stops and stares at the current button target. I found out that it's just staring at it because it's too far away to press it.  And its's not moving because the current path in the mob navigation is "done". If I move it further away it recalculates a non weird path and fixes itself. If I push it towards the button so it can press it, it sets the target to null, so it once again calculates a new path and fixes itself. What is causing the path to be "done"? And how can I fix this? This is the tick method in the custom goal class: public void tick() { if (mob.buttonTarget != null) { float x = mob.buttonTarget.getX(); float y = mob.buttonTarget.getY(); float z = mob.buttonTarget.getZ(); float hx = x + 0.5f; float hy = y + 0.5f; float hz = z + 0.5f; mob.getLookControl().setLookAt(hx, hy, hz, 30.0F, 30.0F); double distSqr = mob.distanceToSqr(hx, hy, hz); ticksUntilNextPathRecalculation = Math.max(ticksUntilNextPathRecalculation - 1, 0); if ((followingTargetEvenIfNotSeen || hasLineOfSight(mob, mob.buttonTarget)) && ticksUntilNextPathRecalculation <= 0 && ((pathedTargetX == 0.0D && pathedTargetY == 0.0D && pathedTargetZ == 0.0D) || mob.buttonTarget.distSqr(new Vec3i(pathedTargetX, pathedTargetY, pathedTargetZ)) >= 1.0D || mob.getRandom().nextFloat() < 0.05F)) { pathedTargetX = hx; pathedTargetY = hy; pathedTargetZ = hz; ticksUntilNextPathRecalculation = 4 + mob.getRandom().nextInt(7); if (distSqr > 1024.0D) ticksUntilNextPathRecalculation += 10; else if (distSqr > 256.0D) ticksUntilNextPathRecalculation += 5; Path newPath = mob.getNavigation().createPath(mob.buttonTarget, 0); if (newPath != null && !newPath.canReach()) { mob.buttonTarget = null; return; } if (!mob.getNavigation().moveTo(newPath, speedModifier)) ticksUntilNextPathRecalculation += 15; ticksUntilNextPathRecalculation = adjustedTickDelay(ticksUntilNextPathRecalculation); } ticksUntilNextAttack = Math.max(getTicksUntilNextAttack() - 1, 0); checkAndPerformAttack(mob.buttonTarget, distSqr); } }  
    • As of Forge version 44.1.5 and above, Minecraft has started crashing whenever I played it with the Create mod. Without any mods, it loads normally, but when I put in a mod, like MrCrayfish's furniture mod for example, it fails to read the mod properly. Basically I press play and it loads. When the launcher closes, it opens back up again with an Exit Code 1 error, stating that the game crashed due to an unexpected error. Here's my latest.log for more information. https://pastebin.com/pUPBjpbr  
    • The files are harmed by system flaws and glitches. Additionally, the installation procedure might cause files to be lost. The game is so terminated, and an exit code 1 error is displayed. Therefore, attempt a game reinstall.
    • Hello there!  I'm hosting a Minecraft Tekxit PI server on Minecraft 1.12.2 on my computer and I have a little issue. Everything was going great until one day my light in my house went down while a friend was inside the server. The server crashed and when I came back 2 chunks swapped but everything seemed normal. Fastforward in time my friend who was inside the server tried to come back in it but everti-me he tries to join he cannot do it and there is a crash in the server logs. If any other friend tries to join they can, but, that friend that was inside the server when that occurred cannot join. I tried rebooting, rebooting my modem, deleting his player data to start over and nothing works. I'm leaving the crash logs here. Thank you!!! ----------------------------------------------- CRASH LOGS   [10:27:10] [Netty Server IO #1/INFO]: Transforming class 'gu', alias 'net.minecraft.network.NettyCompressionDecoder' [10:27:10] [Netty Server IO #1/INFO]: Scanning method 'decode(Lio/netty/channel/ChannelHandlerContext;Lio/netty/buffer/ByteBuf;Ljava/util/List;)V' [10:27:10] [Netty Server IO #1/INFO]: Patching constant (java.lang.Integer) 2097152 [10:27:10] [Netty Server IO #1/INFO]: Patching constant (java.lang.Integer) 2097152 [10:27:10] [Netty Server IO #1/INFO]: [com.creativemd.creativecore.transformer.CreativeTransformer:transform:49]: [littletiles] Patched net.minecraft.network.NettyCompressionDecoder ... [10:27:13] [Server thread/ERROR]: HandshakeMessageHandler exception java.lang.OutOfMemoryError: Java heap space     at java.util.ArrayList.<init>(Unknown Source) ~[?:1.8.0_361]     at com.google.common.collect.Lists.newArrayListWithCapacity(Lists.java:190) ~[minecraft_server.1.12.2.jar:?]     at net.minecraft.nbt.NBTTagList.read(NBTTagList.java:58) ~[ge.class:?]     at net.minecraft.nbt.NBTTagCompound.readNBT(NBTTagCompound.java:497) ~[fy.class:?]     at net.minecraft.nbt.NBTTagCompound.read(NBTTagCompound.java:58) ~[fy.class:?]     at net.minecraft.nbt.NBTTagCompound.readNBT(NBTTagCompound.java:497) ~[fy.class:?]     at net.minecraft.nbt.NBTTagCompound.read(NBTTagCompound.java:58) ~[fy.class:?]     at net.minecraft.nbt.CompressedStreamTools.read(CompressedStreamTools.java:133) ~[gi.class:?]     at net.minecraft.nbt.CompressedStreamTools.read(CompressedStreamTools.java:88) ~[gi.class:?]     at net.minecraft.nbt.CompressedStreamTools.readCompressed(CompressedStreamTools.java:31) ~[gi.class:?]     at net.minecraft.world.storage.SaveHandler.getPlayerNBT(SaveHandler.java:278) ~[bfb.class:?]     at net.minecraft.server.management.PlayerList.getPlayerNBT(PlayerList.java:340) ~[pl.class:?]     at net.minecraftforge.fml.common.network.handshake.NetworkDispatcher.serverInitiateHandshake(NetworkDispatcher.java:220) ~[NetworkDispatcher.class:?]     at net.minecraftforge.fml.common.network.handshake.FMLHandshakeServerState$1.accept(FMLHandshakeServerState.java:49) ~[FMLHandshakeServerState$1.class:?]     at net.minecraftforge.fml.common.network.handshake.HandshakeMessageHandler.userEventTriggered(HandshakeMessageHandler.java:65) ~[HandshakeMessageHandler.class:?]     at io.netty.channel.AbstractChannelHandlerContext.invokeUserEventTriggered(AbstractChannelHandlerContext.java:329) ~[minecraft_server.1.12.2.jar:?]     at io.netty.channel.AbstractChannelHandlerContext.invokeUserEventTriggered(AbstractChannelHandlerContext.java:315) ~[minecraft_server.1.12.2.jar:?]     at io.netty.channel.AbstractChannelHandlerContext.fireUserEventTriggered(AbstractChannelHandlerContext.java:307) ~[minecraft_server.1.12.2.jar:?]     at io.netty.channel.ChannelInboundHandlerAdapter.userEventTriggered(ChannelInboundHandlerAdapter.java:108) ~[minecraft_server.1.12.2.jar:?]     at io.netty.channel.AbstractChannelHandlerContext.invokeUserEventTriggered(AbstractChannelHandlerContext.java:329) ~[minecraft_server.1.12.2.jar:?]     at io.netty.channel.AbstractChannelHandlerContext.invokeUserEventTriggered(AbstractChannelHandlerContext.java:315) ~[minecraft_server.1.12.2.jar:?]     at io.netty.channel.AbstractChannelHandlerContext.fireUserEventTriggered(AbstractChannelHandlerContext.java:307) ~[minecraft_server.1.12.2.jar:?]     at io.netty.channel.ChannelInboundHandlerAdapter.userEventTriggered(ChannelInboundHandlerAdapter.java:108) ~[minecraft_server.1.12.2.jar:?]     at io.netty.channel.AbstractChannelHandlerContext.invokeUserEventTriggered(AbstractChannelHandlerContext.java:329) ~[minecraft_server.1.12.2.jar:?]     at io.netty.channel.AbstractChannelHandlerContext.invokeUserEventTriggered(AbstractChannelHandlerContext.java:315) ~[minecraft_server.1.12.2.jar:?]     at io.netty.channel.AbstractChannelHandlerContext.fireUserEventTriggered(AbstractChannelHandlerContext.java:307) ~[minecraft_server.1.12.2.jar:?]     at io.netty.channel.DefaultChannelPipeline$HeadContext.userEventTriggered(DefaultChannelPipeline.java:1352) ~[minecraft_server.1.12.2.jar:?]     at io.netty.channel.AbstractChannelHandlerContext.invokeUserEventTriggered(AbstractChannelHandlerContext.java:329) ~[minecraft_server.1.12.2.jar:?]     at io.netty.channel.AbstractChannelHandlerContext.invokeUserEventTriggered(AbstractChannelHandlerContext.java:315) ~[minecraft_server.1.12.2.jar:?]     at io.netty.channel.DefaultChannelPipeline.fireUserEventTriggered(DefaultChannelPipeline.java:920) ~[minecraft_server.1.12.2.jar:?]     at net.minecraftforge.fml.common.network.handshake.NetworkDispatcher.insertIntoChannel(NetworkDispatcher.java:183) ~[NetworkDispatcher.class:?]     at net.minecraftforge.fml.common.network.handshake.NetworkDispatcher.serverToClientHandshake(NetworkDispatcher.java:154) ~[NetworkDispatcher.class:?]
    • (Mod name is Custom Npcs)
  • Topics

×
×
  • Create New...

Important Information

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