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; return false;
} }
sitlocation = playersitevent.getSitLocation().clone(); sitlocation = playersitevent.getSitLocation().clone();
try { if (plugin.notifyplayer) {
if (plugin.notifyplayer) { player.sendMessage(plugin.msgSitting);
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();
} }
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; return true;
} }
public void reSitPlayer(final Player player) { public void reSitPlayer(final Player player) {
try { SitData sitdata = sit.get(player);
SitData sitdata = sit.get(player); sitdata.sitting = false;
sitdata.sitting = false; final Entity prevarrow = sit.get(player).arrow;
final Entity prevarrow = sit.get(player).arrow; player.eject();
player.eject(); Entity arrow = plugin.getNMSAccess().spawnArrow(prevarrow.getLocation());
Entity arrow = plugin.getNMSAccess().spawnArrow(prevarrow.getLocation()); arrow.setPassenger(player);
arrow.setPassenger(player); sitdata.arrow = arrow;
sitdata.arrow = arrow; sitdata.sitting = true;
sitdata.sitting = true; Bukkit.getScheduler().scheduleSyncDelayedTask(
Bukkit.getScheduler().scheduleSyncDelayedTask( plugin,
plugin, new Runnable() {
new Runnable() { @Override
@Override public void run() {
public void run() { prevarrow.remove();
prevarrow.remove(); }
} },
}, 100
100 );
);
} catch (Exception e) {
e.printStackTrace();
}
} }
public boolean unsitPlayerNormal(Player player) { public boolean unsitPlayerNormal(Player player) {

View File

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

View File

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

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() {
}
}