Added '/worldhandler allow_commands' subcommand, improved permission query error messages

This commit is contained in:
Marcel Konrad
2022-06-28 23:46:02 +02:00
parent b205bba317
commit 560b9ee360
9 changed files with 90 additions and 18 deletions

View File

@@ -12,7 +12,9 @@ public class WorldHandlerCommandBuilder extends CommandBuilder
.then(CommandNode.literal("display")
.label(Label.DISPLAY))
.then(CommandNode.literal("version")
.label(Label.VERSION));
.label(Label.VERSION))
.then(CommandNode.literal("allow_commands")
.label(Label.ALLOW_COMMANDS));
@Override
protected CommandNodeLiteral root()
@@ -24,6 +26,7 @@ public class WorldHandlerCommandBuilder extends CommandBuilder
{
HELP,
DISPLAY,
VERSION;
VERSION,
ALLOW_COMMANDS;
}
}

View File

@@ -4,17 +4,26 @@ import org.apache.maven.artifact.versioning.ComparableVersion;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import exopandora.worldhandler.Main;
import exopandora.worldhandler.event.ClientEventHandler;
import exopandora.worldhandler.util.CommandHelper;
import net.minecraft.client.Minecraft;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.Component;
import net.minecraft.server.MinecraftServer;
import net.minecraft.world.level.LevelSettings;
import net.minecraft.world.level.storage.PrimaryLevelData;
import net.minecraftforge.fml.ModList;
import net.minecraftforge.fml.VersionChecker;
public class CommandWorldHandler
{
private static final SimpleCommandExceptionType NOT_IN_SINGLEPLAYER = new SimpleCommandExceptionType(Component.translatable("commands.worldhandler.allow_commands.not_in_singleplayer"));
private static final SimpleCommandExceptionType COMMANDS_ALREADY_ALLOWED = new SimpleCommandExceptionType(Component.translatable("commands.worldhandler.allow_commands.commands_already_allowed"));
public static void register(CommandDispatcher<CommandSourceStack> dispatcher)
{
dispatcher.register(Commands.literal("worldhandler")
@@ -23,7 +32,9 @@ public class CommandWorldHandler
.then(Commands.literal("display")
.executes(context -> display(context.getSource())))
.then(Commands.literal("version")
.executes(context -> version(context.getSource()))));
.executes(context -> version(context.getSource())))
.then(Commands.literal("allow_commands")
.executes(context -> allowCommands(context.getSource()))));
}
private static int help(CommandSourceStack source) throws CommandSyntaxException
@@ -31,6 +42,7 @@ public class CommandWorldHandler
CommandHelper.sendFeedback(source, "/worldhandler help");
CommandHelper.sendFeedback(source, "/worldhandler display");
CommandHelper.sendFeedback(source, "/worldhandler version");
CommandHelper.sendFeedback(source, "/worldhandler allow_commands");
return 1;
}
@@ -47,4 +59,27 @@ public class CommandWorldHandler
CommandHelper.sendFeedback(source, "Latest: " + Main.MC_VERSION + "-" + (target != null ? target : Main.MOD_VERSION));
return 1;
}
private static int allowCommands(CommandSourceStack source) throws CommandSyntaxException
{
if(!Minecraft.getInstance().hasSingleplayerServer())
{
throw NOT_IN_SINGLEPLAYER.create();
}
MinecraftServer server = Minecraft.getInstance().getSingleplayerServer();
PrimaryLevelData worldData = (PrimaryLevelData) server.getWorldData();
LevelSettings settings = worldData.settings;
if(settings.allowCommands())
{
throw COMMANDS_ALREADY_ALLOWED.create();
}
worldData.settings = new LevelSettings(settings.levelName(), settings.gameType(), settings.hardcore(), settings.difficulty(), true, settings.gameRules(), settings.getDataPackConfig(), settings.getLifecycle());
int operatorPermissionLevel = server.getOperatorUserPermissionLevel();
Minecraft.getInstance().player.setPermissionLevel(operatorPermissionLevel);
source.sendSuccess(Component.translatable("commands.worldhandler.allow_commands.success"), false);
return operatorPermissionLevel;
}
}

View File

@@ -181,10 +181,18 @@ public class ActionHelper
public static void displayGui()
{
if(!CommandHelper.canPlayerIssueCommand() && Config.getSettings().permissionQuery())
if(Config.getSettings().permissionQuery() && !CommandHelper.canPlayerIssueCommand())
{
Minecraft.getInstance().gui.getChat().addMessage(Component.literal(ChatFormatting.RED + I18n.get("worldhandler.permission.refused")));
Minecraft.getInstance().gui.getChat().addMessage(Component.literal(ChatFormatting.RED + I18n.get("worldhandler.permission.refused.change", I18n.get("gui.worldhandler.config.settings.permission_query"))));
Minecraft.getInstance().gui.getChat().addMessage(Component.translatable("worldhandler.permission.refused").withStyle(ChatFormatting.RED));
if(Minecraft.getInstance().hasSingleplayerServer())
{
Minecraft.getInstance().gui.getChat().addMessage(Component.translatable("worldhandler.permission.refused.singleplayer", I18n.get("gui.worldhandler.config.settings.permission_query")).withStyle(ChatFormatting.RED));
}
else
{
Minecraft.getInstance().gui.getChat().addMessage(Component.translatable("worldhandler.permission.refused.multiplayer", I18n.get("gui.worldhandler.config.settings.permission_query")).withStyle(ChatFormatting.RED));
}
}
else
{