mirror of
https://github.com/GTNewHorizons/Botania.git
synced 2026-11-18 03:15:07 +00:00
Some network boilerplate
This commit is contained in:
2 files changed
+123
No files matched your search
@@ -0,0 +1,78 @@
|
||||
package vazkii.botania.common.network;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraftforge.fml.common.network.ByteBufUtils;
|
||||
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
|
||||
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
|
||||
import net.minecraftforge.fml.common.network.simpleimpl.MessageContext;
|
||||
|
||||
public class PacketBotaniaEffect implements IMessage {
|
||||
|
||||
private EffectType type;
|
||||
private double x;
|
||||
private double y;
|
||||
private double z;
|
||||
private int[] args;
|
||||
|
||||
|
||||
public PacketBotaniaEffect() {}
|
||||
|
||||
public PacketBotaniaEffect(EffectType type, double x, double y, double z, int... args) {
|
||||
this.type = type;
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
this.z = z;
|
||||
this.args = args;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromBytes(ByteBuf buf) {
|
||||
type = EffectType.values()[buf.readShort()];
|
||||
x = buf.readDouble();
|
||||
y = buf.readDouble();
|
||||
z = buf.readDouble();
|
||||
args = new int[type.argCount];
|
||||
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
args[i] = ByteBufUtils.readVarInt(buf, 3);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBytes(ByteBuf buf) {
|
||||
buf.writeShort(type.ordinal());
|
||||
buf.writeDouble(x);
|
||||
buf.writeDouble(y);
|
||||
buf.writeDouble(z);
|
||||
|
||||
for (int i = 0; i < type.argCount; i++) {
|
||||
ByteBufUtils.writeVarInt(buf, args[i], 3);
|
||||
}
|
||||
}
|
||||
|
||||
public static class Handler implements IMessageHandler<PacketBotaniaEffect, IMessage> {
|
||||
|
||||
@Override
|
||||
public IMessage onMessage(PacketBotaniaEffect message, MessageContext ctx) {
|
||||
Minecraft.getMinecraft().addScheduledTask(() -> {
|
||||
|
||||
});
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public enum EffectType {
|
||||
;
|
||||
|
||||
private final int argCount;
|
||||
|
||||
EffectType(int argCount) {
|
||||
this.argCount = argCount;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package vazkii.botania.common.network;
|
||||
|
||||
import net.minecraft.client.entity.AbstractClientPlayer;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
import net.minecraft.server.management.PlayerManager;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.World;
|
||||
import net.minecraft.world.WorldServer;
|
||||
import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
|
||||
import net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper;
|
||||
import net.minecraftforge.fml.relauncher.Side;
|
||||
import vazkii.botania.common.lib.LibMisc;
|
||||
|
||||
public final class PacketHandler {
|
||||
|
||||
private static final SimpleNetworkWrapper HANDLER = new SimpleNetworkWrapper(LibMisc.MOD_ID);
|
||||
|
||||
public static void init() {
|
||||
HANDLER.registerMessage(PacketBotaniaEffect.Handler.class, PacketBotaniaEffect.class, 0, Side.CLIENT);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send message to all within 64 blocks that have this chunk loaded
|
||||
*/
|
||||
public static void sendToNearby(World world, BlockPos pos, IMessage toSend) {
|
||||
if(world instanceof WorldServer) {
|
||||
WorldServer ws = ((WorldServer) world);
|
||||
PlayerManager.PlayerInstance chunk = ws.getPlayerChunkMap().getEntry(pos.getX() >> 4, pos.getZ() >> 4);
|
||||
|
||||
for (EntityPlayer player : ws.playerEntities) {
|
||||
EntityPlayerMP playerMP = ((EntityPlayerMP) player);
|
||||
|
||||
if (playerMP.getDistanceSq(pos) < 64 * 64
|
||||
&& chunk.containsPlayer(playerMP)) {
|
||||
HANDLER.sendTo(toSend, playerMP);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private PacketHandler() {}
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user