diff --git a/modules/VirtualPlayers/pom.xml b/modules/VirtualPlayers/pom.xml index 3b21318..f0542aa 100644 --- a/modules/VirtualPlayers/pom.xml +++ b/modules/VirtualPlayers/pom.xml @@ -3,7 +3,7 @@ 4.0.0 mc.alk VirtualPlayers - 1.8.3 + 1.9.0 jar VirtualPlayers http://dev.bukkit.org/bukkit-plugins/virtualplayers2/ @@ -286,6 +286,14 @@ compile true + + mc.alk + vp-v1_15_R1 + static_version + jar + compile + true + org.bstats bstats-bukkit @@ -358,7 +366,7 @@ org.apache.maven.plugins maven-shade-plugin - 2.3 + 3.2.1 package diff --git a/modules/VirtualPlayers/src/main/java/mc/alk/virtualplayers/executors/VPBaseExecutor.java b/modules/VirtualPlayers/src/main/java/mc/alk/virtualplayers/executors/VPBaseExecutor.java index 2a61256..5abe1e8 100644 --- a/modules/VirtualPlayers/src/main/java/mc/alk/virtualplayers/executors/VPBaseExecutor.java +++ b/modules/VirtualPlayers/src/main/java/mc/alk/virtualplayers/executors/VPBaseExecutor.java @@ -82,7 +82,6 @@ protected Object invoke(CustomCommandExecutor.MethodWrapper mwrapper, CustomComm } } - @SuppressWarnings("SimplifiableIfStatement") @Override public boolean sendMessage(CommandSender sender, String string) { if (string == null || !showEventMessages) { diff --git a/modules/VirtualPlayers/src/main/java/mc/alk/virtualplayers/executors/VPExecutor.java b/modules/VirtualPlayers/src/main/java/mc/alk/virtualplayers/executors/VPExecutor.java index 65d0653..31cebeb 100644 --- a/modules/VirtualPlayers/src/main/java/mc/alk/virtualplayers/executors/VPExecutor.java +++ b/modules/VirtualPlayers/src/main/java/mc/alk/virtualplayers/executors/VPExecutor.java @@ -1,16 +1,14 @@ package mc.alk.virtualplayers.executors; -import java.util.Collection; -import java.util.Collections; import java.util.Comparator; import java.util.List; -import mc.alk.virtualplayers.VirtualPlayers; import mc.alk.virtualplayers.api.VirtualPlayer; import mc.alk.virtualplayers.api.Vps; import org.bukkit.Bukkit; import org.bukkit.command.CommandSender; +import org.bukkit.entity.HumanEntity; import org.bukkit.plugin.Plugin; /** @@ -48,15 +46,8 @@ public void listVirtualPlayers(CommandSender sender) { List players = Vps.getApi().getVirtualPlayersList(); sender.sendMessage("VirtualPlayers count=" + players.size()); - Collections.sort(players, new Comparator() { - @Override - public int compare(VirtualPlayer o1, VirtualPlayer o2) { - return o1.getName().compareTo(o2.getName()); - } - }); - players.forEach((vp) -> { - sendMessage(sender, vp.getName() + " : " + vp); - }); + players.sort(Comparator.comparing(HumanEntity::getName)); + players.forEach((vp) -> sendMessage(sender, vp.getName() + " : " + vp)); } @MCCommand(cmds = {"showMessages"}, op = true) diff --git a/modules/api/src/main/java/mc/alk/virtualplayers/api/DamageHandlerFactory.java b/modules/api/src/main/java/mc/alk/virtualplayers/api/DamageHandlerFactory.java index daaf26b..d5c656e 100644 --- a/modules/api/src/main/java/mc/alk/virtualplayers/api/DamageHandlerFactory.java +++ b/modules/api/src/main/java/mc/alk/virtualplayers/api/DamageHandlerFactory.java @@ -17,8 +17,8 @@ public class DamageHandlerFactory { public static IDamageHandler getNewInstance() { Version server = VersionFactory.getServerVersion(); - IDamageHandler handler = null; - Class clazz = null; + IDamageHandler handler = IDamageHandler.DEFAULT_HANDLER; + Class clazz = null; try { Class[] args = {}; if (server.isGreaterThanOrEqualTo("1.2.5") && server.isLessThan("1.6.1")) { @@ -27,11 +27,14 @@ public static IDamageHandler getNewInstance() { clazz = Class.forName("mc.alk.virtualplayers.nms.v1_6_R1.DamageHandler"); } + if (clazz == null) + return handler; + handler = (IDamageHandler) clazz.getConstructor(args).newInstance((Object[]) args); } catch (Exception ex) { ex.printStackTrace(); } - return (handler == null) ? IDamageHandler.DEFAULT_HANDLER : handler; + return handler; } } \ No newline at end of file diff --git a/modules/api/src/main/java/mc/alk/virtualplayers/api/VirtualPlayer.java b/modules/api/src/main/java/mc/alk/virtualplayers/api/VirtualPlayer.java index 0312af6..1779757 100644 --- a/modules/api/src/main/java/mc/alk/virtualplayers/api/VirtualPlayer.java +++ b/modules/api/src/main/java/mc/alk/virtualplayers/api/VirtualPlayer.java @@ -9,13 +9,13 @@ */ public interface VirtualPlayer extends Player { - public void setOnline(boolean b); + void setOnline(boolean b); - public void respawn(Location loc); - public void moveTo(Location loc); - public boolean teleport(Location location, boolean respawn); + void respawn(Location loc); + void moveTo(Location loc); + boolean teleport(Location location, boolean respawn); - public Player getInformed(); - public void setShowMessages(boolean visibility); + Player getInformed(); + void setShowMessages(boolean visibility); } diff --git a/modules/api/src/main/java/mc/alk/virtualplayers/api/VirtualPlayerFactory.java b/modules/api/src/main/java/mc/alk/virtualplayers/api/VirtualPlayerFactory.java index 76c26f4..587afd2 100644 --- a/modules/api/src/main/java/mc/alk/virtualplayers/api/VirtualPlayerFactory.java +++ b/modules/api/src/main/java/mc/alk/virtualplayers/api/VirtualPlayerFactory.java @@ -10,7 +10,6 @@ import mc.euro.version.VersionFactory; -import org.bukkit.entity.Player; /** * abstract VirtualPlayerFactory: handle the creation, tracking, and deletion of VirtualPlayers. @@ -41,15 +40,15 @@ public static synchronized VirtualPlayerFactory newInstance() { return factory; } Class[] args = {}; - Constructor con = null; - VirtualPlayerFactory $factory = null; + Constructor con; + VirtualPlayerFactory factory = null; try { con = getNmsClass("CraftVirtualPlayerFactory").getConstructor(args); - $factory = (VirtualPlayerFactory) con.newInstance(); + factory = (VirtualPlayerFactory) con.newInstance(); } catch (Exception ex) { ex.printStackTrace(); } - return $factory; + return factory; } private static Class getNmsClass(String clazz) throws Exception { @@ -61,7 +60,7 @@ private static Class getNmsClass(String clazz) throws Exception { public abstract void deleteVirtualPlayer(VirtualPlayer vp); public static VirtualPlayer getOrCreate(String name) { - Player vp = names.get(name); + VirtualPlayer vp = names.get(name); if (vp == null) { try { vp = factory.makeVirtualPlayer(name); @@ -69,7 +68,7 @@ public static VirtualPlayer getOrCreate(String name) { e.printStackTrace(); } } - return (VirtualPlayer) vp; + return vp; } public static Collection getVirtualPlayers() { @@ -86,7 +85,7 @@ public static Map getNames() { public static List getNewPlayerList() { synchronized (vps) { - return new ArrayList(vps.values()); + return new ArrayList<>(vps.values()); } } diff --git a/modules/api/src/main/java/mc/alk/virtualplayers/api/VirtualPlayersAPI.java b/modules/api/src/main/java/mc/alk/virtualplayers/api/VirtualPlayersAPI.java index 9146079..782f354 100644 --- a/modules/api/src/main/java/mc/alk/virtualplayers/api/VirtualPlayersAPI.java +++ b/modules/api/src/main/java/mc/alk/virtualplayers/api/VirtualPlayersAPI.java @@ -7,6 +7,7 @@ import java.util.UUID; import mc.euro.bukkitinterface.BukkitInterface; import org.bukkit.Bukkit; +import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; /** @@ -28,53 +29,47 @@ public interface VirtualPlayersAPI { * * @return an Array of bukkit.entity.Player + api.VirtualPlayer. */ - public default Collection getOnlinePlayers() { + default Collection getOnlinePlayers() { List players = new ArrayList<>(); - VirtualPlayerFactory.getVirtualPlayers().stream().filter((p) -> (p.isOnline())).forEachOrdered((p) -> { - players.add(p); - }); + VirtualPlayerFactory.getVirtualPlayers().stream().filter(OfflinePlayer::isOnline).forEachOrdered(players::add); players.addAll(BukkitInterface.getOnlinePlayers()); return players; }; - public Player makeVirtualPlayer(String name) throws Exception; - public void setEventMessages(boolean visibility); - public void deleteVirtualPlayer(VirtualPlayer vp); - public void deleteVirtualPlayers(); + Player makeVirtualPlayer(String name) throws Exception; + void setEventMessages(boolean visibility); + void deleteVirtualPlayer(VirtualPlayer vp); + void deleteVirtualPlayers(); - public default void setGlobalMessages(boolean visibility) { - VirtualPlayerFactory.getVirtualPlayers().forEach((vp) -> { - vp.setShowMessages(visibility); - }); + default void setGlobalMessages(boolean visibility) { + VirtualPlayerFactory.getVirtualPlayers().forEach((vp) -> vp.setShowMessages(visibility)); } - public default Player[] getOnlinePlayersArray() { + default Player[] getOnlinePlayersArray() { return getOnlinePlayers().toArray(new Player[0]); - }; + } - public default Collection getVirtualPlayers() { + default Collection getVirtualPlayers() { return VirtualPlayerFactory.getVirtualPlayers(); - }; + } - public default List getVirtualPlayersList() { + default List getVirtualPlayersList() { return VirtualPlayerFactory.getNewPlayerList(); } - public default void setPlayerMessages(boolean visibility) { - getVirtualPlayers().forEach((vp) -> { - vp.setShowMessages(visibility); - }); + default void setPlayerMessages(boolean visibility) { + getVirtualPlayers().forEach((vp) -> vp.setShowMessages(visibility)); } - public default Map getVps() { + default Map getVps() { return VirtualPlayerFactory.getVps(); } - public default Map getNames() { + default Map getNames() { return VirtualPlayerFactory.getNames(); } - public default Player getPlayer(String pname) { + default Player getPlayer(String pname) { Player vp = Bukkit.getPlayer(pname); if (vp == null) { vp = getNames().get(pname); @@ -82,7 +77,7 @@ public default Player getPlayer(String pname) { return vp; } - public default Player getPlayer(UUID id) { + default Player getPlayer(UUID id) { Player vp = Bukkit.getPlayer(id); if (vp == null) { vp = getVps().get(id); @@ -90,7 +85,7 @@ public default Player getPlayer(UUID id) { return vp; } - public default Player getPlayerExact(String pname) { + default Player getPlayerExact(String pname) { Player vp = Bukkit.getPlayerExact(pname); if (vp == null) { vp = getNames().get(pname); @@ -98,7 +93,7 @@ public default Player getPlayerExact(String pname) { return vp; } - public default Player getOrMakePlayer(String pname) { + default Player getOrMakePlayer(String pname) { Player vp = Bukkit.getPlayer(pname); if (vp == null) { vp = getNames().get(pname); @@ -113,7 +108,7 @@ public default Player getOrMakePlayer(String pname) { return vp; } - public default VirtualPlayer getOrCreate(String name) { + default VirtualPlayer getOrCreate(String name) { Player vp = VirtualPlayerFactory.getNames().get(name); if (vp == null) { try { @@ -125,11 +120,11 @@ public default VirtualPlayer getOrCreate(String name) { return (VirtualPlayer) vp; } - public default Player makeVirtualPlayer() throws Exception { + default Player makeVirtualPlayer() throws Exception { return makeVirtualPlayer(null); } - public default void deleteVirtualPlayer(String name) { + default void deleteVirtualPlayer(String name) { VirtualPlayer vp = getNames().get(name); deleteVirtualPlayer(vp); } diff --git a/modules/api/src/main/java/mc/alk/virtualplayers/api/handlers/IDamageHandler.java b/modules/api/src/main/java/mc/alk/virtualplayers/api/handlers/IDamageHandler.java index ea78dc4..fc2f504 100644 --- a/modules/api/src/main/java/mc/alk/virtualplayers/api/handlers/IDamageHandler.java +++ b/modules/api/src/main/java/mc/alk/virtualplayers/api/handlers/IDamageHandler.java @@ -12,13 +12,7 @@ */ public interface IDamageHandler { - public void damageEntity(Player player, double dmg); + void damageEntity(Player player, double dmg); - public static final IDamageHandler DEFAULT_HANDLER = new IDamageHandler() { - - @Override - public void damageEntity(Player player, double dmg) { - player.damage((int) dmg); - } - }; + IDamageHandler DEFAULT_HANDLER = (player, dmg) -> player.damage((int) dmg); } diff --git a/modules/api/src/main/java/mc/alk/virtualplayers/util/Util.java b/modules/api/src/main/java/mc/alk/virtualplayers/util/Util.java index eebfb85..b0c499a 100644 --- a/modules/api/src/main/java/mc/alk/virtualplayers/util/Util.java +++ b/modules/api/src/main/java/mc/alk/virtualplayers/util/Util.java @@ -21,10 +21,10 @@ public class Util { public interface VPMessageListener { - public void gettingMessage(Player player, String msg); + void gettingMessage(Player player, String msg); } - static final List listeners = new ArrayList(); + static final List listeners = new ArrayList<>(); static public String getLocString(Location l) { if (l == null) { @@ -80,7 +80,7 @@ public static boolean removeListener(VPMessageListener listener) { * @return the nearby entities */ public static Collection getNearbyEntities(VirtualPlayer vp, int radius) { - Map emap = new HashMap(); + Map emap = new HashMap<>(); int numChunks = (int) Math.floor(radius / 16) + 1; int blocks = numChunks * 16; int diameter = blocks * 2; diff --git a/modules/v1_15_R1/pom.xml b/modules/v1_15_R1/pom.xml new file mode 100644 index 0000000..a2b6123 --- /dev/null +++ b/modules/v1_15_R1/pom.xml @@ -0,0 +1,47 @@ + + + 4.0.0 + + vp-parent-pom + mc.alk + static_version + ../../ + + mc.alk + vp-v1_15_R1 + static_version + jar + virtualplayers-v1_15_R1 + + + org.spigotmc + spigot + 1.15.0-R0.1 + provided + + + mc.alk + VirtualPlayersAPI + jar + ${virtualplayers.api.version} + true + + + junit + junit + test + + + + ./target + ./src/main/java + + + org.apache.maven.plugins + maven-compiler-plugin + + + + \ No newline at end of file diff --git a/modules/v1_15_R1/src/main/java/mc/alk/virtualplayers/nms/v1_15_R1/CraftVirtualPlayer.java b/modules/v1_15_R1/src/main/java/mc/alk/virtualplayers/nms/v1_15_R1/CraftVirtualPlayer.java new file mode 100644 index 0000000..857c3d4 --- /dev/null +++ b/modules/v1_15_R1/src/main/java/mc/alk/virtualplayers/nms/v1_15_R1/CraftVirtualPlayer.java @@ -0,0 +1,338 @@ +package mc.alk.virtualplayers.nms.v1_15_R1; + +import com.mojang.authlib.GameProfile; +import mc.alk.virtualplayers.api.VirtualPlayer; +import mc.alk.virtualplayers.util.Util; +import net.minecraft.server.v1_15_R1.BlockPosition; +import net.minecraft.server.v1_15_R1.EntityPlayer; +import net.minecraft.server.v1_15_R1.EnumMoveType; +import net.minecraft.server.v1_15_R1.MinecraftServer; +import net.minecraft.server.v1_15_R1.PacketPlayOutBlockChange; +import net.minecraft.server.v1_15_R1.PlayerInteractManager; +import net.minecraft.server.v1_15_R1.Vec3D; +import net.minecraft.server.v1_15_R1.WorldServer; +import org.bukkit.Bukkit; +import org.bukkit.GameMode; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.craftbukkit.v1_15_R1.CraftServer; +import org.bukkit.craftbukkit.v1_15_R1.CraftWorld; +import org.bukkit.craftbukkit.v1_15_R1.entity.CraftPlayer; +import org.bukkit.craftbukkit.v1_15_R1.scoreboard.CraftScoreboard; +import org.bukkit.craftbukkit.v1_15_R1.util.CraftMagicNumbers; +import org.bukkit.entity.Player; +import org.bukkit.event.player.PlayerChangedWorldEvent; +import org.bukkit.event.player.PlayerRespawnEvent; +import org.bukkit.event.player.PlayerTeleportEvent; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.InventoryView; +import org.bukkit.potion.PotionEffect; +import org.bukkit.potion.PotionEffectType; +import org.bukkit.scoreboard.DisplaySlot; +import org.bukkit.scoreboard.Scoreboard; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; + +public class CraftVirtualPlayer extends CraftPlayer implements VirtualPlayer { + + Player keepInformed; // / who to send the messages to + boolean online = true; + double health = 20; + boolean isop = true; + boolean showMessages = true; + boolean showTeleports = true; + GameMode gamemode = GameMode.SURVIVAL; + Location location; + CraftScoreboard scoreboard; + + public CraftVirtualPlayer(CraftServer cserver, MinecraftServer mcserver, WorldServer worldServer, + GameProfile gameProfile, PlayerInteractManager pim, Location loc) { + super(cserver, new EntityPlayer(mcserver, worldServer, gameProfile, pim)); + this.location = loc; + } + + public CraftVirtualPlayer(CraftServer cserver, MinecraftServer mcserver, WorldServer worldServer, + GameProfile gameProfile, PlayerInteractManager pim) { + super(cserver, new EntityPlayer(mcserver, worldServer, gameProfile, pim)); + this.location = this.getLocation(); + } + + public CraftVirtualPlayer(CraftServer cserver, EntityPlayer ep) { + super(cserver, ep); + this.location = this.getLocation(); + } + + @Override + public List getNearbyEntities(double x, double y, double z) { + List notchEntityList = this.entity.world.getEntities(this.entity, this.entity.getBoundingBox().grow(x, y, z)); + List bukkitEntityList = new ArrayList<>(notchEntityList.size()); + for (net.minecraft.server.v1_15_R1.Entity e : notchEntityList) { + bukkitEntityList.add(e.getBukkitEntity()); + } + return bukkitEntityList; + } + + @Override + public InventoryView openInventory(Inventory inv) { + return null; + } + + @Override + public boolean addPotionEffect(PotionEffect effect) { + return false; + } + + @Override + public boolean addPotionEffect(PotionEffect effect, boolean force) { + return false; + } + + @Override + public boolean addPotionEffects(Collection effects) { + return false; + } + + @Override + public void removePotionEffect(PotionEffectType effect) { + /// do nothing + } + + @Override + public void closeInventory() { + /// do nothing + } + + @Override + public void updateInventory() { + /// Do nothing + } + + @Override + public void setGameMode(GameMode gamemode) { + try { + super.setGameMode(gamemode); + } catch (Exception e) { + /* say nothing*/ + } + this.gamemode = gamemode; + } + + @Override + public GameMode getGameMode() { + return gamemode; + } + + @Override + public double getHealth() { + return health; + } + + @Override + public void setHealth(double h) { + if (h < 0) { + h = 0; + } + this.health = h; + try { + super.setHealth(h); + } catch (Exception e) { + } + try { + this.getHandle().setHealth((float) h); + } catch (Exception e) { + e.printStackTrace(); + } + } + + @Override + public boolean isDead() { + return super.isDead() || health <= 0; + } + + @Override + public void sendMessage(String s) { + if (showMessages) { + Util.sendMessage(this, (!isOnline() ? "&4(Offline)&b" : "") + + getName() + " gettingMessage= " + s); + } + } + + @Override + public void moveTo(Location loc) { + entity.move(EnumMoveType.SELF, new Vec3D(loc.getX(), loc.getY(), loc.getZ())); + } + + @Override + public boolean teleport(Location l, boolean respawn) { + if (isDead()) { + return false; + } + try { + boolean changedWorlds = !this.location.getWorld().getName() + .equals(l.getWorld().getName()); + final String teleporting = respawn ? "respawning" : "teleporting"; + if (showTeleports && showMessages) { + String fromWorld = ""; + String toWorld = ""; + if (changedWorlds) { + fromWorld = "&5" + location.getWorld().getName() + "&4,"; + toWorld = "&5" + l.getWorld().getName() + "&4,"; + } + Util.sendMessage(this, getName() + "&e " + teleporting + " from &4" + + fromWorld + Util.getLocString(location) + " &e-> &4" + toWorld + + Util.getLocString(l)); + } + this.location = l.clone(); + if (changedWorlds) { + PlayerChangedWorldEvent pcwe = new PlayerChangedWorldEvent(this, + l.getWorld()); + CraftServer cserver = (CraftServer) Bukkit.getServer(); + cserver.getPluginManager().callEvent(pcwe); + /// For some reason, world doesnt get changed, so lets explicitly set it + this.entity.world = ((CraftWorld) location.getWorld()).getHandle(); + } + } catch (Exception e) { + e.printStackTrace(); + } + return true; + } + + @Override + public boolean teleport(Location location, PlayerTeleportEvent.TeleportCause cause) { + if (isDead()) { + return false; + } + super.teleport(location, cause); + teleport(location, false); + return true; + } + + @Override + public boolean teleport(Location l) { + return teleport(l, PlayerTeleportEvent.TeleportCause.UNKNOWN); + } + + @Override + public void respawn(Location loc) { + this.health = 20; + boolean changedWorlds = !this.location.getWorld().getName() + .equals(loc.getWorld().getName()); + CraftServer cserver = (CraftServer) Bukkit.getServer(); + PlayerRespawnEvent respawnEvent = new PlayerRespawnEvent(this, loc, + false); + cserver.getPluginManager().callEvent(respawnEvent); + if (changedWorlds) { + PlayerChangedWorldEvent pcwe = new PlayerChangedWorldEvent(this, + loc.getWorld()); + cserver.getPluginManager().callEvent(pcwe); + } + } + + @Override + public Location getLocation() { + return location; + } + + @Override + public boolean isOnline() { + return online; + } + + @Override + public void setOnline(boolean b) { + if (showMessages) { + Util.sendMessage(this, getName() + " is " + + (b ? "connecting" : "disconnecting")); + } + online = b; + } + + @Override + public boolean isOp() { + return isop; + } + + @Override + public void setOp(boolean b) { + isop = b; + } + + @Override + public String toString() { + String world = "&5" + this.location.getWorld().getName() + ","; + return getName() + "&e h=&2" + getHealth() + "&e o=&5" + isOnline() + + "&e d=&7" + isDead() + "&e loc=&4" + world + "&4" + + Util.getLocString(location) + "&e gm=&8" + getGameMode(); + } + + @Override + public void setScoreboard(Scoreboard scoreboard) { + Object s = null; + this.scoreboard = (CraftScoreboard) scoreboard; + if (scoreboard != null) { + if (Bukkit.getScoreboardManager().getMainScoreboard() != null + && scoreboard.equals(Bukkit.getScoreboardManager().getMainScoreboard())) { + s = "BukkitMainScoreboard"; + } else if (scoreboard.getObjective(DisplaySlot.SIDEBAR) != null) { + s = scoreboard.getObjective(DisplaySlot.SIDEBAR).getName(); + } else if (scoreboard.getObjective(DisplaySlot.PLAYER_LIST) != null) { + s = scoreboard.getObjective(DisplaySlot.PLAYER_LIST).getName(); + } + } + if (showMessages) { + Util.sendMessage(this, getName() + " setting scoreboard " + s); + } + } + + @Override + public CraftScoreboard getScoreboard() { + return this.scoreboard; + } + + public void setLocation(Location l) { + location = l; + } + + @Override + public Player getInformed() { + return keepInformed; + } + + @Override + public void setShowMessages(boolean visibility) { + showMessages = visibility; + } + + // @Override + public void sendBlockChange(Location loc, String block, byte data) { + if (this.getHandle().playerConnection == null) { + return; + } + PacketPlayOutBlockChange packet = new PacketPlayOutBlockChange(((CraftWorld)loc.getWorld()).getHandle(), new BlockPosition(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ())); + Material material = Material.getMaterial(block); + packet.block = CraftMagicNumbers.getBlock(material, data); + this.getHandle().playerConnection.sendPacket(packet); + } + + @Override + public boolean sleep(Location loc, boolean b) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public void wakeup(boolean b) { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public Location getBedLocation() { + throw new UnsupportedOperationException("Not supported yet."); + } + + @Override + public EntityPlayer getHandle() { + return super.getHandle(); + } +} diff --git a/modules/v1_15_R1/src/main/java/mc/alk/virtualplayers/nms/v1_15_R1/CraftVirtualPlayerFactory.java b/modules/v1_15_R1/src/main/java/mc/alk/virtualplayers/nms/v1_15_R1/CraftVirtualPlayerFactory.java new file mode 100644 index 0000000..527aa24 --- /dev/null +++ b/modules/v1_15_R1/src/main/java/mc/alk/virtualplayers/nms/v1_15_R1/CraftVirtualPlayerFactory.java @@ -0,0 +1,68 @@ +package mc.alk.virtualplayers.nms.v1_15_R1; + +import com.mojang.authlib.GameProfile; +import mc.alk.virtualplayers.api.VirtualPlayer; +import mc.alk.virtualplayers.api.VirtualPlayerFactory; +import mc.alk.virtualplayers.util.Util; +import net.minecraft.server.v1_15_R1.DimensionManager; +import net.minecraft.server.v1_15_R1.MinecraftServer; +import net.minecraft.server.v1_15_R1.PlayerInteractManager; +import net.minecraft.server.v1_15_R1.WorldServer; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.World; +import org.bukkit.craftbukkit.v1_15_R1.CraftServer; +import org.bukkit.craftbukkit.v1_15_R1.CraftWorld; + +import java.util.List; +import java.util.UUID; + +/** + * Make, track, & delete VirtualPlayers. + */ +public class CraftVirtualPlayerFactory extends VirtualPlayerFactory { + + @Override + public VirtualPlayer makeVirtualPlayer(String name) throws Exception { + CraftVirtualPlayer cvp = newCraftVirtualPlayer(name); + vps.put(cvp.getUniqueId(), cvp); + names.put(cvp.getName(), cvp); + return cvp; + } + + private CraftVirtualPlayer newCraftVirtualPlayer(String name) throws Exception { + CraftServer cserver = (CraftServer) Bukkit.getServer(); + List worlds = cserver.getWorlds(); + if (worlds == null || worlds.isEmpty()) { + throw new Exception("There must be at least one world"); + } + CraftWorld w = (CraftWorld) worlds.get(0); + Location location = new Location(w, 0, 0, 0); + MinecraftServer mcserver = cserver.getServer(); + DimensionManager dm = DimensionManager.OVERWORLD; + WorldServer worldServer = mcserver.getWorldServer(dm); + PlayerInteractManager pim = new PlayerInteractManager(worldServer); + if (name == null) { + name = "p" + (vps.size() + 1); + } + GameProfile gameProfile = new GameProfile(UUID.randomUUID(), Util.colorChat(name)); + CraftVirtualPlayer cvp = new CraftVirtualPlayer(cserver, mcserver, worldServer, gameProfile, pim, location); + return cvp; + } + + @Override + public void deleteVirtualPlayer(String name) { + deleteVirtualPlayer(names.get(name)); + } + + @Override + public void deleteVirtualPlayer(VirtualPlayer vp) { + CraftVirtualPlayer cvp = (CraftVirtualPlayer) vp; + WorldServer world = ((CraftWorld) cvp.getLocation().getWorld()).getHandle(); + world.removeEntity(cvp.getHandle()); + cvp.remove(); + vps.remove(cvp.getUniqueId()); + names.remove(cvp.getName()); + } + +} diff --git a/pom.xml b/pom.xml index 711635c..65708cc 100644 --- a/pom.xml +++ b/pom.xml @@ -44,6 +44,7 @@ modules/v1_13_R1 modules/v1_13_R2 modules/v1_14_R1 + modules/v1_15_R1 modules/VirtualPlayers