Packets!!! (#69)

This commit is contained in:
Quarris 2020-01-22 00:35:47 +00:00 committed by Ellpeck
parent 18a7cd407d
commit e4a20b1268
6 changed files with 159 additions and 168 deletions

View file

@ -0,0 +1,4 @@
package de.ellpeck.naturesaura.packet;
public interface IPacket {
}

View file

@ -1,84 +1,71 @@
/* TODO sync aura chunks
package de.ellpeck.naturesaura.packet;
import de.ellpeck.naturesaura.NaturesAura;
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
import de.ellpeck.naturesaura.chunk.AuraChunk;
import io.netty.buffer.ByteBuf;
import net.minecraft.client.Minecraft;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.chunk.Chunk;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.fml.network.NetworkEvent;
import org.apache.commons.lang3.mutable.MutableInt;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;
public class PacketAuraChunk implements IMessage {
public class PacketAuraChunk implements IPacket {
private int chunkX;
private int chunkZ;
private Map<BlockPos, MutableInt> drainSpots;
public PacketAuraChunk(int chunkX, int chunkZ, Map<BlockPos, MutableInt> drainSpots) {
this.chunkX = chunkX;
this.chunkZ = chunkZ;
this.drainSpots = drainSpots;
}
public static PacketAuraChunk fromBytes(PacketBuffer buf) {
PacketAuraChunk packet = new PacketAuraChunk();
packet.chunkX = buf.readInt();
packet.chunkZ = buf.readInt();
public PacketAuraChunk() {
}
@Override
public void fromBytes(ByteBuf buf) {
this.chunkX = buf.readInt();
this.chunkZ = buf.readInt();
this.drainSpots = new HashMap<>();
packet.drainSpots = new HashMap<>();
int amount = buf.readInt();
for (int i = 0; i < amount; i++) {
this.drainSpots.put(
packet.drainSpots.put(
BlockPos.fromLong(buf.readLong()),
new MutableInt(buf.readInt())
);
}
return packet;
}
@Override
public void toBytes(ByteBuf buf) {
buf.writeInt(this.chunkX);
buf.writeInt(this.chunkZ);
public static void toBytes(PacketAuraChunk packet, PacketBuffer buf) {
buf.writeInt(packet.chunkX);
buf.writeInt(packet.chunkZ);
buf.writeInt(this.drainSpots.size());
for (Map.Entry<BlockPos, MutableInt> entry : this.drainSpots.entrySet()) {
buf.writeInt(packet.drainSpots.size());
for (Map.Entry<BlockPos, MutableInt> entry : packet.drainSpots.entrySet()) {
buf.writeLong(entry.getKey().toLong());
buf.writeInt(entry.getValue().intValue());
}
}
public static class Handler implements IMessageHandler<PacketAuraChunk, IMessage> {
public static class Handler {
@Override
@OnlyIn(Dist.CLIENT)
public IMessage onMessage(PacketAuraChunk message, MessageContext ctx) {
NaturesAura.proxy.scheduleTask(() -> {
World world = Minecraft.getMinecraft().world;
public static void onMessage(PacketAuraChunk message, Supplier<NetworkEvent.Context> ctx) {
ctx.get().enqueueWork(() -> {
World world = Minecraft.getInstance().world;
if (world != null) {
Chunk chunk = world.getChunk(message.chunkX, message.chunkZ);
if (chunk.hasCapability(NaturesAuraAPI.capAuraChunk, null)) {
AuraChunk auraChunk = (AuraChunk) chunk.getCapability(NaturesAuraAPI.capAuraChunk, null);
if (chunk.getCapability(NaturesAuraAPI.capAuraChunk).isPresent()) {
AuraChunk auraChunk = (AuraChunk) chunk.getCapability(NaturesAuraAPI.capAuraChunk).orElse(null);
auraChunk.setSpots(message.drainSpots);
}
}
});
return null;
ctx.get().setPacketHandled(true);
}
}
}*/
}

View file

