Jump to content

[1.12] Problem with PlayerInteractEvent.RightClickBlock


Recommended Posts

Posted

Hi.

I'm trying to catch the RightClickBlock Event. Everything works fine, but what I didn't expect was that it also triggers if the player opens a chest; but I need it to only trigger if the block can't be used (so not Chests, Doors, Buttons...)
To be more detailed: I want to spawn an ItemMonsterPlacer (no NBT) whenever a player uses a ItemMonsterPlacer (with NBT) and an entity is spawned.

Another note: Everything has to be Server-Sided, because it's a Server-Only mod.

This is what I have:

@SubscribeEvent
	public static void onPlayerRightClickBlock(PlayerInteractEvent.RightClickBlock ev){
		World world = ev.getWorld();
		EntityPlayer entityPlayer = ev.getEntityPlayer();
		ItemStack itemStack = ev.getItemStack();
		BlockPos blockPos = ev.getPos();
		IBlockState iBlockState = world.getBlockState(blockPos);
		EnumFacing enumFacing = ev.getFace();

		if(itemStack.getItem() instanceof ItemMonsterPlacer){
			if(itemStack.getTagCompound() == null) return; //Filter Capture Items
			if(!entityPlayer.canPlayerEdit(blockPos.offset(enumFacing), enumFacing, itemStack)) return; //Filter if player edit, not use; see ItemMonsterPlacer
			if(world.getBlockState(blockPos).getBlock() == Blocks.MOB_SPAWNER) return; //Filter MOB_SPAWNER Blocks
			if(entityPlayer.capabilities.isCreativeMode) return; //Filter creative mode.

			entityPlayer.sendMessage(new TextComponentString("" + world.));

			world.spawnEntity(new EntityItem(world, blockPos.getX(), blockPos.getY()+2, blockPos.getZ(), new ItemStack(Items.SPAWN_EGG)));
			if(entityPlayer instanceof EntityPlayerMP)
				((EntityPlayerMP)entityPlayer).connection.sendPacket(new SPacketCustomSound("minecraft:entity.endermen.teleport", SoundCategory.AMBIENT, blockPos.getX(), blockPos.getY(), blockPos.getZ(), (float)1, (float)1));


			return;
		}

 

Many thanks in advance
Rurido :)

If my answer helped you out, you may give that answer a like. ^-^
Funny but true JavaScript statements: 

null != 0  null !<0  null !> 0    null <= 0  null >= 0  Number(null) == 0

Posted (edited)

The big problem is, that i can't create my own items without creating a client version of the mod, right? So that onItemUse(...){...} is not an option.


My last idea was - a rather ugly solution, but hey, it's one - to check manually if a block could handle the click action and decide if i have to spawn the item or not, like this:
 

/*previous return blocks*/

List<Block> usableBlocks = Arrays.asList(new Block[]{Blocks.CHEST, Blocks.ENDER_CHEST, Blocks.OAK_DOOR /*and so on...*/});
if(usableBlocks.contains(iBlockState.getBlock())) return;
  
/*spawn block*/

 

But i don't think this would do well when it comes to performance...

 

Opinion(s)?

Edited by Rurido

If my answer helped you out, you may give that answer a like. ^-^
Funny but true JavaScript statements: 

null != 0  null !<0  null !> 0    null <= 0  null >= 0  Number(null) == 0

Posted
10 minutes ago, Rurido said:

But i don't think this would do well when it comes to performance...

Two changes would make it really performant.

 

1) Declare the list once and save it to a field

2) Sort the list.

 

If the list is sorted, you can use Arrays.binarySearch to get the index of the item (or a value less than zero, which indicates its not in the array). This form of searching the array is extra fast because it can take any sorted array and treat it as a binary tree, making fewer total operations.

 

That said, I am not sure you can do a binary search on arbitrary types, but it might work.

  • Like 1

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.

Posted (edited)

I applied my idea and it seems to work.

I made the following changes:
 

@Mod.EventBusSubscriber
public class EventHandler {
	/*some stuff*/
	@SubscribeEvent
	public static void onPlayerRightClickBlock(PlayerInteractEvent.RightClickBlock ev){
		/*previous return blocks*/
		if(BlockUtil.isBlockUsableByPlayer(iBlockState)) return; //Filter right-clickable blocks
		/*spawn block*/
	}
}

public final class BlockUtil {

