Play Strawberry Docs
Scripts

Displays and interactions

Show text, blocks and items with display entities, animate them together, attach parts to entities and use interaction entities as click targets.

Display and interaction entities

There are text, block and item display entities, and interaction entities that serve as click targets. They are real entities in the world: every player near them sees them.

The SDK adds EntityDisplayExt (as_display, as_text_display, as_block_display, as_item_display, as_interaction on an Entity), DisplayEntityExt, TextDisplayEntityExt, ItemDisplayEntityExt, TransformationBuilder, BillboardMode, ItemDisplayMode and TextAlignment.

Keep transformations, rotations, interpolation times and sizes bounded and finite. If you cache display templates, give the cache a size limit and clear it on unload. For what a Bedrock player sees, see What Bedrock players see.

Animate several displays together

For a character made of several displays, group them in a rig: DisplayRig::create(&pieces) takes display entities of one world. rig.apply(&poses, interpolation_ticks) gives every piece named in poses its next transformation in one call. Each Java player who sees the rig receives that keyframe at once, so all pieces start gliding in the same client tick instead of drifting apart.

rig.piece_count() says how many pieces are left. A removed piece leaves the rig, a rig with no piece left has ended, and dropping the rig leaves the pieces where they are.

Let the server play an animation

  1. Add each animation once with rig.add_animation(&animation). A RigAnimation has a name, a length in ticks and, for each piece, a RigTrack of RigKeyframes.
  2. Start one by name with rig.play(name, looped).
  3. End it with rig.stop().

The server moves the pieces on its own ticks, so your script makes no call per keyframe, and a player who starts to see the rig sees the pose it has at that moment. While an animation plays, apply is refused.

Limits

  • A rig holds at most 64 pieces, a script at most 512 rigs, and a rig takes at most 20 keyframes a second (one per tick).
  • An animation has a name of at most 64 bytes and at most 12,000 ticks and 65,536 keyframes; a rig holds at most 64 animations.

Over a limit the call returns the reason with the number that was too high, and nothing is sent.

Attach parts to an entity

display::attach_part(&wearer, viewers, part) attaches an item, block or text display to any entity, for one player (One(uuid)) or for every player but the wearer (AllOthers).

  • The part exists only in those players' games. No world holds it, so the game, other scripts and saves see the entity as it is.
  • It appears and disappears with the entity, rides along while the entity moves without any call from your script, and turns with the entity's body or head (PartFollow).
  • It ends with your script, with the entity, or with the one player it is for.
  • An entity carries at most 64 parts of all scripts.

detach_part, detach_parts and get_parts manage your own parts. set_part_own_view lets the wearer see a part on themself too.

Name tags

A player who carries parts keeps the name tag: the server draws it above the parts, as that player's game would (the team's prefix, colour and suffix, the line below the name, sneaking, invisibility, the team's name tag rule). Any other entity that carries a part shows no name tag in a Java game.

Bedrock players see a text part as a floating name, and an item or block part held by a display figure (on servers whose Bedrock pack carries it), riding the entity and turning with its body.

Parts on a player's head

display::attach_head_part(&player, viewers, part, place) puts a part on a player's head.

  • Its transformation is measured from the neck (the centre of the bottom of the head), and it turns with the head as the player looks around, up and down too. part.follow must be PartFollow::Head.
  • It moves down when the player sneaks, and nothing is drawn while the player swims, glides or sleeps.
  • With a place (HeadPlace::Hat or HeadPlace::Face), the part is hidden while the head slot holds an item, such as a real helmet.

A hat made of item, block and text parts that use models already in the pack adds nothing to the pack. The part's id works with detach_part, get_parts and set_part_own_view. Bedrock players see it at the head, turning with the body.

The name sits 3 px above the highest part. The server works the height out from each part's box and transformation. A pack's model can be drawn anywhere, and the server does not read packs, so give its box with display::set_part_box(&player, id, min, max): the box the model is drawn in, in the part's own space, in blocks. Then a tall hat never covers the name.

Spawn particles with options

world.spawn_particle_with_options(options, pos, offset, speed, count, long_distance, always_show) spawns particles of any type with the data their type carries. player.spawn_particles_with_options(options, pos, count, offset, speed, long_distance, always_show) shows them to one player only. Paper's world.spawnParticle(Particle.DUST, location, count, dx, dy, dz, speed, new Particle.DustOptions(Color.RED, 2.0f), force) becomes:

rust
use strawberry_plugin_api::particle::{ParticleOptions, rgb};

world.spawn_particle_with_options(
    ParticleOptions::dust(rgb(0xFF0000), 2.0),
    (x, y, z),
    (dx, dy, dz),
    speed,
    count,
    false, // long_distance
    false, // always_show
)?;
  • Types without data use ParticleOptions::plain(Particle::Flame) or Particle::Flame.into().
  • Each type with data has its own case: dust (a colour and a size), dust_transition, block (a block state), item (an item stack with its components), entity_effect (a colour with transparency, argb(0xAARRGGBB)), effect, trail, vibration_to_block and vibration_to_entity.
  • long_distance is Paper's force: players further away get the particles too. always_show ignores the player's particle setting.
  • If the data cannot be drawn, the call sends nothing and returns the reason. Counts, durations and dust sizes outside their range are brought into range. The item stack you pass belongs to the call.
  • Bedrock players see the particles Bedrock has an effect for, without their data.

Vehicle seats

  • get_passenger_attachment_point(index) says where a vehicle carries a passenger, measured from the vehicle's feet.
  • add_seat(offset) adds a seat at your own offset, at most 16 blocks on each axis and 16 seats per vehicle. A player or any entity rides the seat like any vehicle, and is dismounted when the vehicle goes. Seats are not saved with the world.
  • set_follow_vehicle_yaw(true) makes a display that rides a vehicle turn with it.

A player is put first among a vehicle's riders when the first rider is not a player, with add_passenger, set_vehicle or /ride. So get_passengers lists the player first, and a player who gets into a boat that carries a mob steers it. Other riders are added at the end.

Interaction entities and clicks

An interaction entity is a click target. Before you change an inventory, a reward or money, check the player, the permission, the target and the action. An entity id alone is not proof of anything: entities are removed and ids reused.

Which hand clicked

events::event_hand() (Paper's getHand) answers for the player interact event and the interact entity events, the unknown entity one included. right is the main hand and left the off hand. A left click, an attack and a Bedrock player's interaction use the main hand.

The player interact event fires once for each use of a hand that the client sends, for Java and Bedrock players:

  • A right click on a block fires it for that hand with the block and its face (right-click-block). The item use the client sends next for the same block, hand and item does not fire it again and takes that event's answer.
  • A use of an item aimed at the air fires it for its hand (right-click-air).
  • So a handler that guards an item reads the stack of the event's hand (events::event_hand_item()), not the main hand's.
  • Cancelling stops everything that click would have done for that hand (the block's use and the item's use), and the client is sent the block and its hand again. The other hand's event has its own answer.
  • If a handler changes the item in the hand, the item that is used is the one the hand holds afterwards.

Clean up

Removing the entity and dropping your handle are different things. Remove the displays you spawned when the feature ends and in on_unload.

On this page