TheEndGatewayBlockEntity .java

net.minecraft.world.level.block.entity.TheEndGatewayBlockEntity (class): members: GATEWAY_HEIGHT_ABOVE_SURFACE, DEFAULT_EXACT_TELEPORT, ATTENTION_INTERVAL.

net.minecraft.world.level.block.entityclass276 linescommonextends TheEndPortalBlockEntity10.1 KB

API Surface (15)

void saveAdditional(final ValueOutput output)
void loadAdditional(final ValueInput input)
void beamAnimationTick(final Level level, final BlockPos pos, final BlockState state, final TheEndGatewayBlockEntity entity)
void portalTick(final Level level, final BlockPos pos, final BlockState state, final TheEndGatewayBlockEntity entity)
boolean isSpawning()
boolean isCoolingDown()
float getSpawnPercent(final float a)
float getCooldownPercent(final float a)
ClientboundBlockEntityDataPacket getUpdatePacket()
CompoundTag getUpdateTag(final HolderLookup.Provider registries)
void triggerCooldown(final Level level, final BlockPos pos, final BlockState blockState, final TheEndGatewayBlockEntity entity)
boolean triggerEvent(final int b0, final int b1)
boolean shouldRenderFace(final Direction direction)
int getParticleAmount()
void setExitPosition(final BlockPos exactPosition, final boolean exact)

Fields & Constants (12)

LOGGER
SPAWN_TIME
COOLDOWN_TIME
ATTENTION_INTERVAL
EVENT_COOLDOWN
GATEWAY_HEIGHT_ABOVE_SURFACE
DEFAULT_AGE
DEFAULT_EXACT_TELEPORT
age
teleportCooldown
exitPortal
exactTeleport

String literals (2)

"exit_portal"
"ExactTeleport"

Full Source

TheEndGatewayBlockEntity.java276 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.block.entity;
2
3
import com.mojang.logging.LogUtils;
4
import net.minecraft.core.BlockPos;
5
import net.minecraft.core.Direction;
6
import net.minecraft.core.HolderLookup;
7
import net.minecraft.core.registries.Registries;
8
import net.minecraft.data.worldgen.features.EndFeatures;
9
import net.minecraft.nbt.CompoundTag;
10
import net.minecraft.network.protocol.game.ClientboundBlockEntityDataPacket;
11
import net.minecraft.server.level.ServerLevel;
12
import net.minecraft.util.Mth;
13
import net.minecraft.util.RandomSource;
14
import net.minecraft.world.level.BlockGetter;
15
import net.minecraft.world.level.ChunkPos;
16
import net.minecraft.world.level.Level;
17
import net.minecraft.world.level.block.Block;
18
import net.minecraft.world.level.block.Blocks;
19
import net.minecraft.world.level.block.state.BlockState;
20
import net.minecraft.world.level.chunk.LevelChunk;
21
import net.minecraft.world.level.levelgen.feature.EndGatewayFeature;
22
import net.minecraft.world.level.storage.ValueInput;
23
import net.minecraft.world.level.storage.ValueOutput;
24
import net.minecraft.world.phys.Vec3;
25
import org.jspecify.annotations.Nullable;
26
import org.slf4j.Logger;
27
28
public class TheEndGatewayBlockEntity extends TheEndPortalBlockEntity {
29
	private static final Logger LOGGER = LogUtils.getLogger();
30
	private static final int SPAWN_TIME = 200;
31
	private static final int COOLDOWN_TIME = 40;
32
	private static final int ATTENTION_INTERVAL = 2400;
33
	private static final int EVENT_COOLDOWN = 1;
34
	private static final int GATEWAY_HEIGHT_ABOVE_SURFACE = 10;
35
	private static final long DEFAULT_AGE = 0L;
36
	private static final boolean DEFAULT_EXACT_TELEPORT = false;
37
	private long age = 0L;
38
	private int teleportCooldown;
39
	private @Nullable BlockPos exitPortal;
40
	private boolean exactTeleport = false;
41
42
	public TheEndGatewayBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
43
		super(BlockEntityTypes.END_GATEWAY, worldPosition, blockState);
44
	}
45
46
	@Override
47
	protected void saveAdditional(final ValueOutput output) {
48
		super.saveAdditional(output);
49
		output.putLong("Age", this.age);
50
		output.storeNullable("exit_portal", BlockPos.CODEC, this.exitPortal);
51
		if (this.exactTeleport) {
52
			output.putBoolean("ExactTeleport", true);
53
		}
54
	}
55
56
	@Override
57
	protected void loadAdditional(final ValueInput input) {
58
		super.loadAdditional(input);
59
		this.age = input.getLongOr("Age", 0L);
60
		this.exitPortal = input.read("exit_portal", BlockPos.CODEC).filter(Level::isInSpawnableBounds).orElse(null);
61
		this.exactTeleport = input.getBooleanOr("ExactTeleport", false);
62
	}
63
64
	public static void beamAnimationTick(final Level level, final BlockPos pos, final BlockState state, final TheEndGatewayBlockEntity entity) {
65
		entity.age++;
66
		if (entity.isCoolingDown()) {
67
			entity.teleportCooldown--;
68
		}
69
	}
