fix: very cheap and temp solution to missing hints

Added a command, /rebuild_hints, which overwrites completer commands
list using reflections and hardcoded obfuscated name. Classy! But it
works without patches
This commit is contained in:
əlemi 2023-01-28 23:41:14 +01:00
parent 732fa8e4aa
commit 6a9df35f40
No known key found for this signature in database
GPG key ID: BBCBFE5D7244634E

View file

@ -3,7 +3,9 @@ package co.fantabos.bscv;
import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
import net.minecraft.client.Minecraft;
import net.minecraft.client.entity.player.ClientPlayerEntity;
import net.minecraft.command.CommandSource;
import net.minecraft.command.Commands;
import net.minecraftforge.client.event.ClientChatEvent;
import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.common.MinecraftForge;
@ -26,6 +28,7 @@ import org.apache.logging.log4j.Logger;
import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
@ -70,6 +73,32 @@ public class BoSCoVicino {
// register config handler
ModLoadingContext.get().registerConfig(Type.CLIENT, spec, "bscv.toml");
// TEMPORARY! add command to regenerate suggestions
dispatcher.register(
Commands.literal("rebuild_hints")
.executes(ctx -> {
ClientPlayerEntity player = BoSCoVicino.minecraft.player;
if (player != null) {
try {
Field commands = player.connection.getClass().getDeclaredField("field_195517_n"); // "commands", it's obfuscated
commands.setAccessible(true);
commands.set(player.connection, this.dispatcher);
LOGGER.info("Rebuild HINTS");
return 1;
} catch (NoSuchFieldException e) {
LOGGER.error("No such field Exception while rebuilding hints");
return 0;
} catch (IllegalAccessException e) {
LOGGER.error("Illegal Access Exception while rebuilding hints");
return 0;
}
} else {
LOGGER.error("Local player is NULL");
return 0;
}
})
);
// Register ourselves for server and other game events we are interested in
MinecraftForge.EVENT_BUS.register(this);
}
@ -105,7 +134,7 @@ public class BoSCoVicino {
@SubscribeEvent
public void onClientChatEvent(ClientChatEvent event) {
if (event.getMessage().startsWith("/")) {
CommandSource source = BoSCoVicino.minecraft.player.createCommandSourceStack();
CommandSource source = BoSCoVicino.minecraft.player.createCommandSourceStack(); // TODO player could be NULL
try {
LOGGER.info(String.format("Running command %s", event.getMessage()));
this.dispatcher.execute(event.getMessage().substring(1), source);