SubServers-2/SubServers.Sync/src/net/ME1312/SubServers/Sync/Network/Packet/PacketDownloadHostInfo.java

59 lines
1.7 KiB
Java

package net.ME1312.SubServers.Sync.Network.Packet;
import net.ME1312.SubServers.Sync.Library.Callback;
import net.ME1312.SubServers.Sync.Library.Config.YAMLSection;
import net.ME1312.SubServers.Sync.Library.Util;
import net.ME1312.SubServers.Sync.Library.Version.Version;
import net.ME1312.SubServers.Sync.Network.PacketIn;
import net.ME1312.SubServers.Sync.Network.PacketOut;
import java.util.HashMap;
import java.util.UUID;
/**
* Download Host Info Packet
*/
public class PacketDownloadHostInfo implements PacketIn, PacketOut {
private static HashMap<String, Callback<YAMLSection>[]> callbacks = new HashMap<String, Callback<YAMLSection>[]>();
private String host;
private String id;
/**
* New PacketDownloadHostInfo (In)
*/
public PacketDownloadHostInfo() {}
/**
* New PacketDownloadHostInfo (Out)
*
* @param host Host name (or null for all)
* @param callback Callbacks
*/
@SafeVarargs
public PacketDownloadHostInfo(String host, Callback<YAMLSection>... callback) {
if (Util.isNull((Object) callback)) throw new NullPointerException();
this.host = host;
this.id = Util.getNew(callbacks.keySet(), UUID::randomUUID).toString();
callbacks.put(id, callback);
}
@Override
public YAMLSection generate() {
YAMLSection json = new YAMLSection();
json.set("id", id);
json.set("host", host);
return json;
}
@Override
public void execute(YAMLSection data) {
for (Callback<YAMLSection> callback : callbacks.get(data.getRawString("id"))) callback.run(data);
callbacks.remove(data.getRawString("id"));
}
@Override
public Version getVersion() {
return new Version("2.13b");
}
}