Optimized player move event

This commit is contained in:
boy0001 2015-07-15 02:16:32 +10:00
parent 35fa448252
commit d2bd642929
6 changed files with 230 additions and 117 deletions

View File

@ -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());

View File

@ -237,21 +237,20 @@ public class FlagManager {
* @param plot
* @return set of flags
*/
public static Collection<Flag> getPlotFlags(final Plot plot) {
public static HashMap<String, Flag> getPlotFlags(final Plot plot) {
return getSettingFlags(plot.world, plot.settings);
}
public static Collection<Flag> getSettingFlags(final String world, final PlotSettings settings) {
public static HashMap<String, Flag> getSettingFlags(final String world, final PlotSettings settings) {
PlotWorld plotworld = PS.get().getPlotWorld(world);
HashMap<String, Flag> map;
if (plotworld == null) {
map = new HashMap<>();
if (plotworld == null || plotworld.DEFAULT_FLAGS.size() == 0) {
return settings.flags;
}
else {
map = (HashMap<String, Flag>) plotworld.DEFAULT_FLAGS.clone();
}
HashMap<String, Flag> map = (HashMap<String, Flag>) plotworld.DEFAULT_FLAGS.clone();
map.putAll(settings.flags);
return map.values();
return map;
}
}
public static boolean removePlotFlag(final Plot plot, final String id) {

View File

@ -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);
}

View File

@ -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;
@ -355,47 +356,29 @@ public class PlayerEvents extends com.intellectualcrafters.plot.listeners.PlotLi
@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;
}
if (passed) {
player.teleport(event.getTo());
final PlotPlayer pp = BukkitUtil.getPlayer(player);
MainUtil.sendMessage(pp, C.BORDER);
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;
}
}
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());
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());
@ -404,15 +387,102 @@ public class PlayerEvents extends com.intellectualcrafters.plot.listeners.PlotLi
return;
}
}
else if (id.equals(lastId)) {
return;
}
if (!plot.equals(MainUtil.getPlot(f))) {
plotEntry(pp, plot);
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;
}
}
else if (MainUtil.leftPlot(f, t)) {
plot = MainUtil.getPlot(f);
plotExit(BukkitUtil.getPlayer(player), plot);
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());
MainUtil.sendMessage(pp, C.BORDER);
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);
}
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 (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)) {

View File

@ -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,18 +103,41 @@ 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");
final PlayerEnterPlotEvent callEvent = new PlayerEnterPlotEvent(player, plot);
Bukkit.getPluginManager().callEvent(callEvent);
HashMap<String, Flag> 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 = (String) greetingFlag.getValue();
}
else {
greeting = "";
}
final Flag gamemodeFlag = flags.get("gamemode");
if (gamemodeFlag != null) {
player.setGameMode(getGameMode(gamemodeFlag.getValueString()));
}
final Flag flyFlag = FlagManager.getPlotFlag(plot, "fly");
final Flag flyFlag = flags.get("fly");
if (flyFlag != null) {
player.setAllowFlight((boolean) flyFlag.getValue());
}
final Flag timeFlag = FlagManager.getPlotFlag(plot, "time");
final Flag timeFlag = flags.get("time");
if (timeFlag != null) {
try {
final long time = (long) timeFlag.getValue();
@ -114,32 +146,12 @@ public class PlotListener extends APlotListener {
FlagManager.removePlotFlag(plot, "time");
}
}
final Flag weatherFlag = FlagManager.getPlotFlag(plot, "weather");
final Flag weatherFlag = flags.get("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;
if (greetingFlag != null) {
greeting = greetingFlag.getValue() + "";
}
else {
greeting = "";
}
String alias = plot.settings.getAlias();
if (alias.length() == 0) {
alias = plot.toString();
}
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");
Flag musicFlag = flags.get("music");
if (musicFlag != null) {
final Integer id = (Integer) musicFlag.getValue();
if ((id >= 2256 && id <= 2267) || id == 0) {
@ -149,7 +161,7 @@ public class PlotListener extends APlotListener {
player.playEffect(lastLoc, Effect.RECORD_PLAY, 0);
if (id == 0) {
pp.deleteMeta("music");
return;
return true;
}
}
try {
@ -168,9 +180,41 @@ public class PlotListener extends APlotListener {
}
CommentManager.sendTitle(pp, plot);
}
else if (titles) {
greeting = "";
}
else {
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<String, String> 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);
}
}
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) {

View File

@ -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();
}