Force unsit player on teleport. Workaround spigot bug. Other

improvements.
This commit is contained in:
Shevchik 2015-01-15 12:51:13 +04:00
parent 94825e44a8
commit 7cc0d12ed6
3 changed files with 371 additions and 421 deletions

View File

@ -90,7 +90,7 @@ public class Chairs extends JavaPlugin {
if (psitdata != null) { if (psitdata != null) {
for (Player player : Utils.getOnlinePlayers()) { for (Player player : Utils.getOnlinePlayers()) {
if (psitdata.isSitting(player)) { if (psitdata.isSitting(player)) {
psitdata.unsitPlayerNow(player); psitdata.unsitPlayerForce(player);
} }
} }
} }

View File

@ -71,7 +71,6 @@ public class PlayerSitData {
SitData sitdata = sit.get(player); SitData sitdata = sit.get(player);
sitdata.sitting = false; sitdata.sitting = false;
final Entity prevarrow = sit.get(player).arrow; final Entity prevarrow = sit.get(player).arrow;
player.eject();
Entity arrow = plugin.getNMSAccess().spawnArrow(prevarrow.getLocation()); Entity arrow = plugin.getNMSAccess().spawnArrow(prevarrow.getLocation());
arrow.setPassenger(player); arrow.setPassenger(player);
sitdata.arrow = arrow; sitdata.arrow = arrow;
@ -88,22 +87,15 @@ public class PlayerSitData {
); );
} }
public boolean unsitPlayerNormal(Player player) { public boolean unsitPlayer(Player player) {
UnsitParams params = new UnsitParams(false, true, false); return unsitPlayer(player, true);
return unsitPlayer(player, true, params);
} }
public void unsitPlayerForce(Player player) { public void unsitPlayerForce(Player player) {
UnsitParams params = new UnsitParams(true, true, false); unsitPlayer(player, false);
unsitPlayer(player, false, params);
} }
public void unsitPlayerNow(Player player) { private boolean unsitPlayer(final Player player, boolean canCancel) {
UnsitParams params = new UnsitParams(true, false, true);
unsitPlayer(player, false, params);
}
private boolean unsitPlayer(final Player player, boolean canCancel, UnsitParams params) {
SitData sitdata = sit.get(player); SitData sitdata = sit.get(player);
final PlayerChairUnsitEvent playerunsitevent = new PlayerChairUnsitEvent(player, sitdata.teleportloc.clone(), canCancel); final PlayerChairUnsitEvent playerunsitevent = new PlayerChairUnsitEvent(player, sitdata.teleportloc.clone(), canCancel);
Bukkit.getPluginManager().callEvent(playerunsitevent); Bukkit.getPluginManager().callEvent(playerunsitevent);
@ -111,25 +103,10 @@ public class PlayerSitData {
return false; return false;
} }
sitdata.sitting = false; sitdata.sitting = false;
if (params.eject()) { player.leaveVehicle();
player.eject();
}
sitdata.arrow.remove(); sitdata.arrow.remove();
if (params.restorePostion()) { player.teleport(playerunsitevent.getTeleportLocation().clone());
Bukkit.getScheduler().scheduleSyncDelayedTask( player.setSneaking(false);
plugin,
new Runnable() {
@Override
public void run() {
player.teleport(playerunsitevent.getTeleportLocation().clone());
player.setSneaking(false);
}
},
1
);
} else if (params.correctLeavePosition()) {
player.teleport(playerunsitevent.getTeleportLocation());
}
sitblock.values().remove(player); sitblock.values().remove(player);
Bukkit.getScheduler().cancelTask(sitdata.resittask); Bukkit.getScheduler().cancelTask(sitdata.resittask);
sit.remove(player); sit.remove(player);
@ -139,32 +116,6 @@ public class PlayerSitData {
return true; return true;
} }
private class UnsitParams {
private boolean eject;
private boolean restoreposition;
private boolean correctleaveposition;
public UnsitParams(boolean eject, boolean restoreposition, boolean correctleaveposition) {
this.eject = eject;
this.restoreposition = restoreposition;
this.correctleaveposition = correctleaveposition;
}
public boolean eject() {
return eject;
}
public boolean restorePostion() {
return restoreposition;
}
public boolean correctLeavePosition() {
return correctleaveposition;
}
}
private class SitData { private class SitData {
private boolean sitting; private boolean sitting;

View File

@ -21,12 +21,11 @@ public class TryUnsitEventListener implements Listener {
this.plugin = plugin; this.plugin = plugin;
} }
//spigot...
@EventHandler(priority=EventPriority.LOWEST) @EventHandler(priority=EventPriority.LOWEST)
public void onPlayerTeleport(PlayerTeleportEvent event) { public void onPlayerTeleport(PlayerTeleportEvent event) {
final Player player = event.getPlayer(); final Player player = event.getPlayer();
if (plugin.getPlayerSitData().isSitting(player)) { if (plugin.getPlayerSitData().isSitting(player)) {
event.setCancelled(true); plugin.getPlayerSitData().unsitPlayerForce(player);
} }
} }
@ -34,7 +33,7 @@ public class TryUnsitEventListener implements Listener {
public void onPlayerQuit(PlayerQuitEvent event) { public void onPlayerQuit(PlayerQuitEvent event) {
Player player = event.getPlayer(); Player player = event.getPlayer();
if (plugin.getPlayerSitData().isSitting(player)) { if (plugin.getPlayerSitData().isSitting(player)) {
plugin.getPlayerSitData().unsitPlayerNow(player); plugin.getPlayerSitData().unsitPlayerForce(player);
} }
} }
@ -42,7 +41,7 @@ public class TryUnsitEventListener implements Listener {
public void onPlayerDeath(PlayerDeathEvent event) { public void onPlayerDeath(PlayerDeathEvent event) {
Player player = event.getEntity(); Player player = event.getEntity();
if (plugin.getPlayerSitData().isSitting(player)) { if (plugin.getPlayerSitData().isSitting(player)) {
plugin.getPlayerSitData().unsitPlayerNow(player); plugin.getPlayerSitData().unsitPlayerForce(player);
} }
} }
@ -51,7 +50,7 @@ public class TryUnsitEventListener implements Listener {
if (e.getVehicle().getPassenger() instanceof Player) { if (e.getVehicle().getPassenger() instanceof Player) {
final Player player = (Player) e.getVehicle().getPassenger(); final Player player = (Player) e.getVehicle().getPassenger();
if (plugin.getPlayerSitData().isSitting(player)) { if (plugin.getPlayerSitData().isSitting(player)) {
if (!plugin.getPlayerSitData().unsitPlayerNormal(player)) { if (!plugin.getPlayerSitData().unsitPlayer(player)) {
e.setCancelled(true); e.setCancelled(true);
} }
} }