NaturesAura/src/main/java/de/ellpeck/naturesaura/packet/PacketClient.java

67 lines
2.5 KiB
Java
Raw Normal View History

2019-03-20 20:51:24 +01:00
package de.ellpeck.naturesaura.packet;
2024-03-10 10:41:34 +01:00
import de.ellpeck.naturesaura.NaturesAura;
2020-01-26 01:41:49 +01:00
import de.ellpeck.naturesaura.items.ItemRangeVisualizer;
2020-02-07 23:50:16 +01:00
import de.ellpeck.naturesaura.items.ModItems;
2019-03-20 20:51:24 +01:00
import net.minecraft.client.Minecraft;
2021-12-15 16:24:53 +01:00
import net.minecraft.core.BlockPos;
import net.minecraft.core.particles.ParticleTypes;
2021-12-04 15:40:09 +01:00
import net.minecraft.nbt.CompoundTag;
2021-12-15 16:24:53 +01:00
import net.minecraft.network.FriendlyByteBuf;
2024-03-10 10:41:34 +01:00
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
2021-12-15 16:24:53 +01:00
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.item.ItemStack;
2024-03-10 10:41:34 +01:00
import net.neoforged.neoforge.network.handling.PlayPayloadContext;
2019-03-20 20:51:24 +01:00
2024-03-10 10:41:34 +01:00
public class PacketClient implements CustomPacketPayload {
2020-01-22 01:35:47 +01:00
2024-03-10 10:41:34 +01:00
public static final ResourceLocation ID = new ResourceLocation(NaturesAura.MOD_ID, "client");
2021-12-15 16:24:53 +01:00
2024-03-10 10:41:34 +01:00
private final int type;
private final CompoundTag data;
2019-03-20 20:51:24 +01:00
2021-12-04 15:40:09 +01:00
public PacketClient(int type, CompoundTag data) {
2020-01-22 23:21:52 +01:00
this.type = type;
this.data = data;
}
2024-03-10 10:41:34 +01:00
public PacketClient(FriendlyByteBuf buf) {
this.type = buf.readByte();
this.data = buf.readNbt();
2020-01-22 23:21:52 +01:00
}
2024-03-10 10:41:34 +01:00
@Override
public void write(FriendlyByteBuf buf) {
buf.writeByte(this.type);
buf.writeNbt(this.data);
2019-03-20 20:51:24 +01:00
}
2024-03-10 10:41:34 +01:00
@Override
public ResourceLocation id() {
return PacketClient.ID;
2019-03-20 20:51:24 +01:00
}
2024-03-10 10:41:34 +01:00
public static void onMessage(PacketClient message, PlayPayloadContext ctx) {
ctx.workHandler().execute(() -> {
var mc = Minecraft.getInstance();
if (mc.level != null) {
switch (message.type) {
case 0: // dimension rail visualization
var goalDim = new ResourceLocation(message.data.getString("dim"));
var goalPos = BlockPos.of(message.data.getLong("pos"));
ItemRangeVisualizer.visualize(mc.player, ItemRangeVisualizer.VISUALIZED_RAILS, goalDim, goalPos);
case 1:
var entity = mc.level.getEntity(message.data.getInt("id"));
mc.particleEngine.createTrackingEmitter(entity, ParticleTypes.TOTEM_OF_UNDYING, 30);
mc.level.playLocalSound(entity.getX(), entity.getY(), entity.getZ(), SoundEvents.TOTEM_USE, entity.getSoundSource(), 1.0F, 1.0F, false);
if (entity == mc.player) {
mc.gameRenderer.displayItemActivation(new ItemStack(ModItems.DEATH_RING));
}
2019-03-20 20:51:24 +01:00
}
2020-01-22 23:21:52 +01:00
}
});
2019-03-20 20:51:24 +01:00
}
2024-03-10 10:41:34 +01:00
2020-01-22 01:35:47 +01:00
}