Skip to content

Translation notice

This page was translated with machine translation and may contain inaccuracies. If you can help improve it, please open an issue or submit a pull request.

Commonly used right-click detection

by CR_019
This article was also published in Void Spirit Forum and BiliBili column

Right-click detection is a classic problem of data pack. With updates, there are more and more ways to implement right-click detection. Here is a brief introduction to several commonly used right-click detection methods. They each have their own advantages, and you can choose the appropriate method to use in actual development.

Of course, I also know that no one likes to see large sections of code, so I made a sample data pack. Interested players can download and unpack it for study. In the text, I will only briefly describe the principles and show as little large sections of code as possible.

Basic right-click detection

carrot fishing rod method

  • Applicable version: 1.13+
  • Features: The simplest and most versatile, but will attract pigs
  • Applicable scenarios: simple right-click

Brief description of principle

The carrot fishing rod (and the weird fungus fishing rod) is the most common right-click detection solution. The principle is that after pressing the button to use it, even if it is not used on the pig,used:carrot_on_a_stickStatistical items will also be accumulated, and corresponding scoring items will be accumulated simultaneously.

In actual load, the player can use the specified props by combining two criteria:

  1. player holds item of specific nbt
  2. The player's scoreboard has a count

So we can create a new oneused:carrot_on_a_stickA scoreboard for the criteria, and changes to the scoreboard are detected in the tickfunction. When a change in the scoreboard is detected, it means that the player held the fishing rod and pressed the right button. At this time, you can add the judgment of the item in the player's hand. If the item is specified, the function operation after pressing the item will be executed.

Other discussions

  • When we long press the carrot fishing rod, the scoreboard will trigger again after 4 ticks (actually 0.2s, it will still trigger in 0.2s after changing the tick rate); therefore, the long press detection of the carrot fishing rod is discontinuous, which is not very suitable in most cases.
  • But the carrot fishing rod has an interesting but not necessarily practical feature: it is not considered a "used" item when right-clicked. "Use" here refers to operations such as eating food, which may have some additional actions and sound effects, and when using an item, you cannot use the left button to dig blocks at the same time;
    • Therefore, the carrot fishing rod may be the only item that can detect the right click without blocking the mining block operation, which may be useful in some extreme cases.

interactive entity

  • Applicable version: 1.19.4+
  • Features: Can detect left and right keys at the same time, but will swallow other operations
  • Applicable scenarios: Fixed position buttons, completely rewriting left and right key operations

Brief description of principle

Interactive entities, as you can see at a glance, are used to handle interactive events. After the interactive entity is left or right clicked by the player, the player's UUID and the timestamp of the click will be stored inattack(left click) andinteraction(right click) tag.

Principle: useplayer_hurt_entity(left click) andplayer_interacted_with_entity(Right click) These two advancement triggers monitor the interaction between the player and the interactive entity and trigger a series of behaviors. Within advancement, you can check whether the interactive entity has specific data to mark different interactive entities.

If we only need to trigger a series of operations with the player as the executor, we can end here. But if we need to use the interactive entity as the executor, we also need to find the interactive entity being interacted with, which requires additional steps.

We store the player's UUID in temporary storage, and use the interactive entity near the player as the executor. Check that the interactive entity corresponds to the UUID in the NBT. If it matches, use the interactive entity as the executor to execute the corresponding instructions.

Match UUID: Pass the playerUUID in storage/dataCopied to the interactive entity, if the modification is successful (execute store resultReturning 1) means different, and failing (returning 0) means the same.

Of course, in the end, remember to clear the UUID in nbt (set to [0,0,0,0]).

