Resolves #12 (major config changes)

Also allows more customization within the updater
This commit is contained in:
Telesphoreo 2022-06-04 22:08:13 -05:00
parent dc4ac39fe4
commit abf2aca0f2
3 changed files with 109 additions and 52 deletions

View File

@ -1,7 +1,6 @@
package dev.plex.listener.impl;
import dev.plex.Plex;
import dev.plex.cache.DataUtils;
import dev.plex.listener.PlexListener;
import dev.plex.player.PlexPlayer;
import dev.plex.rank.enums.Rank;
@ -31,42 +30,41 @@ import org.jetbrains.annotations.NotNull;
public class WorldListener extends PlexListener
{
private final List<String> EDIT_COMMANDS = Arrays.asList("bigtree", "ebigtree", "largetree", "elargetree",
"break", "ebreak", "antioch", "nuke", "editsign", "tree", "etree");
private final List<String> EDIT_COMMANDS = Arrays.asList("bigtree", "ebigtree", "largetree", "elargetree", "break", "ebreak", "antioch", "nuke", "editsign", "tree", "etree");
@EventHandler
public void onBlockPlace(BlockPlaceEvent e)
public void onBlockPlace(BlockPlaceEvent event)
{
if (!checkPermission(e.getPlayer(), true))
if (!canModifyWorld(event.getPlayer(), true))
{
e.setCancelled(true);
event.setCancelled(true);
}
}
@EventHandler
public void onBlockBreak(BlockBreakEvent e)
public void onBlockBreak(BlockBreakEvent event)
{
if (!checkPermission(e.getPlayer(), true))
if (!canModifyWorld(event.getPlayer(), true))
{
e.setCancelled(true);
event.setCancelled(true);
}
}
@EventHandler
public void onEntitySpawn(EntitySpawnEvent e)
public void onEntitySpawn(EntitySpawnEvent event)
{
if (e.getEntityType() != EntityType.SLIME)
if (event.getEntityType() != EntityType.SLIME)
{
return;
}
e.setCancelled(true);
event.setCancelled(true);
}
@EventHandler(priority = EventPriority.HIGHEST)
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent event)
{
// If the person has permission to modify the world, we don't need to block WorldEdit
if (checkPermission(event.getPlayer(), false))
if (canModifyWorld(event.getPlayer(), false))
{
return;
}
@ -88,26 +86,12 @@ public class WorldListener extends PlexListener
}
}
// TODO: Add an entry setting in the config.yml and allow checking for all worlds
@EventHandler
public void onWorldTeleport(PlayerTeleportEvent e)
public void onWorldTeleport(PlayerTeleportEvent event)
{
final World adminworld = Bukkit.getWorld("adminworld");
if (adminworld == null)
if (!canEnterWorld(event.getPlayer()))
{
return;
}
PlexPlayer plexPlayer = DataUtils.getPlayer(e.getPlayer().getUniqueId());
if (e.getTo().getWorld().equals(adminworld))
{
if (plugin.getSystem().equals("ranks") && !plexPlayer.isAdminActive())
{
e.setCancelled(true);
}
else if (plugin.getSystem().equals("permissions") && !plugin.getPermissionHandler().hasPermission(e.getPlayer(), "plex.adminworld.enter"))
{
e.setCancelled(true);
}
event.setCancelled(true);
}
}
@ -152,13 +136,20 @@ public class WorldListener extends PlexListener
return hasAccess;
}
private boolean checkPermission(Player player, boolean showMessage)
/**
* Check if a Player has the ability to modify the world they are in
*
* @param player
* @param showMessage
* @return
*/
private boolean canModifyWorld(Player player, boolean showMessage)
{
PlexPlayer plexPlayer = plugin.getPlayerCache().getPlexPlayerMap().get(player.getUniqueId());
World world = player.getWorld();
if (plugin.getSystem().equalsIgnoreCase("permissions"))
{
String permission = plugin.config.getString("plex." + world.getName().toLowerCase() + ".permission");
String permission = plugin.config.getString("worlds." + world.getName().toLowerCase() + ".modification.permission");
if (permission == null)
{
return true;
@ -170,9 +161,9 @@ public class WorldListener extends PlexListener
}
else if (plugin.getSystem().equalsIgnoreCase("ranks"))
{
if (plugin.config.contains("worlds." + world.getName().toLowerCase() + ".requiredLevels"))
if (plugin.config.contains("worlds." + world.getName().toLowerCase() + ".modification.requiredLevels"))
{
@NotNull List<String> requiredLevel = plugin.config.getStringList("worlds." + world.getName().toLowerCase() + ".requiredLevels");
@NotNull List<String> requiredLevel = plugin.config.getStringList("worlds." + world.getName().toLowerCase() + ".modification.requiredLevels");
if (checkLevel(plexPlayer, requiredLevel.toArray(String[]::new)))
{
return true;
@ -186,7 +177,7 @@ public class WorldListener extends PlexListener
if (showMessage)
{
String noEdit = plugin.config.getString("worlds." + world.getName().toLowerCase() + ".noEdit");
String noEdit = plugin.config.getString("worlds." + world.getName().toLowerCase() + ".modification.message");
if (noEdit != null)
{
player.sendMessage(MiniMessage.miniMessage().deserialize(noEdit));
@ -194,4 +185,50 @@ public class WorldListener extends PlexListener
}
return false;
}
/**
* Check if a Player has the ability to enter the requested world
*
* @param player
* @return
*/
private boolean canEnterWorld(Player player)
{
PlexPlayer plexPlayer = plugin.getPlayerCache().getPlexPlayerMap().get(player.getUniqueId());
World world = player.getWorld();
if (plugin.getSystem().equalsIgnoreCase("permissions"))
{
String permission = plugin.config.getString("worlds." + world.getName().toLowerCase() + ".entry.permission");
if (permission == null)
{
return true;
}
if (plugin.getPermissionHandler().hasPermission(player, permission))
{
return true;
}
}
else if (plugin.getSystem().equalsIgnoreCase("ranks"))
{
if (plugin.config.contains("worlds." + world.getName().toLowerCase() + ".entry.requiredLevels"))
{
@NotNull List<String> requiredLevel = plugin.config.getStringList("worlds." + world.getName().toLowerCase() + ".entry.requiredLevels");
if (checkLevel(plexPlayer, requiredLevel.toArray(String[]::new)))
{
return true;
}
}
else
{
return true;
}
}
String noEntry = plugin.config.getString("worlds." + world.getName().toLowerCase() + ".entry.message");
if (noEntry != null)
{
player.sendMessage(MiniMessage.miniMessage().deserialize(noEntry));
}
return false;
}
}

View File

@ -38,7 +38,8 @@ public class UpdateChecker implements PlexBase
* > 0 = Number of commits behind
*/
private final String DOWNLOAD_PAGE = "https://ci.plex.us.org/job/";
private String branch = plugin.config.getString("update_branch");
private String BRANCH = plugin.config.getString("update_branch");
private final String REPO = plugin.config.getString("update_repo");
private int distance = -4;
// Adapted from Paper
@ -85,15 +86,15 @@ public class UpdateChecker implements PlexBase
// If verbose is 2, it will display all messages
public boolean getUpdateStatusMessage(CommandSender sender, boolean cached, int verbosity)
{
if (branch == null)
if (BRANCH == null)
{
PlexLog.error("You did not specify a branch to use for update checking. Defaulting to master.");
branch = "master";
BRANCH = "master";
}
// If it's -4, it hasn't checked for updates yet
if (distance == -4)
{
distance = fetchDistanceFromGitHub("plexusorg/Plex", branch, BuildInfo.getHead());
distance = fetchDistanceFromGitHub(REPO, BRANCH, BuildInfo.getHead());
PlexLog.debug("Never checked for updates, checking now...");
}
else
@ -101,7 +102,7 @@ public class UpdateChecker implements PlexBase
// If the request isn't asked to be cached, fetch it
if (!cached)
{
distance = fetchDistanceFromGitHub("plexusorg/Plex", branch, BuildInfo.getHead());
distance = fetchDistanceFromGitHub(REPO, BRANCH, BuildInfo.getHead());
PlexLog.debug("We have checked for updates before, but this request was not asked to be cached.");
}
else
@ -155,7 +156,7 @@ public class UpdateChecker implements PlexBase
AtomicReference<String> url = new AtomicReference<>(DOWNLOAD_PAGE + name);
if (!module)
{
url.set(url.get() + "/job/" + branch);
url.set(url.get() + "/job/" + BRANCH);
}
PlexLog.debug(url.toString());
HttpGet get = new HttpGet(url + "/lastSuccessfulBuild/api/json");

View File

@ -169,8 +169,9 @@ global_gamerules:
worlds:
flatlands:
name: "Flatlands"
permission: "plex.world.flatlands"
noEdit: "<red>You can't edit this world!"
modification:
permission: "plex.world.flatlands.modify"
message: "<red>You do not have permission to modify this world."
gameRules:
# The gamerules here override the global gamerules
- "doWeatherCycle;false"
@ -182,10 +183,18 @@ worlds:
bedrock: 1
adminworld:
name: "Admin World"
permission: "plex.world.adminworld"
requiredLevels:
- "Rank.ADMIN" # Minimum rank requirement
noEdit: "<red>You can't edit this world!"
entry:
permission: "plex.world.adminworld.enter"
# Minimum rank requirement
requiredLevels:
- "Rank.ADMIN"
message: "<red>You do not have permission to enter this world."
modification:
permission: "plex.world.adminworld.modify"
# Minimum rank requirement
requiredLevels:
- "Rank.ADMIN"
message: "<red>You do not have permission to modify this world."
gameRules:
- "doWeatherCycle;false"
- "doDaylightCycle;false"
@ -196,10 +205,17 @@ worlds:
bedrock: 1
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: "<red>You can't edit this world!"
entry:
permission: "plex.world.masterbuilderworld.enter"
requiredLevels:
- "Rank.OP"
message: "<red>You do not have permission to enter this world."
modification:
permission: "plex.world.masterbuilderworld.modify"
requiredLevels:
# Title has no "minimum", so this will have to be their title
- "Title.MASTER_BUILDER"
message: "<red><red>You do not have permission to modify this world."
gameRules:
- "doWeatherCycle;false"
- "doDaylightCycle;false"
@ -209,8 +225,11 @@ worlds:
stone: 16
bedrock: 1
# If you are running a custom fork of Plex, you may wish to check for updates from a different repository.
update_repo: "plexusorg/Plex"
# What branch should Plex fetch updates from?
update_branch: master
update_branch: "master"
# Additional logging for debugging
debug: false