- Fix world permissions handling and add more customization

This commit is contained in:
Taah 2022-02-21 20:51:05 -08:00
parent 3d3d92d72b
commit 455713db3c
12 changed files with 256 additions and 196 deletions

View File

@ -129,7 +129,7 @@ public abstract class PlexCommand extends Command implements PluginIdentifiableC
{
if (!plexPlayer.getRankFromString().isAtLeast(getLevel()))
{
send(sender, tl("noPermissionRank", ChatColor.stripColor(getLevel().getLoginMSG())));
send(sender, tl("noPermissionRank", ChatColor.stripColor(getLevel().getLoginMessage())));
return true;
}
}
@ -262,7 +262,7 @@ public abstract class PlexCommand extends Command implements PluginIdentifiableC
{
if (!plexPlayer.getRankFromString().isAtLeast(getLevel()))
{
throw new CommandFailException(PlexUtils.tl("noPermissionRank", ChatColor.stripColor(rank.getLoginMSG())));
throw new CommandFailException(PlexUtils.tl("noPermissionRank", ChatColor.stripColor(rank.getLoginMessage())));
}
}
else if (plugin.getSystem().equalsIgnoreCase("permissions"))

View File

@ -23,7 +23,7 @@ public class RankCMD extends PlexCommand
if (!(playerSender == null))
{
Rank rank = getPlexPlayer(playerSender).getRankFromString();
return tl("yourRank", rank.getReadableString());
return tl("yourRank", rank.getReadable());
}
return null;
}

View File

@ -41,6 +41,6 @@ public class AdminListener extends PlexListener
Rank newRank = event.getRank();
target.setRank(newRank.name().toLowerCase());
DataUtils.update(target);
PlexUtils.broadcast(tl("adminSetRank", userSender, target.getName(), newRank.getReadableString()));
PlexUtils.broadcast(tl("adminSetRank", userSender, target.getName(), newRank.getReadable()));
}
}

View File

@ -1,6 +1,5 @@
package dev.plex.listener.impl;
import dev.plex.Plex;
import dev.plex.cache.PlayerCache;
import dev.plex.listener.PlexListener;
import dev.plex.player.PlexPlayer;
@ -23,15 +22,11 @@ public class ChatListener extends PlexListener
{
PlexPlayer plexPlayer = PlayerCache.getPlexPlayerMap().get(event.getPlayer().getUniqueId());
if (!plexPlayer.getPrefix().isEmpty())
String prefix = plugin.getRankManager().getPrefix(plexPlayer);
if (!prefix.isEmpty())
{
renderer.hasPrefix = true;
renderer.prefix = plexPlayer.getPrefix();
}
else if (Plex.get().getRankManager().isAdmin(plexPlayer))
{
renderer.hasPrefix = true;
renderer.prefix = plexPlayer.getRankFromString().getPrefix();
renderer.prefix = prefix;
}
event.renderer(renderer);
}

View File

