mirror of
https://github.com/GTNewHorizons/Botania.git
synced 2026-11-18 19:29:31 +00:00
packets
This commit is contained in:
1 parent
a219aad6ee
commit
22a0c2026f
5 files changed
+128
-139
No files matched your search
@@ -3,16 +3,14 @@ package vazkii.botania.common.network;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import net.minecraft.client.Minecraft;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.init.Particles;
|
||||
import net.minecraft.item.EnumDyeColor;
|
||||
import net.minecraft.network.PacketBuffer;
|
||||
import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraft.util.EnumParticleTypes;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.world.World;
|
||||
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;
|
||||
import net.minecraftforge.fml.network.NetworkEvent;
|
||||
import vazkii.botania.common.Botania;
|
||||
import vazkii.botania.common.block.tile.TileTerraPlate;
|
||||
import vazkii.botania.common.core.handler.ConfigHandler;
|
||||
@@ -21,18 +19,17 @@ import vazkii.botania.common.entity.EntityDoppleganger;
|
||||
import vazkii.botania.common.item.ItemTwigWand;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
// Prefer using World.addBlockEvent/Block.eventReceived/TileEntity.receiveClientEvent where possible
|
||||
// as those use less network bandwidth (~14 bytes), vs 26+ bytes here
|
||||
public class PacketBotaniaEffect implements IMessage {
|
||||
public class PacketBotaniaEffect {
|
||||
|
||||
private EffectType type;
|
||||
private double x;
|
||||
private double y;
|
||||
private double z;
|
||||
private int[] args;
|
||||
|
||||
public PacketBotaniaEffect() {}
|
||||
private final EffectType type;
|
||||
private final double x;
|
||||
private final double y;
|
||||
private final double z;
|
||||
private final int[] args;
|
||||
|
||||
public PacketBotaniaEffect(EffectType type, double x, double y, double z, int... args) {
|
||||
this.type = type;
|
||||
@@ -42,50 +39,47 @@ public class PacketBotaniaEffect implements IMessage {
|
||||
this.args = args;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void fromBytes(ByteBuf buf) {
|
||||
type = EffectType.values()[buf.readByte()];
|
||||
x = buf.readDouble();
|
||||
y = buf.readDouble();
|
||||
z = buf.readDouble();
|
||||
args = new int[type.argCount];
|
||||
public static PacketBotaniaEffect decode(PacketBuffer buf) {
|
||||
EffectType type = EffectType.values()[buf.readByte()];
|
||||
double x = buf.readDouble();
|
||||
double y = buf.readDouble();
|
||||
double z = buf.readDouble();
|
||||
int[] args = new int[type.argCount];
|
||||
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
args[i] = ByteBufUtils.readVarInt(buf, 5);
|
||||
args[i] = buf.readVarInt();
|
||||
}
|
||||
return new PacketBotaniaEffect(type, x, y, z, args);
|
||||
}
|
||||
|
||||
public static void encode(PacketBotaniaEffect msg, PacketBuffer buf) {
|
||||
buf.writeByte(msg.type.ordinal());
|
||||
buf.writeDouble(msg.x);
|
||||
buf.writeDouble(msg.y);
|
||||
buf.writeDouble(msg.z);
|
||||
|
||||
for (int i = 0; i < msg.type.argCount; i++) {
|
||||
buf.writeVarInt(msg.args[i]);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void toBytes(ByteBuf buf) {
|
||||
buf.writeByte(type.ordinal());
|
||||
buf.writeDouble(x);
|
||||
buf.writeDouble(y);
|
||||
buf.writeDouble(z);
|
||||
|
||||
for (int i = 0; i < type.argCount; i++) {
|
||||
ByteBufUtils.writeVarInt(buf, args[i], 5);
|
||||
}
|
||||
}
|
||||
|
||||
public static class Handler implements IMessageHandler<PacketBotaniaEffect, IMessage> {
|
||||
|
||||
@Override
|
||||
public IMessage onMessage(final PacketBotaniaEffect message, final MessageContext ctx) {
|
||||
Minecraft.getMinecraft().addScheduledTask(new Runnable() {
|
||||
public static class Handler {
|
||||
public static void handle(final PacketBotaniaEffect message, final Supplier<NetworkEvent.Context> ctx) {
|
||||
ctx.get().enqueueWork(new Runnable() {
|
||||
// Use anon - lambda causes classloading issues
|
||||
@Override
|
||||
public void run() {
|
||||
Minecraft mc = Minecraft.getMinecraft();
|
||||
Minecraft mc = Minecraft.getInstance();
|
||||
World world = mc.world;
|
||||
switch (message.type) {
|
||||
case PAINT_LENS: {
|
||||
EnumDyeColor placeColor = EnumDyeColor.byMetadata(message.args[0]);
|
||||
EnumDyeColor placeColor = EnumDyeColor.byId(message.args[0]);
|
||||
int hex = placeColor.getColorValue();
|
||||
int r = (hex & 0xFF0000) >> 16;
|
||||
int g = (hex & 0xFF00) >> 8;
|
||||
int b = hex & 0xFF;
|
||||
for(int i = 0; i < 10; i++) {
|
||||
BlockPos pos = new BlockPos(message.x, message.y, message.z).offset(EnumFacing.VALUES[world.rand.nextInt(6)]);
|
||||
BlockPos pos = new BlockPos(message.x, message.y, message.z).offset(EnumFacing.BY_INDEX[world.rand.nextInt(6)]);
|
||||
Botania.proxy.sparkleFX(
|
||||
pos.getX() + (float) Math.random(), pos.getY() + (float) Math.random(), pos.getZ() + (float) Math.random(),
|
||||
r / 255F, g / 255F, b / 255F, 0.6F + (float) Math.random() * 0.5F, 5);
|
||||
@@ -118,7 +112,7 @@ public class PacketBotaniaEffect implements IMessage {
|
||||
double d1 = item.world.rand.nextGaussian() * m;
|
||||
double d2 = item.world.rand.nextGaussian() * m;
|
||||
double d3 = 10.0D;
|
||||
item.world.spawnParticle(EnumParticleTypes.EXPLOSION_NORMAL,
|
||||
item.world.spawnParticle(Particles.POOF,
|
||||
message.x + item.world.rand.nextFloat() * item.width * 2.0F - item.width - d0 * d3, message.y + item.world.rand.nextFloat() * item.height - d1 * d3,
|
||||
message.z + item.world.rand.nextFloat() * item.width * 2.0F - item.width - d2 * d3, d0, d1, d2);
|
||||
}
|
||||
@@ -249,13 +243,13 @@ public class PacketBotaniaEffect implements IMessage {
|
||||
break;
|
||||
}
|
||||
case PARTICLE_BEAM: {
|
||||
ItemTwigWand.doParticleBeam(Minecraft.getMinecraft().world,
|
||||
ItemTwigWand.doParticleBeam(Minecraft.getInstance().world,
|
||||
new Vector3(message.x, message.y, message.z),
|
||||
new Vector3(message.args[0] + 0.5, message.args[1] + 0.5, message.args[2] + 0.5));
|
||||
break;
|
||||
}
|
||||
case DIVA_EFFECT: {
|
||||
Entity target = Minecraft.getMinecraft().world.getEntityByID(message.args[0]);
|
||||
Entity target = Minecraft.getInstance().world.getEntityByID(message.args[0]);
|
||||
if(target == null)
|
||||
break;
|
||||
|
||||
@@ -271,7 +265,7 @@ public class PacketBotaniaEffect implements IMessage {
|
||||
}
|
||||
});
|
||||
|
||||
return null;
|
||||
ctx.get().setPacketHandled(true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -14,47 +14,44 @@ import baubles.api.BaublesApi;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.network.PacketBuffer;
|
||||
import net.minecraft.util.SoundCategory;
|
||||
import net.minecraft.util.text.TextComponentString;
|
||||
import net.minecraft.util.text.TextComponentTranslation;
|
||||
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.fml.network.NetworkEvent;
|
||||
import net.minecraftforge.items.IItemHandler;
|
||||
import vazkii.botania.common.core.handler.ModSounds;
|
||||
import vazkii.botania.common.core.helper.ItemNBTHelper;
|
||||
import vazkii.botania.common.item.ModItems;
|
||||
import vazkii.botania.common.item.equipment.bauble.ItemDodgeRing;
|
||||
import vazkii.botania.common.item.equipment.tool.terrasteel.ItemTerraSword;
|
||||
|
||||
public class PacketDodge implements IMessage {
|
||||
import java.util.function.Supplier;
|
||||
|
||||
@Override
|
||||
public void fromBytes(ByteBuf buf) {}
|
||||
public class PacketDodge {
|
||||
public static void encode(PacketDodge msg, PacketBuffer buf) {}
|
||||
|
||||
@Override
|
||||
public void toBytes(ByteBuf buf) {}
|
||||
|
||||
public static class Handler implements IMessageHandler<PacketDodge, IMessage> {
|
||||
|
||||
@Override
|
||||
public IMessage onMessage(PacketDodge message, MessageContext ctx) {
|
||||
EntityPlayerMP player = ctx.getServerHandler().player;
|
||||
player.server.addScheduledTask(() -> {
|
||||
player.world.playSound(null, player.posX, player.posY, player.posZ, ModSounds.dash, SoundCategory.PLAYERS, 1F, 1F);
|
||||
|
||||
IItemHandler baublesInv = BaublesApi.getBaublesHandler(player);
|
||||
ItemStack ringStack = baublesInv.getStackInSlot(1);
|
||||
if(ringStack.isEmpty() || !(ringStack.getItem() instanceof ItemDodgeRing)) {
|
||||
ringStack = baublesInv.getStackInSlot(2);
|
||||
if(ringStack.isEmpty() || !(ringStack.getItem() instanceof ItemDodgeRing))
|
||||
ctx.getServerHandler().disconnect(new TextComponentTranslation("botaniamisc.invalidDodge"));
|
||||
return;
|
||||
}
|
||||
|
||||
player.addExhaustion(0.3F);
|
||||
ItemNBTHelper.setInt(ringStack, ItemDodgeRing.TAG_DODGE_COOLDOWN, ItemDodgeRing.MAX_CD);
|
||||
});
|
||||
return null;
|
||||
}
|
||||
public static PacketDodge decode(PacketBuffer buf) {
|
||||
return new PacketDodge();
|
||||
}
|
||||
|
||||
public static void handle(PacketDodge msg, Supplier<NetworkEvent.Context> ctx) {
|
||||
ctx.get().enqueueWork(() -> {
|
||||
EntityPlayerMP player = ctx.get().getSender();
|
||||
player.world.playSound(null, player.posX, player.posY, player.posZ, ModSounds.dash, SoundCategory.PLAYERS, 1F, 1F);
|
||||
|
||||
IItemHandler baublesInv = BaublesApi.getBaublesHandler(player);
|
||||
ItemStack ringStack = baublesInv.getStackInSlot(1);
|
||||
if(ringStack.isEmpty() || !(ringStack.getItem() instanceof ItemDodgeRing)) {
|
||||
ringStack = baublesInv.getStackInSlot(2);
|
||||
if(ringStack.isEmpty() || !(ringStack.getItem() instanceof ItemDodgeRing))
|
||||
player.connection.disconnect(new TextComponentTranslation("botaniamisc.invalidDodge"));
|
||||
return;
|
||||
}
|
||||
|
||||
player.addExhaustion(0.3F);
|
||||
ItemNBTHelper.setInt(ringStack, ItemDodgeRing.TAG_DODGE_COOLDOWN, ItemDodgeRing.MAX_CD);
|
||||
});
|
||||
ctx.get().setPacketHandled(true);
|
||||
}
|
||||
}
|
||||
@@ -3,30 +3,36 @@ package vazkii.botania.common.network;
|
||||
import net.minecraft.entity.Entity;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
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.api.distmarker.Dist;
|
||||
import net.minecraftforge.fml.network.NetworkDirection;
|
||||
import net.minecraftforge.fml.network.NetworkRegistry;
|
||||
import net.minecraftforge.fml.network.simple.SimpleChannel;
|
||||
import vazkii.botania.common.lib.LibMisc;
|
||||
|
||||
public final class PacketHandler {
|
||||
|
||||
private static final SimpleNetworkWrapper HANDLER = new SimpleNetworkWrapper(LibMisc.NETWORK_CHANNEL);
|
||||
private static final String PROTOCOL = "1";
|
||||
private static final SimpleChannel HANDLER = NetworkRegistry.newSimpleChannel(
|
||||
new ResourceLocation(LibMisc.MOD_ID, "chan"),
|
||||
() -> PROTOCOL,
|
||||
PROTOCOL::equals,
|
||||
PROTOCOL::equals
|
||||
);
|
||||
|
||||
public static void init() {
|
||||
int id = 0;
|
||||
HANDLER.registerMessage(PacketBotaniaEffect.Handler.class, PacketBotaniaEffect.class, id++, Side.CLIENT);
|
||||
HANDLER.registerMessage(PacketLeftClick.Handler.class, PacketLeftClick.class, id++, Side.SERVER);
|
||||
HANDLER.registerMessage(PacketDodge.Handler.class, PacketDodge.class, id++, Side.SERVER);
|
||||
HANDLER.registerMessage(PacketJump.Handler.class, PacketJump.class, id++, Side.SERVER);
|
||||
HANDLER.registerMessage(id++, PacketBotaniaEffect.class, PacketBotaniaEffect::encode, PacketBotaniaEffect::decode, PacketBotaniaEffect.Handler::handle);
|
||||
HANDLER.registerMessage(id++, PacketLeftClick.class, PacketLeftClick::encode, PacketLeftClick::decode, PacketLeftClick::handle);
|
||||
HANDLER.registerMessage(id++, PacketDodge.class, PacketDodge::encode, PacketDodge::decode, PacketDodge::handle);
|
||||
HANDLER.registerMessage(id++, PacketJump.class, PacketJump::encode, PacketJump::decode, PacketJump::handle);
|
||||
}
|
||||
|
||||
/**
|
||||
* Send message to all within 64 blocks that have this chunk loaded
|
||||
*/
|
||||
public static void sendToNearby(World world, BlockPos pos, IMessage toSend) {
|
||||
public static void sendToNearby(World world, BlockPos pos, Object toSend) {
|
||||
if(world instanceof WorldServer) {
|
||||
WorldServer ws = (WorldServer) world;
|
||||
|
||||
@@ -35,22 +41,22 @@ public final class PacketHandler {
|
||||
|
||||
if (playerMP.getDistanceSq(pos) < 64 * 64
|
||||
&& ws.getPlayerChunkMap().isPlayerWatchingChunk(playerMP, pos.getX() >> 4, pos.getZ() >> 4)) {
|
||||
HANDLER.sendTo(toSend, playerMP);
|
||||
HANDLER.sendTo(toSend, playerMP.connection.getNetworkManager(), NetworkDirection.PLAY_TO_CLIENT);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public static void sendToNearby(World world, Entity e, IMessage toSend) {
|
||||
public static void sendToNearby(World world, Entity e, Object toSend) {
|
||||
sendToNearby(world, new BlockPos(e), toSend);
|
||||
}
|
||||
|
||||
public static void sendTo(EntityPlayerMP playerMP, IMessage toSend) {
|
||||
HANDLER.sendTo(toSend, playerMP);
|
||||
public static void sendTo(EntityPlayerMP playerMP, Object toSend) {
|
||||
HANDLER.sendTo(toSend, playerMP.connection.getNetworkManager(), NetworkDirection.PLAY_TO_CLIENT);
|
||||
}
|
||||
|
||||
public static void sendToServer(IMessage msg) {
|
||||
public static void sendToServer(Object msg) {
|
||||
HANDLER.sendToServer(msg);
|
||||
}
|
||||
|
||||
|
||||
@@ -14,42 +14,39 @@ import baubles.api.BaublesApi;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
import net.minecraft.item.ItemStack;
|
||||
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.fml.network.NetworkEvent;
|
||||
import net.minecraftforge.items.IItemHandler;
|
||||
import vazkii.botania.common.item.ModItems;
|
||||
import vazkii.botania.common.item.equipment.bauble.CloudPendantShim;
|
||||
import vazkii.botania.common.item.equipment.bauble.ItemTravelBelt;
|
||||
import vazkii.botania.common.item.equipment.tool.terrasteel.ItemTerraSword;
|
||||
|
||||
public class PacketJump implements IMessage {
|
||||
import java.util.function.Supplier;
|
||||
|
||||
@Override
|
||||
public void fromBytes(ByteBuf buf) {}
|
||||
public class PacketJump {
|
||||
public static void encode(PacketJump msg, PacketBuffer buf) {}
|
||||
|
||||
@Override
|
||||
public void toBytes(ByteBuf buf) {}
|
||||
|
||||
public static class Handler implements IMessageHandler<PacketJump, IMessage> {
|
||||
|
||||
@Override
|
||||
public IMessage onMessage(PacketJump message, MessageContext ctx) {
|
||||
EntityPlayerMP player = ctx.getServerHandler().player;
|
||||
player.server.addScheduledTask(() -> {
|
||||
IItemHandler baublesInv = BaublesApi.getBaublesHandler(player);
|
||||
ItemStack amuletStack = baublesInv.getStackInSlot(0);
|
||||
|
||||
if(!amuletStack.isEmpty() && amuletStack.getItem() instanceof CloudPendantShim) {
|
||||
player.addExhaustion(0.3F);
|
||||
player.fallDistance = 0;
|
||||
|
||||
ItemStack belt = BaublesApi.getBaublesHandler(player).getStackInSlot(3);
|
||||
|
||||
if(!belt.isEmpty() && belt.getItem() instanceof ItemTravelBelt)
|
||||
player.fallDistance = -((ItemTravelBelt) belt.getItem()).fallBuffer * ((CloudPendantShim) amuletStack.getItem()).getMaxAllowedJumps();
|
||||
}
|
||||
});
|
||||
return null;
|
||||
}
|
||||
public static PacketJump decode(PacketBuffer buf) {
|
||||
return new PacketJump();
|
||||
}
|
||||
|
||||
public static void handle(PacketJump msg, Supplier<NetworkEvent.Context> ctx) {
|
||||
ctx.get().enqueueWork(() -> {
|
||||
EntityPlayerMP player = ctx.get().getSender();
|
||||
IItemHandler baublesInv = BaublesApi.getBaublesHandler(player);
|
||||
ItemStack amuletStack = baublesInv.getStackInSlot(0);
|
||||
|
||||
if(!amuletStack.isEmpty() && amuletStack.getItem() instanceof CloudPendantShim) {
|
||||
player.addExhaustion(0.3F);
|
||||
player.fallDistance = 0;
|
||||
|
||||
ItemStack belt = BaublesApi.getBaublesHandler(player).getStackInSlot(3);
|
||||
|
||||
if(!belt.isEmpty() && belt.getItem() instanceof ItemTravelBelt)
|
||||
player.fallDistance = -((ItemTravelBelt) belt.getItem()).fallBuffer * ((CloudPendantShim) amuletStack.getItem()).getMaxAllowedJumps();
|
||||
}
|
||||
});
|
||||
ctx.get().setPacketHandled(true);
|
||||
}
|
||||
}
|
||||
@@ -1,28 +1,23 @@
|
||||
package vazkii.botania.common.network;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import net.minecraft.entity.player.EntityPlayerMP;
|
||||
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.fml.network.NetworkEvent;
|
||||
import vazkii.botania.common.item.ModItems;
|
||||
import vazkii.botania.common.item.equipment.tool.terrasteel.ItemTerraSword;
|
||||
|
||||
public class PacketLeftClick implements IMessage {
|
||||
@Override
|
||||
public void fromBytes(ByteBuf buf) {}
|
||||
import java.util.function.Supplier;
|
||||
|
||||
@Override
|
||||
public void toBytes(ByteBuf buf) {}
|
||||
public class PacketLeftClick {
|
||||
public static void encode(PacketLeftClick msg, PacketBuffer buf) {}
|
||||
|
||||
public static class Handler implements IMessageHandler<PacketLeftClick, IMessage> {
|
||||
|
||||
@Override
|
||||
public IMessage onMessage(PacketLeftClick message, MessageContext ctx) {
|
||||
EntityPlayerMP player = ctx.getServerHandler().player;
|
||||
player.server.addScheduledTask(() -> ((ItemTerraSword) ModItems.terraSword).trySpawnBurst(player));
|
||||
return null;
|
||||
}
|
||||
public static PacketLeftClick decode(PacketBuffer buf) {
|
||||
return new PacketLeftClick();
|
||||
}
|
||||
|
||||
public static void handle(PacketLeftClick msg, Supplier<NetworkEvent.Context> ctx) {
|
||||
ctx.get().enqueueWork(() -> {
|
||||
((ItemTerraSword) ModItems.terraSword).trySpawnBurst(ctx.get().getSender());
|
||||
});
|
||||
ctx.get().setPacketHandled(true);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user