| 1 | package net.minecraft.world.level.gameevent;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import net.minecraft.core.Holder;
|
| 5 | import net.minecraft.core.Registry;
|
| 6 | import net.minecraft.core.registries.BuiltInRegistries;
|
| 7 | import net.minecraft.core.registries.Registries;
|
| 8 | import net.minecraft.core.registries.codec.RegistryCodecs;
|
| 9 | import net.minecraft.resources.Identifier;
|
| 10 | import net.minecraft.world.entity.Entity;
|
| 11 | import net.minecraft.world.level.block.state.BlockState;
|
| 12 | import net.minecraft.world.phys.Vec3;
|
| 13 | import org.jspecify.annotations.Nullable;
|
| 14 |
|
| 15 | public record GameEvent(int notificationRadius) {
|
| 16 | public static final Holder.Reference<GameEvent> BLOCK_ACTIVATE = register("block_activate");
|
| 17 | public static final Holder.Reference<GameEvent> BLOCK_ATTACH = register("block_attach");
|
| 18 | public static final Holder.Reference<GameEvent> BLOCK_CHANGE = register("block_change");
|
| 19 | public static final Holder.Reference<GameEvent> BLOCK_CLOSE = register("block_close");
|
| 20 | public static final Holder.Reference<GameEvent> BLOCK_DEACTIVATE = register("block_deactivate");
|
| 21 | public static final Holder.Reference<GameEvent> BLOCK_DESTROY = register("block_destroy");
|
| 22 | public static final Holder.Reference<GameEvent> BLOCK_DETACH = register("block_detach");
|
| 23 | public static final Holder.Reference<GameEvent> BLOCK_OPEN = register("block_open");
|
| 24 | public static final Holder.Reference<GameEvent> BLOCK_PLACE = register("block_place");
|
| 25 | public static final Holder.Reference<GameEvent> BOUNCE = register("bounce");
|
| 26 | public static final Holder.Reference<GameEvent> CONTAINER_CLOSE = register("container_close");
|
| 27 | public static final Holder.Reference<GameEvent> CONTAINER_OPEN = register("container_open");
|
| 28 | public static final Holder.Reference<GameEvent> DRINK = register("drink");
|
| 29 | public static final Holder.Reference<GameEvent> EAT = register("eat");
|
| 30 | public static final Holder.Reference<GameEvent> ELYTRA_GLIDE = register("elytra_glide");
|
| 31 | public static final Holder.Reference<GameEvent> ENTITY_DAMAGE = register("entity_damage");
|
| 32 | public static final Holder.Reference<GameEvent> ENTITY_DIE = register("entity_die");
|
| 33 | public static final Holder.Reference<GameEvent> ENTITY_DISMOUNT = register("entity_dismount");
|
| 34 | public static final Holder.Reference<GameEvent> ENTITY_INTERACT = register("entity_interact");
|
| 35 | public static final Holder.Reference<GameEvent> ENTITY_MOUNT = register("entity_mount");
|
| 36 | public static final Holder.Reference<GameEvent> ENTITY_PLACE = register("entity_place");
|
| 37 | public static final Holder.Reference<GameEvent> ENTITY_ACTION = register("entity_action");
|
| 38 | public static final Holder.Reference<GameEvent> EQUIP = register("equip");
|
| 39 | public static final Holder.Reference<GameEvent> EXPLODE = register("explode");
|
| 40 | public static final Holder.Reference<GameEvent> FLAP = register("flap");
|
| 41 | public static final Holder.Reference<GameEvent> FLUID_PICKUP = register("fluid_pickup");
|
| 42 | public static final Holder.Reference<GameEvent> FLUID_PLACE = register("fluid_place");
|
| 43 | public static final Holder.Reference<GameEvent> HIT_GROUND = register("hit_ground");
|
| 44 | public static final Holder.Reference<GameEvent> INSTRUMENT_PLAY = register("instrument_play");
|
| 45 | public static final Holder.Reference<GameEvent> ITEM_INTERACT_FINISH = register("item_interact_finish");
|
| 46 | public static final Holder.Reference<GameEvent> ITEM_INTERACT_START = register("item_interact_start");
|
| 47 | public static final Holder.Reference<GameEvent> JUKEBOX_PLAY = register("jukebox_play", 10);
|
| 48 | public static final Holder.Reference<GameEvent> JUKEBOX_STOP_PLAY = register("jukebox_stop_play", 10);
|
| 49 | public static final Holder.Reference<GameEvent> LIGHTNING_STRIKE = register("lightning_strike");
|
| 50 | public static final Holder.Reference<GameEvent> NOTE_BLOCK_PLAY = register("note_block_play");
|
| 51 | public static final Holder.Reference<GameEvent> PRIME_FUSE = register("prime_fuse");
|
| 52 | public static final Holder.Reference<GameEvent> PROJECTILE_LAND = register("projectile_land");
|
| 53 | public static final Holder.Reference<GameEvent> PROJECTILE_SHOOT = register("projectile_shoot");
|
| 54 | public static final Holder.Reference<GameEvent> SCULK_SENSOR_TENDRILS_CLICKING = register("sculk_sensor_tendrils_clicking");
|
| 55 | public static final Holder.Reference<GameEvent> SHEAR = register("shear");
|
| 56 | public static final Holder.Reference<GameEvent> SHRIEK = register("shriek", 32);
|
| 57 | public static final Holder.Reference<GameEvent> SPLASH = register("splash");
|
| 58 | public static final Holder.Reference<GameEvent> STEP = register("step");
|
| 59 | public static final Holder.Reference<GameEvent> SWIM = register("swim");
|
| 60 | public static final Holder.Reference<GameEvent> TELEPORT = register("teleport");
|
| 61 | public static final Holder.Reference<GameEvent> UNEQUIP = register("unequip");
|
| 62 | public static final Holder.Reference<GameEvent> RESONATE_1 = register("resonate_1");
|
| 63 | public static final Holder.Reference<GameEvent> RESONATE_2 = register("resonate_2");
|
| 64 | public static final Holder.Reference<GameEvent> RESONATE_3 = register("resonate_3");
|
| 65 | public static final Holder.Reference<GameEvent> RESONATE_4 = register("resonate_4");
|
| 66 | public static final Holder.Reference<GameEvent> RESONATE_5 = register("resonate_5");
|
| 67 | public static final Holder.Reference<GameEvent> RESONATE_6 = register("resonate_6");
|
| 68 | public static final Holder.Reference<GameEvent> RESONATE_7 = register("resonate_7");
|
| 69 | public static final Holder.Reference<GameEvent> RESONATE_8 = register("resonate_8");
|
| 70 | public static final Holder.Reference<GameEvent> RESONATE_9 = register("resonate_9");
|
| 71 | public static final Holder.Reference<GameEvent> RESONATE_10 = register("resonate_10");
|
| 72 | public static final Holder.Reference<GameEvent> RESONATE_11 = register("resonate_11");
|
| 73 | public static final Holder.Reference<GameEvent> RESONATE_12 = register("resonate_12");
|
| 74 | public static final Holder.Reference<GameEvent> RESONATE_13 = register("resonate_13");
|
| 75 | public static final Holder.Reference<GameEvent> RESONATE_14 = register("resonate_14");
|
| 76 | public static final Holder.Reference<GameEvent> RESONATE_15 = register("resonate_15");
|
| 77 | public static final int DEFAULT_NOTIFICATION_RADIUS = 16;
|
| 78 | public static final Codec<Holder<GameEvent>> CODEC = RegistryCodecs.holder(Registries.GAME_EVENT);
|
| 79 |
|
| 80 | public static Holder<GameEvent> bootstrap(final Registry<GameEvent> registry) {
|
| 81 | return BLOCK_ACTIVATE;
|
| 82 | }
|
| 83 |
|
| 84 | private static Holder.Reference<GameEvent> register(final String name) {
|
| 85 | return register(name, 16);
|
| 86 | }
|
| 87 |
|
| 88 | private static Holder.Reference<GameEvent> register(final String name, final int notificationRadius) {
|
| 89 | return Registry.registerForHolder(BuiltInRegistries.GAME_EVENT, Identifier.withDefaultNamespace(name), new GameEvent(notificationRadius));
|
| 90 | }
|
| 91 |
|
| 92 | public record Context(@Nullable Entity sourceEntity, @Nullable BlockState affectedState) {
|
| 93 | public static GameEvent.Context of(final @Nullable Entity sourceEntity) {
|
| 94 | return new GameEvent.Context(sourceEntity, null);
|
| 95 | }
|
| 96 |
|
| 97 | public static GameEvent.Context of(final @Nullable BlockState state) {
|
| 98 | return new GameEvent.Context(null, state);
|
| 99 | }
|
| 100 |
|
| 101 | public static GameEvent.Context of(final @Nullable Entity sourceEntity, final @Nullable BlockState state) {
|
| 102 | return new GameEvent.Context(sourceEntity, state);
|
| 103 | }
|
| 104 | }
|
| 105 |
|
| 106 | public static final class ListenerInfo implements Comparable<GameEvent.ListenerInfo> {
|
| 107 | private final Holder<GameEvent> gameEvent;
|
| 108 | private final Vec3 source;
|
| 109 | private final GameEvent.Context context;
|
| 110 | private final GameEventListener recipient;
|
| 111 | private final double distanceToRecipient;
|
| 112 |
|
| 113 | public ListenerInfo(
|
| 114 | final Holder<GameEvent> gameEvent, final Vec3 source, final GameEvent.Context context, final GameEventListener recipient, final Vec3 recipientPos
|
| 115 | ) {
|
| 116 | this.gameEvent = gameEvent;
|
| 117 | this.source = source;
|
| 118 | this.context = context;
|
| 119 | this.recipient = recipient;
|
| 120 | this.distanceToRecipient = source.distanceToSqr(recipientPos);
|
| 121 | }
|
| 122 |
|
| 123 | public int compareTo(final GameEvent.ListenerInfo other) {
|
| 124 | return Double.compare(this.distanceToRecipient, other.distanceToRecipient);
|
| 125 | }
|
| 126 |
|
| 127 | public Holder<GameEvent> gameEvent() {
|
| 128 | return this.gameEvent;
|
| 129 | }
|
| 130 |
|
| 131 | public Vec3 source() {
|
| 132 | return this.source;
|
| 133 | }
|
| 134 |
|
| 135 | public GameEvent.Context context() {
|
| 136 | return this.context;
|
| 137 | }
|
| 138 |
|
| 139 | public GameEventListener recipient() {
|
| 140 | return this.recipient;
|
| 141 | }
|
| 142 | }
|
| 143 | }
|
| 144 |
|