	public static final List<Block> usableBlocks = Arrays.asList(new Block[]{Blocks.CHEST, Blocks.ENDER_CHEST, Blocks.TRAPPED_CHEST, Blocks.OAK_DOOR, Blocks.ACACIA_DOOR, Blocks.BIRCH_DOOR,
			Blocks.DARK_OAK_DOOR, Blocks.JUNGLE_DOOR, Blocks.SPRUCE_DOOR, Blocks.TRAPDOOR, Blocks.ACACIA_FENCE_GATE, Blocks.BIRCH_FENCE_GATE, Blocks.DARK_OAK_FENCE_GATE,
			Blocks.JUNGLE_FENCE_GATE, Blocks.OAK_FENCE_GATE, Blocks.SPRUCE_FENCE_GATE, Blocks.POWERED_REPEATER, Blocks.UNPOWERED_REPEATER, Blocks.POWERED_COMPARATOR,
			Blocks.UNPOWERED_COMPARATOR, Blocks.STONE_BUTTON, Blocks.WOODEN_BUTTON, Blocks.STANDING_SIGN, Blocks.WALL_SIGN, Blocks.LEVER, Blocks.DAYLIGHT_DETECTOR,
			Blocks.DAYLIGHT_DETECTOR_INVERTED, Blocks.DROPPER, Blocks.DISPENSER, Blocks.FURNACE, Blocks.LIT_FURNACE, Blocks.ENCHANTING_TABLE, Blocks.HOPPER, Blocks.ANVIL,
			Blocks.BED, Blocks.BLACK_SHULKER_BOX, Blocks.BLUE_SHULKER_BOX, Blocks.BROWN_SHULKER_BOX, Blocks.CYAN_SHULKER_BOX, Blocks.GRAY_SHULKER_BOX, Blocks.GREEN_SHULKER_BOX,
			Blocks.LIGHT_BLUE_SHULKER_BOX, Blocks.LIME_SHULKER_BOX, Blocks.MAGENTA_SHULKER_BOX, Blocks.ORANGE_SHULKER_BOX, Blocks.PINK_SHULKER_BOX, Blocks.PURPLE_SHULKER_BOX,
			Blocks.RED_SHULKER_BOX, Blocks.SILVER_SHULKER_BOX, Blocks.WHITE_SHULKER_BOX, Blocks.YELLOW_SHULKER_BOX, Blocks.BREWING_STAND, Blocks.CRAFTING_TABLE});

	public static boolean isBlockUsableByPlayer(Block block){
		return usableBlocks.contains(block);
	}
  	
  	public static boolean isBlockUsableByPlayer(IBlockState iBlockState){
		return isBlockUsableByPlayer(iBlockState.getBlock());
	}
	/*some other stuff*/
}

 

12 hours ago, Draco18s said:

Declare the list once and save it to a field

DONE

 

Quote

Sort the list.  [... ]you can use Arrays.binarySearch

Should I do .sort() once or is there a way I can define it already sorted manually?

 

All Usable Blocks i found, did I miss one?

mc_usable_blocks.JPG

Edited by Rurido

If my answer helped you out, you may give that answer a like. ^-^
Funny but true JavaScript statements: 

null != 0  null !<0  null !> 0    null <= 0  null >= 0  Number(null) == 0

Posted
41 minutes ago, diesieben07 said:

use a Set. HashSet::contains (or better even ImmutableSet::contains)

Works fine!
 

Updated code:

	public static final ImmutableSet<Block> usableBlocks = ImmutableSet.of(Blocks.CHEST, Blocks.ENDER_CHEST, Blocks.TRAPPED_CHEST,
		Blocks.OAK_DOOR, Blocks.ACACIA_DOOR, Blocks.BIRCH_DOOR, Blocks.DARK_OAK_DOOR, Blocks.JUNGLE_DOOR, Blocks.SPRUCE_DOOR,
		Blocks.TRAPDOOR, Blocks.ACACIA_FENCE_GATE, Blocks.BIRCH_FENCE_GATE, Blocks.DARK_OAK_FENCE_GATE,  Blocks.JUNGLE_FENCE_GATE,
		Blocks.OAK_FENCE_GATE, Blocks.SPRUCE_FENCE_GATE, Blocks.POWERED_REPEATER, Blocks.UNPOWERED_REPEATER, Blocks.POWERED_COMPARATOR,
		Blocks.UNPOWERED_COMPARATOR, Blocks.STONE_BUTTON, Blocks.WOODEN_BUTTON, Blocks.STANDING_SIGN, Blocks.WALL_SIGN, Blocks.LEVER,
		Blocks.DAYLIGHT_DETECTOR, Blocks.DAYLIGHT_DETECTOR_INVERTED, Blocks.DROPPER, Blocks.DISPENSER, Blocks.FURNACE, Blocks.LIT_FURNACE,
		Blocks.ENCHANTING_TABLE, Blocks.HOPPER, Blocks.ANVIL, Blocks.BED, Blocks.BLACK_SHULKER_BOX, Blocks.BLUE_SHULKER_BOX,
		Blocks.BROWN_SHULKER_BOX, Blocks.CYAN_SHULKER_BOX, Blocks.GRAY_SHULKER_BOX, Blocks.GREEN_SHULKER_BOX, Blocks.LIGHT_BLUE_SHULKER_BOX,
		Blocks.LIME_SHULKER_BOX, Blocks.MAGENTA_SHULKER_BOX, Blocks.ORANGE_SHULKER_BOX, Blocks.PINK_SHULKER_BOX, Blocks.PURPLE_SHULKER_BOX,
		Blocks.RED_SHULKER_BOX, Blocks.SILVER_SHULKER_BOX, Blocks.WHITE_SHULKER_BOX, Blocks.YELLOW_SHULKER_BOX, Blocks.BREWING_STAND,
		Blocks.CRAFTING_TABLE);

	public static boolean isBlockUsableByPlayer(Block block){ return usableBlocks.contains(block); }

	public static boolean isBlockUsableByPlayer(IBlockState iBlockState){ return isBlockUsableByPlayer(iBlockState.getBlock()); }

 

If my answer helped you out, you may give that answer a like. ^-^
Funny but true JavaScript statements: 

null != 0  null !<0  null !> 0    null <= 0  null >= 0  Number(null) == 0

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.