Skip to content

Commit

Permalink
Permissions update: removed over-abstracted Node class; switch from V…
Browse files Browse the repository at this point in the history
…ault to LuckPerms (for now)
  • Loading branch information
bermudalocket committed Oct 27, 2018
1 parent e2ec635 commit 70bd2a5
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 261 deletions.
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@
<type>jar</type>
</dependency>
<dependency>
<groupId>net.milkbowl.vault</groupId>
<artifactId>VaultAPI</artifactId>
<version>1.7</version>
<groupId>me.lucko.luckperms</groupId>
<artifactId>luckperms-api</artifactId>
<version>4.3</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
4 changes: 3 additions & 1 deletion src/nu/nerd/modmode/AbstractPlayerCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.UUID;
import java.util.stream.Collectors;

// ------------------------------------------------------------------------
/**
Expand Down Expand Up @@ -70,7 +72,7 @@ synchronized boolean contains(Player player) {
* @param config the configuration.
*/
synchronized void save(FileConfiguration config) {
config.set(_configKey, CACHE.toArray());
config.set(_configKey, new ArrayList<>(CACHE.stream().map(UUID::toString).collect(Collectors.toSet())));
}

// ------------------------------------------------------------------------
Expand Down
71 changes: 31 additions & 40 deletions src/nu/nerd/modmode/Configuration.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.function.Predicate;
import java.util.stream.Collectors;