Other discussions

  • After all, the display entity is not like the item. It is another entity independent of the player. Therefore, it is more suitable to be used as an NPC or button, fixed at a specific location on the map.
    • If you want to use it like an item, a simple way is to put it on your head and synchronize it with the player's position through tp or other methods;
    • However, this will cause a problem, that is, if the player can also perform other left and right key operations, the interactive entity operation will be triggered first, so that other operations cannot be triggered;
    • Therefore, this approach is suitable for highly customized maps that rewrite all various operations and use them as the triggering interface for left and right key events.
  • The right-click behavior of the interactive entity is consistent with the carrot fishing rod, which is triggered once every 4 ticks; the left-click behavior is consistent with the interactive behavior of attacking other mobs, which is triggered once with a single click, and has no effect if long-pressed;
    • Therefore, the past methods of detecting left clicks, such as generating villagers on the player's face, can be replaced.

consumable component

  • Applicable version: 1.21+
  • Features: More flexible, triggered by 1tick, need to process the returned item
  • Applicable scenarios: Various click and long press scenarios

Brief description of principle

New in 1.21consumableComponent, which can be used to detect the player's use of items. Items with this component can be used by right-clicking and will be consumed after the specified time.

The behavior of consuming items is availableconsume_itemadvancement trigger detection, therefore, we can putconsume_secondsSet to 0 and useuse_remainderThe component sets a special return item, so that we get an item that can be consumed instantly. Then, we use the above-mentioned advancement trigger to monitor the consumption behavior, and trigger the right-click related function and replacement function at the same time, detect the returned item and replace it. A simple method is used in the sample data pack.

However, it should be noted that at the moment when advancement is triggered, the returned item has not yet reached the player backpack, and direct replacement will fail; you need to use/scheduleThe command must be delayed by 1 tick to succeed.

Other discussions

  • In the past, when this component was not available, we would use the Eye of Ender for right-click detection.
    • The principle is that when there is no dungeon, the Eye of Ender cannot be thrown, but it can be used continuously in the hand.
    • At this timeusing_itemThe advancement trigger can capture usage behavior and trigger related functions.
    • And nowconsumableComponents can completely replace this work, so this method can also be used for detection in dungeon environments.
  • In fact, by settingconsume_secondsWe can also use this component to detect long presses. We'll get to that in a minute.

Advanced interaction detection

Benefit fromconsumableComponent, we can perform a variety of rich right-click detection. Such as long press, etc.

Long press trigger

when we putconsume_secondsSet it to a relatively long value. In this case, useusing_itemThe advancement trigger can continuously monitor the player's long press behavior; the advancement will be triggered once every tick, and can be used to create particle special effects and other behaviors;

Long press to charge

Sometimes we may want a prop that requires the player to press and hold it for a period of time before it can be triggered. If it is released before the specified time, it will have no effect. At this time, we can use a method similar to the one above. The only difference is thatconsume_secondsSet to how long you want the charge to last, so that the effect is triggered after holding down for the specified time.

Press and hold to loosen hair

Another requirement is to trigger the effect when released, similar to the charging of a bow; this effect is a little more complicated. We need to set up two scoreboards, one of which saves the long press status of this tick (marked as cur), and the other saves the long press status of the previous tickplayer (marked as lt);

In terms of implementation, we can assign the value of cur to lt at the end of tickfunction;

In the normal state, we set the value of cur to 0; in the state where the player presses the right button (useusing_itemadvancement trigger detection), we set the value of cur to 1;

Then detect in the tick: when the value of cur is 0 (currently not pressed) and the value of lt is 1 (pressed in the previous tick), we think that the player has released the prop, and the corresponding function is triggered.

Of course, the above detection methods do not conflict with each other, and we can use them in combination depending on the situation. For example, this video, releasing and holding the button for 5 seconds will trigger ejection.

Example data pack usage

Sample data pack download: sample.zip

Each namespace corresponds to a module mentioned in the article; for examplecarrot_on_a_stickModule corresponding to carrot fishing rod;

In the namespace, use givefunction to obtain the corresponding item, and the behavior of the item is described in the item's lore; for interactive entities, it is summonfunction, and the behavior is displayed in the dialog using tellrawcommand.

The other parts are functional implementations, and you can study the principles or copy them directly for use. I tried my best to write some notes, but it may not be comprehensive enough. It is recommended to study it in conjunction with this article.

Powered by VitePress and GitHub Pages