ops options for commands

This commit is contained in:
gnembon committed 2019-12-01 01:04:58 -04:00
1 parent c2f22a141a
commit b52b5c335f
16 files changed
+133 -51

No files matched your search

@@ -1,6 +1,7 @@
package carpet.commands;
import carpet.settings.CarpetSettings;
import carpet.settings.SettingsManager;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
@@ -21,13 +22,13 @@ public class CameraModeCommand
public static void register(CommandDispatcher<ServerCommandSource> dispatcher)
{
LiteralArgumentBuilder<ServerCommandSource> camera = literal("c").
requires((player) -> CarpetSettings.commandCameramode).
requires((player) -> SettingsManager.canUseCommand(player, CarpetSettings.commandCameramode)).
executes((c) -> cameraMode(c.getSource(), c.getSource().getPlayer())).
then(argument("player", EntityArgumentType.player()).
executes( (c) -> cameraMode(c.getSource(), EntityArgumentType.getPlayer(c, "player"))));
LiteralArgumentBuilder<ServerCommandSource> survival = literal("s").
requires((player) -> CarpetSettings.commandCameramode).
requires((player) -> SettingsManager.canUseCommand(player, CarpetSettings.commandCameramode)).
executes((c) -> survivalMode(
c.getSource(),
c.getSource().getPlayer())).
@@ -1,6 +1,7 @@
package carpet.commands;
import carpet.settings.CarpetSettings;
import carpet.settings.SettingsManager;
import carpet.utils.DistanceCalculator;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
@@ -15,7 +16,7 @@ public class DistanceCommand
public static void register(CommandDispatcher<ServerCommandSource> dispatcher)
{
LiteralArgumentBuilder<ServerCommandSource> command = literal("distance").
requires((player) -> CarpetSettings.commandDistance).
requires((player) -> SettingsManager.canUseCommand(player, CarpetSettings.commandDistance)).
then(literal("from").
executes( (c) -> DistanceCalculator.setStart(c.getSource(), c.getSource().getPosition())).
then(argument("from", Vec3ArgumentType.vec3()).
@@ -1,6 +1,7 @@
package carpet.commands;
import carpet.settings.CarpetSettings;
import carpet.settings.SettingsManager;
import carpet.utils.Messenger;
import com.google.common.collect.Lists;
import com.mojang.brigadier.CommandDispatcher;
@@ -29,7 +30,7 @@ public class DrawCommand
public static void register(CommandDispatcher<ServerCommandSource> dispatcher)
{
LiteralArgumentBuilder<ServerCommandSource> command = literal("draw").
requires((player) -> CarpetSettings.commandDraw).
requires((player) -> SettingsManager.canUseCommand(player, CarpetSettings.commandDraw)).
then(literal("sphere").
then(argument("center",BlockPosArgumentType.blockPos()).
then(argument("radius",IntegerArgumentType.integer(1)).
@@ -1,6 +1,7 @@
package carpet.commands;
import carpet.settings.CarpetSettings;
import carpet.settings.SettingsManager;
import carpet.utils.BlockInfo;
import carpet.utils.EntityInfo;
import carpet.utils.Messenger;
@@ -29,7 +30,7 @@ public class InfoCommand
public static void register(CommandDispatcher<ServerCommandSource> dispatcher)
{
LiteralArgumentBuilder<ServerCommandSource> command = literal("info").
requires((player) -> CarpetSettings.commandInfo).
requires((player) -> SettingsManager.canUseCommand(player, CarpetSettings.commandInfo)).
then(literal("block").
then(argument("block position", BlockPosArgumentType.blockPos()).
executes( (c) -> infoBlock(
@@ -3,6 +3,7 @@ package carpet.commands;
import carpet.settings.CarpetSettings;
import carpet.logging.Logger;
import carpet.logging.LoggerRegistry;
import carpet.settings.SettingsManager;
import carpet.utils.Messenger;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType;
@@ -26,7 +27,7 @@ public class LogCommand
public static void register(CommandDispatcher<ServerCommandSource> dispatcher)
{
LiteralArgumentBuilder<ServerCommandSource> literalargumentbuilder = CommandManager.literal("log").
requires((player) -> CarpetSettings.commandLog).
requires((player) -> SettingsManager.canUseCommand(player, CarpetSettings.commandLog)).
executes((context) -> listLogs(context.getSource())).
then(CommandManager.literal("clear").
executes( (c) -> unsubFromAll(c.getSource(), c.getSource().getName())).
@@ -1,6 +1,7 @@
package carpet.commands;
import carpet.settings.CarpetSettings;
import carpet.settings.SettingsManager;
import carpet.utils.MobAI;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.arguments.StringArgumentType;
@@ -18,7 +19,7 @@ public class MobAICommand
public static void register(CommandDispatcher<ServerCommandSource> dispatcher)
{
LiteralArgumentBuilder<ServerCommandSource> command = literal("track").
requires((player) -> CarpetSettings.commandTrackAI).
requires((player) -> SettingsManager.canUseCommand(player, CarpetSettings.commandTrackAI)).
then(argument("entity type", EntitySummonArgumentType.entitySummon()).
suggests( (c, b) -> suggestMatching(MobAI.availbleTypes(), b)).
@@ -1,6 +1,7 @@
package carpet.commands;
import carpet.settings.CarpetSettings;
import carpet.settings.SettingsManager;
import carpet.utils.Messenger;
import carpet.utils.PerimeterDiagnostics;
import com.mojang.brigadier.CommandDispatcher;
@@ -22,7 +23,7 @@ public class PerimeterInfoCommand
public static void register(CommandDispatcher<ServerCommandSource> dispatcher)
{
LiteralArgumentBuilder<ServerCommandSource> command = literal("perimeterinfo").
requires((player) -> CarpetSettings.commandPerimeterInfo).
requires((player) -> SettingsManager.canUseCommand(player, CarpetSettings.commandPerimeterInfo)).
executes( (c) -> perimeterDiagnose(
c.getSource(),
new BlockPos(c.getSource().getPosition()),
@@ -4,6 +4,7 @@ import carpet.helpers.EntityPlayerActionPack;
import carpet.settings.CarpetSettings;
import carpet.fakes.ServerPlayerEntityInterface;
import carpet.patches.EntityPlayerMPFake;
import carpet.settings.SettingsManager;
import carpet.utils.Messenger;
import com.google.common.collect.Sets;
import com.mojang.authlib.GameProfile;
@@ -44,7 +45,7 @@ public class PlayerCommand
public static void register(CommandDispatcher<ServerCommandSource> dispatcher)
{
LiteralArgumentBuilder<ServerCommandSource> literalargumentbuilder = literal("player")
.requires((player) -> CarpetSettings.commandPlayer)
.requires((player) -> SettingsManager.canUseCommand(player, CarpetSettings.commandPlayer))
.then(argument("player", StringArgumentType.word())
.suggests( (c, b) -> suggestMatching(getPlayers(c.getSource()), b))
.then(literal("stop").executes(PlayerCommand::stop))
@@ -3,6 +3,7 @@ package carpet.commands;
import carpet.settings.CarpetSettings;
import carpet.helpers.HopperCounter;
import carpet.helpers.TickSpeed;
import carpet.settings.SettingsManager;
import carpet.utils.Messenger;
import carpet.utils.SpawnReporter;
import com.mojang.brigadier.CommandDispatcher;
@@ -37,7 +38,7 @@ public class SpawnCommand
public static void register(CommandDispatcher<ServerCommandSource> dispatcher)
{
LiteralArgumentBuilder<ServerCommandSource> literalargumentbuilder = literal("spawn").
requires((player) -> CarpetSettings.commandSpawn);
requires((player) -> SettingsManager.canUseCommand(player, CarpetSettings.commandSpawn));
literalargumentbuilder.
then(literal("list").
@@ -2,6 +2,7 @@ package carpet.commands;
import carpet.settings.CarpetSettings;
import carpet.helpers.TickSpeed;
import carpet.settings.SettingsManager;
import carpet.utils.CarpetProfiler;
import carpet.utils.Messenger;
import com.mojang.brigadier.CommandDispatcher;
@@ -26,7 +27,7 @@ public class TickCommand
public static void register(CommandDispatcher<ServerCommandSource> dispatcher)
{
LiteralArgumentBuilder<ServerCommandSource> literalargumentbuilder = literal("tick").
requires((player) -> CarpetSettings.commandTick).
requires((player) -> SettingsManager.canUseCommand(player, CarpetSettings.commandTick)).
then(literal("rate").
executes((c) -> queryTps(c.getSource())).
then(argument("rate", floatArg(0.1F, 500.0F)).
@@ -25,6 +25,7 @@ import static carpet.settings.RuleCategory.FEATURE;
import static carpet.settings.RuleCategory.OPTIMIZATION;
import static carpet.settings.RuleCategory.SURVIVAL;
import static carpet.settings.RuleCategory.TNT;
import static carpet.settings.RuleCategory.DISPENSER;
public class CarpetSettings
{
@@ -108,7 +109,7 @@ public class CarpetSettings
}
}
@Rule( desc = "Sets the tnt random explosion range to a fixed value", category = TNT, options = "-1",
@Rule( desc = "Sets the tnt random explosion range to a fixed value", category = TNT, options = "-1", strict = false,
validate = {CheckOptimizedTntEnabledValidator.class, TNTRandomRangeValidator.class}, extra = "Set to -1 for default behavior")
public static double tntRandomRange = -1;
@@ -124,7 +125,7 @@ public class CarpetSettings
}
}
@Rule( desc = "Sets the horizontal random angle on TNT for debugging of TNT contraptions", category = TNT, options = "-1",
@Rule( desc = "Sets the horizontal random angle on TNT for debugging of TNT contraptions", category = TNT, options = "-1", strict = false,
validate = TNTAngleValidator.class, extra = "Set to -1 for default behavior")
public static double hardcodeTNTangle;
@@ -218,23 +219,20 @@ public class CarpetSettings
public static boolean summonNaturalLightning = false;
@Rule(desc = "Enables /spawn command for spawn tracking", category = COMMAND)
public static boolean commandSpawn = true;
public static String commandSpawn = "true";
@Rule(desc = "Enables /tick command to control game clocks", category = COMMAND)
public static boolean commandTick = true;
public static String commandTick = "true";
@Rule(
desc = "Enables /log command to monitor events in the game via chat and overlays",
category = COMMAND//,
//validate = Validator.WIP.class
)
public static boolean commandLog = true;
@Rule(desc = "Enables /log command to monitor events via chat and overlays", category = COMMAND)
public static String commandLog = "true";
@Rule(
desc = "sets these loggers in their default configurations for all new players",
extra = "use csv, like 'tps,mobcaps' for multiple loggers, none for nothing",
category = {CREATIVE, SURVIVAL},
options = {"none", "tps", "mobcaps,tps"}
options = {"none", "tps", "mobcaps,tps"},
strict = false
)
public static String defaultLoggers = "none";
@@ -243,7 +241,7 @@ public class CarpetSettings
extra = "Also enables brown carpet placement action if 'carpets' rule is turned on as well",
category = COMMAND
)
public static boolean commandDistance = true;
public static String commandDistance = "true";
@Rule(
desc = "Enables /info command for blocks",
@@ -253,24 +251,24 @@ public class CarpetSettings
},
category = COMMAND
)
public static boolean commandInfo = true;
public static String commandInfo = "true";
@Rule(
desc = "Enables /c and /s commands to quickly switch between camera and survival modes",
extra = "/c and /s commands are available to all players regardless of their permission levels",
category = COMMAND
)
public static boolean commandCameramode = true;
public static String commandCameramode = "true";
@Rule(
desc = "Enables /perimeterinfo command",
extra = "... that scans the area around the block for potential spawnable spots",
category = COMMAND
)
public static boolean commandPerimeterInfo = true;
public static String commandPerimeterInfo = "true";
@Rule(desc = "Enables /draw commands", extra = "... allows for drawing simple shapes", category = COMMAND)
public static boolean commandDraw = true;
public static String commandDraw = "true";
@Rule(desc = "Enables /script command", extra = "An in-game scripting API for Scarpet programming language", category = COMMAND)
public static boolean commandScript = true;
@@ -283,10 +281,10 @@ public class CarpetSettings
public static boolean scriptsAutoload = false;
@Rule(desc = "Enables /player command to control/spawn players", category = COMMAND)
public static boolean commandPlayer = true;
public static String commandPlayer = "true";
@Rule(desc = "Allows to track mobs AI via /track command", category = COMMAND)
public static boolean commandTrackAI = true;
public static String commandTrackAI = "true";
@Rule(desc = "Placing carpets may issue carpet commands for non-op players", category = SURVIVAL)
public static boolean carpets = false;
@@ -308,7 +306,7 @@ public class CarpetSettings
extra="Increases player allowed mining distance to 32 blocks",
category = SURVIVAL
)
public static boolean miningGhostBlockFix;
public static boolean miningGhostBlockFix = false;
private static class PushLimitLimits extends Validator<Integer> {
@Override public Integer validate(ServerCommandSource source, ParsedRule<Integer> currentRule, Integer newValue, String string) {
@@ -321,6 +319,7 @@ public class CarpetSettings
desc = "Customizable piston push limit",
options = {"10", "12", "14", "100"},
category = CREATIVE,
strict = false,
validate = PushLimitLimits.class
)
public static int pushLimit = 12;
@@ -329,6 +328,7 @@ public class CarpetSettings
desc = "Customizable powered rail power range",
options = {"9", "15", "30"},
category = CREATIVE,
strict = false,
validate = PushLimitLimits.class
)
public static int railPowerLimit = 9;
@@ -344,6 +344,7 @@ public class CarpetSettings
desc = "Customizable fill/clone volume limit",
options = {"32768", "250000", "1000000"},
category = CREATIVE,
strict = false,
validate = FillLimitLimits.class
)
public static int fillLimit = 32768;
@@ -352,17 +353,12 @@ public class CarpetSettings
desc = "Customizable maximal entity collision limits, 0 for no limits",
options = {"0", "1", "20"},
category = OPTIMIZATION,
strict = false,
validate = Validator.NONNEGATIVE_NUMBER.class
)
public static int maxEntityCollisions = 0;
/*
@Rule(
desc = "Fix for piston ghost blocks",
category = BUGFIX,
validate = Validator.WIP.class
)
public static boolean pistonGhostBlocksFix = true;
@Rule(
desc = "fixes water performance issues",
@@ -379,11 +375,16 @@ public class CarpetSettings
desc = "Sets a different motd message on client trying to connect to the server",
extra = "use '_' to use the startup setting from server.properties",
options = "_",
strict = false,
category = CREATIVE
)
public static String customMOTD = "_";
@Rule(desc = "Cactus in dispensers rotates blocks.", extra = "Rotates block anti-clockwise if possible", category = FEATURE)
@Rule(
desc = "Cactus in dispensers rotates blocks.",
extra = "Rotates block anti-clockwise if possible",
category = {FEATURE, DISPENSER}
)
public static boolean rotatorBlock = false;
private static class ViewDistanceValidator extends Validator<Integer>
@@ -422,6 +423,7 @@ public class CarpetSettings
extra = "Set to 0 to not override the value in server settings.",
options = {"0", "12", "16", "32"},
category = CREATIVE,
strict = false,
validate = ViewDistanceValidator.class
)
public static int viewDistance = 0;
@@ -470,6 +472,7 @@ public class CarpetSettings
desc = "limits growth limit of newly naturally generated kelp to this amount of blocks",
options = {"0", "2", "25"},
category = FEATURE,
strict = false,
validate = KelpLimit.class
)
public static int kelpGenerationGrowthLimit = 25;
+27 -7
View File
@@ -3,6 +3,7 @@ package carpet.settings;
import carpet.CarpetServer;
import carpet.utils.Messenger;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Sets;
import net.minecraft.server.command.ServerCommandSource;
import java.lang.reflect.Constructor;
@@ -38,25 +39,43 @@ public final class ParsedRule<T> implements Comparable<ParsedRule> {
for (Class v : rule.validate())
this.validators.add((Validator<T>) callConstructor(v));
if (categories.contains(RuleCategory.COMMAND))
{
this.validators.add(callConstructor(Validator._COMMAND.class));
if (this.type == String.class)
{
this.isStrict = false;
this.validators.add((Validator<T>) callConstructor(Validator._COMMAND_LEVEL_VALIDATOR.class));
}
}
this.defaultValue = get();
this.defaultAsString = convertToString(this.defaultValue);
if (this.type == boolean.class)
if (rule.options().length > 0)
{
this.options = ImmutableList.copyOf(rule.options());
}
else if (this.type == boolean.class || (this.type == String.class && categories.contains(RuleCategory.COMMAND)))
{
this.isStrict = true; // it has no sense otherwise
this.options = ImmutableList.of("true", "false");
}
else if (this.type.isEnum())
{
this.isStrict = true; // it has no sense otherwise
this.options = Arrays.stream(this.type.getEnumConstants()).map(e -> ((Enum) e).name().toLowerCase(Locale.ROOT)).collect(ImmutableList.toImmutableList());
}
else
{
this.options = ImmutableList.copyOf(rule.options());
this.options = ImmutableList.of();
}
if (isStrict && !this.options.isEmpty())
{
if (this.type == boolean.class || this.type == int.class || this.type == double.class || this.type == float.class)
{
this.validators.add(callConstructor(Validator._STRICT_IGNORECASE.class));
}
else
{
this.validators.add(callConstructor(Validator._STRICT.class));
}
}
if (isStrict)
this.validators.add(callConstructor(Validator._STRICT.class));
}
private <T> T callConstructor(Class<T> cls)
@@ -115,7 +134,8 @@ public final class ParsedRule<T> implements Comparable<ParsedRule> {
if (value == null)
{
Messenger.m(source, "r Wrong value for " + name + ": " + stringValue);
Messenger.m(source, "r " + validator.description());
if (validator.description()!= null)
Messenger.m(source, "r " + validator.description());
return null;
}
}
+1 -1
View File
@@ -50,7 +50,7 @@ public @interface Rule
* any of the options
* For enums, its always strict, same for booleans - no need to set that for them.
*/
boolean strict() default false;
boolean strict() default true;
/**
* The class of the validator checked when the rule is changed.
@@ -185,6 +185,23 @@ public class SettingsManager
}));
}
public static boolean canUseCommand(ServerCommandSource source, String commandLevel)
{
switch (commandLevel)
{
case "true": return true;
case "false": return false;
case "ops": return source.hasPermissionLevel(2); // typical for other cheaty commands
case "0":
case "1":
case "2":
case "3":
case "4":
return source.hasPermissionLevel(Integer.parseInt(commandLevel));
}
return false;
}
private void loadConfigurationFromConf()
{
for (ParsedRule<?> rule : rules.values()) rule.resetToDefault(server.getCommandSource());
+33 -1
View File
@@ -2,9 +2,11 @@ package carpet.settings;
import carpet.CarpetServer;
import carpet.utils.Messenger;
import com.google.common.collect.ImmutableList;
import net.minecraft.server.command.ServerCommandSource;
import java.util.Locale;
import java.util.stream.Collectors;
public abstract class Validator<T>
{
@@ -39,6 +41,21 @@ public abstract class Validator<T>
public String description() { return "It has an accompanying command";}
}
public static class _COMMAND_LEVEL_VALIDATOR extends Validator<String> {
private static ImmutableList<String> OPTIONS = ImmutableList.of("true", "false", "ops", "0", "1", "2", "3", "4");
@Override public String validate(ServerCommandSource source, ParsedRule<String> currentRule, String newValue, String userString) {
if (!OPTIONS.contains(userString.toLowerCase(Locale.ROOT)))
{
Messenger.m(source, "r Valid options for command type rules is 'true' or 'false'");
Messenger.m(source, "r Optionally you can choose 'ops' to only allow operators");
Messenger.m(source, "r or provide a custom required permission level");
return null;
}
return userString.toLowerCase(Locale.ROOT);
}
public String description() { return "Can be limited to 'ops' only, or a custom permission level";}
}
public static class WIP<T> extends Validator<T>
{
@Override
@@ -55,7 +72,7 @@ public abstract class Validator<T>
@Override
public T validate(ServerCommandSource source, ParsedRule<T> currentRule, T newValue, String string)
{
if (!currentRule.options.contains(string.toLowerCase(Locale.ROOT)))
if (!currentRule.options.contains(string))
{
Messenger.m(source, "r Valid options: " + currentRule.options.toString());
return null;
@@ -64,6 +81,21 @@ public abstract class Validator<T>
}
}
public static class _STRICT_IGNORECASE<T> extends Validator<T>
{
@Override
public T validate(ServerCommandSource source, ParsedRule<T> currentRule, T newValue, String string)
{
if (!currentRule.options.stream().map(s->s.toLowerCase(Locale.ROOT)).collect(Collectors.toSet())
.contains(string.toLowerCase(Locale.ROOT)))
{
Messenger.m(source, "r Valid options (case insensitive): " + currentRule.options.toString());
return null;
}
return newValue;
}
}
public static class NONNEGATIVE_NUMBER <T extends Number> extends Validator<T>
{
@Override
+5 -5
View File
@@ -37,16 +37,16 @@ public class WoolTool
switch (color)
{
case PINK:
if (CarpetSettings.commandSpawn)
if (!"false".equals(CarpetSettings.commandSpawn))
Messenger.send(placer, SpawnReporter.report(pos, worldIn));
break;
case BLACK:
if (CarpetSettings.commandSpawn)
if (!"false".equals(CarpetSettings.commandSpawn))
Messenger.send(placer, SpawnReporter.show_mobcaps(pos, worldIn));
break;
case BROWN:
if (CarpetSettings.commandDistance)
if (!"false".equals(CarpetSettings.commandDistance))
{
ServerCommandSource source = placer.getCommandSource();
if (!DistanceCalculator.hasStartingPoint(source) || placer.isSneaking()) {
@@ -58,11 +58,11 @@ public class WoolTool
}
break;
case GRAY:
if (CarpetSettings.commandInfo)
if (!"false".equals(CarpetSettings.commandInfo))
Messenger.send(placer, BlockInfo.blockInfo(pos.down(), worldIn));
break;
case YELLOW:
if (CarpetSettings.commandInfo)
if (!"false".equals(CarpetSettings.commandInfo))
Messenger.m(placer, "r This used to show entity info around the player. Use data get entity command, sorry");
//EntityInfo.issue_entity_info(placer);
break;