mirror of
https://github.com/AtlasMediaGroup/TotalFreedomMod.git
synced 2025-04-18 08:23:01 +00:00
274 lines
8.0 KiB
Java
274 lines
8.0 KiB
Java
package me.totalfreedom.totalfreedommod.rank;
|
|
|
|
import me.totalfreedom.totalfreedommod.FreedomService;
|
|
import me.totalfreedom.totalfreedommod.admin.Admin;
|
|
import me.totalfreedom.totalfreedommod.config.ConfigEntry;
|
|
import me.totalfreedom.totalfreedommod.player.FPlayer;
|
|
import me.totalfreedom.totalfreedommod.player.PlayerData;
|
|
import me.totalfreedom.totalfreedommod.util.FUtil;
|
|
import net.kyori.adventure.text.Component;
|
|
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
|
|
import org.apache.commons.lang.StringUtils;
|
|
import org.bukkit.command.CommandSender;
|
|
import org.bukkit.command.ConsoleCommandSender;
|
|
import org.bukkit.entity.Player;
|
|
import org.bukkit.event.EventHandler;
|
|
import org.bukkit.event.EventPriority;
|
|
import org.bukkit.event.player.PlayerJoinEvent;
|
|
import org.bukkit.scoreboard.Scoreboard;
|
|
import org.bukkit.scoreboard.Team;
|
|
|
|
import java.util.Objects;
|
|
|
|
public class RankManager extends FreedomService
|
|
{
|
|
@Override
|
|
public void onStart()
|
|
{
|
|
// We don't need to do anything here.
|
|
}
|
|
|
|
@Override
|
|
public void onStop()
|
|
{
|
|
// We don't need to do anything here.
|
|
}
|
|
|
|
public Displayable getDisplay(CommandSender sender)
|
|
{
|
|
if (!(sender instanceof Player player))
|
|
{
|
|
return getRank(sender); // Consoles don't have display ranks
|
|
}
|
|
|
|
// If the player's an owner, display that
|
|
if (ConfigEntry.SERVER_OWNERS.getList().contains(player.getName()))
|
|
{
|
|
return Title.OWNER;
|
|
}
|
|
|
|
// If the user is an executive, display that.
|
|
if (ConfigEntry.SERVER_EXECUTIVES.getList().contains(player.getName()) && plugin.al.isAdmin(player))
|
|
{
|
|
return Title.EXECUTIVE;
|
|
}
|
|
|
|
// Developers always show up after executive.
|
|
if (FUtil.isDeveloper(player))
|
|
{
|
|
return Title.DEVELOPER;
|
|
}
|
|
|
|
if (ConfigEntry.SERVER_ASSISTANT_EXECUTIVES.getList().contains(player.getName()) && plugin.al.isAdmin(player))
|
|
{
|
|
return Title.ASST_EXEC;
|
|
}
|
|
|
|
// Master builders show up if they are not an admin
|
|
if (plugin.pl.getData(player).isMasterBuilder() && !plugin.al.isAdmin(player))
|
|
{
|
|
return Title.MASTER_BUILDER;
|
|
}
|
|
|
|
return getRank(player);
|
|
}
|
|
|
|
public Displayable getDisplay(Admin admin)
|
|
{
|
|
// If the player's an owner, display that
|
|
if (ConfigEntry.SERVER_OWNERS.getList().contains(admin.getName()))
|
|
{
|
|
return Title.OWNER;
|
|
}
|
|
|
|
// Developers always show up
|
|
if (FUtil.isDeveloper((Player) admin))
|
|
{
|
|
return Title.DEVELOPER;
|
|
}
|
|
|
|
if (ConfigEntry.SERVER_EXECUTIVES.getList().contains(admin.getName()))
|
|
{
|
|
return Title.EXECUTIVE;
|
|
}
|
|
|
|
return admin.getRank();
|
|
}
|
|
|
|
public DisplayableGroup getRank(CommandSender sender)
|
|
{
|
|
if (sender instanceof Player player)
|
|
{
|
|
return getRank(player);
|
|
}
|
|
|
|
// CONSOLE?
|
|
if (sender instanceof ConsoleCommandSender console)
|
|
{
|
|
if (sender.getName().equalsIgnoreCase("CONSOLE"))
|
|
{
|
|
return ConfigEntry.ADMINLIST_CONSOLE_IS_ADMIN.getBoolean()
|
|
? GroupProvider.SENIOR_ADMIN.getGroup()
|
|
: GroupProvider.ADMIN.getGroup();
|
|
}
|
|
}
|
|
|
|
// Console admin, get by name
|
|
Admin admin = plugin.al.getEntryByName(sender.getName());
|
|
|
|
// Unknown console: RCON?
|
|
if (admin == null)
|
|
{
|
|
return GroupProvider.SENIOR_ADMIN.getGroup();
|
|
}
|
|
|
|
return admin.getRank();
|
|
}
|
|
|
|
public DisplayableGroup getRank(Player player)
|
|
{
|
|
final Admin entry = plugin.al.getAdmin(player);
|
|
if (entry != null)
|
|
{
|
|
return entry.getRank();
|
|
}
|
|
|
|
return plugin.lpb.getAPI()
|
|
.getPlayerAdapter(Player.class)
|
|
.getUser(player)
|
|
.getPrimaryGroup()
|
|
.equalsIgnoreCase("op")
|
|
? GroupProvider.OP.getGroup()
|
|
: GroupProvider.NON_OP.getGroup();
|
|
}
|
|
|
|
public Component getTag(Player player, Component defaultTag)
|
|
{
|
|
Component tag = defaultTag;
|
|
|
|
PlayerData playerData = plugin.pl.getData(player);
|
|
Component t = playerData.getTag();
|
|
if (t != null && !(t.equals(Component.empty())))
|
|
{
|
|
tag = t;
|
|
}
|
|
|
|
return tag;
|
|
}
|
|
|
|
public void updateDisplay(Player player)
|
|
{
|
|
if (!player.isOnline())
|
|
{
|
|
return;
|
|
}
|
|
FPlayer fPlayer = plugin.pl.getPlayer(player);
|
|
PlayerData data = plugin.pl.getData(player);
|
|
Displayable display = getDisplay(player);
|
|
|
|
if (plugin.al.isAdmin(player) || data.isMasterBuilder() || FUtil.isDeveloper(player))
|
|
{
|
|
player.playerListName(Component.text(player.getName()).color(display.getColor()));
|
|
} else
|
|
{
|
|
fPlayer.setTag(null);
|
|
player.setPlayerListName(null);
|
|
}
|
|
|
|
fPlayer.setTag(getTag(player, display.getColoredTag()));
|
|
updatePlayerTeam(player);
|
|
}
|
|
|
|
@EventHandler(priority = EventPriority.MONITOR)
|
|
public void onPlayerJoin(PlayerJoinEvent event)
|
|
{
|
|
final Player player = event.getPlayer();
|
|
PlayerData target = plugin.pl.getData(player);
|
|
|
|
boolean isAdmin = plugin.al.isAdmin(player);
|
|
|
|
// Updates last login time
|
|
if (isAdmin)
|
|
{
|
|
plugin.al.updateLastLogin(player);
|
|
} else
|
|
{
|
|
// Ensure admins don't have admin functionality when removed (FS-222)
|
|
FPlayer freedomPlayer = plugin.pl.getPlayer(player);
|
|
|
|
freedomPlayer.removeAdminFunctionality();
|
|
}
|
|
|
|
// Broadcast login message
|
|
if ((isAdmin || FUtil.isDeveloper(player) || plugin.pl.getData(player).isMasterBuilder()
|
|
|| plugin.pl.getData(player).hasLoginMessage()) && !plugin.al.isVanished(player.getUniqueId()))
|
|
{
|
|
server.broadcast(craftLoginMessage(player, null));
|
|
}
|
|
|
|
// Set display
|
|
updateDisplay(player);
|
|
|
|
if (target.getTag() != null)
|
|
{
|
|
plugin.pl.getData(player).setTag(target.getTag());
|
|
}
|
|
}
|
|
|
|
public Component craftLoginMessage(Player player, String message)
|
|
{
|
|
Displayable display = plugin.rm.getDisplay(player);
|
|
PlayerData playerData = plugin.pl.getData(player);
|
|
if (message == null)
|
|
{
|
|
if (playerData.hasLoginMessage())
|
|
{
|
|
message = playerData.getLoginMessage();
|
|
} else
|
|
{
|
|
if (display.hasDefaultLoginMessage())
|
|
{
|
|
message = "<aqua><name> is <art> <coloredrank>";
|
|
}
|
|
}
|
|
}
|
|
if (message != null)
|
|
{
|
|
return FUtil.miniMessage(message,
|
|
Placeholder.unparsed("name", player.getName()),
|
|
Placeholder.component("rank", display.getName()),
|
|
Placeholder.component("coloredrank", display.getColoredName()),
|
|
Placeholder.component("art", display.getArticle()));
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
@SuppressWarnings("deprecation")
|
|
public void updatePlayerTeam(Player player)
|
|
{
|
|
Displayable display = getDisplay(player);
|
|
Scoreboard scoreboard = Objects.requireNonNull(server.getScoreboardManager()).getMainScoreboard();
|
|
Team team = scoreboard.getPlayerTeam(player);
|
|
if (!display.hasTeam())
|
|
{
|
|
if (team != null)
|
|
{
|
|
team.removePlayer(player);
|
|
}
|
|
return;
|
|
}
|
|
String name = StringUtils.substring(display.toString(), 0, 16);
|
|
team = scoreboard.getTeam(name);
|
|
if (team == null)
|
|
{
|
|
team = scoreboard.registerNewTeam(name);
|
|
team.setColor(display.getTeamColor());
|
|
}
|
|
if (!team.hasPlayer(player))
|
|
{
|
|
team.addPlayer(player);
|
|
}
|
|
}
|
|
}
|