Skip to content

Commit

Permalink
Merge pull request #188 from fixrtm/completion-of-permit-command
Browse files Browse the repository at this point in the history
add compilation for /permit
  • Loading branch information
anatawa12 authored Apr 2, 2021
2 parents e0d0eb8 + f05c46e commit bfd4d3f
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 0 deletions.
1 change: 1 addition & 0 deletions .patching-mods/ngtlib.patching-mod/modified-classes.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
jp.ngt.ngtlib.NGTCore
jp.ngt.ngtlib.command.CommandPermit
jp.ngt.ngtlib.io.NGTLog
jp.ngt.ngtlib.item.craft.RecipeManager
jp.ngt.ngtlib.item.craft.ShapedRecipes55
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/anatawa12/fixRtm/ApiBridgeImpl.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.anatawa12.fixRtm

import com.anatawa12.fixRtm.api.IPermissionManager

@Suppress("DEPRECATION")
class ApiBridgeImpl : com.anatawa12.fixRtm.api.ApiBridge.IApiBridge {
override fun getPermissions(): IPermissionManager = PermissionManager
}
1 change: 1 addition & 0 deletions src/main/java/com/anatawa12/fixRtm/FixRtm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ object FixRtm {
MinecraftForge.EVENT_BUS.register(this)
modMetadata = e.modMetadata
NetworkHandler.init()
PermissionManager.registerBuiltinPermissions()
}

@Mod.EventHandler
Expand Down
50 changes: 50 additions & 0 deletions src/main/java/com/anatawa12/fixRtm/PermissionManager.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.anatawa12.fixRtm

import com.anatawa12.fixRtm.api.IPermissionManager
import net.minecraft.command.ICommandSender
import net.minecraftforge.fml.common.Loader
import java.util.*
import jp.ngt.ngtlib.util.PermissionManager as NGTPermissionManager

object PermissionManager : IPermissionManager() {
private val permissionsBacked = mutableSetOf<String>()

val permissions get() = Collections.unmodifiableSet(permissionsBacked)

override fun registerPermission(permission: String) {
permissionsBacked.add(permission)
if (com.anatawa12.fixRtm.asm.config.MainConfig.addNegativePermissionEnabled) {
permissionsBacked.add("negative.$permission")
}
}

override fun hasPermission(player: ICommandSender, permission: String) {
require(permission in permissions) { "permission not registerd: $player" }
NGTPermissionManager.INSTANCE.hasPermission(player, permission)
}

fun registerBuiltinPermissions() {
registerFixRtmPermissions()
registerRTMPermissions()
if (Loader.instance().activeModList.any { it.modId == "mcte" })
registerMCTEPermissions()
}

private fun registerFixRtmPermissions() {
registerPermission("fixrtm.all_permit")
}

private fun registerRTMPermissions() {
registerPermission("driveTrain")
registerPermission("editVehicle")
registerPermission("useCannon")
registerPermission("useRazer")
registerPermission("useGun")
registerPermission("editRail")
registerPermission("changeModel")
}

private fun registerMCTEPermissions() {
registerPermission("useEditor")
}
}
27 changes: 27 additions & 0 deletions src/main/java/com/anatawa12/fixRtm/api/ApiBridge.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.anatawa12.fixRtm.api;

/**
* internal bridge. please do not use
*/
@SuppressWarnings("DeprecatedIsStillUsed")
@Deprecated
public class ApiBridge {
private ApiBridge() {
}

@Deprecated
public interface IApiBridge {
IPermissionManager getPermissions();
}

@Deprecated
static IApiBridge INSTANCE;

static {
try {
INSTANCE = (IApiBridge) Class.forName("com.anatawa12.fixRtm.ApiBridgeImpl").newInstance();
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
throw new RuntimeException("api bridge init failed", e);
}
}
}
16 changes: 16 additions & 0 deletions src/main/java/com/anatawa12/fixRtm/api/IPermissionManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.anatawa12.fixRtm.api;

import net.minecraft.command.ICommandSender;

/**
* the endpoint for permissions.
* This is not stable for implementation, just for use
*/
public abstract class IPermissionManager {
@SuppressWarnings("deprecation")
IPermissionManager INSTANCE = ApiBridge.INSTANCE.getPermissions();

public abstract void registerPermission(String permission);

public abstract void hasPermission(ICommandSender player, String permission);
}
22 changes: 22 additions & 0 deletions src/main/java/com/anatawa12/fixRtm/ngtlib/command/CommandPermit.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.anatawa12.fixRtm.ngtlib.command

import com.anatawa12.fixRtm.PermissionManager
import net.minecraft.command.CommandBase
import net.minecraft.server.MinecraftServer

fun getTabCompletions(
server: MinecraftServer,
args: Array<String>,
): List<String> = when (args.size) {
1 -> CommandBase.getListOfStringsMatchingLastWord(args, "list", "myname", "add", "remove")
2 -> when (args[0]) {
"add", "remove" -> CommandBase.getListOfStringsMatchingLastWord(args,
server.onlinePlayerNames.toMutableList().apply { add("-all") })
else -> emptyList()
}
3 -> when (args[0]) {
"add", "remove" -> CommandBase.getListOfStringsMatchingLastWord(args, PermissionManager.permissions)
else -> emptyList()
}
else -> emptyList()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--- a/jp/ngt/ngtlib/command/CommandPermit.java
+++ b/jp/ngt/ngtlib/command/CommandPermit.java
@@ -4,11 +4,16 @@
import jp.ngt.ngtlib.util.PermissionManager;
import net.minecraft.command.CommandBase;
import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.server.MinecraftServer;
+import net.minecraft.util.math.BlockPos;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;

+import java.util.List;
+
public class CommandPermit extends CommandBase {
public int getRequiredPermissionLevel() {
return 3;
}

@@ -50,6 +55,13 @@
NGTLog.sendChatMessage(sender, "/permit add <player or -all> <category>");
NGTLog.sendChatMessage(sender, "/permit remove <player or -all> <category>");
NGTLog.sendChatMessage(sender, "/permit list");
NGTLog.sendChatMessage(sender, "/permit myname");
}
+
+ @NotNull
+ @Override
+ public List<String> getTabCompletions(@NotNull MinecraftServer server, @NotNull ICommandSender sender,
+ @NotNull String[] args, @Nullable BlockPos targetPos) {
+ return com.anatawa12.fixRtm.ngtlib.command.CommandPermitKt.getTabCompletions(server, args);
+ }
}

0 comments on commit bfd4d3f

Please sign in to comment.