Cleans the boundary bypass listener

This commit is contained in:
Kristian Knarvik 2022-11-14 17:35:19 +01:00
parent 6d62a10de3
commit ce63d39339
4 changed files with 126 additions and 88 deletions

View File

@ -2,7 +2,7 @@ package net.knarcraft.hungerarena;
import com.sk89q.worldedit.bukkit.WorldEditPlugin;
import net.knarcraft.hungerarena.Listeners.BlockChangeListener;
import net.knarcraft.hungerarena.Listeners.Boundaries;
import net.knarcraft.hungerarena.Listeners.BoundaryBypassListener;
import net.knarcraft.hungerarena.Listeners.ChatListener;
import net.knarcraft.hungerarena.Listeners.DeathListener;
import net.knarcraft.hungerarena.Listeners.FreezeListener;
@ -107,7 +107,7 @@ public class HungerArena extends JavaPlugin {
final Listener BlockStorage = new BlockChangeListener(this);
//Listener WinGames = new WinGamesListener(this);
final Listener WorldChange = new WorldChange(this);
final Listener Boundaries = new Boundaries(this);
final Listener Boundaries = new BoundaryBypassListener(this);
final Listener spawnsListener = new spawnsListener(this);
final CommandExecutor HaCommands = new HaCommands(this);
final CommandExecutor SponsorCommands = new SponsorCommands(this);

View File

@ -1,85 +0,0 @@
package net.knarcraft.hungerarena.Listeners;
import net.knarcraft.hungerarena.HungerArena;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import java.util.Map;
import java.util.Map.Entry;
public class Boundaries implements Listener {
public final HungerArena plugin;
public Boundaries(HungerArena m) {
this.plugin = m;
}
@EventHandler
public void boundsCheck(PlayerMoveEvent event) {
Player p = event.getPlayer();
boolean inGame = plugin.getArena(p) != null;
boolean spectating = plugin.isSpectating(p);
if (plugin.config.getBoolean("WorldEdit")) {
if (insideBounds(p.getLocation()) && (inGame || spectating) || (!insideBounds(p.getLocation()) && inGame)) {
event.setCancelled(true);
}
}
}
@EventHandler
public void blockBounds(BlockBreakEvent event) {
Player p = event.getPlayer();
if (plugin.getArena(p) == null) {
if (plugin.config.getBoolean("WorldEdit")) {
if (insideBounds(event.getBlock().getLocation())) {
p.sendMessage(ChatColor.RED + "That block is protected by HungerArena!");
event.setCancelled(true);
}
}
}
}
public boolean insideBounds(Location l) {
Location minl = null;
Location maxl = null;
if (plugin.spawns.get("Arena") != null) {
Map<String, Object> temp = plugin.spawns.getConfigurationSection("Arena").getValues(false);
for (Entry<String, Object> entry : temp.entrySet()) {
if (plugin.spawns.getConfigurationSection("Arena." + entry.getKey()) != null) {
String[] min = (plugin.spawns.get("Arena." + entry.getKey()) + ".Min").split(",");
String[] max = (plugin.spawns.get("Arena." + entry.getKey()) + ".Max").split(",");
try {
World world = Bukkit.getWorld(min[0]);
double x = Double.parseDouble(min[1]);
double y = Double.parseDouble(min[2]);
double z = Double.parseDouble(min[3]);
minl = new Location(world, x, y, z);
World world2 = Bukkit.getWorld(max[0]);
double x2 = Double.parseDouble(max[1]);
double y2 = Double.parseDouble(max[2]);
double z2 = Double.parseDouble(max[3]);
minl = new Location(world2, x2, y2, z2);
if (minl != null && maxl != null) {
return l.getX() >= minl.getBlockX()
&& l.getX() < maxl.getBlockX() + 1 && l.getY() >= minl.getBlockY()
&& l.getY() < maxl.getBlockY() + 1 && l.getZ() >= minl.getBlockZ()
&& l.getZ() < maxl.getBlockZ() + 1;
}
} catch (Exception e) {
System.out.println(e);
return false;
}
}
}
}
return false;
}
}

View File

@ -0,0 +1,123 @@
package net.knarcraft.hungerarena.Listeners;
import net.knarcraft.hungerarena.Arena;
import net.knarcraft.hungerarena.HungerArena;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.BlockBreakEvent;
import org.bukkit.event.player.PlayerMoveEvent;
/**
* A listener for players doing anything that would bypass the arena's defined boundaries
*/
public class BoundaryBypassListener implements Listener {
public final HungerArena plugin;
public BoundaryBypassListener(HungerArena hungerArena) {
this.plugin = hungerArena;
}
@EventHandler
public void leavingBoundsListener(PlayerMoveEvent event) {
if (event.isCancelled()) {
return;
}
//If not restricted to a WorldEdit area, abort
if (!plugin.config.getBoolean("WorldEdit")) {
return;
}
Player player = event.getPlayer();
Arena playerArena = plugin.getArena(player);
if (playerArena == null) {
return;
}
//If currently in bounds and in a game or spectating, and about to leave the bounds, deny movement
if (insideBounds(event.getFrom(), playerArena.getArenaId()) &&
!insideBounds(event.getTo(), playerArena.getArenaId())) {
event.setCancelled(true);
}
}
@EventHandler
public void blockBounds(BlockBreakEvent event) {
if (event.isCancelled()) {
return;
}
//If not restricted to a WorldEdit area, abort
if (!plugin.config.getBoolean("WorldEdit")) {
return;
}
Player player = event.getPlayer();
Arena playerArena = plugin.getArena(player);
if (playerArena == null) {
return;
}
if (insideBounds(event.getBlock().getLocation(), playerArena.getArenaId())) {
player.sendMessage(ChatColor.RED + "That block is protected by HungerArena!");
event.setCancelled(true);
}
}
/**
* Checks if the given location is inside the bounds of the given arena
*
* @param location <p>The location to check</p>
* @param arenaId <p>The id of the arena to check</p>
* @return <p>True if the location is inside the arena's bounds</p>
*/
public boolean insideBounds(Location location, int arenaId) {
if (plugin.spawns.get("Arena") == null) {
return false;
}
Location minimumCorner;
Location maximumCorner;
if (plugin.spawns.getConfigurationSection("Arena." + arenaId) == null) {
return false;
}
String[] min = (plugin.spawns.get("Arena." + arenaId) + ".Min").split(",");
String[] max = (plugin.spawns.get("Arena." + arenaId) + ".Max").split(",");
try {
minimumCorner = getLocation(min);
maximumCorner = getLocation(max);
return location.getX() >= minimumCorner.getBlockX() &&
location.getX() < maximumCorner.getBlockX() + 1 &&
location.getY() >= minimumCorner.getBlockY() &&
location.getY() < maximumCorner.getBlockY() + 1 &&
location.getZ() >= minimumCorner.getBlockZ() &&
location.getZ() < maximumCorner.getBlockZ() + 1;
} catch (Exception e) {
System.out.println(e.getMessage());
return false;
}
}
/**
* Gets the location defined in the given info list (world,x,y,z)
*
* @param location <p>The string list describing the location</p>
* @return <p>The parsed location</p>
*/
private Location getLocation(String[] location) throws NumberFormatException {
World world = Bukkit.getWorld(location[0]);
double x = Double.parseDouble(location[1]);
double y = Double.parseDouble(location[2]);
double z = Double.parseDouble(location[3]);
return new Location(world, x, y, z);
}
}

View File

@ -69,7 +69,7 @@ Cannon_Death: true
# Whether players are allowed to use beds, or not!
DenyBedUsage: true
# Whether to have the arena be the world or the WorldEdit selection
# Whether the arena is limited to the selected WorldEdit region instead of the entire world
WorldEdit: false
# What item should be used to set spawns after /startpoint [arena#]