@ -9,8 +9,6 @@ import dev.plex.listener.PlexListener;
import dev.plex.player.PlexPlayer;
import dev.plex.player.PunishedPlayer;
import dev.plex.util.PlexLog;
import java.util.Collections;
import java.util.UUID;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
@ -19,6 +17,9 @@ import org.bukkit.event.EventPriority;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import java.util.Collections;
import java.util.UUID;
public class PlayerListener extends PlexListener
{
private final MongoPlayerData mongoPlayerData = plugin.getMongoPlayerData() != null ? plugin.getMongoPlayerData() : null;
@ -36,8 +37,7 @@ public class PlayerListener extends PlexListener
{
player.setOp(true);
PlexLog.debug("Automatically opped " + player.getName() + " since ranks are enabled.");
}
else if (plugin.getSystem().equalsIgnoreCase("permissions"))
} else if (plugin.getSystem().equalsIgnoreCase("permissions"))
{
player.setOp(false);
PlexLog.debug("Automatically deopped " + player.getName() + " since ranks are disabled.");
@ -50,8 +50,7 @@ public class PlayerListener extends PlexListener
plexPlayer.setName(player.getName()); //set the name of the player
plexPlayer.setIps(Collections.singletonList(player.getAddress().getAddress().getHostAddress().trim())); //set the arraylist of ips
DataUtils.insert(plexPlayer); // insert data in some wack db
}
else
} else
{
plexPlayer = DataUtils.getPlayer(player.getUniqueId());
}
@ -62,27 +61,30 @@ public class PlayerListener extends PlexListener
{
punishedPlayer = new PunishedPlayer(player.getUniqueId());
PlayerCache.getPunishedPlayerMap().put(player.getUniqueId(), punishedPlayer);
} else {
} else
{
punishedPlayer = PlayerCache.getPunishedPlayer(player.getUniqueId());
}
punishedPlayer.convertPunishments();
assert plexPlayer != null;
if (plugin.getRankManager().isAdmin(plexPlayer))
if (plugin.getSystem().equalsIgnoreCase("ranks"))
{
Admin admin = new Admin(UUID.fromString(plexPlayer.getUuid()));
admin.setRank(plexPlayer.getRankFromString());
plugin.getAdminList().addToCache(admin);
if (!plexPlayer.getLoginMSG().isEmpty())
if (plugin.getRankManager().isAdmin(plexPlayer))
{
event.joinMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(ChatColor.AQUA + player.getName() + " is " + plexPlayer.getLoginMSG()));
}
else
{
event.joinMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(ChatColor.AQUA + player.getName() + " is " + plexPlayer.getRankFromString().getLoginMSG()));
Admin admin = new Admin(UUID.fromString(plexPlayer.getUuid()));
admin.setRank(plexPlayer.getRankFromString());
plugin.getAdminList().addToCache(admin);
if (!plexPlayer.getLoginMSG().isEmpty())
{
event.joinMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(ChatColor.AQUA + player.getName() + " is " + plexPlayer.getLoginMSG()));
} else
{
event.joinMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(ChatColor.AQUA + player.getName() + " is " + plexPlayer.getRankFromString().getLoginMessage()));
}
}
}
}
@ -101,8 +103,7 @@ public class PlayerListener extends PlexListener
if (mongoPlayerData != null) //back to mongo checking
{
mongoPlayerData.update(plexPlayer); //update the player's document
}
else if (sqlPlayerData != null) //sql checking
} else if (sqlPlayerData != null) //sql checking
{
sqlPlayerData.update(plexPlayer);
}

View File

