Skip to content

Commit

Permalink
Fix HiddenRequirement not being hidden on NeoForge
Browse files Browse the repository at this point in the history
  • Loading branch information
jpenilla committed Jul 26, 2024
1 parent 462f104 commit 55d5457
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 10 deletions.
2 changes: 2 additions & 0 deletions fabric/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ loom {
}
register("adventure-platform-fabric-testmod") {
sourceSet(testmod)
sourceSet("main", project(":test-resources"))
}
}

Expand All @@ -165,6 +166,7 @@ loom {

dependencies {
"testmodRuntimeOnly"(permissionsApiCompat.output)
"testmodRuntimeOnly"(project.project(":test-resources").sourceSets.main.get().output)
"modPermissionsApiCompat"(libs.fabric.permissionsApi) {
isTransitive = false
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package net.kyori.adventure.platform.fabric.impl.mixin.minecraft.commands;

import com.google.common.collect.Iterators;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.builder.ArgumentBuilder;
import com.mojang.brigadier.builder.RequiredArgumentBuilder;
Expand All @@ -35,6 +36,7 @@
import net.kyori.adventure.platform.fabric.impl.ServerArgumentType;
import net.kyori.adventure.platform.fabric.impl.ServerArgumentTypes;
import net.kyori.adventure.platform.fabric.impl.accessor.brigadier.builder.RequiredArgumentBuilderAccess;
import net.kyori.adventure.platform.modcommon.impl.HiddenRequirement;
import net.minecraft.commands.CommandBuildContext;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
Expand All @@ -43,6 +45,7 @@
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.ModifyVariable;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
import org.spongepowered.asm.mixin.injection.callback.LocalCapture;

Expand Down Expand Up @@ -82,4 +85,17 @@ public abstract class CommandsMixin {
}

}

/**
* Hide hidden commands from the client upon sync.
*
* <p>This injection is optional because its failure won't break any essential behavior.</p>
*
* @param itr original rootCommandSource.getChildren() iterator
* @return the filtered iterator
*/
@ModifyVariable(method = "fillUsableCommands", at = @At("STORE"), ordinal = 0, require = 0)
private Iterator<CommandNode<CommandSourceStack>> adventure$filterHiddenCommands(final Iterator<CommandNode<CommandSourceStack>> itr) {
return Iterators.filter(itr, node -> !(node.getRequirement() instanceof HiddenRequirement<CommandSourceStack>));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"api.SignedMessageMixin",
"authlib.GameProfileMixin",
"brigadier.exceptions.CommandSyntaxExceptionMixin",
"minecraft.commands.CommandsMixin",
"minecraft.commands.CommandSourceStackMixin",
"minecraft.network.FriendlyByteBufMixin",
"minecraft.network.chat.ClickEvent_ActionMixin",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* This file is part of adventure-platform-mod, licensed under the MIT License.
*
* Copyright (c) 2024 KyoriPowered
*
* 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.
*/
package net.kyori.adventure.platform.neoforge.impl;

public final class HiddenRequirementHelper {
public static final ThreadLocal<Boolean> SENDING = ThreadLocal.withInitial(() -> false);

private HiddenRequirementHelper() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* This file is part of adventure-platform-mod, licensed under the MIT License.
*
* Copyright (c) 2024 KyoriPowered
*
* 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.
*/
package net.kyori.adventure.platform.neoforge.impl.mixin.minecraft.commands;

import net.kyori.adventure.platform.neoforge.impl.HiddenRequirementHelper;
import net.minecraft.commands.Commands;
import net.minecraft.server.level.ServerPlayer;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

@Mixin(Commands.class)
abstract class CommandsMixin {
@Inject(method = "sendCommands", at = @At("HEAD"))
void injectSendStart(ServerPlayer player, CallbackInfo ci) {
HiddenRequirementHelper.SENDING.set(true);
}

@Inject(method = "sendCommands", at = @At("TAIL"))
void injectSendEnd(ServerPlayer player, CallbackInfo ci) {
HiddenRequirementHelper.SENDING.set(false);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* This file is part of adventure-platform-mod, licensed under the MIT License.
*
* Copyright (c) 2022-2024 KyoriPowered
* Copyright (c) 2024 KyoriPowered
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -21,20 +21,22 @@
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package net.kyori.adventure.platform.modcommon.impl.mixin.minecraft.commands;
package net.kyori.adventure.platform.neoforge.impl.mixin.neoforge;

import com.google.common.collect.Iterators;
import com.mojang.brigadier.tree.CommandNode;
import java.util.Iterator;
import net.kyori.adventure.platform.modcommon.impl.HiddenRequirement;
import net.kyori.adventure.platform.neoforge.impl.HiddenRequirementHelper;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.neoforged.neoforge.server.command.CommandHelper;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.ModifyVariable;

@Mixin(Commands.class)
public abstract class CommandsMixin {
@Mixin(CommandHelper.class)
abstract class CommandHelperMixin {

/**
* Hide hidden commands from the client upon sync.
*
Expand All @@ -43,8 +45,11 @@ public abstract class CommandsMixin {
* @param itr original rootCommandSource.getChildren() iterator
* @return the filtered iterator
*/
@ModifyVariable(method = "fillUsableCommands", at = @At("STORE"), ordinal = 0, require = 0)
private Iterator<CommandNode<CommandSourceStack>> adventure$filterHiddenCommands(final Iterator<CommandNode<CommandSourceStack>> itr) {
return Iterators.filter(itr, node -> !(node.getRequirement() instanceof HiddenRequirement<CommandSourceStack>));
@ModifyVariable(method = "mergeCommandNode", at = @At("STORE"), ordinal = 0, require = 0)
private static Iterator<CommandNode<CommandSourceStack>> adventure$filterHiddenCommands(final Iterator<CommandNode<CommandSourceStack>> itr) {
if (HiddenRequirementHelper.SENDING.get()) {
return Iterators.filter(itr, node -> !(node.getRequirement() instanceof HiddenRequirement<CommandSourceStack>));
}
return itr;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"package": "net.kyori.adventure.platform.neoforge.impl.mixin",
"parent": "adventure-platform-mod-shared.parent.mixins.json",
"mixins": [
"minecraft.server.players.PlayerListMixin"
"minecraft.commands.CommandsMixin",
"minecraft.server.players.PlayerListMixin",
"neoforge.CommandHelperMixin"
]
}

0 comments on commit 55d5457

Please sign in to comment.