DemoMode .java

net.minecraft.server.level.DemoMode (class): members: demoEndedReminder, TOTAL_PLAY_TICKS, displayedIntro, gameModeTicks, demoHasEnded, DEMO_DAYS, useItem.

net.minecraft.server.levelclass103 linesserverextends ServerPlayerGameMode3.3 KB

API Surface (4)

void tick()
void handleBlockBreakAction(final BlockPos pos, final ServerboundPlayerActionPacket.Action action, final Direction direction, final int maxY, final int sequence)
InteractionResult useItem(final ServerPlayer player, final Level level, final ItemStack itemStack, final InteractionHand hand)
InteractionResult useItemOn(final ServerPlayer player, final Level level, final ItemStack itemStack, final InteractionHand hand, final BlockHitResult hitResult)

Fields & Constants (8)

DEMO_DAYS
TOTAL_PLAY_TICKS
displayedIntro
demoHasEnded
demoEndedReminder
gameModeTicks
maxY
hand

String literals (3)

"demo.day."
"demo.day.warning"
"demo.reminder"

Full Source

DemoMode.java103 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.server.level;
2
3
import net.minecraft.core.BlockPos;
4
import net.minecraft.core.Direction;
5
import net.minecraft.network.chat.Component;
6
import net.minecraft.network.protocol.game.ClientboundGameEventPacket;
7
import net.minecraft.network.protocol.game.ServerboundPlayerActionPacket;
8
import net.minecraft.world.InteractionHand;
9
import net.minecraft.world.InteractionResult;
10
import net.minecraft.world.item.ItemStack;
11
import net.minecraft.world.level.Level;
12
import net.minecraft.world.phys.BlockHitResult;
13
14
public class DemoMode extends ServerPlayerGameMode {
15
	public static final int DEMO_DAYS = 5;
16
	public static final int TOTAL_PLAY_TICKS = 120500;
17
	private boolean displayedIntro;
18
	private boolean demoHasEnded;
19
	private int demoEndedReminder;
20
	private int gameModeTicks;
21
22
	public DemoMode(final ServerPlayer player) {
23
		super(player);
24
	}
25
26
	@Override
27
	public void tick() {
28
		super.tick();
29
		this.gameModeTicks++;
30
		long time = this.level.getGameTime();
31
		long day = time / 24000L + 1L;
32
		if (!this.displayedIntro && this.gameModeTicks > 20) {
33
			this.displayedIntro = true;
34
			this.player.connection.send(new ClientboundGameEventPacket(ClientboundGameEventPacket.DEMO_EVENT, 0.0F));
35
		}
36
37
		this.demoHasEnded = time > 120500L;
38
		if (this.demoHasEnded) {
39
			this.demoEndedReminder++;
40
		}
41
42
		if (time % 24000L == 500L) {
43
			if (day <= 6L) {
44
				if (day == 6L) {
45
					this.player.connection.send(new ClientboundGameEventPacket(ClientboundGameEventPacket.DEMO_EVENT, 104.0F));
46
				} else {
47
					this.player.sendSystemMessage(Component.translatable("demo.day." + day));
48
				}
49
			}
50
		} else if (day == 1L) {
51
			if (time == 100L) {
52
				this.player.connection.send(new ClientboundGameEventPacket(ClientboundGameEventPacket.DEMO_EVENT, 101.0F));
53
			} else if (time == 175L) {
54
				this.player.connection.send(new ClientboundGameEventPacket(ClientboundGameEventPacket.DEMO_EVENT, 102.0F));
55
			} else if (time == 250L) {
56
				this.player.connection.send(new ClientboundGameEventPacket(ClientboundGameEventPacket.DEMO_EVENT, 103.0F));
57
			}
58
		} else if (day == 5L && time % 24000L == 22000L) {
59
			this.player.sendSystemMessage(Component.translatable("demo.day.warning"));
60
		}
61
	}
62
63
	private void outputDemoReminder() {
64
		if (this.demoEndedReminder > 100) {
65
			this.player.sendSystemMessage(Component.translatable("demo.reminder"));
66
			this.demoEndedReminder = 0;
67
		}
68
	}
69
70
	@Override
71
	public void handleBlockBreakAction(
72
		final BlockPos pos, final ServerboundPlayerActionPacket.Action action, final Direction direction, final int maxY, final int sequence
73
	) {
74
		if (this.demoHasEnded) {
75
			this.outputDemoReminder();
76
		} else {
77
			super.handleBlockBreakAction(pos, action, direction, maxY, sequence);
78
		}
79
	}
80
81
	@Override
82
	public InteractionResult useItem(final ServerPlayer player, final Level level, final ItemStack itemStack, final InteractionHand hand) {
83
		if (this.demoHasEnded) {
84
			this.outputDemoReminder();
85
			return InteractionResult.PASS;
86
		} else {
87
			return super.useItem(player, level, itemStack, hand);
88
		}
89
	}
90
91
	@Override
92
	public InteractionResult useItemOn(
93
		final ServerPlayer player, final Level level, final ItemStack itemStack, final InteractionHand hand, final BlockHitResult hitResult
94
	) {
95
		if (this.demoHasEnded) {
96
			this.outputDemoReminder();
97
			return InteractionResult.PASS;
98
		} else {
99
			return super.useItemOn(player, level, itemStack, hand, hitResult);
100
		}
101
	}
102
}
103

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