mirror of
https://github.com/SimplexDevelopment/FreedomNetworkSuite.git
synced 2025-07-03 14:16:40 +00:00
Migrates the entire package nomenclature to be more direct and straightforward. (#17)
Signed-off-by: Paul Reilly <pawereus@gmail.com>
This commit is contained in:
243
Datura/src/main/java/fns/datura/perms/FreedomUser.java
Normal file
243
Datura/src/main/java/fns/datura/perms/FreedomUser.java
Normal file
@ -0,0 +1,243 @@
|
||||
package fns.datura.perms;
|
||||
|
||||
import fns.datura.Datura;
|
||||
import fns.datura.user.SimpleUserData;
|
||||
import fns.patchwork.base.Patchwork;
|
||||
import fns.patchwork.security.Node;
|
||||
import fns.patchwork.user.User;
|
||||
import fns.patchwork.user.UserData;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import net.kyori.adventure.text.Component;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.permissions.Permission;
|
||||
import org.bukkit.permissions.PermissionAttachment;
|
||||
import org.bukkit.permissions.PermissionAttachmentInfo;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
/**
|
||||
* The superinterface User extends PermissionHolder, which is an extension of
|
||||
* {@link org.bukkit.permissions.Permissible}. This means that our permission data can be interchanged with other
|
||||
* permission plugins.
|
||||
*/
|
||||
public class FreedomUser implements User
|
||||
{
|
||||
private final UUID uuid;
|
||||
private final Set<Node> permissions;
|
||||
private final Map<Node, PermissionAttachment> bukkitAttachments = new HashMap<>();
|
||||
private final Component displayName;
|
||||
private static final String NOT_ONLINE = "Player is not online";
|
||||
private final UserData userData;
|
||||
|
||||
public FreedomUser(final Player player)
|
||||
{
|
||||
this.uuid = player.getUniqueId();
|
||||
this.permissions = new HashSet<>();
|
||||
this.displayName = player.displayName();
|
||||
|
||||
final Datura datura = Patchwork.getInstance()
|
||||
.getRegistrations()
|
||||
.getModuleRegistry()
|
||||
.getProvider(Datura.class)
|
||||
.getModule();
|
||||
|
||||
UserData data = SimpleUserData.fromSQL(datura.getSQL(), uuid.toString());
|
||||
|
||||
if (data == null)
|
||||
{
|
||||
data = new SimpleUserData(player);
|
||||
}
|
||||
|
||||
this.userData = data;
|
||||
|
||||
Patchwork.getInstance()
|
||||
.getRegistrations()
|
||||
.getUserRegistry()
|
||||
.registerUserData(this, userData);
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserData getUserData()
|
||||
{
|
||||
return userData;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Component getDisplayName()
|
||||
{
|
||||
return displayName;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isOnline()
|
||||
{
|
||||
return Bukkit.getPlayer(uuid) != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UUID getUniqueId()
|
||||
{
|
||||
return uuid;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Node> permissions()
|
||||
{
|
||||
return permissions;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean addPermission(final Node node)
|
||||
{
|
||||
final boolean value = !node.isTemporary() || node.isExpired();
|
||||
final PermissionAttachment attachment = addAttachment(Patchwork.getInstance(), node.key(), value);
|
||||
bukkitAttachments.put(node, attachment);
|
||||
return permissions().add(node);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean removePermission(final Node node)
|
||||
{
|
||||
removeAttachment(bukkitAttachments.get(node));
|
||||
bukkitAttachments.remove(node);
|
||||
return permissions.remove(node);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPermissionSet(@NotNull final String name)
|
||||
{
|
||||
final Player player = Bukkit.getPlayer(uuid);
|
||||
return player != null && player.isPermissionSet(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isPermissionSet(@NotNull final Permission perm)
|
||||
{
|
||||
final Player player = Bukkit.getPlayer(uuid);
|
||||
return player != null && player.isPermissionSet(perm);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(@NotNull final String name)
|
||||
{
|
||||
final Player player = Bukkit.getPlayer(uuid);
|
||||
return player != null && player.hasPermission(name);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasPermission(@NotNull final Permission perm)
|
||||
{
|
||||
final Player player = Bukkit.getPlayer(uuid);
|
||||
return player != null && player.hasPermission(perm);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull PermissionAttachment addAttachment(@NotNull final Plugin plugin, @NotNull final String name,
|
||||
final boolean value)
|
||||
{
|
||||
final Player player = Bukkit.getPlayer(uuid);
|
||||
if (player != null)
|
||||
{
|
||||
return player.addAttachment(plugin, name, value);
|
||||
}
|
||||
|
||||
throw new IllegalStateException(NOT_ONLINE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull PermissionAttachment addAttachment(@NotNull final Plugin plugin)
|
||||
{
|
||||
final Player player = Bukkit.getPlayer(uuid);
|
||||
if (player != null)
|
||||
{
|
||||
return player.addAttachment(plugin);
|
||||
}
|
||||
|
||||
throw new IllegalStateException(NOT_ONLINE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable PermissionAttachment addAttachment(@NotNull final Plugin plugin, @NotNull final String name,
|
||||
final boolean value, final int ticks)
|
||||
{
|
||||
final Player player = Bukkit.getPlayer(uuid);
|
||||
if (player != null)
|
||||
{
|
||||
return player.addAttachment(plugin, name, value, ticks);
|
||||
}
|
||||
|
||||
throw new IllegalStateException(NOT_ONLINE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable PermissionAttachment addAttachment(@NotNull final Plugin plugin, final int ticks)
|
||||
{
|
||||
final Player player = Bukkit.getPlayer(uuid);
|
||||
if (player != null)
|
||||
{
|
||||
return player.addAttachment(plugin, ticks);
|
||||
}
|
||||
|
||||
throw new IllegalStateException(NOT_ONLINE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeAttachment(@NotNull final PermissionAttachment attachment)
|
||||
{
|
||||
final Player player = Bukkit.getPlayer(uuid);
|
||||
if (player != null)
|
||||
{
|
||||
player.removeAttachment(attachment);
|
||||
}
|
||||
|
||||
throw new IllegalStateException(NOT_ONLINE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void recalculatePermissions()
|
||||
{
|
||||
final Player player = Bukkit.getPlayer(uuid);
|
||||
if (player != null)
|
||||
{
|
||||
player.recalculatePermissions();
|
||||
}
|
||||
|
||||
throw new IllegalStateException(NOT_ONLINE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull Set<PermissionAttachmentInfo> getEffectivePermissions()
|
||||
{
|
||||
final Player player = Bukkit.getPlayer(uuid);
|
||||
if (player != null)
|
||||
{
|
||||
return player.getEffectivePermissions();
|
||||
}
|
||||
|
||||
throw new IllegalStateException(NOT_ONLINE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isOp()
|
||||
{
|
||||
return permissions().contains(DefaultNodes.OP);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setOp(final boolean value)
|
||||
{
|
||||
if (value)
|
||||
{
|
||||
permissions().add(DefaultNodes.OP);
|
||||
} else
|
||||
{
|
||||
permissions().remove(DefaultNodes.OP);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user