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
{

View File

@@ -6,3 +6,4 @@ public net.minecraft.commands.arguments.coordinates.LocalCoordinates m_119908_(L
public net.minecraft.advancements.critereon.MinMaxBounds$Doubles <init>(Ljava/lang/Double;Ljava/lang/Double;)V # constructor
public net.minecraft.network.chat.MutableComponent <init>(Lnet/minecraft/network/chat/ComponentContents;Ljava/util/List;Lnet/minecraft/network/chat/Style;)V # constructor
public net.minecraft.commands.arguments.item.ItemParser f_120991_ # ERROR_NO_TAGS_ALLOWED
public net.minecraft.world.level.storage.PrimaryLevelData f_78443_ # settings

View File

@@ -404,11 +404,16 @@
"key.categories.worldhandler": "World Handler",
"key.worldhandler": "World Handler öffnen",
"key.worldhandler.pos1": "Position 1 setzen",
"key.worldhandler.pos2": "Position 2 setzen",
"key.worldhandler.pos1": "Setze Position 1",
"key.worldhandler.pos2": "Setze Position 2",
"worldhandler.permission.refused": "Du hast nicht die benötigte Berechtigung, um den World Handler zu benutzen",
"worldhandler.permission.refused.change": "Ändere \"%s\" um diese Nachricht zu umgehen",
"commands.worldhandler.allow_commands.not_in_singleplayer": "Kommandos können im Mehrspieler nicht aktiviert werden",
"commands.worldhandler.allow_commands.commands_already_allowed": "Kommandos sind bereits aktiviert in dieser Welt",
"commands.worldhandler.allow_commands.success": "Kommandos sind in dieser Welt jetzt aktiviert",
"worldhandler.permission.refused": "Du hast nicht die erforderlichen Berechtigungen Kommandos zu benutzen",
"worldhandler.permission.refused.multiplayer": "Deaktiviere \"%s\" in der Konfigurationsdatei, wenn du den World Handler, mit eingeschränkter Funktionalität, trotzdem benutzen möchtest.",
"worldhandler.permission.refused.singleplayer": "Deaktiviere \"%s\" in der Konfigurationsdatei, wenn du den World Handler, mit eingeschränkter Funktionalität, trotzdem benutzen möchtest. Alternativ kannst du auch \"/worldhandler allow_commands\" ausführen, um Kommandos in dieser Welt zu aktivieren",
"worldhandler.error.gui": "Ein unerwarteter Fehler ist aufgetreten."
}

View File

@@ -407,8 +407,13 @@
"key.worldhandler.pos1": "Set Position 1",
"key.worldhandler.pos2": "Set Position 2",
"worldhandler.permission.refused": "You do not have permission to use the World Handler",
"worldhandler.permission.refused.change": "Change \"%s\" to disable this message",
"commands.worldhandler.allow_commands.not_in_singleplayer": "Cannot enable commands in a multiplayer world",
"commands.worldhandler.allow_commands.commands_already_allowed": "Commands are already allowed in this world",
"commands.worldhandler.allow_commands.success": "Commands are now enabled in this world",
"worldhandler.permission.refused": "You do not have permission to use commands",
"worldhandler.permission.refused.multiplayer": "If you want to use the World Handler regardless, with limited functionality, disable \"%s\" in the config",
"worldhandler.permission.refused.singleplayer": "If you want to use the World Handler regardless, with limited functionality, disable \"%s\" in the config. Alternatively you can run \"/worldhandler allow_commands\" to enable commands in this world",
"worldhandler.error.gui": "An unexpected error occured."
}

View File

@@ -408,8 +408,13 @@
"key.worldhandler.pos1": "Définir la position 1",
"key.worldhandler.pos2": "Définir la position 2",
"worldhandler.permission.refused": "Vous n'avez pas la permission d'utiliser World Handler",
"worldhandler.permission.refused.change": "Modifiez \"%s\" pour désactiver ce message",
"commands.worldhandler.allow_commands.not_in_singleplayer": "Cannot enable commands in a multiplayer world",
"commands.worldhandler.allow_commands.commands_already_allowed": "Commands are already allowed in this world",
"commands.worldhandler.allow_commands.success": "Commands are now enabled in this world",
"worldhandler.permission.refused": "You do not have permission to use commands",
"worldhandler.permission.refused.multiplayer": "If you want to use the World Handler regardless, with limited functionality, disable \"%s\" in the config",
"worldhandler.permission.refused.singleplayer": "If you want to use the World Handler regardless, with limited functionality, disable \"%s\" in the config. Alternatively you can run \"/worldhandler allow_commands\" to enable commands in this world",
"worldhandler.error.gui": "Une erreur inattendue s'est produite."
}

View File

@@ -407,8 +407,13 @@
"key.worldhandler.pos1": "Установить позицию 1",
"key.worldhandler.pos2": "Установить позицию 2",
"worldhandler.permission.refused": "У вас нет разрешения на использование World Handler",
"worldhandler.permission.refused.change": "Измените \"%s\", чтобы отключить это сообщение.",
"commands.worldhandler.allow_commands.not_in_singleplayer": "Cannot enable commands in a multiplayer world",
"commands.worldhandler.allow_commands.commands_already_allowed": "Commands are already allowed in this world",
"commands.worldhandler.allow_commands.success": "Commands are now enabled in this world",
"worldhandler.permission.refused": "You do not have permission to use commands",
"worldhandler.permission.refused.multiplayer": "If you want to use the World Handler regardless, with limited functionality, disable \"%s\" in the config",
"worldhandler.permission.refused.singleplayer": "If you want to use the World Handler regardless, with limited functionality, disable \"%s\" in the config. Alternatively you can run \"/worldhandler allow_commands\" to enable commands in this world",
"worldhandler.error.gui": "Произошла неожиданная ошибка."
}

View File

@@ -408,8 +408,13 @@
"key.worldhandler.pos1": "Set Position 1",
"key.worldhandler.pos2": "Set Position 2",
"worldhandler.permission.refused": "您没有权限使用 World Handler",
"worldhandler.permission.refused.change": "更改 \"%s\" 来停止显示这条信息",
"commands.worldhandler.allow_commands.not_in_singleplayer": "Cannot enable commands in a multiplayer world",
"commands.worldhandler.allow_commands.commands_already_allowed": "Commands are already allowed in this world",
"commands.worldhandler.allow_commands.success": "Commands are now enabled in this world",
"worldhandler.permission.refused": "You do not have permission to use commands",
"worldhandler.permission.refused.multiplayer": "If you want to use the World Handler regardless, with limited functionality, disable \"%s\" in the config",
"worldhandler.permission.refused.singleplayer": "If you want to use the World Handler regardless, with limited functionality, disable \"%s\" in the config. Alternatively you can run \"/worldhandler allow_commands\" to enable commands in this world",
"worldhandler.error.gui": "An unexpected error occured."
}