Skip to content

Commit b1455fe

Browse files
authored
Merge pull request #576 from Multiverse/ben/mv5/async-cache
Ben/mv5/async cache
2 parents c3c16cd + 5c85eb0 commit b1455fe

40 files changed

+756
-501
lines changed

build.gradle

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ dependencies {
3939
exclude group: 'junit', module: 'junit'
4040
}
4141

42+
// Caching
43+
shadowed("com.github.ben-manes.caffeine:caffeine:3.2.0")
44+
4245
// Other plugins for import
4346
compileOnly('uk.co:MultiInv:3.0.6') {
4447
exclude group: '*', module: '*'
@@ -64,6 +67,8 @@ shadowJar {
6467
relocate 'com.dumptruckman.minecraft.util.DebugLog', 'org.mvplugins.multiverse.inventories.utils.DebugFileLogger'
6568
relocate 'com.dumptruckman.bukkit.configuration', 'org.mvplugins.multiverse.inventories.utils.configuration'
6669
relocate 'net.minidev', 'org.mvplugins.multiverse.inventories.utils.minidev'
70+
relocate 'com.github.benmanes', 'org.mvplugins.multiverse.inventories.utils.benmanes'
71+
relocate 'com.google.errorprone', 'org.mvplugins.multiverse.inventories.utils.errorprone'
6772

6873
dependencies {
6974
exclude(dependency {

src/main/java/org/mvplugins/multiverse/inventories/commands/CacheCommand.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.mvplugins.multiverse.inventories.commands;
22

3-
import com.google.common.cache.CacheStats;
3+
import com.github.benmanes.caffeine.cache.stats.CacheStats;
44
import org.bukkit.entity.Player;
55
import org.jvnet.hk2.annotations.Service;
66
import org.mvplugins.multiverse.core.commandtools.MVCommandIssuer;
@@ -38,7 +38,6 @@ void onCacheStatsCommand(MVCommandIssuer issuer) {
3838
issuer.sendMessage(" hits count: " + entry.getValue().hitCount());
3939
issuer.sendMessage(" misses count: " + entry.getValue().missCount());
4040
issuer.sendMessage(" loads count: " + entry.getValue().loadCount());
41-
issuer.sendMessage(" exceptions: " + entry.getValue().loadExceptionCount());
4241
issuer.sendMessage(" evictions: " + entry.getValue().evictionCount());
4342
issuer.sendMessage(" hit rate: " + entry.getValue().hitRate() * 100 + "%");
4443
issuer.sendMessage(" miss rate: " + entry.getValue().missRate() * 100 + "%");

src/main/java/org/mvplugins/multiverse/inventories/config/InventoriesConfig.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import java.io.IOException;
1818
import java.nio.file.Path;
19+
import java.util.List;
1920

2021
/**
2122
* Provides methods for interacting with the configuration of Multiverse-Inventories.
@@ -200,6 +201,21 @@ public Try<Void> setAlwaysWriteWorldProfile(boolean alwaysWriteWorldProfile) {
200201
return this.configHandle.set(configNodes.alwaysWriteWorldProfile, alwaysWriteWorldProfile);
201202
}
202203

204+
public List<String> getPreloadDataOnJoinWorlds() {
205+
return this.configHandle.get(configNodes.preloadDataOnJoinWorlds);
206+
}
207+
208+
public Try<Void> setPreloadDataOnJoinWorlds(List<String> preloadDataOnJoinWorlds) {
209+
return this.configHandle.set(configNodes.preloadDataOnJoinWorlds, preloadDataOnJoinWorlds);
210+
}
211+
212+
public List<String> getPreloadDataOnJoinGroups() {
213+
return this.configHandle.get(configNodes.preloadDataOnJoinGroups);
214+
}
215+
216+
public Try<Void> setPreloadDataOnJoinGroups(List<String> preloadDataOnJoinGroups) {
217+
return this.configHandle.set(configNodes.preloadDataOnJoinGroups, preloadDataOnJoinGroups);
218+
}
203219

204220
public int getPlayerFileCacheSize() {
205221
return this.configHandle.get(configNodes.playerFileCacheSize);

src/main/java/org/mvplugins/multiverse/inventories/config/InventoriesConfigNodes.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
import org.mvplugins.multiverse.core.configuration.functions.NodeSerializer;
44
import org.mvplugins.multiverse.core.configuration.node.ConfigHeaderNode;
55
import org.mvplugins.multiverse.core.configuration.node.ConfigNode;
6+
import org.mvplugins.multiverse.core.configuration.node.ListConfigNode;
67
import org.mvplugins.multiverse.core.configuration.node.Node;
78
import org.mvplugins.multiverse.core.configuration.node.NodeGroup;
89
import org.mvplugins.multiverse.inventories.share.Sharables;
910
import org.mvplugins.multiverse.inventories.share.Shares;
1011

12+
import java.util.ArrayList;
1113
import java.util.List;
1214
import java.util.Objects;
1315

@@ -160,6 +162,20 @@ public Object serialize(Shares sharables, Class<Shares> aClass) {
160162
.name("always-write-world-profile")
161163
.build());
162164

165+
private final ConfigHeaderNode preloadHeader = node(ConfigHeaderNode.builder("performance.preload-data-on-join")
166+
.comment("")
167+
.build());
168+
169+
final ListConfigNode<String> preloadDataOnJoinWorlds = node(ListConfigNode.listBuilder("performance.preload-data-on-join.worlds", String.class)
170+
.defaultValue(ArrayList::new)
171+
.name("preload-data-on-join-worlds")
172+
.build());
173+
174+
final ListConfigNode<String> preloadDataOnJoinGroups = node(ListConfigNode.listBuilder("performance.preload-data-on-join.groups", String.class)
175+
.defaultValue(ArrayList::new)
176+
.name("preload-data-on-join-groups")
177+
.build());
178+
163179
private final ConfigHeaderNode cacheHeader = node(ConfigHeaderNode.builder("performance.cache")
164180
.comment("")
165181
.comment("NOTE: Cache options require a server restart to take effect.")

src/main/java/org/mvplugins/multiverse/inventories/dataimport/multiinv/MultiInvImportHelper.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,10 @@ private void mergeData(OfflinePlayer player, MIPlayerFileLoader playerFileLoader
106106
Logging.warning("Could not import player data for group: " + dataName);
107107
return;
108108
}
109-
playerProfile = group.getGroupProfileContainer().getPlayerData(ProfileTypes.SURVIVAL, player);
109+
playerProfile = group.getGroupProfileContainer().getPlayerDataNow(ProfileTypes.SURVIVAL, player);
110110
} else {
111111
playerProfile = profileContainerStoreProvider.getStore(type)
112-
.getContainer(dataName).getPlayerData(ProfileTypes.SURVIVAL, player);
112+
.getContainer(dataName).getPlayerDataNow(ProfileTypes.SURVIVAL, player);
113113
}
114114
MIInventoryInterface inventoryInterface =
115115
playerFileLoader.getInventory(GameMode.SURVIVAL.toString());

src/main/java/org/mvplugins/multiverse/inventories/dataimport/perworldinventory/PwiImportHelper.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ private void saveMVDataForGroup(Group group) throws DataImportException {
180180
}
181181

182182
private void saveMVDataForPlayer(Group group, OfflinePlayer offlinePlayer) throws DataImportException {
183-
GlobalProfile globalProfile = profileDataSource.getGlobalProfile(offlinePlayer);
183+
GlobalProfile globalProfile = profileDataSource.getGlobalProfileNow(offlinePlayer);
184184
globalProfile.setLoadOnLogin(pwiSettings.getProperty(PluginSettings.LOAD_DATA_ON_JOIN));
185185
profileDataSource.updateGlobalProfile(globalProfile);
186186
for (GameMode gameMode : GameMode.values()) {
@@ -205,10 +205,10 @@ private void saveMVDataForPlayer(Group group, OfflinePlayer offlinePlayer) throw
205205
private List<PlayerProfile> getMVPlayerData(
206206
@NotNull OfflinePlayer offlinePlayer, @NotNull Group group, @NotNull GameMode gameMode) {
207207
List<PlayerProfile> profiles = new ArrayList<>();
208-
profiles.add(profileDataSource.getPlayerData(org.mvplugins.multiverse.inventories.profile.ProfileKey
208+
profiles.add(profileDataSource.getPlayerDataNow(org.mvplugins.multiverse.inventories.profile.ProfileKey
209209
.create(ContainerType.GROUP, group.getName(), ProfileTypes.forGameMode(gameMode), offlinePlayer.getUniqueId())));
210210
for (var worldName : group.getWorlds()) {
211-
profiles.add(profileDataSource.getPlayerData(org.mvplugins.multiverse.inventories.profile.ProfileKey
211+
profiles.add(profileDataSource.getPlayerDataNow(org.mvplugins.multiverse.inventories.profile.ProfileKey
212212
.create(ContainerType.WORLD, worldName, ProfileTypes.forGameMode(gameMode), offlinePlayer.getUniqueId())));
213213
}
214214
return profiles;

src/main/java/org/mvplugins/multiverse/inventories/dataimport/worldinventories/WorldInventoriesImportHelper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ private Set<ProfileContainer> getWorldsWithoutGroups() {
144144
}
145145

146146
private void transferData(OfflinePlayer player, Group wiGroup, ProfileContainer profileContainer) {
147-
PlayerProfile playerProfile = profileContainer.getPlayerData(ProfileTypes.SURVIVAL, player);
147+
PlayerProfile playerProfile = profileContainer.getPlayerDataNow(ProfileTypes.SURVIVAL, player);
148148
WIPlayerInventory wiInventory = this.loadPlayerInventory(player, wiGroup);
149149
WIPlayerStats wiStats = this.loadPlayerStats(player, wiGroup);
150150
if (wiInventory != null) {

src/main/java/org/mvplugins/multiverse/inventories/event/ShareHandlingEvent.java

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,11 @@ public abstract class ShareHandlingEvent extends Event implements Cancellable {
1616
private boolean cancelled;
1717

1818
private final Player player;
19-
private final PersistingProfile alwaysWriteProfile;
2019
private final List<PersistingProfile> writeProfiles;
2120
private final List<PersistingProfile> readProfiles;
2221

2322
ShareHandlingEvent(Player player, AffectedProfiles affectedProfiles) {
2423
this.player = player;
25-
this.alwaysWriteProfile = affectedProfiles.getAlwaysWriteProfile();
2624
this.writeProfiles = affectedProfiles.getWriteProfiles();
2725
this.readProfiles = affectedProfiles.getReadProfiles();
2826
}
@@ -44,17 +42,7 @@ public void setCancelled(boolean cancel) {
4442
}
4543

4644
/**
47-
* Returns the profile that will always be saved to. By default, this is a profile for the world the player was in.
48-
*
49-
* @return The profile that will always be saved to when this event occurs.
50-
*/
51-
public PersistingProfile getAlwaysWriteProfile() {
52-
return alwaysWriteProfile;
53-
}
54-
55-
/**
56-
* @return The profiles for the world/groups the player is coming from that data will be saved to in addition to
57-
* the profile returned by {@link #getAlwaysWriteProfile()}.
45+
* @return The profiles for the world/groups the player is coming from that data will be saved to.
5846
*/
5947
public List<PersistingProfile> getWriteProfiles() {
6048
return this.writeProfiles;

src/main/java/org/mvplugins/multiverse/inventories/handleshare/AffectedProfiles.java

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,32 @@
55

66
import java.util.LinkedList;
77
import java.util.List;
8-
9-
import static org.mvplugins.multiverse.inventories.share.Sharables.enabled;
8+
import java.util.concurrent.CompletableFuture;
109

1110
public final class AffectedProfiles {
1211

13-
private PersistingProfile alwaysWriteProfile;
1412
private final List<PersistingProfile> writeProfiles = new LinkedList<>();
1513
private final List<PersistingProfile> readProfiles = new LinkedList<>();
1614

1715
AffectedProfiles() {
1816
}
1917

20-
void setAlwaysWriteProfile(PlayerProfile profile) {
21-
alwaysWriteProfile = new PersistingProfile(enabled(), profile);
22-
}
23-
2418
/**
2519
* @param profile The player profile that will need data saved to.
2620
* @param shares What from this group needs to be saved.
2721
*/
28-
void addWriteProfile(PlayerProfile profile, Shares shares) {
22+
void addWriteProfile(CompletableFuture<PlayerProfile> profile, Shares shares) {
2923
writeProfiles.add(new PersistingProfile(shares, profile));
3024
}
3125

3226
/**
3327
* @param profile The player profile that will need data loaded from.
3428
* @param shares What from this group needs to be loaded.
3529
*/
36-
void addReadProfile(PlayerProfile profile, Shares shares) {
30+
void addReadProfile(CompletableFuture<PlayerProfile> profile, Shares shares) {
3731
readProfiles.add(new PersistingProfile(shares, profile));
3832
}
3933

40-
public PersistingProfile getAlwaysWriteProfile() {
41-
return alwaysWriteProfile;
42-
}
43-
4434
public List<PersistingProfile> getWriteProfiles() {
4535
return writeProfiles;
4636
}

src/main/java/org/mvplugins/multiverse/inventories/handleshare/GameModeShareHandler.java

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.mvplugins.multiverse.inventories.profile.ProfileTypes;
1010
import org.mvplugins.multiverse.inventories.profile.container.ProfileContainer;
1111
import org.mvplugins.multiverse.inventories.share.Sharables;
12+
import org.mvplugins.multiverse.inventories.share.Shares;
1213
import org.mvplugins.multiverse.inventories.util.Perm;
1314
import org.bukkit.GameMode;
1415
import org.bukkit.entity.Player;
@@ -49,13 +50,19 @@ protected ShareHandlingEvent createEvent() {
4950

5051
@Override
5152
protected void prepareProfiles() {
52-
Logging.finer("=== " + player.getName() + " changing game mode from: " + fromType
53+
Logging.fine("=== " + player.getName() + " changing game mode from: " + fromType
5354
+ " to: " + toType + " for world: " + world + " ===");
5455

55-
affectedProfiles.setAlwaysWriteProfile(worldProfileContainerStore.getContainer(world).getPlayerData(fromType, player));
56-
5756
if (isPlayerAffectedByChange()) {
5857
addProfiles();
58+
} else if (inventoriesConfig.getAlwaysWriteWorldProfile()) {
59+
// Write to world profile to ensure data is saved incase bypass is removed
60+
affectedProfiles.addWriteProfile(
61+
worldProfileContainerStore.getContainer(world).getPlayerData(player),
62+
(worldGroups.isEmpty() && !inventoriesConfig.getUseOptionalsForUngroupedWorlds())
63+
? Sharables.standard()
64+
: Sharables.enabled()
65+
);
5966
}
6067
}
6168

@@ -73,22 +80,28 @@ private boolean isPlayerBypassingChange() {
7380
}
7481

7582
private void addProfiles() {
76-
if (hasWorldGroups()) {
77-
worldGroups.forEach(this::addProfilesForWorldGroup);
78-
} else {
79-
Logging.finer("No groups for world.");
80-
affectedProfiles.addReadProfile(worldProfileContainerStore.getContainer(world).getPlayerData(toType, player),
81-
inventoriesConfig.getUseOptionalsForUngroupedWorlds() ? Sharables.enabled() : Sharables.standardOf());
83+
Shares handledShares = Sharables.noneOf();
84+
worldGroups.forEach(worldGroup -> addProfilesForWorldGroup(handledShares,worldGroup));
85+
Shares unhandledShares = Sharables.enabledOf().setSharing(handledShares, false);
86+
if (!unhandledShares.isEmpty()) {
87+
affectedProfiles.addReadProfile(worldProfileContainerStore.getContainer(world).getPlayerData(fromType, player), unhandledShares);
8288
}
83-
}
8489

85-
private boolean hasWorldGroups() {
86-
return !worldGroups.isEmpty();
90+
if (inventoriesConfig.getAlwaysWriteWorldProfile()) {
91+
affectedProfiles.addWriteProfile(worldProfileContainerStore.getContainer(world).getPlayerData(toType, player),
92+
inventoriesConfig.getUseOptionalsForUngroupedWorlds() ? Sharables.enabled() : Sharables.standard());
93+
} else {
94+
if (!unhandledShares.isEmpty()) {
95+
affectedProfiles.addWriteProfile(worldProfileContainerStore.getContainer(world).getPlayerData(toType, player), unhandledShares);
96+
}
97+
}
8798
}
8899

89-
private void addProfilesForWorldGroup(WorldGroup worldGroup) {
100+
private void addProfilesForWorldGroup(Shares handledShares, WorldGroup worldGroup) {
90101
ProfileContainer container = worldGroup.getGroupProfileContainer();
91-
affectedProfiles.addWriteProfile(container.getPlayerData(fromType, player), Sharables.enabled());
92-
affectedProfiles.addReadProfile(container.getPlayerData(toType, player), Sharables.enabled());
102+
affectedProfiles.addWriteProfile(container.getPlayerData(fromType, player), worldGroup.getApplicableShares());
103+
affectedProfiles.addReadProfile(container.getPlayerData(toType, player), worldGroup.getApplicableShares());
104+
handledShares.addAll(worldGroup.getApplicableShares());
105+
handledShares.addAll(worldGroup.getDisabledShares());
93106
}
94107
}

0 commit comments

Comments
 (0)