-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Teams higher up in the list will now inherit commands of the teams under them
- Loading branch information
Showing
8 changed files
with
232 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
src/main/java/com/raxdiam/teamperms/util/PermissionCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
74
src/main/java/com/raxdiam/teamperms/util/PermissionManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
20
src/main/java/com/raxdiam/teamperms/util/PermissionTeam.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |