Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Custom permission handlers #105

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/main/java/net/minestom/server/command/CommandSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,31 @@
* <p>
* Main implementations are {@link Player} and {@link ConsoleSender}.
*/
public interface CommandSender extends PermissionHandler, Audience, Taggable, Identified {
public interface CommandSender extends Audience, Taggable, Identified {

/**
* Gets a permission handler of the sender.
*
* @return a permission handler of the sender
*/
@NotNull PermissionHandler getPermissionHandler();

/**
* Sets a permission handler for the sender.
*
* @param handler the permission handler
*/
void setPermissionHandler(@NotNull PermissionHandler handler);

/**
* Gets if this sender has the permission {@code permission}.
*
* @param permission the permission to check
* @return true if the sender has the permission, false otherwise
*/
default boolean hasPermission(@NotNull String permission) {
return getPermissionHandler().hasPermission(permission);
}

/**
* Sends a raw string message.
Expand Down
25 changes: 14 additions & 11 deletions src/main/java/net/minestom/server/command/ConsoleSender.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package net.minestom.server.command;

import java.util.UUID;

import net.kyori.adventure.audience.MessageType;
import net.kyori.adventure.identity.Identified;
import net.kyori.adventure.identity.Identity;
import net.kyori.adventure.pointer.Pointers;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.logger.slf4j.ComponentLogger;
import net.minestom.server.permission.Permission;
import net.minestom.server.permission.DefaultPermissionHandler;
import net.minestom.server.permission.PermissionHandler;
import net.minestom.server.tag.TagHandler;
import org.jetbrains.annotations.NotNull;

Expand All @@ -21,28 +19,33 @@
public class ConsoleSender implements CommandSender {
private static final ComponentLogger LOGGER = ComponentLogger.logger(ConsoleSender.class);

private final Set<Permission> permissions = new CopyOnWriteArraySet<>();
private final TagHandler tagHandler = TagHandler.newHandler();

private final Identity identity = Identity.nil();
private final Pointers pointers = Pointers.builder()
.withStatic(Identity.UUID, this.identity.uuid())
.build();

private PermissionHandler permissionHandler = new DefaultPermissionHandler();

@Override
public void sendMessage(@NotNull String message) {
LOGGER.info(message);
public @NotNull PermissionHandler getPermissionHandler() {
return permissionHandler;
}

@Override
public void sendMessage(@NotNull Identity source, @NotNull Component message, @NotNull MessageType type) {
public void setPermissionHandler(@NotNull PermissionHandler handler) {
this.permissionHandler = handler;
}

@Override
public void sendMessage(@NotNull String message) {
LOGGER.info(message);
}

@NotNull
@Override
public Set<Permission> getAllPermissions() {
return permissions;
public void sendMessage(@NotNull Identity source, @NotNull Component message, @NotNull MessageType type) {
LOGGER.info(message);
}

@Override
Expand Down
24 changes: 13 additions & 11 deletions src/main/java/net/minestom/server/command/ServerSender.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.identity.Identity;
import net.minestom.server.command.builder.CommandContext;
import net.minestom.server.permission.Permission;
import net.minestom.server.permission.DefaultPermissionHandler;
import net.minestom.server.permission.PermissionHandler;
import net.minestom.server.tag.TagHandler;
import org.jetbrains.annotations.NotNull;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

/**
* Sender used in {@link CommandManager#executeServerCommand(String)}.
* <p>
Expand All @@ -20,14 +17,9 @@
*/
public class ServerSender implements CommandSender {

private final Set<Permission> permissions = Collections.unmodifiableSet(new HashSet<>());
private final TagHandler tagHandler = TagHandler.newHandler();

@NotNull
@Override
public Set<Permission> getAllPermissions() {
return permissions;
}
private PermissionHandler permissionHandler = new DefaultPermissionHandler();

@Override
public @NotNull TagHandler tagHandler() {
Expand All @@ -38,4 +30,14 @@ public Set<Permission> getAllPermissions() {
public @NotNull Identity identity() {
return Identity.nil();
}

@Override
public @NotNull PermissionHandler getPermissionHandler() {
return permissionHandler;
}

@Override
public void setPermissionHandler(@NotNull PermissionHandler handler) {
this.permissionHandler = handler;
}
}
11 changes: 1 addition & 10 deletions src/main/java/net/minestom/server/entity/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,6 @@
import net.minestom.server.network.packet.server.LazyPacket;
import net.minestom.server.network.packet.server.ServerPacket;
import net.minestom.server.network.packet.server.play.*;
import net.minestom.server.permission.Permission;
import net.minestom.server.permission.PermissionHandler;
import net.minestom.server.potion.Potion;
import net.minestom.server.potion.PotionEffect;
import net.minestom.server.potion.TimedPotion;
Expand Down Expand Up @@ -84,7 +82,7 @@
* To create your own entity you probably want to extends {@link LivingEntity} or {@link EntityCreature} instead.
*/
public class Entity implements Viewable, Tickable, Schedulable, Snapshotable, EventHandler<EntityEvent>, Taggable,
PermissionHandler, HoverEventSource<ShowEntity>, Sound.Emitter, Shape {
HoverEventSource<ShowEntity>, Sound.Emitter, Shape {

private static final int VELOCITY_UPDATE_INTERVAL = 1;

Expand Down Expand Up @@ -156,7 +154,6 @@ public void referenceUpdate(@NotNull Point point, @Nullable EntityTracker tracke
private final TagHandler tagHandler = TagHandler.newHandler();
private final Scheduler scheduler = Scheduler.newScheduler();
private final EventNode<EntityEvent> eventNode;
private final Set<Permission> permissions = new CopyOnWriteArraySet<>();

protected UUID uuid;
private boolean isActive; // False if entity has only been instanced without being added somewhere
Expand Down Expand Up @@ -528,12 +525,6 @@ public synchronized void switchEntityType(@NotNull EntityType entityType) {
viewers.forEach(this::updateNewViewer);
}

@NotNull
@Override
public Set<Permission> getAllPermissions() {
return permissions;
}

/**
* Updates the entity, called every tick.
* <p>
Expand Down
17 changes: 14 additions & 3 deletions src/main/java/net/minestom/server/entity/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,13 @@
import net.minestom.server.adventure.audience.Audiences;
import net.minestom.server.attribute.Attribute;
import net.minestom.server.collision.BoundingBox;
import net.minestom.server.collision.CollisionUtils;
import net.minestom.server.collision.PhysicsResult;
import net.minestom.server.command.CommandSender;
import net.minestom.server.coordinate.Point;
import net.minestom.server.coordinate.Pos;
import net.minestom.server.coordinate.Vec;
import net.minestom.server.effects.Effects;
import net.minestom.server.entity.damage.DamageType;
import net.minestom.server.entity.fakeplayer.FakePlayer;
import net.minestom.server.entity.metadata.LivingEntityMeta;
import net.minestom.server.entity.metadata.PlayerMeta;
import net.minestom.server.entity.vehicle.PlayerVehicleInformation;
import net.minestom.server.event.EventDispatcher;
Expand Down Expand Up @@ -65,6 +62,8 @@
import net.minestom.server.network.player.GameProfile;
import net.minestom.server.network.player.PlayerConnection;
import net.minestom.server.network.player.PlayerSocketConnection;
import net.minestom.server.permission.DefaultPermissionHandler;
import net.minestom.server.permission.PermissionHandler;
import net.minestom.server.recipe.Recipe;
import net.minestom.server.recipe.RecipeManager;
import net.minestom.server.registry.Registry;
Expand Down Expand Up @@ -216,6 +215,8 @@ public class Player extends LivingEntity implements CommandSender, Localizable,
private Identity identity;
private final Pointers pointers;

private PermissionHandler permissionHandler = new DefaultPermissionHandler();

public Player(@NotNull UUID uuid, @NotNull String username, @NotNull PlayerConnection playerConnection) {
super(EntityType.PLAYER, uuid);
this.username = username;
Expand Down Expand Up @@ -2220,6 +2221,16 @@ public void setUuid(@NotNull UUID uuid) {
this.identity = Identity.identity(uuid);
}

@Override
public @NotNull PermissionHandler getPermissionHandler() {
return permissionHandler;
}

@Override
public void setPermissionHandler(@NotNull PermissionHandler handler) {
this.permissionHandler = handler;
}

@Override
public boolean isPlayer() {
return true;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package net.minestom.server.permission;

import org.jetbrains.annotations.NotNull;

/**
* A default permission handler. Used when no custom permission handler is set.
*/
public final class DefaultPermissionHandler implements PermissionHandler {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class should implement the changes suggested in PermissionHandler. The implementations should have the same behaviour as before.

@Override
public boolean hasPermission(@NotNull String permission) {
return false;
}
}
74 changes: 0 additions & 74 deletions src/main/java/net/minestom/server/permission/Permission.java

This file was deleted.

Loading
Loading