@ -1,65 +1,55 @@
/* TODO packets
package de.ellpeck.naturesaura.packet;
import de.ellpeck.naturesaura.NaturesAura;
import de.ellpeck.naturesaura.items.RangeVisualizer;
import io.netty.buffer.ByteBuf;
import net.minecraft.client.Minecraft;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.math.BlockPos;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
import net.minecraft.world.dimension.DimensionType;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.fml.network.NetworkEvent;
public class PacketClient implements IMessage {
import java.util.function.Supplier;
public class PacketClient implements IPacket {
private int type;
private int[] data;
public PacketClient(int type, int... data) {
this.type = type;
this.data = data;
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();
return client;
}
public PacketClient() {
}
@Override
public void fromBytes(ByteBuf buf) {
this.type = buf.readByte();
this.data = new int[buf.readByte()];
for (int i = 0; i < this.data.length; i++)
this.data[i] = buf.readInt();
}
@Override
public void toBytes(ByteBuf buf) {
buf.writeByte(this.type);
buf.writeByte(this.data.length);
for (int i : this.data)
public static void toBytes(PacketClient packet, PacketBuffer buf) {
buf.writeByte(packet.type);
buf.writeByte(packet.data.length);
for (int i : packet.data)
buf.writeInt(i);
}
public static class Handler implements IMessageHandler<PacketClient, IMessage> {
public static class Handler {
@Override
@OnlyIn(Dist.CLIENT)
public IMessage onMessage(PacketClient message, MessageContext ctx) {
NaturesAura.proxy.scheduleTask(() -> {
Minecraft mc = Minecraft.getMinecraft();
public static void onMessage(PacketClient message, Supplier<NetworkEvent.Context> ctx) {
ctx.get().enqueueWork(() -> {
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]);
RangeVisualizer.visualize(mc.player, RangeVisualizer.VISUALIZED_RAILS, goalDim, goalPos);
RangeVisualizer.visualize(mc.player, RangeVisualizer.VISUALIZED_RAILS, DimensionType.getById(goalDim), goalPos);
}
}
});
return null;
ctx.get().setPacketHandled(true);
}
}
}*/
}

View file

@ -1,27 +1,48 @@
package de.ellpeck.naturesaura.packet;
// TODO packets
import de.ellpeck.naturesaura.NaturesAura;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.IWorld;
import net.minecraft.world.World;
import net.minecraftforge.fml.network.NetworkRegistry;
import net.minecraftforge.fml.network.PacketDistributor;
import net.minecraftforge.fml.network.simple.SimpleChannel;
public final class PacketHandler {
/*private static SimpleNetworkWrapper network;*/
private static String version = "1";
private static SimpleChannel network;
public static void init() {
/*network = new SimpleNetworkWrapper(NaturesAura.MOD_ID);
network.registerMessage(PacketParticleStream.Handler.class, PacketParticleStream.class, 0, Dist.CLIENT);
network.registerMessage(PacketParticles.Handler.class, PacketParticles.class, 1, Dist.CLIENT);
network.registerMessage(PacketAuraChunk.Handler.class, PacketAuraChunk.class, 2, Dist.CLIENT);
network.registerMessage(PacketClient.Handler.class, PacketClient.class, 3, Dist.CLIENT);*/
network = NetworkRegistry.newSimpleChannel(
NaturesAura.createRes("network"),
() -> version,
version::equals,
version::equals);
network.registerMessage(0, PacketParticleStream.class, PacketParticleStream::toBytes, PacketParticleStream::fromBytes, PacketParticleStream.Handler::onMessage);
network.registerMessage(1, PacketParticles.class, PacketParticles::toBytes, PacketParticles::fromBytes, PacketParticles.Handler::onMessage);
network.registerMessage(2, PacketAuraChunk.class, PacketAuraChunk::toBytes, PacketAuraChunk::fromBytes, PacketAuraChunk.Handler::onMessage);
network.registerMessage(3, PacketClient.class, PacketClient::toBytes, PacketClient::fromBytes, PacketClient.Handler::onMessage);
}
/*public static void sendToAllLoaded(World world, BlockPos pos, IMessage message) {
network.sendToAllTracking(message, new NetworkRegistry.TargetPoint(world.provider.getDimension(), pos.getX(), pos.getY(), pos.getZ(), 0));
@Deprecated
public static void sendToAllLoaded(World world, BlockPos pos, IPacket message) {
sendToAllLoaded(message);
}
public static void sendToAllAround(World world, BlockPos pos, int range, IMessage message) {
network.sendToAllAround(message, new NetworkRegistry.TargetPoint(world.provider.getDimension(), pos.getX(), pos.getY(), pos.getZ(), range));
public static void sendToAllLoaded(IPacket message) {
network.send(PacketDistributor.ALL.noArg(), message);
}
public static void sendTo(PlayerEntity player, IMessage message) {
network.sendTo(message, (ServerPlayerEntity) player);
}*/
public static void sendToAllAround(IWorld world, BlockPos pos, int range, IPacket message) {
network.send(PacketDistributor.NEAR.with(
() -> new PacketDistributor.TargetPoint(pos.getX(), pos.getY(), pos.getZ(), range, world.getDimension().getType())),
message);
}
public static void sendTo(PlayerEntity player, IPacket message) {
network.send(PacketDistributor.PLAYER.with(() -> (ServerPlayerEntity) player), message);
}
}

