Skip to content

Commit

Permalink
Moved some code from the stargate class
Browse files Browse the repository at this point in the history
relates to issue #120
  • Loading branch information
Thorinwasher committed May 24, 2022
1 parent 66efa5d commit 0fa7370
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 84 deletions.
87 changes: 6 additions & 81 deletions src/main/java/net/TheDgtl/Stargate/Stargate.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@
import net.TheDgtl.Stargate.property.PluginChannel;
import net.TheDgtl.Stargate.thread.SynchronousPopulator;
import net.TheDgtl.Stargate.util.BStatsHelper;
import net.TheDgtl.Stargate.util.BungeeHelper;
import net.TheDgtl.Stargate.util.FileHelper;
import net.TheDgtl.Stargate.util.PortalHelper;
import net.md_5.bungee.api.ChatColor;
import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit;
Expand Down Expand Up @@ -117,10 +119,6 @@ public class Stargate extends JavaPlugin implements StargateLogger {
public static final int MAX_TEXT_LENGTH = 40;

public static EconomyManager economyManager;
/*
* Used in bungee / waterfall
*/
private final HashMap<String, Portal> bungeeQueue = new HashMap<>();
public static String serverName;
public static boolean knowsServerName = false;

Expand Down Expand Up @@ -162,40 +160,7 @@ public void onEnable() {

//Register bStats metrics
int pluginId = 13629;
//TODO: Nothing is done with the created metrics object
Metrics metrics = BStatsHelper.getMetrics(pluginId, this);
}

private void loadBungeeServerName() {
Stargate.log(Level.FINEST, DATA_FOLDER);
File path = new File(this.getDataFolder(), INTERNAL_FOLDER);
if (!path.exists() && path.mkdir()) {
try {
Files.setAttribute(path.toPath(), "dos:hidden", true);
} catch (IOException e) {
e.printStackTrace();
}
}
File file = new File(path, "serverUUID.txt");
if (!file.exists()) {
try {
if (!file.createNewFile()) {
throw new FileNotFoundException("serverUUID.txt was not found and could not be created");
}
BufferedWriter writer = FileHelper.getBufferedWriter(file);
writer.write(UUID.randomUUID().toString());
writer.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
try {
BufferedReader reader = FileHelper.getBufferedReader(file);
Stargate.serverUUID = UUID.fromString(reader.readLine());
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
BStatsHelper.getMetrics(pluginId, this);
}

private void loadColors() {
Expand Down Expand Up @@ -316,7 +281,7 @@ public void reload() {
private void load() {
loadColors();
if (ConfigurationHelper.getBoolean(ConfigurationOption.USING_REMOTE_DATABASE)) {
loadBungeeServerName();
BungeeHelper.loadBungeeServerName(DATA_FOLDER,INTERNAL_FOLDER);
}
economyManager = new EconomyManager();
String debugLevelString = ConfigurationHelper.getString(ConfigurationOption.DEBUG_LEVEL);
Expand All @@ -339,8 +304,8 @@ private void load() {
@Override
public void onDisable() {
//Close networked always-on Stargates as they have no destination on next start
closeAllPortals(registry.getBungeeNetworkMap());
closeAllPortals(registry.getNetworkMap());
PortalHelper.closeAllPortals(registry.getBungeeNetworkMap());
PortalHelper.closeAllPortals(registry.getNetworkMap());
/*
* Replacement for legacy, which used:
* methodPortal.closeAllGates(this); Portal.clearGates(); managedWorlds.clear();
Expand All @@ -360,17 +325,6 @@ public void onDisable() {
storageAPI.endInterServerConnection();
}

private void closeAllPortals(Map<String, Network> networkMap) {
for (Network network : networkMap.values()) {
for (Portal portal : network.getAllPortals()) {
if (portal.hasFlag(PortalFlag.ALWAYS_ON) && !portal.hasFlag(PortalFlag.FIXED) &&
portal instanceof RealPortal) {
((RealPortal) portal).getGate().close();
}
}
}
}

public static void log(Level priorityLevel, String message) {
if (instance != null) {
instance.logMessage(priorityLevel, message);
Expand Down Expand Up @@ -418,35 +372,6 @@ private void registerCommands() {
}
}

public static void addToQueue(String playerName, String portalName, String netName, boolean isInterServer) {
Network network = getRegistry().getNetwork(netName, isInterServer);


/*
* In some cases, there might be issues with a portal being deleted in a server, but still present in the
* inter-server database. Therefore, we have to check for that...
*/
if (network == null) {
// Error: This bungee portal's %type% has been removed from the destination server instance.
//(See Discussion One) %type% = network.
String msg = String.format("Inter-server network ''%s'' could not be found", netName);
Stargate.log(Level.WARNING, msg);
}
Portal portal = network == null ? null : network.getPortal(portalName);
if (portal == null) {
// Error: This bungee portal's %type% has been removed from the destination server instance.
//(See Discussion One) %type% = gate.
String msg = String.format("Inter-server portal ''%s'' in network ''%s'' could not be found", portalName, netName);
Stargate.log(Level.WARNING, msg);
}
instance.bungeeQueue.put(playerName, portal);
}


public static Portal pullFromQueue(String playerName) {
return instance.bungeeQueue.remove(playerName);
}

public static RegistryAPI getRegistry() {
return registry;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import net.TheDgtl.Stargate.property.PluginChannel;
import net.TheDgtl.Stargate.property.StargateProtocolProperty;
import net.TheDgtl.Stargate.property.StargateProtocolRequestType;
import net.TheDgtl.Stargate.util.BungeeHelper;

import org.bukkit.entity.Player;
import org.bukkit.plugin.messaging.PluginMessageListener;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -144,7 +146,7 @@ private void legacyPlayerConnect(String message) {
Player player = stargate.getServer().getPlayer(playerName);
if (player == null) {
Stargate.log(Level.FINEST, "Player was null; adding to queue");
Stargate.addToQueue(playerName, destination, bungeeNetwork, false);
BungeeHelper.addToQueue(Stargate.getRegistry(),playerName, destination, bungeeNetwork, false);
} else {
Network network = Stargate.getRegistry().getNetwork(bungeeNetwork, false);
Portal destinationPortal = network.getPortal(destination);
Expand Down Expand Up @@ -210,7 +212,7 @@ private void playerConnect(String message) {
Player player = stargate.getServer().getPlayer(playerName);
if (player == null) {
Stargate.log(Level.FINEST, "Player was null; adding to queue");
Stargate.addToQueue(playerName, portalName, networkName, true);
BungeeHelper.addToQueue(Stargate.getRegistry(), playerName, portalName, networkName, true);
return;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/TheDgtl/Stargate/util/BStatsHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
public class BStatsHelper {

/**
* Creates a new metrics containing relevant portal data
* Creates a new metrics containing relevant portal data. Note that the returned class does not need to be used.
*
* @param pluginId <p>The id of the Stargate plugin</p>
* @param plugin <p>A Stargate plugin instance</p>
Expand Down
91 changes: 91 additions & 0 deletions src/main/java/net/TheDgtl/Stargate/util/BungeeHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package net.TheDgtl.Stargate.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.UUID;
import java.util.logging.Level;

import net.TheDgtl.Stargate.Stargate;
import net.TheDgtl.Stargate.network.Network;
import net.TheDgtl.Stargate.network.RegistryAPI;
import net.TheDgtl.Stargate.network.portal.Portal;

public class BungeeHelper {


/*
* Used in bungee / waterfall
*/
private static final HashMap<String, Portal> bungeeQueue = new HashMap<>();

/**
*
* @param dataFolder
* @param internalFolder
*/
public static void loadBungeeServerName(String dataFolder, String internalFolder) {
Stargate.log(Level.FINEST, dataFolder);
File path = new File(dataFolder, internalFolder);
if (!path.exists() && path.mkdir()) {
try {
Files.setAttribute(path.toPath(), "dos:hidden", true);
} catch (IOException e) {
e.printStackTrace();
}
}
File file = new File(path, "serverUUID.txt");
if (!file.exists()) {
try {
if (!file.createNewFile()) {
throw new FileNotFoundException("serverUUID.txt was not found and could not be created");
}
BufferedWriter writer = FileHelper.getBufferedWriter(file);
writer.write(UUID.randomUUID().toString());
writer.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
try {
BufferedReader reader = FileHelper.getBufferedReader(file);
Stargate.serverUUID = UUID.fromString(reader.readLine());
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}

public static void addToQueue(RegistryAPI registry, String playerName, String portalName, String netName, boolean isInterServer) {
Network network = registry.getNetwork(netName, isInterServer);


/*
* In some cases, there might be issues with a portal being deleted in a server, but still present in the
* inter-server database. Therefore, we have to check for that...
*/
if (network == null) {
// Error: This bungee portal's %type% has been removed from the destination server instance.
//(See Discussion One) %type% = network.
String msg = String.format("Inter-server network ''%s'' could not be found", netName);
Stargate.log(Level.WARNING, msg);
}
Portal portal = network == null ? null : network.getPortal(portalName);
if (portal == null) {
// Error: This bungee portal's %type% has been removed from the destination server instance.
//(See Discussion One) %type% = gate.
String msg = String.format("Inter-server portal ''%s'' in network ''%s'' could not be found", portalName, netName);
Stargate.log(Level.WARNING, msg);
}
bungeeQueue.put(playerName, portal);
}


public static Portal pullFromQueue(String playerName) {
return bungeeQueue.remove(playerName);
}
}
21 changes: 21 additions & 0 deletions src/main/java/net/TheDgtl/Stargate/util/PortalHelper.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
package net.TheDgtl.Stargate.util;

import net.TheDgtl.Stargate.network.Network;
import net.TheDgtl.Stargate.network.portal.Portal;
import net.TheDgtl.Stargate.network.portal.PortalFlag;
import net.TheDgtl.Stargate.network.portal.RealPortal;

import java.util.Map;
import java.util.Set;

/**
Expand All @@ -22,5 +26,22 @@ public static String flagsToString(Set<PortalFlag> flags) {
}
return flagsStringBuilder.toString();
}



/**
* Close all portals in specified map
* @param networkMap <p> The map with all portals to close </p>
*/
public static void closeAllPortals(Map<String, Network> networkMap) {
for (Network network : networkMap.values()) {
for (Portal portal : network.getAllPortals()) {
if (portal.hasFlag(PortalFlag.ALWAYS_ON) && !portal.hasFlag(PortalFlag.FIXED) &&
portal instanceof RealPortal) {
((RealPortal) portal).getGate().close();
}
}
}
}

}

0 comments on commit 0fa7370

Please sign in to comment.