Use new factory approach

This commit is contained in:
Shevchik 2014-07-21 02:53:19 +04:00
parent 3a3c322c0d
commit 068eaf2a13
20 changed files with 425 additions and 102 deletions

View File

@ -40,60 +40,52 @@ public class PlayerSitData {
return false;
}
sitlocation = playersitevent.getSitLocation().clone();
try {
if (plugin.notifyplayer) {
player.sendMessage(plugin.msgSitting);
}
SitData sitdata = new SitData();
Location arrowloc = sitlocation.getBlock().getLocation().add(0.5, 0 , 0.5);
Entity arrow = plugin.getNMSAccess().spawnArrow(arrowloc);
sitdata.arrow = arrow;
sitdata.teleportloc = player.getLocation();
int task = Bukkit.getScheduler().scheduleSyncRepeatingTask(
plugin,
new Runnable() {
@Override
public void run() {
reSitPlayer(player);
}
},
1000, 1000
);
sitdata.resittask = task;
player.teleport(sitlocation);
arrow.setPassenger(player);
sitdata.sitting = true;
sit.put(player, sitdata);
sitblock.put(blocktooccupy, player);
} catch (Exception e) {
e.printStackTrace();
if (plugin.notifyplayer) {
player.sendMessage(plugin.msgSitting);
}
SitData sitdata = new SitData();
Location arrowloc = sitlocation.getBlock().getLocation().add(0.5, 0 , 0.5);
Entity arrow = plugin.getNMSAccess().spawnArrow(arrowloc);
sitdata.arrow = arrow;
sitdata.teleportloc = player.getLocation();
int task = Bukkit.getScheduler().scheduleSyncRepeatingTask(
plugin,
new Runnable() {
@Override
public void run() {
reSitPlayer(player);
}
},
1000, 1000
);
sitdata.resittask = task;
player.teleport(sitlocation);
arrow.setPassenger(player);
sitdata.sitting = true;
sit.put(player, sitdata);
sitblock.put(blocktooccupy, player);
return true;
}
public void reSitPlayer(final Player player) {
try {
SitData sitdata = sit.get(player);
sitdata.sitting = false;
final Entity prevarrow = sit.get(player).arrow;
player.eject();
Entity arrow = plugin.getNMSAccess().spawnArrow(prevarrow.getLocation());
arrow.setPassenger(player);
sitdata.arrow = arrow;
sitdata.sitting = true;
Bukkit.getScheduler().scheduleSyncDelayedTask(
plugin,
new Runnable() {
@Override
public void run() {
prevarrow.remove();
}
},
100
);
} catch (Exception e) {
e.printStackTrace();
}
SitData sitdata = sit.get(player);
sitdata.sitting = false;
final Entity prevarrow = sit.get(player).arrow;
player.eject();
Entity arrow = plugin.getNMSAccess().spawnArrow(prevarrow.getLocation());
arrow.setPassenger(player);
sitdata.arrow = arrow;
sitdata.sitting = true;
Bukkit.getScheduler().scheduleSyncDelayedTask(
plugin,
new Runnable() {
@Override
public void run() {
prevarrow.remove();
}
},
100
);
}
public boolean unsitPlayerNormal(Player player) {

View File

@ -1,11 +1,11 @@
package com.cnaude.chairs.sitaddons;
import org.bukkit.Bukkit;
import org.bukkit.entity.Damageable;
import org.bukkit.entity.Entity;
import org.bukkit.entity.ExperienceOrb;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;
import org.bukkit.entity.Damageable;
import org.bukkit.event.player.PlayerExpChangeEvent;
import org.bukkit.event.player.PlayerLevelChangeEvent;
import org.bukkit.event.player.PlayerPickupItemEvent;

View File

@ -1,54 +1,27 @@
package com.cnaude.chairs.vehiclearrow;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Arrow;
public class NMSAccess {
private Class<?> nmsArrowClass;
private NMSArrowFactoryInterface arrowfactory;
public void setupChairsArrow() throws NMSAccessException, ClassNotFoundException {
String pkgname = getClass().getPackage().getName();
String packageName = Bukkit.getServer().getClass().getPackage().getName();
String nmspackageversion = packageName.substring(packageName.lastIndexOf('.') + 1);
switch (nmspackageversion) {
case "v1_7_R4": {
nmsArrowClass = Class.forName(pkgname+"."+"nmsV17R4"+".NMSChairsArrow");
return;
}
case "v1_7_R3": {
nmsArrowClass = Class.forName(pkgname+"."+"nmsV17R3"+".NMSChairsArrow");
return;
}
case "v1_7_R2": {
nmsArrowClass = Class.forName(pkgname+"."+"nmsV17R2"+".NMSChairsArrow");
return;
}
case "v1_7_R1": {
nmsArrowClass = Class.forName(pkgname+"."+"nmsV17R1"+".NMSChairsArrow");
return;
}
case "v1_6_R3": {
nmsArrowClass = Class.forName(pkgname+"."+"nmsV16R3"+".NMSChairsArrow");
return;
}
try {
arrowfactory = (NMSArrowFactoryInterface) Class.forName(pkgname+"."+nmspackageversion+".NMSArrowFactory").newInstance();
return;
} catch (Throwable t) {
}
throw new NMSAccessException("ChairsReloaded is not compatible with your server version");
}
public Arrow spawnArrow(Location location) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
World world = location.getWorld();
Constructor<?> ct = nmsArrowClass.getConstructor(world.getClass());
NMSChairsArrowInterface vehiclearrow = (NMSChairsArrowInterface) ct.newInstance(world);
vehiclearrow.setArrowLocation(location);
vehiclearrow.addToWorld();
vehiclearrow.setBukkitEntity(Bukkit.getServer());
return vehiclearrow.getBukkitArrow();
public Arrow spawnArrow(Location location) {
return arrowfactory.spawnArrow(location);
}
}

View File

@ -0,0 +1,10 @@
package com.cnaude.chairs.vehiclearrow;
import org.bukkit.Location;
import org.bukkit.entity.Arrow;
public interface NMSArrowFactoryInterface {
public Arrow spawnArrow(Location location);
}

View File

@ -1,17 +0,0 @@
package com.cnaude.chairs.vehiclearrow;
import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.entity.Arrow;
public interface NMSChairsArrowInterface {
public void setBukkitEntity(Server server);
public Arrow getBukkitArrow();
public void setArrowLocation(Location location);
public void addToWorld();
}

View File

@ -0,0 +1,30 @@
package com.cnaude.chairs.vehiclearrow.v1_6_R3;
import net.minecraft.server.v1_6_R3.EntityArrow;
import org.bukkit.craftbukkit.v1_6_R3.CraftServer;
import org.bukkit.craftbukkit.v1_6_R3.entity.CraftArrow;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Vehicle;
import com.cnaude.chairs.api.ChairsAPI;
public class CraftChairsArrow extends CraftArrow implements Vehicle {
public CraftChairsArrow(CraftServer server, EntityArrow entity) {
super(server, entity);
}
@Override
public void remove() {
Entity passenger = getPassenger();
if (passenger != null && passenger instanceof Player) {
if (ChairsAPI.isSitting((Player) passenger)) {
return;
}
}
super.remove();
}
}

View File

@ -0,0 +1,20 @@
package com.cnaude.chairs.vehiclearrow.v1_6_R3;
import net.minecraft.server.v1_6_R3.EntityArrow;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_6_R3.CraftWorld;
import org.bukkit.entity.Arrow;
import com.cnaude.chairs.vehiclearrow.NMSArrowFactoryInterface;
public class NMSArrowFactory implements NMSArrowFactoryInterface {
@Override
public Arrow spawnArrow(Location location) {
CraftWorld world = (CraftWorld) location.getWorld();
EntityArrow arrow = new NMSChairsArrow(world, location);
return (Arrow) arrow.getBukkitEntity();
}
}

View File

@ -0,0 +1,23 @@
package com.cnaude.chairs.vehiclearrow.v1_6_R3;
import net.minecraft.server.v1_6_R3.EntityArrow;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_6_R3.CraftServer;
import org.bukkit.craftbukkit.v1_6_R3.CraftWorld;
public class NMSChairsArrow extends EntityArrow {
public NMSChairsArrow(CraftWorld cworld, Location location) {
super(cworld.getHandle());
setPositionRotation(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
world.addEntity(this);
bukkitEntity = new CraftChairsArrow((CraftServer) Bukkit.getServer(), this);
}
@Override
public void l_() {
}
}

View File

@ -0,0 +1,30 @@
package com.cnaude.chairs.vehiclearrow.v1_7_R1;
import net.minecraft.server.v1_7_R1.EntityArrow;
import org.bukkit.craftbukkit.v1_7_R1.CraftServer;
import org.bukkit.craftbukkit.v1_7_R1.entity.CraftArrow;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Vehicle;
import com.cnaude.chairs.api.ChairsAPI;
public class CraftChairsArrow extends CraftArrow implements Vehicle {
public CraftChairsArrow(CraftServer server, EntityArrow entity) {
super(server, entity);
}
@Override
public void remove() {
Entity passenger = getPassenger();
if (passenger != null && passenger instanceof Player) {
if (ChairsAPI.isSitting((Player) passenger)) {
return;
}
}
super.remove();
}
}

View File

@ -0,0 +1,20 @@
package com.cnaude.chairs.vehiclearrow.v1_7_R1;
import net.minecraft.server.v1_7_R1.EntityArrow;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_7_R1.CraftWorld;
import org.bukkit.entity.Arrow;
import com.cnaude.chairs.vehiclearrow.NMSArrowFactoryInterface;
public class NMSArrowFactory implements NMSArrowFactoryInterface {
@Override
public Arrow spawnArrow(Location location) {
CraftWorld world = (CraftWorld) location.getWorld();
EntityArrow arrow = new NMSChairsArrow(world, location);
return (Arrow) arrow.getBukkitEntity();
}
}

View File

@ -0,0 +1,23 @@
package com.cnaude.chairs.vehiclearrow.v1_7_R1;
import net.minecraft.server.v1_7_R1.EntityArrow;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_7_R1.CraftServer;
import org.bukkit.craftbukkit.v1_7_R1.CraftWorld;
public class NMSChairsArrow extends EntityArrow {
public NMSChairsArrow(CraftWorld cworld, Location location) {
super(cworld.getHandle());
setPositionRotation(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
world.addEntity(this);
bukkitEntity = new CraftChairsArrow((CraftServer) Bukkit.getServer(), this);
}
@Override
public void h() {
}
}

View File

@ -0,0 +1,30 @@
package com.cnaude.chairs.vehiclearrow.v1_7_R2;
import net.minecraft.server.v1_7_R2.EntityArrow;
import org.bukkit.craftbukkit.v1_7_R2.CraftServer;
import org.bukkit.craftbukkit.v1_7_R2.entity.CraftArrow;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Vehicle;
import com.cnaude.chairs.api.ChairsAPI;
public class CraftChairsArrow extends CraftArrow implements Vehicle {
public CraftChairsArrow(CraftServer server, EntityArrow entity) {
super(server, entity);
}
@Override
public void remove() {
Entity passenger = getPassenger();
if (passenger != null && passenger instanceof Player) {
if (ChairsAPI.isSitting((Player) passenger)) {
return;
}
}
super.remove();
}
}

View File

@ -0,0 +1,20 @@
package com.cnaude.chairs.vehiclearrow.v1_7_R2;
import net.minecraft.server.v1_7_R2.EntityArrow;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_7_R2.CraftWorld;
import org.bukkit.entity.Arrow;
import com.cnaude.chairs.vehiclearrow.NMSArrowFactoryInterface;
public class NMSArrowFactory implements NMSArrowFactoryInterface {
@Override
public Arrow spawnArrow(Location location) {
CraftWorld world = (CraftWorld) location.getWorld();
EntityArrow arrow = new NMSChairsArrow(world, location);
return (Arrow) arrow.getBukkitEntity();
}
}

View File

@ -0,0 +1,23 @@
package com.cnaude.chairs.vehiclearrow.v1_7_R2;
import net.minecraft.server.v1_7_R2.EntityArrow;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_7_R2.CraftServer;
import org.bukkit.craftbukkit.v1_7_R2.CraftWorld;
public class NMSChairsArrow extends EntityArrow {
public NMSChairsArrow(CraftWorld cworld, Location location) {
super(cworld.getHandle());
setPositionRotation(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
world.addEntity(this);
bukkitEntity = new CraftChairsArrow((CraftServer) Bukkit.getServer(), this);
}
@Override
public void h() {
}
}

View File

@ -0,0 +1,30 @@
package com.cnaude.chairs.vehiclearrow.v1_7_R3;
import net.minecraft.server.v1_7_R3.EntityArrow;
import org.bukkit.craftbukkit.v1_7_R3.CraftServer;
import org.bukkit.craftbukkit.v1_7_R3.entity.CraftArrow;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Vehicle;
import com.cnaude.chairs.api.ChairsAPI;
public class CraftChairsArrow extends CraftArrow implements Vehicle {
public CraftChairsArrow(CraftServer server, EntityArrow entity) {
super(server, entity);
}
@Override
public void remove() {
Entity passenger = getPassenger();
if (passenger != null && passenger instanceof Player) {
if (ChairsAPI.isSitting((Player) passenger)) {
return;
}
}
super.remove();
}
}

View File

@ -0,0 +1,20 @@
package com.cnaude.chairs.vehiclearrow.v1_7_R3;
import net.minecraft.server.v1_7_R3.EntityArrow;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_7_R3.CraftWorld;
import org.bukkit.entity.Arrow;
import com.cnaude.chairs.vehiclearrow.NMSArrowFactoryInterface;
public class NMSArrowFactory implements NMSArrowFactoryInterface {
@Override
public Arrow spawnArrow(Location location) {
CraftWorld world = (CraftWorld) location.getWorld();
EntityArrow arrow = new NMSChairsArrow(world, location);
return (Arrow) arrow.getBukkitEntity();
}
}

View File

@ -0,0 +1,23 @@
package com.cnaude.chairs.vehiclearrow.v1_7_R3;
import net.minecraft.server.v1_7_R3.EntityArrow;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_7_R3.CraftServer;
import org.bukkit.craftbukkit.v1_7_R3.CraftWorld;
public class NMSChairsArrow extends EntityArrow {
public NMSChairsArrow(CraftWorld cworld, Location location) {
super(cworld.getHandle());
setPositionRotation(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
world.addEntity(this);
bukkitEntity = new CraftChairsArrow((CraftServer) Bukkit.getServer(), this);
}
@Override
public void h() {
}
}

View File

@ -0,0 +1,30 @@
package com.cnaude.chairs.vehiclearrow.v1_7_R4;
import net.minecraft.server.v1_7_R4.EntityArrow;
import org.bukkit.craftbukkit.v1_7_R4.CraftServer;
import org.bukkit.craftbukkit.v1_7_R4.entity.CraftArrow;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.entity.Vehicle;
import com.cnaude.chairs.api.ChairsAPI;
public class CraftChairsArrow extends CraftArrow implements Vehicle {
public CraftChairsArrow(CraftServer server, EntityArrow entity) {
super(server, entity);
}
@Override
public void remove() {
Entity passenger = getPassenger();
if (passenger != null && passenger instanceof Player) {
if (ChairsAPI.isSitting((Player) passenger)) {
return;
}
}
super.remove();
}
}

View File

@ -0,0 +1,20 @@
package com.cnaude.chairs.vehiclearrow.v1_7_R4;
import net.minecraft.server.v1_7_R4.EntityArrow;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_7_R4.CraftWorld;
import org.bukkit.entity.Arrow;
import com.cnaude.chairs.vehiclearrow.NMSArrowFactoryInterface;
public class NMSArrowFactory implements NMSArrowFactoryInterface {
@Override
public Arrow spawnArrow(Location location) {
CraftWorld world = (CraftWorld) location.getWorld();
EntityArrow arrow = new NMSChairsArrow(world, location);
return (Arrow) arrow.getBukkitEntity();
}
}

View File

@ -0,0 +1,23 @@
package com.cnaude.chairs.vehiclearrow.v1_7_R4;
import net.minecraft.server.v1_7_R4.EntityArrow;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.craftbukkit.v1_7_R4.CraftServer;
import org.bukkit.craftbukkit.v1_7_R4.CraftWorld;
public class NMSChairsArrow extends EntityArrow {
public NMSChairsArrow(CraftWorld cworld, Location location) {
super(cworld.getHandle());
setPositionRotation(location.getX(), location.getY(), location.getZ(), location.getYaw(), location.getPitch());
world.addEntity(this);
bukkitEntity = new CraftChairsArrow((CraftServer) Bukkit.getServer(), this);
}
@Override
public void h() {
}
}