2017-04-01 07:37:48 +02:00
|
|
|
package net.ME1312.SubServers.Bungee.Network.Packet;
|
|
|
|
|
2019-05-11 23:23:31 +02:00
|
|
|
import net.ME1312.SubData.Server.Protocol.Initial.InitialPacket;
|
2019-04-18 16:02:09 +02:00
|
|
|
import net.ME1312.SubData.Server.SubDataClient;
|
2019-05-10 04:43:34 +02:00
|
|
|
import net.ME1312.SubServers.Bungee.Host.External.ExternalHost;
|
2017-04-01 07:37:48 +02:00
|
|
|
import net.ME1312.SubServers.Bungee.Host.Host;
|
2019-04-18 16:02:09 +02:00
|
|
|
import net.ME1312.Galaxi.Library.Map.ObjectMap;
|
|
|
|
import net.ME1312.Galaxi.Library.Util;
|
|
|
|
import net.ME1312.SubData.Server.ClientHandler;
|
|
|
|
import net.ME1312.SubData.Server.Protocol.PacketObjectIn;
|
|
|
|
import net.ME1312.SubData.Server.Protocol.PacketObjectOut;
|
2019-05-10 04:43:34 +02:00
|
|
|
import net.ME1312.SubServers.Bungee.Host.ServerContainer;
|
2019-05-17 00:36:41 +02:00
|
|
|
import net.ME1312.SubServers.Bungee.Library.Compatibility.Logger;
|
2017-04-01 07:37:48 +02:00
|
|
|
import net.ME1312.SubServers.Bungee.SubPlugin;
|
|
|
|
|
2019-05-10 04:43:34 +02:00
|
|
|
import java.util.HashMap;
|
2017-04-01 07:37:48 +02:00
|
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Link External Host Packet
|
|
|
|
*/
|
2019-05-11 23:23:31 +02:00
|
|
|
public class PacketLinkExHost implements InitialPacket, PacketObjectIn<Integer>, PacketObjectOut<Integer> {
|
2017-04-01 07:37:48 +02:00
|
|
|
private SubPlugin plugin;
|
|
|
|
private int response;
|
2019-05-13 05:28:27 +02:00
|
|
|
private String message;
|
2017-04-01 07:37:48 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* New PacketLinkExHost (In)
|
|
|
|
*
|
|
|
|
* @param plugin SubPlugin
|
|
|
|
*/
|
|
|
|
public PacketLinkExHost(SubPlugin plugin) {
|
|
|
|
if (Util.isNull(plugin)) throw new NullPointerException();
|
|
|
|
this.plugin = plugin;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* New PacketLinkExHost (Out)
|
|
|
|
*
|
|
|
|
* @param response Response ID
|
2019-05-13 05:28:27 +02:00
|
|
|
* @param message Message
|
2017-04-01 07:37:48 +02:00
|
|
|
*/
|
2019-05-13 05:28:27 +02:00
|
|
|
public PacketLinkExHost(int response, String message) {
|
|
|
|
if (Util.isNull(response)) throw new NullPointerException();
|
2017-04-01 07:37:48 +02:00
|
|
|
this.response = response;
|
2019-05-13 05:28:27 +02:00
|
|
|
this.message = message;
|
2017-04-01 07:37:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-04-18 16:02:09 +02:00
|
|
|
public ObjectMap<Integer> send(SubDataClient client) {
|
|
|
|
ObjectMap<Integer> data = new ObjectMap<Integer>();
|
|
|
|
data.set(0x0001, response);
|
2019-05-13 05:28:27 +02:00
|
|
|
if (message != null) data.set(0x0002, message);
|
2018-04-15 03:53:51 +02:00
|
|
|
return data;
|
2017-04-01 07:37:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-04-18 16:02:09 +02:00
|
|
|
public void receive(SubDataClient client, ObjectMap<Integer> data) {
|
2017-04-01 07:37:48 +02:00
|
|
|
try {
|
|
|
|
Map<String, Host> hosts = plugin.api.getHosts();
|
2019-04-18 16:02:09 +02:00
|
|
|
if (hosts.keySet().contains(data.getRawString(0x0000).toLowerCase())) {
|
|
|
|
Host host = hosts.get(data.getRawString(0x0000).toLowerCase());
|
2019-05-10 04:43:34 +02:00
|
|
|
if (host instanceof ExternalHost) {
|
|
|
|
Integer channel = data.getInt(0x0001);
|
|
|
|
HashMap<Integer, SubDataClient> subdata = Util.getDespiteException(() -> Util.reflect(ExternalHost.class.getDeclaredField("subdata"), host), null);
|
|
|
|
if (!subdata.keySet().contains(channel) || (channel == 0 && subdata.get(0) == null)) {
|
|
|
|
((ExternalHost) host).setSubData(client, channel);
|
2019-05-17 00:36:41 +02:00
|
|
|
Logger.get("SubData").info(client.getAddress().toString() + " has been defined as Host: " + host.getName() + ((channel > 0)?" (Sub-"+channel+")":""));
|
2019-05-13 05:28:27 +02:00
|
|
|
client.sendPacket(new PacketLinkExHost(0, null));
|
2019-05-11 23:23:31 +02:00
|
|
|
setReady(client, true);
|
2017-04-01 07:37:48 +02:00
|
|
|
} else {
|
2019-05-13 05:28:27 +02:00
|
|
|
client.sendPacket(new PacketLinkExHost(3, "Host already linked"));
|
2017-04-01 07:37:48 +02:00
|
|
|
}
|
|
|
|
} else {
|
2019-05-13 05:28:27 +02:00
|
|
|
client.sendPacket(new PacketLinkExHost(4, "That host does not support a network interface"));
|
2017-04-01 07:37:48 +02:00
|
|
|
}
|
|
|
|
} else {
|
2019-05-13 05:28:27 +02:00
|
|
|
client.sendPacket(new PacketLinkExHost(2, "There is no host with name: " + data.getRawString(0x0000)));
|
2017-04-01 07:37:48 +02:00
|
|
|
}
|
2019-05-11 23:23:31 +02:00
|
|
|
} catch (Throwable e) {
|
2019-05-13 05:28:27 +02:00
|
|
|
client.sendPacket(new PacketLinkExHost(1, null));
|
2017-07-23 06:32:57 +02:00
|
|
|
e.printStackTrace();
|
2017-04-01 07:37:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-04-18 16:02:09 +02:00
|
|
|
public int version() {
|
|
|
|
return 0x0001;
|
2017-04-01 07:37:48 +02:00
|
|
|
}
|
|
|
|
}
|