Skip to content

Commit

Permalink
Document the remaining non api public methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Thorinwasher committed Jun 17, 2024
1 parent d92aa5a commit fa71da9
Show file tree
Hide file tree
Showing 9 changed files with 133 additions and 25 deletions.
47 changes: 33 additions & 14 deletions src/main/java/org/sgrewritten/stargate/Stargate.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,8 @@
import org.sgrewritten.stargate.economy.VaultEconomyManager;
import org.sgrewritten.stargate.exception.StargateInitializationException;
import org.sgrewritten.stargate.formatting.StargateLanguageManager;
import org.sgrewritten.stargate.listener.BKCommonLibListener;
import org.sgrewritten.stargate.listener.BlockEventListener;
import org.sgrewritten.stargate.listener.EntityInsideBlockEventListener;
import org.sgrewritten.stargate.listener.MoveEventListener;
import org.sgrewritten.stargate.listener.PlayerAdvancementListener;
import org.sgrewritten.stargate.listener.PlayerEventListener;
import org.sgrewritten.stargate.listener.PluginEventListener;
import org.sgrewritten.stargate.listener.StargateBungeePluginMessageListener;
import org.sgrewritten.stargate.manager.BlockLoggingManager;
import org.sgrewritten.stargate.manager.CoreProtectManager;
import org.sgrewritten.stargate.manager.BlockDropManager;
import org.sgrewritten.stargate.manager.StargateBungeeManager;
import org.sgrewritten.stargate.manager.StargatePermissionManager;
import org.sgrewritten.stargate.listener.*;
import org.sgrewritten.stargate.manager.*;
import org.sgrewritten.stargate.migration.DataMigrator;
import org.sgrewritten.stargate.network.StargateNetworkManager;
import org.sgrewritten.stargate.network.StargateRegistry;
Expand Down Expand Up @@ -174,7 +163,7 @@ public void onEnable() {
BStatsHelper.registerMetrics(pluginId, this, getRegistry());
servicesManager = this.getServer().getServicesManager();
servicesManager.register(StargateAPI.class, this, this, ServicePriority.High);
if(NonLegacyClass.BLOCK_UTIL.isImplemented()) {
if (NonLegacyClass.BLOCK_UTIL.isImplemented()) {
RegisteredServiceProvider<BlockUtilAPI> blockUtilProvider = servicesManager.getRegistration(BlockUtilAPI.class);
BlockDropManager.setProvider(blockUtilProvider);
}
Expand Down Expand Up @@ -484,10 +473,21 @@ public void onDisable() {
servicesManager.unregisterAll(this);
}

/**
* Log an exception at the WARNING level
*
* @param throwable <p>THe exception to log</p>
*/
public static void log(Throwable throwable) {
Stargate.log(Level.WARNING, throwable);
}

/**
* Log an exception at the given level
*
* @param logLevel <p>The level to log on</p>
* @param throwable <p>The exception to log</p>
*/
public static void log(Level logLevel, Throwable throwable) {
if (throwable == null) {
return;
Expand All @@ -511,6 +511,12 @@ private static String getStackTrace(Throwable throwable) {
return builder.toString();
}

/**
* Log any message at the given level
*
* @param priorityLevel <p>The log level to log the message on</p>
* @param message <p>The message to log</p>
*/
public static void log(Level priorityLevel, String message) {
if (priorityLevel.intValue() < Stargate.logLevel.intValue()) {
return;
Expand Down Expand Up @@ -540,6 +546,12 @@ public static Stargate getInstance() {
return instance;
}

/**
* Log any message at the given level
*
* @param priorityLevel <p>The log level to log the message on</p>
* @param message <p>The message to log</p>
*/
public void logMessage(Level priorityLevel, String message) {
if (priorityLevel.intValue() < logLevel.intValue()) {
return;
Expand All @@ -551,6 +563,9 @@ public void logMessage(Level priorityLevel, String message) {
}
}

/**
* @return <p>Get the configuration of stargate, or an empty config</p>
*/
public static FileConfiguration getFileConfiguration() {
if (instance != null) {
return instance.getConfig();
Expand All @@ -570,6 +585,10 @@ private void registerCommands() {
}
}

/**
* Static access of the database interface, will throw null pointer exception if stargate has not been initialized
* @return <p>A storage API instance</p>
*/
public static StorageAPI getStorageAPIStatic() {
return instance.storageAPI;
}
Expand Down
43 changes: 35 additions & 8 deletions src/main/java/org/sgrewritten/stargate/util/BungeeHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@
import org.sgrewritten.stargate.property.StargateProtocolProperty;
import org.sgrewritten.stargate.property.StargateProtocolRequestType;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.*;
import java.nio.file.Files;
import java.util.UUID;
import java.util.logging.Level;
Expand Down Expand Up @@ -83,7 +79,7 @@ public static void getServerId(String dataFolder, String internalFolder) {
* @param registry <p>The registry to use</p>
* @param bungeeNetwork <p>The name of the legacy bungee network</p>
* @return <p>The legacy bungee network, or null if unobtainable</p>
* @throws UnimplementedFlagException
* @throws UnimplementedFlagException <p>If an unimplemented network type was used in creation (should not trigger)</p>
*/
public static @Nullable Network getLegacyBungeeNetwork(RegistryAPI registry, NetworkManager networkManager, String bungeeNetwork) throws UnimplementedFlagException {
Network network = registry.getNetwork(bungeeNetwork, StorageType.LOCAL);
Expand All @@ -102,6 +98,11 @@ public static void getServerId(String dataFolder, String internalFolder) {
return network;
}

/**
* @param portal <p>The portal which was created or destroyed</p>
* @param requestType <p>Whether the portal was created or destroyed</p>
* @return <p>A json string message</p>
*/
public static String generateJsonMessage(Portal portal, StargateProtocolRequestType requestType) {
JsonObject jsonData = new JsonObject();
jsonData.add(StargateProtocolProperty.REQUEST_TYPE.toString(), new JsonPrimitive(requestType.toString()));
Expand All @@ -113,6 +114,12 @@ public static String generateJsonMessage(Portal portal, StargateProtocolRequestT
return jsonData.toString();
}

/**
* Get json message for changing the id of a network
* @param newId <p>The id to change to</p>
* @param oldId <p>Previous id of the network</p>
* @return <p>A json string with a rename network message</p>
*/
public static String generateRenameNetworkMessage(String newId, String oldId) {
JsonObject jsonData = new JsonObject();
jsonData.add(StargateProtocolProperty.REQUEST_TYPE.toString(), new JsonPrimitive(StargateProtocolRequestType.NETWORK_RENAME.toString()));
Expand All @@ -121,6 +128,13 @@ public static String generateRenameNetworkMessage(String newId, String oldId) {
return jsonData.toString();
}

/**
*
* @param newName <p>The new portal name to change to</p>
* @param oldName <p>The previous portal name</p>
* @param network <p>The network of the portal</p>
* @return <p>A json string with a rename portal message</p>
*/
public static String generateRenamePortalMessage(String newName, String oldName, Network network) {
JsonObject jsonData = new JsonObject();
jsonData.add(StargateProtocolProperty.REQUEST_TYPE.toString(), new JsonPrimitive(StargateProtocolRequestType.PORTAL_RENAME.toString()));
Expand All @@ -130,6 +144,11 @@ public static String generateRenamePortalMessage(String newName, String oldName,
return jsonData.toString();
}

/**
* @param player <p>The player to teleport</p>
* @param portal <p>The portal to teleport to (in the other server)</p>
* @return <p>A json string with a teleport player message</p>
*/
public static String generateTeleportJsonMessage(String player, Portal portal) {
JsonObject jsonData = new JsonObject();
jsonData.add(StargateProtocolProperty.PLAYER.toString(), new JsonPrimitive(player));
Expand All @@ -138,12 +157,20 @@ public static String generateTeleportJsonMessage(String player, Portal portal) {
return jsonData.toString();
}

/**
* @param player <p>The player to teleport</p>
* @param portal <p>The portal to teleport</p>
* @return <p>A legacy format (csv-ish) with teleport message</p>
*/
public static String generateLegacyTeleportMessage(String player, Portal portal) {
return player + "#@#" + portal.getName();
}



/**
* To send bungee messages, it is required that a player is online and that stargate knows the server name as defined
* In the velocity / bungee config.
* @return <p>True if the server can send bungee messages</p>
*/
public static boolean canSendBungeeMessages() {
return (!Bukkit.getServer().getOnlinePlayers().isEmpty() && Stargate.knowsServerName());
}
Expand Down
18 changes: 18 additions & 0 deletions src/main/java/org/sgrewritten/stargate/util/ExceptionHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ public class ExceptionHelper {
private ExceptionHelper(){
throw new IllegalStateException("Utility class");
}

/**
* @param expectedType <p>The expected type of the exception</p>
* @param runnable <p>The function to be run</p>
* @return <p>True if the exception got thrown</p>
* @param <T> <p>Any throwable</p>
*/
public static <T extends Throwable> boolean doesThrow(Class<T> expectedType, Runnable runnable) {
try {
runnable.run();
Expand All @@ -18,10 +25,21 @@ public static <T extends Throwable> boolean doesThrow(Class<T> expectedType, Run
}
}

/**
*
* @param expectedType <p>The expected type to not throw</p>
* @param runnable <p>A function to run</p>
* @return <p>True if the expected type did not throw</p>
* @param <T> <p>The expected type to not throw</p>
*/
public static <T extends Throwable> boolean doesNotThrow(Class<T> expectedType, Runnable runnable) {
return !doesThrow(expectedType, runnable);
}

/**
* @param runnable <p>The runnable to check if it throws</p>
* @return <p>True if runnable did not throw any exception</p>
*/
public static boolean doesNotThrow(Runnable runnable) {
try {
runnable.run();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public final class FileHelper {
private static final String UTF8_BOM = "\uFEFF";

private FileHelper() {

throw new IllegalStateException("Utility class");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,13 @@ private static Map<StargateFlag, Integer> loadFlagIndices() {
return output;
}


public static String findConfigKey(String[] possibleKeys, Map<String, Object> oldConfig) {
/**
* Utility method to find any of the given possible keys in the config
* @param possibleKeys <p>name variations of possible keys</p>
* @param oldConfig <p>The config to fetch data from</p>
* @return <p>The key that had a value in the config, or null if none matched</p>
*/
public static @Nullable String findConfigKey(String[] possibleKeys, Map<String, Object> oldConfig) {
for (String possibleKey : possibleKeys) {
if (oldConfig.get(possibleKey) != null) {
return possibleKey;
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/org/sgrewritten/stargate/util/MessageUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ private MessageUtils() {
throw new IllegalStateException("Utility class");
}

/**
* Send a message from this portal; triggers a {@link StargateSendMessagePortalEvent}
* @param portal <p>The portal that relates to this message</p>
* @param receiver <p>The receiving entity of this message</p>
* @param message <p>The legacy serialized message to send</p>
* @param type <p>The type of the message (see {@link MessageType})</p>
*/
public static void sendMessageFromPortal(Portal portal, @Nullable Entity receiver, String message, MessageType type) {
if (message == null || message.isBlank() || receiver == null) {
return;
Expand All @@ -36,6 +43,11 @@ public static void sendMessageFromPortal(Portal portal, @Nullable Entity receive
}
}

/**
* Send a message that does not belong to any portal, will trigger a {@link StargateMessageEvent}
* @param receiver <p>The receiver of the message</p>
* @param message <p>The legacy serialized message</p>
*/
public static void sendMessage(@Nullable Entity receiver, String message) {
if (receiver == null){
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,19 @@ private NetworkCreationHelper() {

}

/**
* Get banned network names
* @return <p>All banned network names</p>
*/
public static Set<String> getBannedNames() {
Set<String> output = new HashSet<>();
output.add(ConfigurationHelper.getString(ConfigurationOption.LEGACY_BUNGEE_NETWORK).toLowerCase());
return output;
}

/**
* @return <p>All names taken upp by the default networks</p>
*/
public static Set<String> getDefaultNamesTaken() {
Set<String> output = new HashSet<>();
output.add(ConfigurationHelper.getString(ConfigurationOption.DEFAULT_NETWORK).toLowerCase());
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/org/sgrewritten/stargate/util/VectorUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,24 @@ public static double directionalAngleOperator(Vector vector1, Vector vector2) {
return Math.atan2(vector1.clone().crossProduct(vector2).getY(), vector1.dot(vector2));
}

/**
* Convert between two different coordinate systems. In this case from format space to minecraft space
* @param topLeft <p>Origo of system in format space</p>
* @param vectorOperation <p>Rotations and z axis flips to convert between coordinate systems</p>
* @param vector <p>The vector to convert</p>
* @return <p>A location in minecraft space</p>
*/
public static Location getLocation(Location topLeft, VectorOperation vectorOperation, Vector vector){
return topLeft.clone().add(vectorOperation.performToRealSpaceOperation(vector));
}

/**
* Convert between two different coordinate systems. In this case from minecraft space to format space
* @param topLeft <p>The origo of format space</p>
* @param vectorOperation <p>Rotations and z axis flips to convert between coordinate systems</p>
* @param location <p>A location to convert from</p>
* @return <p>A vector in format space</p>
*/
public static Vector getFormatSpaceCoordinate(Location topLeft, VectorOperation vectorOperation, Location location){
return vectorOperation.performToAbstractSpaceOperation(topLeft.clone().subtract(location).toVector());
}
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/sgrewritten/stargate/util/WebHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ private WebHelper(){
throw new IllegalStateException("Utility class");
}

/**
* Download file from the web
* @param link <p>URL for web address to fetch form</p>
* @param file <p>File location to save to</p>
* @throws IOException <p>If unable to download the file</p>
*/
public static void downloadFile(String link, File file) throws IOException {
URL url = new URL(link);
try (InputStream inputStream = url.openStream()) {
Expand Down

0 comments on commit fa71da9

Please sign in to comment.