Cleanup duplicate code

This commit is contained in:
Max Qian 2016-07-31 23:45:24 -07:00
parent 82ff025b3a
commit 65f264aedc
5 changed files with 83 additions and 268 deletions

View File

@ -2,9 +2,8 @@ package com.sekwah.advancedportals.listeners;
import com.sekwah.advancedportals.AdvancedPortalsPlugin;
import com.sekwah.advancedportals.ConfigAccessor;
import com.sekwah.advancedportals.portals.AdvancedPortal;
import com.sekwah.advancedportals.portals.Portal;
import org.bukkit.block.Block;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
@ -32,42 +31,7 @@ public class FlowStopper implements Listener {
@EventHandler(priority = EventPriority.HIGH)
public void onBlockFromTo(BlockFromToEvent event) {
// when checking positions check the block and the to block
Block blockTo = event.getToBlock();
Block block = event.getBlock();
if (!Portal.portalsActive) {
return;
}
for (AdvancedPortal portal : Portal.Portals) {
if (portal.worldName.equals(block.getWorld().getName())) {
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);
}
}
}
}
if (Portal.inPortalRegion(event.getBlock().getLocation(), 3) | Portal.inPortalRegion(event.getToBlock().getLocation(), 3))
event.setCancelled(true);
}
}

View File

