Skip to content

Commit

Permalink
Lock the start method for event usage
Browse files Browse the repository at this point in the history
  • Loading branch information
ME1312 committed Jul 29, 2018
1 parent 97c73bf commit d69840f
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 31 deletions.
Expand Up @@ -35,6 +35,7 @@ public class ExternalSubServer extends SubServerContainer {
private boolean restart;
private boolean temporary;
private boolean running;
private boolean lock;

/**
* Creates an External SubServer
Expand Down Expand Up @@ -68,13 +69,16 @@ public ExternalSubServer(ExternalHost host, String name, boolean enabled, int po

this.running = false;
this.temporary = false;
this.lock = false;
}

@Override
public boolean start(UUID player) {
if (isEnabled() && !running && getCurrentIncompatibilities().size() == 0) {
if (!lock && isEnabled() && !running && getCurrentIncompatibilities().size() == 0) {
lock = true;
SubStartEvent event = new SubStartEvent(player, this);
host.plugin.getPluginManager().callEvent(event);
lock = false;
if (!event.isCancelled()) {
System.out.println("SubServers > Now starting " + getName());
running = true;
Expand Down
Expand Up @@ -42,6 +42,7 @@ public class InternalSubServer extends SubServerContainer {
private boolean restart;
private boolean allowrestart;
private boolean temporary;
private boolean lock;

/**
* Creates an Internal SubServer
Expand All @@ -57,12 +58,11 @@ public class InternalSubServer extends SubServerContainer {
* @param stopcmd Stop Command
* @param hidden Hidden Status
* @param restricted Restricted Status
* @param temporary Temporary Status
* @throws InvalidServerException
*/
public InternalSubServer(InternalHost host, String name, boolean enabled, int port, String motd, boolean log, String directory, Executable executable, String stopcmd, boolean hidden, boolean restricted, boolean temporary) throws InvalidServerException {
public InternalSubServer(InternalHost host, String name, boolean enabled, int port, String motd, boolean log, String directory, Executable executable, String stopcmd, boolean hidden, boolean restricted) throws InvalidServerException {
super(host, name, port, motd, hidden, restricted);
if (Util.isNull(host, name, enabled, port, motd, log, directory, executable, stopcmd, hidden, restricted, temporary)) throw new NullPointerException();
if (Util.isNull(host, name, enabled, port, motd, log, directory, executable, stopcmd, hidden, restricted)) throw new NullPointerException();
this.host = host;
this.enabled = enabled;
this.editable = false;
Expand Down Expand Up @@ -115,7 +115,8 @@ public InternalSubServer(InternalHost host, String name, boolean enabled, int po
e.printStackTrace();
}
}
this.temporary = temporary && start();
this.temporary = false;
this.lock = false;
}

private void run() {
Expand Down Expand Up @@ -169,9 +170,11 @@ private void run() {

@Override
public boolean start(UUID player) {
if (isEnabled() && !(thread != null && thread.isAlive()) && getCurrentIncompatibilities().size() == 0) {
if (!lock && isEnabled() && !(thread != null && thread.isAlive()) && getCurrentIncompatibilities().size() == 0) {
lock = true;
SubStartEvent event = new SubStartEvent(player, this);
host.plugin.getPluginManager().callEvent(event);
lock = false;
if (!event.isCancelled()) {
(thread = new Thread(this::run)).start();
return true;
Expand Down
Expand Up @@ -303,7 +303,7 @@ public void removeClient(String address) throws IOException {
* @param handle Handle to Bind
*/
public static void registerPacket(PacketIn packet, String channel, String handle) {
if (Util.isNull(packet, handle)) throw new NullPointerException();
if (Util.isNull(packet, channel, handle)) throw new NullPointerException();
HashMap<String, List<PacketIn>> map = (pIn.keySet().contains(channel.toLowerCase()))?pIn.get(channel.toLowerCase()):new HashMap<String, List<PacketIn>>();
List<PacketIn> list = (map.keySet().contains(handle))?map.get(handle):new ArrayList<PacketIn>();
if (!list.contains(packet)) {
Expand All @@ -320,7 +320,7 @@ public static void registerPacket(PacketIn packet, String channel, String handle
* @param packet PacketIn to unregister
*/
public static void unregisterPacket(String channel, PacketIn packet) {
if (Util.isNull(packet)) throw new NullPointerException();
if (Util.isNull(channel, packet)) throw new NullPointerException();
if (pIn.keySet().contains(channel.toLowerCase())) {
List<String> search = new ArrayList<String>();
search.addAll(pIn.get(channel.toLowerCase()).keySet());
Expand All @@ -345,7 +345,7 @@ public static void unregisterPacket(String channel, PacketIn packet) {
* @param handle Handle to bind
*/
public static void registerPacket(Class<? extends PacketOut> packet, String channel, String handle) {
if (Util.isNull(packet, handle)) throw new NullPointerException();
if (Util.isNull(packet, channel, handle)) throw new NullPointerException();
pOut.put(packet, new NamedContainer<String, String>(channel.toLowerCase(), handle));
}

Expand All @@ -356,7 +356,7 @@ public static void registerPacket(Class<? extends PacketOut> packet, String chan
* @param packet PacketOut to unregister
*/
public static void unregisterPacket(String channel, Class<? extends PacketOut> packet) {
if (Util.isNull(packet)) throw new NullPointerException();
if (Util.isNull(channel, packet)) throw new NullPointerException();
if (pOut.keySet().contains(packet) && pOut.get(packet).name().equalsIgnoreCase(channel)) pOut.remove(packet);
}

Expand All @@ -368,7 +368,7 @@ public static void unregisterPacket(String channel, Class<? extends PacketOut> p
* @return PacketIn
*/
public static List<? extends PacketIn> getPacket(String channel, String handle) {
if (Util.isNull(handle)) throw new NullPointerException();
if (Util.isNull(channel, handle)) throw new NullPointerException();
return new ArrayList<PacketIn>(pIn.get(channel.toLowerCase()).get(handle));
}

Expand Down
Expand Up @@ -234,7 +234,7 @@ public static Cipher getCipher(String handle) {
* @param handle Handle to Bind
*/
public static void registerPacket(PacketIn packet, String channel, String handle) {
if (Util.isNull(packet, handle)) throw new NullPointerException();
if (Util.isNull(packet, channel, handle)) throw new NullPointerException();
HashMap<String, List<PacketIn>> map = (pIn.keySet().contains(channel.toLowerCase()))?pIn.get(channel.toLowerCase()):new HashMap<String, List<PacketIn>>();
List<PacketIn> list = (map.keySet().contains(handle))?map.get(handle):new ArrayList<PacketIn>();
if (!list.contains(packet)) {
Expand All @@ -251,7 +251,7 @@ public static void registerPacket(PacketIn packet, String channel, String handle
* @param packet PacketIn to unregister
*/
public static void unregisterPacket(String channel, PacketIn packet) {
if (Util.isNull(packet)) throw new NullPointerException();
if (Util.isNull(channel, packet)) throw new NullPointerException();
if (pIn.keySet().contains(channel.toLowerCase())) {
List<String> search = new ArrayList<String>();
search.addAll(pIn.get(channel.toLowerCase()).keySet());
Expand All @@ -276,7 +276,7 @@ public static void unregisterPacket(String channel, PacketIn packet) {
* @param handle Handle to bind
*/
public static void registerPacket(Class<? extends PacketOut> packet, String channel, String handle) {
if (Util.isNull(packet, handle)) throw new NullPointerException();
if (Util.isNull(packet, channel, handle)) throw new NullPointerException();
pOut.put(packet, new NamedContainer<String, String>(channel.toLowerCase(), handle));
}

Expand All @@ -287,7 +287,7 @@ public static void registerPacket(Class<? extends PacketOut> packet, String chan
* @param packet PacketOut to unregister
*/
public static void unregisterPacket(String channel, Class<? extends PacketOut> packet) {
if (Util.isNull(packet)) throw new NullPointerException();
if (Util.isNull(channel, packet)) throw new NullPointerException();
if (pOut.keySet().contains(packet) && pOut.get(packet).name().equalsIgnoreCase(channel)) pOut.remove(packet);
}

Expand All @@ -299,7 +299,7 @@ public static void unregisterPacket(String channel, Class<? extends PacketOut> p
* @return PacketIn
*/
public static List<? extends PacketIn> getPacket(String channel, String handle) {
if (Util.isNull(handle)) throw new NullPointerException();
if (Util.isNull(channel, handle)) throw new NullPointerException();
return new ArrayList<PacketIn>(pIn.get(channel.toLowerCase()).get(handle));
}

Expand Down
Expand Up @@ -239,7 +239,7 @@ public static Cipher getCipher(String handle) {
* @param handle Handle to Bind
*/
public static void registerPacket(PacketIn packet, String channel, String handle) {
if (Util.isNull(packet, handle)) throw new NullPointerException();
if (Util.isNull(packet, channel, handle)) throw new NullPointerException();
HashMap<String, List<PacketIn>> map = (pIn.keySet().contains(channel.toLowerCase()))?pIn.get(channel.toLowerCase()):new HashMap<String, List<PacketIn>>();
List<PacketIn> list = (map.keySet().contains(handle))?map.get(handle):new ArrayList<PacketIn>();
if (!list.contains(packet)) {
Expand All @@ -256,7 +256,7 @@ public static void registerPacket(PacketIn packet, String channel, String handle
* @param packet PacketIn to unregister
*/
public static void unregisterPacket(String channel, PacketIn packet) {
if (Util.isNull(packet)) throw new NullPointerException();
if (Util.isNull(channel, packet)) throw new NullPointerException();
if (pIn.keySet().contains(channel.toLowerCase())) {
List<String> search = new ArrayList<String>();
search.addAll(pIn.get(channel.toLowerCase()).keySet());
Expand All @@ -281,7 +281,7 @@ public static void unregisterPacket(String channel, PacketIn packet) {
* @param handle Handle to bind
*/
public static void registerPacket(Class<? extends PacketOut> packet, String channel, String handle) {
if (Util.isNull(packet, handle)) throw new NullPointerException();
if (Util.isNull(packet, channel, handle)) throw new NullPointerException();
pOut.put(packet, new NamedContainer<String, String>(channel.toLowerCase(), handle));
}

Expand All @@ -292,7 +292,7 @@ public static void registerPacket(Class<? extends PacketOut> packet, String chan
* @param packet PacketOut to unregister
*/
public static void unregisterPacket(String channel, Class<? extends PacketOut> packet) {
if (Util.isNull(packet)) throw new NullPointerException();
if (Util.isNull(channel, packet)) throw new NullPointerException();
if (pOut.keySet().contains(packet) && pOut.get(packet).name().equalsIgnoreCase(channel)) pOut.remove(packet);
}

Expand All @@ -304,7 +304,7 @@ public static void unregisterPacket(String channel, Class<? extends PacketOut> p
* @return PacketIn
*/
public static List<? extends PacketIn> getPacket(String channel, String handle) {
if (Util.isNull(handle)) throw new NullPointerException();
if (Util.isNull(channel, handle)) throw new NullPointerException();
return new ArrayList<PacketIn>(pIn.get(channel.toLowerCase()).get(handle));
}

Expand Down
Expand Up @@ -255,7 +255,7 @@ public static Cipher getCipher(String handle) {
* @param handle Handle to Bind
*/
public static void registerPacket(PacketIn packet, String channel, String handle) {
if (Util.isNull(packet, handle)) throw new NullPointerException();
if (Util.isNull(packet, channel, handle)) throw new NullPointerException();
HashMap<String, List<PacketIn>> map = (pIn.keySet().contains(channel.toLowerCase()))?pIn.get(channel.toLowerCase()):new HashMap<String, List<PacketIn>>();
List<PacketIn> list = (map.keySet().contains(handle))?map.get(handle):new ArrayList<PacketIn>();
if (!list.contains(packet)) {
Expand All @@ -272,7 +272,7 @@ public static void registerPacket(PacketIn packet, String channel, String handle
* @param packet PacketIn to unregister
*/
public static void unregisterPacket(String channel, PacketIn packet) {
if (Util.isNull(packet)) throw new NullPointerException();
if (Util.isNull(channel, packet)) throw new NullPointerException();
if (pIn.keySet().contains(channel.toLowerCase())) {
List<String> search = new ArrayList<String>();
search.addAll(pIn.get(channel.toLowerCase()).keySet());
Expand All @@ -297,7 +297,7 @@ public static void unregisterPacket(String channel, PacketIn packet) {
* @param handle Handle to bind
*/
public static void registerPacket(Class<? extends PacketOut> packet, String channel, String handle) {
if (Util.isNull(packet, handle)) throw new NullPointerException();
if (Util.isNull(packet, channel, handle)) throw new NullPointerException();
pOut.put(packet, new NamedContainer<String, String>(channel.toLowerCase(), handle));
}

Expand All @@ -308,7 +308,7 @@ public static void registerPacket(Class<? extends PacketOut> packet, String chan
* @param packet PacketOut to unregister
*/
public static void unregisterPacket(String channel, Class<? extends PacketOut> packet) {
if (Util.isNull(packet)) throw new NullPointerException();
if (Util.isNull(channel, packet)) throw new NullPointerException();
if (pOut.keySet().contains(packet) && pOut.get(packet).name().equalsIgnoreCase(channel)) pOut.remove(packet);
}

Expand All @@ -320,7 +320,7 @@ public static void unregisterPacket(String channel, Class<? extends PacketOut> p
* @return PacketIn
*/
public static List<? extends PacketIn> getPacket(String channel, String handle) {
if (Util.isNull(handle)) throw new NullPointerException();
if (Util.isNull(channel, handle)) throw new NullPointerException();
return new ArrayList<PacketIn>(pIn.get(channel.toLowerCase()).get(handle));
}

Expand Down
Expand Up @@ -260,7 +260,7 @@ public static Cipher getCipher(String handle) {
* @param handle Handle to Bind
*/
public static void registerPacket(PacketIn packet, String channel, String handle) {
if (Util.isNull(packet, handle)) throw new NullPointerException();
if (Util.isNull(packet, channel, handle)) throw new NullPointerException();
HashMap<String, List<PacketIn>> map = (pIn.keySet().contains(channel.toLowerCase()))?pIn.get(channel.toLowerCase()):new HashMap<String, List<PacketIn>>();
List<PacketIn> list = (map.keySet().contains(handle))?map.get(handle):new ArrayList<PacketIn>();
if (!list.contains(packet)) {
Expand All @@ -277,7 +277,7 @@ public static void registerPacket(PacketIn packet, String channel, String handle
* @param packet PacketIn to unregister
*/
public static void unregisterPacket(String channel, PacketIn packet) {
if (Util.isNull(packet)) throw new NullPointerException();
if (Util.isNull(channel, packet)) throw new NullPointerException();
if (pIn.keySet().contains(channel.toLowerCase())) {
List<String> search = new ArrayList<String>();
search.addAll(pIn.get(channel.toLowerCase()).keySet());
Expand All @@ -302,7 +302,7 @@ public static void unregisterPacket(String channel, PacketIn packet) {
* @param handle Handle to bind
*/
public static void registerPacket(Class<? extends PacketOut> packet, String channel, String handle) {
if (Util.isNull(packet, handle)) throw new NullPointerException();
if (Util.isNull(packet, channel, handle)) throw new NullPointerException();
pOut.put(packet, new NamedContainer<String, String>(channel.toLowerCase(), handle));
}

Expand All @@ -313,7 +313,7 @@ public static void registerPacket(Class<? extends PacketOut> packet, String chan
* @param packet PacketOut to unregister
*/
public static void unregisterPacket(String channel, Class<? extends PacketOut> packet) {
if (Util.isNull(packet)) throw new NullPointerException();
if (Util.isNull(channel, packet)) throw new NullPointerException();
if (pOut.keySet().contains(packet) && pOut.get(packet).name().equalsIgnoreCase(channel)) pOut.remove(packet);
}

Expand All @@ -325,7 +325,7 @@ public static void unregisterPacket(String channel, Class<? extends PacketOut> p
* @return PacketIn
*/
public static List<? extends PacketIn> getPacket(String channel, String handle) {
if (Util.isNull(handle)) throw new NullPointerException();
if (Util.isNull(channel, handle)) throw new NullPointerException();
return new ArrayList<PacketIn>(pIn.get(channel.toLowerCase()).get(handle));
}

Expand Down

0 comments on commit d69840f

Please sign in to comment.