Skip to content

Commit

Permalink
Cleanup & organization
Browse files Browse the repository at this point in the history
  • Loading branch information
Raxdiam committed Jun 21, 2020
1 parent e1c20cb commit 25e5aa1
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 50 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.1.0-1.16-rc1
mod_version = 1.1.1-1.16-rc1
maven_group = com.raxdiam
archives_base_name = teamperms

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

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.ServerCommandSource;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand All @@ -20,10 +14,6 @@ public class TeamPerms implements ModInitializer {

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

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

public static PermissionManager PERM_MANAGER;

@Override
Expand All @@ -32,31 +22,7 @@ public void onInitialize() {
}

private void onServerStart(MinecraftServer minecraftServer) {
COMMAND_MANAGER = minecraftServer.getCommandManager();
PLAYER_MANAGER = minecraftServer.getPlayerManager();
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);
return false;
};

ScoreboardCallbacks.TEAM_JOIN.register(leaveJoinCallback);
ScoreboardCallbacks.TEAM_LEAVE.register(leaveJoinCallback);
ScoreboardCallbacks.TEAM_REMOVE_AFTER.register(team -> {
var players = team.getPlayerList();
for (var playerName : players) {
safeSendCommandTree(playerName, PLAYER_MANAGER, COMMAND_MANAGER);
}
return false;
});
}

private static void safeSendCommandTree(String playerName, PlayerManager playerManager, CommandManager commandManager) {
var player = playerManager.getPlayer(playerName);
if (player != null) commandManager.sendCommandTree(player);
}
}
15 changes: 5 additions & 10 deletions src/main/java/com/raxdiam/teamperms/mixin/ScoreboardMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,26 @@
public class ScoreboardMixin {
@Inject(method = "updateScoreboardTeam", at = @At("TAIL"), cancellable = true)
private void onTeamUpdate(Team team, CallbackInfo info) {
if (ScoreboardCallbacks.TEAM_UPDATE.invoker().handle(team))
info.cancel();
if (ScoreboardCallbacks.TEAM_UPDATE.invoker().handle(team)) info.cancel();
}

@Inject(method = "addPlayerToTeam", at = @At("TAIL"), cancellable = true)
private void onTeamJoin(String playerName, Team team, CallbackInfoReturnable<Boolean> info) {
if (ScoreboardCallbacks.TEAM_JOIN.invoker().handle(playerName, team))
info.cancel();
if (ScoreboardCallbacks.TEAM_JOIN.invoker().handle(playerName, team)) info.cancel();
}

@Inject(method = "removePlayerFromTeam", at = @At("TAIL"), cancellable = true)
private void onTeamLeave(String playerName, Team team, CallbackInfo info) {
if (ScoreboardCallbacks.TEAM_LEAVE.invoker().handle(playerName, team))
info.cancel();
if (ScoreboardCallbacks.TEAM_LEAVE.invoker().handle(playerName, team)) info.cancel();
}

@Inject(method = "removeTeam", at = @At("HEAD"), cancellable = true)
private void onTeamRemoveBefore(Team team, CallbackInfo info) {
if (ScoreboardCallbacks.TEAM_REMOVE_BEFORE.invoker().handle(team))
info.cancel();
if (ScoreboardCallbacks.TEAM_REMOVE_BEFORE.invoker().handle(team)) info.cancel();
}

@Inject(method = "removeTeam", at = @At("TAIL"), cancellable = true)
private void onTeamRemoveAfter(Team team, CallbackInfo info) {
if (ScoreboardCallbacks.TEAM_REMOVE_AFTER.invoker().handle(team))
info.cancel();
if (ScoreboardCallbacks.TEAM_REMOVE_AFTER.invoker().handle(team)) info.cancel();
}
}
27 changes: 22 additions & 5 deletions src/main/java/com/raxdiam/teamperms/util/PermissionManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.mojang.brigadier.tree.RootCommandNode;
import com.raxdiam.teamperms.TeamPerms;
import com.raxdiam.teamperms.events.ScoreboardCallbacks;
import com.raxdiam.teamperms.events.TeamPlayerCallback;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.PlayerManager;
import net.minecraft.server.command.CommandManager;
Expand Down Expand Up @@ -34,11 +36,6 @@ public PermissionManager(TeamMap teamMap, MinecraftServer minecraftServer) {

createPermTeams(teamMap);
createCommands();

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

}

public void apply() {
Expand All @@ -48,6 +45,21 @@ public void apply() {

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

var leaveJoinCallback = (TeamPlayerCallback) (playerName, team) -> {
safeSendCommandTree(playerName, playerManager, commandManager);
return false;
};

ScoreboardCallbacks.TEAM_JOIN.register(leaveJoinCallback);
ScoreboardCallbacks.TEAM_LEAVE.register(leaveJoinCallback);
ScoreboardCallbacks.TEAM_REMOVE_AFTER.register(team -> {
var players = team.getPlayerList();
for (var playerName : players) {
safeSendCommandTree(playerName, playerManager, commandManager);
}
return false;
});
}

public PermissionTeam getPermTeam(String name) {
Expand All @@ -71,4 +83,9 @@ private void createCommands() {
}
}
}

private static void safeSendCommandTree(String playerName, PlayerManager playerManager, CommandManager commandManager) {
var player = playerManager.getPlayer(playerName);
if (player != null) commandManager.sendCommandTree(player);
}
}

0 comments on commit 25e5aa1

Please sign in to comment.