Jump to content

[1.7.10] Can't get PlayerUseItemEvent to work


Silly511

Recommended Posts

I am trying to make it where when you right click a block with a keybind held down, then it places a block. But when I right click a block it does nothing. Heres my code:

 

        @SubscribeEvent
public void onItemUse(PlayerUseItemEvent event) {
	if (KeyBindings.Ability.isPressed()) {
		MovingObjectPosition mop = WorldHelper.getBlockLookingAt(event.entityPlayer);
		if (mop != null) {
			ForgeDirection side = ForgeDirection.getOrientation(mop.sideHit);

			if (event.entityPlayer.worldObj.getBlock(mop.blockX, mop.blockY, mop.blockZ) == Blocks.stone) {
				event.entityPlayer.worldObj.setBlock(mop.blockX, mop.blockY, mop.blockZ, EnchantedAuraBlocks.Spectistone);
				event.entityPlayer.worldObj.playAuxSFX(2001, mop.blockX, mop.blockY, mop.blockZ, Block.getIdFromBlock(event.entityPlayer.worldObj.getBlock(mop.blockX, mop.blockY, mop.blockZ)) + (0 << 12));
			} else {
				event.entityPlayer.worldObj.setBlock(mop.blockX + side.offsetX, mop.blockY + side.offsetY, mop.blockZ + side.offsetZ, EnchantedAuraBlocks.Spectiglass);
			}
		}
	}
}

 

WorldHelper.getBlockLookingAt:

        public static MovingObjectPosition getBlockLookingAt(EntityLivingBase entity) {
	Vec3 vec3 = entity.getPosition(1.0F);
	Vec3 vec31 = entity.getLook(1.0F);
                Vec3 vec32 = vec3.addVector(vec31.xCoord * 200, vec31.yCoord * 200, vec31.zCoord * 200);
                return entity.worldObj.func_147447_a(vec3, vec32, false, false, true);
}

 

What am I doing wrong?

Link to comment
Share on other sites

The subclasses of

PlayerUseItemEvent

are only fired for items that used while right click is held, e.g. charging/firing bows, eating food, blocking with swords.

 

PlayerInteractEvent

is fired with

Action

of

RIGHT_CLICK_BLOCK

when the player right clicks a block.

 

KeyBinding

s are client-only, but any persistent changes to the world (e.g. placing blocks) must be made from the server side. You need to send a packet to the server when the player right clicks a block with the key held. When the server receives this packet, check if the player is allowed to place the block (e.g. the target position is in range, the player has unlocked this ability) and then place it.

 

This site explains networking, including how to use the Simple Network Wrapper (which you'll need to use here).

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

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.