GameEventDispatcher .java

net.minecraft.world.level.gameevent.GameEventDispatcher (class): members: post, level. imports: DebugGameEventInfo, DebugSubscriptions, ChunkAccess, Vec3.

net.minecraft.world.level.gameeventclass72 linescommon2.8 KB

API Surface (1)

void post(final Holder<GameEvent> gameEvent, final Vec3 position, final GameEvent.Context context)

Fields & Constants (1)

level

Full Source

GameEventDispatcher.java72 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.gameevent;
2
3
import java.util.ArrayList;
4
import java.util.Collections;
5
import java.util.List;
6
import net.minecraft.core.BlockPos;
7
import net.minecraft.core.Holder;
8
import net.minecraft.core.SectionPos;
9
import net.minecraft.server.level.ServerLevel;
10
import net.minecraft.util.debug.DebugGameEventInfo;
11
import net.minecraft.util.debug.DebugSubscriptions;
12
import net.minecraft.world.level.chunk.ChunkAccess;
13
import net.minecraft.world.phys.Vec3;
14
15
public class GameEventDispatcher {
16
	private final ServerLevel level;
17
18
	public GameEventDispatcher(final ServerLevel level) {
19
		this.level = level;
20
	}
21
22
	public void post(final Holder<GameEvent> gameEvent, final Vec3 position, final GameEvent.Context context) {
23
		int radius = gameEvent.value().notificationRadius();
24
		BlockPos center = BlockPos.containing(position);
25
		int sectionMinX = SectionPos.blockToSectionCoord(center.getX() - radius);
26
		int sectionMinY = SectionPos.blockToSectionCoord(center.getY() - radius);
27
		int sectionMinZ = SectionPos.blockToSectionCoord(center.getZ() - radius);
28
		int sectionMaxX = SectionPos.blockToSectionCoord(center.getX() + radius);
29
		int sectionMaxY = SectionPos.blockToSectionCoord(center.getY() + radius);
30
		int sectionMaxZ = SectionPos.blockToSectionCoord(center.getZ() + radius);
31
		List<GameEvent.ListenerInfo> toHandleByDistance = new ArrayList<>();
32
		GameEventListenerRegistry.ListenerVisitor visitListeners = (listener, pos) -> {
33
			if (listener.getDeliveryMode() == GameEventListener.DeliveryMode.BY_DISTANCE) {
34
				toHandleByDistance.add(new GameEvent.ListenerInfo(gameEvent, position, context, listener, pos));
35
			} else {
36
				listener.handleGameEvent(this.level, gameEvent, context, position);
37
			}
38
		};
39
		boolean applicable = false;
40
41
		for (int chunkX = sectionMinX; chunkX <= sectionMaxX; chunkX++) {
42
			for (int chunkZ = sectionMinZ; chunkZ <= sectionMaxZ; chunkZ++) {
43
				ChunkAccess chunk = this.level.getChunkSource().getChunkNow(chunkX, chunkZ);
44
				if (chunk != null) {
45
					for (int section = sectionMinY; section <= sectionMaxY; section++) {
46
						applicable |= chunk.getListenerRegistry(section).visitInRangeListeners(gameEvent, position, context, visitListeners);
47
					}
48
				}
49
			}
50
		}
51
52
		if (!toHandleByDistance.isEmpty()) {
53
			this.handleGameEventMessagesInQueue(toHandleByDistance);
54
		}
55
56
		if (applicable) {
57
			this.level
58
				.debugSynchronizers()
59
				.broadcastEventToTracking(BlockPos.containing(position), DebugSubscriptions.GAME_EVENTS, new DebugGameEventInfo(gameEvent, position));
60
		}
61
	}
62
63
	private void handleGameEventMessagesInQueue(final List<GameEvent.ListenerInfo> listenerInfos) {
64
		Collections.sort(listenerInfos);
65
66
		for (GameEvent.ListenerInfo listenerInfo : listenerInfos) {
67
			GameEventListener listener = listenerInfo.recipient();
68
			listener.handleGameEvent(this.level, listenerInfo.gameEvent(), listenerInfo.context(), listenerInfo.source());
69
		}
70
	}
71
}
72

Decompiled Minecraft 26.3 source of GameEventDispatcher (net/minecraft/world/level/gameevent/GameEventDispatcher.java) with Mojang official mappings, stored verbatim on the site.