Skip to content

Commit

Permalink
Fixed inherency
Browse files Browse the repository at this point in the history
Teams higher up in the list will now inherit commands of the teams under them
  • Loading branch information
Raxdiam committed Jun 21, 2020
1 parent d7e93d2 commit e1c20cb
Show file tree
Hide file tree
Showing 8 changed files with 232 additions and 48 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ org.gradle.jvmargs=-Xmx1G
loader_version=0.8.8+build.202

# Mod Properties
mod_version = 1.0.0-1.16-rc1
mod_version = 1.1.0-1.16-rc1
maven_group = com.raxdiam
archives_base_name = teamperms

Expand Down
47 changes: 12 additions & 35 deletions src/main/java/com/raxdiam/teamperms/TeamPerms.java
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
package com.raxdiam.teamperms;

import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.tree.RootCommandNode;
import com.raxdiam.teamperms.config.Config;
import com.raxdiam.teamperms.events.ScoreboardCallbacks;
import com.raxdiam.teamperms.events.TeamPlayerCallback;
import com.raxdiam.teamperms.util.PermissionManager;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.server.ServerStartCallback;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.PlayerManager;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.CommandSource;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.function.Predicate;

public class TeamPerms implements ModInitializer {
public static final Config CONFIG = Config.load();
public static final String MOD_NAME = "TeamPerms";

private static final Logger LOGGER = LogManager.getLogger(MOD_NAME);

private static CommandManager COMMAND_MANAGER;
private static PlayerManager PLAYER_MANAGER;
public static CommandManager COMMAND_MANAGER;
public static PlayerManager PLAYER_MANAGER;
public static RootCommandNode<ServerCommandSource> ROOT_NODE;

public static PermissionManager PERM_MANAGER;

@Override
public void onInitialize() {
Expand All @@ -33,10 +34,10 @@ public void onInitialize() {
private void onServerStart(MinecraftServer minecraftServer) {
COMMAND_MANAGER = minecraftServer.getCommandManager();
PLAYER_MANAGER = minecraftServer.getPlayerManager();
var rootNode = COMMAND_MANAGER.getDispatcher().getRoot();
CONFIG.teamCommands.forEach((team, cmds) -> cmds.forEach(cmd -> {
CommandNodeHelper.changeRequirement(rootNode, cmd, createTeamPredicate(team));
}));
ROOT_NODE = COMMAND_MANAGER.getDispatcher().getRoot();

PERM_MANAGER = new PermissionManager(CONFIG.teamCommands, minecraftServer);
PERM_MANAGER.apply();

var leaveJoinCallback = (TeamPlayerCallback) (playerName, team) -> {
safeSendCommandTree(playerName, PLAYER_MANAGER, COMMAND_MANAGER);
Expand All @@ -58,28 +59,4 @@ private static void safeSendCommandTree(String playerName, PlayerManager playerM
var player = playerManager.getPlayer(playerName);
if (player != null) commandManager.sendCommandTree(player);
}

private static Predicate<?> createTeamPredicate(String teamName) {
return o -> {
if (teamName.equalsIgnoreCase("Default")) return true;
if (((CommandSource) o).hasPermissionLevel(4)) return true;
if (o instanceof ServerCommandSource) {
var s = (ServerCommandSource) o;

ServerPlayerEntity player;
try {
player = s.getPlayer();
} catch (CommandSyntaxException e) {
return false;
}
if (player == null) return false;

var team = player.getScoreboardTeam();
if (team == null) return false;

return team.getName().equalsIgnoreCase(teamName);
}
return false;
};
}
}
10 changes: 4 additions & 6 deletions src/main/java/com/raxdiam/teamperms/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,14 @@

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.raxdiam.teamperms.util.TeamMap;
import com.raxdiam.teamperms.TeamPerms;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class Config {
private static final String CONFIG_PATH = "config/" + TeamPerms.MOD_NAME + ".json";
Expand All @@ -29,7 +27,7 @@ private Config(boolean useDefault) {

public short configVersion = 1;

public LinkedHashMap<String, List<String>> teamCommands;
public TeamMap teamCommands;

public void save() {
save(this);
Expand Down Expand Up @@ -68,8 +66,8 @@ private static void save(Config config) {
}
}

private static LinkedHashMap<String, List<String>> createDefaultTeamMap() {
var map = new LinkedHashMap<String, List<String>>();
private static TeamMap createDefaultTeamMap() {
var map = new TeamMap();
map.put("Admin", List.of(
"advancement",
"attribute",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* https://github.com/vacla/Watson
*/

package com.raxdiam.teamperms;
package com.raxdiam.teamperms.util;

import java.lang.reflect.Field;
import java.util.Map;
Expand Down Expand Up @@ -40,11 +40,15 @@ public class CommandNodeHelper
public static void changeRequirement(CommandNode<?> node, String command, Predicate<?> predicate) {
var child = node.getChild(command);
if (child != null) {
try {
REQUIREMENT.set(child, predicate);
} catch (ReflectiveOperationException e) {
throw new RuntimeException("Error changing requirement for command: " + command, e);
}
changeRequirement(child, predicate);
}
}

public static void changeRequirement(CommandNode<?> node, Predicate<?> predicate) {
try {
REQUIREMENT.set(node, predicate);
} catch (ReflectiveOperationException e) {
throw new RuntimeException("Error changing requirement for command: " + node.getName(), e);
}
}

Expand Down
68 changes: 68 additions & 0 deletions src/main/java/com/raxdiam/teamperms/util/PermissionCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.raxdiam.teamperms.util;

import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.raxdiam.teamperms.TeamPerms;
import net.minecraft.server.command.CommandSource;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.server.network.ServerPlayerEntity;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.function.Predicate;

public class PermissionCommand {
private static final Logger LOGGER = LogManager.getLogger(TeamPerms.MOD_NAME);

private final PermissionManager manager;
private final PermissionTeam permTeam;
private final String name;
private final int level;
private final Predicate<?> requirement;

public PermissionCommand(PermissionManager manager, PermissionTeam permTeam, String name, int level) {
this.manager = manager;
this.permTeam = permTeam;
this.name = name;
this.level = level;
this.requirement = createRequirement();
}

public PermissionTeam getPermTeam() {
return this.permTeam;
}

public String getName() {
return this.name;
}

public int getLevel() {
return this.level;
}

public Predicate<?> getRequirement() {
return this.requirement;
}

private Predicate<?> createRequirement() {
return o -> {
if (((CommandSource) o).hasPermissionLevel(4)) return true;
if (o instanceof ServerCommandSource) {
var s = (ServerCommandSource) o;

ServerPlayerEntity player;
try {
player = s.getPlayer();
} catch (CommandSyntaxException e) {
return false;
}
if (player == null) return false;

var team = player.getScoreboardTeam();
var level = team == null ? PermissionManager.MAX_PERM_LEVEL : manager.getPermTeam(team.getName()).level;

return level <= this.level;
}
return false;
};
}
}
74 changes: 74 additions & 0 deletions src/main/java/com/raxdiam/teamperms/util/PermissionManager.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package com.raxdiam.teamperms.util;

import com.mojang.brigadier.tree.RootCommandNode;
import com.raxdiam.teamperms.TeamPerms;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.PlayerManager;
import net.minecraft.server.command.CommandManager;
import net.minecraft.server.command.ServerCommandSource;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.LinkedHashMap;
import java.util.LinkedList;

public class PermissionManager {
private static final Logger LOGGER = LogManager.getLogger(TeamPerms.MOD_NAME);

public static int MAX_PERM_LEVEL;

private final CommandManager commandManager;
private final PlayerManager playerManager;
private final RootCommandNode<ServerCommandSource> rootNode;
private final LinkedList<PermissionTeam> permTeams;

public final LinkedHashMap<String, PermissionCommand> commands;

public PermissionManager(TeamMap teamMap, MinecraftServer minecraftServer) {
MAX_PERM_LEVEL = teamMap.size() - 1;
commandManager = minecraftServer.getCommandManager();
playerManager = minecraftServer.getPlayerManager();
rootNode = commandManager.getDispatcher().getRoot();
permTeams = new LinkedList<>();
commands = new LinkedHashMap<>();

createPermTeams(teamMap);
createCommands();

for (var team : permTeams) {
LOGGER.info("Team: " + team.name + ", Level: " + team.level);
}

}

public void apply() {
for (var node : rootNode.getChildren()) {
var perm = commands.get(node.getName());
if (perm == null) continue;

CommandNodeHelper.changeRequirement(node, perm.getRequirement());
}
}

public PermissionTeam getPermTeam(String name) {
for (var team : permTeams) {
if (team.name.equalsIgnoreCase(name)) return team;
}
return null;
}

private void createPermTeams(TeamMap teamMap) {
for (var team : teamMap.entrySet()) {
var name = team.getKey();
permTeams.add(new PermissionTeam(this, name, teamMap.indexOf(name), team.getValue()));
}
}

private void createCommands() {
for (var permTeam : permTeams) {
for (var command : permTeam.commands) {
commands.put(command.getName(), command);
}
}
}
}
20 changes: 20 additions & 0 deletions src/main/java/com/raxdiam/teamperms/util/PermissionTeam.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.raxdiam.teamperms.util;

import java.util.ArrayList;
import java.util.List;

public class PermissionTeam {
public String name;
public List<PermissionCommand> commands;
public int level;

public PermissionTeam(PermissionManager manager, String name, int level, List<String> commands) {
this.name = name;
this.level = level;
//this.commands = commands;
this.commands = new ArrayList<>();
for (var command : commands) {
this.commands.add(new PermissionCommand(manager, this, command, level));
}
}
}
43 changes: 43 additions & 0 deletions src/main/java/com/raxdiam/teamperms/util/TeamMap.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.raxdiam.teamperms.util;

import java.util.*;

public class TeamMap extends LinkedHashMap<String, List<String>> {
private final ArrayList<String> indicies = new ArrayList<>();

@Override
public boolean containsKey(Object key) {
return super.containsKey(((String) key).toLowerCase());
}

@Override
public List<String> get(Object key) {
return super.get(((String) key).toLowerCase());
}

public List<String> getAtIndex(int index) {
return super.get(indicies.get(index));
}

public String getKeyAtIndex(int index) {
return indicies.get(index);
}

public int indexOf(String key) {
return indicies.indexOf(key);
}

@Override
public List<String> put(String key, List<String> value) {
var newKey = key.toLowerCase();
if (!super.containsKey(newKey)) indicies.add(newKey);
return super.put(newKey, value);
}

@Override
public List<String> remove(Object key) {
var newKey = ((String) key).toLowerCase();
indicies.remove(newKey);
return super.remove(newKey);
}
}

0 comments on commit e1c20cb

Please sign in to comment.