@ -1,21 +1,29 @@
package dev.plex.listener.impl;
import dev.plex.Plex;
import dev.plex.cache.PlayerCache;
import dev.plex.listener.PlexListener;
import dev.plex.player.PlexPlayer;
import dev.plex.rank.enums.Rank;
import dev.plex.rank.enums.Title;
import dev.plex.util.PlexLog;
import dev.plex.util.PlexUtils;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import org.bukkit.World;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.entity.EntitySpawnEvent;
import static dev.plex.util.PlexUtils.tl;
import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
public class WorldListener extends PlexListener
{
// TODO: Actually implement permissions for every world properly
private final String permission = plugin.config.getString("plex.adminworld.permission");
@EventHandler
public void onBlockPlace(BlockPlaceEvent e)
@ -23,28 +31,58 @@ public class WorldListener extends PlexListener
Player player = e.getPlayer();
PlexPlayer plexPlayer = PlayerCache.getPlexPlayerMap().get(player.getUniqueId());
World world = player.getWorld();
switch (world.getName().toLowerCase())
if (plugin.getSystem().equalsIgnoreCase("permissions"))
{
case "adminworld" -> {
if (plugin.getSystem().equalsIgnoreCase("ranks"))
{
if (plexPlayer.getRankFromString().isAtLeast(Rank.ADMIN))
{
return;
}
}
else if (plugin.getSystem().equalsIgnoreCase("permissions"))
{
if (player.hasPermission(permission));
{
return;
}
}
e.setCancelled(true);
player.sendMessage(tl("noAdminWorldBlockPlace"));
break;
String permission = plugin.config.getString("worlds." + world.getName().toLowerCase() + ".permission");
if (permission == null) return;
if (player.hasPermission(permission)) return;
} else if (plugin.getSystem().equalsIgnoreCase("ranks"))
{
if (plugin.config.contains("worlds." + world.getName().toLowerCase() + ".requiredLevels"))
{
@NotNull List<String> requiredLevel = plugin.config.getStringList("worlds." + world.getName().toLowerCase() + ".requiredLevels");
if (checkLevel(plexPlayer, requiredLevel.toArray(String[]::new))) return;
} else {
return;
}
}
e.setCancelled(true);
String noEdit = plugin.config.getString("worlds." + world.getName().toLowerCase() + ".noEdit");
if (noEdit != null)
{
player.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(noEdit));
}
}
@EventHandler
public void onBlockBreak(BlockBreakEvent e)
{
Player player = e.getPlayer();
PlexPlayer plexPlayer = PlayerCache.getPlexPlayerMap().get(player.getUniqueId());
World world = player.getWorld();
if (plugin.getSystem().equalsIgnoreCase("permissions"))
{
String permission = plugin.config.getString("worlds." + world.getName().toLowerCase() + ".permission");
if (permission == null) return;
if (player.hasPermission(permission)) return;
} else if (plugin.getSystem().equalsIgnoreCase("ranks"))
{
if (plugin.config.contains("worlds." + world.getName().toLowerCase() + ".requiredLevels"))
{
@NotNull List<String> requiredLevel = plugin.config.getStringList("worlds." + world.getName().toLowerCase() + ".requiredLevels");
if (checkLevel(plexPlayer, requiredLevel.toArray(String[]::new))) return;
} else {
return;
}
}
e.setCancelled(true);
String noEdit = plugin.config.getString("worlds." + world.getName().toLowerCase() + ".noEdit");
if (noEdit != null)
{
player.sendMessage(LegacyComponentSerializer.legacyAmpersand().deserialize(noEdit));
}
}
@EventHandler
@ -56,4 +94,40 @@ public class WorldListener extends PlexListener
}
e.setCancelled(true);
}
private boolean checkLevel(PlexPlayer player, String[] requiredList)
{
PlexLog.debug("Checking world required levels " + Arrays.toString(requiredList));
boolean hasAccess = false;
for (String required : requiredList)
{
PlexLog.debug("Checking if player has " + required);
if (required.startsWith("Title"))
{
String titleString = required.split("\\.")[1];
Title title = Title.valueOf(titleString.toUpperCase(Locale.ROOT));
switch (title)
{
case DEV -> {
hasAccess = PlexUtils.DEVELOPERS.contains(player.getUuid());
}
case MASTER_BUILDER -> {
hasAccess = Plex.get().config.contains("titles.masterbuilders") && Plex.get().config.getStringList("titles.masterbuilders").contains(player.getName());
}
case OWNER -> {
hasAccess = Plex.get().config.contains("titles.owners") && Plex.get().config.getStringList("titles.owners").contains(player.getName());
}
default -> {
return false;
}
}
} else if (required.startsWith("Rank"))
{
String rankString = required.split("\\.")[1];
Rank rank = Rank.valueOf(rankString.toUpperCase(Locale.ROOT));
hasAccess = player.getRankFromString().isAtLeast(rank);
}
}
return hasAccess;
}
}

View File

@ -1,24 +0,0 @@
package dev.plex.rank;
import com.google.common.collect.Lists;
import dev.plex.rank.enums.Rank;
import java.util.List;
import lombok.Getter;
@Getter
public class DefaultRankObj
{
private final String prefix;
private final String loginMSG;
private final String readableName;
private final List<String> permissions;
public DefaultRankObj(Rank rank)
{
this.prefix = rank.getPrefix();
this.loginMSG = rank.getLoginMSG();
this.readableName = rank.getReadableString();
this.permissions = Lists.newArrayList();
permissions.add("example.permission");
}
}

View File

