Updated listeners

This commit is contained in:
Alastair 2016-08-01 03:27:17 +00:00
parent ce9a679bbc
commit e56853f0ea
4 changed files with 113 additions and 180 deletions

View File

@ -36,36 +36,17 @@ public class FlowStopper implements Listener {
Block blockTo = event.getToBlock(); Block blockTo = event.getToBlock();
Block block = event.getBlock(); Block block = event.getBlock();
if (!Portal.portalsActive) {
return; int floodRegion = 3;
AdvancedPortal inPortal = Portal.blockLocationInPortal(block.getLocation(), floodRegion);
if(inPortal != null){
event.setCancelled(true);
} }
for (AdvancedPortal portal : Portal.Portals) { inPortal = Portal.blockLocationInPortal(blockTo.getLocation(), floodRegion);
if (portal.worldName.equals(block.getWorld().getName())) { if(inPortal != null){
event.setCancelled(true);
if ((portal.pos1.getX() + 3D) >= block.getX() && (portal.pos1.getY() + 3D) >= block.getY() && (portal.pos1.getZ() + 3D) >= block.getZ()) {
if ((portal.pos2.getX() - 3D) <= block.getX() && (portal.pos2.getY() - 3D) <= block.getY() && (portal.pos2.getZ() - 3D) <= block.getZ()) {
event.setCancelled(true);
}
}
}
if (portal.worldName.equals(blockTo.getWorld().getName())) {
if ((portal.pos1.getX() + 3D) >= blockTo.getX() && (portal.pos1.getY() + 3D) >= blockTo.getY() && (portal.pos1.getZ() + 3D) >= blockTo.getZ()) {
if ((portal.pos2.getX() - 3D) <= blockTo.getX() && (portal.pos2.getY() - 3D) <= blockTo.getY() && (portal.pos2.getZ() - 3D) <= blockTo.getZ()) {
event.setCancelled(true);
}
}
}
} }
} }

View File

