Removes the last of the unprotected variables from Stargate

Makes all classes use getInstance() to get a Stargate instance
Removes the server variable as it's not needed
This commit is contained in:
Kristian Knarvik 2021-10-29 18:35:20 +02:00
parent 0237f45046
commit 56ed0495b0
14 changed files with 47 additions and 35 deletions

View File

@ -48,8 +48,7 @@ public class Stargate extends JavaPlugin {
private static final Queue<ChunkUnloadRequest> chunkUnloadQueue = new PriorityQueue<>();
private static Logger logger;
public static Server server;
public static Stargate stargate;
private static Stargate stargate;
private static String pluginVersion;
@ -75,6 +74,15 @@ public class Stargate extends JavaPlugin {
super(loader, descriptionFile, dataFolder, file);
}
/**
* Gets an instance of this plugin
*
* @return <p>An instance of this plugin, or null if not instantiated</p>
*/
public static Stargate getInstance() {
return stargate;
}
/**
* Adds a block change request to the request queue
*
@ -299,7 +307,7 @@ public class Stargate extends JavaPlugin {
pluginManager = getServer().getPluginManager();
FileConfiguration newConfig = this.getConfig();
logger = Logger.getLogger("Minecraft");
server = getServer();
Server server = getServer();
stargate = this;
stargateConfig = new StargateConfig(logger);

View File

@ -235,7 +235,7 @@ public final class EconomyConfig {
//Check if vault is loaded
Plugin vault = pluginManager.getPlugin("Vault");
if (vault != null && vault.isEnabled()) {
ServicesManager servicesManager = Stargate.server.getServicesManager();
ServicesManager servicesManager = Stargate.getInstance().getServer().getServicesManager();
RegisteredServiceProvider<Economy> economyProvider = servicesManager.getRegistration(Economy.class);
if (economyProvider != null) {
economy = economyProvider.getProvider();

View File

@ -56,7 +56,7 @@ public final class StargateConfig {
public StargateConfig(Logger logger) {
this.logger = logger;
dataFolderPath = Stargate.stargate.getDataFolder().getPath().replaceAll("\\\\", "/");
dataFolderPath = Stargate.getInstance().getDataFolder().getPath().replaceAll("\\\\", "/");
portalFolder = dataFolderPath + "/portals/";
gateFolder = dataFolderPath + "/gates/";
languageLoader = new LanguageLoader(dataFolderPath + "/lang/");
@ -260,11 +260,11 @@ public final class StargateConfig {
String bungeeChannel = "BungeeCord";
if (start) {
messenger.registerOutgoingPluginChannel(Stargate.stargate, bungeeChannel);
messenger.registerIncomingPluginChannel(Stargate.stargate, bungeeChannel, new BungeeCordListener());
messenger.registerOutgoingPluginChannel(Stargate.getInstance(), bungeeChannel);
messenger.registerIncomingPluginChannel(Stargate.getInstance(), bungeeChannel, new BungeeCordListener());
} else {
messenger.unregisterIncomingPluginChannel(Stargate.stargate, bungeeChannel);
messenger.unregisterOutgoingPluginChannel(Stargate.stargate, bungeeChannel);
messenger.unregisterIncomingPluginChannel(Stargate.getInstance(), bungeeChannel);
messenger.unregisterOutgoingPluginChannel(Stargate.getInstance(), bungeeChannel);
}
}
@ -293,8 +293,8 @@ public final class StargateConfig {
* Loads all config values
*/
public void loadConfig() {
Stargate.stargate.reloadConfig();
FileConfiguration newConfig = Stargate.stargate.getConfig();
Stargate.getInstance().reloadConfig();
FileConfiguration newConfig = Stargate.getInstance().getConfig();
boolean isMigrating = false;
if (newConfig.getString("lang") != null ||
@ -329,7 +329,7 @@ public final class StargateConfig {
//Load all economy config values
economyConfig = new EconomyConfig(newConfig);
Stargate.stargate.saveConfig();
Stargate.getInstance().saveConfig();
}
/**
@ -403,7 +403,7 @@ public final class StargateConfig {
* Loads all portals in all un-managed worlds
*/
public void loadAllPortals() {
for (World world : Stargate.stargate.getServer().getWorlds()) {
for (World world : Stargate.getInstance().getServer().getWorlds()) {
if (!managedWorlds.contains(world.getName())) {
PortalFileHelper.loadAllPortals(world);
managedWorlds.add(world.getName());
@ -421,7 +421,8 @@ public final class StargateConfig {
logger.severe("Unable to create portal directory");
}
}
File newFile = new File(portalFolder, Stargate.stargate.getServer().getWorlds().get(0).getName() + ".db");
File newFile = new File(portalFolder, Stargate.getInstance().getServer().getWorlds().get(0).getName() +
".db");
if (!newFile.exists() && !newFile.getParentFile().exists()) {
if (!newFile.getParentFile().mkdirs()) {
logger.severe("Unable to create portal database folder: " + newFile.getParentFile().getPath());

View File

@ -83,7 +83,8 @@ public class BlockEventListener implements Listener {
Stargate.getMessageSender().sendSuccessMessage(player, Stargate.getString("createMsg"));
Stargate.debug("onSignChange", "Initialized stargate: " + portal.getName());
Stargate.server.getScheduler().scheduleSyncDelayedTask(Stargate.stargate, portal::drawSign, 1);
Stargate.getInstance().getServer().getScheduler().scheduleSyncDelayedTask(Stargate.getInstance(),
portal::drawSign, 1);
}
/**
@ -122,7 +123,7 @@ public class BlockEventListener implements Listener {
//Create and call a StarGateDestroyEvent
StargateDestroyEvent destroyEvent = new StargateDestroyEvent(portal, player, deny, denyMessage, cost);
Stargate.server.getPluginManager().callEvent(destroyEvent);
Stargate.getInstance().getServer().getPluginManager().callEvent(destroyEvent);
if (destroyEvent.isCancelled()) {
event.setCancelled(true);
return;

View File

@ -67,7 +67,7 @@ public class PlayerTeleporter extends Teleporter {
*/
private Location triggerPlayerPortalEvent(Portal origin, Location exit, PlayerMoveEvent event) {
StargatePlayerPortalEvent stargatePlayerPortalEvent = new StargatePlayerPortalEvent(player, origin, portal, exit);
Stargate.server.getPluginManager().callEvent(stargatePlayerPortalEvent);
Stargate.getInstance().getServer().getPluginManager().callEvent(stargatePlayerPortalEvent);
//Teleport is cancelled. Teleport the player back to where it came from
if (stargatePlayerPortalEvent.isCancelled()) {
new PlayerTeleporter(origin, player).teleport(origin, event);

View File

@ -164,7 +164,7 @@ public class PortalActivator {
*/
private boolean triggerStargateActivationEvent(Player player) {
StargateActivateEvent event = new StargateActivateEvent(portal, player, destinations, destination);
Stargate.server.getPluginManager().callEvent(event);
Stargate.getInstance().getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) {
Stargate.getStargateConfig().getActivePortalsQueue().remove(portal);
return false;
@ -183,7 +183,7 @@ public class PortalActivator {
public void deactivate() {
//Trigger a stargate deactivate event to allow other plugins to cancel the event
StargateDeactivateEvent event = new StargateDeactivateEvent(portal);
Stargate.server.getPluginManager().callEvent(event);
Stargate.getInstance().getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) {
return;
}

View File

@ -177,7 +177,7 @@ public class PortalCreator {
//Call StargateCreateEvent to let other plugins cancel or overwrite denial
StargateCreateEvent stargateCreateEvent = new StargateCreateEvent(player, portal, lines, deny,
denyMessage, createCost);
Stargate.server.getPluginManager().callEvent(stargateCreateEvent);
Stargate.getInstance().getServer().getPluginManager().callEvent(stargateCreateEvent);
if (stargateCreateEvent.isCancelled()) {
return null;
}

View File

@ -76,7 +76,7 @@ public class PortalOpener {
public void openPortal(Player openFor, boolean force) {
//Call the StargateOpenEvent to allow the opening to be cancelled
StargateOpenEvent event = new StargateOpenEvent(openFor, portal, force);
Stargate.server.getPluginManager().callEvent(event);
Stargate.getInstance().getServer().getPluginManager().callEvent(event);
if (event.isCancelled() || (isOpen() && !event.getForce())) {
return;
}
@ -152,7 +152,7 @@ public class PortalOpener {
//Call the StargateCloseEvent to allow other plugins to cancel the closing, or change whether to force it closed
StargateCloseEvent event = new StargateCloseEvent(portal, force);
Stargate.server.getPluginManager().callEvent(event);
Stargate.getInstance().getServer().getPluginManager().callEvent(event);
if (event.isCancelled()) {
return;
}

View File

@ -34,7 +34,7 @@ public abstract class Teleporter {
*/
public Teleporter(Portal portal) {
this.portal = portal;
this.scheduler = Stargate.server.getScheduler();
this.scheduler = Stargate.getInstance().getServer().getScheduler();
}
@ -207,7 +207,7 @@ public abstract class Teleporter {
*/
protected void loadChunks() {
for (Chunk chunk : getChunksToLoad()) {
chunk.addPluginChunkTicket(Stargate.stargate);
chunk.addPluginChunkTicket(Stargate.getInstance());
//Allow the chunk to unload after 3 seconds
Stargate.addChunkUnloadRequest(new ChunkUnloadRequest(chunk, 3000L));
}

View File

@ -88,7 +88,8 @@ public class VehicleTeleporter extends Teleporter {
} else {
//Teleport an empty vehicle
teleportingVehicle.teleport(exit);
scheduler.scheduleSyncDelayedTask(Stargate.stargate, () -> teleportingVehicle.setVelocity(newVelocity), 1);
scheduler.scheduleSyncDelayedTask(Stargate.getInstance(),
() -> teleportingVehicle.setVelocity(newVelocity), 1);
}
}
@ -102,7 +103,7 @@ public class VehicleTeleporter extends Teleporter {
private Location triggerEntityPortalEvent(Portal origin, Location exit) {
StargateEntityPortalEvent stargateEntityPortalEvent = new StargateEntityPortalEvent(teleportingVehicle, origin,
portal, exit);
Stargate.server.getPluginManager().callEvent(stargateEntityPortalEvent);
Stargate.getInstance().getServer().getPluginManager().callEvent(stargateEntityPortalEvent);
//Teleport is cancelled. Teleport the entity back to where it came from just for sanity's sake
if (stargateEntityPortalEvent.isCancelled()) {
new VehicleTeleporter(origin, teleportingVehicle).teleport(origin);
@ -149,7 +150,7 @@ public class VehicleTeleporter extends Teleporter {
//Set rotation, add passengers and restore velocity
newVehicle.setRotation(exit.getYaw(), exit.getPitch());
handleVehiclePassengers(passengers, newVehicle, 1);
scheduler.scheduleSyncDelayedTask(Stargate.stargate, () -> newVehicle.setVelocity(newVelocity), 1);
scheduler.scheduleSyncDelayedTask(Stargate.getInstance(), () -> newVehicle.setVelocity(newVelocity), 1);
}
/**
@ -162,7 +163,8 @@ public class VehicleTeleporter extends Teleporter {
private void handleVehiclePassengers(List<Entity> passengers, Vehicle vehicle, long delay) {
for (Entity passenger : passengers) {
passenger.eject();
scheduler.scheduleSyncDelayedTask(Stargate.stargate, () -> teleportAndAddPassenger(vehicle, passenger), delay);
scheduler.scheduleSyncDelayedTask(Stargate.getInstance(), () -> teleportAndAddPassenger(vehicle, passenger),
delay);
}
}

View File

@ -23,7 +23,7 @@ public class ChunkUnloadThread implements Runnable {
unloadQueue.remove();
Chunk chunkToUnload = firstElement.getChunkToUnload();
//Allow the chunk to be unloaded
chunkToUnload.removePluginChunkTicket(Stargate.stargate);
chunkToUnload.removePluginChunkTicket(Stargate.getInstance());
firstElement = unloadQueue.peek();
}
}

View File

@ -78,7 +78,7 @@ public final class BungeeHelper {
//Write the actual message
dataOutputStream.writeBytes(message);
//Send the plugin message
player.sendPluginMessage(Stargate.stargate, bungeeChannel, byteArrayOutputStream.toByteArray());
player.sendPluginMessage(Stargate.getInstance(), bungeeChannel, byteArrayOutputStream.toByteArray());
} catch (IOException ex) {
Stargate.logSevere("Error sending BungeeCord teleport packet");
ex.printStackTrace();
@ -104,7 +104,7 @@ public final class BungeeHelper {
dataOutputStream.writeUTF(entrancePortal.getNetwork());
//Send the plugin message
player.sendPluginMessage(Stargate.stargate, bungeeChannel, byteArrayOutputStream.toByteArray());
player.sendPluginMessage(Stargate.getInstance(), bungeeChannel, byteArrayOutputStream.toByteArray());
} catch (IOException ex) {
Stargate.logSevere("Error sending BungeeCord connect packet");
ex.printStackTrace();
@ -155,7 +155,7 @@ public final class BungeeHelper {
String destination = messageParts[1];
//Check if the player is online, if so, teleport, otherwise, queue
Player player = Stargate.server.getPlayer(playerUUID);
Player player = Stargate.getInstance().getServer().getPlayer(playerUUID);
if (player == null) {
bungeeQueue.put(playerUUID, destination);
} else {

View File

@ -53,9 +53,9 @@ public final class EconomyHelper {
PortalOwner owner = entrancePortal.getOwner();
Player portalOwner;
if (owner.getUUID() != null) {
portalOwner = Stargate.server.getPlayer(owner.getUUID());
portalOwner = Stargate.getInstance().getServer().getPlayer(owner.getUUID());
} else {
portalOwner = Stargate.server.getPlayer(owner.getName());
portalOwner = Stargate.getInstance().getServer().getPlayer(owner.getName());
}
//Notify the gate owner of received payment

View File

@ -81,7 +81,7 @@ public final class PermissionHelper {
*/
public static boolean portalAccessDenied(Player player, Portal portal, boolean deny) {
StargateAccessEvent event = new StargateAccessEvent(player, portal, deny);
Stargate.server.getPluginManager().callEvent(event);
Stargate.getInstance().getServer().getPluginManager().callEvent(event);
return event.getDeny();
}