-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Store serena toggle persistently on player
Previously the toggles done by serena would not persist through a server restart, as it was purely maintained in an in-memory queue cache. As this was rather inconvinient for the players, this commit now offers a new implementation for the player toggle register, based on the persistent data container api provided by spigot, which stores the toggle value of serena directly on the player nbt tags. New version: 1.0.4-SNAPSHOT
- Loading branch information
Showing
4 changed files
with
75 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/main/java/dev/lynxplay/serena/player/PersistentPlayerToggleRegister.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package dev.lynxplay.serena.player; | ||
|
||
import org.bukkit.NamespacedKey; | ||
import org.bukkit.entity.Player; | ||
import org.bukkit.persistence.PersistentDataHolder; | ||
import org.bukkit.persistence.PersistentDataType; | ||
|
||
import java.util.Optional; | ||
import java.util.UUID; | ||
import java.util.function.Function; | ||
|
||
/** | ||
* The persistent player toggle register uses the {@link org.bukkit.persistence.PersistentDataHolder} interface provided | ||
* by spigot to store the current toggle value for a player. | ||
*/ | ||
public class PersistentPlayerToggleRegister implements PlayerToggleRegistry { | ||
|
||
private final Function<UUID, Player> playerLookup; | ||
private final NamespacedKey toggleNamespace; | ||
|
||
public PersistentPlayerToggleRegister(final Function<UUID, Player> playerLookup, | ||
final NamespacedKey toggleNamespace) { | ||
this.playerLookup = playerLookup; | ||
this.toggleNamespace = toggleNamespace; | ||
} | ||
|
||
/** | ||
* Returns the current toggle value, being {@code true} if the toggle is enabled, else {@code false} | ||
* | ||
* @param uuid the uuid to check against | ||
* @return the toggle value | ||
*/ | ||
@Override | ||
public boolean getCurrentToggle(UUID uuid) { | ||
return Optional.of(uuid) | ||
.map(this.playerLookup) | ||
.map(PersistentDataHolder::getPersistentDataContainer) | ||
.map(c -> c.get(this.toggleNamespace, PersistentDataType.BYTE)) | ||
.map(b -> b == 1) | ||
.orElse(false); | ||
} | ||
|
||
/** | ||
* Sets the toggle value | ||
* | ||
* @param uuid the uuid to update | ||
* @param value the new value for the given uuid | ||
* @return the previous value | ||
*/ | ||
@Override | ||
public boolean setToggle(UUID uuid, boolean value) { | ||
return Optional.of(uuid) | ||
.map(this.playerLookup) | ||
.map(PersistentDataHolder::getPersistentDataContainer) | ||
.map(c -> { | ||
final boolean previousValue = Optional.ofNullable(c.get(this.toggleNamespace, PersistentDataType.BYTE)) | ||
.map(b -> b == 1) | ||
.orElse(false); | ||
|
||
c.set(this.toggleNamespace, PersistentDataType.BYTE, (byte) (value ? 1 : 0)); | ||
return previousValue; | ||
}) | ||
.orElse(false); | ||
} | ||
} |
42 changes: 0 additions & 42 deletions
42
src/main/java/dev/lynxplay/serena/player/QueuePlayerToggleRegistry.java
This file was deleted.
Oops, something went wrong.