Skip to content

Commit

Permalink
fix crash with commands that doesn't extend CommandBase (#125)
Browse files Browse the repository at this point in the history
Signed-off-by: Lyfts <[email protected]>
  • Loading branch information
Lyfts authored Oct 27, 2024
1 parent afc2785 commit 98d666c
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 12 deletions.
6 changes: 3 additions & 3 deletions src/main/java/serverutils/command/CmdDumpPermissions.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,16 +150,16 @@ public void processCommand(ICommandSender sender, String[] args) {
commandList.add(Arrays.asList(EMPTY_ROW));

for (ICommand command : CommandUtils.getAllCommands(sender)) {
ICommandWithPermission commands = (ICommandWithPermission) command;
String node = commands.serverutilities$getPermissionNode();
ICommandWithPermission cmd = (ICommandWithPermission) command;
String node = cmd.serverutilities$getPermissionNode();
DefaultPermissionLevel defaultPermissionLevel = DefaultPermissionHandler.INSTANCE
.getDefaultPermissionLevel(node);
IChatComponent usage = CommandUtils.getTranslatedUsage(command, sender);

commandList.add(
Arrays.asList(
node,
"/" + commands.getCommandName(),
"/" + command.getCommandName(),
defaultPermissionLevel.name(),
usage.getUnformattedText()));
}
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/serverutils/mixin/Mixins.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@
public enum Mixins {

COMMAND_PERMISSIONS(new Builder("Command Permissions").addTargetedMod(VANILLA).setSide(Side.BOTH)
.setPhase(Phase.EARLY).setApplyIf(() -> ranks.enabled && ranks.command_permissions)
.addMixinClasses("minecraft.MixinCommandBase", "minecraft.MixinCommandHandler"));
.setPhase(Phase.EARLY).setApplyIf(() -> ranks.enabled && ranks.command_permissions).addMixinClasses(
"minecraft.MixinCommandBase",
"minecraft.MixinCommandHandler",
"minecraft.MixinICommand")),;

private final List<String> mixinClasses;
private final Supplier<Boolean> applyIf;
Expand Down
5 changes: 3 additions & 2 deletions src/main/java/serverutils/net/MessageRanks.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.HashSet;
import java.util.Map;

import net.minecraft.command.ICommand;
import net.minecraft.util.ChatComponentText;
import net.minecraft.util.ChatComponentTranslation;
import net.minecraft.util.EnumChatFormatting;
Expand Down Expand Up @@ -137,8 +138,8 @@ public MessageRanks(Ranks r, ForgePlayer p) {
EnumChatFormatting.BLUE + "[" + command.serverutilities$getModName() + "]\n");
ConfigBoolean val = new ConfigBoolean(level == DefaultPermissionLevel.ALL);
commandPermissions.add(node, val, val, StringUtils.FLAG_ID_PERIOD_DEFAULTS)
.setDisplayName(new ChatComponentTranslation(node))
.setInfo(name.appendSibling(CommandUtils.getTranslatedUsage(command, p.getPlayer())));
.setDisplayName(new ChatComponentTranslation(node)).setInfo(
name.appendSibling(CommandUtils.getTranslatedUsage((ICommand) command, p.getPlayer())));
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/serverutils/ranks/ICommandWithPermission.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package serverutils.ranks;

import net.minecraft.command.ICommand;
import java.util.HashMap;
import java.util.Map;

import org.jetbrains.annotations.NotNull;

public interface ICommandWithPermission extends ICommand {
public interface ICommandWithPermission {

Map<String, String> commandOwners = new HashMap<>();

Map<String, String> commandPermissions = new HashMap<>();

String serverutilities$getPermissionNode();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ public abstract class MixinCommandHandler {
String node = (container == null ? Rank.NODE_COMMAND : (Rank.NODE_COMMAND + '.' + container.getModId())) + "."
+ command.getCommandName();
ICommandWithPermission cmd = (ICommandWithPermission) command;
cmd.serverutilities$setPermissionNode(node);
cmd.serverutilities$setPermissionNode(node.toLowerCase());
cmd.serverutilities$setModName(container == null ? "Minecraft" : container.getName());
serverUtilities$registerPermissions(cmd);
}

@Unique
private DefaultPermissionLevel serverUtilities$getDefaultLevel(ICommand command) {
private DefaultPermissionLevel serverUtilities$getDefaultLevel(ICommandWithPermission command) {
if (command instanceof CommandBase cmdBase) {
return cmdBase.getRequiredPermissionLevel() > 0 ? DefaultPermissionLevel.OP : DefaultPermissionLevel.ALL;
}
Expand All @@ -79,7 +79,7 @@ public abstract class MixinCommandHandler {
if (command instanceof CommandTreeBase tree) {
for (ICommand c : tree.getSubCommands()) {
ICommandWithPermission child = (ICommandWithPermission) c;
child.serverutilities$setPermissionNode(node + '.' + child.getCommandName());
child.serverutilities$setPermissionNode(node.toLowerCase() + '.' + c.getCommandName());
child.serverutilities$setModName(command.serverutilities$getModName());
serverUtilities$registerPermissions(child);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package serverutils.mixins.early.minecraft;

import net.minecraft.command.ICommand;

import org.jetbrains.annotations.NotNull;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;

import serverutils.ranks.ICommandWithPermission;

@Mixin(ICommand.class)
public interface MixinICommand extends ICommandWithPermission {

@Shadow
String getCommandName();

@Override
default String serverutilities$getPermissionNode() {
return commandPermissions.get(getCommandName());
}

@Override
default void serverutilities$setPermissionNode(@NotNull String node) {
commandPermissions.put(getCommandName(), node);
}

@Override
default String serverutilities$getModName() {
return commandOwners.get(getCommandName());
}

@Override
default void serverutilities$setModName(@NotNull String modName) {
commandOwners.put(getCommandName(), modName);
}
}

0 comments on commit 98d666c

Please sign in to comment.