@ -1,22 +1,24 @@
package dev.plex.rank;
import com.google.common.collect.Maps;
import com.google.gson.Gson;
import dev.plex.Plex;
import dev.plex.player.PlexPlayer;
import dev.plex.rank.enums.Rank;
import dev.plex.util.PlexLog;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;
import java.util.stream.Collectors;
import dev.plex.rank.enums.Title;
import dev.plex.util.PlexUtils;
import lombok.SneakyThrows;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;
import java.io.*;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class RankManager
{
private final File defaultRanks;
private final File options;
public RankManager()
{
@ -26,41 +28,23 @@ public class RankManager
ranksFolder.mkdir();
}
defaultRanks = new File(ranksFolder, "default-ranks.json");
options = new File(ranksFolder, "options.json");
}
@SneakyThrows
public void generateDefaultRanks()
{
if (defaultRanks.exists())
if (options.exists())
{
return;
}
try
{
defaultRanks.createNewFile();
Map<String, DefaultRankObj> rankMap = Maps.newHashMap();
for (Rank rank : Rank.values())
{
rankMap.put(rank.name().toUpperCase(), new DefaultRankObj(rank));
}
JSONObject obj = new JSONObject();
if (obj.length() == 0)
{
obj.put("ranks", rankMap);
FileWriter writer = new FileWriter(defaultRanks);
writer.append(obj.toString(4));
writer.flush();
writer.close();
PlexLog.log("Generating default-ranks.json");
}
}
catch (IOException e)
{
e.printStackTrace();
}
JSONObject object = new JSONObject();
object.put("ranks", new JSONArray().putAll(Arrays.stream(Rank.values()).map(Rank::toJSON).collect(Collectors.toList())));
object.put("titles", new JSONArray().putAll(Arrays.stream(Title.values()).map(Title::toJSON).collect(Collectors.toList())));
FileWriter writer = new FileWriter(options);
writer.append(object.toString(4));
writer.flush();
writer.close();
}
public Rank getRankFromString(String rank)
@ -70,34 +54,60 @@ public class RankManager
public void importDefaultRanks()
{
if (!defaultRanks.exists())
if (!options.exists())
{
return;
}
try
try (FileInputStream fis = new FileInputStream(options))
{
FileInputStream stream = new FileInputStream(defaultRanks);
JSONTokener tokener = new JSONTokener(stream);
JSONTokener tokener = new JSONTokener(fis);
JSONObject object = new JSONObject(tokener);
JSONObject rankObj = object.getJSONObject("ranks");
for (Rank rank : Rank.values())
{
if (rankObj.isNull(rank.name().toUpperCase()))
{
continue;
}
rank.setLoginMessage(rankObj.getJSONObject(rank.name().toUpperCase()).getString("loginMSG"));
rank.setPrefix(rankObj.getJSONObject(rank.name().toUpperCase()).getString("prefix")); //should i even be doing this
rank.setHumanReadableString(rankObj.getJSONObject(rank.name().toUpperCase()).getString("readableName")); // i dont know
rank.setPermissions(rankObj.getJSONObject(rank.name().toUpperCase()).getJSONArray("permissions").toList().stream().map(Object::toString).collect(Collectors.toList()));
}
}
catch (IOException e)
JSONArray ranks = object.getJSONArray("ranks");
ranks.forEach(r -> {
JSONObject rank = new JSONObject(r.toString());
String key = rank.keys().next();
Rank.valueOf(key).setLoginMessage(rank.getJSONObject(key).getString("loginMessage"));
Rank.valueOf(key).setPrefix(rank.getJSONObject(key).getString("prefix"));
});
JSONArray titles = object.getJSONArray("titles");
titles.forEach(t -> {
JSONObject title = new JSONObject(t.toString());
String key = title.keys().next();
Title.valueOf(key).setLoginMessage(title.getJSONObject(key).getString("loginMessage"));
Title.valueOf(key).setPrefix(title.getJSONObject(key).getString("prefix"));
});
} catch (IOException e)
{
e.printStackTrace();
}
}
public String getPrefix(PlexPlayer player)
{
if (!player.getPrefix().isEmpty())
{
return player.getPrefix();
}
if (Plex.get().config.contains("titles.owners") && Plex.get().config.getStringList("titles.owners").contains(player.getName()))
{
return Title.OWNER.getPrefix();
}
if (PlexUtils.DEVELOPERS.contains(player.getUuid())) // don't remove or we will front door ur mother
{
return Title.DEV.getPrefix();
}
if (Plex.get().config.contains("titles.masterbuilders") && Plex.get().config.getStringList("titles.masterbuilders").contains(player.getName()))
{
return Title.MASTER_BUILDER.getPrefix();
}
if (Plex.get().getSystem().equalsIgnoreCase("ranks") && isAdmin(player))
{
return player.getRankFromString().getPrefix();
}
return "";
}
public boolean isAdmin(PlexPlayer plexPlayer)

