mirror of
https://github.com/ME1312/SubServers-2.git
synced 2024-11-22 10:15:52 +01:00
Allow multiple packets to be registered with the name name
It's sorta experimental, but in theory: multiple packets should be able to write to the same handle and multiple packets should be able to read from the same handle.
This commit is contained in:
parent
8255e2464a
commit
ae587deaf0
Binary file not shown.
Binary file not shown.
@ -164,7 +164,7 @@
|
||||
<dl>
|
||||
<dt><span class="memberNameLink"><a href="net/ME1312/SubServers/Bungee/Network/SubDataServer.html#broadcastPacket-net.ME1312.SubServers.Bungee.Network.PacketOut-">broadcastPacket(PacketOut)</a></span> - Method in class net.ME1312.SubServers.Bungee.Network.<a href="net/ME1312/SubServers/Bungee/Network/SubDataServer.html" title="class in net.ME1312.SubServers.Bungee.Network">SubDataServer</a></dt>
|
||||
<dd>
|
||||
<div class="block">Broadcast a Packet to everything on the Network
|
||||
<div class="block">Broadcast a Packet to everything on the Network<br>
|
||||
<b>Warning:</b> There are usually different types of applications on the network at once, they may not recognise the same packet handles</div>
|
||||
</dd>
|
||||
</dl>
|
||||
@ -601,7 +601,7 @@
|
||||
<dd> </dd>
|
||||
<dt><span class="memberNameLink"><a href="net/ME1312/SubServers/Bungee/Network/SubDataServer.html#getPacket-java.lang.String-">getPacket(String)</a></span> - Static method in class net.ME1312.SubServers.Bungee.Network.<a href="net/ME1312/SubServers/Bungee/Network/SubDataServer.html" title="class in net.ME1312.SubServers.Bungee.Network">SubDataServer</a></dt>
|
||||
<dd>
|
||||
<div class="block">Grab PacketIn Instance via handle</div>
|
||||
<div class="block">Grab PacketIn Instances via handle</div>
|
||||
</dd>
|
||||
<dt><span class="memberNameLink"><a href="net/ME1312/SubServers/Bungee/Event/SubAddServerEvent.html#getPlayer--">getPlayer()</a></span> - Method in class net.ME1312.SubServers.Bungee.Event.<a href="net/ME1312/SubServers/Bungee/Event/SubAddServerEvent.html" title="class in net.ME1312.SubServers.Bungee.Event">SubAddServerEvent</a></dt>
|
||||
<dd>
|
||||
@ -1307,7 +1307,7 @@
|
||||
</dd>
|
||||
<dt><span class="memberNameLink"><a href="net/ME1312/SubServers/Bungee/Network/Client.html#setHandler-net.ME1312.SubServers.Bungee.Network.ClientHandler-">setHandler(ClientHandler)</a></span> - Method in class net.ME1312.SubServers.Bungee.Network.<a href="net/ME1312/SubServers/Bungee/Network/Client.html" title="class in net.ME1312.SubServers.Bungee.Network">Client</a></dt>
|
||||
<dd>
|
||||
<div class="block">Sets the Handler
|
||||
<div class="block">Sets the Handler<br>
|
||||
<b>Warning:</b> This method should only be called by ClientHandler methods</div>
|
||||
</dd>
|
||||
<dt><span class="memberNameLink"><a href="net/ME1312/SubServers/Bungee/Host/Server.html#setHidden-boolean-">setHidden(boolean)</a></span> - Method in class net.ME1312.SubServers.Bungee.Host.<a href="net/ME1312/SubServers/Bungee/Host/Server.html" title="class in net.ME1312.SubServers.Bungee.Host">Server</a></dt>
|
||||
|
@ -191,7 +191,7 @@ extends java.lang.Object</pre>
|
||||
<tr id="i6" class="altColor">
|
||||
<td class="colFirst"><code>void</code></td>
|
||||
<td class="colLast"><code><span class="memberNameLink"><a href="../../../../../net/ME1312/SubServers/Bungee/Network/Client.html#setHandler-net.ME1312.SubServers.Bungee.Network.ClientHandler-">setHandler</a></span>(<a href="../../../../../net/ME1312/SubServers/Bungee/Network/ClientHandler.html" title="interface in net.ME1312.SubServers.Bungee.Network">ClientHandler</a> obj)</code>
|
||||
<div class="block">Sets the Handler
|
||||
<div class="block">Sets the Handler<br>
|
||||
<b>Warning:</b> This method should only be called by ClientHandler methods</div>
|
||||
</td>
|
||||
</tr>
|
||||
@ -331,7 +331,7 @@ extends java.lang.Object</pre>
|
||||
<li class="blockList">
|
||||
<h4>setHandler</h4>
|
||||
<pre>public void setHandler(<a href="../../../../../net/ME1312/SubServers/Bungee/Network/ClientHandler.html" title="interface in net.ME1312.SubServers.Bungee.Network">ClientHandler</a> obj)</pre>
|
||||
<div class="block">Sets the Handler
|
||||
<div class="block">Sets the Handler<br>
|
||||
<b>Warning:</b> This method should only be called by ClientHandler methods</div>
|
||||
<dl>
|
||||
<dt><span class="paramLabel">Parameters:</span></dt>
|
||||
|
@ -169,7 +169,7 @@ extends java.lang.Object</pre>
|
||||
<tr id="i2" class="altColor">
|
||||
<td class="colFirst"><code>void</code></td>
|
||||
<td class="colLast"><code><span class="memberNameLink"><a href="../../../../../net/ME1312/SubServers/Bungee/Network/SubDataServer.html#broadcastPacket-net.ME1312.SubServers.Bungee.Network.PacketOut-">broadcastPacket</a></span>(<a href="../../../../../net/ME1312/SubServers/Bungee/Network/PacketOut.html" title="interface in net.ME1312.SubServers.Bungee.Network">PacketOut</a> packet)</code>
|
||||
<div class="block">Broadcast a Packet to everything on the Network
|
||||
<div class="block">Broadcast a Packet to everything on the Network<br>
|
||||
<b>Warning:</b> There are usually different types of applications on the network at once, they may not recognise the same packet handles</div>
|
||||
</td>
|
||||
</tr>
|
||||
@ -198,9 +198,9 @@ extends java.lang.Object</pre>
|
||||
</td>
|
||||
</tr>
|
||||
<tr id="i7" class="rowColor">
|
||||
<td class="colFirst"><code>static <a href="../../../../../net/ME1312/SubServers/Bungee/Network/PacketIn.html" title="interface in net.ME1312.SubServers.Bungee.Network">PacketIn</a></code></td>
|
||||
<td class="colFirst"><code>static java.util.List<? extends <a href="../../../../../net/ME1312/SubServers/Bungee/Network/PacketIn.html" title="interface in net.ME1312.SubServers.Bungee.Network">PacketIn</a>></code></td>
|
||||
<td class="colLast"><code><span class="memberNameLink"><a href="../../../../../net/ME1312/SubServers/Bungee/Network/SubDataServer.html#getPacket-java.lang.String-">getPacket</a></span>(java.lang.String handle)</code>
|
||||
<div class="block">Grab PacketIn Instance via handle</div>
|
||||
<div class="block">Grab PacketIn Instances via handle</div>
|
||||
</td>
|
||||
</tr>
|
||||
<tr id="i8" class="altColor">
|
||||
@ -423,8 +423,8 @@ extends java.lang.Object</pre>
|
||||
<ul class="blockList">
|
||||
<li class="blockList">
|
||||
<h4>getPacket</h4>
|
||||
<pre>public static <a href="../../../../../net/ME1312/SubServers/Bungee/Network/PacketIn.html" title="interface in net.ME1312.SubServers.Bungee.Network">PacketIn</a> getPacket(java.lang.String handle)</pre>
|
||||
<div class="block">Grab PacketIn Instance via handle</div>
|
||||
<pre>public static java.util.List<? extends <a href="../../../../../net/ME1312/SubServers/Bungee/Network/PacketIn.html" title="interface in net.ME1312.SubServers.Bungee.Network">PacketIn</a>> getPacket(java.lang.String handle)</pre>
|
||||
<div class="block">Grab PacketIn Instances via handle</div>
|
||||
<dl>
|
||||
<dt><span class="paramLabel">Parameters:</span></dt>
|
||||
<dd><code>handle</code> - Handle</dd>
|
||||
@ -440,7 +440,7 @@ extends java.lang.Object</pre>
|
||||
<li class="blockList">
|
||||
<h4>broadcastPacket</h4>
|
||||
<pre>public void broadcastPacket(<a href="../../../../../net/ME1312/SubServers/Bungee/Network/PacketOut.html" title="interface in net.ME1312.SubServers.Bungee.Network">PacketOut</a> packet)</pre>
|
||||
<div class="block">Broadcast a Packet to everything on the Network
|
||||
<div class="block">Broadcast a Packet to everything on the Network<br>
|
||||
<b>Warning:</b> There are usually different types of applications on the network at once, they may not recognise the same packet handles</div>
|
||||
<dl>
|
||||
<dt><span class="paramLabel">Parameters:</span></dt>
|
||||
|
@ -463,7 +463,6 @@ Table styles
|
||||
.useSummary td, .constantsSummary td, .deprecatedSummary td {
|
||||
text-align:left;
|
||||
padding:0px 0px 12px 10px;
|
||||
width:100%;
|
||||
}
|
||||
th.colOne, th.colFirst, th.colLast, .useSummary th, .constantsSummary th,
|
||||
td.colOne, td.colFirst, td.colLast, .useSummary td, .constantsSummary td{
|
||||
@ -488,6 +487,7 @@ td.colOne, th.colOne {
|
||||
font-size:13px;
|
||||
}
|
||||
.overviewSummary td.colFirst, .overviewSummary th.colFirst,
|
||||
.useSummary td.colFirst, .useSummary th.colFirst,
|
||||
.overviewSummary td.colOne, .overviewSummary th.colOne,
|
||||
.memberSummary td.colFirst, .memberSummary th.colFirst,
|
||||
.memberSummary td.colOne, .memberSummary th.colOne,
|
||||
|
@ -434,7 +434,7 @@
|
||||
<dd> </dd>
|
||||
<dt><span class="memberNameLink"><a href="net/ME1312/SubServers/Client/Bukkit/Network/SubDataClient.html#getPacket-java.lang.String-">getPacket(String)</a></span> - Static method in class net.ME1312.SubServers.Client.Bukkit.Network.<a href="net/ME1312/SubServers/Client/Bukkit/Network/SubDataClient.html" title="class in net.ME1312.SubServers.Client.Bukkit.Network">SubDataClient</a></dt>
|
||||
<dd>
|
||||
<div class="block">Grab PacketIn Instance via handle</div>
|
||||
<div class="block">Grab PacketIn Instances via handle</div>
|
||||
</dd>
|
||||
<dt><span class="memberNameLink"><a href="net/ME1312/SubServers/Client/Bukkit/Event/SubAddServerEvent.html#getPlayer--">getPlayer()</a></span> - Method in class net.ME1312.SubServers.Client.Bukkit.Event.<a href="net/ME1312/SubServers/Client/Bukkit/Event/SubAddServerEvent.html" title="class in net.ME1312.SubServers.Client.Bukkit.Event">SubAddServerEvent</a></dt>
|
||||
<dd>
|
||||
|
@ -168,9 +168,9 @@ extends java.lang.Object</pre>
|
||||
</td>
|
||||
</tr>
|
||||
<tr id="i3" class="rowColor">
|
||||
<td class="colFirst"><code>static <a href="../../../../../../net/ME1312/SubServers/Client/Bukkit/Network/PacketIn.html" title="interface in net.ME1312.SubServers.Client.Bukkit.Network">PacketIn</a></code></td>
|
||||
<td class="colFirst"><code>static java.util.List<? extends <a href="../../../../../../net/ME1312/SubServers/Client/Bukkit/Network/PacketIn.html" title="interface in net.ME1312.SubServers.Client.Bukkit.Network">PacketIn</a>></code></td>
|
||||
<td class="colLast"><code><span class="memberNameLink"><a href="../../../../../../net/ME1312/SubServers/Client/Bukkit/Network/SubDataClient.html#getPacket-java.lang.String-">getPacket</a></span>(java.lang.String handle)</code>
|
||||
<div class="block">Grab PacketIn Instance via handle</div>
|
||||
<div class="block">Grab PacketIn Instances via handle</div>
|
||||
</td>
|
||||
</tr>
|
||||
<tr id="i4" class="altColor">
|
||||
@ -311,8 +311,8 @@ extends java.lang.Object</pre>
|
||||
<ul class="blockList">
|
||||
<li class="blockList">
|
||||
<h4>getPacket</h4>
|
||||
<pre>public static <a href="../../../../../../net/ME1312/SubServers/Client/Bukkit/Network/PacketIn.html" title="interface in net.ME1312.SubServers.Client.Bukkit.Network">PacketIn</a> getPacket(java.lang.String handle)</pre>
|
||||
<div class="block">Grab PacketIn Instance via handle</div>
|
||||
<pre>public static java.util.List<? extends <a href="../../../../../../net/ME1312/SubServers/Client/Bukkit/Network/PacketIn.html" title="interface in net.ME1312.SubServers.Client.Bukkit.Network">PacketIn</a>> getPacket(java.lang.String handle)</pre>
|
||||
<div class="block">Grab PacketIn Instances via handle</div>
|
||||
<dl>
|
||||
<dt><span class="paramLabel">Parameters:</span></dt>
|
||||
<dd><code>handle</code> - Handle</dd>
|
||||
|
@ -463,7 +463,6 @@ Table styles
|
||||
.useSummary td, .constantsSummary td, .deprecatedSummary td {
|
||||
text-align:left;
|
||||
padding:0px 0px 12px 10px;
|
||||
width:100%;
|
||||
}
|
||||
th.colOne, th.colFirst, th.colLast, .useSummary th, .constantsSummary th,
|
||||
td.colOne, td.colFirst, td.colLast, .useSummary td, .constantsSummary td{
|
||||
@ -488,6 +487,7 @@ td.colOne, th.colOne {
|
||||
font-size:13px;
|
||||
}
|
||||
.overviewSummary td.colFirst, .overviewSummary th.colFirst,
|
||||
.useSummary td.colFirst, .useSummary th.colFirst,
|
||||
.overviewSummary td.colOne, .overviewSummary th.colOne,
|
||||
.memberSummary td.colFirst, .memberSummary th.colFirst,
|
||||
.memberSummary td.colOne, .memberSummary th.colOne,
|
||||
|
@ -17,14 +17,13 @@ import java.util.TreeMap;
|
||||
import java.util.UUID;
|
||||
|
||||
public class InternalHost extends Host {
|
||||
HashMap<String, SubServer> servers = new HashMap<String, SubServer>();
|
||||
|
||||
private HashMap<String, SubServer> servers = new HashMap<String, SubServer>();
|
||||
private String name;
|
||||
private boolean enabled;
|
||||
private InetAddress address;
|
||||
private InternalSubCreator creator;
|
||||
private String directory;
|
||||
SubPlugin plugin;
|
||||
protected SubPlugin plugin;
|
||||
|
||||
public InternalHost(SubPlugin plugin, String name, Boolean enabled, InetAddress address, String directory, String gitBash) {
|
||||
super(plugin, name, enabled, address, directory, gitBash);
|
||||
|
@ -48,53 +48,52 @@ public class InternalSubServer extends SubServer {
|
||||
}
|
||||
|
||||
private void run() {
|
||||
(thread = new Thread(() -> {
|
||||
allowrestart = true;
|
||||
|
||||
allowrestart = true;
|
||||
try {
|
||||
process = Runtime.getRuntime().exec(executable.toString(), null, directory);
|
||||
System.out.println("SubServers > Now starting " + getName());
|
||||
final InternalSubLogger read = new InternalSubLogger(process, getName(), log, null);
|
||||
read.start();
|
||||
command = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
|
||||
|
||||
process.waitFor();
|
||||
} catch (IOException | InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
allowrestart = false;
|
||||
}
|
||||
|
||||
SubStoppedEvent event = new SubStoppedEvent(this);
|
||||
host.plugin.getPluginManager().callEvent(event);
|
||||
System.out.println("SubServers > " + getName() + " has stopped");
|
||||
process = null;
|
||||
command = null;
|
||||
|
||||
if (isTemporary()) {
|
||||
try {
|
||||
process = Runtime.getRuntime().exec(executable.toString(), null, directory);
|
||||
System.out.println("SubServers > Now starting " + getName());
|
||||
final InternalSubLogger read = new InternalSubLogger(process, getName(), log, null);
|
||||
read.start();
|
||||
command = new BufferedWriter(new OutputStreamWriter(process.getOutputStream()));
|
||||
|
||||
process.waitFor();
|
||||
} catch (IOException | InterruptedException e) {
|
||||
host.removeSubServer(getName());
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
allowrestart = false;
|
||||
}
|
||||
|
||||
SubStoppedEvent event = new SubStoppedEvent(this);
|
||||
host.plugin.getPluginManager().callEvent(event);
|
||||
System.out.println("SubServers > " + getName() + " has stopped");
|
||||
process = null;
|
||||
command = null;
|
||||
|
||||
if (temporary) {
|
||||
} else {
|
||||
if (willAutoRestart() && allowrestart) {
|
||||
try {
|
||||
host.removeSubServer(getName());
|
||||
Thread.sleep(2500);
|
||||
start();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
if (restart && allowrestart) {
|
||||
try {
|
||||
Thread.sleep(2500);
|
||||
start();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
})).start();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean start(UUID player) {
|
||||
if (enabled && !isRunning()) {
|
||||
if (isEnabled() && !isRunning()) {
|
||||
SubStartEvent event = new SubStartEvent(player, this);
|
||||
host.plugin.getPluginManager().callEvent(event);
|
||||
if (!event.isCancelled()) {
|
||||
run();
|
||||
(thread = new Thread(() -> run())).start();
|
||||
return true;
|
||||
} else return false;
|
||||
} else return false;
|
||||
|
@ -68,12 +68,13 @@ public final class Client {
|
||||
while ((input = in.readLine()) != null) {
|
||||
try {
|
||||
JSONObject json = new JSONObject(input);
|
||||
PacketIn packet = SubDataServer.decodePacket(json);
|
||||
if (authorized == null || packet instanceof PacketAuthorization) {
|
||||
try {
|
||||
packet.execute(instance, (json.keySet().contains("c")) ? json.getJSONObject("c") : null);
|
||||
} catch (Exception e) {
|
||||
new InvocationTargetException(e, "Exception while executing PacketIn").printStackTrace();
|
||||
for (PacketIn packet : SubDataServer.decodePacket(json)) {
|
||||
if (authorized == null || packet instanceof PacketAuthorization) {
|
||||
try {
|
||||
packet.execute(instance, (json.keySet().contains("c")) ? json.getJSONObject("c") : null);
|
||||
} catch (Exception e) {
|
||||
new InvocationTargetException(e, "Exception while executing PacketIn").printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (IllegalPacketException e) {
|
||||
@ -159,7 +160,7 @@ public final class Client {
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the Handler
|
||||
* Sets the Handler<br>
|
||||
* <b>Warning:</b> This method should only be called by ClientHandler methods
|
||||
*
|
||||
* @see ClientHandler
|
||||
|
@ -20,7 +20,7 @@ import java.util.List;
|
||||
*/
|
||||
public final class SubDataServer {
|
||||
private static HashMap<Class<? extends PacketOut>, String> pOut = new HashMap<Class<? extends PacketOut>, String>();
|
||||
private static HashMap<String, PacketIn> pIn = new HashMap<String, PacketIn>();
|
||||
private static HashMap<String, List<PacketIn>> pIn = new HashMap<String, List<PacketIn>>();
|
||||
private static List<InetAddress> allowedAddresses = new ArrayList<InetAddress>();
|
||||
private static boolean defaults = false;
|
||||
private HashMap<InetSocketAddress, Client> clients = new HashMap<InetSocketAddress, Client>();
|
||||
@ -174,11 +174,9 @@ public final class SubDataServer {
|
||||
* @param handle Handle to Bind
|
||||
*/
|
||||
public static void registerPacket(PacketIn packet, String handle) {
|
||||
if (!pIn.keySet().contains(handle)) {
|
||||
pIn.put(handle, packet);
|
||||
} else {
|
||||
throw new IllegalStateException("PacketIn Handle \"" + handle + "\" is already in use!");
|
||||
}
|
||||
List<PacketIn> list = (pIn.keySet().contains(handle))?pIn.get(handle):new ArrayList<PacketIn>();
|
||||
if (!list.contains(packet)) list.add(packet);
|
||||
pIn.put(handle, list);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -188,25 +186,21 @@ public final class SubDataServer {
|
||||
* @param handle Handle to bind
|
||||
*/
|
||||
public static void registerPacket(Class<? extends PacketOut> packet, String handle) {
|
||||
if (!pOut.values().contains(handle)) {
|
||||
pOut.put(packet, handle);
|
||||
} else {
|
||||
throw new IllegalStateException("PacketOut Handle \"" + handle + "\" is already in use!");
|
||||
}
|
||||
pOut.put(packet, handle);
|
||||
}
|
||||
|
||||
/**
|
||||
* Grab PacketIn Instance via handle
|
||||
* Grab PacketIn Instances via handle
|
||||
*
|
||||
* @param handle Handle
|
||||
* @return PacketIn
|
||||
*/
|
||||
public static PacketIn getPacket(String handle) {
|
||||
public static List<? extends PacketIn> getPacket(String handle) {
|
||||
return pIn.get(handle);
|
||||
}
|
||||
|
||||
/**
|
||||
* Broadcast a Packet to everything on the Network
|
||||
* Broadcast a Packet to everything on the Network<br>
|
||||
* <b>Warning:</b> There are usually different types of applications on the network at once, they may not recognise the same packet handles
|
||||
*
|
||||
* @param packet Packet to send
|
||||
@ -263,13 +257,20 @@ public final class SubDataServer {
|
||||
* @throws IllegalPacketException
|
||||
* @throws InvocationTargetException
|
||||
*/
|
||||
protected static PacketIn decodePacket(JSONObject json) throws IllegalPacketException, InvocationTargetException {
|
||||
protected static List<PacketIn> decodePacket(JSONObject json) throws IllegalPacketException, InvocationTargetException {
|
||||
if (!json.keySet().contains("h") || !json.keySet().contains("v")) throw new IllegalPacketException("Unknown Packet Format: " + json.toString());
|
||||
if (!pIn.keySet().contains(json.getString("h"))) throw new IllegalPacketException("Unknown PacketIn Channel: " + json.getString("h"));
|
||||
|
||||
PacketIn packet = pIn.get(json.getString("h"));
|
||||
if (!new Version(json.getString("v")).equals(packet.getVersion())) throw new IllegalPacketException("Packet Version Mismatch in " + json.getString("h") + ": " + json.getString("v") + "->" + packet.getVersion().toString());
|
||||
return packet;
|
||||
List<PacketIn> list = new ArrayList<PacketIn>();
|
||||
for (PacketIn packet : pIn.get(json.getString("h"))) {
|
||||
if (new Version(json.getString("v")).equals(packet.getVersion())) {
|
||||
list.add(packet);
|
||||
} else {
|
||||
new IllegalPacketException("Packet Version Mismatch in " + json.getString("h") + ": " + json.getString("v") + " -> " + packet.getVersion().toString()).printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -38,8 +38,8 @@ public final class SubPlugin extends BungeeCord {
|
||||
public YAMLConfig lang;
|
||||
public HashMap<String, String> exLang = new HashMap<String, String>();
|
||||
public SubDataServer subdata = null;
|
||||
public final Version version = new Version("2.11.2d");
|
||||
protected Version bversion = null;
|
||||
public final Version version = new Version("2.11.2e");
|
||||
protected Version bversion = new Version(1);
|
||||
|
||||
protected boolean running = false;
|
||||
public final SubAPI api = new SubAPI(this);
|
||||
|
@ -16,11 +16,13 @@ import java.lang.reflect.InvocationTargetException;
|
||||
import java.net.InetAddress;
|
||||
import java.net.Socket;
|
||||
import java.net.SocketException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
|
||||
public final class SubDataClient {
|
||||
private static HashMap<Class<? extends PacketOut>, String> pOut = new HashMap<Class<? extends PacketOut>, String>();
|
||||
private static HashMap<String, PacketIn> pIn = new HashMap<String, PacketIn>();
|
||||
private static HashMap<String, List<PacketIn>> pIn = new HashMap<String, List<PacketIn>>();
|
||||
private static boolean defaults = false;
|
||||
private PrintWriter writer;
|
||||
private Socket socket;
|
||||
@ -89,11 +91,12 @@ public final class SubDataClient {
|
||||
while ((input = in.readLine()) != null) {
|
||||
try {
|
||||
JSONObject json = new JSONObject(input);
|
||||
PacketIn packet = decodePacket(json);
|
||||
try {
|
||||
packet.execute((json.keySet().contains("c")) ? json.getJSONObject("c") : null);
|
||||
} catch (Exception e) {
|
||||
new InvocationTargetException(e, "Exception while executing PacketIn").printStackTrace();
|
||||
for (PacketIn packet : decodePacket(json)) {
|
||||
try {
|
||||
packet.execute((json.keySet().contains("c")) ? json.getJSONObject("c") : null);
|
||||
} catch (Exception e) {
|
||||
new InvocationTargetException(e, "Exception while executing PacketIn").printStackTrace();
|
||||
}
|
||||
}
|
||||
} catch (IllegalPacketException e) {
|
||||
e.printStackTrace();
|
||||
@ -142,7 +145,9 @@ public final class SubDataClient {
|
||||
* @param handle Handle to Bind
|
||||
*/
|
||||
public static void registerPacket(PacketIn packet, String handle) {
|
||||
pIn.put(handle, packet);
|
||||
List<PacketIn> list = (pIn.keySet().contains(handle))?pIn.get(handle):new ArrayList<PacketIn>();
|
||||
if (!list.contains(packet)) list.add(packet);
|
||||
pIn.put(handle, list);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -156,12 +161,12 @@ public final class SubDataClient {
|
||||
}
|
||||
|
||||
/**
|
||||
* Grab PacketIn Instance via handle
|
||||
* Grab PacketIn Instances via handle
|
||||
*
|
||||
* @param handle Handle
|
||||
* @return PacketIn
|
||||
*/
|
||||
public static PacketIn getPacket(String handle) {
|
||||
public static List<? extends PacketIn> getPacket(String handle) {
|
||||
return pIn.get(handle);
|
||||
}
|
||||
|
||||
@ -185,7 +190,7 @@ public final class SubDataClient {
|
||||
* @return JSON Formatted Packet
|
||||
* @throws IllegalPacketException
|
||||
*/
|
||||
protected static JSONObject encodePacket(PacketOut packet) throws IllegalPacketException {
|
||||
private static JSONObject encodePacket(PacketOut packet) throws IllegalPacketException {
|
||||
JSONObject json = new JSONObject();
|
||||
|
||||
if (!pOut.keySet().contains(packet.getClass())) throw new IllegalPacketException("Unknown PacketOut Channel: " + packet.getClass().getCanonicalName());
|
||||
@ -206,13 +211,20 @@ public final class SubDataClient {
|
||||
* @throws IllegalPacketException
|
||||
* @throws InvocationTargetException
|
||||
*/
|
||||
protected static PacketIn decodePacket(JSONObject json) throws IllegalPacketException, InvocationTargetException {
|
||||
private static List<PacketIn> decodePacket(JSONObject json) throws IllegalPacketException, InvocationTargetException {
|
||||
if (!json.keySet().contains("h") || !json.keySet().contains("v")) throw new IllegalPacketException("Unknown Packet Format: " + json.toString());
|
||||
if (!pIn.keySet().contains(json.getString("h"))) throw new IllegalPacketException("Unknown PacketIn Channel: " + json.getString("h"));
|
||||
|
||||
PacketIn packet = pIn.get(json.getString("h"));
|
||||
if (!new Version(json.getString("v")).equals(packet.getVersion())) throw new IllegalPacketException("Packet Version Mismatch in " + json.getString("h") + ": " + json.getString("v") + "->" + packet.getVersion().toString());
|
||||
return packet;
|
||||
List<PacketIn> list = new ArrayList<PacketIn>();
|
||||
for (PacketIn packet : pIn.get(json.getString("h"))) {
|
||||
if (new Version(json.getString("v")).equals(packet.getVersion())) {
|
||||
list.add(packet);
|
||||
} else {
|
||||
new IllegalPacketException("Packet Version Mismatch in " + json.getString("h") + ": " + json.getString("v") + " -> " + packet.getVersion().toString()).printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -22,7 +22,7 @@ public final class SubPlugin extends JavaPlugin {
|
||||
|
||||
public UIListener gui = null;
|
||||
public Version version;
|
||||
protected Version bversion = null;
|
||||
protected Version bversion = new Version(1);
|
||||
|
||||
//public final SubAPI api = new SubAPI(this);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user