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

74 lines
2.8 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-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;
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.minecraft.world.dimension.DimensionType;
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;
private int[] data;
2020-01-22 23:21:52 +01:00
public PacketClient(int type, int... data) {
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();
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);
}
// 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
int goalDim = message.data[0];
BlockPos goalPos = new BlockPos(message.data[1], message.data[2], message.data[3]);
2020-01-26 01:41:49 +01:00
ItemRangeVisualizer.visualize(mc.player, ItemRangeVisualizer.VISUALIZED_RAILS, DimensionType.getById(goalDim), goalPos);
2020-02-07 23:50:16 +01:00
case 1:
Entity entity = mc.world.getEntityByID(message.data[0]);
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
}