Skip to content

Commit

Permalink
VirtualPlayers 1.9.0 w/ 1.15 support and code cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
RednedEpic authored and Redned235 committed Dec 13, 2019
1 parent deed3ee commit 53b3aeb
Show file tree
Hide file tree
Showing 13 changed files with 516 additions and 73 deletions.
12 changes: 10 additions & 2 deletions modules/VirtualPlayers/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>mc.alk</groupId>
<artifactId>VirtualPlayers</artifactId>
<version>1.8.3</version>
<version>1.9.0</version>
<packaging>jar</packaging>
<name>VirtualPlayers</name>
<url>http://dev.bukkit.org/bukkit-plugins/virtualplayers2/</url>
Expand Down Expand Up @@ -286,6 +286,14 @@
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>mc.alk</groupId>
<artifactId>vp-v1_15_R1</artifactId>
<version>static_version</version>
<type>jar</type>
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.bstats</groupId>
<artifactId>bstats-bukkit</artifactId>
Expand Down Expand Up @@ -358,7 +366,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand Down Expand Up @@ -48,15 +46,8 @@ public void listVirtualPlayers(CommandSender sender) {
List<VirtualPlayer> players = Vps.getApi().getVirtualPlayersList();
sender.sendMessage("VirtualPlayers count=" + players.size());

Collections.sort(players, new Comparator<VirtualPlayer>() {
@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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public class DamageHandlerFactory {

public static IDamageHandler getNewInstance() {
Version<Server> 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")) {
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

import mc.euro.version.VersionFactory;

import org.bukkit.entity.Player;

/**
* abstract VirtualPlayerFactory: handle the creation, tracking, and deletion of VirtualPlayers.
Expand Down Expand Up @@ -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 {
Expand All @@ -61,15 +60,15 @@ 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);
} catch (Exception e) {
e.printStackTrace();
}
}
return (VirtualPlayer) vp;
return vp;
}

public static Collection<VirtualPlayer> getVirtualPlayers() {
Expand All @@ -86,7 +85,7 @@ public static Map<String, VirtualPlayer> getNames() {

public static List<VirtualPlayer> getNewPlayerList() {
synchronized (vps) {
return new ArrayList<VirtualPlayer>(vps.values());
return new ArrayList<>(vps.values());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -28,77 +29,71 @@ public interface VirtualPlayersAPI {
*
* @return an Array of bukkit.entity.Player + api.VirtualPlayer.
*/
public default Collection<? extends Player> getOnlinePlayers() {
default Collection<? extends Player> getOnlinePlayers() {
List<Player> 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<? extends VirtualPlayer> getVirtualPlayers() {
default Collection<? extends VirtualPlayer> getVirtualPlayers() {
return VirtualPlayerFactory.getVirtualPlayers();
};
}

public default List<VirtualPlayer> getVirtualPlayersList() {
default List<VirtualPlayer> 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<UUID, VirtualPlayer> getVps() {
default Map<UUID, VirtualPlayer> getVps() {
return VirtualPlayerFactory.getVps();
}

public default Map<String, VirtualPlayer> getNames() {
default Map<String, VirtualPlayer> 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);
}
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);
}
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);
}
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);
Expand All @@ -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 {
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<VPMessageListener> listeners = new ArrayList<VPMessageListener>();
static final List<VPMessageListener> listeners = new ArrayList<>();

static public String getLocString(Location l) {
if (l == null) {
Expand Down Expand Up @@ -80,7 +80,7 @@ public static boolean removeListener(VPMessageListener listener) {
* @return the nearby entities
*/
public static Collection<Entity> getNearbyEntities(VirtualPlayer vp, int radius) {
Map<Integer, Entity> emap = new HashMap<Integer, Entity>();
Map<Integer, Entity> emap = new HashMap<>();
int numChunks = (int) Math.floor(radius / 16) + 1;
int blocks = numChunks * 16;
int diameter = blocks * 2;
Expand Down
47 changes: 47 additions & 0 deletions modules/v1_15_R1/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<artifactId>vp-parent-pom</artifactId>
<groupId>mc.alk</groupId>
<version>static_version</version>
<relativePath>../../</relativePath>
</parent>
<groupId>mc.alk</groupId>
<artifactId>vp-v1_15_R1</artifactId>
<version>static_version</version>
<packaging>jar</packaging>
<name>virtualplayers-v1_15_R1</name>
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot</artifactId>
<version>1.15.0-R0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>mc.alk</groupId>
<artifactId>VirtualPlayersAPI</artifactId>
<type>jar</type>
<version>${virtualplayers.api.version}</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<directory>./target</directory>
<sourceDirectory>./src/main/java</sourceDirectory>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Loading

0 comments on commit 53b3aeb

Please sign in to comment.