Skip to content

Commit da9a130

Browse files
authored
Merge branch 'MV5' into ben/mv5/refactor-profile-source
2 parents 5a893e6 + 6d405b6 commit da9a130

File tree

4 files changed

+48
-14
lines changed

4 files changed

+48
-14
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ void playerQuit(final PlayerQuitEvent event) {
208208
globalProfile.setLoadOnLogin(true);
209209
}
210210
profileDataSource.updateGlobalProfile(globalProfile);
211-
SingleShareWriter.of(this.inventories, player, Sharables.LAST_LOCATION).write(player.getLocation());
211+
SingleShareWriter.of(this.inventories, player, Sharables.LAST_LOCATION).write(player.getLocation().clone());
212212
}
213213

214214
private void verifyCorrectWorld(Player player, String world, GlobalProfile globalProfile) {
@@ -235,7 +235,7 @@ void playerGameModeChange(PlayerGameModeChangeEvent event) {
235235
return;
236236
}
237237
Player player = event.getPlayer();
238-
SingleShareWriter.of(this.inventories, player, Sharables.LAST_LOCATION).write(player.getLocation());
238+
SingleShareWriter.of(this.inventories, player, Sharables.LAST_LOCATION).write(player.getLocation().clone());
239239
new GameModeShareHandler(this.inventories, player,
240240
player.getGameMode(), event.getNewGameMode()).handleSharing();
241241
}
@@ -281,7 +281,7 @@ void playerTeleport(PlayerTeleportEvent event) {
281281
}
282282

283283
Player player = event.getPlayer();
284-
SingleShareWriter.of(this.inventories, player, Sharables.LAST_LOCATION).write(event.getFrom());
284+
SingleShareWriter.of(this.inventories, player, Sharables.LAST_LOCATION).write(event.getFrom().clone());
285285

286286
// Possibly prevents item duping exploit
287287
player.closeInventory();

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

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,15 @@
44
import org.bukkit.entity.Player;
55
import org.mvplugins.multiverse.inventories.MultiverseInventories;
66
import org.mvplugins.multiverse.inventories.config.InventoriesConfig;
7+
import org.mvplugins.multiverse.inventories.profile.PlayerProfile;
8+
import org.mvplugins.multiverse.inventories.profile.ProfileDataSource;
79
import org.mvplugins.multiverse.inventories.profile.container.ContainerType;
810
import org.mvplugins.multiverse.inventories.profile.container.ProfileContainerStoreProvider;
911
import org.mvplugins.multiverse.inventories.profile.group.WorldGroupManager;
1012
import org.mvplugins.multiverse.inventories.share.Sharable;
1113

14+
import java.util.Objects;
15+
1216
/**
1317
* Write a single share to the relevant world and group profiles.
1418
*
@@ -23,14 +27,20 @@ public static <T> SingleShareWriter<T> of(MultiverseInventories inventories, Pla
2327
private final MultiverseInventories inventories;
2428
private final Player player;
2529
private final Sharable<T> sharable;
30+
private final ProfileDataSource profileDataSource;
2631

2732
private SingleShareWriter(MultiverseInventories inventories, Player player, Sharable<T> sharable) {
2833
this.inventories = inventories;
2934
this.player = player;
3035
this.sharable = sharable;
36+
this.profileDataSource = inventories.getServiceLocator().getService(ProfileDataSource.class);
3137
}
3238

3339
public void write(T value) {
40+
write(value, false);
41+
}
42+
43+
public void write(T value, boolean save) {
3444
if (sharable.isOptional() &&
3545
!inventories.getServiceLocator().getService(InventoriesConfig.class).getOptionalShares().contains(sharable)) {
3646
Logging.finer("Skipping write for optional share: " + sharable);
@@ -39,15 +49,31 @@ public void write(T value) {
3949
Logging.finer("Writing single share: " + sharable.getNames()[0]);
4050
String worldName = this.player.getWorld().getName();
4151
var profileContainerStoreProvider = this.inventories.getServiceLocator().getService(ProfileContainerStoreProvider.class);
42-
profileContainerStoreProvider.getStore(ContainerType.WORLD)
43-
.getContainer(worldName)
44-
.getPlayerData(this.player)
45-
.set(this.sharable, value);
52+
writeNewValueToProfile(
53+
profileContainerStoreProvider.getStore(ContainerType.WORLD)
54+
.getContainer(worldName)
55+
.getPlayerData(this.player),
56+
value,
57+
save
58+
);
4659

4760
this.inventories.getServiceLocator().getService(WorldGroupManager.class)
4861
.getGroupsForWorld(worldName)
49-
.forEach(worldGroup ->
50-
worldGroup.getGroupProfileContainer().getPlayerData(this.player)
51-
.set(this.sharable, value));
62+
.forEach(worldGroup -> writeNewValueToProfile(
63+
worldGroup.getGroupProfileContainer().getPlayerData(this.player),
64+
value,
65+
save
66+
));
67+
}
68+
69+
private void writeNewValueToProfile(PlayerProfile profile, T value, boolean save) {
70+
if (Objects.equals(profile.get(sharable), value)) {
71+
return;
72+
}
73+
Logging.finest("Writing %s value: %s for profile %s", sharable, value, profile);
74+
profile.set(sharable, value);
75+
if (save) {
76+
profileDataSource.updatePlayerData(profile);
77+
}
5278
}
5379
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ public SpawnChangeListener(MultiverseInventories inventories) {
3232
@EventHandler(priority = EventPriority.MONITOR)
3333
void onSpawnChange(PlayerSpawnChangeEvent event) {
3434
Player player = event.getPlayer();
35-
36-
Logging.fine("Respawn cause: %s", event.getCause());
37-
3835
if (event.getCause() == Cause.BED) {
3936
updatePlayerSpawn(player, findBedFromRespawnLocation(event.getNewSpawn()));
4037
return;
@@ -47,6 +44,7 @@ void onSpawnChange(PlayerSpawnChangeEvent event) {
4744
}
4845

4946
private void updatePlayerSpawn(Player player, Location location) {
50-
SingleShareWriter.of(this.inventories, player, Sharables.BED_SPAWN).write(location);
47+
SingleShareWriter.of(this.inventories, player, Sharables.BED_SPAWN)
48+
.write(location == null ? null : location.clone(), true);
5149
}
5250
}

src/main/java/org/mvplugins/multiverse/inventories/profile/PlayerProfile.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,14 @@ public PlayerProfile clone() {
9393
public Map<Sharable, Object> getData() {
9494
return data;
9595
}
96+
97+
@Override
98+
public String toString() {
99+
return "PlayerProfile{" +
100+
"player=" + player.getName() +
101+
", containerType=" + containerType +
102+
", containerName='" + containerName + '\'' +
103+
", profileType=" + profileType +
104+
'}';
105+
}
96106
}

0 commit comments

Comments
 (0)