View File

@ -1,23 +1,36 @@
package dev.plex.rank.enums;
import com.google.common.collect.Lists;
import java.util.List;
import org.bukkit.ChatColor;
import java.util.Arrays;
import java.util.List;
import com.google.gson.Gson;
import lombok.Getter;
import lombok.Setter;
import org.bukkit.ChatColor;
import org.json.JSONObject;
@Getter
public enum Rank
{
IMPOSTOR(-1, ChatColor.AQUA + "an " + ChatColor.YELLOW + "Impostor", "Impostor", ChatColor.YELLOW + "[Imp]"),
NONOP(0, "a " + ChatColor.WHITE + "Non-Op", "Non-Op", ChatColor.WHITE + ""),
OP(1, "an " + ChatColor.GREEN + "Operator", "Operator", ChatColor.GREEN + "[OP]"),
ADMIN(2, "an " + ChatColor.DARK_GREEN + "Admin", "Admin", ChatColor.DARK_GREEN + "[Admin]"),
SENIOR_ADMIN(3, "a " + ChatColor.GOLD + "Senior Admin", "Senior Admin", ChatColor.GOLD + "[SrA]"),
EXECUTIVE(4, "an " + ChatColor.RED + "Executive", "Executive", ChatColor.RED + "[Exec]");
IMPOSTOR(-1, ChatColor.AQUA + "an " + ChatColor.YELLOW + "Impostor", "Impostor", "&8[&eImp&8]"),
NONOP(0, "a " + ChatColor.WHITE + "Non-Op", "Non-Op", ""),
OP(1, "an " + ChatColor.GREEN + "Operator", "Operator", "&8[&aOp&8]"),
ADMIN(2, "an " + ChatColor.DARK_GREEN + "Admin", "Admin", "&8[&2Admin&8]"),
SENIOR_ADMIN(3, "a " + ChatColor.GOLD + "Senior Admin", "Senior Admin", "&8[&6SrA&8]"),
EXECUTIVE(4, "an " + ChatColor.RED + "Executive", "Executive", "&8[&cExec&8]");
private final int level;
@Setter
private String loginMessage;
@Setter
private String readable;
@Setter
private String prefix;
private List<String> permissions;
Rank(int level, String loginMessage, String readable, String prefix)
{
@ -25,56 +38,18 @@ public enum Rank
this.loginMessage = loginMessage;
this.readable = readable;
this.prefix = prefix;
this.permissions = Lists.newArrayList();
}
public String getPrefix()
{
return ChatColor.translateAlternateColorCodes('&', prefix);
}
public String getLoginMSG()
{
return ChatColor.translateAlternateColorCodes('&', loginMessage);
}
public int getLevel()
{
return level;
}
public String getReadableString()
{
return readable;
}
public void setLoginMessage(String msg)
{
this.loginMessage = msg;
}
public void setPrefix(String prefix)
{
this.prefix = prefix;
}
public void setHumanReadableString(String readable)
{
this.readable = readable;
}
public boolean isAtLeast(Rank rank)
{
return getLevel() >= rank.getLevel();
return this.level >= rank.getLevel();
}
public List<String> getPermissions()
public JSONObject toJSON()
{
return permissions;
}
public void setPermissions(List<String> permissions)
{
this.permissions = permissions;
JSONObject object = new JSONObject();
object.put("prefix", this.prefix);
object.put("loginMessage", this.loginMessage);
return new JSONObject().put(this.name(), object);
}
}

