From d2bd6429294d289c52faba4d2c60d6f3f3e5c83b Mon Sep 17 00:00:00 2001 From: boy0001 Date: Wed, 15 Jul 2015 02:16:32 +1000 Subject: [PATCH] Optimized player move event --- .../plot/commands/Info.java | 2 +- .../plot/flag/FlagManager.java | 15 +- .../plot/listeners/APlotListener.java | 4 +- .../plot/listeners/PlayerEvents.java | 166 +++++++++++++----- .../plot/listeners/PlotListener.java | 159 +++++++++++------ .../plot/util/bukkit/BukkitChunkManager.java | 1 - 6 files changed, 230 insertions(+), 117 deletions(-) diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Info.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Info.java index be533dfd5..7d5dbfd41 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Info.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/commands/Info.java @@ -200,7 +200,7 @@ public class Info extends SubCommand { final String members = getPlayerList(plot.members); final String denied = getPlayerList(plot.denied); - final String flags = StringMan.replaceFromMap("$2" + (StringUtils.join(FlagManager.getPlotFlags(plot), "").length() > 0 ? StringUtils.join(FlagManager.getPlotFlags(plot), "$1, $2") : C.NONE.s()), C.replacements); + final String flags = StringMan.replaceFromMap("$2" + (StringUtils.join(FlagManager.getPlotFlags(plot).values(), "").length() > 0 ? StringUtils.join(FlagManager.getPlotFlags(plot).values(), "$1, $2") : C.NONE.s()), C.replacements); final boolean build = (player == null) || plot.isAdded(player.getUUID()); String owner = plot.owner == null ? "unowned" : getPlayerList(plot.getOwners()); diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/flag/FlagManager.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/flag/FlagManager.java index 7b40ff709..9b565ab08 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/flag/FlagManager.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/flag/FlagManager.java @@ -237,21 +237,20 @@ public class FlagManager { * @param plot * @return set of flags */ - public static Collection getPlotFlags(final Plot plot) { + public static HashMap getPlotFlags(final Plot plot) { return getSettingFlags(plot.world, plot.settings); } - public static Collection getSettingFlags(final String world, final PlotSettings settings) { + public static HashMap getSettingFlags(final String world, final PlotSettings settings) { PlotWorld plotworld = PS.get().getPlotWorld(world); - HashMap map; - if (plotworld == null) { - map = new HashMap<>(); + if (plotworld == null || plotworld.DEFAULT_FLAGS.size() == 0) { + return settings.flags; } else { - map = (HashMap) plotworld.DEFAULT_FLAGS.clone(); + HashMap map = (HashMap) plotworld.DEFAULT_FLAGS.clone(); + map.putAll(settings.flags); + return map; } - map.putAll(settings.flags); - return map.values(); } public static boolean removePlotFlag(final Plot plot, final String id) { diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/APlotListener.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/APlotListener.java index 6a50dacf8..baa07c6ea 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/APlotListener.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/APlotListener.java @@ -7,7 +7,7 @@ public abstract class APlotListener { public static APlotListener manager; - public abstract void plotExit(final PlotPlayer player, final Plot plot); + public abstract boolean plotEntry(final PlotPlayer player, final Plot plot); - public abstract void plotEntry(final PlotPlayer player, final Plot plot); + public abstract boolean plotExit(final PlotPlayer player, final Plot plot); } diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlayerEvents.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlayerEvents.java index 8bddf4999..6d0492a35 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlayerEvents.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlayerEvents.java @@ -100,6 +100,7 @@ import com.intellectualcrafters.plot.object.PlotBlock; import com.intellectualcrafters.plot.object.PlotHandler; import com.intellectualcrafters.plot.object.PlotId; import com.intellectualcrafters.plot.object.PlotInventory; +import com.intellectualcrafters.plot.object.PlotManager; import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.object.PlotWorld; import com.intellectualcrafters.plot.object.StringWrapper; @@ -352,67 +353,136 @@ public class PlayerEvents extends com.intellectualcrafters.plot.listeners.PlotLi } plotEntry(pp, plot); } - + @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) public void PlayerMove(final PlayerMoveEvent event) { - final Location f = BukkitUtil.getLocation(event.getFrom()); - final Location t = BukkitUtil.getLocation(event.getTo()); - if ((f.getX() != t.getX()) || (f.getZ() != t.getZ())) { - final Player player = event.getPlayer(); - if (Settings.TELEPORT_DELAY != 0) { - TaskManager.TELEPORT_QUEUE.remove(player.getName()); - } - final String worldname = t.getWorld(); - if (!PS.get().isPlotWorld(worldname)) { + org.bukkit.Location from = event.getFrom(); + org.bukkit.Location to = event.getTo(); + int x1, x2; + if ((x1 = (int) from.getX()) != (x2 = (int) to.getX()) ) { + String worldname = to.getWorld().getName(); + PlotWorld plotworld = PS.get().getPlotWorld(worldname); + if (plotworld == null) { return; } - if (MainUtil.worldBorder.containsKey(worldname)) { - final int border = MainUtil.getBorder(worldname); - boolean passed = true; - if (t.getX() > border) { - event.getTo().setX(border); - } else if (t.getX() < -border) { - event.getTo().setX(-border); - } else if (t.getZ() > border) { - event.getTo().setZ(border); - } else if (t.getZ() < -border) { - event.getTo().setZ(-border); - } else { - passed = false; + PlotManager plotManager = PS.get().getPlotManager(worldname); + PlotId id = plotManager.getPlotId(plotworld, x2, 0, (int) to.getZ()); + Player player = event.getPlayer(); + PlotPlayer pp = BukkitUtil.getPlayer(player); + PlotId lastId = (PlotId) pp.getMeta("lastplotid"); + if (id == null) { + if (lastId == null) { + return; } - if (passed) { + Plot plot = MainUtil.getPlot(worldname, lastId); + if (!plotExit(pp, plot)) { + MainUtil.sendMessage(pp, C.NO_PERMISSION, "plots.admin.exit.denied"); + if (plot.equals(MainUtil.getPlot(BukkitUtil.getLocation(from)))) { + player.teleport(from); + } + else { + player.teleport(player.getWorld().getSpawnLocation()); + } + event.setCancelled(true); + return; + } + } + else if (id.equals(lastId)) { + return; + } + else { + Plot plot = MainUtil.getPlot(worldname, id); + if (!plotEntry(pp, plot)) { + MainUtil.sendMessage(pp, C.NO_PERMISSION, "plots.admin.entry.denied"); + if (!plot.equals(MainUtil.getPlot(BukkitUtil.getLocation(from)))) { + player.teleport(from); + } + else { + player.teleport(player.getWorld().getSpawnLocation()); + } + event.setCancelled(true); + return; + } + } + Integer border = MainUtil.worldBorder.get(worldname); + if (border != null) { + if (x2 > border) { + to.setX(border - 4); + player.teleport(event.getTo()); + MainUtil.sendMessage(pp, C.BORDER); + return; + } + else if (x2 < -border) { + to.setX(-border + 4); player.teleport(event.getTo()); - final PlotPlayer pp = BukkitUtil.getPlayer(player); MainUtil.sendMessage(pp, C.BORDER); return; } } - Plot plot = MainUtil.getPlot(t); - if (plot != null) { - final PlotPlayer pp = BukkitUtil.getPlayer(player); - if (plot.denied.size() > 0) { - if (plot.isDenied(pp.getUUID())) { - if (!Permissions.hasPermission(pp, "plots.admin.entry.denied")) { - MainUtil.sendMessage(pp, C.NO_PERMISSION, "plots.admin.entry.denied"); - if (!plot.equals(MainUtil.getPlot(BukkitUtil.getLocation(event.getFrom())))) { - player.teleport(event.getFrom()); - } - else { - player.teleport(player.getWorld().getSpawnLocation()); - } - event.setCancelled(true); - return; - } + return; + } + int z1, z2; + if ((z1 = (int) from.getZ()) != (z2 = (int) to.getZ()) ) { + String worldname = to.getWorld().getName(); + PlotWorld plotworld = PS.get().getPlotWorld(worldname); + if (plotworld == null) { + return; + } + PlotManager plotManager = PS.get().getPlotManager(worldname); + PlotId id = plotManager.getPlotId(plotworld, x2, 0, z2); + Player player = event.getPlayer(); + PlotPlayer pp = BukkitUtil.getPlayer(player); + PlotId lastId = (PlotId) pp.getMeta("lastplotid"); + if (id == null) { + if (lastId == null) { + return; + } + Plot plot = MainUtil.getPlot(worldname, lastId); + if (!plotExit(pp, plot)) { + MainUtil.sendMessage(pp, C.NO_PERMISSION, "plots.admin.exit.denied"); + if (plot.equals(MainUtil.getPlot(BukkitUtil.getLocation(from)))) { + player.teleport(from); } - } - if (!plot.equals(MainUtil.getPlot(f))) { - plotEntry(pp, plot); + else { + player.teleport(player.getWorld().getSpawnLocation()); + } + event.setCancelled(true); + return; } } - else if (MainUtil.leftPlot(f, t)) { - plot = MainUtil.getPlot(f); - plotExit(BukkitUtil.getPlayer(player), plot); + else if (id.equals(lastId)) { + return; } + else { + Plot plot = MainUtil.getPlot(worldname, id); + if (!plotEntry(pp, plot)) { + MainUtil.sendMessage(pp, C.NO_PERMISSION, "plots.admin.entry.denied"); + if (!plot.equals(MainUtil.getPlot(BukkitUtil.getLocation(from)))) { + player.teleport(from); + } + else { + player.teleport(player.getWorld().getSpawnLocation()); + } + event.setCancelled(true); + return; + } + } + Integer border = MainUtil.worldBorder.get(worldname); + if (border != null) { + if (z2 > border) { + to.setZ(border - 4); + player.teleport(event.getTo()); + MainUtil.sendMessage(pp, C.BORDER); + return; + } + else if (z2 < -border) { + to.setZ(-border + 4); + player.teleport(event.getTo()); + MainUtil.sendMessage(pp, C.BORDER); + return; + } + } + return; } } @@ -1221,7 +1291,7 @@ public class PlayerEvents extends com.intellectualcrafters.plot.listeners.PlotLi } else { if (MainUtil.leftPlot(f, t)) { final Plot plot2 = MainUtil.getPlot(f); - APlotListener.manager.plotExit(BukkitUtil.getPlayer(player), plot2); + plotExit(BukkitUtil.getPlayer(player), plot2); } } if ((q.getX() >= 29999999) || (q.getX() <= -29999999) || (q.getZ() >= 29999999) || (q.getZ() <= -29999999)) { diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlotListener.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlotListener.java index 625e9f5bf..45f7724c5 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlotListener.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/listeners/PlotListener.java @@ -21,6 +21,9 @@ package com.intellectualcrafters.plot.listeners; import java.util.Arrays; +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; import java.util.UUID; import org.bukkit.Bukkit; @@ -41,9 +44,15 @@ import com.intellectualcrafters.plot.flag.FlagManager; import com.intellectualcrafters.plot.flag.FlagValue; import com.intellectualcrafters.plot.object.BukkitPlayer; import com.intellectualcrafters.plot.object.Plot; +import com.intellectualcrafters.plot.object.PlotId; import com.intellectualcrafters.plot.object.PlotPlayer; +import com.intellectualcrafters.plot.object.PlotWorld; import com.intellectualcrafters.plot.object.comment.CommentManager; import com.intellectualcrafters.plot.titles.AbstractTitle; +import com.intellectualcrafters.plot.util.MainUtil; +import com.intellectualcrafters.plot.util.Permissions; +import com.intellectualcrafters.plot.util.StringMan; +import com.intellectualcrafters.plot.util.TaskManager; import com.intellectualcrafters.plot.util.bukkit.UUIDHandler; /** @@ -94,83 +103,118 @@ public class PlotListener extends APlotListener { } } - public void plotEntry(final PlotPlayer pp, final Plot plot) { + public boolean plotEntry(final PlotPlayer pp, final Plot plot) { + if (plot.isDenied(pp.getUUID()) && !Permissions.hasPermission(pp, "plots.admin.entry.denied")) { + return false; + } + pp.setMeta("lastplotid", plot.id); final Player player = ((BukkitPlayer) pp).player; if (plot.hasOwner()) { - final Flag gamemodeFlag = FlagManager.getPlotFlag(plot, "gamemode"); - if (gamemodeFlag != null) { - player.setGameMode(getGameMode(gamemodeFlag.getValueString())); - } - final Flag flyFlag = FlagManager.getPlotFlag(plot, "fly"); - if (flyFlag != null) { - player.setAllowFlight((boolean) flyFlag.getValue()); - } - final Flag timeFlag = FlagManager.getPlotFlag(plot, "time"); - if (timeFlag != null) { - try { - final long time = (long) timeFlag.getValue(); - player.setPlayerTime(time, false); - } catch (final Exception e) { - FlagManager.removePlotFlag(plot, "time"); - } - } - final Flag weatherFlag = FlagManager.getPlotFlag(plot, "weather"); - if (weatherFlag != null) { - setWeather(player, weatherFlag.getValueString()); - } - if ((FlagManager.isBooleanFlag(plot, "titles", Settings.TITLES)) && (C.TITLE_ENTERED_PLOT.s().length() > 2)) { - Flag greetingFlag = FlagManager.getPlotFlag(plot, "greeting"); - String greeting; + final PlayerEnterPlotEvent callEvent = new PlayerEnterPlotEvent(player, plot); + Bukkit.getPluginManager().callEvent(callEvent); + + HashMap flags = FlagManager.getPlotFlags(plot); + int size = flags.size(); + boolean titles = Settings.TITLES; + final String greeting; + + if (size != 0) { + Flag titleFlag = flags.get("titles"); + titles = (Boolean) titleFlag.getValue(); + Flag greetingFlag = flags.get("greeting"); if (greetingFlag != null) { - greeting = greetingFlag.getValue() + ""; + greeting = (String) greetingFlag.getValue(); } else { greeting = ""; } - String alias = plot.settings.getAlias(); - if (alias.length() == 0) { - alias = plot.toString(); + + final Flag gamemodeFlag = flags.get("gamemode"); + if (gamemodeFlag != null) { + player.setGameMode(getGameMode(gamemodeFlag.getValueString())); } - final String sTitleMain = C.TITLE_ENTERED_PLOT.s().replaceAll("%x%", plot.id.x + "").replaceAll("%z%", plot.id.y + "").replaceAll("%world%", plot.world + "").replaceAll("%greeting%", greeting).replaceAll("%s", getName(plot.owner)).replaceAll("%alias%", alias); - final String sTitleSub = C.TITLE_ENTERED_PLOT_SUB.s().replaceAll("%x%", plot.id.x + "").replaceAll("%z%", plot.id.y + "").replaceAll("%world%", plot.world + "").replaceAll("%greeting%", greeting).replaceAll("%s", getName(plot.owner)).replaceAll("%alias%", alias); - AbstractTitle.sendTitle(pp, sTitleMain, sTitleSub, ChatColor.valueOf(C.TITLE_ENTERED_PLOT_COLOR.s()), ChatColor.valueOf(C.TITLE_ENTERED_PLOT_SUB_COLOR.s())); - } - { - final PlayerEnterPlotEvent callEvent = new PlayerEnterPlotEvent(player, plot); - Bukkit.getPluginManager().callEvent(callEvent); - } - Flag musicFlag = FlagManager.getPlotFlag(plot, "music"); - if (musicFlag != null) { - final Integer id = (Integer) musicFlag.getValue(); - if ((id >= 2256 && id <= 2267) || id == 0) { - final org.bukkit.Location loc = player.getLocation(); + final Flag flyFlag = flags.get("fly"); + if (flyFlag != null) { + player.setAllowFlight((boolean) flyFlag.getValue()); + } + final Flag timeFlag = flags.get("time"); + if (timeFlag != null) { + try { + final long time = (long) timeFlag.getValue(); + player.setPlayerTime(time, false); + } catch (final Exception e) { + FlagManager.removePlotFlag(plot, "time"); + } + } + final Flag weatherFlag = flags.get("weather"); + if (weatherFlag != null) { + setWeather(player, weatherFlag.getValueString()); + } + + Flag musicFlag = flags.get("music"); + if (musicFlag != null) { + final Integer id = (Integer) musicFlag.getValue(); + if ((id >= 2256 && id <= 2267) || id == 0) { + final org.bukkit.Location loc = player.getLocation(); + org.bukkit.Location lastLoc = (org.bukkit.Location) pp.getMeta("music"); + if (lastLoc != null) { + player.playEffect(lastLoc, Effect.RECORD_PLAY, 0); + if (id == 0) { + pp.deleteMeta("music"); + return true; + } + } + try { + pp.setMeta("music", loc); + player.playEffect(loc, Effect.RECORD_PLAY, Material.getMaterial(id)); + } + catch (Exception e) {} + } + } + else { org.bukkit.Location lastLoc = (org.bukkit.Location) pp.getMeta("music"); if (lastLoc != null) { + pp.deleteMeta("music"); player.playEffect(lastLoc, Effect.RECORD_PLAY, 0); - if (id == 0) { - pp.deleteMeta("music"); - return; - } } - try { - pp.setMeta("music", loc); - player.playEffect(loc, Effect.RECORD_PLAY, Material.getMaterial(id)); - } - catch (Exception e) {} } + CommentManager.sendTitle(pp, plot); + } + else if (titles) { + greeting = ""; } else { - org.bukkit.Location lastLoc = (org.bukkit.Location) pp.getMeta("music"); - if (lastLoc != null) { - pp.deleteMeta("music"); - player.playEffect(lastLoc, Effect.RECORD_PLAY, 0); + return true; + } + if (titles) { + if (C.TITLE_ENTERED_PLOT.s().length() != 0 || C.TITLE_ENTERED_PLOT_SUB.s().length() != 0) { + TaskManager.runTaskLaterAsync(new Runnable() { + @Override + public void run() { + PlotId id = (PlotId) pp.getMeta("lastplotid"); + if (plot.id.equals(id)) { + Map replacements = new HashMap<>(); + replacements.put("%x%", id.x + ""); + replacements.put("%z%", id.y + ""); + replacements.put("%world%", plot.world); + replacements.put("%greeting%", greeting); + replacements.put("%alias", plot.toString()); + replacements.put("%s", getName(plot.owner)); + String main = StringMan.replaceFromMap(C.TITLE_ENTERED_PLOT.s(), replacements); + String sub = StringMan.replaceFromMap(C.TITLE_ENTERED_PLOT_SUB.s(), replacements); + AbstractTitle.sendTitle(pp, main, sub, ChatColor.valueOf(C.TITLE_ENTERED_PLOT_COLOR.s()), ChatColor.valueOf(C.TITLE_ENTERED_PLOT_SUB_COLOR.s())); + } + } + }, 20); } } - CommentManager.sendTitle(pp, plot); + return true; } + return true; } - public void plotExit(final PlotPlayer pp, final Plot plot) { + public boolean plotExit(final PlotPlayer pp, final Plot plot) { + pp.deleteMeta("lastplotid"); Player player = ((BukkitPlayer) pp).player; final PlayerLeavePlotEvent callEvent = new PlayerLeavePlotEvent(player, plot); Bukkit.getPluginManager().callEvent(callEvent); @@ -191,6 +235,7 @@ public class PlotListener extends APlotListener { pp.deleteMeta("music"); player.playEffect(lastLoc, Effect.RECORD_PLAY, 0); } + return true; } public boolean getFlagValue(final String value) { diff --git a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/bukkit/BukkitChunkManager.java b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/bukkit/BukkitChunkManager.java index 02291689a..88d5f52f9 100644 --- a/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/bukkit/BukkitChunkManager.java +++ b/PlotSquared/src/main/java/com/intellectualcrafters/plot/util/bukkit/BukkitChunkManager.java @@ -879,7 +879,6 @@ public class BukkitChunkManager extends ChunkManager { final Player player = (Player) entity; final PlotPlayer pp = BukkitUtil.getPlayer(player); pp.teleport(MainUtil.getDefaultHome(plot)); - APlotListener.manager.plotExit(pp, plot); } else { entity.remove(); }