forked from SpongePowered/Sponge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewerPacketUtil.java
More file actions
231 lines (199 loc) · 11.8 KB
/
ViewerPacketUtil.java
File metadata and controls
231 lines (199 loc) · 11.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
* This file is part of Sponge, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.spongepowered.common.effect.util;
import net.kyori.adventure.sound.Sound;
import net.kyori.adventure.sound.SoundStop;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.title.Title;
import net.minecraft.core.BlockPos;
import net.minecraft.core.Holder;
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.component.DataComponents;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientGamePacketListener;
import net.minecraft.network.protocol.game.ClientboundBlockDestructionPacket;
import net.minecraft.network.protocol.game.ClientboundBlockUpdatePacket;
import net.minecraft.network.protocol.game.ClientboundBundlePacket;
import net.minecraft.network.protocol.game.ClientboundClearTitlesPacket;
import net.minecraft.network.protocol.game.ClientboundEntityEventPacket;
import net.minecraft.network.protocol.game.ClientboundLevelEventPacket;
import net.minecraft.network.protocol.game.ClientboundSetActionBarTextPacket;
import net.minecraft.network.protocol.game.ClientboundSetPlayerInventoryPacket;
import net.minecraft.network.protocol.game.ClientboundSetSubtitleTextPacket;
import net.minecraft.network.protocol.game.ClientboundSetTitleTextPacket;
import net.minecraft.network.protocol.game.ClientboundSetTitlesAnimationPacket;
import net.minecraft.network.protocol.game.ClientboundSoundEntityPacket;
import net.minecraft.network.protocol.game.ClientboundSoundPacket;
import net.minecraft.network.protocol.game.ClientboundStopSoundPacket;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.sounds.SoundSource;
import net.minecraft.util.RandomSource;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.JukeboxSong;
import net.minecraft.world.item.component.DeathProtection;
import net.minecraft.world.level.BlockGetter;
import net.minecraft.world.level.dimension.DimensionType;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.spongepowered.api.Engine;
import org.spongepowered.api.block.BlockState;
import org.spongepowered.api.effect.particle.ParticleEffect;
import org.spongepowered.api.effect.sound.music.MusicDisc;
import org.spongepowered.api.entity.Entity;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.item.inventory.ItemStackLike;
import org.spongepowered.api.world.World;
import org.spongepowered.api.world.WorldType;
import org.spongepowered.common.SpongeCommon;
import org.spongepowered.common.SpongeEngine;
import org.spongepowered.common.adventure.SpongeAdventure;
import org.spongepowered.common.effect.particle.SpongeParticleHelper;
import org.spongepowered.common.item.util.ItemStackUtil;
import org.spongepowered.common.network.packet.ChangeViewerEnvironmentPacket;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
public final class ViewerPacketUtil {
public static ChangeViewerEnvironmentPacket changeEnvironment(final WorldType worldType) {
return new ChangeViewerEnvironmentPacket((DimensionType) (Object) Objects.requireNonNull(worldType, "worldType"));
}
public static ClientboundBundlePacket deathProtection(final Player player, final ItemStackLike stack) {
final ItemStack item = ItemStackUtil.fromLikeToNativeCopy(stack);
// Client will play item animation only if it has DEATH_PROTECTION component,
// otherwise it will use totem of undying
if (!item.has(DataComponents.DEATH_PROTECTION)) {
item.set(DataComponents.DEATH_PROTECTION, new DeathProtection(List.of()));
}
final net.minecraft.world.entity.player.Player mcPlayer = (net.minecraft.world.entity.player.Player) player;
final Inventory inventory = mcPlayer.getInventory();
final int totemSlot = inventory.selected;
final net.minecraft.world.item.ItemStack oldItem = inventory.getSelected();
final List<Packet<? super ClientGamePacketListener>> packets = new ArrayList<>();
// First we need to send a fake totem ItemStack to the player's hand
packets.add(new ClientboundSetPlayerInventoryPacket(totemSlot, item));
// Next we tell the client to play death protection animation
packets.add(new ClientboundEntityEventPacket(mcPlayer, (byte) 35));
// Now we can remove the fake totem
packets.add(new ClientboundSetPlayerInventoryPacket(totemSlot, oldItem));
return new ClientboundBundlePacket(packets);
}
public static ClientboundBundlePacket spawnParticles(final ParticleEffect particleEffect, final double x, final double y, final double z) {
return SpongeParticleHelper.createPacket(particleEffect, x, y, z);
}
public static ClientboundSoundEntityPacket playSound(final Sound sound, final Entity entity) {
Objects.requireNonNull(entity, "entity");
final Holder<SoundEvent> event = resolveEvent(sound);
final SoundSource source = SpongeAdventure.asVanilla(sound.source());
final net.minecraft.world.entity.Entity mcEntity = (net.minecraft.world.entity.Entity) entity;
final long random = sound.seed().orElseGet(() -> mcEntity.level().getRandom().nextLong());
return new ClientboundSoundEntityPacket(event, source, mcEntity, sound.volume(), sound.pitch(), random);
}
public static ClientboundSoundPacket playSound(final Sound sound, final RandomSource randomSource, final double x, final double y, final double z) {
final Holder<SoundEvent> event = resolveEvent(sound);
final SoundSource source = SpongeAdventure.asVanilla(sound.source());
final long random = sound.seed().orElseGet(randomSource::nextLong);
return new ClientboundSoundPacket(event, source, x, y, z, sound.volume(), sound.pitch(), random);
}
public static ClientboundStopSoundPacket stopSound(final SoundStop stop) {
Objects.requireNonNull(stop, "stop");
final @Nullable ResourceLocation sound = SpongeAdventure.asVanillaNullable(stop.sound());
final @Nullable SoundSource source = SpongeAdventure.asVanillaNullable(stop.source());
return new ClientboundStopSoundPacket(sound, source);
}
public static ClientboundLevelEventPacket playMusicDisc(final int x, final int y, final int z, final MusicDisc musicDisc, final RegistryAccess registryAccess) {
Objects.requireNonNull(musicDisc, "musicDisc");
final int songId = registryAccess.lookupOrThrow(Registries.JUKEBOX_SONG).getId((JukeboxSong) (Object) musicDisc);
return new ClientboundLevelEventPacket(1010, new BlockPos(x, y, z), songId, false);
}
public static ClientboundLevelEventPacket stopMusicDisc(final int x, final int y, final int z) {
return new ClientboundLevelEventPacket(1011, new BlockPos(x, y, z), 0, false);
}
public static ClientboundBlockUpdatePacket blockUpdate(final int x, final int y, final int z, final BlockState state) {
Objects.requireNonNull(state, "state");
return new ClientboundBlockUpdatePacket(new BlockPos(x, y, z), (net.minecraft.world.level.block.state.BlockState) state);
}
public static ClientboundBlockUpdatePacket blockUpdate(final int x, final int y, final int z, final World<?, ?> world) {
return new ClientboundBlockUpdatePacket((BlockGetter) world, new BlockPos(x, y, z));
}
public static ClientboundBlockDestructionPacket blockProgress(final int x, final int y, final int z, final double progress, final Engine engine) {
if (progress < 0 || progress > 1) {
throw new IllegalArgumentException("Progress must be between 0 and 1");
}
final BlockPos pos = new BlockPos(x, y, z);
final int id = ((SpongeEngine) engine).getBlockDestructionIdCache().getOrCreate(pos);
final int progressStage = progress == 1 ? 9 : (int) (progress * 10);
return new ClientboundBlockDestructionPacket(id, pos, progressStage);
}
public static Optional<ClientboundBlockDestructionPacket> resetBlockProgress(final int x, final int y, final int z, final Engine engine) {
final BlockPos pos = new BlockPos(x, y, z);
return ((SpongeEngine) engine).getBlockDestructionIdCache().get(pos)
.map(id -> new ClientboundBlockDestructionPacket(id, pos, -1));
}
public static ClientboundSetActionBarTextPacket setActionBarText(final Component message) {
return new ClientboundSetActionBarTextPacket(SpongeAdventure.asVanilla(Objects.requireNonNull(message, "message")));
}
public static ClientboundBundlePacket showTitle(final Title title) {
final Title.Times times = Objects.requireNonNull(title, "title").times();
final List<Packet<? super ClientGamePacketListener>> packets = new ArrayList<>();
if (times != null) {
packets.add(ViewerPacketUtil.setTitlesAnimation(times));
}
packets.add(ViewerPacketUtil.setSubtitleText(title.subtitle()));
packets.add(ViewerPacketUtil.setTitleText(title.title()));
return new ClientboundBundlePacket(packets);
}
public static ClientboundSetTitleTextPacket setTitleText(final Component component) {
return new ClientboundSetTitleTextPacket(SpongeAdventure.asVanilla(component));
}
public static ClientboundSetSubtitleTextPacket setSubtitleText(final Component component) {
return new ClientboundSetSubtitleTextPacket(SpongeAdventure.asVanilla(component));
}
public static ClientboundSetTitlesAnimationPacket setTitlesAnimation(final Title.Times times) {
return new ClientboundSetTitlesAnimationPacket(
durationToTicks(times.fadeIn()),
durationToTicks(times.stay()),
durationToTicks(times.fadeOut()));
}
public static ClientboundClearTitlesPacket clearTitles(final boolean resetTimes) {
return new ClientboundClearTitlesPacket(resetTimes);
}
private static Holder<SoundEvent> resolveEvent(final @NonNull Sound sound) {
final ResourceLocation soundKey = SpongeAdventure.asVanilla(Objects.requireNonNull(sound, "sound").name());
final var soundEventRegistry = SpongeCommon.vanillaRegistry(Registries.SOUND_EVENT);
final SoundEvent event = soundEventRegistry.getOptional(soundKey)
.orElseGet(() -> SoundEvent.createVariableRangeEvent(soundKey));
return soundEventRegistry.wrapAsHolder(event);
}
private static int durationToTicks(final Duration duration) {
return (int) (duration.toMillis() / 50L);
}
private ViewerPacketUtil() {
}
}