2021-01-03 07:21:15 +00:00
|
|
|
package dev.plex.listener.impl;
|
2020-10-31 15:09:13 +00:00
|
|
|
|
2022-02-22 04:51:05 +00:00
|
|
|
import dev.plex.Plex;
|
2021-01-03 07:21:15 +00:00
|
|
|
import dev.plex.listener.PlexListener;
|
|
|
|
import dev.plex.player.PlexPlayer;
|
|
|
|
import dev.plex.rank.enums.Rank;
|
2022-02-22 04:51:05 +00:00
|
|
|
import dev.plex.rank.enums.Title;
|
|
|
|
import dev.plex.util.PlexLog;
|
|
|
|
import dev.plex.util.PlexUtils;
|
2022-02-22 09:30:23 +00:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Locale;
|
2022-04-20 21:37:15 +00:00
|
|
|
import net.kyori.adventure.text.Component;
|
|
|
|
import net.kyori.adventure.text.format.NamedTextColor;
|
2022-06-05 02:31:36 +00:00
|
|
|
import net.kyori.adventure.text.minimessage.MiniMessage;
|
2022-04-08 06:06:34 +00:00
|
|
|
import org.bukkit.Bukkit;
|
2020-10-31 15:09:13 +00:00
|
|
|
import org.bukkit.World;
|
2022-04-20 23:48:23 +00:00
|
|
|
import org.bukkit.command.Command;
|
|
|
|
import org.bukkit.command.PluginIdentifiableCommand;
|
2020-11-02 00:06:08 +00:00
|
|
|
import org.bukkit.entity.EntityType;
|
2020-10-31 15:09:13 +00:00
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
import org.bukkit.event.EventHandler;
|
2022-04-20 21:37:15 +00:00
|
|
|
import org.bukkit.event.EventPriority;
|
2022-02-22 04:51:05 +00:00
|
|
|
import org.bukkit.event.block.BlockBreakEvent;
|
2020-10-31 15:09:13 +00:00
|
|
|
import org.bukkit.event.block.BlockPlaceEvent;
|
2020-11-02 00:06:08 +00:00
|
|
|
import org.bukkit.event.entity.EntitySpawnEvent;
|
2022-04-20 21:37:15 +00:00
|
|
|
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
|
2022-04-08 06:06:34 +00:00
|
|
|
import org.bukkit.event.player.PlayerTeleportEvent;
|
2022-02-22 04:51:05 +00:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
|
|
|
|
2020-10-31 15:09:13 +00:00
|
|
|
public class WorldListener extends PlexListener
|
|
|
|
{
|
2022-06-05 03:08:13 +00:00
|
|
|
private final List<String> EDIT_COMMANDS = Arrays.asList("bigtree", "ebigtree", "largetree", "elargetree", "break", "ebreak", "antioch", "nuke", "editsign", "tree", "etree");
|
2022-04-21 22:17:05 +00:00
|
|
|
|
2020-10-31 15:09:13 +00:00
|
|
|
@EventHandler
|
2022-06-05 03:08:13 +00:00
|
|
|
public void onBlockPlace(BlockPlaceEvent event)
|
2020-10-31 15:09:13 +00:00
|
|
|
{
|
2022-06-05 03:08:13 +00:00
|
|
|
if (!canModifyWorld(event.getPlayer(), true))
|
2020-10-31 15:09:13 +00:00
|
|
|
{
|
2022-06-05 03:08:13 +00:00
|
|
|
event.setCancelled(true);
|
2022-02-22 04:51:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@EventHandler
|
2022-06-05 03:08:13 +00:00
|
|
|
public void onBlockBreak(BlockBreakEvent event)
|
2022-02-22 04:51:05 +00:00
|
|
|
{
|
2022-06-05 03:08:13 +00:00
|
|
|
if (!canModifyWorld(event.getPlayer(), true))
|
2022-02-22 04:51:05 +00:00
|
|
|
{
|
2022-06-05 03:08:13 +00:00
|
|
|
event.setCancelled(true);
|
2022-02-22 04:51:05 +00:00
|
|
|
}
|
2020-10-31 15:09:13 +00:00
|
|
|
}
|
2020-11-02 00:06:08 +00:00
|
|
|
|
|
|
|
@EventHandler
|
2022-06-05 03:08:13 +00:00
|
|
|
public void onEntitySpawn(EntitySpawnEvent event)
|
2020-11-02 00:06:08 +00:00
|
|
|
{
|
2022-06-05 03:08:13 +00:00
|
|
|
if (event.getEntityType() != EntityType.SLIME)
|
2020-11-06 01:29:38 +00:00
|
|
|
{
|
2020-11-02 00:06:08 +00:00
|
|
|
return;
|
2020-11-06 01:29:38 +00:00
|
|
|
}
|
2022-06-05 03:08:13 +00:00
|
|
|
event.setCancelled(true);
|
2020-11-02 00:06:08 +00:00
|
|
|
}
|
2022-02-22 04:51:05 +00:00
|
|
|
|
2022-04-20 21:37:15 +00:00
|
|
|
@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
|
2022-06-05 03:08:13 +00:00
|
|
|
if (canModifyWorld(event.getPlayer(), false))
|
2022-04-20 21:37:15 +00:00
|
|
|
{
|
2022-04-20 23:48:23 +00:00
|
|
|
return;
|
|
|
|
}
|
2022-04-20 21:37:15 +00:00
|
|
|
|
2022-04-20 23:48:23 +00:00
|
|
|
String message = event.getMessage();
|
|
|
|
// Don't check the arguments
|
|
|
|
message = message.replaceAll("\\s.*", "").replaceFirst("/", "");
|
|
|
|
Command command = Bukkit.getCommandMap().getCommand(message);
|
|
|
|
if (command != null)
|
|
|
|
{
|
|
|
|
// This does check for aliases
|
2022-08-02 12:08:52 +00:00
|
|
|
boolean isWeCommand = command instanceof PluginIdentifiableCommand && ((PluginIdentifiableCommand)command).getPlugin().equals(Bukkit.getPluginManager().getPlugin("WorldEdit"));
|
|
|
|
boolean isFaweCommand = command instanceof PluginIdentifiableCommand && ((PluginIdentifiableCommand)command).getPlugin().equals(Bukkit.getPluginManager().getPlugin("FastAsyncWorldEdit"));
|
2022-04-21 22:17:05 +00:00
|
|
|
if (isWeCommand || isFaweCommand || EDIT_COMMANDS.contains(message.toLowerCase()))
|
2022-04-20 23:48:23 +00:00
|
|
|
{
|
2022-04-21 22:17:05 +00:00
|
|
|
event.getPlayer().sendMessage(Component.text("You do not have permission to use that command in this world.").color(NamedTextColor.RED));
|
2022-04-20 23:48:23 +00:00
|
|
|
event.setCancelled(true);
|
|
|
|
}
|
2022-04-20 21:37:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-08 06:06:34 +00:00
|
|
|
@EventHandler
|
2022-06-05 03:08:13 +00:00
|
|
|
public void onWorldTeleport(PlayerTeleportEvent event)
|
2022-04-08 06:06:34 +00:00
|
|
|
{
|
2022-06-05 03:08:13 +00:00
|
|
|
if (!canEnterWorld(event.getPlayer()))
|
2022-04-08 21:37:13 +00:00
|
|
|
{
|
2022-06-05 03:08:13 +00:00
|
|
|
event.setCancelled(true);
|
2022-04-08 06:06:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-22 04:51:05 +00:00
|
|
|
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)
|
|
|
|
{
|
2022-04-13 02:22:17 +00:00
|
|
|
case DEV ->
|
|
|
|
{
|
2022-04-07 07:37:31 +00:00
|
|
|
hasAccess = PlexUtils.DEVELOPERS.contains(player.getUuid().toString());
|
2022-02-22 04:51:05 +00:00
|
|
|
}
|
2022-04-13 02:22:17 +00:00
|
|
|
case MASTER_BUILDER ->
|
|
|
|
{
|
2022-02-22 04:51:05 +00:00
|
|
|
hasAccess = Plex.get().config.contains("titles.masterbuilders") && Plex.get().config.getStringList("titles.masterbuilders").contains(player.getName());
|
|
|
|
}
|
2022-04-13 02:22:17 +00:00
|
|
|
case OWNER ->
|
|
|
|
{
|
2022-02-22 04:51:05 +00:00
|
|
|
hasAccess = Plex.get().config.contains("titles.owners") && Plex.get().config.getStringList("titles.owners").contains(player.getName());
|
|
|
|
}
|
2022-04-13 02:22:17 +00:00
|
|
|
default ->
|
|
|
|
{
|
2022-02-22 04:51:05 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2022-02-22 09:30:23 +00:00
|
|
|
}
|
|
|
|
else if (required.startsWith("Rank"))
|
2022-02-22 04:51:05 +00:00
|
|
|
{
|
|
|
|
String rankString = required.split("\\.")[1];
|
|
|
|
Rank rank = Rank.valueOf(rankString.toUpperCase(Locale.ROOT));
|
2022-04-07 07:37:31 +00:00
|
|
|
hasAccess = rank.isAtLeast(Rank.ADMIN) ? player.isAdminActive() && player.getRankFromString().isAtLeast(rank) : player.getRankFromString().isAtLeast(rank);
|
2022-02-22 04:51:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return hasAccess;
|
|
|
|
}
|
2022-04-20 21:37:15 +00:00
|
|
|
|
2022-06-05 03:08:13 +00:00
|
|
|
/**
|
|
|
|
* Check if a Player has the ability to modify the world they are in
|
|
|
|
*
|
2022-08-02 12:08:52 +00:00
|
|
|
* @param player The player who wants to modify the world
|
2022-06-06 03:34:16 +00:00
|
|
|
* @param showMessage Whether the message from the config.yml should be shown
|
|
|
|
* @return Returns true if the person has the ability to modify the world
|
2022-06-05 03:08:13 +00:00
|
|
|
*/
|
|
|
|
private boolean canModifyWorld(Player player, boolean showMessage)
|
2022-04-20 21:37:15 +00:00
|
|
|
{
|
2022-05-18 10:31:15 +00:00
|
|
|
PlexPlayer plexPlayer = plugin.getPlayerCache().getPlexPlayerMap().get(player.getUniqueId());
|
2022-04-20 21:37:15 +00:00
|
|
|
World world = player.getWorld();
|
|
|
|
if (plugin.getSystem().equalsIgnoreCase("permissions"))
|
|
|
|
{
|
2022-06-05 03:08:13 +00:00
|
|
|
String permission = plugin.config.getString("worlds." + world.getName().toLowerCase() + ".modification.permission");
|
2022-04-20 21:37:15 +00:00
|
|
|
if (permission == null)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
2022-08-02 12:08:52 +00:00
|
|
|
if (player.hasPermission(permission))
|
2022-04-20 21:37:15 +00:00
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (plugin.getSystem().equalsIgnoreCase("ranks"))
|
|
|
|
{
|
2022-06-05 03:08:13 +00:00
|
|
|
if (plugin.config.contains("worlds." + world.getName().toLowerCase() + ".modification.requiredLevels"))
|
2022-04-20 21:37:15 +00:00
|
|
|
{
|
2022-06-05 03:08:13 +00:00
|
|
|
@NotNull List<String> requiredLevel = plugin.config.getStringList("worlds." + world.getName().toLowerCase() + ".modification.requiredLevels");
|
2022-04-20 21:37:15 +00:00
|
|
|
if (checkLevel(plexPlayer, requiredLevel.toArray(String[]::new)))
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-20 23:48:23 +00:00
|
|
|
if (showMessage)
|
2022-04-20 21:37:15 +00:00
|
|
|
{
|
2022-06-05 03:08:13 +00:00
|
|
|
String noEdit = plugin.config.getString("worlds." + world.getName().toLowerCase() + ".modification.message");
|
2022-04-20 23:48:23 +00:00
|
|
|
if (noEdit != null)
|
|
|
|
{
|
2022-06-05 02:31:36 +00:00
|
|
|
player.sendMessage(MiniMessage.miniMessage().deserialize(noEdit));
|
2022-04-20 23:48:23 +00:00
|
|
|
}
|
2022-04-20 21:37:15 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2022-06-05 03:08:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Check if a Player has the ability to enter the requested world
|
2022-08-02 12:08:52 +00:00
|
|
|
*
|
2022-06-06 03:34:16 +00:00
|
|
|
* @param player The player who wants to enter the world
|
|
|
|
* @return Returns true if the person has the ability to enter the world
|
2022-06-05 03:08:13 +00:00
|
|
|
*/
|
|
|
|
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;
|
|
|
|
}
|
2022-08-02 12:08:52 +00:00
|
|
|
if (player.hasPermission(permission))
|
2022-06-05 03:08:13 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2020-10-31 15:09:13 +00:00
|
|
|
}
|