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

56 lines
1.9 KiB
Java
Raw Normal View History

2019-03-20 20:51:24 +01:00
package de.ellpeck.naturesaura.packet;
2019-11-04 19:08:49 +01:00
import de.ellpeck.naturesaura.items.RangeVisualizer;
2019-03-20 20:51:24 +01:00
import net.minecraft.client.Minecraft;
2020-01-22 01:35:47 +01:00
import net.minecraft.network.PacketBuffer;
2019-03-20 20:51:24 +01:00
import net.minecraft.util.math.BlockPos;
2020-01-22 01:35:47 +01:00
import net.minecraft.world.dimension.DimensionType;
2019-10-20 22:30:49 +02:00
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
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;
public class PacketClient implements IPacket {
2019-03-20 20:51:24 +01:00
private int type;
private int[] data;
2020-01-22 01:35:47 +01:00
public static PacketClient fromBytes(PacketBuffer buf) {
PacketClient client = new PacketClient();
client.type = buf.readByte();
client.data = new int[buf.readByte()];
for (int i = 0; i < client.data.length; i++)
client.data[i] = buf.readInt();
2019-03-20 20:51:24 +01:00
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);
buf.writeByte(packet.data.length);
for (int i : packet.data)
2019-03-20 20:51:24 +01:00
buf.writeInt(i);
}
2020-01-22 01:35:47 +01:00
public static class Handler {
2019-03-20 20:51:24 +01:00
2019-10-20 22:30:49 +02:00
@OnlyIn(Dist.CLIENT)
2020-01-22 01:35:47 +01:00
public static void onMessage(PacketClient message, Supplier<NetworkEvent.Context> ctx) {
ctx.get().enqueueWork(() -> {
Minecraft mc = Minecraft.getInstance();
if (mc.world != null) {
2019-03-20 20:51:24 +01:00
switch (message.type) {
case 0: // dimension rail visualization
int goalDim = message.data[0];
BlockPos goalPos = new BlockPos(message.data[1], message.data[2], message.data[3]);
2020-01-22 01:35:47 +01:00
RangeVisualizer.visualize(mc.player, RangeVisualizer.VISUALIZED_RAILS, DimensionType.getById(goalDim), goalPos);
2019-03-20 20:51:24 +01:00
}
}
});
2020-01-22 01:35:47 +01:00
ctx.get().setPacketHandled(true);
2019-03-20 20:51:24 +01:00
}
}
2020-01-22 01:35:47 +01:00
}