@ -26,6 +26,9 @@ public class Listeners implements Listener {
// TODO rewrite and remove unneeded listeners and also fix the warp delay using a hashmap of users last attempt times. // TODO rewrite and remove unneeded listeners and also fix the warp delay using a hashmap of users last attempt times.
// TODO rewrite listeners and take into account the portal cooldown and what happens if they start in a portal (or are warped into a portal).
// The needed config values will be stored so they are easier to access later // The needed config values will be stored so they are easier to access later
// an example is in the interact event in this if statement if((!UseOnlyServerAxe || event.getItem().getItemMeta().getDisplayName().equals("\u00A7eP... // an example is in the interact event in this if statement if((!UseOnlyServerAxe || event.getItem().getItemMeta().getDisplayName().equals("\u00A7eP...
private static boolean UseOnlyServerAxe = false; private static boolean UseOnlyServerAxe = false;
@ -85,100 +88,62 @@ public class Listeners implements Listener {
} }
} }
@EventHandler(priority = EventPriority.HIGHEST) @EventHandler(priority = EventPriority.HIGHEST)
public void onMoveEvent(PlayerMoveEvent event) { public void onMoveEvent(PlayerMoveEvent event) {
// will check if the player is in the portal or not.
if (!Portal.portalsActive) {
return;
}
Player player = event.getPlayer(); Player player = event.getPlayer();
/**
* use to find speed and possibly launch backwards if they should not enter the portal yet (stops so many repeated enter attempts)
*/
Location fromloc = event.getFrom(); Location fromloc = event.getFrom();
Location loc = event.getTo(); Location loc = event.getTo();
// Potentially fixes that stupid error cauzed by a bukkit update.
// Would save event.getTo() as eyeLoc and change the Y position but that seemed to teleport players.
Location eyeLoc = new Location(loc.getWorld(), loc.getX(), loc.getY() + player.getEyeHeight(), loc.getZ());
//System.out.println(loc.getBlock().getType()); // for debugging, remove or comment out when not needed
// This is probably the culprite of the bloody problem, setting the location its pointing to the event location
// rather than sorta making a clone of the object.
//System.out.println(loc.getBlock().getType()); // for debugging, remove or comment out when not needed
for (AdvancedPortal portal : Portal.Portals) {
if (loc.getWorld() != null && portal.worldName.equals(loc.getWorld().getName())) {
if (portal.trigger.equals(loc.getBlock().getType())
|| portal.trigger.equals(eyeLoc.getBlock().getType())) {
if ((portal.pos1.getX() + 1D) >= loc.getX() && (portal.pos1.getY()) >= loc.getY() && (portal.pos1.getZ() + 1D) >= loc.getZ()) {
if (portal.pos2.getX() <= loc.getX() && portal.pos2.getY() <= loc.getY() && portal.pos2.getZ() <= loc.getZ()) {
AdvancedPortal inPortal = Portal.playerInPortal(player);
if(inPortal != null){
WarpEvent warpEvent = new WarpEvent(player, inPortal);
plugin.getServer().getPluginManager().callEvent(warpEvent);
WarpEvent warpEvent = new WarpEvent(player, portal); if (!event.isCancelled()) {
plugin.getServer().getPluginManager().callEvent(warpEvent); boolean warped = Portal.activate(player, inPortal);
if (PortalMessagesDisplay == 1 && warped) {
player.sendMessage("");
player.sendMessage(plugin.customPrefixFail + "\u00A7a You have been warped to \u00A7e" + inPortal.destiation.replaceAll("_", " ") + "\u00A7.");
player.sendMessage("");
} else if (PortalMessagesDisplay == 2 && warped) {
ConfigAccessor config = new ConfigAccessor(plugin, "inPortals.yml");
plugin.nmsAccess.sendActionBarMessage("{\"text\":\"\u00A7aYou have been warped to \u00A7e" + inPortal.destiation.replaceAll("_", " ") + "\u00A7a.\"}", player);
}
if (!warped) {
player.teleport(fromloc, PlayerTeleportEvent.TeleportCause.PLUGIN);
if (!event.isCancelled()) { event.setCancelled(true);
boolean warped = Portal.activate(player, portal);
if (PortalMessagesDisplay == 1 && warped) {
player.sendMessage("");
player.sendMessage(plugin.customPrefixFail + "\u00A7a You have been warped to \u00A7e" + portal.destiation.replaceAll("_", " ") + "\u00A7.");
player.sendMessage("");
} else if (PortalMessagesDisplay == 2 && warped) {
ConfigAccessor config = new ConfigAccessor(plugin, "portals.yml");
plugin.nmsAccess.sendActionBarMessage("{\"text\":\"\u00A7aYou have been warped to \u00A7e" + portal.destiation.replaceAll("_", " ") + "\u00A7a.\"}", player);
/**plugin.nmsAccess.sendActionBarMessage("[{text:\"You have warped to \",color:green},{text:\"" + config.getConfig().getString(portal.portalName + ".destination").replaceAll("_", " ")
+ "\",color:yellow},{\"text\":\".\",color:green}]", player);*/
}
if (warped) {
//event.setFrom(player.getLocation());
//event.setTo(player.getLocation());
//event.setCancelled(true);
} else {
player.teleport(fromloc, PlayerTeleportEvent.TeleportCause.PLUGIN);
event.setCancelled(true);
}
}
if (portal.trigger.equals(Material.PORTAL)) {
if (player.getGameMode().equals(GameMode.CREATIVE)) {
player.setMetadata("hasWarped", new FixedMetadataValue(plugin, true));
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new RemoveWarpData(player), 10);
}
} else if (portal.trigger.equals(Material.LAVA)) {
player.setMetadata("lavaWarped", new FixedMetadataValue(plugin, true));
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new RemoveLavaData(player), 10);
}
}
}
} }
} }
if (inPortal.trigger.equals(Material.PORTAL)) {
if (player.getGameMode().equals(GameMode.CREATIVE)) {
player.setMetadata("hasWarped", new FixedMetadataValue(plugin, true));
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new RemoveWarpData(player), 10);
}
} else if (inPortal.trigger.equals(Material.LAVA)) {
player.setMetadata("lavaWarped", new FixedMetadataValue(plugin, true));
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new RemoveLavaData(player), 10);
}
} }
} }
/*@EventHandler(ignoreCancelled = false, priority = EventPriority.LOWEST) @EventHandler(ignoreCancelled = false, priority = EventPriority.LOWEST)
public void onPlayerTeleport(PlayerTeleportEvent event){ public void onPlayerTeleport(PlayerTeleportEvent event){
Player player = event.getPlayer(); Player player = event.getPlayer();
plugin.getLogger().info(String.valueOf(event.getCause())); //plugin.getLogger().info(String.valueOf(event.getCause()));
Location loc = player.getLocation(); AdvancedPortal inPortal = Portal.playerInPortal(player);
Object[] portals = Portal.Portals; if(inPortal != null){
for (AdvancedPortal portal : Portal.Portals) { event.setCancelled(true);
if (portal.worldName.equals(player.getWorld().getName())) {
if ((portal.pos1.getX() + 1D) >= loc.getX() && (portal.pos1.getY() + 1D) >= loc.getY() && (portal.pos1.getZ() + 1D) >= loc.getZ()) {
if ((portal.pos2.getX()) <= loc.getX() && (portal.pos2.getY()) <= loc.getY() && (portal.pos2.getZ()) <= loc.getZ()) {
event.setCancelled(true);
}
}
}
} }
}*/ }
// These are here because java 7 can only take finals straight into a runnable // These are here because java 7 can only take finals straight into a runnable
class RemoveLavaData implements Runnable{ class RemoveLavaData implements Runnable{
@ -216,51 +181,33 @@ public class Listeners implements Listener {
@EventHandler @EventHandler
public void onCombustEntityEvent(EntityCombustEvent event) { public void onCombustEntityEvent(EntityCombustEvent event) {
if (!Portal.portalsActive) {
return;
}
Location loc = event.getEntity().getLocation(); Location loc = event.getEntity().getLocation();
for (AdvancedPortal portal : Portal.Portals) {
if (portal.worldName.equals(loc.getWorld().getName())) {
if ((portal.pos1.getX() + 3D) >= loc.getX() && (portal.pos1.getY() + 3D) >= loc.getY() && (portal.pos1.getZ() + 3D) >= loc.getZ()) { AdvancedPortal inPortal = Portal.locationInPortal(loc,3);
if(inPortal != null){
if ((portal.pos2.getX() - 3D) <= loc.getX() && (portal.pos2.getY() - 3D) <= loc.getY() && (portal.pos2.getZ() - 3D) <= loc.getZ()) { event.setCancelled(true);
event.setCancelled(true);
return;
}
}
}
} }
} }
@EventHandler @EventHandler
public void onDamEvent(EntityDamageEvent event) { public void onDamEvent(EntityDamageEvent event) {
if (!Portal.portalsActive) {
return;
}
//System.out.println(event.getCause()); //System.out.println(event.getCause());
if (event.getCause() == EntityDamageEvent.DamageCause.LAVA || event.getCause() == EntityDamageEvent.DamageCause.FIRE || event.getCause() == EntityDamageEvent.DamageCause.FIRE_TICK) { if (event.getCause() == EntityDamageEvent.DamageCause.LAVA || event.getCause() == EntityDamageEvent.DamageCause.FIRE || event.getCause() == EntityDamageEvent.DamageCause.FIRE_TICK) {
Location loc = event.getEntity().getLocation();
if (event.getEntity().hasMetadata("lavaWarped")) { if (event.getEntity().hasMetadata("lavaWarped")) {
event.setCancelled(true); event.setCancelled(true);
return; return;
} }
for (AdvancedPortal portal : Portal.Portals) {
if (portal.worldName.equals(loc.getWorld().getName())) {
if ((portal.pos1.getX() + 3D) >= loc.getX() && (portal.pos1.getY() + 3D) >= loc.getY() && (portal.pos1.getZ() + 3D) >= loc.getZ()) { Location loc = event.getEntity().getLocation();
if ((portal.pos2.getX() - 3D) <= loc.getX() && (portal.pos2.getY() - 3D) <= loc.getY() && (portal.pos2.getZ() - 3D) <= loc.getZ()) { AdvancedPortal inPortal = Portal.locationInPortal(loc,3);
event.setCancelled(true); if(inPortal != null){
return; event.setCancelled(true);
return;
}
}
}
} }
} }
@ -272,9 +219,6 @@ public class Listeners implements Listener {
@EventHandler @EventHandler
public void onPortalEvent(PlayerPortalEvent event) { public void onPortalEvent(PlayerPortalEvent event) {
if (!Portal.portalsActive) {
return;
}
Player player = event.getPlayer(); Player player = event.getPlayer();
if (player.hasMetadata("hasWarped")) { if (player.hasMetadata("hasWarped")) {
@ -282,21 +226,9 @@ public class Listeners implements Listener {
return; return;
} }
Location loc = player.getLocation(); AdvancedPortal inPortal = Portal.playerInPortal(player);
Object[] portals = Portal.Portals; if(inPortal != null){
for (AdvancedPortal portal : Portal.Portals) { event.setCancelled(true);
if (portal.worldName.equals(player.getWorld().getName())) {
if ((portal.pos1.getX() + 1D) >= loc.getX() && (portal.pos1.getY() + 1D) >= loc.getY() && (portal.pos1.getZ() + 1D) >= loc.getZ()) {
if ((portal.pos2.getX()) <= loc.getX() && (portal.pos2.getY()) <= loc.getY() && (portal.pos2.getZ()) <= loc.getZ()) {
event.setCancelled(true);
}
}
}
} }
@ -316,23 +248,15 @@ public class Listeners implements Listener {
player.removeMetadata("selectingPortal", plugin); player.removeMetadata("selectingPortal", plugin);
return; return;
} }
Block block = event.getClickedBlock(); Block block = event.getClickedBlock();
for (AdvancedPortal portal : Portal.Portals) { AdvancedPortal inPortal = Portal.blockLocationInPortal(block.getLocation());
if (portal.worldName.equals(block.getWorld().getName())) { if(inPortal != null){
player.sendMessage(plugin.customPrefixFail + "\u00A7a You have selected: \u00A7e" + inPortal.portalName);
if ((portal.pos1.getX()) >= block.getX() && (portal.pos1.getY()) >= block.getY() && (portal.pos1.getZ()) >= block.getZ()) { player.setMetadata("selectedPortal", new FixedMetadataValue(plugin, inPortal.portalName)); // adds the name to the metadata of the character
event.setCancelled(true);
if ((portal.pos2.getX()) <= block.getX() && (portal.pos2.getY()) <= block.getY() && (portal.pos2.getZ()) <= block.getZ()) { player.removeMetadata("selectingPortal", plugin);
player.sendMessage(plugin.customPrefixFail + "\u00A7a You have selected: \u00A7e" + portal.portalName); return;
player.setMetadata("selectedPortal", new FixedMetadataValue(plugin, portal.portalName)); // adds the name to the metadata of the character
event.setCancelled(true);
player.removeMetadata("selectingPortal", plugin);
return;
}
}
}
} }
player.sendMessage(plugin.customPrefixFail + "\u00A7c No portal was selected. If you would like to stop selecting please type \u00A7e/portal select \u00A7cagain!"); player.sendMessage(plugin.customPrefixFail + "\u00A7c No portal was selected. If you would like to stop selecting please type \u00A7e/portal select \u00A7cagain!");
event.setCancelled(true); event.setCancelled(true);

View File

@ -16,7 +16,7 @@ public class PortalPlacer implements Listener {
@SuppressWarnings("unused") @SuppressWarnings("unused")
private final AdvancedPortalsPlugin plugin; private final AdvancedPortalsPlugin plugin;
private final double PortalProtectionRadius; private final int PortalProtectionRadius;
// The needed config values will be stored so they are easier to access later // The needed config values will be stored so they are easier to access later
// an example is in the interact event in this if statement if((!UseOnlyServerAxe || event.getItem().getItemMeta().getDisplayName().equals("<EFBFBD>eP... // an example is in the interact event in this if statement if((!UseOnlyServerAxe || event.getItem().getItemMeta().getDisplayName().equals("<EFBFBD>eP...
@ -28,7 +28,7 @@ public class PortalPlacer implements Listener {
ConfigAccessor config = new ConfigAccessor(plugin, "config.yml"); ConfigAccessor config = new ConfigAccessor(plugin, "config.yml");
this.PortalPlace = config.getConfig().getBoolean("CanBuildPortalBlock"); this.PortalPlace = config.getConfig().getBoolean("CanBuildPortalBlock");
this.PortalProtectionRadius = config.getConfig().getDouble("PortalProtectionRadius"); this.PortalProtectionRadius = config.getConfig().getInt("PortalProtectionRadius");
if (PortalPlace) { if (PortalPlace) {
plugin.getServer().getPluginManager().registerEvents(this, plugin); plugin.getServer().getPluginManager().registerEvents(this, plugin);
@ -40,20 +40,10 @@ public class PortalPlacer implements Listener {
Block block = event.getBlock(); Block block = event.getBlock();
Material material = block.getType(); Material material = block.getType();
if (material == Material.PORTAL) { if (material == Material.PORTAL) {
for (AdvancedPortal portal : Portal.Portals) {
if (portal.worldName.equals(block.getWorld().getName())) {
if ((portal.pos1.getX() + PortalProtectionRadius) >= block.getX() && (portal.pos1.getY() + PortalProtectionRadius) >= block.getY() && (portal.pos1.getZ() + PortalProtectionRadius) >= block.getZ()) { AdvancedPortal inPortal = Portal.blockLocationInPortal(block.getLocation(), PortalProtectionRadius);
if(inPortal != null){
if ((portal.pos2.getX() - PortalProtectionRadius) <= block.getX() && (portal.pos2.getY() - PortalProtectionRadius) <= block.getY() && (portal.pos2.getZ() - PortalProtectionRadius) <= block.getZ()) { event.setCancelled(true);
event.setCancelled(true);
break;
}
}
}
} }
//event.getChangedType(); //event.getChangedType();
//event.setCancelled(true); //event.setCancelled(true);

View File

@ -20,7 +20,15 @@ import java.util.logging.Level;
public class Portal { public class Portal {
/**
* Maybe change to get portals active so it cant be changed by plugins :P
*/
public static boolean portalsActive = false; public static boolean portalsActive = false;
/**
* possibly create a hashmap of portals and worlds for quicker access (it might not be tho)
*/
public static AdvancedPortal[] Portals = new AdvancedPortal[0]; public static AdvancedPortal[] Portals = new AdvancedPortal[0];
private static AdvancedPortalsPlugin plugin; private static AdvancedPortalsPlugin plugin;
public static ConfigAccessor portalData = new ConfigAccessor(plugin, "portals.yml"); public static ConfigAccessor portalData = new ConfigAccessor(plugin, "portals.yml");
@ -498,6 +506,10 @@ public class Portal {
*/ */
public static AdvancedPortal locationInPortal(Location loc, int additionalArea){ public static AdvancedPortal locationInPortal(Location loc, int additionalArea){
if (!Portal.portalsActive) {
return null;
}
for (AdvancedPortal portal : Portal.Portals) { for (AdvancedPortal portal : Portal.Portals) {
if (loc.getWorld() != null && portal.worldName.equals(loc.getWorld().getName())) { if (loc.getWorld() != null && portal.worldName.equals(loc.getWorld().getName())) {
if ((portal.pos1.getX() + 1D + additionalArea) >= loc.getX() && (portal.pos1.getY() + additionalArea) >= loc.getY() && (portal.pos1.getZ() + 1D + additionalArea) >= loc.getZ()) { if ((portal.pos1.getX() + 1D + additionalArea) >= loc.getX() && (portal.pos1.getY() + additionalArea) >= loc.getY() && (portal.pos1.getZ() + 1D + additionalArea) >= loc.getZ()) {
@ -510,6 +522,28 @@ public class Portal {
return null; return null;
} }
public static AdvancedPortal blockLocationInPortal(Location loc){
return locationInPortal(loc, 0);
}
public static AdvancedPortal blockLocationInPortal(Location loc, int additionalArea){
if (!Portal.portalsActive) {
return null;
}
for (AdvancedPortal portal : Portal.Portals) {
if (loc.getWorld() != null && portal.worldName.equals(loc.getWorld().getName())) {
if ((portal.pos1.getX() + additionalArea) >= loc.getX() && (portal.pos1.getY() + additionalArea) >= loc.getY() && (portal.pos1.getZ() + additionalArea) >= loc.getZ()) {
if (portal.pos2.getX() - additionalArea <= loc.getX() && portal.pos2.getY() - additionalArea <= loc.getY() && portal.pos2.getZ() - additionalArea <= loc.getZ()) {
return portal;
}
}
}
}
return null;
}
/** /**
* Only returns the first portal found. May have issues with additional area but overlapping should not occour at 0. * Only returns the first portal found. May have issues with additional area but overlapping should not occour at 0.
@ -520,6 +554,10 @@ public class Portal {
*/ */
public static AdvancedPortal playerInPortal(Player player, Location loc, int additionalArea){ public static AdvancedPortal playerInPortal(Player player, Location loc, int additionalArea){
if (!Portal.portalsActive) {
return null;
}
if(loc == null){ if(loc == null){
loc = player.getLocation(); loc = player.getLocation();
} }