Use 1 map instead of 3

This commit is contained in:
Shevchik 2014-07-13 21:05:28 +04:00
parent cc80fef7e0
commit 3a3c322c0d

View File

@ -18,13 +18,11 @@ public class PlayerSitData {
this.plugin = plugin; this.plugin = plugin;
} }
private HashMap<String, Entity> sit = new HashMap<String, Entity>(); private HashMap<Player, SitData> sit = new HashMap<Player, SitData>();
private HashMap<Block, String> sitblock = new HashMap<Block, String>(); private HashMap<Block, Player> sitblock = new HashMap<Block, Player>();
private HashMap<String, Location> sitstopteleportloc = new HashMap<String, Location>();
private HashMap<String, Integer> sittask = new HashMap<String, Integer>();
public boolean isSitting(Player player) { public boolean isSitting(Player player) {
return sit.containsKey(player.getName()); return sit.containsKey(player) && sit.get(player).sitting;
} }
public boolean isBlockOccupied(Block block) { public boolean isBlockOccupied(Block block) {
@ -32,56 +30,57 @@ public class PlayerSitData {
} }
public Player getPlayerOnChair(Block chair) { public Player getPlayerOnChair(Block chair) {
return Bukkit.getPlayerExact(sitblock.get(chair)); return sitblock.get(chair);
} }
public boolean sitPlayer(Player player, Block blocktooccupy, Location sitlocation) { public boolean sitPlayer(final Player player, Block blocktooccupy, Location sitlocation) {
PlayerChairSitEvent playersitevent = new PlayerChairSitEvent(player, sitlocation); PlayerChairSitEvent playersitevent = new PlayerChairSitEvent(player, sitlocation.clone());
Bukkit.getPluginManager().callEvent(playersitevent); Bukkit.getPluginManager().callEvent(playersitevent);
if (playersitevent.isCancelled()) { if (playersitevent.isCancelled()) {
return false; return false;
} }
sitlocation = playersitevent.getSitLocation(); sitlocation = playersitevent.getSitLocation().clone();
try { try {
if (plugin.notifyplayer) { if (plugin.notifyplayer) {
player.sendMessage(plugin.msgSitting); player.sendMessage(plugin.msgSitting);
} }
sitstopteleportloc.put(player.getName(), player.getLocation()); SitData sitdata = new SitData();
player.teleport(sitlocation);
Location arrowloc = sitlocation.getBlock().getLocation().add(0.5, 0 , 0.5); Location arrowloc = sitlocation.getBlock().getLocation().add(0.5, 0 , 0.5);
Entity arrow = plugin.getNMSAccess().spawnArrow(arrowloc); Entity arrow = plugin.getNMSAccess().spawnArrow(arrowloc);
sitdata.arrow = arrow;
sitdata.teleportloc = player.getLocation();
int task = Bukkit.getScheduler().scheduleSyncRepeatingTask(
plugin,
new Runnable() {
@Override
public void run() {
reSitPlayer(player);
}
},
1000, 1000
);
sitdata.resittask = task;
player.teleport(sitlocation);
arrow.setPassenger(player); arrow.setPassenger(player);
sit.put(player.getName(), arrow); sitdata.sitting = true;
sitblock.put(blocktooccupy, player.getName()); sit.put(player, sitdata);
startReSitTask(player); sitblock.put(blocktooccupy, player);
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
return true; return true;
} }
public void startReSitTask(final Player player) {
int task = Bukkit.getScheduler().scheduleSyncRepeatingTask(
plugin,
new Runnable() {
@Override
public void run() {
reSitPlayer(player);
}
},
1000, 1000
);
sittask.put(player.getName(), task);
}
public void reSitPlayer(final Player player) { public void reSitPlayer(final Player player) {
try { try {
final Entity prevarrow = sit.get(player.getName()); SitData sitdata = sit.get(player);
sit.remove(player.getName()); sitdata.sitting = false;
final Entity prevarrow = sit.get(player).arrow;
player.eject(); player.eject();
Entity arrow = plugin.getNMSAccess().spawnArrow(prevarrow.getLocation()); Entity arrow = plugin.getNMSAccess().spawnArrow(prevarrow.getLocation());
arrow.setPassenger(player); arrow.setPassenger(player);
sit.put(player.getName(), arrow); sitdata.arrow = arrow;
sitdata.sitting = true;
Bukkit.getScheduler().scheduleSyncDelayedTask( Bukkit.getScheduler().scheduleSyncDelayedTask(
plugin, plugin,
new Runnable() { new Runnable() {
@ -113,24 +112,24 @@ public class PlayerSitData {
} }
private boolean unsitPlayer(final Player player, boolean canCancel, UnsitParams params) { private boolean unsitPlayer(final Player player, boolean canCancel, UnsitParams params) {
final PlayerChairUnsitEvent playerunsitevent = new PlayerChairUnsitEvent(player, sitstopteleportloc.get(player.getName()), canCancel); SitData sitdata = sit.get(player);
final PlayerChairUnsitEvent playerunsitevent = new PlayerChairUnsitEvent(player, sitdata.teleportloc.clone(), canCancel);
Bukkit.getPluginManager().callEvent(playerunsitevent); Bukkit.getPluginManager().callEvent(playerunsitevent);
if (playerunsitevent.isCancelled() && playerunsitevent.canBeCancelled()) { if (playerunsitevent.isCancelled() && playerunsitevent.canBeCancelled()) {
return false; return false;
} }
final Entity arrow = sit.get(player.getName()); sitdata.sitting = false;
sit.remove(player.getName());
if (params.eject()) { if (params.eject()) {
player.eject(); player.eject();
} }
arrow.remove(); sitdata.arrow.remove();
if (params.restorePostion()) { if (params.restorePostion()) {
Bukkit.getScheduler().scheduleSyncDelayedTask( Bukkit.getScheduler().scheduleSyncDelayedTask(
plugin, plugin,
new Runnable() { new Runnable() {
@Override @Override
public void run() { public void run() {
player.teleport(playerunsitevent.getTeleportLocation()); player.teleport(playerunsitevent.getTeleportLocation().clone());
player.setSneaking(false); player.setSneaking(false);
} }
}, },
@ -139,10 +138,9 @@ public class PlayerSitData {
} else if (params.correctLeavePosition()) { } else if (params.correctLeavePosition()) {
player.teleport(playerunsitevent.getTeleportLocation()); player.teleport(playerunsitevent.getTeleportLocation());
} }
sitblock.values().remove(player.getName()); sitblock.values().remove(player);
sitstopteleportloc.remove(player.getName()); Bukkit.getScheduler().cancelTask(sitdata.resittask);
Bukkit.getScheduler().cancelTask(sittask.get(player.getName())); sit.remove(player);
sittask.remove(player.getName());
if (plugin.notifyplayer) { if (plugin.notifyplayer) {
player.sendMessage(plugin.msgStanding); player.sendMessage(plugin.msgStanding);
} }
@ -175,4 +173,13 @@ public class PlayerSitData {
} }
private class SitData {
private boolean sitting;
private Entity arrow;
private Location teleportloc;
private int resittask;
}
} }