@ -6,7 +6,6 @@ import com.sekwah.advancedportals.api.events.WarpEvent;
import com.sekwah.advancedportals.portals.AdvancedPortal;
import com.sekwah.advancedportals.portals.Portal;
import org.bukkit.*;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
@ -18,7 +17,6 @@ import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerMoveEvent;
import org.bukkit.event.player.PlayerPortalEvent;
import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.metadata.FixedMetadataValue;
public class Listeners implements Listener {
@ -82,52 +80,36 @@ public class Listeners implements Listener {
Player player = event.getPlayer();
//Location fromloc = event.getFrom();
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()) {
WarpEvent warpEvent = new WarpEvent(player, portal);
plugin.getServer().getPluginManager().callEvent(warpEvent);
if (portal.inPortal.contains(player)) return;
if (!event.isCancelled()) {
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("_", " ")
if (Portal.locationInPortalTrigger(portal, loc) | Portal.locationInPortalTrigger(portal, eyeLoc)) {
WarpEvent warpEvent = new WarpEvent(player, portal);
plugin.getServer().getPluginManager().callEvent(warpEvent);
if (portal.inPortal.contains(player)) return;
if (!event.isCancelled()) {
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 (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);
}
portal.inPortal.add(player);
return;
}
}
}
}
portal.inPortal.remove(player);
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);
}
portal.inPortal.add(player);
} else portal.inPortal.remove(player);
}
}
@ -168,90 +150,26 @@ public class Listeners implements Listener {
@EventHandler
public void onCombustEntityEvent(EntityCombustEvent event) {
if (!Portal.portalsActive) {
return;
}
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()) {
if ((portal.pos2.getX() - 3D) <= loc.getX() && (portal.pos2.getY() - 3D) <= loc.getY() && (portal.pos2.getZ() - 3D) <= loc.getZ()) {
event.setCancelled(true);
return;
}
}
}
}
if (Portal.inPortalTriggerRegion(event.getEntity().getLocation()))
event.setCancelled(true);
}
@EventHandler
public void onDamEvent(EntityDamageEvent event) {
if (!Portal.portalsActive) {
return;
}
//System.out.println(event.getCause());
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") | Portal.inPortalTriggerRegion(event.getEntity().getLocation()))
event.setCancelled(true);
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()) {
if ((portal.pos2.getX() - 3D) <= loc.getX() && (portal.pos2.getY() - 3D) <= loc.getY() && (portal.pos2.getZ() - 3D) <= loc.getZ()) {
event.setCancelled(true);
return;
}
}
}
}
}
}
// https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/event/player/PlayerTeleportEvent.TeleportCause.html
// Also listen to this possibly.
@SuppressWarnings("deprecation")
@EventHandler
public void onPortalEvent(PlayerPortalEvent event) {
if (!Portal.portalsActive) {
return;
}
Player player = event.getPlayer();
if (player.hasMetadata("hasWarped")) {
if (player.hasMetadata("hasWarped") | Portal.inPortalTriggerRegion(player.getLocation()))
event.setCancelled(true);
return;
}
Location loc = player.getLocation();
Object[] portals = Portal.Portals;
for (AdvancedPortal portal : Portal.Portals) {
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);
}
}
}
}
}
@EventHandler
@ -262,28 +180,13 @@ public class Listeners implements Listener {
Player player = event.getPlayer();
if (player.hasMetadata("selectingPortal") && (event.getAction() == Action.LEFT_CLICK_BLOCK || event.getAction() == Action.RIGHT_CLICK_BLOCK)) {
if (!Portal.portalsActive) {
player.sendMessage("\u00A7a[\u00A77AdvancedPortals\u00A7c] There are no portals that exist to select. Portal selection canceled.");
event.setCancelled(true);
player.removeMetadata("selectingPortal", plugin);
return;
}
Block block = event.getClickedBlock();
for (AdvancedPortal portal : Portal.Portals) {
if (portal.worldName.equals(block.getWorld().getName())) {
if ((portal.pos1.getX()) >= block.getX() && (portal.pos1.getY()) >= block.getY() && (portal.pos1.getZ()) >= block.getZ()) {
if ((portal.pos2.getX()) <= block.getX() && (portal.pos2.getY()) <= block.getY() && (portal.pos2.getZ()) <= block.getZ()) {
player.sendMessage(plugin.customPrefixFail + "\u00A7a You have selected: \u00A7e" + portal.portalName);
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;
}
}
if (Portal.locationInPortal(portal, event.getClickedBlock().getLocation(), 0)) {
player.sendMessage(plugin.customPrefixFail + "\u00A7a You have selected: \u00A7e" + portal.portalName);
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!");

View File

@ -2,7 +2,6 @@ package com.sekwah.advancedportals.listeners;
import com.sekwah.advancedportals.AdvancedPortalsPlugin;
import com.sekwah.advancedportals.ConfigAccessor;
import com.sekwah.advancedportals.portals.AdvancedPortal;
import com.sekwah.advancedportals.portals.Portal;
import org.bukkit.Material;
import org.bukkit.block.Block;
@ -16,7 +15,7 @@ public class PortalPlacer implements Listener {
@SuppressWarnings("unused")
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
// an example is in the interact event in this if statement if((!UseOnlyServerAxe || event.getItem().getItemMeta().getDisplayName().equals("<EFBFBD>eP...
@ -28,7 +27,7 @@ public class PortalPlacer implements Listener {
ConfigAccessor config = new ConfigAccessor(plugin, "config.yml");
this.PortalPlace = config.getConfig().getBoolean("CanBuildPortalBlock");
this.PortalProtectionRadius = config.getConfig().getDouble("PortalProtectionRadius");
this.PortalProtectionRadius = config.getConfig().getInt("PortalProtectionRadius");
if (PortalPlace) {
plugin.getServer().getPluginManager().registerEvents(this, plugin);
@ -39,26 +38,7 @@ public class PortalPlacer implements Listener {
public void onBlockPhysics(BlockPhysicsEvent event) {
Block block = event.getBlock();
Material material = block.getType();
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()) {
if ((portal.pos2.getX() - PortalProtectionRadius) <= block.getX() && (portal.pos2.getY() - PortalProtectionRadius) <= block.getY() && (portal.pos2.getZ() - PortalProtectionRadius) <= block.getZ()) {
event.setCancelled(true);
break;
}
}
}
}
//event.getChangedType();
//event.setCancelled(true);
}
if (material == Material.PORTAL && Portal.inPortalRegion(block.getLocation(), PortalProtectionRadius))
event.setCancelled(true);
}
}

View File

@ -2,7 +2,6 @@ package com.sekwah.advancedportals.listeners;
import com.sekwah.advancedportals.AdvancedPortalsPlugin;
import com.sekwah.advancedportals.ConfigAccessor;
import com.sekwah.advancedportals.portals.AdvancedPortal;
import com.sekwah.advancedportals.portals.Portal;
import org.bukkit.block.Block;
import org.bukkit.event.EventHandler;
@ -23,7 +22,7 @@ public class PortalProtect implements Listener {
// an example is in the interact event in this if statement if((!UseOnlyServerAxe || event.getItem().getItemMeta().getDisplayName().equals("<EFBFBD>eP...
private boolean PortalProtect = true;
private double PortalProtectionRadius = 5D;
private int PortalProtectionRadius = 5;
public PortalProtect(AdvancedPortalsPlugin plugin) {
this.plugin = plugin;
@ -31,97 +30,36 @@ public class PortalProtect implements Listener {
ConfigAccessor config = new ConfigAccessor(plugin, "config.yml");
this.PortalProtect = config.getConfig().getBoolean("PortalProtection");
this.PortalProtectionRadius = config.getConfig().getDouble("PortalProtectionRadius");
this.PortalProtectionRadius = config.getConfig().getInt("PortalProtectionRadius");
if (PortalProtect) {
plugin.getServer().getPluginManager().registerEvents(this, plugin);
}
}
@SuppressWarnings("unused")
@EventHandler(priority = EventPriority.HIGH)
public void onBlockBreak(BlockBreakEvent event) {
if (!Portal.portalsActive) {
return;
}
if (!event.getPlayer().hasPermission("advancedportals.build")) {
Block block = event.getBlock();
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()) {
if ((portal.pos2.getX() - PortalProtectionRadius) <= block.getX() && (portal.pos2.getY() - PortalProtectionRadius) <= block.getY() && (portal.pos2.getZ() - PortalProtectionRadius) <= block.getZ()) {
event.setCancelled(true);
break;
}
}
}
}
}
if (!event.getPlayer().hasPermission("advancedportals.build")
&& Portal.inPortalRegion(event.getBlock().getLocation(), PortalProtectionRadius))
event.setCancelled(true);
}
@SuppressWarnings("unused")
@EventHandler(priority = EventPriority.HIGH)
public void onBlockPlace(BlockPlaceEvent event) {
if (!Portal.portalsActive) {
return;
}
if (!event.getPlayer().hasPermission("advancedportals.build")) {
Block block = event.getBlock();
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()) {
if ((portal.pos2.getX() - PortalProtectionRadius) <= block.getX() && (portal.pos2.getY() - PortalProtectionRadius) <= block.getY() && (portal.pos2.getZ() - PortalProtectionRadius) <= block.getZ()) {
event.setCancelled(true);
break;
}
}
}
}
}
if (!event.getPlayer().hasPermission("advancedportals.build")
&& Portal.inPortalRegion(event.getBlock().getLocation(), PortalProtectionRadius))
event.setCancelled(true);
}
@EventHandler(priority = EventPriority.HIGH)
public void onExplosion(EntityExplodeEvent event) {
if (!Portal.portalsActive) {
return;
}
List<Block> blockList = event.blockList();
for (int i = 0; i < blockList.size(); i++) {
Block block = blockList.get(i);
Object[] portals = Portal.Portals;
for (AdvancedPortal portal : Portal.Portals) { // change for format for(int i = 0; i < portals.length; i++){
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()) {
if ((portal.pos2.getX() - PortalProtectionRadius) <= block.getX() && (portal.pos2.getY() - PortalProtectionRadius) <= block.getY() && (portal.pos2.getZ() - PortalProtectionRadius) <= block.getZ()) {
blockList.remove(i);
i--;
}
}
}
if (Portal.inPortalRegion(block.getLocation(), PortalProtectionRadius)) {
blockList.remove(i);
i--;
}
}
}
}

