44import org .bukkit .entity .Player ;
55import org .mvplugins .multiverse .inventories .MultiverseInventories ;
66import org .mvplugins .multiverse .inventories .config .InventoriesConfig ;
7+ import org .mvplugins .multiverse .inventories .profile .PlayerProfile ;
8+ import org .mvplugins .multiverse .inventories .profile .ProfileDataSource ;
79import org .mvplugins .multiverse .inventories .profile .container .ContainerType ;
810import org .mvplugins .multiverse .inventories .profile .container .ProfileContainerStoreProvider ;
911import org .mvplugins .multiverse .inventories .profile .group .WorldGroupManager ;
1012import 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}
0 commit comments