View File

@ -1,23 +1,41 @@
package dev.plex.rank.enums;
import lombok.Getter;
import lombok.Setter;
import org.bukkit.ChatColor;
import org.json.JSONObject;
@Getter
public enum Title
{
MASTER_BUILDER(0, ChatColor.AQUA + "a " + ChatColor.DARK_AQUA + "Master Builder", "Master Builder", ChatColor.DARK_AQUA + "[MB]"),
DEV(1, ChatColor.AQUA + "a " + ChatColor.DARK_PURPLE + "Developer", "Developer", ChatColor.DARK_PURPLE + "[DEV]"),
OWNER(2, ChatColor.AQUA + "an " + ChatColor.BLUE + "Owner", "Owner", ChatColor.BLUE + "[Owner]");
MASTER_BUILDER(0, ChatColor.AQUA + "a " + ChatColor.DARK_AQUA + "Master Builder", "Master Builder", "&8[&3Master Builder&8]"),
DEV(1, ChatColor.AQUA + "a " + ChatColor.DARK_PURPLE + "Developer", "Developer", "&8[&5Developer&8]"),
OWNER(2, ChatColor.AQUA + "an " + ChatColor.BLUE + "Owner", "Owner", "&8[&9Owner&8]");
private int level;
private String loginMSG;
private final int level;
@Setter
private String loginMessage;
@Setter
private String readable;
@Setter
private String prefix;
Title(int level, String loginMSG, String readable, String prefix)
Title(int level, String loginMessage, String readable, String prefix)
{
this.level = level;
this.loginMSG = loginMSG;
this.loginMessage = loginMessage;
this.readable = readable;
this.prefix = prefix;
}
public JSONObject toJSON()
{
JSONObject object = new JSONObject();
object.put("prefix", this.prefix);
object.put("loginMessage", this.loginMessage);
return new JSONObject().put(this.name(), object);
}
}

View File

@ -8,6 +8,10 @@ server:
sample:
- "&cForums: https://forum.plex.us.org"
titles:
masterbuilders: []
owners: []
# Ban message is customized in the messages.yml file. The URL to appeal at is below.
banning:
ban_url: "https://forum.plex.us.org"
@ -33,10 +37,12 @@ data:
port: 6379
password: ""
# requiredLevel if permissions are ranks, else permissions
worlds:
flatlands:
name: "Flatlands"
permission: "plex.world.flatlands"
noEdit: "&cYou can't edit this world!"
parameters:
grass_block: 1
dirt: 32
@ -45,6 +51,9 @@ worlds:
adminworld:
name: "Admin World"
permission: "plex.world.adminworld"
requiredLevels:
- "Rank.ADMIN" # Minimum rank requirement
noEdit: "&cYou can't edit this world!"
parameters:
grass_block: 1
dirt: 32
@ -53,6 +62,9 @@ worlds:
masterbuilderworld:
name: "MasterBuilder World"
permission: "plex.world.masterbuilderworld"
requiredLevels:
- "Title.MASTER_BUILDER" # Title has no "minimum", so this will have to be their title
noEdit: "&cYou can't edit this world!"
parameters:
grass_block: 1
dirt: 32

View File

@ -29,8 +29,7 @@ test: "this is a test message!"
variableTest: "variable test with <v>!"
playerNotFound: "Player not found!"
worldNotFound: "World not found!"
noAdminWorldBlockPlace: "<e>You are not allowed to place blocks in the admin world!"
noAdminWorldBlockBreak: "<e>You are not allowed to break blocks in the admin world!"
# 1: the world you have been teleported to
playerWorldTeleport: "You have been teleported to <v>."
# 1: the command sender who opped everyone