11package nl .rutgerkok .betterenderchest .importers ;
22
3- import java .io .IOException ;
43import java .util .HashSet ;
54import java .util .List ;
65import java .util .Set ;
6+ import java .util .concurrent .ExecutionException ;
77
8+ import com .google .common .util .concurrent .Futures ;
9+ import com .google .common .util .concurrent .ListenableFuture ;
10+ import com .google .common .util .concurrent .SettableFuture ;
11+ import nl .rutgerkok .betterenderchest .exception .ChestNotFoundException ;
812import org .bukkit .Bukkit ;
913import org .bukkit .OfflinePlayer ;
1014import org .bukkit .inventory .Inventory ;
1115import org .bukkit .inventory .ItemStack ;
1216
13- import com .onarandombox .multiverseinventories .MultiverseInventories ;
14- import com .onarandombox .multiverseinventories .WorldGroup ;
15- import com .onarandombox .multiverseinventories .profile .GlobalProfile ;
16- import com .onarandombox .multiverseinventories .profile .PlayerProfile ;
17- import com .onarandombox .multiverseinventories .profile .ProfileType ;
18- import com .onarandombox .multiverseinventories .profile .ProfileTypes ;
19- import com .onarandombox .multiverseinventories .share .Sharables ;
17+ import org .mvplugins .multiverse .inventories .MultiverseInventoriesApi ;
18+ import org .mvplugins .multiverse .inventories .profile .data .PlayerProfile ;
19+ import org .mvplugins .multiverse .inventories .profile .key .GlobalProfileKey ;
20+ import org .mvplugins .multiverse .inventories .profile .key .ProfileType ;
21+ import org .mvplugins .multiverse .inventories .profile .key .ProfileTypes ;
22+ import org .mvplugins .multiverse .inventories .share .Sharables ;
2023
2124import nl .rutgerkok .betterenderchest .BetterEnderChest ;
2225import nl .rutgerkok .betterenderchest .chestowner .ChestOwner ;
26+ import org .mvplugins .multiverse .inventories .profile .group .WorldGroup ;
2327
2428
2529public class MultiverseInventoriesImporter extends InventoryImporter {
@@ -35,96 +39,107 @@ public Priority getPriority() {
3539 }
3640
3741 @ Override
38- public Inventory importInventory (ChestOwner chestOwner , nl .rutgerkok .betterenderchest .WorldGroup worldGroup ,
39- BetterEnderChest plugin ) throws IOException {
42+ public ListenableFuture <Inventory > importInventoryAsync (final ChestOwner chestOwner , nl .rutgerkok .betterenderchest .WorldGroup worldGroup , BetterEnderChest plugin ) {
4043 String groupName = worldGroup .getGroupName ();
4144
4245 OfflinePlayer offlinePlayer = chestOwner .getOfflinePlayer ();
4346 if (offlinePlayer == null || chestOwner .isSpecialChest ()) {
4447 // Public chests and default chests cannot be imported.
45- return null ;
48+ return Futures . immediateFailedFuture ( new ChestNotFoundException ( chestOwner , worldGroup )) ;
4649 }
4750
4851 // Get the plugin
49- MultiverseInventories multiverseInventories = ( MultiverseInventories ) Bukkit . getServer (). getPluginManager (). getPlugin ( "Multiverse-Inventories" );
52+ MultiverseInventoriesApi multiverseInventories = MultiverseInventoriesApi . get ( );
5053
5154 // Make groupName case-correct
52- WorldGroup group = null ;
53- List <WorldGroup > multiverseInventoriesGroups = multiverseInventories .getGroupManager ().getGroups ();
54- for (WorldGroup aGroup : multiverseInventoriesGroups ) {
55- if (aGroup .getName ().equalsIgnoreCase (groupName )) {
56- group = aGroup ;
57- break ;
58- }
59- }
55+ List <WorldGroup > multiverseInventoriesGroups = multiverseInventories .getWorldGroupManager ().getGroups ();
56+
57+ WorldGroup group = multiverseInventoriesGroups .stream ()
58+ .filter (aGroup -> aGroup .getName ().equalsIgnoreCase (groupName ))
59+ .findAny ()
60+ .orElse (null );
6061
6162 // Check if a matching group has been found
6263 if (group == null ) {
6364 plugin .warning ("No matching Multiverse-Inventories group found for " + groupName + ". Cannot import " + chestOwner .getDisplayName () + "." );
64- return null ;
65+ return Futures . immediateFailedFuture ( new ChestNotFoundException ( chestOwner , worldGroup )) ;
6566 }
6667
6768 // Get the global profile of the player
68- GlobalProfile globalProfile = multiverseInventories .getData ().getGlobalProfile (offlinePlayer .getName (),
69- offlinePlayer .getUniqueId ());
70- if (globalProfile == null ) {
71- plugin .debug ("It seems that there is no data for " + chestOwner .getDisplayName () + ", so nothing can be imported." );
72- return null ;
73- }
74- if (globalProfile .getLastWorld () == null ) {
75- plugin .debug ("It seems that the world of " + chestOwner .getDisplayName () + " is null, so nothing can be imported." );
76- return null ;
77- }
78-
79- // If the player is in the current worldgroup, it should load from
80- // vanilla (Multiverse-Inventories would return an outdated inventory).
81- // If the player is in anthor worldgroup, it should load from
82- // Multiverse-Inventories.
83- if (group .containsWorld (globalProfile .getLastWorld ())) {
84- // Player is in the current group, load from vanilla
85- return plugin .getInventoryImporters ().getRegistration ("vanilla" ).importInventory (chestOwner , worldGroup , plugin );
86- } else {
87- // Get the correct gamemode
88- ProfileType profileType ;
89- if (multiverseInventories .getMVIConfig ().isUsingGameModeProfiles ()) {
90- // BetterEnderChest doesn't support seperation of gamemodes, so
91- // use the default gamemode of the server
92- profileType = ProfileTypes .forGameMode (Bukkit .getDefaultGameMode ());
93- } else {
94- // Multiverse-Inventories gamemode seperation disabled, use
95- // SURVIVAL
96- profileType = ProfileTypes .SURVIVAL ;
69+ SettableFuture <Inventory > returnedInventory = SettableFuture .create ();
70+ multiverseInventories .getProfileDataSource ().getGlobalProfile (GlobalProfileKey .of (offlinePlayer )).thenAcceptAsync (globalProfile -> {
71+ if (globalProfile == null ) {
72+ plugin .debug ("It seems that there is no data for " + chestOwner .getDisplayName () + ", so nothing can be imported." );
73+ returnedInventory .setException (new ChestNotFoundException (chestOwner , worldGroup ));
74+ return ;
9775 }
98-
99- // Get the data
100- PlayerProfile playerData = multiverseInventories .getGroupManager ().getGroup (groupName )
101- .getGroupProfileContainer ().getPlayerData (profileType , offlinePlayer );
102-
103- // Return nothing if there is nothing
104- if (playerData == null ) {
105- return null ;
76+ if (globalProfile .getLastWorld () == null ) {
77+ plugin .debug ("It seems that the world of " + chestOwner .getDisplayName () + " is null, so nothing can be imported." );
78+ returnedInventory .setException (new ChestNotFoundException (chestOwner , worldGroup ));
79+ return ;
10680 }
10781
108- // Get the item stacks
109- ItemStack [] stacks = playerData .get (Sharables .ENDER_CHEST );
110-
111- // Return nothing if there is nothing
112- if (stacks == null || stacks .length == 0 ) {
113- return null ;
82+ // If the player is in the current worldgroup, it should load from
83+ // vanilla (Multiverse-Inventories would return an outdated inventory).
84+ // If the player is in anothor worldgroup, it should load from
85+ // Multiverse-Inventories.
86+ if (group .containsWorld (globalProfile .getLastWorld ())) {
87+ // Player is in the current group, load from vanilla
88+ returnedInventory .setFuture (plugin .getInventoryImporters ().getRegistration ("vanilla" ).importInventoryAsync (chestOwner , worldGroup , plugin ));
89+ } else {
90+ // Get the correct gamemode
91+ ProfileType profileType ;
92+ if (multiverseInventories .getInventoriesConfig ().getEnableGamemodeShareHandling ()) {
93+ // BetterEnderChest doesn't support seperation of gamemodes, so
94+ // use the default gamemode of the server
95+ profileType = ProfileTypes .forGameMode (Bukkit .getDefaultGameMode ());
96+ } else {
97+ // Multiverse-Inventories gamemode seperation disabled, use the default
98+ profileType = ProfileTypes .getDefault ();
99+ }
100+
101+ // Get the data (we can halt this thread, we're on a worker thread anyway)
102+ try {
103+ PlayerProfile playerData = multiverseInventories .getWorldGroupManager ().getGroup (groupName )
104+ .getGroupProfileContainer ().getPlayerData (profileType , offlinePlayer ).get ();
105+
106+ // Return nothing if there is nothing
107+ if (playerData == null ) {
108+ returnedInventory .setException (new ChestNotFoundException (chestOwner , worldGroup ));
109+ return ;
110+ }
111+
112+ // Get the item stacks
113+ ItemStack [] stacks = playerData .get (Sharables .ENDER_CHEST );
114+
115+ // Return nothing if there is nothing
116+ if (stacks == null || stacks .length == 0 ) {
117+ returnedInventory .setException (new ChestNotFoundException (chestOwner , worldGroup ));
118+ return ;
119+ }
120+
121+ // Add everything from Multiverse-Inventories to betterInventory
122+ Inventory betterInventory = plugin .getEmptyInventoryProvider ().loadEmptyInventory (chestOwner , worldGroup );
123+ betterInventory .setContents (stacks );
124+ returnedInventory .set (betterInventory );
125+ } catch (InterruptedException e ) {
126+ returnedInventory .setException (e );
127+ } catch (ExecutionException e ) {
128+ returnedInventory .setException (e .getCause ());
129+ }
114130 }
115-
116- // Add everything from Multiverse-Inventories to betterInventory
117- Inventory betterInventory = plugin .getEmptyInventoryProvider ().loadEmptyInventory (chestOwner , worldGroup );
118- betterInventory .setContents (stacks );
119- return betterInventory ;
120- }
131+ }, plugin .getExecutors ().workerThreadExecutor ()).exceptionally (e -> {
132+ returnedInventory .setException (e );
133+ return null ;
134+ });
135+ return returnedInventory ;
121136 }
122137
123138 @ Override
124139 public Iterable <nl .rutgerkok .betterenderchest .WorldGroup > importWorldGroups (BetterEnderChest plugin ) {
125140 Set <nl .rutgerkok .betterenderchest .WorldGroup > becGroups = new HashSet <>();
126- MultiverseInventories multiverseInventories = ( MultiverseInventories ) Bukkit . getServer (). getPluginManager (). getPlugin ( "Multiverse-Inventories" );
127- for (WorldGroup miGroup : multiverseInventories .getGroupManager ().getGroups ()) {
141+ MultiverseInventoriesApi multiverseInventories = MultiverseInventoriesApi . get ( );
142+ for (WorldGroup miGroup : multiverseInventories .getWorldGroupManager ().getGroups ()) {
128143 // Convert each group config
129144 nl .rutgerkok .betterenderchest .WorldGroup worldGroup = new nl .rutgerkok .betterenderchest .WorldGroup (
130145 miGroup .getName ());
0 commit comments