Skip to content

Instantly share code, notes, and snippets.

@SemmieDev
Last active March 26, 2024 15:32
Show Gist options
  • Save SemmieDev/d06525beacf9785b29d1fdc62f2f326c to your computer and use it in GitHub Desktop.
Save SemmieDev/d06525beacf9785b29d1fdc62f2f326c to your computer and use it in GitHub Desktop.
Trial spawner technical breakdown

Trial spawner technical breakdown

Config

The config can be changed using NBT, and the config of trial spawners inside trial chambers tend to have different values then these defaults. These defaults only apply if the option is not defined in the NBT.

  • required_player_range: 14 blocks
  • spawn_range: 4 blocks
  • total_mobs: 6
  • simultaneous_mobs: 2
  • total_mobs_added_per_player: 2
  • simultaneous_mobs_added_per_player: 1
  • ticks_between_spawn: 40
  • target_cooldown_length: 36000 ticks
  • spawn_potentials: none
  • loot_tables_to_eject: none

States

Inactive

The trial spawner is only in the inactive state when it doesn't have a mob inside it. If it does, it goes to the waiting for players state.

Waiting for players

If there are any survival or adventure mode players within the range defined in the required_player_range option, the trial spawner will switch to the active state.

Active

First the trial spawner will check for any players that are within the range stated in the required_player_range option. If it found any new players, two things will happen. First, the new players will be remembered as a detected player. Secondly, the trial spawner will ensure that the next mob will spawn in at least two seconds. If the trial spawner hasn't spawned all the mobs required (which is calculated like total_mobs + total_mobs_added_per_player * (detected player count - 1)), it will check if the mob spawn cooldown is over, and check if the amount of mobs spawned by this trial spawner that are still alive is less then the calculation simultaneous_mobs + simultaneous_mobs_added_per_player * (detected player count - 1). If the checks pass, a new mob is spawned and the mob spawn cooldown is set to the ticks_between_spawn option. If the spawner has spawned all the mobs required, and if all the spawned mobs have died, it will set the cooldown to the target_cooldown_length option, and switch to the waiting for reward ejection state. A mob is considered dead when it is no longer alive, in another dimension or further than 47 blocks.

Waiting for reward ejection

The trial spawner will stay on this state until two seconds after all the mobs spawned by this trial spawner have died. It will then switch to the ejecting reward state.

Ejecting reward

The trial spawner will first choose a random loot table from the loot_tables_to_eject option. Then, every 1.5 seconds, it will eject random items from the loot table. It does this for every detected player. So if there is only one detected player, random items will only be dropped once. After it ejected the rewards, the list of detected players will be removed.

Cooldown

The cooldown state only checks if the cooldown has ended. If it did, the state will change to waiting for players.

Mob spawning logic

The mob to spawn will be a random mob chosen from the spawn_potentials option. The spawn position is the NBT tag Pos of the chosen mob. If the mob does not have a Pos tag, the spawn position will be the position of the trial spawner plus some random values. The random X and Z coordinates will be from -spawn_range to spawn_range, and the random Y coordinate will be from -1 to 2. Then a couple checks are performed. If any of the checks fail, no mob is spawned. The checks are as followed:

  • Does the mob collide at the chosen position?
  • Is there a line of sight between the chosen position and the trial spawner?
  • Every spawn rule the mob would normally have for being spawned from a spawner, except the light level requirement and the mob needs to spawn on a valid block.

The spawned mob will be persistent, so it doesn't despawn.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment