Skip to content
Merged
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
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.3.0] - 2025-08-05
### Added
- Fine-grained selector permission
- Selector weights
- Selector limits

## [0.2.11] - 2025-07-10
### Fixed
- Gamemode switcher in 1.21.6+
Expand Down Expand Up @@ -53,6 +59,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
- Using wrong fallback command node requirement

### Changed
- `minecraft.command.<command>.*` are granted by default

## [0.2.1] - 2023-07-01
### Fixed
- Checking permissions to early
Expand Down Expand Up @@ -97,4 +106,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Use API for offline permissions

## [0.1.0] - 2022-10-07
Init
Init
220 changes: 196 additions & 24 deletions README.md

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ dependencies {

publishMods {
file.set(tasks.remapJar.get().archiveFile)
type.set(STABLE)
type.set(BETA)
changelog.set(fetchChangelog())

displayName = "VanillaPermissions ${version.get()}"
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ modrinth_minecraft_versions=[VERSIONED]
java_version=[VERSIONED]
loader_version=0.16.14
# Mod Properties
mod_version=0.2.11
mod_version=0.3.0
maven_group=me.drex
archives_base_name=vanilla-permissions
# Dependencies
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package me.drex.vanillapermissions.mixin.selector.argument;

import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.arguments.EntityArgument;
import net.minecraft.world.entity.Entity;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.Collection;

import static me.drex.vanillapermissions.util.ArgumentPermission.validate;

import static java.util.Collections.singleton;

@Mixin(EntityArgument.class)
public abstract class EntityArgumentMixin {

@Inject(
method = {
"getEntity",
"getPlayer",
},
at = @At("RETURN")
)
private static void addEntityPermission(CommandContext<CommandSourceStack> commandContext, String string, CallbackInfoReturnable<Entity> cir) throws CommandSyntaxException {
validate(commandContext, string, singleton(cir.getReturnValue()));
}

@Inject(
method = {
"getEntities",
"getOptionalEntities",
"getPlayers",
"getOptionalPlayers",
},
at = @At("RETURN")
)
private static void addEntitiesPermission(CommandContext<CommandSourceStack> commandContext, String string, CallbackInfoReturnable<Collection<Entity>> cir) throws CommandSyntaxException {
validate(commandContext, string, cir.getReturnValue());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package me.drex.vanillapermissions.mixin.selector.argument;

import com.mojang.authlib.GameProfile;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.arguments.GameProfileArgument;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.Collection;

import static me.drex.vanillapermissions.util.ArgumentPermission.validate;

@Mixin(GameProfileArgument.class)
public abstract class GameProfileArgumentMixin {

@Inject(
method = "getGameProfiles",
at = @At("RETURN")
)
private static void addGameProfilesPermission(CommandContext<CommandSourceStack> commandContext, String string, CallbackInfoReturnable<Collection<GameProfile>> cir) throws CommandSyntaxException {
validate(commandContext, string, cir.getReturnValue());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package me.drex.vanillapermissions.mixin.selector.argument;

import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.arguments.ScoreHolderArgument;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import java.util.Collection;
import java.util.function.Supplier;

import static me.drex.vanillapermissions.util.ArgumentPermission.validate;

@Mixin(ScoreHolderArgument.class)
public abstract class ScoreHolderArgumentMixin {

@Inject(
method = "getNames(Lcom/mojang/brigadier/context/CommandContext;Ljava/lang/String;Ljava/util/function/Supplier;)Ljava/util/Collection;",
at = @At("RETURN")
)
private static void addScoreHoldersPermission(CommandContext<CommandSourceStack> commandContext, String string, Supplier<Collection<?>> supplier, CallbackInfoReturnable<Collection<?>> cir) throws CommandSyntaxException {
validate(commandContext, string, cir.getReturnValue());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package me.drex.vanillapermissions.mixin.selector.argument;
//? if >= 1.21.6 {
import com.llamalad7.mixinextras.sugar.Local;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.arguments.WaypointArgument;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.waypoints.WaypointTransmitter;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable;

import static me.drex.vanillapermissions.util.ArgumentPermission.validate;

import static java.util.Collections.singleton;

@Mixin(WaypointArgument.class)
public abstract class WaypointArgumentMixin {

@Inject(
method = "getWaypoint",
at = @At("RETURN")
)
private static void addWaypointPermission(CommandContext<CommandSourceStack> commandContext, String string, CallbackInfoReturnable<WaypointTransmitter> cir, @Local Entity entity) throws CommandSyntaxException {
validate(commandContext, string, singleton(entity));
}
}
//?} else {

/*import net.minecraft.server.MinecraftServer;
import org.spongepowered.asm.mixin.Mixin;

@Mixin(MinecraftServer.class)
public abstract class WaypointArgumentMixin {

}
*///?}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package me.drex.vanillapermissions.util;

import com.google.common.collect.Iterables;
import com.mojang.authlib.GameProfile;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.tree.RootCommandNode;
import me.lucko.fabric.api.permissions.v0.Options;
import me.lucko.fabric.api.permissions.v0.Permissions;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.player.Player;

import java.util.Collection;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;

import static me.drex.vanillapermissions.VanillaPermissionsMod.LOGGER;
import static me.drex.vanillapermissions.util.Permission.*;
import static net.minecraft.commands.arguments.EntityArgument.ERROR_SELECTORS_NOT_ALLOWED;

public class ArgumentPermission {

public static void validate(CommandContext<CommandSourceStack> context, String selector, Collection<?> selected) throws CommandSyntaxException {
var source = context.getSource();
if (!source.isPlayer()) return;

String[] parts;
if (context.getRootNode() instanceof RootCommandNode) {
parts = context.getNodes().stream().map(node -> node.getNode().getName()).toArray(String[]::new);
} else {
parts = source.getServer().getCommands().getDispatcher().getPath(Iterables.getLast(context.getNodes()).getNode()).toArray(String[]::new);
}
var name = Permission.build(parts[0], selector, Permission.build(1, parts.length, parts));

var limit = Options.get(source, SELECTOR_LIMIT.formatted(name), Integer::parseInt);
if (limit.isPresent() && limit.get() < selected.size()) throw ERROR_SELECTORS_NOT_ALLOWED.create();

var entity = Permissions.check(source, SELECTOR_ENTITY.formatted(name), true);
var player = Permissions.check(source, SELECTOR_PLAYER.formatted(name), true);
var self = Permissions.check(source, SELECTOR_SELF.formatted(name), true);

var weight = SELECTOR_WEIGHT.formatted(name);
var sourceWeight = Options.get(source, weight, Integer::parseInt);
var sourceWeightPresent = sourceWeight.isPresent();
if (entity && player && self && !sourceWeightPresent) return;
var sourceWeightValue = sourceWeight.orElse(0);

var sourcePlayer = source.getPlayer().getGameProfile();
try {
CompletableFuture.allOf(selected.stream().mapMulti((object, consumer) -> {
var selectedEntity = object;
if (selectedEntity instanceof Player selectedPlayer) {
selectedEntity = selectedPlayer.getGameProfile();
}

if (selectedEntity instanceof Entity) {
if (!entity) throwSelectorError();
} else if (selectedEntity instanceof GameProfile selectedPlayer) {
if (selectedPlayer.equals(sourcePlayer)) {
if (!self) throwSelectorError();
} else {
if (!player) throwSelectorError();
if (!sourceWeightPresent) return;

//? if >= 1.21.6 {
consumer.accept(Options.get(selectedPlayer, weight, Integer::parseInt).thenAcceptAsync(selectedWeight -> {
if (selectedWeight.isPresent() && selectedWeight.get() > sourceWeightValue) {
throw new CompletionException(ERROR_SELECTORS_NOT_ALLOWED.create());
}
}));
//?} else {
/*if (object instanceof Player onlinePlayer) {
var selectedWeight = Options.get(onlinePlayer, weight, Integer::parseInt);
if (selectedWeight.isPresent() && selectedWeight.get() > sourceWeightValue) {
consumer.accept(CompletableFuture.failedFuture(new CompletionException(ERROR_SELECTORS_NOT_ALLOWED.create())));
}
}
*///?}
}
} else throwSelectorError();
}).unordered().toArray(CompletableFuture[]::new)).get();
} catch (Exception e) {
if (e.getCause() instanceof CommandSyntaxException exception) throw exception;
LOGGER.warn("Bad selector in command {}", name, e);
throw ERROR_SELECTORS_NOT_ALLOWED.create();
}
}

private static void throwSelectorError() throws RuntimeException {
throw new RuntimeException(ERROR_SELECTORS_NOT_ALLOWED.create());
}
}
10 changes: 10 additions & 0 deletions src/main/java/me/drex/vanillapermissions/util/Permission.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.drex.vanillapermissions.util;

import net.minecraft.resources.ResourceLocation;
import org.apache.commons.lang3.StringUtils;

public class Permission {

Expand All @@ -24,6 +25,11 @@ public class Permission {
public static final String OPERATOR_BLOCK_EDIT = permission("operator_block.%s.edit");
public static final String OPERATOR_BLOCK_BREAK = permission("operator_block.%s.break");
public static final String SELECTOR = permission("selector");
public static final String SELECTOR_ENTITY = permission("selector.entity.%s");
public static final String SELECTOR_PLAYER = permission("selector.player.%s");
public static final String SELECTOR_SELF = permission("selector.self.%s");
public static final String SELECTOR_LIMIT = permission("selector.limit.%s");
public static final String SELECTOR_WEIGHT = permission("selector.weight.%s");

protected static String permission(String permission) {
return build(ResourceLocation.DEFAULT_NAMESPACE, permission);
Expand All @@ -33,4 +39,8 @@ public static String build(String... parts) {
return String.join(".", parts);
}

public static String build(int startIndex, int endIndex, String... parts) {
return StringUtils.join(parts, '.', startIndex, endIndex);
}

}
3 changes: 2 additions & 1 deletion src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"fabric-permissions-api-v0": "*"
},
"suggests": {
"luckperms": "*"
"luckperms": "*",
"metadatawildcard4fabric-permissions-api": "*"
}
}
4 changes: 4 additions & 0 deletions src/main/resources/vanilla-permissions.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
"PlayerListMixin",
"ServerPlayerAccessor",
"adminbroadcast.CommandSourceStackMixin",
"selector.argument.EntityArgumentMixin",
"selector.argument.GameProfileArgumentMixin",
"selector.argument.ScoreHolderArgumentMixin",
"selector.argument.WaypointArgumentMixin",
"bypass.chat_speed.ServerGamePacketListenerImplMixin",
"bypass.move_speed.ServerGamePacketListenerImplMixin",
"bypass.spawn_protection.DedicatedServerMixin",
Expand Down