Move event handling to player sit data class. Add locations to events

This commit is contained in:
Shevchik 2014-07-09 09:05:36 +04:00
parent 58d97e1bfe
commit 3b9b3c4fba
6 changed files with 86 additions and 39 deletions

View File

@ -1,5 +1,6 @@
package com.cnaude.chairs.api; package com.cnaude.chairs.api;
import org.bukkit.Location;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable; import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
@ -10,8 +11,19 @@ public class PlayerChairSitEvent extends PlayerEvent implements Cancellable {
private static final HandlerList handlers = new HandlerList(); private static final HandlerList handlers = new HandlerList();
private boolean cancelled = false; private boolean cancelled = false;
public PlayerChairSitEvent(Player who) { private Location sitLocation;
public PlayerChairSitEvent(Player who, Location sitLocation) {
super(who); super(who);
this.sitLocation = sitLocation;
}
public Location getSitLocation() {
return sitLocation.clone();
}
public void setSitLocation(Location location) {
sitLocation = location;
} }
@Override @Override

View File

@ -1,5 +1,6 @@
package com.cnaude.chairs.api; package com.cnaude.chairs.api;
import org.bukkit.Location;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.Cancellable; import org.bukkit.event.Cancellable;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
@ -9,10 +10,13 @@ public class PlayerChairUnsitEvent extends PlayerEvent implements Cancellable {
private static final HandlerList handlers = new HandlerList(); private static final HandlerList handlers = new HandlerList();
private boolean cancelled = false; private boolean cancelled = false;
private boolean canbecancelled = true;
public PlayerChairUnsitEvent(Player who, boolean canbecancelled) { private boolean canbecancelled = true;
private Location unsitLocation;
public PlayerChairUnsitEvent(Player who, Location unsitLocation, boolean canbecancelled) {
super(who); super(who);
this.unsitLocation = unsitLocation;
this.canbecancelled = canbecancelled; this.canbecancelled = canbecancelled;
} }
@ -20,6 +24,14 @@ public class PlayerChairUnsitEvent extends PlayerEvent implements Cancellable {
return canbecancelled; return canbecancelled;
} }
public Location getTeleportLocation() {
return unsitLocation.clone();
}
public void setTeleportLocation(Location location) {
unsitLocation = location;
}
@Override @Override
public HandlerList getHandlers() { public HandlerList getHandlers() {
return handlers; return handlers;

View File

@ -7,7 +7,6 @@ import java.util.List;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger; import java.util.logging.Logger;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
@ -16,7 +15,6 @@ import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import com.cnaude.chairs.api.ChairsAPI; import com.cnaude.chairs.api.ChairsAPI;
import com.cnaude.chairs.api.PlayerChairUnsitEvent;
import com.cnaude.chairs.commands.ChairsCommand; import com.cnaude.chairs.commands.ChairsCommand;
import com.cnaude.chairs.commands.ChairsIgnoreList; import com.cnaude.chairs.commands.ChairsIgnoreList;
import com.cnaude.chairs.listeners.NANLoginListener; import com.cnaude.chairs.listeners.NANLoginListener;
@ -93,8 +91,6 @@ public class Chairs extends JavaPlugin {
if (psitdata != null) { if (psitdata != null) {
for (Player player : getServer().getOnlinePlayers()) { for (Player player : getServer().getOnlinePlayers()) {
if (psitdata.isSitting(player)) { if (psitdata.isSitting(player)) {
PlayerChairUnsitEvent playerunsitevent = new PlayerChairUnsitEvent(player, false);
Bukkit.getPluginManager().callEvent(playerunsitevent);
psitdata.unsitPlayerNow(player); psitdata.unsitPlayerNow(player);
} }
} }

View File

@ -8,6 +8,9 @@ import org.bukkit.block.Block;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import com.cnaude.chairs.api.PlayerChairSitEvent;
import com.cnaude.chairs.api.PlayerChairUnsitEvent;
public class PlayerSitData { public class PlayerSitData {
private Chairs plugin; private Chairs plugin;
@ -32,7 +35,13 @@ public class PlayerSitData {
return Bukkit.getPlayerExact(sitblock.get(chair)); return Bukkit.getPlayerExact(sitblock.get(chair));
} }
public void sitPlayer(Player player, Block blocktooccupy, Location sitlocation) { public boolean sitPlayer(Player player, Block blocktooccupy, Location sitlocation) {
PlayerChairSitEvent playersitevent = new PlayerChairSitEvent(player, sitlocation);
Bukkit.getPluginManager().callEvent(playersitevent);
if (playersitevent.isCancelled()) {
return false;
}
sitlocation = playersitevent.getSitLocation();
try { try {
if (plugin.notifyplayer) { if (plugin.notifyplayer) {
player.sendMessage(plugin.msgSitting); player.sendMessage(plugin.msgSitting);
@ -48,6 +57,7 @@ public class PlayerSitData {
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
return true;
} }
public void startReSitTask(final Player player) { public void startReSitTask(final Player player) {
@ -87,40 +97,47 @@ public class PlayerSitData {
} }
} }
public void unsitPlayerNormal(Player player) { public boolean unsitPlayerNormal(Player player) {
unsitPlayer(player, false, true, false); UnsitParams params = new UnsitParams(false, true, false);
return unsitPlayer(player, true, params);
} }
public void unsitPlayerForce(Player player) { public void unsitPlayerForce(Player player) {
unsitPlayer(player, true, true, false); UnsitParams params = new UnsitParams(true, true, false);
unsitPlayer(player, false, params);
} }
public void unsitPlayerNow(Player player) { public void unsitPlayerNow(Player player) {
unsitPlayer(player, true, false, true); UnsitParams params = new UnsitParams(true, false, true);
unsitPlayer(player, false, params);
} }
private void unsitPlayer(final Player player, boolean eject, boolean restoreposition, boolean correctleaveposition) { private boolean unsitPlayer(final Player player, boolean canCancel, UnsitParams params) {
final PlayerChairUnsitEvent playerunsitevent = new PlayerChairUnsitEvent(player, sitstopteleportloc.get(player.getName()), canCancel);
Bukkit.getPluginManager().callEvent(playerunsitevent);
if (playerunsitevent.isCancelled() && playerunsitevent.canBeCancelled()) {
return false;
}
final Entity arrow = sit.get(player.getName()); final Entity arrow = sit.get(player.getName());
sit.remove(player.getName()); sit.remove(player.getName());
if (eject) { if (params.eject()) {
player.eject(); player.eject();
} }
arrow.remove(); arrow.remove();
final Location tploc = sitstopteleportloc.get(player.getName()); if (params.restorePostion()) {
if (restoreposition) {
Bukkit.getScheduler().scheduleSyncDelayedTask( Bukkit.getScheduler().scheduleSyncDelayedTask(
plugin, plugin,
new Runnable() { new Runnable() {
@Override @Override
public void run() { public void run() {
player.teleport(tploc); player.teleport(playerunsitevent.getTeleportLocation());
player.setSneaking(false); player.setSneaking(false);
} }
}, },
1 1
); );
} else if (correctleaveposition) { } else if (params.correctLeavePosition()) {
player.teleport(tploc); player.teleport(playerunsitevent.getTeleportLocation());
} }
sitblock.values().remove(player.getName()); sitblock.values().remove(player.getName());
sitstopteleportloc.remove(player.getName()); sitstopteleportloc.remove(player.getName());
@ -129,6 +146,33 @@ public class PlayerSitData {
if (plugin.notifyplayer) { if (plugin.notifyplayer) {
player.sendMessage(plugin.msgStanding); player.sendMessage(plugin.msgStanding);
} }
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;
}
} }
} }

View File

@ -1,6 +1,5 @@
package com.cnaude.chairs.listeners; package com.cnaude.chairs.listeners;
import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.block.Block; import org.bukkit.block.Block;
@ -17,7 +16,6 @@ import org.bukkit.material.Stairs;
import org.bukkit.material.Step; import org.bukkit.material.Step;
import org.bukkit.material.WoodenStep; import org.bukkit.material.WoodenStep;
import com.cnaude.chairs.api.PlayerChairSitEvent;
import com.cnaude.chairs.commands.ChairsIgnoreList; import com.cnaude.chairs.commands.ChairsIgnoreList;
import com.cnaude.chairs.core.ChairBlock; import com.cnaude.chairs.core.ChairBlock;
import com.cnaude.chairs.core.Chairs; import com.cnaude.chairs.core.Chairs;
@ -39,12 +37,9 @@ public class TrySitEventListener implements Listener {
Player player = event.getPlayer(); Player player = event.getPlayer();
Block block = event.getClickedBlock(); Block block = event.getClickedBlock();
if (sitAllowed(player, block)) { if (sitAllowed(player, block)) {
PlayerChairSitEvent playersitevent = new PlayerChairSitEvent(player); Location sitLocation = getSitLocation(block, player.getLocation().getYaw());
Bukkit.getPluginManager().callEvent(playersitevent); if (plugin.getPlayerSitData().sitPlayer(player, block, sitLocation)) {
if (!playersitevent.isCancelled()) {
event.setCancelled(true); event.setCancelled(true);
Location sitLocation = getSitLocation(block, player.getLocation().getYaw());
plugin.getPlayerSitData().sitPlayer(player, block, sitLocation);
} }
} }
} }

View File

@ -1,6 +1,5 @@
package com.cnaude.chairs.listeners; package com.cnaude.chairs.listeners;
import org.bukkit.Bukkit;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
@ -12,7 +11,6 @@ import org.bukkit.event.player.PlayerQuitEvent;
import org.bukkit.event.player.PlayerTeleportEvent; import org.bukkit.event.player.PlayerTeleportEvent;
import org.bukkit.event.vehicle.VehicleExitEvent; import org.bukkit.event.vehicle.VehicleExitEvent;
import com.cnaude.chairs.api.PlayerChairUnsitEvent;
import com.cnaude.chairs.core.Chairs; import com.cnaude.chairs.core.Chairs;
public class TryUnsitEventListener implements Listener { public class TryUnsitEventListener implements Listener {
@ -36,8 +34,6 @@ 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)) {
PlayerChairUnsitEvent playerunsitevent = new PlayerChairUnsitEvent(player, false);
Bukkit.getPluginManager().callEvent(playerunsitevent);
plugin.getPlayerSitData().unsitPlayerNow(player); plugin.getPlayerSitData().unsitPlayerNow(player);
} }
} }
@ -46,8 +42,6 @@ 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)) {
PlayerChairUnsitEvent playerunsitevent = new PlayerChairUnsitEvent(player, false);
Bukkit.getPluginManager().callEvent(playerunsitevent);
plugin.getPlayerSitData().unsitPlayerNow(player); plugin.getPlayerSitData().unsitPlayerNow(player);
} }
} }
@ -57,11 +51,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)) {
PlayerChairUnsitEvent playerunsitevent = new PlayerChairUnsitEvent(player, true); if (!plugin.getPlayerSitData().unsitPlayerNormal(player)) {
Bukkit.getPluginManager().callEvent(playerunsitevent);
if (!playerunsitevent.isCancelled()) {
plugin.getPlayerSitData().unsitPlayerNormal(player);
} else {
e.setCancelled(true); e.setCancelled(true);
} }
} }
@ -73,8 +63,6 @@ public class TryUnsitEventListener implements Listener {
Block b = event.getBlock(); Block b = event.getBlock();
if (plugin.getPlayerSitData().isBlockOccupied(b)) { if (plugin.getPlayerSitData().isBlockOccupied(b)) {
Player player = plugin.getPlayerSitData().getPlayerOnChair(b); Player player = plugin.getPlayerSitData().getPlayerOnChair(b);
PlayerChairUnsitEvent playerunsitevent = new PlayerChairUnsitEvent(player, false);
Bukkit.getPluginManager().callEvent(playerunsitevent);
plugin.getPlayerSitData().unsitPlayerForce(player); plugin.getPlayerSitData().unsitPlayerForce(player);
} }
} }