Skip to content

Commit

Permalink
Replace command override with mixins (#124)
Browse files Browse the repository at this point in the history
* replace janky command override with mixins

Signed-off-by: Lyfts <127234178+Lyfts@users.noreply.github.com>

* show which mod adds commands in command overview

Signed-off-by: Lyfts <127234178+Lyfts@users.noreply.github.com>

* update deps

Signed-off-by: Lyfts <127234178+Lyfts@users.noreply.github.com>

* update .gitignore to allow mixin configs

Signed-off-by: Lyfts <127234178+Lyfts@users.noreply.github.com>

* prevent crash in obf when initiating config in coremod due to loading mc classes too early (it wasn't used anyways)

Signed-off-by: Lyfts <127234178+Lyfts@users.noreply.github.com>

---------

Signed-off-by: Lyfts <127234178+Lyfts@users.noreply.github.com>
  • Loading branch information
Lyfts authored Oct 23, 2024
1 parent 4150f04 commit afc2785
Show file tree
Hide file tree
Showing 27 changed files with 525 additions and 410 deletions.
9 changes: 8 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.gradle
.settings
/.idea/
/.vscode/
/run/
/build/
/eclipse/
Expand All @@ -25,7 +26,13 @@ whitelist.json
*.iml
*.ipr
*.iws
src/main/resources/mixins.*.json
src/main/resources/mixins.*([!.]).json
*.bat
*.DS_Store
!gradlew.bat
.factorypath
addon.local.gradle
addon.local.gradle.kts
addon.late.local.gradle
addon.late.local.gradle.kts
layout.json
9 changes: 5 additions & 4 deletions dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@

dependencies {

api("com.github.GTNewHorizons:GTNHLib:0.5.8:dev")
compileOnly("com.github.GTNewHorizons:NotEnoughItems:2.6.36-GTNH:dev")
compileOnly("com.github.GTNewHorizons:EnderIO:2.8.17:dev")
compileOnly("com.github.GTNewHorizons:Navigator:1.0.12:dev")
api("com.github.GTNewHorizons:GTNHLib:0.5.17:dev")
compileOnly("com.github.GTNewHorizons:NotEnoughItems:2.6.44-GTNH:dev")
compileOnly("com.github.GTNewHorizons:EnderIO:2.8.20:dev")
compileOnly("com.github.GTNewHorizons:Navigator:1.0.15:dev")
compileOnly('org.jetbrains:annotations:25.0.0')

runtimeOnlyNonPublishable("com.github.GTNewHorizons:waila:1.8.1:dev")
}
10 changes: 5 additions & 5 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ apiPackage =
accessTransformersFile = serverutil_at.cfg

# Provides setup for Mixins if enabled. If you don't know what mixins are: Keep it disabled!
usesMixins = false
usesMixins = true

# Set to a non-empty string to configure mixins in a separate source set under src/VALUE, instead of src/main.
# This can speed up compile times thanks to not running the mixin annotation processor on all input sources.
# Mixin classes will have access to "main" classes, but not the other way around.
separateMixinSourceSet =
separateMixinSourceSet = mixins

# Adds some debug arguments like verbose output and class export.
usesMixinDebug = false
Expand All @@ -88,12 +88,12 @@ usesMixinDebug = false
mixinPlugin =

# Specify the package that contains all of your Mixins. You may only place Mixins in this package or the build will fail!
mixinsPackage =
mixinsPackage = mixins

# Specify the core mod entry class if you use a core mod. This class must implement IFMLLoadingPlugin!
# This parameter is for legacy compatibility only
# Example value: (coreModClass = asm.FMLPlugin) + (modGroup = com.myname.mymodid) -> com.myname.mymodid.asm.FMLPlugin
coreModClass =
coreModClass = core.ServerUtilitiesCore

# If your project is only a consolidation of mixins or a core mod and does NOT contain a 'normal' mod ( = some class
# that is annotated with @Mod) you want this to be true. When in doubt: leave it on false!
Expand All @@ -104,7 +104,7 @@ forceEnableMixins = false

# If enabled, you may use 'shadowCompile' for dependencies. They will be integrated into your jar. It is your
# responsibility to check the license and request permission for distribution if required.
usesShadowedDependencies = true
usesShadowedDependencies = false

# If disabled, won't remove unused classes from shadowed dependencies. Some libraries use reflection to access
# their own classes, making the minimization unreliable.
Expand Down
65 changes: 0 additions & 65 deletions src/main/java/serverutils/ServerUtilitiesCommon.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,14 @@
import static serverutils.ServerUtilitiesConfig.tasks;
import static serverutils.ServerUtilitiesConfig.world;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.function.Function;

import net.minecraft.command.ICommand;
import net.minecraft.command.ServerCommandManager;
import net.minecraft.launchwrapper.Launch;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ChatComponentText;
Expand All @@ -27,12 +23,8 @@
import net.minecraftforge.common.ForgeChunkManager;
import net.minecraftforge.common.MinecraftForge;

import com.gtnewhorizon.gtnhlib.config.ConfigException;
import com.gtnewhorizon.gtnhlib.config.ConfigurationManager;

import cpw.mods.fml.common.FMLCommonHandler;
import cpw.mods.fml.common.Loader;
import cpw.mods.fml.common.ModContainer;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
Expand All @@ -55,7 +47,6 @@
import serverutils.handlers.ServerUtilitiesRegistryEventHandler;
import serverutils.handlers.ServerUtilitiesServerEventHandler;
import serverutils.handlers.ServerUtilitiesWorldEventHandler;
import serverutils.lib.ATHelper;
import serverutils.lib.EnumReloadType;
import serverutils.lib.OtherMods;
import serverutils.lib.config.ConfigBoolean;
Expand Down Expand Up @@ -90,14 +81,10 @@
import serverutils.lib.icon.Color4I;
import serverutils.lib.math.Ticks;
import serverutils.lib.net.MessageToClient;
import serverutils.lib.util.CommonUtils;
import serverutils.lib.util.InvUtils;
import serverutils.lib.util.ServerUtils;
import serverutils.lib.util.permission.PermissionAPI;
import serverutils.net.ServerUtilitiesNetHandler;
import serverutils.ranks.CommandOverride;
import serverutils.ranks.Rank;
import serverutils.ranks.Ranks;
import serverutils.ranks.ServerUtilitiesPermissionHandler;
import serverutils.task.CleanupTask;
import serverutils.task.DecayTask;
Expand Down Expand Up @@ -134,14 +121,6 @@ public EditingConfig(ConfigGroup g, IConfigCallback c) {
}
}

static {
try {
ConfigurationManager.registerConfig(ServerUtilitiesConfig.class);
} catch (ConfigException e) {
throw new RuntimeException(e);
}
}

public void preInit(FMLPreInitializationEvent event) {
if ((Boolean) Launch.blackboard.get("fml.deobfuscatedEnvironment")) {
ServerUtilities.LOGGER.info("Loading ServerUtilities in development environment");
Expand Down Expand Up @@ -288,50 +267,6 @@ public void onServerStarting(FMLServerStartingEvent event) {
public void onServerStarted(FMLServerStartedEvent event) {
Universe.onServerStarted(event);
registerTasks();

if (Ranks.isActive()) {
Ranks.INSTANCE.commands.clear();

boolean bukkitLoaded = CommonUtils.getClassExists("thermos.ThermosRemapper")
|| CommonUtils.getClassExists("org.ultramine.server.UltraminePlugin")
|| CommonUtils.getClassExists("org.bukkit.World");

if (bukkitLoaded) {
ServerUtilities.LOGGER.warn(
"Thermos/Ultramine detected, command overriding has been disabled. If there are any issues with Server Utilities ranks or permissions, please test them without those mods!");
}

if (!ranks.override_commands || bukkitLoaded) {
return;
}

ServerCommandManager manager = (ServerCommandManager) Ranks.INSTANCE.universe.server.getCommandManager();
List<ICommand> commands = new ArrayList<>(ATHelper.getCommandSet(manager));
ATHelper.getCommandSet(manager).clear();
manager.getCommands().clear();

for (ICommand command : commands) {
ModContainer container = CommonUtils.getModContainerForClass(command.getClass());
manager.registerCommand(
CommandOverride.create(
command,
container == null ? Rank.NODE_COMMAND
: (Rank.NODE_COMMAND + '.' + container.getModId()),
container));
}

List<CommandOverride> ocommands = new ArrayList<>(Ranks.INSTANCE.commands.values());
ocommands.sort((o1, o2) -> {
int i = Boolean.compare(o1.modContainer != null, o2.modContainer != null);
return i == 0 ? o1.node.compareTo(o2.node) : i;
});

for (CommandOverride c : ocommands) {
Ranks.INSTANCE.commands.put(c.node, c);
}

ServerUtilities.LOGGER.info("Overridden {} commands", manager.getCommands().size());
}
}

public void onServerStopping(FMLServerStoppingEvent event) {
Expand Down
30 changes: 4 additions & 26 deletions src/main/java/serverutils/ServerUtilitiesConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,42 +22,20 @@
import serverutils.lib.util.ServerUtils;

@Config(modid = ServerUtilities.MOD_ID, category = "", configSubDirectory = "../serverutilities/")
@Config.RequiresWorldRestart
public class ServerUtilitiesConfig {

@Config.RequiresWorldRestart
public static final AutoShutdown auto_shutdown = new AutoShutdown();

@Config.RequiresWorldRestart
public static final AFK afk = new AFK();

@Config.RequiresWorldRestart
public static final Chat chat = new Chat();

@Config.RequiresWorldRestart
public static final Commands commands = new Commands();

@Config.RequiresWorldRestart
public static final Login login = new Login();

@Config.RequiresWorldRestart
public static final RanksConfig ranks = new RanksConfig();

@Config.RequiresWorldRestart
public static final WorldConfig world = new WorldConfig();

@Config.RequiresWorldRestart
public static final Debugging debugging = new Debugging();

@Config.RequiresWorldRestart
public static final Backups backups = new Backups();

@Config.RequiresWorldRestart
public static final General general = new General();

@Config.RequiresWorldRestart
public static final Teams teams = new Teams();

@Config.RequiresWorldRestart
public static final Tasks tasks = new Tasks();

public static class General {
Expand Down Expand Up @@ -401,17 +379,17 @@ public List<ItemStack> getStartingItems() {

public static class RanksConfig {

@Config.Comment("Enables ranks and adds command.x permissions and allows ranks to control them.")
@Config.Comment("Enables Ranks.")
@Config.DefaultBoolean(true)
public boolean enabled;

@Config.Comment("Adds chat colors/rank-specific syntax.")
@Config.DefaultBoolean(true)
public boolean override_chat;

@Config.Comment("Allow to configure commands with ranks. Disable this if you want to use other permission mod for that.")
@Config.Comment("Add permissions for commands and allow them to be controlled by ranks.")
@Config.DefaultBoolean(true)
public boolean override_commands;
public boolean command_permissions;
}

public static class WorldConfig {
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/serverutils/ServerUtilitiesPermissions.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -38,6 +39,8 @@
public class ServerUtilitiesPermissions {

public static final ServerUtilitiesPermissions INST = new ServerUtilitiesPermissions();

public static final Set<NodeEntry> earlyPermissions = new HashSet<>();
// Display //
public static final String DISPLAY_ADMIN_INFO = "serverutilities.display.admin_info";

Expand Down Expand Up @@ -131,6 +134,10 @@ public void registerRankConfigHandler(RegisterRankConfigHandlerEvent event) {
}

public static void registerPermissions() {
for (NodeEntry entry : earlyPermissions) {
PermissionAPI.registerNode(entry.node, entry.level, entry.desc);
}
earlyPermissions.clear();
PermissionAPI.registerNode(CHAT_SPEAK, DefaultPermissionLevel.ALL, "Controls if player is muted or not");
PermissionAPI.registerNode(
CHAT_FORMATTING,
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/serverutils/aurora/mc/CommandListPage.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import serverutils.aurora.page.HTTPWebPage;
import serverutils.aurora.tag.Style;
import serverutils.aurora.tag.Tag;
import serverutils.ranks.CommandOverride;
import serverutils.ranks.Ranks;
import serverutils.lib.command.CommandUtils;
import serverutils.ranks.ICommandWithPermission;

public class CommandListPage extends HTTPWebPage {

Expand Down Expand Up @@ -54,9 +54,9 @@ public void body(Tag body) {
firstRow.th().text("Available command nodes");
firstRow.th().text("Usage");

for (CommandOverride c : Ranks.INSTANCE.commands.values()) {
for (ICommandWithPermission cmd : CommandUtils.getPermissionCommands(server)) {
Tag row = nodeTable.tr();
row.td().paired("code", c.node);
row.td().paired("code", cmd.serverutilities$getPermissionNode());
}
}
}
Loading

0 comments on commit afc2785

Please sign in to comment.