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
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,15 @@ class Holder {
CombatEntry createCombatEntry(DamageSource damageSource, float damage, @Nullable FallLocationType fallLocationType, float fallDistance);

/**
* Causes this predicate to be considered restricted.
* Applying this to a command node prevents this command from being executed from an
* unattended context, such as click events.
* Allows overriding default restricted check.
* Applying this to a command node forcefully prevents or permits this command to be
* executed from an unattended context, such as click events.
*
* @param predicate wrapped predicate
* @param predicate wrapped predicate
* @param restricted restricted status of the predicate
* @return wrapped predicate
*/
Predicate<CommandSourceStack> restricted(Predicate<CommandSourceStack> predicate);
Predicate<CommandSourceStack> restricted(Predicate<CommandSourceStack> predicate, boolean restricted);

ResolvableProfile defaultMannequinProfile();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,23 @@ static <T> RequiredArgumentBuilder<CommandSourceStack, T> argument(final String
* @return a new predicate with restricted execution behavior
*/
static Predicate<CommandSourceStack> restricted(final Predicate<CommandSourceStack> predicate) {
return InternalAPIBridge.get().restricted(predicate);
return InternalAPIBridge.get().restricted(predicate, true);
}

/**
* Creates an unrestricted {@link Predicate} that wraps the given predicate.
* <p>
* A restricted predicate prevents execution in unattended contexts, such as from chat click events.
* Unrestricting prevents the warning shown on the client before executing the command.
* <p>
* This is used by vanilla to prevent invocation of sensitive commands (like op) from
* players without their knowledge.
*
* @param predicate the original predicate to wrap
* @return a new predicate with unrestricted execution behavior
*/
static Predicate<CommandSourceStack> unrestricted(final Predicate<CommandSourceStack> predicate) {
return InternalAPIBridge.get().restricted(predicate, false);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@
import org.slf4j.Logger;

public class Commands {
+ public interface RestrictedMarker { } // Paper - restricted api
+ public interface RestrictedMarker { boolean restricted(); } // Paper - restricted api
public static final String COMMAND_PREFIX = "/";
private static final ThreadLocal<@Nullable ExecutionContext<CommandSourceStack>> CURRENT_EXECUTION_CONTEXT = new ThreadLocal<>();
private static final Logger LOGGER = LogUtils.getLogger();
@@ -183,6 +_,7 @@

@Override
public boolean isRestricted(final CommandNode<CommandSourceStack> node) {
+ if (node.getRequirement() instanceof RestrictedMarker) return true; // Paper - restricted api
+ if (node.getRequirement() instanceof RestrictedMarker marker) return marker.restricted(); // Paper - restricted api
Predicate<CommandSourceStack> requirement = node.getRequirement();
return !requirement.test(this.noPermissionSource);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,15 @@ private CombatEntry createCombatEntry(
}

@Override
public Predicate<CommandSourceStack> restricted(final Predicate<CommandSourceStack> predicate) {
record RestrictedPredicate(Predicate<CommandSourceStack> predicate) implements Predicate<CommandSourceStack>, Commands.RestrictedMarker {
public Predicate<CommandSourceStack> restricted(final Predicate<CommandSourceStack> predicate, final boolean restricted) {
record RestrictedPredicate(Predicate<CommandSourceStack> predicate, @Override boolean restricted) implements Predicate<CommandSourceStack>, Commands.RestrictedMarker {
@Override
public boolean test(final CommandSourceStack commandSourceStack) {
return this.predicate.test(commandSourceStack);
}
}

return new RestrictedPredicate(predicate);
return new RestrictedPredicate(predicate, restricted);
}

@Override
Expand Down
Loading