70
71
	public static void portalTick(final Level level, final BlockPos pos, final BlockState state, final TheEndGatewayBlockEntity entity) {
72
		boolean spawning = entity.isSpawning();
73
		boolean coolingDown = entity.isCoolingDown();
74
		entity.age++;
75
		if (coolingDown) {
76
			entity.teleportCooldown--;
77
		} else if (entity.age % 2400L == 0L) {
78
			triggerCooldown(level, pos, state, entity);
79
		}
80
81
		if (spawning != entity.isSpawning() || coolingDown != entity.isCoolingDown()) {
82
			setChanged(level, pos, state);
83
		}
84
	}
85
86
	public boolean isSpawning() {
87
		return this.age < 200L;
88
	}
89
90
	public boolean isCoolingDown() {
91
		return this.teleportCooldown > 0;
92
	}
93
94
	public float getSpawnPercent(final float a) {
95
		return Mth.clamp(((float)this.age + a) / 200.0F, 0.0F, 1.0F);
96
	}
97
98
	public float getCooldownPercent(final float a) {
99
		return 1.0F - Mth.clamp((this.teleportCooldown - a) / 40.0F, 0.0F, 1.0F);
100
	}
101
102
	public ClientboundBlockEntityDataPacket getUpdatePacket() {
103
		return ClientboundBlockEntityDataPacket.create(this);
104
	}
105
106
	@Override
107
	public CompoundTag getUpdateTag(final HolderLookup.Provider registries) {
108
		return this.saveCustomOnly(registries);
109
	}
110
111
	public static void triggerCooldown(final Level level, final BlockPos pos, final BlockState blockState, final TheEndGatewayBlockEntity entity) {
112
		if (!level.isClientSide()) {
113
			entity.teleportCooldown = 40;
114
			level.blockEvent(pos, blockState.getBlock(), 1, 0);
115
			setChanged(level, pos, blockState);
116
		}
117
	}
118
119
	@Override
120
	public boolean triggerEvent(final int b0, final int b1) {
121
		if (b0 == 1) {
122
			this.teleportCooldown = 40;
123
			return true;
124
		} else {
125
			return super.triggerEvent(b0, b1);
126
		}
127
	}
128
129
	public @Nullable Vec3 getPortalPosition(final ServerLevel currentLevel, final BlockPos portalEntryPos) {
130
		if (this.exitPortal == null && currentLevel.dimension() == Level.END) {
131
			BlockPos exitPortalPos = findOrCreateValidTeleportPos(currentLevel, portalEntryPos);
132
			exitPortalPos = exitPortalPos.above(10);
133
			LOGGER.debug("Creating portal at {}", exitPortalPos);
134
			spawnGatewayPortal(currentLevel, exitPortalPos, EndGatewayFeature.knownExit(portalEntryPos, false));
135
			this.setExitPosition(exitPortalPos, this.exactTeleport);
136
		}
137
138
		if (this.exitPortal != null) {
139
			BlockPos pos = this.exactTeleport ? this.exitPortal : findExitPosition(currentLevel, this.exitPortal);
140
			return Vec3.atBottomCenterOf(pos);
141
		} else {
142
			return null;
143
		}
144
	}
145
146
	private static BlockPos findExitPosition(final Level level, final BlockPos exitPortal) {
147
		BlockPos pos = findTallestBlock(level, exitPortal.offset(0, 2, 0), 5, false);
148
		LOGGER.debug("Best exit position for portal at {} is {}", exitPortal, pos);
149
		return pos.above();
150
	}
151
152
	private static BlockPos findOrCreateValidTeleportPos(final ServerLevel level, final BlockPos endGatewayPos) {
153
		Vec3 exitPortalXZPosTentative = findExitPortalXZPosTentative(level, endGatewayPos);
154
		LevelChunk exitPortalChunk = getChunk(level, exitPortalXZPosTentative);
155
		BlockPos exitPortalPos = findValidSpawnInChunk(exitPortalChunk);
156
		if (exitPortalPos == null) {
157
			BlockPos newExitPortalPos = BlockPos.containing(exitPortalXZPosTentative.x + 0.5, 75.0, exitPortalXZPosTentative.z + 0.5);
158
			LOGGER.debug("Failed to find a suitable block to teleport to, spawning an island on {}", newExitPortalPos);
159
			level.registryAccess()
160
				.lookup(Registries.FEATURE)
161
				.flatMap(registry -> registry.get(EndFeatures.END_ISLAND))
162
				.ifPresent(
163
					endIsland -> endIsland.value().place(level, level.getChunkSource().getGenerator(), RandomSource.create(newExitPortalPos.asLong()), newExitPortalPos)
164
				);
165
			exitPortalPos = newExitPortalPos;
166
		} else {
167
			LOGGER.debug("Found suitable block to teleport to: {}", exitPortalPos);
168
		}
169
170
		return findTallestBlock(level, exitPortalPos, 16, true);
171
	}
