Adds comments and simplifies some of the code
Adds a PortalOption enum to simplify portal options Adds a BungeeHelper class to collect the bungee-related code
This commit is contained in:
133
src/main/java/net/knarcraft/stargate/utility/BungeeHelper.java
Normal file
133
src/main/java/net/knarcraft/stargate/utility/BungeeHelper.java
Normal file
@ -0,0 +1,133 @@
|
||||
package net.knarcraft.stargate.utility;
|
||||
|
||||
import net.knarcraft.stargate.Portal;
|
||||
import net.knarcraft.stargate.PortalHandler;
|
||||
import net.knarcraft.stargate.Stargate;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.DataInputStream;
|
||||
import java.io.DataOutputStream;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* This class contains helpful functions to help with sending and receiving BungeeCord plugin messages
|
||||
*/
|
||||
public final class BungeeHelper {
|
||||
|
||||
private final static String bungeeSubChannel = "SGBungee";
|
||||
private final static String bungeeChannel = "BungeeCord";
|
||||
private final static String teleportMessageDelimiter = "#@#";
|
||||
|
||||
/**
|
||||
* Sends a plugin message to BungeeCord allowing the target server to catch it
|
||||
*
|
||||
* @param player <p>The player teleporting</p>
|
||||
* @param entrancePortal <p>The portal the player is teleporting from</p>
|
||||
* @return <p>True if the message was successfully sent</p>
|
||||
*/
|
||||
public static boolean sendTeleportationMessage(Player player, Portal entrancePortal) {
|
||||
try {
|
||||
// Build the message, format is <player>#@#<destination>
|
||||
String message = player.getName() + teleportMessageDelimiter + entrancePortal.getDestinationName();
|
||||
// Build the message data, sent over the SGBungee BungeeCord channel
|
||||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||
DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);
|
||||
dataOutputStream.writeUTF("Forward");
|
||||
dataOutputStream.writeUTF(entrancePortal.getNetwork()); // Server
|
||||
//Specify SGBungee channel/tag
|
||||
dataOutputStream.writeUTF(bungeeSubChannel);
|
||||
//Length of the message
|
||||
dataOutputStream.writeShort(message.length());
|
||||
//The data to send
|
||||
dataOutputStream.writeBytes(message);
|
||||
player.sendPluginMessage(Stargate.stargate, bungeeChannel, byteArrayOutputStream.toByteArray());
|
||||
} catch (IOException ex) {
|
||||
Stargate.log.severe(Stargate.getString("prefix") + "Error sending BungeeCord teleport packet");
|
||||
ex.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends the bungee message necessary to change the server
|
||||
*
|
||||
* @param player <p>The player to teleport</p>
|
||||
* @param entrancePortal <p>The bungee portal the player teleports from</p>
|
||||
* @return <p>True if able to send the plugin message</p>
|
||||
*/
|
||||
public static boolean changeServer(Player player, Portal entrancePortal) {
|
||||
try {
|
||||
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
|
||||
DataOutputStream dataOutputStream = new DataOutputStream(byteArrayOutputStream);
|
||||
dataOutputStream.writeUTF("Connect");
|
||||
dataOutputStream.writeUTF(entrancePortal.getNetwork());
|
||||
|
||||
player.sendPluginMessage(Stargate.stargate, bungeeChannel, byteArrayOutputStream.toByteArray());
|
||||
byteArrayOutputStream.reset();
|
||||
} catch (IOException ex) {
|
||||
Stargate.log.severe(Stargate.getString("prefix") + "Error sending BungeeCord connect packet");
|
||||
ex.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a plugin message byte array to a string if it's sent from another stargate plugin
|
||||
*
|
||||
* @param message <p>The byte array to read</p>
|
||||
* @return <p>The message contained in the byte array or null on failure</p>
|
||||
*/
|
||||
public static String readPluginMessage(byte[] message) {
|
||||
// Get data from message
|
||||
byte[] data;
|
||||
try {
|
||||
DataInputStream dataInputStream = new DataInputStream(new ByteArrayInputStream(message));
|
||||
String subChannel = dataInputStream.readUTF();
|
||||
//Only listen for the SGBungee channel
|
||||
if (!subChannel.equals(bungeeSubChannel)) {
|
||||
return null;
|
||||
}
|
||||
short dataLength = dataInputStream.readShort();
|
||||
data = new byte[dataLength];
|
||||
dataInputStream.readFully(data);
|
||||
} catch (IOException ex) {
|
||||
Stargate.log.severe(Stargate.getString("prefix") + "Error receiving BungeeCord message");
|
||||
ex.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
|
||||
// Data should be player name, and destination gate name
|
||||
return new String(data);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the receival of a teleport message
|
||||
*
|
||||
* @param receivedMessage <p>The received message</p>
|
||||
*/
|
||||
public static void handleTeleportMessage(String receivedMessage) {
|
||||
String[] messageParts = receivedMessage.split(teleportMessageDelimiter);
|
||||
|
||||
String playerName = messageParts[0];
|
||||
String destination = messageParts[1];
|
||||
|
||||
// Check if the player is online, if so, teleport, otherwise, queue
|
||||
Player player = Stargate.server.getPlayer(playerName);
|
||||
if (player == null) {
|
||||
Stargate.bungeeQueue.put(playerName.toLowerCase(), destination);
|
||||
} else {
|
||||
Portal destinationPortal = PortalHandler.getBungeeGate(destination);
|
||||
// Specified an invalid gate. For now we'll just let them connect at their current location
|
||||
if (destinationPortal == null) {
|
||||
Stargate.log.info(Stargate.getString("prefix") + "Bungee gate " + destination + " does not exist");
|
||||
return;
|
||||
}
|
||||
destinationPortal.teleport(player, destinationPortal, null);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -9,9 +9,10 @@ public class EconomyHelper {
|
||||
|
||||
/**
|
||||
* Tries to make the given user pay the teleport fee
|
||||
*
|
||||
* @param entrancePortal <p>The portal the player is entering</p>
|
||||
* @param player <p>The player wishing to teleport</p>
|
||||
* @param cost <p>The cost of teleportation</p>
|
||||
* @param player <p>The player wishing to teleport</p>
|
||||
* @param cost <p>The cost of teleportation</p>
|
||||
* @return <p>True if payment was successful</p>
|
||||
*/
|
||||
public static boolean payTeleportFee(Portal entrancePortal, Player player, int cost) {
|
||||
@ -52,9 +53,10 @@ public class EconomyHelper {
|
||||
|
||||
/**
|
||||
* Sends a message to the gate owner telling him/her how much he/she earned from a player using his/her gate
|
||||
* @param portalName <p>The name of the used portal</p>
|
||||
*
|
||||
* @param portalName <p>The name of the used portal</p>
|
||||
* @param portalOwner <p>The owner of the portal</p>
|
||||
* @param earnings <p>The amount the owner earned</p>
|
||||
* @param earnings <p>The amount the owner earned</p>
|
||||
*/
|
||||
public static void sendObtainMessage(String portalName, Player portalOwner, int earnings) {
|
||||
String obtainedMsg = Stargate.getString("ecoObtain");
|
||||
@ -64,9 +66,10 @@ public class EconomyHelper {
|
||||
|
||||
/**
|
||||
* Sends a message telling the user how much they paid for interacting with a portal
|
||||
*
|
||||
* @param portalName <p>The name of the portal interacted with</p>
|
||||
* @param player <p>The interacting player</p>
|
||||
* @param cost <p>The cost of the interaction</p>
|
||||
* @param player <p>The interacting player</p>
|
||||
* @param cost <p>The cost of the interaction</p>
|
||||
*/
|
||||
public static void sendDeductMessage(String portalName, Player player, int cost) {
|
||||
String deductMsg = Stargate.getString("ecoDeduct");
|
||||
@ -76,9 +79,10 @@ public class EconomyHelper {
|
||||
|
||||
/**
|
||||
* Sends a message telling the user they don't have enough funds to do a portal interaction
|
||||
*
|
||||
* @param portalName <p>The name of the portal interacted with</p>
|
||||
* @param player <p>The interacting player</p>
|
||||
* @param cost <p>The cost of the interaction</p>
|
||||
* @param player <p>The interacting player</p>
|
||||
* @param cost <p>The cost of the interaction</p>
|
||||
*/
|
||||
public static void sendInsufficientFundsMessage(String portalName, Player player, int cost) {
|
||||
String inFundMsg = Stargate.getString("ecoInFunds");
|
||||
@ -88,9 +92,10 @@ public class EconomyHelper {
|
||||
|
||||
/**
|
||||
* Sends a message telling the user how much they are refunded for breaking their portal
|
||||
*
|
||||
* @param portalName <p>The name of the broken portal</p>
|
||||
* @param player <p>The player breaking the portal</p>
|
||||
* @param cost <p>The amount the user has to pay for destroying the portal. (expects a negative value)</p>
|
||||
* @param player <p>The player breaking the portal</p>
|
||||
* @param cost <p>The amount the user has to pay for destroying the portal. (expects a negative value)</p>
|
||||
*/
|
||||
public static void sendRefundMessage(String portalName, Player player, int cost) {
|
||||
String refundMsg = Stargate.getString("ecoRefund");
|
||||
@ -100,9 +105,10 @@ public class EconomyHelper {
|
||||
|
||||
/**
|
||||
* Replaces the cost and portal variables in a string
|
||||
* @param message <p>The message to replace variables in</p>
|
||||
*
|
||||
* @param message <p>The message to replace variables in</p>
|
||||
* @param portalName <p>The name of the relevant portal</p>
|
||||
* @param cost <p>The cost for a given interaction</p>
|
||||
* @param cost <p>The cost for a given interaction</p>
|
||||
* @return <p>The same string with cost and portal variables replaced</p>
|
||||
*/
|
||||
private static String replaceVars(String message, String portalName, int cost) {
|
||||
|
@ -10,6 +10,7 @@ public final class MaterialHelper {
|
||||
|
||||
/**
|
||||
* Checks whether the given material is a dead or alive wall coral
|
||||
*
|
||||
* @param material <p>The material to check</p>
|
||||
* @return <p>True if the material is a wall coral</p>
|
||||
*/
|
||||
@ -24,6 +25,7 @@ public final class MaterialHelper {
|
||||
|
||||
/**
|
||||
* Checks whether the given material can be used as a button
|
||||
*
|
||||
* @param material <p>The material to check</p>
|
||||
* @return <p>True if the material can be used as a button</p>
|
||||
*/
|
||||
|
Reference in New Issue
Block a user