Skip to content

Commit

Permalink
Merge pull request #9 from Gu-ZT/main
Browse files Browse the repository at this point in the history
changed: If it is an Owner in single-player mode, all command is allowed
  • Loading branch information
SkyDynamic authored Apr 30, 2024
2 parents 3caf64a + 56a7860 commit ba4bbb0
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

import com.mojang.brigadier.arguments.StringArgumentType;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import org.jetbrains.annotations.NotNull;

import java.text.SimpleDateFormat;

Expand All @@ -14,7 +17,7 @@ public class MakeCommand {

private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd-HHmmss");

public static LiteralArgumentBuilder<ServerCommandSource> makeCommand = literal("make").requires(me -> me.hasPermissionLevel(2))
public static LiteralArgumentBuilder<ServerCommandSource> makeCommand = literal("make").requires(QuickBackupMultiCommand::checkPermission)
.executes(it -> makeSaveBackup(it.getSource(), dateFormat.format(System.currentTimeMillis()), ""))
.then(CommandManager.argument("name", StringArgumentType.string())
.executes(it -> makeSaveBackup(it.getSource(), StringArgumentType.getString(it, "name"), ""))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.ArrayList;
Expand Down Expand Up @@ -54,11 +55,11 @@ public static void RegisterCommand(CommandDispatcher<ServerCommandSource> dispat

.then(makeCommand)

.then(literal("back").requires(me -> me.hasPermissionLevel(2))
.then(literal("back").requires(QuickBackupMultiCommand::checkPermission)
.then(CommandManager.argument("name", StringArgumentType.string())
.executes(it -> restoreSaveBackup(it.getSource(), StringArgumentType.getString(it, "name")))))

.then(literal("confirm").requires(me -> me.hasPermissionLevel(2))
.then(literal("confirm").requires(QuickBackupMultiCommand::checkPermission)
.executes(it -> {
try {
executeRestore(it.getSource());
Expand All @@ -68,10 +69,10 @@ public static void RegisterCommand(CommandDispatcher<ServerCommandSource> dispat
return 0;
}))

.then(literal("cancel").requires(me -> me.hasPermissionLevel(2))
.then(literal("cancel").requires(QuickBackupMultiCommand::checkPermission)
.executes(it -> cancelRestore(it.getSource())))

.then(literal("delete").requires(me -> me.hasPermissionLevel(2))
.then(literal("delete").requires(QuickBackupMultiCommand::checkPermission)
.then(CommandManager.argument("name", StringArgumentType.string())
.executes(it -> deleteSaveBackup(it.getSource(), StringArgumentType.getString(it, "name")))))

Expand Down Expand Up @@ -201,4 +202,15 @@ private static int listSaveBackups(ServerCommandSource commandSource, int page)
Messenger.sendMessage(commandSource, resultText);
return 1;
}

public static boolean checkPermission(@NotNull ServerCommandSource source) {
//
boolean flag = source.hasPermissionLevel(2);
ServerPlayerEntity player;
MinecraftServer server;
if (!flag && (server = source.getServer()).isSingleplayer() && source.isExecutedByPlayer() && (player = source.getPlayer()) != null) {
flag = server.isHost(player.getGameProfile());
}
return flag;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@

public class SettingCommand {

public static LiteralArgumentBuilder<ServerCommandSource> settingCommand = literal("setting").requires(me -> me.hasPermissionLevel(2))
public static LiteralArgumentBuilder<ServerCommandSource> settingCommand = literal("setting").requires(QuickBackupMultiCommand::checkPermission)
.then(literal("lang")
.then(literal("get").executes(it -> getLang(it.getSource())))
.then(literal("set").requires(me -> me.hasPermissionLevel(2))
.then(literal("set").requires(QuickBackupMultiCommand::checkPermission)
.then(CommandManager.argument("lang", StringArgumentType.string())
.suggests(new LangSuggestionProvider())
.executes(it -> setLang(it.getSource(), StringArgumentType.getString(it, "lang"))))))
Expand Down

0 comments on commit ba4bbb0

Please sign in to comment.