Refactor this all

This commit is contained in:
Shevchik 2013-09-03 01:57:50 +04:00
parent e5ce4c0f8c
commit cfe5ca1674
2 changed files with 112 additions and 85 deletions

View File

@ -32,28 +32,6 @@ public class EventListener implements Listener {
this.plugin = plugin; this.plugin = plugin;
this.ignoreList = ignoreList; this.ignoreList = ignoreList;
} }
public boolean isValidChair(Block block) {
for (ChairBlock cb : plugin.allowedBlocks) {
if (cb.getMat().equals(block.getType())) {
return true;
}
}
return false;
}
private boolean isSitting(Player player) {
if (plugin.sit.containsKey(player.getName())) {
if (player.isInsideVehicle()) {
return true;
} else {
plugin.unSit(player);
}
}
return false;
}
@EventHandler(priority=EventPriority.LOWEST,ignoreCancelled=true) @EventHandler(priority=EventPriority.LOWEST,ignoreCancelled=true)
public void onJoin(PlayerJoinEvent e) public void onJoin(PlayerJoinEvent e)
@ -96,6 +74,7 @@ public class EventListener implements Listener {
plugin.ejectPlayer(player); plugin.ejectPlayer(player);
} }
} }
@EventHandler @EventHandler
public void onPlayerInteract(PlayerInteractEvent event) { public void onPlayerInteract(PlayerInteractEvent event) {
@ -108,30 +87,32 @@ public class EventListener implements Listener {
&& plugin.ignoreIfBlockInHand) { && plugin.ignoreIfBlockInHand) {
return; return;
} }
if (event.hasBlock() && event.getAction() == Action.RIGHT_CLICK_BLOCK) { if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
sitPlayer(player, event.getClickedBlock()); Block block = event.getClickedBlock();
if (sit) { if (sitAllowed(player, block))
event.setCancelled(true); {
sit = false; Location sitLocation = getSitLocation(block, player.getLocation().getYaw());
sitPlayer(player, block, sitLocation);
event.setCancelled(true);
} }
} }
} }
private boolean sit = false;
protected void sitPlayer(Player player, Block block)
private boolean sitAllowed(Player player, Block block)
{ {
Stairs stairs = null; Stairs stairs = null;
Step step = null; Step step = null;
WoodenStep wStep = null; WoodenStep wStep = null;
double sh = plugin.sittingHeight;
boolean blockOkay = false; boolean blockOkay = false;
if (ignoreList.isIgnored(player.getName())) { if (ignoreList.isIgnored(player.getName())) {
return; return false;
} }
// Permissions Check // Permissions Check
if (plugin.permissions) { if (plugin.permissions) {
if (!player.hasPermission("chairs.sit")) { if (!player.hasPermission("chairs.sit")) {
return; return false;
} }
} }
if (plugin.perItemPerms) { if (plugin.perItemPerms) {
@ -161,13 +142,11 @@ public class EventListener implements Listener {
if (cb.getMat().toString().contains("STAIRS")) { if (cb.getMat().toString().contains("STAIRS")) {
if (cb.getMat().equals(block.getType())) { if (cb.getMat().equals(block.getType())) {
blockOkay = true; blockOkay = true;
sh = cb.getSitHeight();
continue; continue;
} }
} else if (cb.getMat().equals(block.getType()) } else if (cb.getMat().equals(block.getType())
&& cb.getDamage() == block.getData()) { && cb.getDamage() == block.getData()) {
blockOkay = true; blockOkay = true;
sh = cb.getSitHeight();
continue; continue;
} }
} }
@ -183,41 +162,39 @@ public class EventListener implements Listener {
step = (Step) block.getState().getData(); step = (Step) block.getState().getData();
} else if (block.getState().getData() instanceof WoodenStep) { } else if (block.getState().getData() instanceof WoodenStep) {
wStep = (WoodenStep) block.getState().getData(); wStep = (WoodenStep) block.getState().getData();
} else {
sh += plugin.sittingHeightAdj;
} }
int chairwidth = 1; int chairwidth = 1;
// Check if block beneath chair is solid. // Check if block beneath chair is solid.
if (block.getRelative(BlockFace.DOWN).isLiquid()) { if (block.getRelative(BlockFace.DOWN).isLiquid()) {
return; return false;
} }
if (block.getRelative(BlockFace.DOWN).isEmpty()) { if (block.getRelative(BlockFace.DOWN).isEmpty()) {
return; return false;
} }
if (!block.getRelative(BlockFace.DOWN).getType().isSolid()) { if (!block.getRelative(BlockFace.DOWN).getType().isSolid()) {
return; return false;
} }
// Check for distance distance between player and chair. // Check for distance distance between player and chair.
if (plugin.distance > 0 && player.getLocation().distance(block.getLocation().add(0.5, 0, 0.5)) > plugin.distance) { if (plugin.distance > 0 && player.getLocation().distance(block.getLocation().add(0.5, 0, 0.5)) > plugin.distance) {
return; return false;
} }
if (stairs != null) { if (stairs != null) {
if (stairs.isInverted() && plugin.invertedStairCheck) { if (stairs.isInverted() && plugin.invertedStairCheck) {
return; return false;
} }
} }
if (step != null) { if (step != null) {
if (step.isInverted() && plugin.invertedStepCheck) { if (step.isInverted() && plugin.invertedStepCheck) {
return; return false;
} }
} }
if (wStep != null) { if (wStep != null) {
if (wStep.isInverted() && plugin.invertedStepCheck) { if (wStep.isInverted() && plugin.invertedStepCheck) {
return; return false;
} }
} }
@ -235,7 +212,7 @@ public class EventListener implements Listener {
} }
if (!(sign1 == true && sign2 == true)) { if (!(sign1 == true && sign2 == true)) {
return; return false;
} }
} }
@ -250,7 +227,7 @@ public class EventListener implements Listener {
} }
if (chairwidth > plugin.maxChairWidth) { if (chairwidth > plugin.maxChairWidth) {
return; return false;
} }
} }
@ -262,52 +239,80 @@ public class EventListener implements Listener {
if (!plugin.msgOccupied.isEmpty()) { if (!plugin.msgOccupied.isEmpty()) {
player.sendMessage(plugin.msgOccupied.replaceAll("%PLAYER%", plugin.sitblock.get(block))); player.sendMessage(plugin.msgOccupied.replaceAll("%PLAYER%", plugin.sitblock.get(block)));
} }
return; return false;
} }
} }
} }
Location plocation = block.getLocation().clone();
plocation.add(0.5D, (sh - 0.5D), 0.5D);
// Rotate the player's view to the descending side of the block.
if (plugin.autoRotate && stairs != null) {
switch (stairs.getDescendingDirection()) {
case NORTH:
plocation.setYaw(180);
break;
case EAST:
plocation.setYaw(-90);
break;
case SOUTH:
plocation.setYaw(0);
break;
case WEST:
plocation.setYaw(90);
default:
;
}
} else {
plocation.setYaw(player.getLocation().getYaw());
}
if (plugin.notifyplayer && !plugin.msgSitting.isEmpty()) {
player.sendMessage(plugin.msgSitting);
}
plugin.sitstopteleportloc.put(player.getName(), player.getLocation());
player.teleport(plocation);
Entity arrow = block.getWorld().spawnArrow(block.getLocation().add(0.5, 0 , 0.5), new Vector(0, 0, 0), 0, 0);
arrow.setPassenger(player);
plugin.sit.put(player.getName(), arrow);
plugin.sitblock.put(block, player.getName());
plugin.sitblockbr.put(player.getName(), block);
startReSitTask(player);
sit = true;
} }
return true;
} }
protected void startReSitTask(final Player player) private Location getSitLocation(Block block, Float playerYaw)
{
double sh = plugin.sittingHeight;
for (ChairBlock cb : plugin.allowedBlocks) {
if (cb.getMat().toString().contains("STAIRS")) {
if (cb.getMat().equals(block.getType())) {
sh = cb.getSitHeight();
continue;
}
} else if (cb.getMat().equals(block.getType())
&& cb.getDamage() == block.getData()) {
sh = cb.getSitHeight();
continue;
}
}
Stairs stairs = null;
if (block.getState().getData() instanceof Stairs) {
stairs = (Stairs) block.getState().getData();
} else {
sh += plugin.sittingHeightAdj;
}
Location plocation = block.getLocation().clone();
plocation.add(0.5D, (sh - 0.5D), 0.5D);
// Rotate the player's view to the descending side of the block.
if (plugin.autoRotate && stairs != null) {
switch (stairs.getDescendingDirection()) {
case NORTH:
plocation.setYaw(180);
break;
case EAST:
plocation.setYaw(-90);
break;
case SOUTH:
plocation.setYaw(0);
break;
case WEST:
plocation.setYaw(90);
default:
;
}
} else {
plocation.setYaw(playerYaw);
}
return plocation;
}
private void sitPlayer(Player player, Block block, Location sitlocation)
{
if (plugin.notifyplayer && !plugin.msgSitting.isEmpty()) {
player.sendMessage(plugin.msgSitting);
}
plugin.sitstopteleportloc.put(player.getName(), player.getLocation());
player.teleport(sitlocation);
Entity arrow = block.getWorld().spawnArrow(block.getLocation().add(0.5, 0 , 0.5), new Vector(0, 0, 0), 0, 0);
arrow.setPassenger(player);
plugin.sit.put(player.getName(), arrow);
plugin.sitblock.put(block, player.getName());
plugin.sitblockbr.put(player.getName(), block);
startReSitTask(player);
}
private void startReSitTask(final Player player)
{ {
int task = int task =
Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable() Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable()
@ -319,6 +324,28 @@ public class EventListener implements Listener {
},1000,1000); },1000,1000);
plugin.sittask.put(player.getName(), task); plugin.sittask.put(player.getName(), task);
} }
public boolean isValidChair(Block block) {
for (ChairBlock cb : plugin.allowedBlocks) {
if (cb.getMat().equals(block.getType())) {
return true;
}
}
return false;
}
private boolean isSitting(Player player) {
if (plugin.sit.containsKey(player.getName())) {
if (player.isInsideVehicle()) {
return true;
} else {
plugin.unSit(player);
}
}
return false;
}
private int getChairWidth(Block block, BlockFace face) { private int getChairWidth(Block block, BlockFace face) {
int width = 0; int width = 0;

Binary file not shown.