View file

@ -1,16 +1,16 @@
/* TODO packets
package de.ellpeck.naturesaura.packet;
import de.ellpeck.naturesaura.NaturesAura;
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
import io.netty.buffer.ByteBuf;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
import net.minecraft.network.PacketBuffer;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.fml.network.NetworkEvent;
public class PacketParticleStream implements IMessage {
import java.util.function.Supplier;
public class PacketParticleStream implements IPacket {
private float startX;
private float startY;
@ -40,43 +40,44 @@ public class PacketParticleStream implements IMessage {
}
@Override
public void fromBytes(ByteBuf buf) {
this.startX = buf.readFloat();
this.startY = buf.readFloat();
this.startZ = buf.readFloat();
this.endX = buf.readFloat();
this.endY = buf.readFloat();
this.endZ = buf.readFloat();
this.speed = buf.readFloat();
this.color = buf.readInt();
this.scale = buf.readFloat();
public static PacketParticleStream fromBytes(PacketBuffer buf) {
PacketParticleStream packet = new PacketParticleStream();
packet.startX = buf.readFloat();
packet.startY = buf.readFloat();
packet.startZ = buf.readFloat();
packet.endX = buf.readFloat();
packet.endY = buf.readFloat();
packet.endZ = buf.readFloat();
packet.speed = buf.readFloat();
packet.color = buf.readInt();
packet.scale = buf.readFloat();
return packet;
}
@Override
public void toBytes(ByteBuf buf) {
buf.writeFloat(this.startX);
buf.writeFloat(this.startY);
buf.writeFloat(this.startZ);
buf.writeFloat(this.endX);
buf.writeFloat(this.endY);
buf.writeFloat(this.endZ);
buf.writeFloat(this.speed);
buf.writeInt(this.color);
buf.writeFloat(this.scale);
public static void toBytes(PacketParticleStream packet, PacketBuffer buf) {
buf.writeFloat(packet.startX);
buf.writeFloat(packet.startY);
buf.writeFloat(packet.startZ);
buf.writeFloat(packet.endX);
buf.writeFloat(packet.endY);
buf.writeFloat(packet.endZ);
buf.writeFloat(packet.speed);
buf.writeInt(packet.color);
buf.writeFloat(packet.scale);
}
public static class Handler implements IMessageHandler<PacketParticleStream, IMessage> {
public static class Handler {
@Override
@OnlyIn(Dist.CLIENT)
public IMessage onMessage(PacketParticleStream message, MessageContext ctx) {
NaturesAura.proxy.scheduleTask(() -> NaturesAuraAPI.instance().spawnParticleStream(
public static void onMessage(PacketParticleStream message, Supplier<NetworkEvent.Context> ctx) {
ctx.get().enqueueWork(() -> NaturesAuraAPI.instance().spawnParticleStream(
message.startX, message.startY, message.startZ,
message.endX, message.endY, message.endZ,
message.speed, message.color, message.scale));
return null;
ctx.get().setPacketHandled(true);
}
}
}*/
}

View file