172
173
	private static Vec3 findExitPortalXZPosTentative(final ServerLevel level, final BlockPos endGatewayPos) {
174
		Vec3 teleportXZDirectionVector = new Vec3(endGatewayPos.getX(), 0.0, endGatewayPos.getZ()).normalize();
175
		int teleportDistance = 1024;
176
		Vec3 exitPortalXZPosTentative = teleportXZDirectionVector.scale(1024.0);
177
178
		for (int chunkLimit = 16;
179
			!isChunkEmpty(level, exitPortalXZPosTentative) && chunkLimit-- > 0;
180
			exitPortalXZPosTentative = exitPortalXZPosTentative.add(teleportXZDirectionVector.scale(-16.0))
181
		) {
182
			LOGGER.debug("Skipping backwards past nonempty chunk at {}", exitPortalXZPosTentative);
183
		}
184
185
		for (int var6 = 16;
186
			isChunkEmpty(level, exitPortalXZPosTentative) && var6-- > 0;
187
			exitPortalXZPosTentative = exitPortalXZPosTentative.add(teleportXZDirectionVector.scale(16.0))
188
		) {
189
			LOGGER.debug("Skipping forward past empty chunk at {}", exitPortalXZPosTentative);
190
		}
191
192
		LOGGER.debug("Found chunk at {}", exitPortalXZPosTentative);
193
		return exitPortalXZPosTentative;
194
	}
195
196
	private static boolean isChunkEmpty(final ServerLevel level, final Vec3 xzPos) {
197
		return getChunk(level, xzPos).getHighestFilledSectionIndex() == -1;
198
	}
199
200
	private static BlockPos findTallestBlock(final BlockGetter level, final BlockPos around, final int dist, final boolean allowBedrock) {
201
		BlockPos tallest = null;
202
203
		for (int xd = -dist; xd <= dist; xd++) {
204
			for (int zd = -dist; zd <= dist; zd++) {
205
				if (xd != 0 || zd != 0 || allowBedrock) {
206
					for (int y = level.getMaxY(); y > (tallest == null ? level.getMinY() : tallest.getY()); y--) {
207
						BlockPos pos = new BlockPos(around.getX() + xd, y, around.getZ() + zd);
208
						BlockState state = level.getBlockState(pos);
209
						if (state.isCollisionShapeFullBlock(level, pos) && (allowBedrock || !state.is(Blocks.BEDROCK))) {
210
							tallest = pos;
211
							break;
212
						}
213
					}
214
				}
215
			}
216
		}
217
218
		return tallest == null ? around : tallest;
219
	}
220
221
	private static LevelChunk getChunk(final Level level, final Vec3 pos) {
222
		return level.getChunk(Mth.floor(pos.x / 16.0), Mth.floor(pos.z / 16.0));
223
	}
224
225
	private static @Nullable BlockPos findValidSpawnInChunk(final LevelChunk chunk) {
226
		ChunkPos chunkPos = chunk.getPos();
227
		BlockPos start = new BlockPos(chunkPos.getMinBlockX(), 30, chunkPos.getMinBlockZ());
228
		int maxY = chunk.getHighestSectionPosition() + 16 - 1;
229
		BlockPos end = new BlockPos(chunkPos.getMaxBlockX(), maxY, chunkPos.getMaxBlockZ());
230
		BlockPos closest = null;
231
		double closestDist = 0.0;
232
233
		for (BlockPos pos : BlockPos.betweenClosed(start, end)) {
234
			BlockState state = chunk.getBlockState(pos);
235
			BlockPos above = pos.above();
236
			BlockPos above2 = pos.above(2);
237
			if (state.is(Blocks.END_STONE)
238
				&& !chunk.getBlockState(above).isCollisionShapeFullBlock(chunk, above)
239
				&& !chunk.getBlockState(above2).isCollisionShapeFullBlock(chunk, above2)) {
240
				double dist = pos.distToCenterSqr(0.0, 0.0, 0.0);
241
				if (closest == null || dist < closestDist) {
242
					closest = pos;
243
					closestDist = dist;
244
				}
245
			}
246
		}
247
248
		return closest;
249
	}
250
251
	private static void spawnGatewayPortal(final ServerLevel level, final BlockPos portalPos, final EndGatewayFeature feature) {
252
		feature.place(level, level.getChunkSource().getGenerator(), RandomSource.create(), portalPos);
253
	}
254
255
	@Override
256
	public boolean shouldRenderFace(final Direction direction) {
257
		return Block.shouldRenderFace(this.getBlockState(), this.level.getBlockState(this.getBlockPos().relative(direction)), direction);
258
	}
259
260
	public int getParticleAmount() {
261
		int count = 0;
262
263
		for (Direction direction : Direction.values()) {
264
			count += this.shouldRenderFace(direction) ? 1 : 0;
265
		}
266
267
		return count;
268
	}
269
270
	public void setExitPosition(final BlockPos exactPosition, final boolean exact) {
271
		this.exactTeleport = exact;
272
		this.exitPortal = exactPosition;
273
		this.setChanged();
274
	}
275
}
276

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