Add fabric-permission-api support for carpet commands.

This commit is contained in:
iceBear67 committed 2024-09-21 20:12:36 +08:00
1 parent 9940feb7d3
commit e2b683c0db
11 files changed
+38 -33

No files matched your search

+2
View File
@@ -29,6 +29,8 @@ dependencies {
// PSA: Some older mods, compiled on Loom 0.2.1, might have outdated Maven POMs. // PSA: Some older mods, compiled on Loom 0.2.1, might have outdated Maven POMs.
// You may need to force-disable transitiveness on them. // You may need to force-disable transitiveness on them.
compileOnly "com.google.code.findbugs:jsr305:${project.jsr305_version}" compileOnly "com.google.code.findbugs:jsr305:${project.jsr305_version}"
modImplementation 'me.lucko:fabric-permissions-api:0.3.1'
} }
processResources { processResources {
@@ -5,6 +5,7 @@ import carpet.helpers.HopperCounter;
import carpet.utils.Messenger; import carpet.utils.Messenger;
import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import me.lucko.fabric.api.permissions.v0.Permissions;
import net.minecraft.commands.CommandBuildContext; import net.minecraft.commands.CommandBuildContext;
import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.CommandSourceStack;
import net.minecraft.network.chat.Component; import net.minecraft.network.chat.Component;
@@ -15,21 +16,18 @@ import static net.minecraft.commands.Commands.literal;
/** /**
* Class for the /counter command which allows to use hoppers pointing into wool * Class for the /counter command which allows to use hoppers pointing into wool
*/ */
public class CounterCommand public class CounterCommand {
{
/** /**
* The method used to register the command and make it available for the players to use. * The method used to register the command and make it available for the players to use.
*/ */
public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext commandBuildContext) public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext commandBuildContext) {
{
LiteralArgumentBuilder<CommandSourceStack> commandBuilder = literal("counter") LiteralArgumentBuilder<CommandSourceStack> commandBuilder = literal("counter")
.requires(c -> CarpetSettings.hopperCounters) .requires(c -> Permissions.check(c, "carpet.command.counter", CarpetSettings.hopperCounters))
.executes(c -> listAllCounters(c.getSource(), false)) .executes(c -> listAllCounters(c.getSource(), false))
.then(literal("reset") .then(literal("reset")
.executes(c -> resetCounters(c.getSource()))); .executes(c -> resetCounters(c.getSource())));
for (DyeColor dyeColor : DyeColor.values()) for (DyeColor dyeColor : DyeColor.values()) {
{
commandBuilder.then( commandBuilder.then(
literal(dyeColor.toString()) literal(dyeColor.toString())
.executes(c -> displayCounter(c.getSource(), dyeColor, false)) .executes(c -> displayCounter(c.getSource(), dyeColor, false))
@@ -37,31 +35,29 @@ public class CounterCommand
.executes(c -> resetCounter(c.getSource(), dyeColor))) .executes(c -> resetCounter(c.getSource(), dyeColor)))
.then(literal("realtime") .then(literal("realtime")
.executes(c -> displayCounter(c.getSource(), dyeColor, true))) .executes(c -> displayCounter(c.getSource(), dyeColor, true)))
); );
} }
dispatcher.register(commandBuilder); dispatcher.register(commandBuilder);
} }
/** /**
* A method to prettily display the contents of a counter to the player * A method to prettily display the contents of a counter to the player
* @param color The counter colour whose contents we are querying. *
* @param color The counter colour whose contents we are querying.
* @param realtime Whether or not to display it as in-game time or IRL time, which accounts for less than 20TPS which * @param realtime Whether or not to display it as in-game time or IRL time, which accounts for less than 20TPS which
* would make it slower than IRL * would make it slower than IRL
*/ */
private static int displayCounter(CommandSourceStack source, DyeColor color, boolean realtime) private static int displayCounter(CommandSourceStack source, DyeColor color, boolean realtime) {
{
HopperCounter counter = HopperCounter.getCounter(color); HopperCounter counter = HopperCounter.getCounter(color);
for (Component message: counter.format(source.getServer(), realtime, false)) for (Component message : counter.format(source.getServer(), realtime, false)) {
{
source.sendSuccess(() -> message, false); source.sendSuccess(() -> message, false);
} }
return 1; return 1;
} }
private static int resetCounters(CommandSourceStack source) private static int resetCounters(CommandSourceStack source) {
{
HopperCounter.resetAll(source.getServer(), false); HopperCounter.resetAll(source.getServer(), false);
Messenger.m(source, "w Restarted all counters"); Messenger.m(source, "w Restarted all counters");
return 1; return 1;
@@ -69,11 +65,10 @@ public class CounterCommand
/** /**
* A method to reset the counter's timer to 0 and empty its items * A method to reset the counter's timer to 0 and empty its items
* *
* @param color The counter whose contents we want to reset * @param color The counter whose contents we want to reset
*/ */
private static int resetCounter(CommandSourceStack source, DyeColor color) private static int resetCounter(CommandSourceStack source, DyeColor color) {
{
HopperCounter.getCounter(color).reset(source.getServer()); HopperCounter.getCounter(color).reset(source.getServer());
Messenger.m(source, "w Restarted " + color + " counter"); Messenger.m(source, "w Restarted " + color + " counter");
return 1; return 1;
@@ -81,13 +76,12 @@ public class CounterCommand
/** /**
* A method to prettily display all the counters to the player * A method to prettily display all the counters to the player
*
* @param realtime Whether or not to display it as in-game time or IRL time, which accounts for less than 20TPS which * @param realtime Whether or not to display it as in-game time or IRL time, which accounts for less than 20TPS which
* would make it slower than IRL * would make it slower than IRL
*/ */
private static int listAllCounters(CommandSourceStack source, boolean realtime) private static int listAllCounters(CommandSourceStack source, boolean realtime) {
{ for (Component message : HopperCounter.formatAll(source.getServer(), realtime)) {
for (Component message: HopperCounter.formatAll(source.getServer(), realtime))
{
source.sendSuccess(() -> message, false); source.sendSuccess(() -> message, false);
} }
return 1; return 1;
@@ -5,6 +5,7 @@ import carpet.utils.CommandHelper;
import carpet.utils.DistanceCalculator; import carpet.utils.DistanceCalculator;
import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import me.lucko.fabric.api.permissions.v0.Permissions;
import net.minecraft.commands.CommandBuildContext; import net.minecraft.commands.CommandBuildContext;
import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.arguments.coordinates.Vec3Argument; import net.minecraft.commands.arguments.coordinates.Vec3Argument;
@@ -17,7 +18,7 @@ public class DistanceCommand
public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext commandBuildContext) public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext commandBuildContext)
{ {
LiteralArgumentBuilder<CommandSourceStack> command = literal("distance"). LiteralArgumentBuilder<CommandSourceStack> command = literal("distance").
requires((player) -> CommandHelper.canUseCommand(player, CarpetSettings.commandDistance)). requires((player) -> Permissions.check(player,"carpet.command.distance",CommandHelper.canUseCommand(player, CarpetSettings.commandDistance))).
then(literal("from"). then(literal("from").
executes( (c) -> DistanceCalculator.setStart(c.getSource(), c.getSource().getPosition())). executes( (c) -> DistanceCalculator.setStart(c.getSource(), c.getSource().getPosition())).
then(argument("from", Vec3Argument.vec3()). then(argument("from", Vec3Argument.vec3()).
@@ -16,6 +16,7 @@ import com.mojang.brigadier.exceptions.CommandSyntaxException;
import java.util.List; import java.util.List;
import java.util.function.Predicate; import java.util.function.Predicate;
import me.lucko.fabric.api.permissions.v0.Permissions;
import net.minecraft.commands.CommandBuildContext; import net.minecraft.commands.CommandBuildContext;
import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.arguments.blocks.BlockInput; import net.minecraft.commands.arguments.blocks.BlockInput;
@@ -40,7 +41,7 @@ public class DrawCommand
public static void register(CommandDispatcher<CommandSourceStack> dispatcher, final CommandBuildContext context) public static void register(CommandDispatcher<CommandSourceStack> dispatcher, final CommandBuildContext context)
{ {
LiteralArgumentBuilder<CommandSourceStack> command = literal("draw"). LiteralArgumentBuilder<CommandSourceStack> command = literal("draw").
requires((player) -> CommandHelper.canUseCommand(player, CarpetSettings.commandDraw)). requires((player) -> Permissions.check(player,"carpet.command.draw",CommandHelper.canUseCommand(player, CarpetSettings.commandDraw))).
then(literal("sphere"). then(literal("sphere").
then(argument("center", BlockPosArgument.blockPos()). then(argument("center", BlockPosArgument.blockPos()).
then(argument("radius", IntegerArgumentType.integer(1)). then(argument("radius", IntegerArgumentType.integer(1)).
@@ -10,6 +10,7 @@ import java.util.List;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import me.lucko.fabric.api.permissions.v0.Permissions;
import net.minecraft.commands.CommandBuildContext; import net.minecraft.commands.CommandBuildContext;
import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands; import net.minecraft.commands.Commands;
@@ -27,7 +28,7 @@ public class InfoCommand
public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext commandBuildContext) public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext commandBuildContext)
{ {
LiteralArgumentBuilder<CommandSourceStack> command = literal("info"). LiteralArgumentBuilder<CommandSourceStack> command = literal("info").
requires((player) -> CommandHelper.canUseCommand(player, CarpetSettings.commandInfo)). requires((player) -> Permissions.check(player, "carpet.command.info",CommandHelper.canUseCommand(player, CarpetSettings.commandInfo))).
then(literal("block"). then(literal("block").
then(argument("block position", BlockPosArgument.blockPos()). then(argument("block position", BlockPosArgument.blockPos()).
executes( (c) -> infoBlock( executes( (c) -> infoBlock(
@@ -15,6 +15,7 @@ import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import me.lucko.fabric.api.permissions.v0.Permissions;
import net.minecraft.commands.CommandBuildContext; import net.minecraft.commands.CommandBuildContext;
import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands; import net.minecraft.commands.Commands;
@@ -29,7 +30,7 @@ public class LogCommand
public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext commandBuildContext) public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext commandBuildContext)
{ {
LiteralArgumentBuilder<CommandSourceStack> literalargumentbuilder = Commands.literal("log"). LiteralArgumentBuilder<CommandSourceStack> literalargumentbuilder = Commands.literal("log").
requires((player) -> CommandHelper.canUseCommand(player, CarpetSettings.commandLog)). requires((player) -> Permissions.check(player,"carpet.command.log",CommandHelper.canUseCommand(player, CarpetSettings.commandLog))).
executes((context) -> listLogs(context.getSource())). executes((context) -> listLogs(context.getSource())).
then(Commands.literal("clear"). then(Commands.literal("clear").
executes( (c) -> unsubFromAll(c.getSource(), c.getSource().getTextName())). executes( (c) -> unsubFromAll(c.getSource(), c.getSource().getTextName())).
@@ -6,6 +6,7 @@ import carpet.utils.MobAI;
import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType; import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import me.lucko.fabric.api.permissions.v0.Permissions;
import net.minecraft.commands.CommandBuildContext; import net.minecraft.commands.CommandBuildContext;
import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.CommandSourceStack;
import net.minecraft.core.registries.Registries; import net.minecraft.core.registries.Registries;
@@ -21,7 +22,7 @@ public class MobAICommand
public static void register(CommandDispatcher<CommandSourceStack> dispatcher, final CommandBuildContext commandBuildContext) public static void register(CommandDispatcher<CommandSourceStack> dispatcher, final CommandBuildContext commandBuildContext)
{ {
LiteralArgumentBuilder<CommandSourceStack> command = literal("track"). LiteralArgumentBuilder<CommandSourceStack> command = literal("track").
requires((player) -> CommandHelper.canUseCommand(player, CarpetSettings.commandTrackAI)). requires((player) -> Permissions.check(player, "carpet.command.track",CommandHelper.canUseCommand(player, CarpetSettings.commandTrackAI))).
then(argument("entity type", resource(commandBuildContext, Registries.ENTITY_TYPE)). then(argument("entity type", resource(commandBuildContext, Registries.ENTITY_TYPE)).
suggests( (c, b) -> suggest(MobAI.availbleTypes(c.getSource()), b)). suggests( (c, b) -> suggest(MobAI.availbleTypes(c.getSource()), b)).
@@ -6,6 +6,7 @@ import carpet.utils.Messenger;
import carpet.utils.PerimeterDiagnostics; import carpet.utils.PerimeterDiagnostics;
import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import me.lucko.fabric.api.permissions.v0.Permissions;
import net.minecraft.commands.CommandBuildContext; import net.minecraft.commands.CommandBuildContext;
import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.arguments.coordinates.BlockPosArgument; import net.minecraft.commands.arguments.coordinates.BlockPosArgument;
@@ -27,7 +28,7 @@ public class PerimeterInfoCommand
public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext commandBuildContext) public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext commandBuildContext)
{ {
LiteralArgumentBuilder<CommandSourceStack> command = literal("perimeterinfo"). LiteralArgumentBuilder<CommandSourceStack> command = literal("perimeterinfo").
requires((player) -> CommandHelper.canUseCommand(player, CarpetSettings.commandPerimeterInfo)). requires((player) -> Permissions.check(player,"carpet.command.perimeterinfo",CommandHelper.canUseCommand(player, CarpetSettings.commandPerimeterInfo))).
executes( (c) -> perimeterDiagnose( executes( (c) -> perimeterDiagnose(
c.getSource(), c.getSource(),
BlockPos.containing(c.getSource().getPosition()), BlockPos.containing(c.getSource().getPosition()),
@@ -16,6 +16,7 @@ import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.context.CommandContext; import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.exceptions.CommandSyntaxException;
import me.lucko.fabric.api.permissions.v0.Permissions;
import net.minecraft.SharedConstants; import net.minecraft.SharedConstants;
import net.minecraft.commands.CommandBuildContext; import net.minecraft.commands.CommandBuildContext;
import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.CommandSourceStack;
@@ -51,7 +52,7 @@ public class PlayerCommand
public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext commandBuildContext) public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext commandBuildContext)
{ {
LiteralArgumentBuilder<CommandSourceStack> command = literal("player") LiteralArgumentBuilder<CommandSourceStack> command = literal("player")
.requires((player) -> CommandHelper.canUseCommand(player, CarpetSettings.commandPlayer)) .requires((player) -> Permissions.check(player,"carpet.command.player",CommandHelper.canUseCommand(player, CarpetSettings.commandPlayer)))
.then(argument("player", StringArgumentType.word()) .then(argument("player", StringArgumentType.word())
.suggests((c, b) -> suggest(getPlayerSuggestions(c.getSource()), b)) .suggests((c, b) -> suggest(getPlayerSuggestions(c.getSource()), b))
.then(literal("stop").executes(manipulation(EntityPlayerActionPack::stopAll))) .then(literal("stop").executes(manipulation(EntityPlayerActionPack::stopAll)))
@@ -6,6 +6,7 @@ import carpet.utils.CommandHelper;
import com.mojang.brigadier.CommandDispatcher; import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import me.lucko.fabric.api.permissions.v0.Permissions;
import net.minecraft.commands.CommandBuildContext; import net.minecraft.commands.CommandBuildContext;
import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.CommandSourceStack;
@@ -19,7 +20,7 @@ public class ProfileCommand
public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext commandBuildContext) public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext commandBuildContext)
{ {
LiteralArgumentBuilder<CommandSourceStack> literalargumentbuilder = literal("profile"). LiteralArgumentBuilder<CommandSourceStack> literalargumentbuilder = literal("profile").
requires((player) -> CommandHelper.canUseCommand(player, CarpetSettings.commandProfile)). requires((player) -> Permissions.check(player, "carpet.command.profile",CommandHelper.canUseCommand(player, CarpetSettings.commandProfile))).
executes( (c) -> healthReport(c.getSource(), 100)). executes( (c) -> healthReport(c.getSource(), 100)).
then(literal("health"). then(literal("health").
executes( (c) -> healthReport(c.getSource(), 100)). executes( (c) -> healthReport(c.getSource(), 100)).
@@ -17,6 +17,7 @@ import java.util.Map;
import java.util.function.Function; import java.util.function.Function;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import me.lucko.fabric.api.permissions.v0.Permissions;
import net.minecraft.commands.CommandBuildContext; import net.minecraft.commands.CommandBuildContext;
import net.minecraft.commands.CommandSourceStack; import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.arguments.DimensionArgument; import net.minecraft.commands.arguments.DimensionArgument;
@@ -42,7 +43,7 @@ public class SpawnCommand
public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext commandBuildContext) public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext commandBuildContext)
{ {
LiteralArgumentBuilder<CommandSourceStack> literalargumentbuilder = literal("spawn"). LiteralArgumentBuilder<CommandSourceStack> literalargumentbuilder = literal("spawn").
requires((player) -> CommandHelper.canUseCommand(player, CarpetSettings.commandSpawn)); requires((player) -> Permissions.check(player, "carpet.command.spawn",CommandHelper.canUseCommand(player, CarpetSettings.commandSpawn)));
literalargumentbuilder. literalargumentbuilder.
then(literal("list"). then(literal("list").