Skip to content

Commit

Permalink
Store serena toggle persistently on player
Browse files Browse the repository at this point in the history
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
lynxplay committed Jun 22, 2020
1 parent f426ca5 commit aec5e57
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 50 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>dev.lynxplay</groupId>
<artifactId>serena</artifactId>
<version>1.0.3-SNAPSHOT</version>
<version>1.0.4-SNAPSHOT</version>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
Expand Down
16 changes: 9 additions & 7 deletions src/main/java/dev/lynxplay/serena/Serena.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@

import dev.lynxplay.serena.commands.SerenaReloadCommand;
import dev.lynxplay.serena.commands.SerenaToggleCommand;
import dev.lynxplay.serena.configuration.ConfigurationFactory;
import dev.lynxplay.serena.configuration.language.ImmutableLanguageConfigurationFactory;
import dev.lynxplay.serena.configuration.language.LanguageConfiguration;
import dev.lynxplay.serena.configuration.properties.ImmutablePropertyConfigurationFactory;
import dev.lynxplay.serena.configuration.properties.PropertyConfiguration;
import dev.lynxplay.serena.cooldown.CooldownContainer;
import dev.lynxplay.serena.cooldown.HashedCooldownContainer;
import dev.lynxplay.serena.listener.PlayerConnectionListener;
import dev.lynxplay.serena.listener.PlayerPickupListener;
import dev.lynxplay.serena.listener.PlayerThrowListener;
import dev.lynxplay.serena.permission.PlayerPermissionChecker;
import dev.lynxplay.serena.permission.SimplePlayerPermissionChecker;
import dev.lynxplay.serena.player.PersistentPlayerToggleRegister;
import dev.lynxplay.serena.player.PlayerToggleRegistry;
import dev.lynxplay.serena.player.QueuePlayerToggleRegistry;
import dev.lynxplay.serena.configuration.ConfigurationFactory;
import dev.lynxplay.serena.configuration.language.ImmutableLanguageConfigurationFactory;
import dev.lynxplay.serena.configuration.language.LanguageConfiguration;
import dev.lynxplay.serena.configuration.properties.ImmutablePropertyConfigurationFactory;
import dev.lynxplay.serena.configuration.properties.PropertyConfiguration;
import org.bukkit.Bukkit;
import org.bukkit.NamespacedKey;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
Expand Down Expand Up @@ -51,7 +53,7 @@ public void onEnable() {

this.cooldownContainer = new HashedCooldownContainer();
this.playerPermissionChecker = new SimplePlayerPermissionChecker();
this.playerToggleRegistry = new QueuePlayerToggleRegistry();
this.playerToggleRegistry = new PersistentPlayerToggleRegister(Bukkit::getPlayer, new NamespacedKey(this, "ToggleValue"));

this.fixedScheduler = (r, l) -> getServer().getScheduler().runTaskLater(this, r, l);

Expand Down
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);
}
}

This file was deleted.

0 comments on commit aec5e57

Please sign in to comment.