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 members = getPlayerList(plot.members);
final String denied = getPlayerList(plot.denied); 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()); final boolean build = (player == null) || plot.isAdded(player.getUUID());
String owner = plot.owner == null ? "unowned" : getPlayerList(plot.getOwners()); String owner = plot.owner == null ? "unowned" : getPlayerList(plot.getOwners());

View File

@ -237,21 +237,20 @@ public class FlagManager {
* @param plot * @param plot
* @return set of flags * @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); 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); PlotWorld plotworld = PS.get().getPlotWorld(world);
HashMap<String, Flag> map; if (plotworld == null || plotworld.DEFAULT_FLAGS.size() == 0) {
if (plotworld == null) { return settings.flags;
map = new HashMap<>();
} }
else { else {
map = (HashMap<String, Flag>) plotworld.DEFAULT_FLAGS.clone(); HashMap<String, Flag> map = (HashMap<String, Flag>) plotworld.DEFAULT_FLAGS.clone();
}
map.putAll(settings.flags); map.putAll(settings.flags);
return map.values(); return map;
}
} }
public static boolean removePlotFlag(final Plot plot, final String id) { 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 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.PlotHandler;
import com.intellectualcrafters.plot.object.PlotId; import com.intellectualcrafters.plot.object.PlotId;
import com.intellectualcrafters.plot.object.PlotInventory; import com.intellectualcrafters.plot.object.PlotInventory;
import com.intellectualcrafters.plot.object.PlotManager;
import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.object.PlotPlayer;
import com.intellectualcrafters.plot.object.PlotWorld; import com.intellectualcrafters.plot.object.PlotWorld;
import com.intellectualcrafters.plot.object.StringWrapper; 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) @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void PlayerMove(final PlayerMoveEvent event) { public void PlayerMove(final PlayerMoveEvent event) {
final Location f = BukkitUtil.getLocation(event.getFrom()); org.bukkit.Location from = event.getFrom();
final Location t = BukkitUtil.getLocation(event.getTo()); org.bukkit.Location to = event.getTo();
if ((f.getX() != t.getX()) || (f.getZ() != t.getZ())) { int x1, x2;
final Player player = event.getPlayer(); if ((x1 = (int) from.getX()) != (x2 = (int) to.getX()) ) {
if (Settings.TELEPORT_DELAY != 0) { String worldname = to.getWorld().getName();
TaskManager.TELEPORT_QUEUE.remove(player.getName()); PlotWorld plotworld = PS.get().getPlotWorld(worldname);
} if (plotworld == null) {
final String worldname = t.getWorld();
if (!PS.get().isPlotWorld(worldname)) {
return; return;
} }
if (MainUtil.worldBorder.containsKey(worldname)) { PlotManager plotManager = PS.get().getPlotManager(worldname);
final int border = MainUtil.getBorder(worldname); PlotId id = plotManager.getPlotId(plotworld, x2, 0, (int) to.getZ());
boolean passed = true; Player player = event.getPlayer();
if (t.getX() > border) { PlotPlayer pp = BukkitUtil.getPlayer(player);
event.getTo().setX(border); PlotId lastId = (PlotId) pp.getMeta("lastplotid");
} else if (t.getX() < -border) { if (id == null) {
event.getTo().setX(-border); if (lastId == null) {
} 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);
return; return;
} }
} Plot plot = MainUtil.getPlot(worldname, lastId);
Plot plot = MainUtil.getPlot(t); if (!plotExit(pp, plot)) {
if (plot != null) { MainUtil.sendMessage(pp, C.NO_PERMISSION, "plots.admin.exit.denied");
final PlotPlayer pp = BukkitUtil.getPlayer(player); if (plot.equals(MainUtil.getPlot(BukkitUtil.getLocation(from)))) {
if (plot.denied.size() > 0) { player.teleport(from);
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 { else {
player.teleport(player.getWorld().getSpawnLocation()); player.teleport(player.getWorld().getSpawnLocation());
@ -404,15 +387,102 @@ public class PlayerEvents extends com.intellectualcrafters.plot.listeners.PlotLi
return; return;
} }
} }
else if (id.equals(lastId)) {
return;
} }
if (!plot.equals(MainUtil.getPlot(f))) { else {
plotEntry(pp, plot); 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)) { Integer border = MainUtil.worldBorder.get(worldname);
plot = MainUtil.getPlot(f); if (border != null) {
plotExit(BukkitUtil.getPlayer(player), plot); 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 { } else {
if (MainUtil.leftPlot(f, t)) { if (MainUtil.leftPlot(f, t)) {
final Plot plot2 = MainUtil.getPlot(f); 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)) { if ((q.getX() >= 29999999) || (q.getX() <= -29999999) || (q.getZ() >= 29999999) || (q.getZ() <= -29999999)) {

View File

@ -21,6 +21,9 @@
package com.intellectualcrafters.plot.listeners; package com.intellectualcrafters.plot.listeners;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID; import java.util.UUID;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
@ -41,9 +44,15 @@ import com.intellectualcrafters.plot.flag.FlagManager;
import com.intellectualcrafters.plot.flag.FlagValue; import com.intellectualcrafters.plot.flag.FlagValue;
import com.intellectualcrafters.plot.object.BukkitPlayer; import com.intellectualcrafters.plot.object.BukkitPlayer;
import com.intellectualcrafters.plot.object.Plot; import com.intellectualcrafters.plot.object.Plot;
import com.intellectualcrafters.plot.object.PlotId;
import com.intellectualcrafters.plot.object.PlotPlayer; import com.intellectualcrafters.plot.object.PlotPlayer;
import com.intellectualcrafters.plot.object.PlotWorld;
import com.intellectualcrafters.plot.object.comment.CommentManager; import com.intellectualcrafters.plot.object.comment.CommentManager;
import com.intellectualcrafters.plot.titles.AbstractTitle; 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; 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; final Player player = ((BukkitPlayer) pp).player;
if (plot.hasOwner()) { 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) { if (gamemodeFlag != null) {
player.setGameMode(getGameMode(gamemodeFlag.getValueString())); player.setGameMode(getGameMode(gamemodeFlag.getValueString()));
} }
final Flag flyFlag = FlagManager.getPlotFlag(plot, "fly"); final Flag flyFlag = flags.get("fly");
if (flyFlag != null) { if (flyFlag != null) {
player.setAllowFlight((boolean) flyFlag.getValue()); player.setAllowFlight((boolean) flyFlag.getValue());
} }
final Flag timeFlag = FlagManager.getPlotFlag(plot, "time"); final Flag timeFlag = flags.get("time");
if (timeFlag != null) { if (timeFlag != null) {
try { try {
final long time = (long) timeFlag.getValue(); final long time = (long) timeFlag.getValue();
@ -114,32 +146,12 @@ public class PlotListener extends APlotListener {
FlagManager.removePlotFlag(plot, "time"); FlagManager.removePlotFlag(plot, "time");
} }
} }
final Flag weatherFlag = FlagManager.getPlotFlag(plot, "weather"); final Flag weatherFlag = flags.get("weather");
if (weatherFlag != null) { if (weatherFlag != null) {
setWeather(player, weatherFlag.getValueString()); setWeather(player, weatherFlag.getValueString());
} }
if ((FlagManager.isBooleanFlag(plot, "titles", Settings.TITLES)) && (C.TITLE_ENTERED_PLOT.s().length() > 2)) {
Flag greetingFlag = FlagManager.getPlotFlag(plot, "greeting"); Flag musicFlag = flags.get("music");
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");
if (musicFlag != null) { if (musicFlag != null) {
final Integer id = (Integer) musicFlag.getValue(); final Integer id = (Integer) musicFlag.getValue();
if ((id >= 2256 && id <= 2267) || id == 0) { if ((id >= 2256 && id <= 2267) || id == 0) {
@ -149,7 +161,7 @@ public class PlotListener extends APlotListener {
player.playEffect(lastLoc, Effect.RECORD_PLAY, 0); player.playEffect(lastLoc, Effect.RECORD_PLAY, 0);
if (id == 0) { if (id == 0) {
pp.deleteMeta("music"); pp.deleteMeta("music");
return; return true;
} }
} }
try { try {
@ -168,9 +180,41 @@ public class PlotListener extends APlotListener {
} }
CommentManager.sendTitle(pp, plot); 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; Player player = ((BukkitPlayer) pp).player;
final PlayerLeavePlotEvent callEvent = new PlayerLeavePlotEvent(player, plot); final PlayerLeavePlotEvent callEvent = new PlayerLeavePlotEvent(player, plot);
Bukkit.getPluginManager().callEvent(callEvent); Bukkit.getPluginManager().callEvent(callEvent);
@ -191,6 +235,7 @@ public class PlotListener extends APlotListener {
pp.deleteMeta("music"); pp.deleteMeta("music");
player.playEffect(lastLoc, Effect.RECORD_PLAY, 0); player.playEffect(lastLoc, Effect.RECORD_PLAY, 0);
} }
return true;
} }
public boolean getFlagValue(final String value) { public boolean getFlagValue(final String value) {

View File

@ -879,7 +879,6 @@ public class BukkitChunkManager extends ChunkManager {
final Player player = (Player) entity; final Player player = (Player) entity;
final PlotPlayer pp = BukkitUtil.getPlayer(player); final PlotPlayer pp = BukkitUtil.getPlayer(player);
pp.teleport(MainUtil.getDefaultHome(plot)); pp.teleport(MainUtil.getDefaultHome(plot));
APlotListener.manager.plotExit(pp, plot);
} else { } else {
entity.remove(); entity.remove();
} }