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

70 lines
2.7 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;
2020-02-07 23:50:16 +01:00
import net.minecraft.entity.Entity;
import net.minecraft.item.ItemStack;
2020-09-22 15:38:58 +02:00
import net.minecraft.nbt.CompoundNBT;
2020-01-22 01:35:47 +01:00
import net.minecraft.network.PacketBuffer;
2020-02-07 23:50:16 +01:00
import net.minecraft.particles.ParticleTypes;
2020-09-22 15:38:58 +02:00
import net.minecraft.util.ResourceLocation;
2020-02-07 23:50:16 +01:00
import net.minecraft.util.SoundEvents;
2019-03-20 20:51:24 +01:00
import net.minecraft.util.math.BlockPos;
2020-01-22 01:35:47 +01:00
import net.minecraftforge.fml.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 {
2019-03-20 20:51:24 +01:00
private int type;
2020-09-22 15:38:58 +02:00
private CompoundNBT data;
2019-03-20 20:51:24 +01:00
2020-09-22 15:38:58 +02:00
public PacketClient(int type, CompoundNBT data) {
2020-01-22 23:21:52 +01:00
this.type = type;
this.data = data;
}
private PacketClient() {
}
2020-01-22 01:35:47 +01:00
public static PacketClient fromBytes(PacketBuffer buf) {
PacketClient client = new PacketClient();
client.type = buf.readByte();
2020-09-22 15:38:58 +02:00
client.data = buf.readCompoundTag();
2020-01-22 01:35:47 +01:00
return client;
2019-03-20 20:51:24 +01:00
}
2020-01-22 01:35:47 +01:00
public static void toBytes(PacketClient packet, PacketBuffer buf) {
buf.writeByte(packet.type);
2020-09-22 15:38:58 +02:00
buf.writeCompoundTag(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() {
Minecraft mc = Minecraft.getInstance();
if (mc.world != null) {
switch (message.type) {
case 0: // dimension rail visualization
2020-09-22 15:38:58 +02:00
ResourceLocation goalDim = new ResourceLocation(message.data.getString("dim"));
BlockPos goalPos = BlockPos.fromLong(message.data.getLong("pos"));
ItemRangeVisualizer.visualize(mc.player, ItemRangeVisualizer.VISUALIZED_RAILS, goalDim, goalPos);
2020-02-07 23:50:16 +01:00
case 1:
2020-09-22 15:38:58 +02:00
Entity entity = mc.world.getEntityByID(message.data.getInt("id"));
2020-02-07 23:50:16 +01:00
mc.particles.emitParticleAtEntity(entity, ParticleTypes.TOTEM_OF_UNDYING, 30);
mc.world.playSound(entity.getPosX(), entity.getPosY(), entity.getPosZ(), SoundEvents.ITEM_TOTEM_USE, entity.getSoundCategory(), 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
}
});
ctx.get().setPacketHandled(true);
2019-03-20 20:51:24 +01:00
}
2020-01-22 01:35:47 +01:00
}