Move player sit operations to its own class

This commit is contained in:
Shevchik 2013-12-16 23:02:31 +04:00
parent a5086a4af5
commit 83e3f998c6
6 changed files with 187 additions and 151 deletions

View File

@ -36,8 +36,7 @@ public class ChairEffects {
@Override @Override
public void run() { public void run() {
for (Player p : Bukkit.getOnlinePlayers()) { for (Player p : Bukkit.getOnlinePlayers()) {
String pName = p.getName(); if (plugin.getPlayerSitData().isSitting(p)) {
if (plugin.sit.containsKey(pName)) {
if (p.hasPermission("chairs.sit.health")) { if (p.hasPermission("chairs.sit.health")) {
double pHealthPcnt = ((double) p.getHealth()) / (double) p.getMaxHealth() * 100d; double pHealthPcnt = ((double) p.getHealth()) / (double) p.getMaxHealth() * 100d;
if ((pHealthPcnt < plugin.sitMaxHealth) && (p.getHealth() < p.getMaxHealth())) { if ((pHealthPcnt < plugin.sitMaxHealth) && (p.getHealth() < p.getMaxHealth())) {

View File

@ -1,27 +1,20 @@
package com.cnaude.chairs; package com.cnaude.chairs;
import java.io.File; import java.io.File;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; 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.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Arrow; import org.bukkit.entity.Arrow;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
@ -45,7 +38,14 @@ public class Chairs extends JavaPlugin {
public ChairsIgnoreList ignoreList; public ChairsIgnoreList ignoreList;
public String msgSitting, msgStanding, msgOccupied, msgNoPerm, msgReloaded, msgDisabled, msgEnabled, msgCommandRestricted; public String msgSitting, msgStanding, msgOccupied, msgNoPerm, msgReloaded, msgDisabled, msgEnabled, msgCommandRestricted;
private Class<?> vehiclearrowclass;
private PlayerSitData psitdata;
protected PlayerSitData getPlayerSitData()
{
return psitdata;
}
protected Class<?> vehiclearrowclass;
@Override @Override
public void onEnable() { public void onEnable() {
@ -71,6 +71,7 @@ public class Chairs extends JavaPlugin {
getConfig().options().copyDefaults(true); getConfig().options().copyDefaults(true);
saveConfig(); saveConfig();
loadConfig(); loadConfig();
psitdata = new PlayerSitData(this);
getServer().getPluginManager().registerEvents(new TrySitEventListener(this, ignoreList), this); getServer().getPluginManager().registerEvents(new TrySitEventListener(this, ignoreList), this);
getServer().getPluginManager().registerEvents(new TryUnsitEventListener(this), this); getServer().getPluginManager().registerEvents(new TryUnsitEventListener(this), this);
getServer().getPluginManager().registerEvents(new CommandRestrict(this), this); getServer().getPluginManager().registerEvents(new CommandRestrict(this), this);
@ -80,8 +81,8 @@ public class Chairs extends JavaPlugin {
@Override @Override
public void onDisable() { public void onDisable() {
for (Player player : getServer().getOnlinePlayers()) { for (Player player : getServer().getOnlinePlayers()) {
if (sit.containsKey(player.getName())) { if (psitdata.isSitting(player)) {
unSitPlayer(player, false, true); psitdata.unSitPlayer(player, false, true);
} }
} }
if (ignoreList != null) { if (ignoreList != null) {
@ -100,129 +101,6 @@ public class Chairs extends JavaPlugin {
} }
} }
protected HashMap<String, Entity> sit = new HashMap<String, Entity>();
protected HashMap<Block, String> sitblock = new HashMap<Block, String>();
protected HashMap<String, Block> sitblockbr = new HashMap<String, Block>();
protected HashMap<String, Location> sitstopteleportloc = new HashMap<String, Location>();
protected HashMap<String, Integer> sittask = new HashMap<String, Integer>();
protected void sitPlayer(Player player, Location sitlocation)
{
try {
if (notifyplayer && !msgSitting.isEmpty())
{
player.sendMessage(msgSitting);
}
Block block = sitlocation.getBlock();
sitstopteleportloc.put(player.getName(), player.getLocation());
player.teleport(sitlocation);
Location arrowloc = block.getLocation().add(0.5, 0 , 0.5);
Entity arrow = sitPlayerOnArrow(player, arrowloc);
sit.put(player.getName(), arrow);
sitblock.put(block, player.getName());
sitblockbr.put(player.getName(), block);
startReSitTask(player);
} catch (Exception e) {
e.printStackTrace();
}
}
protected void startReSitTask(final Player player)
{
int task =
Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable()
{
public void run()
{
reSitPlayer(player);
}
},1000,1000);
sittask.put(player.getName(), task);
}
protected void reSitPlayer(final Player player)
{
try {
final Entity prevarrow = sit.get(player.getName());
sit.remove(player.getName());
player.eject();
Block block = sitblockbr.get(player.getName());
Location arrowloc = block.getLocation().add(0.5, 0 , 0.5);
Entity arrow = sitPlayerOnArrow(player, arrowloc);
sit.put(player.getName(), arrow);
Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable()
{
public void run()
{
prevarrow.remove();
}
},100);
} catch (Exception e) {
e.printStackTrace();
}
}
private Entity sitPlayerOnArrow(Player player, Location arrowloc) throws NoSuchMethodException, SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException, InstantiationException, InvocationTargetException
{
Entity arrow = player.getWorld().spawnArrow(arrowloc, new Vector(0, 0.1 ,0), 0, 0);
Method getHandleMethod = arrow.getClass().getDeclaredMethod("getHandle");
getHandleMethod.setAccessible(true);
Object nmsarrow = getHandleMethod.invoke(arrow);
Field bukkitEntityField = nmsarrow.getClass().getSuperclass().getDeclaredField("bukkitEntity");
bukkitEntityField.setAccessible(true);
Constructor<?> ctor = vehiclearrowclass.getDeclaredConstructor(this.getServer().getClass(), nmsarrow.getClass());
ctor.setAccessible(true);
Object vehiclearrow = ctor.newInstance(this.getServer(), nmsarrow);
bukkitEntityField.set(nmsarrow, vehiclearrow);
arrow.setPassenger(player);
return arrow;
}
protected void unSitPlayer(final Player player, boolean restoreposition, boolean correctnmspostion)
{
final Entity arrow = sit.get(player.getName());
sit.remove(player.getName());
player.eject();
arrow.remove();
final Location tploc = sitstopteleportloc.get(player.getName());
if (restoreposition)
{
Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable()
{
public void run()
{
player.teleport(tploc);
player.setSneaking(false);
}
},1);
} else
{
if (correctnmspostion)
{
try {
Method getHandleMethod = player.getClass().getDeclaredMethod("getHandle");
getHandleMethod.setAccessible(true);
Object nmsPlayer = getHandleMethod.invoke(player);
Class<?> entityClass = nmsPlayer.getClass().getSuperclass().getSuperclass().getSuperclass();
Field locXField = entityClass.getDeclaredField("locX");
locXField.setAccessible(true);
locXField.set(nmsPlayer, tploc.getX());
Field locYField = entityClass.getDeclaredField("locY");
locYField.setAccessible(true);
locYField.set(nmsPlayer, tploc.getY());
Field locZField = entityClass.getDeclaredField("locZ");
locZField.setAccessible(true);
locZField.set(nmsPlayer, tploc.getZ());
} catch (Exception e) {
e.printStackTrace();
}
}
}
sitblock.remove(sitblockbr.get(player.getName()));
sitblockbr.remove(player.getName());
sitstopteleportloc.remove(player.getName());
Bukkit.getScheduler().cancelTask(sittask.get(player.getName()));
sittask.remove(player.getName());
if (notifyplayer && !msgStanding.isEmpty())
{
player.sendMessage(msgStanding);
}
}
public void loadConfig() { public void loadConfig() {
FileConfiguration config = YamlConfiguration.loadConfiguration(new File(this.getDataFolder(),"config.yml")); FileConfiguration config = YamlConfiguration.loadConfiguration(new File(this.getDataFolder(),"config.yml"));

View File

@ -18,7 +18,7 @@ public class CommandRestrict implements Listener {
{ {
Player player = event.getPlayer(); Player player = event.getPlayer();
String playercommand = event.getMessage().toLowerCase(); String playercommand = event.getMessage().toLowerCase();
if (plugin.sit.containsKey(player.getName())) if (plugin.getPlayerSitData().isSitting(player))
{ {
if (plugin.sitDisableAllCommands) if (plugin.sitDisableAllCommands)
{ {

View File

@ -0,0 +1,159 @@
package com.cnaude.chairs;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.util.Vector;
public class PlayerSitData {
private Chairs plugin;
public PlayerSitData(Chairs plugin) {
this.plugin = plugin;
}
private HashMap<String, Entity> sit = new HashMap<String, Entity>();
private HashMap<Block, String> sitblock = new HashMap<Block, String>();
private HashMap<String, Block> sitblockbr = new HashMap<String, Block>();
private HashMap<String, Location> sitstopteleportloc = new HashMap<String, Location>();
private HashMap<String, Integer> sittask = new HashMap<String, Integer>();
protected boolean isSitting(Player player)
{
return sit.containsKey(player.getName());
}
protected boolean isBlockOccupied(Block block)
{
return sitblock.containsKey(block);
}
protected Player getPlayerOnChair(Block chair)
{
return Bukkit.getPlayerExact(sitblock.get(chair));
}
protected void sitPlayer(Player player, Location sitlocation)
{
try {
if (plugin.notifyplayer)
{
player.sendMessage(plugin.msgSitting);
}
Block block = sitlocation.getBlock();
sitstopteleportloc.put(player.getName(), player.getLocation());
player.teleport(sitlocation);
Location arrowloc = block.getLocation().add(0.5, 0 , 0.5);
Entity arrow = sitPlayerOnArrow(player, arrowloc);
sit.put(player.getName(), arrow);
sitblock.put(block, player.getName());
sitblockbr.put(player.getName(), block);
startReSitTask(player);
} catch (Exception e) {
e.printStackTrace();
}
}
protected void startReSitTask(final Player player)
{
int task =
Bukkit.getScheduler().scheduleSyncRepeatingTask(plugin, new Runnable()
{
public void run()
{
reSitPlayer(player);
}
},1000,1000);
sittask.put(player.getName(), task);
}
protected void reSitPlayer(final Player player)
{
try {
final Entity prevarrow = sit.get(player.getName());
sit.remove(player.getName());
player.eject();
Block block = sitblockbr.get(player.getName());
Location arrowloc = block.getLocation().add(0.5, 0 , 0.5);
Entity arrow = sitPlayerOnArrow(player, arrowloc);
sit.put(player.getName(), arrow);
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable()
{
public void run()
{
prevarrow.remove();
}
},100);
} catch (Exception e) {
e.printStackTrace();
}
}
private Entity sitPlayerOnArrow(Player player, Location arrowloc) throws NoSuchMethodException, SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException, InstantiationException, InvocationTargetException
{
Entity arrow = player.getWorld().spawnArrow(arrowloc, new Vector(0, 0.1 ,0), 0, 0);
Method getHandleMethod = arrow.getClass().getDeclaredMethod("getHandle");
getHandleMethod.setAccessible(true);
Object nmsarrow = getHandleMethod.invoke(arrow);
Field bukkitEntityField = nmsarrow.getClass().getSuperclass().getDeclaredField("bukkitEntity");
bukkitEntityField.setAccessible(true);
Constructor<?> ctor = plugin.vehiclearrowclass.getDeclaredConstructor(Bukkit.getServer().getClass(), nmsarrow.getClass());
ctor.setAccessible(true);
Object vehiclearrow = ctor.newInstance(Bukkit.getServer(), nmsarrow);
bukkitEntityField.set(nmsarrow, vehiclearrow);
arrow.setPassenger(player);
return arrow;
}
protected void unSitPlayer(final Player player, boolean restoreposition, boolean correctnmspostion)
{
final Entity arrow = sit.get(player.getName());
sit.remove(player.getName());
player.eject();
arrow.remove();
final Location tploc = sitstopteleportloc.get(player.getName());
if (restoreposition)
{
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable()
{
public void run()
{
player.teleport(tploc);
player.setSneaking(false);
}
},1);
} else
{
if (correctnmspostion)
{
try {
Method getHandleMethod = player.getClass().getDeclaredMethod("getHandle");
getHandleMethod.setAccessible(true);
Object nmsPlayer = getHandleMethod.invoke(player);
Class<?> entityClass = nmsPlayer.getClass().getSuperclass().getSuperclass().getSuperclass();
Field locXField = entityClass.getDeclaredField("locX");
locXField.setAccessible(true);
locXField.set(nmsPlayer, tploc.getX());
Field locYField = entityClass.getDeclaredField("locY");
locYField.setAccessible(true);
locYField.set(nmsPlayer, tploc.getY());
Field locZField = entityClass.getDeclaredField("locZ");
locZField.setAccessible(true);
locZField.set(nmsPlayer, tploc.getZ());
} catch (Exception e) {
e.printStackTrace();
}
}
}
sitblock.remove(sitblockbr.get(player.getName()));
sitblockbr.remove(player.getName());
sitstopteleportloc.remove(player.getName());
Bukkit.getScheduler().cancelTask(sittask.get(player.getName()));
sittask.remove(player.getName());
if (plugin.notifyplayer)
{
player.sendMessage(plugin.msgStanding);
}
}
}

View File

@ -34,7 +34,7 @@ public class TrySitEventListener implements Listener {
if (sitAllowed(player, block)) { if (sitAllowed(player, block)) {
event.setCancelled(true); event.setCancelled(true);
Location sitLocation = getSitLocation(block, player.getLocation().getYaw()); Location sitLocation = getSitLocation(block, player.getLocation().getYaw());
plugin.sitPlayer(player, sitLocation); plugin.getPlayerSitData().sitPlayer(player, sitLocation);
} }
} }
} }
@ -67,8 +67,8 @@ public class TrySitEventListener implements Listener {
} }
// Sit occupied check // Sit occupied check
if (plugin.sitblock.containsKey(block)) { if (plugin.getPlayerSitData().isBlockOccupied(block)) {
player.sendMessage(plugin.msgOccupied.replace("%PLAYER%", plugin.sitblock.get(block))); player.sendMessage(plugin.msgOccupied.replace("%PLAYER%", plugin.getPlayerSitData().getPlayerOnChair(block).getName()));
return false; return false;
} }
@ -227,7 +227,7 @@ public class TrySitEventListener implements Listener {
} }
private boolean isSitting(Player player) { private boolean isSitting(Player player) {
return plugin.sit.containsKey(player.getName()); return plugin.getPlayerSitData().isSitting(player);
} }
private int getChairWidth(Block block, BlockFace face) { private int getChairWidth(Block block, BlockFace face) {

View File

@ -26,9 +26,9 @@ 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.sit.containsKey(player.getName())) if (plugin.getPlayerSitData().isSitting(player))
{ {
plugin.unSitPlayer(player, false, true); plugin.getPlayerSitData().unSitPlayer(player, false, true);
} }
} }
@ -36,7 +36,7 @@ public class TryUnsitEventListener implements Listener {
public void onPlayerTeleport(PlayerTeleportEvent event) public void onPlayerTeleport(PlayerTeleportEvent event)
{ {
final Player player = event.getPlayer(); final Player player = event.getPlayer();
if (plugin.sit.containsKey(player.getName())) if (plugin.getPlayerSitData().isSitting(player))
{ {
event.setCancelled(true); event.setCancelled(true);
} }
@ -46,9 +46,9 @@ 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.sit.containsKey(player.getName())) if (plugin.getPlayerSitData().isSitting(player))
{ {
plugin.unSitPlayer(player, false, false); plugin.getPlayerSitData().unSitPlayer(player, false, false);
} }
} }
@ -59,7 +59,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.sit.containsKey(player.getName())) if (plugin.getPlayerSitData().isSitting(player))
{ {
e.setCancelled(true); e.setCancelled(true);
if (!queueUnsit.contains(player.getName())) if (!queueUnsit.contains(player.getName()))
@ -70,7 +70,7 @@ public class TryUnsitEventListener implements Listener {
public void run() public void run()
{ {
queueUnsit.remove(player.getName()); queueUnsit.remove(player.getName());
plugin.unSitPlayer(player, true, false); plugin.getPlayerSitData().unSitPlayer(player, true, false);
} }
}); });
} }
@ -82,10 +82,10 @@ public class TryUnsitEventListener implements Listener {
public void onBlockBreak(BlockBreakEvent event) public void onBlockBreak(BlockBreakEvent event)
{ {
Block b = event.getBlock(); Block b = event.getBlock();
if (plugin.sitblock.containsKey(b)) if (plugin.getPlayerSitData().isBlockOccupied(b))
{ {
Player player = Bukkit.getPlayerExact(plugin.sitblock.get(b)); Player player = plugin.getPlayerSitData().getPlayerOnChair(b);
plugin.unSitPlayer(player, true, false); plugin.getPlayerSitData().unSitPlayer(player, true, false);
} }
} }