Move event handling to player sit data class. Add locations to events
This commit is contained in:
parent
58d97e1bfe
commit
3b9b3c4fba
@ -1,5 +1,6 @@
|
||||
package com.cnaude.chairs.api;
|
||||
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.Cancellable;
|
||||
import org.bukkit.event.HandlerList;
|
||||
@ -10,8 +11,19 @@ public class PlayerChairSitEvent extends PlayerEvent implements Cancellable {
|
||||
private static final HandlerList handlers = new HandlerList();
|
||||
private boolean cancelled = false;
|
||||
|
||||
public PlayerChairSitEvent(Player who) {
|
||||
private Location sitLocation;
|
||||
|
||||
public PlayerChairSitEvent(Player who, Location sitLocation) {
|
||||
super(who);
|
||||
this.sitLocation = sitLocation;
|
||||
}
|
||||
|
||||
public Location getSitLocation() {
|
||||
return sitLocation.clone();
|
||||
}
|
||||
|
||||
public void setSitLocation(Location location) {
|
||||
sitLocation = location;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -1,5 +1,6 @@
|
||||
package com.cnaude.chairs.api;
|
||||
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.Cancellable;
|
||||
import org.bukkit.event.HandlerList;
|
||||
@ -9,10 +10,13 @@ public class PlayerChairUnsitEvent extends PlayerEvent implements Cancellable {
|
||||
|
||||
private static final HandlerList handlers = new HandlerList();
|
||||
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);
|
||||
this.unsitLocation = unsitLocation;
|
||||
this.canbecancelled = canbecancelled;
|
||||
}
|
||||
|
||||
@ -20,6 +24,14 @@ public class PlayerChairUnsitEvent extends PlayerEvent implements Cancellable {
|
||||
return canbecancelled;
|
||||
}
|
||||
|
||||
public Location getTeleportLocation() {
|
||||
return unsitLocation.clone();
|
||||
}
|
||||
|
||||
public void setTeleportLocation(Location location) {
|
||||
unsitLocation = location;
|
||||
}
|
||||
|
||||
@Override
|
||||
public HandlerList getHandlers() {
|
||||
return handlers;
|
||||
|
@ -7,7 +7,6 @@ import java.util.List;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
@ -16,7 +15,6 @@ import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import com.cnaude.chairs.api.ChairsAPI;
|
||||
import com.cnaude.chairs.api.PlayerChairUnsitEvent;
|
||||
import com.cnaude.chairs.commands.ChairsCommand;
|
||||
import com.cnaude.chairs.commands.ChairsIgnoreList;
|
||||
import com.cnaude.chairs.listeners.NANLoginListener;
|
||||
@ -93,8 +91,6 @@ public class Chairs extends JavaPlugin {
|
||||
if (psitdata != null) {
|
||||
for (Player player : getServer().getOnlinePlayers()) {
|
||||
if (psitdata.isSitting(player)) {
|
||||
PlayerChairUnsitEvent playerunsitevent = new PlayerChairUnsitEvent(player, false);
|
||||
Bukkit.getPluginManager().callEvent(playerunsitevent);
|
||||
psitdata.unsitPlayerNow(player);
|
||||
}
|
||||
}
|
||||
|
@ -8,6 +8,9 @@ import org.bukkit.block.Block;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import com.cnaude.chairs.api.PlayerChairSitEvent;
|
||||
import com.cnaude.chairs.api.PlayerChairUnsitEvent;
|
||||
|
||||
public class PlayerSitData {
|
||||
|
||||
private Chairs plugin;
|
||||
@ -32,7 +35,13 @@ public class PlayerSitData {
|
||||
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 {
|
||||
if (plugin.notifyplayer) {
|
||||
player.sendMessage(plugin.msgSitting);
|
||||
@ -48,6 +57,7 @@ public class PlayerSitData {
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public void startReSitTask(final Player player) {
|
||||
@ -87,40 +97,47 @@ public class PlayerSitData {
|
||||
}
|
||||
}
|
||||
|
||||
public void unsitPlayerNormal(Player player) {
|
||||
unsitPlayer(player, false, true, false);
|
||||
public boolean unsitPlayerNormal(Player player) {
|
||||
UnsitParams params = new UnsitParams(false, true, false);
|
||||
return unsitPlayer(player, true, params);
|
||||
}
|
||||
|
||||
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) {
|
||||
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());
|
||||
sit.remove(player.getName());
|
||||
if (eject) {
|
||||
if (params.eject()) {
|
||||
player.eject();
|
||||
}
|
||||
arrow.remove();
|
||||
final Location tploc = sitstopteleportloc.get(player.getName());
|
||||
if (restoreposition) {
|
||||
if (params.restorePostion()) {
|
||||
Bukkit.getScheduler().scheduleSyncDelayedTask(
|
||||
plugin,
|
||||
new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
player.teleport(tploc);
|
||||
player.teleport(playerunsitevent.getTeleportLocation());
|
||||
player.setSneaking(false);
|
||||
}
|
||||
},
|
||||
1
|
||||
);
|
||||
} else if (correctleaveposition) {
|
||||
player.teleport(tploc);
|
||||
} else if (params.correctLeavePosition()) {
|
||||
player.teleport(playerunsitevent.getTeleportLocation());
|
||||
}
|
||||
sitblock.values().remove(player.getName());
|
||||
sitstopteleportloc.remove(player.getName());
|
||||
@ -129,6 +146,33 @@ public class PlayerSitData {
|
||||
if (plugin.notifyplayer) {
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,6 +1,5 @@
|
||||
package com.cnaude.chairs.listeners;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.block.Block;
|
||||
@ -17,7 +16,6 @@ import org.bukkit.material.Stairs;
|
||||
import org.bukkit.material.Step;
|
||||
import org.bukkit.material.WoodenStep;
|
||||
|
||||
import com.cnaude.chairs.api.PlayerChairSitEvent;
|
||||
import com.cnaude.chairs.commands.ChairsIgnoreList;
|
||||
import com.cnaude.chairs.core.ChairBlock;
|
||||
import com.cnaude.chairs.core.Chairs;
|
||||
@ -39,12 +37,9 @@ public class TrySitEventListener implements Listener {
|
||||
Player player = event.getPlayer();
|
||||
Block block = event.getClickedBlock();
|
||||
if (sitAllowed(player, block)) {
|
||||
PlayerChairSitEvent playersitevent = new PlayerChairSitEvent(player);
|
||||
Bukkit.getPluginManager().callEvent(playersitevent);
|
||||
if (!playersitevent.isCancelled()) {
|
||||
Location sitLocation = getSitLocation(block, player.getLocation().getYaw());
|
||||
if (plugin.getPlayerSitData().sitPlayer(player, block, sitLocation)) {
|
||||
event.setCancelled(true);
|
||||
Location sitLocation = getSitLocation(block, player.getLocation().getYaw());
|
||||
plugin.getPlayerSitData().sitPlayer(player, block, sitLocation);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,5 @@
|
||||
package com.cnaude.chairs.listeners;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.block.Block;
|
||||
import org.bukkit.entity.Player;
|
||||
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.vehicle.VehicleExitEvent;
|
||||
|
||||
import com.cnaude.chairs.api.PlayerChairUnsitEvent;
|
||||
import com.cnaude.chairs.core.Chairs;
|
||||
|
||||
public class TryUnsitEventListener implements Listener {
|
||||
@ -36,8 +34,6 @@ public class TryUnsitEventListener implements Listener {
|
||||
public void onPlayerQuit(PlayerQuitEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
if (plugin.getPlayerSitData().isSitting(player)) {
|
||||
PlayerChairUnsitEvent playerunsitevent = new PlayerChairUnsitEvent(player, false);
|
||||
Bukkit.getPluginManager().callEvent(playerunsitevent);
|
||||
plugin.getPlayerSitData().unsitPlayerNow(player);
|
||||
}
|
||||
}
|
||||
@ -46,8 +42,6 @@ public class TryUnsitEventListener implements Listener {
|
||||
public void onPlayerDeath(PlayerDeathEvent event) {
|
||||
Player player = event.getEntity();
|
||||
if (plugin.getPlayerSitData().isSitting(player)) {
|
||||
PlayerChairUnsitEvent playerunsitevent = new PlayerChairUnsitEvent(player, false);
|
||||
Bukkit.getPluginManager().callEvent(playerunsitevent);
|
||||
plugin.getPlayerSitData().unsitPlayerNow(player);
|
||||
}
|
||||
}
|
||||
@ -57,11 +51,7 @@ public class TryUnsitEventListener implements Listener {
|
||||
if (e.getVehicle().getPassenger() instanceof Player) {
|
||||
final Player player = (Player) e.getVehicle().getPassenger();
|
||||
if (plugin.getPlayerSitData().isSitting(player)) {
|
||||
PlayerChairUnsitEvent playerunsitevent = new PlayerChairUnsitEvent(player, true);
|
||||
Bukkit.getPluginManager().callEvent(playerunsitevent);
|
||||
if (!playerunsitevent.isCancelled()) {
|
||||
plugin.getPlayerSitData().unsitPlayerNormal(player);
|
||||
} else {
|
||||
if (!plugin.getPlayerSitData().unsitPlayerNormal(player)) {
|
||||
e.setCancelled(true);
|
||||
}
|
||||
}
|
||||
@ -73,8 +63,6 @@ public class TryUnsitEventListener implements Listener {
|
||||
Block b = event.getBlock();
|
||||
if (plugin.getPlayerSitData().isBlockOccupied(b)) {
|
||||
Player player = plugin.getPlayerSitData().getPlayerOnChair(b);
|
||||
PlayerChairUnsitEvent playerunsitevent = new PlayerChairUnsitEvent(player, false);
|
||||
Bukkit.getPluginManager().callEvent(playerunsitevent);
|
||||
plugin.getPlayerSitData().unsitPlayerForce(player);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user