Skip to content

Commit

Permalink
Meger shared code into common
Browse files Browse the repository at this point in the history
  • Loading branch information
Hendrix-Shen committed Nov 15, 2022
1 parent c16cda0 commit 1a43dd4
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -1,30 +1,19 @@
package top.catowncraft.autoworldmap.bungee.handler;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.event.PluginMessageEvent;
import net.md_5.bungee.api.plugin.Listener;
import net.md_5.bungee.event.EventHandler;
import top.catowncraft.autoworldmap.common.SharedConstant;

import java.nio.charset.StandardCharsets;
import top.catowncraft.autoworldmap.common.helper.PacketCreator;

public class VoxelMapHandler implements Listener {
@EventHandler
public void onPluginMessage(PluginMessageEvent event) {
if (event.getSender() instanceof ProxiedPlayer) {
if (event.getTag().equals(SharedConstant.VOXEL_MAP_CHANNEL)) {
ProxiedPlayer player = (ProxiedPlayer) event.getSender();
byte[] serverName = player.getServer().getInfo().getName().getBytes(StandardCharsets.UTF_8);
ByteBuf buf = Unpooled.buffer();
buf.writeByte(0);
buf.writeByte(serverName.length);
buf.writeBytes(serverName);
byte[] voxelArray = buf.array();
buf.release();
player.sendData(SharedConstant.VOXEL_MAP_CHANNEL, voxelArray);
}
if (event.getSender() instanceof ProxiedPlayer && event.getTag().equals(SharedConstant.VOXEL_MAP_CHANNEL)) {
ProxiedPlayer player = (ProxiedPlayer) event.getSender();
player.sendData(SharedConstant.VOXEL_MAP_CHANNEL,
PacketCreator.voxelMap(player.getServer().getInfo().getName()));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,17 @@

import dev.simplix.protocolize.api.listener.PacketReceiveEvent;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import net.md_5.bungee.api.config.ServerInfo;
import top.catowncraft.autoworldmap.common.SharedConstant;
import top.catowncraft.autoworldmap.common.event.IClientboundSetDefaultSpawnPositionEvent;
import top.catowncraft.autoworldmap.common.helper.PacketCreator;
import top.catowncraft.autoworldmap.common.packet.ClientboundCustomPayloadPacket;
import top.catowncraft.autoworldmap.common.packet.ClientboundSetDefaultSpawnPositionPacket;

import java.nio.charset.StandardCharsets;
import java.util.zip.CRC32;

public class XaeroMapHandler implements IClientboundSetDefaultSpawnPositionEvent {
@Override
public void onEvent(PacketReceiveEvent<ClientboundSetDefaultSpawnPositionPacket> packetReceiveEvent) {
byte[] serverName = ((ServerInfo) packetReceiveEvent.server()).getName().getBytes(StandardCharsets.UTF_8);
CRC32 crc32 = new CRC32();
crc32.update(serverName, 0, serverName.length);
ByteBuf buf = Unpooled.buffer();
buf.writeByte(0);
buf.writeInt((int) crc32.getValue());
ByteBuf buf = PacketCreator.xaeroMap(((ServerInfo) packetReceiveEvent.server()).getName());
packetReceiveEvent.player().sendPacket(new ClientboundCustomPayloadPacket(SharedConstant.XAERO_MINI_MAP_CHANNEL, buf));
packetReceiveEvent.player().sendPacket(new ClientboundCustomPayloadPacket(SharedConstant.XAERO_WORLD_MAP_CHANNEL, buf));
buf.release();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package top.catowncraft.autoworldmap.common.helper;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

import java.nio.charset.StandardCharsets;
import java.util.zip.CRC32;

public class PacketCreator {
public static byte[] voxelMap(String serverName) {
byte[] serverNameByte = serverName.getBytes(StandardCharsets.UTF_8);
ByteBuf buf = Unpooled.buffer();
buf.writeByte(0);
buf.writeByte(serverNameByte.length);
buf.writeBytes(serverNameByte);
byte[] voxelArray = buf.array();
buf.release();
return voxelArray;
}

public static ByteBuf xaeroMap(String serverName) {
byte[] serverNameByte = serverName.getBytes(StandardCharsets.UTF_8);
CRC32 crc32 = new CRC32();
crc32.update(serverNameByte, 0, serverNameByte.length);
ByteBuf buf = Unpooled.buffer();
buf.writeByte(0);
buf.writeInt((int) crc32.getValue());
return buf;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@
import com.velocitypowered.api.event.connection.PluginMessageEvent;
import com.velocitypowered.api.proxy.Player;
import com.velocitypowered.api.proxy.messages.MinecraftChannelIdentifier;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import top.catowncraft.autoworldmap.AutoWorldMapVelocity;
import top.catowncraft.autoworldmap.common.SharedConstant;

import java.nio.charset.StandardCharsets;
import top.catowncraft.autoworldmap.common.helper.PacketCreator;

@Singleton
public class VoxelMapHandler {
Expand All @@ -25,16 +22,8 @@ public void onPluginMessageEvent(PluginMessageEvent pluginMessageEvent) {
if (pluginMessageEvent.getSource() instanceof Player && pluginMessageEvent.getIdentifier().equals(VOXEL_CHANNEL)) {
Player player = (Player) pluginMessageEvent.getSource();
player.getCurrentServer().ifPresent(
serverConnection -> {
byte[] serverName = serverConnection.getServerInfo().getName().getBytes(StandardCharsets.UTF_8);
ByteBuf buf = Unpooled.buffer();
buf.writeByte(0);
buf.writeByte(serverName.length);
buf.writeBytes(serverName);
byte[] bytes = buf.array();
buf.release();
player.sendPluginMessage(VoxelMapHandler.VOXEL_CHANNEL, bytes);
});
serverConnection -> player.sendPluginMessage(VoxelMapHandler.VOXEL_CHANNEL,
PacketCreator.voxelMap(serverConnection.getServerInfo().getName())));
pluginMessageEvent.setResult(PluginMessageEvent.ForwardResult.handled());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@
import com.velocitypowered.api.proxy.server.ServerInfo;
import dev.simplix.protocolize.api.listener.PacketReceiveEvent;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import top.catowncraft.autoworldmap.AutoWorldMapVelocity;
import top.catowncraft.autoworldmap.common.SharedConstant;
import top.catowncraft.autoworldmap.common.event.IClientboundSetDefaultSpawnPositionEvent;
import top.catowncraft.autoworldmap.common.helper.PacketCreator;
import top.catowncraft.autoworldmap.common.packet.ClientboundSetDefaultSpawnPositionPacket;

import java.nio.charset.StandardCharsets;
import java.util.Optional;
import java.util.zip.CRC32;

@Singleton
public class XaeroMapHandler implements IClientboundSetDefaultSpawnPositionEvent {
Expand All @@ -23,16 +21,11 @@ public class XaeroMapHandler implements IClientboundSetDefaultSpawnPositionEvent

@Override
public void onEvent(PacketReceiveEvent<ClientboundSetDefaultSpawnPositionPacket> packetReceiveEvent) {
byte[] serverName = ((ServerInfo) packetReceiveEvent.server()).getName().getBytes(StandardCharsets.UTF_8);
CRC32 crc32 = new CRC32();
crc32.update(serverName, 0, serverName.length);
ByteBuf buf = Unpooled.buffer();
buf.writeByte(0);
buf.writeInt((int) crc32.getValue());
byte[] bytes = buf.array();
buf.release();
Optional<Player> player = AutoWorldMapVelocity.getServer().getPlayer(packetReceiveEvent.player().uniqueId());
player.ifPresent(p -> {
ByteBuf buf = PacketCreator.xaeroMap(((ServerInfo) packetReceiveEvent.server()).getName());
byte[] bytes = buf.array();
buf.release();
p.sendPluginMessage(XaeroMapHandler.XAERO_MINI_MAP_CHANNEL, bytes);
p.sendPluginMessage(XaeroMapHandler.XAERO_WORLD_MAP_CHANNEL, bytes);
});
Expand Down

0 comments on commit 1a43dd4

Please sign in to comment.