SubServers-2/SubServers.Client/Bukkit/src/net/ME1312/SubServers/Client/Bukkit/Network/Packet/PacketLinkServer.java

90 lines
3.2 KiB
Java
Raw Normal View History

2016-12-20 00:31:01 +01:00
package net.ME1312.SubServers.Client.Bukkit.Network.Packet;
2019-04-18 16:02:09 +02:00
import net.ME1312.Galaxi.Library.Map.ObjectMap;
import net.ME1312.Galaxi.Library.Util;
2019-05-11 23:23:31 +02:00
import net.ME1312.SubData.Client.Protocol.Initial.InitialPacket;
2019-04-18 16:02:09 +02:00
import net.ME1312.SubData.Client.Protocol.PacketObjectIn;
import net.ME1312.SubData.Client.Protocol.PacketObjectOut;
2019-10-19 00:24:58 +02:00
import net.ME1312.SubData.Client.SubDataSender;
2019-01-27 03:04:53 +01:00
import net.ME1312.SubServers.Client.Bukkit.SubAPI;
2016-12-20 00:31:01 +01:00
import net.ME1312.SubServers.Client.Bukkit.SubPlugin;
2020-11-16 21:34:59 +01:00
2016-12-20 00:31:01 +01:00
import org.bukkit.Bukkit;
/**
* Link Server Packet
*/
2019-05-11 23:23:31 +02:00
public class PacketLinkServer implements InitialPacket, PacketObjectIn<Integer>, PacketObjectOut<Integer> {
2016-12-20 00:31:01 +01:00
private SubPlugin plugin;
2019-05-10 04:43:34 +02:00
private int channel;
2016-12-20 00:31:01 +01:00
/**
2019-05-10 04:43:34 +02:00
* New PacketLinkServer (In)
*
* @param plugin SubServers.Client
*/
2016-12-20 00:31:01 +01:00
public PacketLinkServer(SubPlugin plugin) {
if (Util.isNull(plugin)) throw new NullPointerException();
2016-12-20 00:31:01 +01:00
this.plugin = plugin;
}
2019-05-10 04:43:34 +02:00
/**
* New PacketLinkServer (Out)
*
* @param plugin SubServers.Client
* @param channel Channel ID
*/
public PacketLinkServer(SubPlugin plugin, int channel) {
if (Util.isNull(plugin)) throw new NullPointerException();
this.plugin = plugin;
this.channel = channel;
}
2016-12-20 00:31:01 +01:00
@Override
2019-10-19 00:24:58 +02:00
public ObjectMap<Integer> send(SubDataSender client) {
2019-04-18 16:02:09 +02:00
ObjectMap<Integer> json = new ObjectMap<Integer>();
if (plugin.api.getName() != null) json.set(0x0000, plugin.api.getName());
String address = plugin.server_address;
if (address != null) {
if (address.indexOf(':') == -1) address += ":" + Bukkit.getServer().getPort();
json.set(0x0001, address);
} else {
json.set(0x0001, Bukkit.getServer().getPort());
}
2019-05-10 04:43:34 +02:00
json.set(0x0002, channel);
2016-12-20 00:31:01 +01:00
return json;
}
@Override
2019-10-19 00:24:58 +02:00
public void receive(SubDataSender client, ObjectMap<Integer> data) {
2019-04-18 16:02:09 +02:00
if (data.getInt(0x0001) == 0) {
try {
if (data.contains(0x0000)) Util.reflect(SubAPI.class.getDeclaredField("name"), plugin.api, data.getRawString(0x0000));
2021-01-21 20:28:23 +01:00
setReady(client.getConnection());
2019-05-11 23:23:31 +02:00
} catch (Throwable e) {
e.printStackTrace();
}
2017-04-01 22:31:57 +02:00
} else {
2019-04-18 16:02:09 +02:00
Bukkit.getLogger().info("SubData > Could not link name with server" + ((data.contains(0x0002))?": "+data.getRawString(0x0002):'.'));
try {
2019-04-18 16:02:09 +02:00
if (data.getInt(0x0001) == 2) {
if (!plugin.config.get().getMap("Settings").getMap("SubData").contains("Name")) {
plugin.config.get().getMap("Settings").getMap("SubData").set("Name", "");
plugin.config.save();
}
2019-04-18 16:02:09 +02:00
if (plugin.config.get().getMap("Settings").getMap("SubData").getRawString("Name").length() <= 0)
2019-01-27 03:04:53 +01:00
Bukkit.getLogger().info("SubData > Use the server \"Name\" option to override auto-linking");
}
} catch (Exception e) {}
2019-04-18 16:02:09 +02:00
new IllegalStateException().printStackTrace();
plugin.onDisable();
2016-12-20 00:31:01 +01:00
}
}
@Override
2019-04-18 16:02:09 +02:00
public int version() {
return 0x0001;
2016-12-20 00:31:01 +01:00
}
}