Add command permissions system

This commit is contained in:
DoctorMacc 2020-10-07 22:42:17 -04:00
parent 56b70927f2
commit ae3896d1d0
8 changed files with 100 additions and 24 deletions

View file

@ -31,7 +31,6 @@ import net.fabricmc.loader.api.ModContainer;
import net.minecraft.server.MinecraftServer;
import org.geysermc.connector.common.serializer.AsteriskSerializer;
import org.geysermc.connector.dump.BootstrapDumpInfo;
import org.geysermc.platform.fabric.command.ModInfo;
import java.util.ArrayList;
import java.util.List;

View file

@ -50,16 +50,19 @@ import org.geysermc.platform.fabric.command.GeyserFabricCommandExecutor;
import org.geysermc.platform.fabric.command.GeyserFabricCommandManager;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.util.Map;
import java.util.UUID;
import java.util.*;
import java.util.function.Function;
@Environment(EnvType.SERVER)
public class GeyserFabricMod implements DedicatedServerModInitializer, GeyserBootstrap {
private GeyserConnector connector;
private Path dataFolder;
private List<String> playerCommands;
private MinecraftServer server;
private GeyserFabricCommandManager geyserCommandManager;
@ -83,6 +86,8 @@ public class GeyserFabricMod implements DedicatedServerModInitializer, GeyserBoo
File configFile = FileUtils.fileOrCopiedFromResource(dataFolder.resolve("config.yml").toFile(), "config.yml",
(x) -> x.replaceAll("generateduuid", UUID.randomUUID().toString()));
this.geyserConfig = FileUtils.loadConfig(configFile, GeyserFabricConfiguration.class);
File permissionsFile = fileOrCopiedFromResource(dataFolder.resolve("permissions.yml").toFile(), "permissions.yml");
this.playerCommands = Arrays.asList(FileUtils.loadConfig(permissionsFile, GeyserFabricPermissions.class).getCommands());
} catch (IOException ex) {
LogManager.getLogger("geyser-fabric").error(LanguageUtils.getLocaleStringLog("geyser.config.failed"), ex);
ex.printStackTrace();
@ -118,11 +123,12 @@ public class GeyserFabricMod implements DedicatedServerModInitializer, GeyserBoo
// Start command building
// Set just "geyser" as the help command
LiteralArgumentBuilder<ServerCommandSource> builder = net.minecraft.server.command.CommandManager.literal("geyser")
.executes(new GeyserFabricCommandExecutor(connector, "help"));
.executes(new GeyserFabricCommandExecutor(connector, "help", !playerCommands.contains("help")));
for (Map.Entry<String, GeyserCommand> command : connector.getCommandManager().getCommands().entrySet()) {
// Register all subcommands as valid
builder.then(net.minecraft.server.command.CommandManager.literal(
command.getKey()).executes(new GeyserFabricCommandExecutor(connector, command.getKey())));
command.getKey()).executes(new GeyserFabricCommandExecutor(connector, command.getKey(),
!playerCommands.contains(command.getKey()))));
}
server.getCommandManager().getDispatcher().register(builder);
});
@ -167,4 +173,28 @@ public class GeyserFabricMod implements DedicatedServerModInitializer, GeyserBoo
public BootstrapDumpInfo getDumpInfo() {
return new GeyserFabricDumpInfo(server);
}
private File fileOrCopiedFromResource(File file, String name) throws IOException {
if (!file.exists()) {
//noinspection ResultOfMethodCallIgnored
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
InputStream input = GeyserFabricMod.class.getResourceAsStream("/" + name); // resources need leading "/" prefix
byte[] bytes = new byte[input.available()];
//noinspection ResultOfMethodCallIgnored
input.read(bytes);
for(char c : new String(bytes).toCharArray()) {
fos.write(c);
}
fos.flush();
input.close();
fos.close();
}
return file;
}
}

View file

@ -0,0 +1,42 @@
/*
* Copyright (c) 2020 GeyserMC. http://geysermc.org
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @author GeyserMC
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.platform.fabric;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;
/**
* A class outline of the permissions.yml file
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class GeyserFabricPermissions {
@Getter
@JsonProperty("commands")
private String[] commands;
}

View file

@ -23,7 +23,7 @@
* @link https://github.com/GeyserMC/Geyser
*/
package org.geysermc.platform.fabric.command;
package org.geysermc.platform.fabric;
import lombok.Getter;
import net.fabricmc.loader.api.ModContainer;

View file

@ -30,21 +30,32 @@ import com.mojang.brigadier.context.CommandContext;
import net.minecraft.server.command.ServerCommandSource;
import org.geysermc.connector.GeyserConnector;
import org.geysermc.connector.command.GeyserCommand;
import org.geysermc.connector.utils.LanguageUtils;
public class GeyserFabricCommandExecutor implements Command<ServerCommandSource> {
private final String commandName;
private final GeyserConnector connector;
/**
* Whether the command requires an OP permission level of 2 or greater
*/
private final boolean requiresPermission;
public GeyserFabricCommandExecutor(GeyserConnector connector, String commandName) {
public GeyserFabricCommandExecutor(GeyserConnector connector, String commandName, boolean requiresPermission) {
this.commandName = commandName;
this.connector = connector;
this.requiresPermission = requiresPermission;
}
@Override
public int run(CommandContext context) {
ServerCommandSource source = (ServerCommandSource) context.getSource();
getCommand(commandName).execute(new FabricCommandSender(source), new String[0]);
FabricCommandSender sender = new FabricCommandSender(source);
if (requiresPermission && !source.hasPermissionLevel(2)) {
sender.sendMessage(LanguageUtils.getLocaleStringLog("geyser.bootstrap.command.permission_fail"));
return 0;
}
getCommand(commandName).execute(sender, new String[0]);
return 0;
}

View file

@ -1,13 +0,0 @@
{
"required": true,
"minVersion": "0.8",
"package": "org.geysermc.platform.fabric.mixin",
"compatibilityLevel": "JAVA_8",
"mixins": [
],
"client": [
],
"injectors": {
"defaultRequire": 1
}
}

View file

@ -19,9 +19,6 @@
"org.geysermc.platform.fabric.GeyserFabricMod"
]
},
"mixins": [
"fabric.mixins.json"
],
"depends": {
"fabricloader": ">=0.10.1+build.209",
"fabric": "*",

View file

@ -0,0 +1,10 @@
# Uncomment any commands that you wish to be run by clients
# Commented commands require an OP permission of 2 or greater
commands:
# - dump
- help
- offhand
# - list
# - reload
# - shutdown
# - version