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

View file

@ -1,65 +1,55 @@
/* TODO packets
package de.ellpeck.naturesaura.packet; package de.ellpeck.naturesaura.packet;
import de.ellpeck.naturesaura.NaturesAura;
import de.ellpeck.naturesaura.items.RangeVisualizer; import de.ellpeck.naturesaura.items.RangeVisualizer;
import io.netty.buffer.ByteBuf;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.network.PacketBuffer;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage; import net.minecraft.world.dimension.DimensionType;
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.Dist;
import net.minecraftforge.api.distmarker.OnlyIn; 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 type;
private int[] data; private int[] data;
public PacketClient(int type, int... data) { public static PacketClient fromBytes(PacketBuffer buf) {
this.type = type; PacketClient client = new PacketClient();
this.data = data; 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() { public static void toBytes(PacketClient packet, PacketBuffer buf) {
buf.writeByte(packet.type);
} buf.writeByte(packet.data.length);
for (int i : packet.data)
@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)
buf.writeInt(i); buf.writeInt(i);
} }
public static class Handler implements IMessageHandler<PacketClient, IMessage> { public static class Handler {
@Override
@OnlyIn(Dist.CLIENT) @OnlyIn(Dist.CLIENT)
public IMessage onMessage(PacketClient message, MessageContext ctx) { public static void onMessage(PacketClient message, Supplier<NetworkEvent.Context> ctx) {
NaturesAura.proxy.scheduleTask(() -> { ctx.get().enqueueWork(() -> {
Minecraft mc = Minecraft.getMinecraft(); Minecraft mc = Minecraft.getInstance();
if (mc.world != null) { if (mc.world != null) {
switch (message.type) { switch (message.type) {
case 0: // dimension rail visualization case 0: // dimension rail visualization
int goalDim = message.data[0]; int goalDim = message.data[0];
BlockPos goalPos = new BlockPos(message.data[1], message.data[2], message.data[3]); 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; 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 { public final class PacketHandler {
/*private static SimpleNetworkWrapper network;*/ private static String version = "1";
private static SimpleChannel network;
public static void init() { public static void init() {
/*network = new SimpleNetworkWrapper(NaturesAura.MOD_ID); network = NetworkRegistry.newSimpleChannel(
network.registerMessage(PacketParticleStream.Handler.class, PacketParticleStream.class, 0, Dist.CLIENT); NaturesAura.createRes("network"),
network.registerMessage(PacketParticles.Handler.class, PacketParticles.class, 1, Dist.CLIENT); () -> version,
network.registerMessage(PacketAuraChunk.Handler.class, PacketAuraChunk.class, 2, Dist.CLIENT); version::equals,
network.registerMessage(PacketClient.Handler.class, PacketClient.class, 3, Dist.CLIENT);*/ 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) { @Deprecated
network.sendToAllTracking(message, new NetworkRegistry.TargetPoint(world.provider.getDimension(), pos.getX(), pos.getY(), pos.getZ(), 0)); public static void sendToAllLoaded(World world, BlockPos pos, IPacket message) {
sendToAllLoaded(message);
} }
public static void sendToAllAround(World world, BlockPos pos, int range, IMessage message) { public static void sendToAllLoaded(IPacket message) {
network.sendToAllAround(message, new NetworkRegistry.TargetPoint(world.provider.getDimension(), pos.getX(), pos.getY(), pos.getZ(), range)); network.send(PacketDistributor.ALL.noArg(), message);
} }
public static void sendTo(PlayerEntity player, IMessage message) { public static void sendToAllAround(IWorld world, BlockPos pos, int range, IPacket message) {
network.sendTo(message, (ServerPlayerEntity) player); 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; package de.ellpeck.naturesaura.packet;
import de.ellpeck.naturesaura.NaturesAura; import de.ellpeck.naturesaura.NaturesAura;
import de.ellpeck.naturesaura.api.NaturesAuraAPI; import de.ellpeck.naturesaura.api.NaturesAuraAPI;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage; import net.minecraft.network.PacketBuffer;
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.Dist;
import net.minecraftforge.api.distmarker.OnlyIn; 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 startX;
private float startY; private float startY;
@ -40,43 +40,44 @@ public class PacketParticleStream implements IMessage {
} }
@Override public static PacketParticleStream fromBytes(PacketBuffer buf) {
public void fromBytes(ByteBuf buf) { PacketParticleStream packet = new PacketParticleStream();
this.startX = buf.readFloat();
this.startY = buf.readFloat(); packet.startX = buf.readFloat();
this.startZ = buf.readFloat(); packet.startY = buf.readFloat();
this.endX = buf.readFloat(); packet.startZ = buf.readFloat();
this.endY = buf.readFloat(); packet.endX = buf.readFloat();
this.endZ = buf.readFloat(); packet.endY = buf.readFloat();
this.speed = buf.readFloat(); packet.endZ = buf.readFloat();
this.color = buf.readInt(); packet.speed = buf.readFloat();
this.scale = buf.readFloat(); packet.color = buf.readInt();
packet.scale = buf.readFloat();
return packet;
} }
@Override public static void toBytes(PacketParticleStream packet, PacketBuffer buf) {
public void toBytes(ByteBuf buf) { buf.writeFloat(packet.startX);
buf.writeFloat(this.startX); buf.writeFloat(packet.startY);
buf.writeFloat(this.startY); buf.writeFloat(packet.startZ);
buf.writeFloat(this.startZ); buf.writeFloat(packet.endX);
buf.writeFloat(this.endX); buf.writeFloat(packet.endY);
buf.writeFloat(this.endY); buf.writeFloat(packet.endZ);
buf.writeFloat(this.endZ); buf.writeFloat(packet.speed);
buf.writeFloat(this.speed); buf.writeInt(packet.color);
buf.writeInt(this.color); buf.writeFloat(packet.scale);
buf.writeFloat(this.scale);
} }
public static class Handler implements IMessageHandler<PacketParticleStream, IMessage> { public static class Handler {
@Override
@OnlyIn(Dist.CLIENT) @OnlyIn(Dist.CLIENT)
public IMessage onMessage(PacketParticleStream message, MessageContext ctx) { public static void onMessage(PacketParticleStream message, Supplier<NetworkEvent.Context> ctx) {
NaturesAura.proxy.scheduleTask(() -> NaturesAuraAPI.instance().spawnParticleStream( ctx.get().enqueueWork(() -> NaturesAuraAPI.instance().spawnParticleStream(
message.startX, message.startY, message.startZ, message.startX, message.startY, message.startZ,
message.endX, message.endY, message.endZ, message.endX, message.endY, message.endZ,
message.speed, message.color, message.scale)); message.speed, message.color, message.scale));
return null; ctx.get().setPacketHandled(true);
} }
} }
}*/ }

View file

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