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

70 lines
2.6 KiB
Java
Raw Normal View History

2019-03-20 20:51:24 +01:00
package de.ellpeck.naturesaura.packet;
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;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.network.NetworkEvent;
2019-03-20 20:51:24 +01:00
2020-01-22 01:35:47 +01:00
import java.util.function.Supplier;
2020-01-22 23:21:52 +01:00
public class PacketClient {
2021-12-15 16:24:53 +01:00
2019-03-20 20:51:24 +01:00
private int type;
2021-12-04 15:40:09 +01:00
private 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;
}
private PacketClient() {
}
2021-12-15 16:24:53 +01:00
public static PacketClient fromBytes(FriendlyByteBuf buf) {
2021-12-15 16:30:22 +01:00
var client = new PacketClient();
2020-01-22 01:35:47 +01:00
client.type = buf.readByte();
2021-12-15 16:24:53 +01:00
client.data = buf.readNbt();
2020-01-22 01:35:47 +01:00
return client;
2019-03-20 20:51:24 +01:00
}
2021-12-15 16:24:53 +01:00
public static void toBytes(PacketClient packet, FriendlyByteBuf buf) {
2020-01-22 01:35:47 +01:00
buf.writeByte(packet.type);
2021-12-15 16:24:53 +01:00
buf.writeNbt(packet.data);
2019-03-20 20:51:24 +01:00
}
// lambda causes classloading issues on a server here
@SuppressWarnings("Convert2Lambda")
2020-01-22 23:21:52 +01:00
public static void onMessage(PacketClient message, Supplier<NetworkEvent.Context> ctx) {
ctx.get().enqueueWork(new Runnable() {
@Override
public void run() {
2021-12-15 16:30:22 +01:00
var mc = Minecraft.getInstance();
2021-12-04 15:40:09 +01:00
if (mc.level != null) {
switch (message.type) {
case 0: // dimension rail visualization
2021-12-15 16:30:22 +01:00
var goalDim = new ResourceLocation(message.data.getString("dim"));
var goalPos = BlockPos.of(message.data.getLong("pos"));
2020-09-22 15:38:58 +02:00
ItemRangeVisualizer.visualize(mc.player, ItemRangeVisualizer.VISUALIZED_RAILS, goalDim, goalPos);
2020-02-07 23:50:16 +01:00
case 1:
2021-12-15 16:30:22 +01:00
var entity = mc.level.getEntity(message.data.getInt("id"));
2021-12-15 16:24:53 +01:00
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);
2020-02-07 23:50:16 +01:00
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
}
});
ctx.get().setPacketHandled(true);
2019-03-20 20:51:24 +01:00
}
2020-01-22 01:35:47 +01:00
}