View File

@ -496,4 +496,34 @@ public class Portal {
return false;
}
}
public static boolean inPortalTriggerRegion(Location loc) {
for (AdvancedPortal portal : Portal.Portals)
if (Portal.locationInPortalTrigger(portal, loc))
return true;
return false;
}
public static boolean locationInPortalTrigger(AdvancedPortal portal, Location loc) {
if (portal.trigger.equals(loc.getBlock().getType()))
return locationInPortal(portal, loc, 0);
return false;
}
public static boolean inPortalRegion(Location loc, int additionalArea) {
for (AdvancedPortal portal : Portal.Portals)
if (Portal.locationInPortal(portal, loc, additionalArea))
return true;
return false;
}
public static boolean locationInPortal(AdvancedPortal portal, Location loc, int additionalArea) {
if (!portalsActive)
return false;
if (loc.getWorld() != null && portal.worldName.equals(loc.getWorld().getName()))
if ((portal.pos1.getX() + 1 + additionalArea) >= loc.getX() && (portal.pos1.getY() + additionalArea) >= loc.getY() && (portal.pos1.getZ() + 1 + additionalArea) >= loc.getZ())
if (portal.pos2.getX() - additionalArea <= loc.getX() && portal.pos2.getY() - additionalArea <= loc.getY() && portal.pos2.getZ() - additionalArea <= loc.getZ())
return true;
return false;
}
}