@ -1,25 +1,24 @@
/*
package de.ellpeck.naturesaura.packet;
import de.ellpeck.naturesaura.NaturesAura;
import de.ellpeck.naturesaura.api.NaturesAuraAPI;
import de.ellpeck.naturesaura.api.aura.type.IAuraType;
import de.ellpeck.naturesaura.blocks.multi.Multiblocks;
import io.netty.buffer.ByteBuf;
import net.minecraft.block.BlockState;
import net.minecraft.client.Minecraft;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.network.PacketBuffer;
import net.minecraft.particles.ParticleTypes;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
import net.minecraft.world.biome.BiomeColors;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.fml.network.NetworkEvent;
public class PacketParticles implements IMessage {
import java.util.function.Supplier;
public class PacketParticles implements IPacket {
private float posX;
private float posY;
@ -27,58 +26,48 @@ public class PacketParticles implements IMessage {
private int type;
private int[] data;
public PacketParticles(float posX, float posY, float posZ, int type, int... data) {
this.posX = posX;
this.posY = posY;
this.posZ = posZ;
this.type = type;
this.data = data;
}
public static PacketParticles fromBytes(PacketBuffer buf) {
PacketParticles packet = new PacketParticles();
public PacketParticles() {
packet.posX = buf.readFloat();
packet.posY = buf.readFloat();
packet.posZ = buf.readFloat();
packet.type = buf.readByte();
}
@Override
public void fromBytes(ByteBuf buf) {
this.posX = buf.readFloat();
this.posY = buf.readFloat();
this.posZ = buf.readFloat();
this.type = buf.readByte();
this.data = new int[buf.readByte()];
for (int i = 0; i < this.data.length; i++) {
this.data[i] = buf.readInt();
packet.data = new int[buf.readByte()];
for (int i = 0; i < packet.data.length; i++) {
packet.data[i] = buf.readInt();
}
return packet;
}
@Override
public void toBytes(ByteBuf buf) {
buf.writeFloat(this.posX);
buf.writeFloat(this.posY);
buf.writeFloat(this.posZ);
buf.writeByte(this.type);
public static void toBytes(PacketParticles packet, PacketBuffer buf) {
buf.writeFloat(packet.posX);
buf.writeFloat(packet.posY);
buf.writeFloat(packet.posZ);
buf.writeByte(packet.type);
buf.writeByte(this.data.length);
for (int i : this.data) {
buf.writeByte(packet.data.length);
for (int i : packet.data) {
buf.writeInt(i);
}
}
public static class Handler implements IMessageHandler<PacketParticles, IMessage> {
public static class Handler {
@Override
@OnlyIn(Dist.CLIENT)
public IMessage onMessage(PacketParticles message, MessageContext ctx) {
NaturesAura.proxy.scheduleTask(() -> {
World world = Minecraft.getMinecraft().world;
public static void onMessage(PacketParticles message, Supplier<NetworkEvent.Context> ctx) {
ctx.get().enqueueWork(() -> {
World world = Minecraft.getInstance().world;
if (world != null) {
switch (message.type) {
case 0: // Tree ritual: Gold powder
BlockPos pos = new BlockPos(message.posX, message.posY, message.posZ);
Multiblocks.TREE_RITUAL.forEach(pos, 'G', (dustPos, matcher) -> {
BlockState state = world.getBlockState(dustPos);
AxisAlignedBB box = state.getBoundingBox(world, dustPos);
//AxisAlignedBB box = state.getBoundingBox(world, dustPos); // TODO
AxisAlignedBB box = state.getShape(world, dustPos).getBoundingBox();
NaturesAuraAPI.instance().spawnMagicParticle(
dustPos.getX() + box.minX + (box.maxX - box.minX) * world.rand.nextFloat(),
dustPos.getY() + 0.1F,
@ -226,7 +215,7 @@ public class PacketParticles implements IMessage {
message.posX + 0.5F,
message.posY + 0.5F,
message.posZ + 0.5F,
0.6F, BiomeColors.getFoliageColorAtPos(world, new BlockPos(sapX, sapY, sapZ)), 1.5F);
0.6F, BiomeColors.getFoliageColor(world, new BlockPos(sapX, sapY, sapZ)), 1.5F);
if (releaseAura)
for (int i = world.rand.nextInt(10) + 10; i >= 0; i--)
NaturesAuraAPI.instance().spawnMagicParticle(
@ -430,7 +419,7 @@ public class PacketParticles implements IMessage {
world.rand.nextGaussian() * 0.01F,
0x5ccc30, 1F + world.rand.nextFloat() * 1.5F, 40, 0F, false, true);
for (int i = world.rand.nextInt(10) + 10; i >= 0; i--)
world.spawnParticle(EnumParticleTypes.FIREWORKS_SPARK,
world.addParticle(ParticleTypes.FIREWORK,
message.posX, message.posY, message.posZ,
world.rand.nextGaussian() * 0.03F,
world.rand.nextGaussian() * 0.03F,
@ -438,8 +427,7 @@ public class PacketParticles implements IMessage {
}
}
});
return null;
ctx.get().setPacketHandled(true);
}
}
}*/
}