// ------------------------------------------------------------------------
Expand All @@ -28,17 +27,6 @@ class Configuration {
*/
private FileConfiguration _config;

/**
* A predicate which tests if a given node is configured to be persistent
* across ModMode toggles.
*/
public static final Predicate<Node> IS_PERSISTENT_NODE = new Predicate<Node>() {
@Override
public boolean test(Node node) {
return PERSISTENT_NODES.contains(node);
}
};

// ------------------------------------------------------------------------
/**
* Constructor.
Expand All @@ -49,29 +37,37 @@ public boolean test(Node node) {

// ------------------------------------------------------------------------
/**
* Returns the given player's serialized nodes. If none exist, an empty
* Returns true if the given groups is configured to be persistent.
*
* @param group the group.
* @return true if the given groups is configured to be persistent.
*/
static boolean isPersistentGroup(String group) {
return PERSISTENT_GROUPS.contains(group);
}

// ------------------------------------------------------------------------
/**
* Returns the given player's serialized groups. If none exist, an empty
* List will be returned.
*
* @param player the player.
* @return the player's serialized nodes; if none, an empty list.
* @return the player's serialized groups; if none, an empty list.
*/
static List<String> getSerializedNodes(Player player) {
static List<String> getSerializedGroups(Player player) {
String playerUuid = player.getUniqueId().toString();
return new ArrayList<>(SERIALIZED_NODES.getStringList(playerUuid));
return new ArrayList<>(SERIALIZED_GROUPS.getStringList(playerUuid));
}

// ------------------------------------------------------------------------
/**
* Serializes the given nodes for the given player.
* Serializes the given groups for the given player.
*
* @param player the player.
* @param nodes the nodes to serialize.
* @param groups the groups to serialize.
*/
static void serializeNodes(Player player, Collection<Node> nodes) {
List<String> serializedNodes = nodes.stream()
.map(Node::serialize)
.collect(Collectors.toList());
SERIALIZED_NODES.set(player.getUniqueId().toString(), serializedNodes);
static void serializeGroups(Player player, Collection<String> groups) {
SERIALIZED_GROUPS.set(player.getUniqueId().toString(), new ArrayList<>(groups));
}

// ------------------------------------------------------------------------
Expand All @@ -81,8 +77,8 @@ static void serializeNodes(Player player, Collection<Node> nodes) {
*
* @param player the player.
*/
static void sanitizeSerializedNodes(Player player) {
SERIALIZED_NODES.set(player.getUniqueId().toString(), null);
static void sanitizeSerializedGroups(Player player) {
SERIALIZED_GROUPS.set(player.getUniqueId().toString(), null);
}

// ------------------------------------------------------------------------
Expand Down Expand Up @@ -135,22 +131,19 @@ synchronized void reload() {
// update collisions for players in ModMode
NerdBoardHook.setAllowCollisions(_config.getBoolean("allow.collisions", true));

// load persistent nodes
List<String> persistentNodes = _config.getStringList("permissions.persistent-nodes");
PERSISTENT_NODES = persistentNodes.stream()
.map(Node::deserialize)
.collect(Collectors.toSet());
// load persistent groups
PERSISTENT_GROUPS = new HashSet<>(_config.getStringList("permissions.persistent-groups"));

// load permission worlds
PERMISSION_WORLDS = _config.getStringList("permissions.worlds")
.stream()
.map(Bukkit::getWorld)
.collect(Collectors.toCollection(HashSet::new));

// store reference to the serialized nodes configuration section
SERIALIZED_NODES = _config.getConfigurationSection("serialized-nodes");
if (SERIALIZED_NODES == null) {
SERIALIZED_NODES = _config.createSection("serialized-nodes");
// store reference to the serialized groups configuration section
SERIALIZED_GROUPS = _config.getConfigurationSection("serialized-groups");
if (SERIALIZED_GROUPS == null) {
SERIALIZED_GROUPS = _config.createSection("serialized-groups");
}

MODERATOR_GROUP = _config.getString("permissions.moderator-group", "moderators");
Expand All @@ -173,9 +166,7 @@ synchronized void save() {
ModMode.getModModeCache().save(_config);
_config.set("allow.flight", allowFlight);
_config.set("allow.collisions", NerdBoardHook.allowsCollisions());
_config.set("permissions.persistent-nodes", PERSISTENT_NODES.stream()
.map(Node::serialize)
.collect(Collectors.toList()));
_config.set("permissions.persistent-groups", new ArrayList<>(PERSISTENT_GROUPS));
_config.set("permissions.worlds", PERMISSION_WORLDS.stream()
.map(World::getName)
.collect(Collectors.toList()));
Expand Down Expand Up @@ -223,9 +214,9 @@ synchronized void save() {
String MODMODE_GROUP;

/**
* A set of nodes which should be retained when entering ModMode.
* A set of groups which should be retained when entering ModMode.
*/
private static Set<Node> PERSISTENT_NODES = new HashSet<>();
private static HashSet<String> PERSISTENT_GROUPS = new HashSet<>();

/**
* A set of worlds with relevant permissions, i.e. when a player enters
Expand All @@ -243,7 +234,7 @@ synchronized void save() {
* of which all values (groups) will be re-applied after said player exits
* ModMode.
*/
private static ConfigurationSection SERIALIZED_NODES;
private static ConfigurationSection SERIALIZED_GROUPS;

/**
* Commands executed immediately before ModMode is activated.
Expand Down
58 changes: 15 additions & 43 deletions src/nu/nerd/modmode/ModMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.potion.PotionEffect;
import org.kitteh.vanish.VanishPerms;
Expand All @@ -25,7 +24,6 @@
import java.io.File;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;

// ------------------------------------------------------------------------
/**
Expand All @@ -43,11 +41,6 @@ public class ModMode extends JavaPlugin {
*/
private static Permissions PERMISSIONS;

/**
* The global ModMode node, built when configuration is loaded.
*/
private static Node MOD_MODE_NODE;

/**
* A cache of players (UUIDs) currently in ModMode.
*/
Expand Down Expand Up @@ -81,12 +74,6 @@ public void onEnable() {
return;
}

if (getServer().getPluginManager().isPluginEnabled("LogBlock")) {
new LogBlockListener();
} else {
log("LogBlock is not loaded. Integration disabled.");
}

vanish = (VanishPlugin) getServer().getPluginManager().getPlugin("VanishNoPacket");
if (vanish == null) {
log("VanishNoPacket required. Download it here http://dev.bukkit.org/server-mods/vanish/");
Expand All @@ -103,15 +90,10 @@ public void onEnable() {
vanishCommand.setPermission(Permissions.VANISH);
}

Plugin logBlock = getServer().getPluginManager().getPlugin("LogBlock");
if (logBlock != null && !logBlock.isEnabled()) {
getPluginLoader().enablePlugin(logBlock);
}

if (getServer().getPluginManager().isPluginEnabled("LogBlock")) {
new LogBlockListener();
} else {
log("LogBlock is not loaded. Integration disabled.");
log("LogBlock missing or unloaded. Integration disabled.");
}

CONFIG = new Configuration();
Expand All @@ -121,10 +103,6 @@ public void onEnable() {

// instantiate permissions handler
PERMISSIONS = new Permissions();

// create global ModMode node
MOD_MODE_NODE = new Node(CONFIG.MODMODE_GROUP, null);

}

// ------------------------------------------------------------------------
Expand Down Expand Up @@ -425,16 +403,11 @@ private void toggleModMode(final Player player, boolean enabled) {

if (!enabled) {
// remove ModMode node
MOD_MODE_NODE.remove(player);

// find the player's serialized nodes
List<String> loadedNodes = Configuration.getSerializedNodes(player);
PERMISSIONS.removeGroup(player, CONFIG.MODMODE_GROUP);

// deserialize nodes and re-add
loadedNodes.stream()
.map(Node::deserialize)
.filter(Objects::nonNull)
.forEach(node -> node.apply(player));
// re-add the player's serialized groups
List<String> serializedGroups = Configuration.getSerializedGroups(player);
serializedGroups.forEach(group -> PERMISSIONS.addGroup(player, group));

// When leaving ModMode, Admins return to their persistent vanish
// state; Moderators become visible
Expand All @@ -452,21 +425,20 @@ private void toggleModMode(final Player player, boolean enabled) {

player.sendMessage(ChatColor.RED + "You are no longer in ModMode!");
} else {
// Clean up old mappings
Configuration.sanitizeSerializedNodes(player);
// clean up old mappings
Configuration.sanitizeSerializedGroups(player);

// get player's current permission nodes
HashSet<Node> nodes = PERMISSIONS.getNodes(player, Configuration.PERMISSION_WORLDS);
// get & serialize player's current groups
HashSet<String> nodes = PERMISSIONS.getGroups(player);
Configuration.serializeGroups(player, nodes);

// remove all nodes that are not configured to be persistent
// remove all groups that are not configured to be persistent
nodes.stream()
.filter(Configuration.IS_PERSISTENT_NODE)
.forEach(node -> node.remove(player));

Configuration.serializeNodes(player, nodes);
.filter(group -> !Configuration.isPersistentGroup(group))
.forEach(group -> PERMISSIONS.removeGroup(player, group));

// apply the ModMode node
MOD_MODE_NODE.apply(player);
PERMISSIONS.addGroup(player, CONFIG.MODMODE_GROUP);

// add to ModMode cache
MODMODE_CACHE.add(player);
Expand Down Expand Up @@ -654,6 +626,6 @@ static void log(String msg) {
/**
* This plugin's prefix as a string; for logging.
*/
private static final String PREFIX = ChatColor.WHITE + "[" + ChatColor.GREEN + "ModMode" + ChatColor.WHITE + "] ";
private static final String PREFIX = ChatColor.DARK_GRAY + "[" + ChatColor.GREEN + "ModMode" + ChatColor.DARK_GRAY + "] ";

} // ModMode
2 changes: 1 addition & 1 deletion src/nu/nerd/modmode/NerdBoardHook.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ static boolean allowsCollisions() {
private static Team configureTeam(String name, ChatColor color, boolean collisions) {
Team team = getOrCreateTeam(name);
if (color != null) {
team.setPrefix(color + "");
team.setColor(color);
}
team.setOption(Team.Option.COLLISION_RULE, boolToStatus(collisions));
return team;
Expand Down
Loading

0 comments on commit 70bd2a5

Please sign in to comment.