Remove checkTab and replace with silentCheckRank

add isCancelled checks for MobListener
Modify the plugin's YAML file to load before essentials
Modify the world command to support the TFM Extras module
Add more checks in WorldListener to prevent spawning entities and interacting with anything except openable things (doors, trapdoors, gates)
This commit is contained in:
Taah 2023-08-24 02:40:52 -07:00
parent 37a649fa7a
commit 8344e3596e
18 changed files with 178 additions and 52 deletions

View File

@ -55,6 +55,10 @@ paper {
required = false required = false
load = 'BEFORE' load = 'BEFORE'
} }
'Essentials' {
required = false
load = 'BEFORE'
}
} }
} }

View File

@ -77,6 +77,16 @@ public abstract class PlexCommand extends Command implements PluginIdentifiableC
if (register) if (register)
{ {
if (getMap().getKnownCommands().containsKey(this.getName().toLowerCase()))
{
getMap().getKnownCommands().remove(this.getName().toLowerCase());
}
this.getAliases().forEach(s -> {
if (getMap().getKnownCommands().containsKey(s.toLowerCase()))
{
getMap().getKnownCommands().remove(s.toLowerCase());
}
});
getMap().register("plex", this); getMap().register("plex", this);
} }
} }
@ -287,7 +297,7 @@ public abstract class PlexCommand extends Command implements PluginIdentifiableC
{ {
return checkRank((Player) sender, rank, permission); return checkRank((Player) sender, rank, permission);
} }
if (!sender.getName().equalsIgnoreCase("console")) /*if (!sender.getName().equalsIgnoreCase("console"))
{ {
PlexPlayer plexPlayer = DataUtils.getPlayer(sender.getName()); PlexPlayer plexPlayer = DataUtils.getPlayer(sender.getName());
if (plugin.getSystem().equalsIgnoreCase("ranks")) if (plugin.getSystem().equalsIgnoreCase("ranks"))
@ -308,7 +318,48 @@ public abstract class PlexCommand extends Command implements PluginIdentifiableC
throw new CommandFailException(PlexUtils.messageString("noPermissionNode", permission)); throw new CommandFailException(PlexUtils.messageString("noPermissionNode", permission));
} }
} }
}*/
return true;
} }
/**
* Checks whether a sender has enough permissions or is high enough a rank
*
* @param sender A CommandSender
* @param rank The rank to check (if the server is using ranks)
* @param permission The permission to check (if the server is using permissions)
* @return true if the sender has enough permissions
* @see Rank
*/
protected boolean silentCheckRank(CommandSender sender, Rank rank, String permission)
{
PlexLog.debug("Checking {0} with {1}", sender.getName(), permission);
if (!isConsole(sender))
{
return silentCheckRank((Player) sender, rank, permission);
}
/*if (!sender.getName().equalsIgnoreCase("console"))
{
PlexPlayer plexPlayer = DataUtils.getPlayer(sender.getName());
if (plugin.getSystem().equalsIgnoreCase("ranks"))
{
if (!plexPlayer.getRankFromString().isAtLeast(rank))
{
throw new CommandFailException(PlexUtils.messageString("noPermissionRank", ChatColor.stripColor(rank.getLoginMessage())));
}
if (rank.isAtLeast(Rank.ADMIN) && !plexPlayer.isAdminActive())
{
throw new CommandFailException(PlexUtils.messageString("noPermissionRank", ChatColor.stripColor(rank.getLoginMessage())));
}
}
else if (plugin.getSystem().equalsIgnoreCase("permissions"))
{
if (!perms.permission().isEmpty() && !plugin.getPermissions().playerHas(null, Bukkit.getOfflinePlayer(plexPlayer.getName()), perms.permission()))
{
throw new CommandFailException(PlexUtils.messageString("noPermissionNode", permission));
}
}
}*/
return true; return true;
} }
@ -330,18 +381,14 @@ public abstract class PlexCommand extends Command implements PluginIdentifiableC
PlexPlayer plexPlayer = getPlexPlayer(player); PlexPlayer plexPlayer = getPlexPlayer(player);
if (plugin.getSystem().equalsIgnoreCase("ranks")) if (plugin.getSystem().equalsIgnoreCase("ranks"))
{ {
if (!plexPlayer.getRankFromString().isAtLeast(rank)) if (!plexPlayer.getRankFromString().isAtLeast(rank) || (rank.isAtLeast(Rank.ADMIN) && !plexPlayer.isAdminActive()))
{
throw new CommandFailException(PlexUtils.messageString("noPermissionRank", ChatColor.stripColor(rank.getLoginMessage())));
}
if (rank.isAtLeast(Rank.ADMIN) && !plexPlayer.isAdminActive())
{ {
throw new CommandFailException(PlexUtils.messageString("noPermissionRank", ChatColor.stripColor(rank.getLoginMessage()))); throw new CommandFailException(PlexUtils.messageString("noPermissionRank", ChatColor.stripColor(rank.getLoginMessage())));
} }
} }
else if (plugin.getSystem().equalsIgnoreCase("permissions")) else if (plugin.getSystem().equalsIgnoreCase("permissions"))
{ {
if (!perms.permission().isEmpty() && !player.hasPermission(perms.permission())) if (!permission.isEmpty() && !player.hasPermission(permission))
{ {
throw new CommandFailException(PlexUtils.messageString("noPermissionNode", permission)); throw new CommandFailException(PlexUtils.messageString("noPermissionNode", permission));
} }
@ -351,10 +398,6 @@ public abstract class PlexCommand extends Command implements PluginIdentifiableC
protected boolean silentCheckRank(Player player, Rank rank, String permission) protected boolean silentCheckRank(Player player, Rank rank, String permission)
{ {
if (player instanceof ConsoleCommandSender)
{
return true;
}
PlexPlayer plexPlayer = getPlexPlayer(player); PlexPlayer plexPlayer = getPlexPlayer(player);
if (plugin.getSystem().equalsIgnoreCase("ranks")) if (plugin.getSystem().equalsIgnoreCase("ranks"))
{ {
@ -362,12 +405,12 @@ public abstract class PlexCommand extends Command implements PluginIdentifiableC
} }
else if (plugin.getSystem().equalsIgnoreCase("permissions")) else if (plugin.getSystem().equalsIgnoreCase("permissions"))
{ {
return !perms.permission().isEmpty() && player.hasPermission(permission); return !permission.isEmpty() && player.hasPermission(permission);
} }
return false; return false;
} }
/** /* *//**
* Checks whether a sender has enough permissions or is high enough a rank * Checks whether a sender has enough permissions or is high enough a rank
* *
* @param sender The player object * @param sender The player object
@ -375,17 +418,17 @@ public abstract class PlexCommand extends Command implements PluginIdentifiableC
* @param permission The permission to check (if the server is using permissions) * @param permission The permission to check (if the server is using permissions)
* @return true if the sender has enough permissions * @return true if the sender has enough permissions
* @see Rank * @see Rank
*/ *//*
protected boolean checkTab(CommandSender sender, Rank rank, String permission) protected boolean silentCheckRank(CommandSender sender, Rank rank, String permission)
{ {
if (!isConsole(sender)) if (!isConsole(sender))
{ {
return checkTab((Player) sender, rank, permission); return silentCheckRank((Player) sender, rank, permission);
} }
return true; return true;
} }
/** *//**
* Checks whether a player has enough permissions or is high enough a rank * Checks whether a player has enough permissions or is high enough a rank
* *
* @param player The player object * @param player The player object
@ -393,8 +436,8 @@ public abstract class PlexCommand extends Command implements PluginIdentifiableC
* @param permission The permission to check (if the server is using permissions) * @param permission The permission to check (if the server is using permissions)
* @return true if the sender has enough permissions * @return true if the sender has enough permissions
* @see Rank * @see Rank
*/ *//*
protected boolean checkTab(Player player, Rank rank, String permission) protected boolean silentCheckRank(Player player, Rank rank, String permission)
{ {
PlexPlayer plexPlayer = getPlexPlayer(player); PlexPlayer plexPlayer = getPlexPlayer(player);
if (plugin.getSystem().equalsIgnoreCase("ranks")) if (plugin.getSystem().equalsIgnoreCase("ranks"))
@ -403,10 +446,10 @@ public abstract class PlexCommand extends Command implements PluginIdentifiableC
} }
else if (plugin.getSystem().equalsIgnoreCase("permissions")) else if (plugin.getSystem().equalsIgnoreCase("permissions"))
{ {
return !perms.permission().isEmpty() && player.hasPermission(permission); return !permission.isEmpty() && player.hasPermission(permission);
} }
return true; return true;
} }*/
/** /**
* Checks if a player is an admin * Checks if a player is an admin

View File

@ -58,7 +58,7 @@ public class AdventureCMD extends PlexCommand
@Override @Override
public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException
{ {
if (checkTab(sender, Rank.ADMIN, "plex.gamemode.adventure.others")) if (silentCheckRank(sender, Rank.ADMIN, "plex.gamemode.adventure.others"))
{ {
return PlexUtils.getPlayerNameList(); return PlexUtils.getPlayerNameList();
} }

View File

@ -110,6 +110,6 @@ public class BanCMD extends PlexCommand
@Override @Override
public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException
{ {
return args.length == 1 && checkTab(sender, Rank.ADMIN, "plex.ban") ? PlexUtils.getPlayerNameList() : ImmutableList.of(); return args.length == 1 && silentCheckRank(sender, Rank.ADMIN, "plex.ban") ? PlexUtils.getPlayerNameList() : ImmutableList.of();
} }
} }

View File

@ -62,7 +62,7 @@ public class CreativeCMD extends PlexCommand
@Override @Override
public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException
{ {
if (checkTab(sender, Rank.ADMIN, "plex.gamemode.creative.others")) if (silentCheckRank(sender, Rank.ADMIN, "plex.gamemode.creative.others"))
{ {
return PlexUtils.getPlayerNameList(); return PlexUtils.getPlayerNameList();
} }

View File

@ -69,6 +69,6 @@ public class FreezeCMD extends PlexCommand
@Override @Override
public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException
{ {
return args.length == 1 && checkTab(sender, Rank.ADMIN, "plex.freeze") ? PlexUtils.getPlayerNameList() : ImmutableList.of(); return args.length == 1 && silentCheckRank(sender, Rank.ADMIN, "plex.freeze") ? PlexUtils.getPlayerNameList() : ImmutableList.of();
} }
} }

View File

@ -54,6 +54,6 @@ public class LockupCMD extends PlexCommand
@Override @Override
public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException
{ {
return args.length == 1 && checkTab(sender, Rank.ADMIN, "plex.lockup") ? PlexUtils.getPlayerNameList() : ImmutableList.of(); return args.length == 1 && silentCheckRank(sender, Rank.ADMIN, "plex.lockup") ? PlexUtils.getPlayerNameList() : ImmutableList.of();
} }
} }

View File

@ -62,6 +62,6 @@ public class MuteCMD extends PlexCommand
@Override @Override
public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException
{ {
return args.length == 1 && checkTab(sender, Rank.ADMIN, "plex.mute") ? PlexUtils.getPlayerNameList() : ImmutableList.of(); return args.length == 1 && silentCheckRank(sender, Rank.ADMIN, "plex.mute") ? PlexUtils.getPlayerNameList() : ImmutableList.of();
} }
} }

View File

@ -144,7 +144,7 @@ public class SmiteCMD extends PlexCommand
@Override @Override
public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException
{ {
if (checkTab(sender, Rank.ADMIN, "plex.smite") && args.length == 1) if (silentCheckRank(sender, Rank.ADMIN, "plex.smite") && args.length == 1)
{ {
return PlexUtils.getPlayerNameList(); return PlexUtils.getPlayerNameList();
} }

View File

@ -58,7 +58,7 @@ public class SpectatorCMD extends PlexCommand
@Override @Override
public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException
{ {
if (checkTab(sender, Rank.ADMIN, "plex.gamemode.spectator.others")) if (silentCheckRank(sender, Rank.ADMIN, "plex.gamemode.spectator.others"))
{ {
return PlexUtils.getPlayerNameList(); return PlexUtils.getPlayerNameList();
} }

View File

@ -59,7 +59,7 @@ public class SurvivalCMD extends PlexCommand
@Override @Override
public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException
{ {
if (checkTab(sender, Rank.ADMIN, "plex.gamemode.survival.others")) if (silentCheckRank(sender, Rank.ADMIN, "plex.gamemode.survival.others"))
{ {
return PlexUtils.getPlayerNameList(); return PlexUtils.getPlayerNameList();
} }

View File

@ -98,6 +98,6 @@ public class TempbanCMD extends PlexCommand
@Override @Override
public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException
{ {
return args.length == 1 && checkTab(sender, Rank.ADMIN, "plex.tempban") ? PlexUtils.getPlayerNameList() : ImmutableList.of(); return args.length == 1 && silentCheckRank(sender, Rank.ADMIN, "plex.tempban") ? PlexUtils.getPlayerNameList() : ImmutableList.of();
} }
} }

View File

@ -61,6 +61,6 @@ public class UnbanCMD extends PlexCommand
@Override @Override
public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException
{ {
return args.length == 1 && checkTab(sender, Rank.ADMIN, "plex.unban") ? PlexUtils.getPlayerNameList() : ImmutableList.of(); return args.length == 1 && silentCheckRank(sender, Rank.ADMIN, "plex.unban") ? PlexUtils.getPlayerNameList() : ImmutableList.of();
} }
} }

View File

@ -41,6 +41,6 @@ public class UnfreezeCMD extends PlexCommand
@Override @Override
public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException
{ {
return args.length == 1 && checkTab(sender, Rank.ADMIN, "plex.unfreeze") ? PlexUtils.getPlayerNameList() : ImmutableList.of(); return args.length == 1 && silentCheckRank(sender, Rank.ADMIN, "plex.unfreeze") ? PlexUtils.getPlayerNameList() : ImmutableList.of();
} }
} }

View File

@ -41,6 +41,6 @@ public class UnmuteCMD extends PlexCommand
@Override @Override
public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException
{ {
return args.length == 1 && checkTab(sender, Rank.ADMIN, "plex.unfreeze") ? PlexUtils.getPlayerNameList() : ImmutableList.of(); return args.length == 1 && silentCheckRank(sender, Rank.ADMIN, "plex.unfreeze") ? PlexUtils.getPlayerNameList() : ImmutableList.of();
} }
} }

View File

@ -1,27 +1,32 @@
package dev.plex.command.impl; package dev.plex.command.impl;
import com.google.common.collect.ImmutableList; import dev.plex.Plex;
import dev.plex.command.PlexCommand; import dev.plex.command.PlexCommand;
import dev.plex.command.annotation.CommandParameters; import dev.plex.command.annotation.CommandParameters;
import dev.plex.command.annotation.CommandPermissions; import dev.plex.command.annotation.CommandPermissions;
import dev.plex.command.source.RequiredCommandSource; import dev.plex.command.source.RequiredCommandSource;
import dev.plex.rank.enums.Rank; import dev.plex.rank.enums.Rank;
import dev.plex.util.PlexLog;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import org.apache.commons.compress.utils.Lists;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.UUID;
import java.util.regex.Pattern;
@CommandPermissions(level = Rank.OP, permission = "plex.world", source = RequiredCommandSource.IN_GAME) @CommandPermissions(level = Rank.OP, permission = "plex.world", source = RequiredCommandSource.IN_GAME)
@CommandParameters(name = "world", description = "Teleport to a world.", usage = "/<command> <world>") @CommandParameters(name = "world", description = "Teleport to a world.", usage = "/<command> <world>")
public class WorldCMD extends PlexCommand public class WorldCMD extends PlexCommand
{ {
private static final Pattern UUID_PATTERN = Pattern.compile("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}");
@Override @Override
protected Component execute(@NotNull CommandSender sender, @Nullable Player playerSender, String[] args) protected Component execute(@NotNull CommandSender sender, @Nullable Player playerSender, String[] args)
{ {
@ -30,18 +35,45 @@ public class WorldCMD extends PlexCommand
{ {
return usage(); return usage();
} }
World world = getNonNullWorld(args[0]); World world = getNonNullWorld(args[0]);
boolean playerWorld = args[0].matches("[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}");
if (playerWorld && Plex.get().getModuleManager().getModules().stream().anyMatch(plexModule -> plexModule.getPlexModuleFile().getName().equalsIgnoreCase("Module-TFMExtras")))
{
checkRank(playerSender, Rank.ADMIN, "plex.world.playerworlds");
}
playerSender.teleportAsync(world.getSpawnLocation()); playerSender.teleportAsync(world.getSpawnLocation());
return messageComponent("playerWorldTeleport", world.getName()); return messageComponent("playerWorldTeleport", world.getName());
} }
@Override @Override
public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException public @NotNull List<String> tabComplete(@NotNull CommandSender sender, @NotNull String alias, @NotNull String[] args) throws IllegalArgumentException
{ {
final List<String> completions = Lists.newArrayList();
final Player player = (Player) sender;
if (args.length == 1) if (args.length == 1)
{ {
return Bukkit.getWorlds().stream().map(World::getName).collect(Collectors.toList()); @NotNull List<World> worlds = Bukkit.getWorlds();
} for (World world : worlds)
return ImmutableList.of(); {
String worldName = world.getName();
try
{
final UUID uuid = UUID.fromString(worldName);
if (uuid.equals(player.getUniqueId()) || silentCheckRank(player, Rank.ADMIN, "plex.world.playerworlds"))
{
completions.add(worldName);
}
}
catch (Exception e)
{
completions.add(worldName);
}
}
}
return completions;
} }
} }

View File

@ -11,6 +11,7 @@ import org.bukkit.entity.Ageable;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.block.Action; import org.bukkit.event.block.Action;
@ -53,6 +54,7 @@ public class MobListener extends PlexListener
@EventHandler @EventHandler
public void onEntitySpawn(EntitySpawnEvent event) public void onEntitySpawn(EntitySpawnEvent event)
{ {
if (event.isCancelled()) return;
if (event.getEntity().getEntitySpawnReason() == CreatureSpawnEvent.SpawnReason.SPAWNER_EGG) if (event.getEntity().getEntitySpawnReason() == CreatureSpawnEvent.SpawnReason.SPAWNER_EGG)
{ {
// for the future, we can instead filter and restrict nbt tags right here. // for the future, we can instead filter and restrict nbt tags right here.
@ -90,6 +92,7 @@ public class MobListener extends PlexListener
@EventHandler(priority = EventPriority.HIGH) @EventHandler(priority = EventPriority.HIGH)
public void onEntityClick(PlayerInteractEntityEvent event) public void onEntityClick(PlayerInteractEntityEvent event)
{ {
if (event.isCancelled()) return;
Material handItem = event.getPlayer().getEquipment().getItem(event.getHand()).getType(); Material handItem = event.getPlayer().getEquipment().getItem(event.getHand()).getType();
if (event.getRightClicked() instanceof Ageable entity) if (event.getRightClicked() instanceof Ageable entity)
{ {
@ -111,6 +114,8 @@ public class MobListener extends PlexListener
@EventHandler(priority = EventPriority.HIGH) @EventHandler(priority = EventPriority.HIGH)
public void onPlayerInteract(PlayerInteractEvent event) public void onPlayerInteract(PlayerInteractEvent event)
{ {
if (event.useItemInHand() == Event.Result.DENY) return;
if (event.useInteractedBlock() == Event.Result.DENY) return;
if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK)
{ {
if (SPAWN_EGGS.contains(event.getMaterial())) if (SPAWN_EGGS.contains(event.getMaterial()))

View File

@ -1,5 +1,6 @@
package dev.plex.listener.impl; package dev.plex.listener.impl;
import com.destroystokyo.paper.event.entity.PlayerNaturallySpawnCreaturesEvent;
import dev.plex.Plex; import dev.plex.Plex;
import dev.plex.listener.PlexListener; import dev.plex.listener.PlexListener;
import dev.plex.player.PlexPlayer; import dev.plex.player.PlexPlayer;
@ -12,6 +13,10 @@ import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.minimessage.MiniMessage;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.block.data.Openable;
import org.bukkit.block.data.Powerable;
import org.bukkit.block.data.type.Door;
import org.bukkit.block.data.type.TrapDoor;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.PluginIdentifiableCommand; import org.bukkit.command.PluginIdentifiableCommand;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
@ -20,9 +25,9 @@ import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.block.BlockBreakEvent; import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.block.BlockPlaceEvent;
import org.bukkit.event.entity.EntityDamageByEntityEvent;
import org.bukkit.event.entity.EntitySpawnEvent; import org.bukkit.event.entity.EntitySpawnEvent;
import org.bukkit.event.player.PlayerCommandPreprocessEvent; import org.bukkit.event.player.*;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.util.Arrays; import java.util.Arrays;
@ -33,7 +38,7 @@ 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 @EventHandler(priority = EventPriority.LOWEST)
public void onBlockPlace(BlockPlaceEvent event) public void onBlockPlace(BlockPlaceEvent event)
{ {
if (!canModifyWorld(event.getPlayer(), true)) if (!canModifyWorld(event.getPlayer(), true))
@ -42,7 +47,7 @@ public class WorldListener extends PlexListener
} }
} }
@EventHandler @EventHandler(priority = EventPriority.LOWEST)
public void onBlockBreak(BlockBreakEvent event) public void onBlockBreak(BlockBreakEvent event)
{ {
if (!canModifyWorld(event.getPlayer(), true)) if (!canModifyWorld(event.getPlayer(), true))
@ -51,7 +56,45 @@ public class WorldListener extends PlexListener
} }
} }
@EventHandler @EventHandler(priority = EventPriority.LOWEST)
public void onInteractWorld(PlayerInteractEvent event)
{
if (event.getInteractionPoint() != null && event.getInteractionPoint().getBlock().getBlockData() instanceof Openable) return;
if (!canModifyWorld(event.getPlayer(), true))
{
event.setCancelled(true);
}
}
@EventHandler(priority = EventPriority.LOWEST)
public void onInteractWorld(PlayerInteractEntityEvent event)
{
if (!canModifyWorld(event.getPlayer(), true))
{
event.setCancelled(true);
}
}
@EventHandler(priority = EventPriority.LOWEST)
public void onInteractWorld(PlayerItemDamageEvent event)
{
if (!canModifyWorld(event.getPlayer(), true))
{
event.setCancelled(true);
}
}
@EventHandler(priority = EventPriority.LOWEST)
public void onInteractWorld(EntityDamageByEntityEvent event)
{
if (!(event.getDamager() instanceof Player player)) return;
if (!canModifyWorld(player, true))
{
event.setCancelled(true);
}
}
@EventHandler(priority = EventPriority.LOWEST)
public void onEntitySpawn(EntitySpawnEvent event) public void onEntitySpawn(EntitySpawnEvent event)
{ {
if (event.getEntityType() != EntityType.SLIME) if (event.getEntityType() != EntityType.SLIME)
@ -87,10 +130,10 @@ public class WorldListener extends PlexListener
} }
} }
@EventHandler @EventHandler(priority = EventPriority.LOWEST)
public void onWorldTeleport(PlayerTeleportEvent event) public void onWorldTeleport(PlayerTeleportEvent event)
{ {
if (!canEnterWorld(event.getPlayer())) if (!canEnterWorld(event.getPlayer(), event.getTo().getWorld()))
{ {
event.setCancelled(true); event.setCancelled(true);
} }
@ -193,13 +236,12 @@ public class WorldListener extends PlexListener
* @param player The player who wants to enter the world * @param player The player who wants to enter the world
* @return Returns true if the person has the ability to enter the world * @return Returns true if the person has the ability to enter the world
*/ */
private boolean canEnterWorld(Player player) private boolean canEnterWorld(Player player, World destination)
{ {
PlexPlayer plexPlayer = plugin.getPlayerCache().getPlexPlayerMap().get(player.getUniqueId()); PlexPlayer plexPlayer = plugin.getPlayerCache().getPlexPlayerMap().get(player.getUniqueId());
World world = player.getWorld();
if (plugin.getSystem().equalsIgnoreCase("permissions")) if (plugin.getSystem().equalsIgnoreCase("permissions"))
{ {
String permission = plugin.config.getString("worlds." + world.getName().toLowerCase() + ".entry.permission"); String permission = plugin.config.getString("worlds." + destination.getName().toLowerCase() + ".entry.permission");
if (permission == null) if (permission == null)
{ {
return true; return true;
@ -211,9 +253,9 @@ public class WorldListener extends PlexListener
} }
else if (plugin.getSystem().equalsIgnoreCase("ranks")) else if (plugin.getSystem().equalsIgnoreCase("ranks"))
{ {
if (plugin.config.contains("worlds." + world.getName().toLowerCase() + ".entry.requiredLevels")) if (plugin.config.contains("worlds." + destination.getName().toLowerCase() + ".entry.requiredLevels"))
{ {
@NotNull List<String> requiredLevel = plugin.config.getStringList("worlds." + world.getName().toLowerCase() + ".entry.requiredLevels"); @NotNull List<String> requiredLevel = plugin.config.getStringList("worlds." + destination.getName().toLowerCase() + ".entry.requiredLevels");
if (checkLevel(plexPlayer, requiredLevel.toArray(String[]::new))) if (checkLevel(plexPlayer, requiredLevel.toArray(String[]::new)))
{ {
return true; return true;
@ -225,7 +267,7 @@ public class WorldListener extends PlexListener
} }
} }
String noEntry = plugin.config.getString("worlds." + world.getName().toLowerCase() + ".entry.message"); String noEntry = plugin.config.getString("worlds." + destination.getName().toLowerCase() + ".entry.message");
if (noEntry != null) if (noEntry != null)
{ {
player.sendMessage(MiniMessage.miniMessage().deserialize(noEntry)); player.sendMessage(MiniMessage.miniMessage().deserialize(noEntry));