diff --git a/src/main/java/org/sgrewritten/stargate/Stargate.java b/src/main/java/org/sgrewritten/stargate/Stargate.java index 17947ed8..dddf04d9 100644 --- a/src/main/java/org/sgrewritten/stargate/Stargate.java +++ b/src/main/java/org/sgrewritten/stargate/Stargate.java @@ -308,6 +308,7 @@ private void registerListeners() { pluginManager.registerEvents(new MoveEventListener(getRegistry()), this); pluginManager.registerEvents(new PlayerEventListener(this.getLanguageManager(), getRegistry(), this.getBungeeManager(), this.getBlockLoggerManager(), this.getStorageAPI()), this); pluginManager.registerEvents(new PluginEventListener(getEconomyManager(), getBlockLoggerManager()), this); + pluginManager.registerEvents(new WorldEventListener(this), this); if (NonLegacyClass.PLAYER_ADVANCEMENT_CRITERION_EVENT.isImplemented()) { pluginManager.registerEvents(new PlayerAdvancementListener(getRegistry()), this); } diff --git a/src/main/java/org/sgrewritten/stargate/api/database/StorageAPI.java b/src/main/java/org/sgrewritten/stargate/api/database/StorageAPI.java index 00bb64a4..b958d404 100644 --- a/src/main/java/org/sgrewritten/stargate/api/database/StorageAPI.java +++ b/src/main/java/org/sgrewritten/stargate/api/database/StorageAPI.java @@ -1,5 +1,6 @@ package org.sgrewritten.stargate.api.database; +import org.bukkit.World; import org.jetbrains.annotations.ApiStatus; import org.sgrewritten.stargate.api.StargateAPI; import org.sgrewritten.stargate.api.network.Network; @@ -173,9 +174,10 @@ void removePortalPosition(RealPortal portal, StorageType portalType, /** * Update the network name in the database - * @param newName
The new name of the network
+ * + * @param newNameThe new name of the network
* @param networkNameThe previous name of the network
- * @param portalTypeThe storage type of the network
+ * @param portalTypeThe storage type of the network
* @throws StorageWriteExceptionIf unable to modify the database
*/ @ApiStatus.Internal @@ -183,8 +185,9 @@ void removePortalPosition(RealPortal portal, StorageType portalType, /** * Update the name of a portal - * @param newNameThe name of the portal
- * @param portalIdA portal id representing this portal
+ * + * @param newNameThe name of the portal
+ * @param portalIdA portal id representing this portal
* @param portalTypeHow the portal is stored
* @throws StorageWriteExceptionIf unable to modify the database
*/ @@ -193,7 +196,8 @@ void removePortalPosition(RealPortal portal, StorageType portalType, /** * Check if the networks exists in the database - * @param netNameNetwork name
+ * + * @param netNameNetwork name
* @param portalTypeThe storage type of the network
* @returnTrue if exists
* @throws StorageReadExceptionIf unable to read the database
@@ -204,4 +208,11 @@ void removePortalPosition(RealPortal portal, StorageType portalType, * @returnThe gate formats of the portals scheduled to be cleared
*/ SetA world
+ */ + void loadPortalsInWorld(World world, StorageType storageType, StargateAPI stargateAPI) throws StorageReadException, StorageWriteException; } diff --git a/src/main/java/org/sgrewritten/stargate/api/network/NetworkManager.java b/src/main/java/org/sgrewritten/stargate/api/network/NetworkManager.java index e91dcd4c..5ebdba1f 100644 --- a/src/main/java/org/sgrewritten/stargate/api/network/NetworkManager.java +++ b/src/main/java/org/sgrewritten/stargate/api/network/NetworkManager.java @@ -1,6 +1,7 @@ package org.sgrewritten.stargate.api.network; import org.bukkit.OfflinePlayer; +import org.bukkit.World; import org.jetbrains.annotations.NotNull; import org.sgrewritten.stargate.api.StargateAPI; import org.sgrewritten.stargate.api.network.portal.Portal; @@ -113,4 +114,10 @@ public interface NetworkManager { * @param portalDestroy portal
*/ void destroyPortal(RealPortal portal); + + /** + * Load all portals in world + * @param worldThe world to load all portals from
+ */ + void loadWorld(World world, StargateAPI stargateAPI); } diff --git a/src/main/java/org/sgrewritten/stargate/api/network/RegistryAPI.java b/src/main/java/org/sgrewritten/stargate/api/network/RegistryAPI.java index 3919c10d..fa3fbe0e 100644 --- a/src/main/java/org/sgrewritten/stargate/api/network/RegistryAPI.java +++ b/src/main/java/org/sgrewritten/stargate/api/network/RegistryAPI.java @@ -21,6 +21,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import java.util.stream.Stream; /** @@ -261,4 +262,6 @@ public interface RegistryAPI { * @returnThe portals in the chunk
*/ @NotNull SetIf an SQL error occurs
*/ private void loadAllPortals(SQLDatabaseAPI database, StorageType portalType, StargateAPI stargateAPI) throws SQLException { - ListA sql connection to the database
- * @param portalThe portal owning the portal position
+ * @param connectionA sql connection to the database
+ * @param portalThe portal owning the portal position
* @param portalPositionThe portal position to get data on
- * @param portalTypeHow the portal is stored
+ * @param portalTypeHow the portal is stored
* @returnA prepared statement able to modify fetch data on the portal position
* @throws SQLExceptionIf the syntax is incorrect or any other sql faults
*/ @@ -621,11 +621,10 @@ public PreparedStatement generateGetPortalPositionStatement(Connection connectio } /** - * - * @param connectionA sql database connection
- * @param newNameThe new name of the network
+ * @param connectionA sql database connection
+ * @param newNameThe new name of the network
* @param networkNameThe previous name of the network
- * @param portalTypeHow the portals in the network are being stored
+ * @param portalTypeHow the portals in the network are being stored
* @returnA prepared statement able to modify the network name of all portals with the specified network
* @throws SQLExceptionIf the syntax is incorrect or any other sql faults
*/ @@ -643,12 +642,11 @@ public PreparedStatement generateUpdateNetworkNameStatement(Connection connectio } /** - * - * @param connectionA sql database connection
- * @param newNameThe new name of the portal to modify
- * @param portalNameThe previous portal name
+ * @param connectionA sql database connection
+ * @param newNameThe new name of the portal to modify
+ * @param portalNameThe previous portal name
* @param networkNameThe network name of the portal
- * @param portalTypeHow the portal is being stored
+ * @param portalTypeHow the portal is being stored
* @returnA prepared statement able to change the name of a portal
* @throws SQLExceptionIf the syntax is incorrect or any other sql faults
*/ @@ -667,9 +665,8 @@ public PreparedStatement generateUpdatePortalNameStatement(Connection connection } /** - * * @param connectionA sql connection to the database
- * @param netNameThe name of the network to get all portals from
+ * @param netNameThe name of the network to get all portals from
* @param portalTypehow the portals in the network is being stored
* @returnA prepared statement able to fetch all portals in specified network
* @throws SQLExceptionIf the syntax is incorrect or any other sql faults
@@ -687,9 +684,8 @@ public PreparedStatement generateGetAllPortalsOfNetwork(Connection connection, S } /** - * - * @param connectionA sql connection to the database
- * @param worldThe world uuid to remove all portals from
+ * @param connectionA sql connection to the database
+ * @param worldThe world uuid to remove all portals from
* @param storageTypeHow the portals in the world is being stored
* @returnA prepared statement able to delete all data on portals in specified world
* @throws SQLExceptionIf the syntax is incorrect or any other sql faults
@@ -708,8 +704,8 @@ public PreparedStatement generateDeleteWorldStatement(Connection connection, Str } /** - * @param connectionA sql connection to the database
- * @param gateFormatThe file name of the gate format to remove all portals of
+ * @param connectionA sql connection to the database
+ * @param gateFormatThe file name of the gate format to remove all portals of
* @param storageTypeHow the portals are being stored
* @returnA prepared statement able to remove all portals of specified gate format
* @throws SQLExceptionIf the syntax is incorrect or any other sql faults
@@ -726,4 +722,16 @@ public PreparedStatement generateRemoveGateStatement(Connection connection, Stri } return statement; } + + public PreparedStatement generateLoadPortalsInWorldStatement(Connection connection, World world, StorageType storageType) throws SQLException { + PreparedStatement statement; + if(storageType == StorageType.LOCAL){ + statement = prepareQuery(connection, getQuery(SQLQuery.GET_ALL_PORTALS_IN_WORLD)); + } else { + statement = prepareQuery(connection, getQuery(SQLQuery.GET_ALL_INTER_PORTALS_IN_WORLD)); + statement.setString(2, Stargate.getServerUUID()); + } + statement.setString(1, world.getUID().toString()); + return statement; + } } diff --git a/src/main/java/org/sgrewritten/stargate/listener/WorldEventListener.java b/src/main/java/org/sgrewritten/stargate/listener/WorldEventListener.java new file mode 100644 index 00000000..c7c73031 --- /dev/null +++ b/src/main/java/org/sgrewritten/stargate/listener/WorldEventListener.java @@ -0,0 +1,30 @@ +package org.sgrewritten.stargate.listener; + +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.world.WorldLoadEvent; +import org.bukkit.event.world.WorldUnloadEvent; +import org.sgrewritten.stargate.api.StargateAPI; +import org.sgrewritten.stargate.api.network.portal.RealPortal; + +public class WorldEventListener implements Listener { + + private final StargateAPI stargateAPI; + + public WorldEventListener(StargateAPI stargateAPI) { + this.stargateAPI = stargateAPI; + } + + @EventHandler + void onWordUnload(WorldUnloadEvent event) { + stargateAPI.getRegistry().getAllPortals().filter(portal -> portal instanceof RealPortal) + .map(portal -> (RealPortal) portal) + .filter(realPortal -> realPortal.getExit().getWorld().equals(event.getWorld())) + .forEach(stargateAPI.getRegistry()::unregisterPortal); + } + + @EventHandler + void onWorldLoad(WorldLoadEvent event) { + stargateAPI.getNetworkManager().loadWorld(event.getWorld(), stargateAPI); + } +} diff --git a/src/main/java/org/sgrewritten/stargate/network/StargateNetworkManager.java b/src/main/java/org/sgrewritten/stargate/network/StargateNetworkManager.java index 4565746c..3831b966 100644 --- a/src/main/java/org/sgrewritten/stargate/network/StargateNetworkManager.java +++ b/src/main/java/org/sgrewritten/stargate/network/StargateNetworkManager.java @@ -2,6 +2,7 @@ import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; +import org.bukkit.World; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.sgrewritten.stargate.Stargate; @@ -13,9 +14,9 @@ import org.sgrewritten.stargate.api.network.NetworkRegistry; import org.sgrewritten.stargate.api.network.RegistryAPI; import org.sgrewritten.stargate.api.network.portal.Portal; +import org.sgrewritten.stargate.api.network.portal.RealPortal; import org.sgrewritten.stargate.api.network.portal.flag.PortalFlag; import org.sgrewritten.stargate.api.network.portal.flag.StargateFlag; -import org.sgrewritten.stargate.api.network.portal.RealPortal; import org.sgrewritten.stargate.api.permission.PermissionManager; import org.sgrewritten.stargate.config.ConfigurationHelper; import org.sgrewritten.stargate.container.TwoTuple; @@ -43,9 +44,8 @@ public class StargateNetworkManager implements NetworkManager { private final StorageAPI storageAPI; /** - * * @param registryAPIA registry containing all portal information
- * @param storageAPIA database interface able to give and modify information about portals
+ * @param storageAPIA database interface able to give and modify information about portals
*/ public StargateNetworkManager(RegistryAPI registryAPI, StorageAPI storageAPI) { this.registry = registryAPI; @@ -286,6 +286,23 @@ public void run() { portal.getNetwork().getPluginMessageSender().sendDeletePortal(portal); } + @Override + public void loadWorld(World world, StargateAPI stargateAPI) { + new StargateQueuedAsyncTask() { + @Override + public void run() { + try { + storageAPI.loadPortalsInWorld(world, StorageType.LOCAL, stargateAPI); + if (ConfigurationHelper.getBoolean(ConfigurationOption.USING_BUNGEE) && ConfigurationHelper.getBoolean(ConfigurationOption.USING_REMOTE_DATABASE)) { + storageAPI.loadPortalsInWorld(world, StorageType.INTER_SERVER, stargateAPI); + } + } catch (StorageReadException | StorageWriteException e) { + Stargate.log(e); + } + } + }.runNow(); + } + /** * Gets a player's UUID from the player's name diff --git a/src/main/java/org/sgrewritten/stargate/network/StargateNetworkRegistry.java b/src/main/java/org/sgrewritten/stargate/network/StargateNetworkRegistry.java index 28cdada3..4dd46ba2 100644 --- a/src/main/java/org/sgrewritten/stargate/network/StargateNetworkRegistry.java +++ b/src/main/java/org/sgrewritten/stargate/network/StargateNetworkRegistry.java @@ -127,4 +127,5 @@ public void closeAllPortals() { public int size() { return networkMap.size(); } + } diff --git a/src/main/java/org/sgrewritten/stargate/network/StargateRegistry.java b/src/main/java/org/sgrewritten/stargate/network/StargateRegistry.java index 56509dfa..f2fb5eef 100644 --- a/src/main/java/org/sgrewritten/stargate/network/StargateRegistry.java +++ b/src/main/java/org/sgrewritten/stargate/network/StargateRegistry.java @@ -17,12 +17,7 @@ import org.sgrewritten.stargate.api.network.Network; import org.sgrewritten.stargate.api.network.NetworkRegistry; import org.sgrewritten.stargate.api.network.RegistryAPI; -import org.sgrewritten.stargate.api.network.portal.BlockLocation; -import org.sgrewritten.stargate.api.network.portal.Portal; -import org.sgrewritten.stargate.api.network.portal.PortalPosition; -import org.sgrewritten.stargate.api.network.portal.PositionType; -import org.sgrewritten.stargate.api.network.portal.RealPortal; -import org.sgrewritten.stargate.api.network.portal.StargateChunk; +import org.sgrewritten.stargate.api.network.portal.*; import org.sgrewritten.stargate.exception.UnimplementedFlagException; import org.sgrewritten.stargate.exception.database.StorageReadException; import org.sgrewritten.stargate.exception.database.StorageWriteException; @@ -33,15 +28,9 @@ import org.sgrewritten.stargate.util.ExceptionHelper; import org.sgrewritten.stargate.util.VectorUtils; -import java.util.ArrayList; -import java.util.EnumMap; -import java.util.HashMap; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.UUID; +import java.util.*; import java.util.logging.Level; +import java.util.stream.Stream; /** * Registry of all portals and networks @@ -314,7 +303,7 @@ public Map