chore: cleaned up Boscovicino, added mods commands

This commit is contained in:
əlemi 2023-03-04 04:03:14 +01:00
parent 1991ea5fba
commit 0dc1644c14
Signed by: alemi
GPG key ID: A4895B84D311642C
2 changed files with 52 additions and 67 deletions

View file

@ -8,18 +8,15 @@ import ftbsc.bscv.api.IModule;
import ftbsc.bscv.events.CommandsBuiltEvent; import ftbsc.bscv.events.CommandsBuiltEvent;
import ftbsc.bscv.system.Friends; import ftbsc.bscv.system.Friends;
import ftbsc.bscv.system.ModManager; import ftbsc.bscv.system.ModManager;
import net.minecraft.client.entity.player.ClientPlayerEntity;
import net.minecraft.client.gui.screen.IngameMenuScreen; import net.minecraft.client.gui.screen.IngameMenuScreen;
import net.minecraft.client.gui.widget.button.Button; import net.minecraft.client.gui.widget.button.Button;
import net.minecraft.command.CommandSource; import net.minecraft.command.CommandSource;
import net.minecraft.command.Commands;
import net.minecraft.util.text.StringTextComponent; import net.minecraft.util.text.StringTextComponent;
import net.minecraft.util.text.TranslationTextComponent; import net.minecraft.util.text.TranslationTextComponent;
import net.minecraftforge.client.event.ClientChatEvent; import net.minecraftforge.client.event.ClientChatEvent;
import net.minecraftforge.client.event.GuiScreenEvent.InitGuiEvent; import net.minecraftforge.client.event.GuiScreenEvent.InitGuiEvent;
import net.minecraftforge.common.ForgeConfigSpec; import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.world.WorldEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.ModLoadingContext; import net.minecraftforge.fml.ModLoadingContext;
import net.minecraftforge.fml.common.Mod; import net.minecraftforge.fml.common.Mod;
@ -29,7 +26,6 @@ import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import java.lang.reflect.Field;
@Mod("bscv") @Mod("bscv")
public class Boscovicino implements ICommons { public class Boscovicino implements ICommons {
@ -83,49 +79,8 @@ public class Boscovicino implements ICommons {
for (IModule m : modManager.mods) { for (IModule m : modManager.mods) {
if (m.isEnabled()) m.enable(); if (m.isEnabled()) m.enable();
} }
}
// TEMPORARY! add command to regenerate suggestions
dispatcher.register(
Commands.literal("hints")
.executes(ctx -> {
ClientPlayerEntity player = MC.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");
log("> rebuilt hints");
return 1;
} catch (NoSuchFieldException e) {
log("! no such field error");
LOGGER.error("No such field Exception while rebuilding hints");
return 0;
} catch (IllegalAccessException e) {
log("! illegal access error");
LOGGER.error("Illegal Access Exception while rebuilding hints");
return 0;
}
} else {
log("! local player is NULL");
LOGGER.error("Local player is NULL");
return 0;
}
})
);
dispatcher.register(
Commands.literal("toggle-all")
.executes(ctx -> {
for (IModule mod : modManager.mods) {
if (mod.isEnabled()) {
mod.disable();
mod.enable();
}
}
return 1;
})
);
@SubscribeEvent @SubscribeEvent
public void onCommandSuggestionsBuilt(CommandsBuiltEvent event) { public void onCommandSuggestionsBuilt(CommandsBuiltEvent event) {
for (CommandNode<CommandSource> child : this.dispatcher.getRoot().getChildren()) { for (CommandNode<CommandSource> child : this.dispatcher.getRoot().getChildren()) {
@ -171,25 +126,4 @@ public class Boscovicino implements ICommons {
screen.children.add(3, mods_btn); screen.children.add(3, mods_btn);
} }
} }
@SubscribeEvent
public void onWorldLoad(WorldEvent.Load event) {
// TEMPORARY! add command to regenerate suggestions
ClientPlayerEntity player = MC.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");
log("> rebuilt hints");
} catch (NoSuchFieldException e) {
LOGGER.error("No such field Exception while rebuilding hints");
} catch (IllegalAccessException e) {
LOGGER.error("Illegal Access Exception while rebuilding hints");
}
} else {
LOGGER.error("Local player is NULL");
}
}
} }

View file

@ -0,0 +1,51 @@
package ftbsc.bscv.commands;
import com.google.auto.service.AutoService;
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
import ftbsc.bscv.Boscovicino;
import ftbsc.bscv.api.ILoadable;
import ftbsc.bscv.api.IModule;
import net.minecraft.command.CommandSource;
import net.minecraft.command.Commands;
import static ftbsc.bscv.Boscovicino.log;
@AutoService(ILoadable.class)
public class ModCommands extends AbstractCommand {
@Override
public String getName() { return "mods"; }
public LiteralArgumentBuilder<CommandSource> register(LiteralArgumentBuilder<CommandSource> builder) {
return builder
.then(
Commands.literal("off")
.executes(ctx -> {
for (IModule mod : Boscovicino.modManager.mods) {
if (mod.isEnabled()) {
mod.disable();
}
}
return 1;
})
)
.then(
Commands.literal("re-enable") // this is to fix some jankyness in event subscriptions
.executes(ctx -> {
for (IModule mod : Boscovicino.modManager.mods) {
if (mod.isEnabled()) {
mod.disable();
mod.enable();
}
}
return 1;
})
)
.executes(ctx -> {
log("no args specified");
return 0;
});
}
}