1
0
mirror of https://github.com/Zrips/Jobs.git synced 2024-12-31 21:37:57 +01:00

Use reflection to send scoreboard packets directly to player

This commit is contained in:
Zrips 2017-06-27 11:51:48 +03:00
parent 62e4d09179
commit 9a46ada423
13 changed files with 237 additions and 124 deletions

View File

@ -2,34 +2,30 @@
<classpath>
<classpathentry kind="src" output="target/classes" path="src/main/java">
<attributes>
<attribute name="optional" value="true" />
<attribute name="maven.pomderived" value="true" />
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="src" output="target/test-classes"
path="src/test/java">
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
<attributes>
<attribute name="optional" value="true" />
<attribute name="maven.pomderived" value="true" />
<attribute name="optional" value="true"/>
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con"
path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7">
<attributes>
<attribute name="maven.pomderived" value="true" />
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="con"
path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
<attributes>
<attribute name="maven.pomderived" value="true" />
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry excluding="**" kind="src" output="target/classes"
path="src/main/resources">
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
<attributes>
<attribute name="maven.pomderived" value="true" />
<attribute name="maven.pomderived" value="true"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="target/classes" />
<classpathentry kind="output" path="target/classes"/>
</classpath>

View File

@ -1,5 +1,8 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
org.eclipse.jdt.core.compiler.compliance=1.7
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
org.eclipse.jdt.core.compiler.source=1.7

View File

@ -60,7 +60,6 @@ import com.gamingmesh.jobs.config.LanguageManager;
import com.gamingmesh.jobs.config.NameTranslatorManager;
import com.gamingmesh.jobs.config.RestrictedAreaManager;
import com.gamingmesh.jobs.config.RestrictedBlockManager;
import com.gamingmesh.jobs.config.ScboardManager;
import com.gamingmesh.jobs.config.ScheduleManager;
import com.gamingmesh.jobs.config.ShopManager;
import com.gamingmesh.jobs.config.TitleManager;
@ -93,6 +92,7 @@ import com.gamingmesh.jobs.stuff.JobsClassLoader;
import com.gamingmesh.jobs.stuff.Loging;
import com.gamingmesh.jobs.stuff.TabComplete;
import com.gamingmesh.jobs.stuff.VersionChecker;
import com.gamingmesh.jobs.stuff.CMIScoreboardManager;
import com.gamingmesh.jobs.tasks.BufferedPaymentThread;
import com.gamingmesh.jobs.tasks.DatabaseSaveThread;
@ -104,7 +104,7 @@ public class Jobs extends JavaPlugin {
private static Language lManager = null;
private static LanguageManager lmManager = null;
private static SignUtil signManager = null;
private static ScboardManager scboardManager = null;
private CMIScoreboardManager CMIScoreboardManager = null;
private static ScheduleManager scheduleManager = null;
private static NameTranslatorManager NameTranslatorManager = null;
private static GuiManager GUIManager = null;
@ -370,12 +370,15 @@ public class Jobs extends JavaPlugin {
* Returns scoreboard manager
* @return the scoreboard manager
*/
public static ScboardManager getScboard() {
return scboardManager;
public CMIScoreboardManager getCMIScoreboardManager() {
if (CMIScoreboardManager == null)
CMIScoreboardManager = new CMIScoreboardManager(this);
return CMIScoreboardManager;
}
public void setScboard() {
scboardManager = new ScboardManager(this);
protected static Jobs instance;
public static Jobs getInstance() {
return instance;
}
/**
@ -507,6 +510,7 @@ public class Jobs extends JavaPlugin {
* @throws IOException
*/
public void startup() {
instance = this;
try {
reload();
} catch (IOException e1) {
@ -767,7 +771,6 @@ public class Jobs extends JavaPlugin {
setPermissionHandler(new PermissionHandler(this));
setJobsClassloader();
setPlayerManager();
setScboard();
setLanguage();
setGUIManager();
setExplore();

View File

@ -1,13 +1,12 @@
package com.gamingmesh.jobs.commands.list;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.EntityType;
import com.gamingmesh.jobs.Jobs;
import com.gamingmesh.jobs.commands.Cmd;
import com.gamingmesh.jobs.commands.JobCommand;
import net.md_5.bungee.api.ChatColor;
public class entitylist implements Cmd {
@Override

View File

@ -1,5 +1,6 @@
package com.gamingmesh.jobs.commands.list;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.Bukkit;
@ -65,23 +66,17 @@ public class gtop implements Cmd {
}
} else {
Jobs.getScboard().addNew(player);
Scoreboard board = player.getScoreboard();
Objective objective = board.getObjective("JobsTopPlayers");
if (objective == null)
objective = board.registerNewObjective("JobsTopPlayers", "dummy");
objective.setDisplaySlot(DisplaySlot.SIDEBAR);
objective.setDisplayName(Jobs.getLanguage().getMessage("scoreboard.gtopline"));
List<String> ls = new ArrayList<String>();
int i = page * 15 - 15;
int line = 16;
for (TopList One : FullList) {
for (TopList one : FullList) {
i++;
line--;
String playername = One.getPlayerName() != null ? One.getPlayerName() : "Unknown";
Score score = objective.getScore(Jobs.getLanguage().getMessage("scoreboard.line", "%number%", i, "%playername%", playername, "%level%", One.getLevel()));
score.setScore(line);
String playername = one.getPlayerName() != null ? one.getPlayerName() : "Unknown";
ls.add(Jobs.getLanguage().getMessage("scoreboard.line", "%number%", i, "%playername%", playername, "%level%", one.getLevel()));
}
player.setScoreboard(board);
plugin.getCMIScoreboardManager().setScoreBoard(player, Jobs.getLanguage().getMessage("scoreboard.gtopline"), ls);
plugin.getCMIScoreboardManager().addNew(player);
int prev = page < 2 ? 1 : page - 1;
int next = page + 1;

View File

@ -1,5 +1,6 @@
package com.gamingmesh.jobs.commands.list;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.Bukkit;
@ -78,26 +79,38 @@ public class top implements Cmd {
One.getExp()));
}
} else {
Jobs.getScboard().addNew(player);
Scoreboard board = player.getScoreboard();
Objective objective = board.getObjective("JobsTopPlayers");
if (objective == null)
objective = board.registerNewObjective("JobsTopPlayers", "dummy");
objective.setDisplaySlot(DisplaySlot.SIDEBAR);
objective.setDisplayName(Jobs.getLanguage().getMessage("scoreboard.topline", "%jobname%", jobName));
int i = start;
int line = 16;
for (TopList One : FullList) {
List<String> ls = new ArrayList<String>();
int i = (start + 1) * 15 - 15;
for (TopList one : FullList) {
i++;
line--;
String playername = One.getPlayerName() != null ? One.getPlayerName() : "Unknown";
Score score = objective.getScore(Jobs.getLanguage().getMessage("scoreboard.line", "%number%", i, "%playername%", playername, "%level%", One.getLevel()));
score.setScore(line);
String playername = one.getPlayerName() != null ? one.getPlayerName() : "Unknown";
ls.add(Jobs.getLanguage().getMessage("scoreboard.line", "%number%", i, "%playername%", playername, "%level%", one.getLevel()));
}
player.setScoreboard(board);
plugin.getCMIScoreboardManager().setScoreBoard(player, Jobs.getLanguage().getMessage("scoreboard.topline", "%jobname%", jobName), ls);
plugin.getCMIScoreboardManager().addNew(player);
// Scoreboard board = player.getScoreboard();
// Objective objective = board.getObjective("JobsTopPlayers");
// if (objective == null)
// objective = board.registerNewObjective("JobsTopPlayers", "dummy");
// objective.setDisplaySlot(DisplaySlot.SIDEBAR);
// objective.setDisplayName(Jobs.getLanguage().getMessage("scoreboard.topline", "%jobname%", jobName));
// int i = start;
// int line = 16;
// for (TopList One : FullList) {
// i++;
// line--;
// String playername = One.getPlayerName() != null ? One.getPlayerName() : "Unknown";
//
// Score score = objective.getScore(Jobs.getLanguage().getMessage("scoreboard.line", "%number%", i, "%playername%", playername, "%level%", One.getLevel()));
// score.setScore(line);
//
// }
// player.setScoreboard(board);
int from = start;
if (start >= 15)

View File

@ -1,62 +0,0 @@
package com.gamingmesh.jobs.config;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.UUID;
import java.util.concurrent.ConcurrentHashMap;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.scoreboard.DisplaySlot;
import org.bukkit.scoreboard.Objective;
import org.bukkit.scoreboard.Scoreboard;
import com.gamingmesh.jobs.Jobs;
import com.gamingmesh.jobs.container.ScoreboardInfo;
public class ScboardManager {
private ConcurrentHashMap<UUID, ScoreboardInfo> timerMap = new ConcurrentHashMap<UUID, ScoreboardInfo>();
private Jobs plugin;
public ScboardManager(Jobs plugin) {
this.plugin = plugin;
}
private void RunScheduler() {
Iterator<Entry<UUID, ScoreboardInfo>> MeinMapIter = timerMap.entrySet().iterator();
while (MeinMapIter.hasNext()) {
Entry<UUID, ScoreboardInfo> Map = MeinMapIter.next();
if (System.currentTimeMillis() > Map.getValue().getTime() + (Jobs.getGCManager().ToplistInScoreboardInterval * 1000)) {
Player player = Bukkit.getPlayer(Map.getKey());
if (player != null) {
player.getScoreboard().clearSlot(DisplaySlot.SIDEBAR);
if (Map.getValue().getObj() != null) {
Objective obj = player.getScoreboard().getObjective(Map.getValue().getObj().getName());
if (obj != null)
obj.setDisplaySlot(DisplaySlot.SIDEBAR);
}
}
timerMap.remove(Map.getKey());
}
}
if (timerMap.size() > 0)
Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
@Override
public void run() {
RunScheduler();
return;
}
}, 20L);
return;
}
public void addNew(Player player) {
Scoreboard scoreBoard = player.getScoreboard();
timerMap.put(player.getUniqueId(), new ScoreboardInfo(scoreBoard, DisplaySlot.SIDEBAR));
RunScheduler();
}
}

View File

@ -35,5 +35,4 @@ public class v1_11 implements NMS {
public Block getTargetBlock(Player player, int range) {
return player.getTargetBlock((Set<Material>) null, range);
}
}

View File

@ -35,5 +35,4 @@ public class v1_12 implements NMS {
public Block getTargetBlock(Player player, int range) {
return player.getTargetBlock((Set<Material>) null, range);
}
}

View File

@ -3,6 +3,7 @@ package com.gamingmesh.jobs.nmsUtil;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Horse;
@ -56,7 +57,7 @@ public class v1_7 implements NMS {
public ItemStack getItemInMainHand(Player player) {
return player.getInventory().getItemInHand();
}
@SuppressWarnings("deprecation")
@Override
public Block getTargetBlock(Player player, int range) {

View File

@ -58,13 +58,13 @@ public class v1_8 implements NMS {
}
return name;
}
@SuppressWarnings("deprecation")
@Override
public ItemStack getItemInMainHand(Player player) {
return player.getInventory().getItemInHand();
}
@Override
public Block getTargetBlock(Player player, int range) {
return player.getTargetBlock((Set<Material>) null, range);

View File

@ -68,5 +68,4 @@ public class v1_9 implements NMS {
public Block getTargetBlock(Player player, int range) {
return player.getTargetBlock((Set<Material>) null, range);
}
}

View File

@ -0,0 +1,168 @@
package com.gamingmesh.jobs.stuff;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.entity.Player;
import org.bukkit.scoreboard.DisplaySlot;
import org.bukkit.scoreboard.Objective;
import org.bukkit.scoreboard.Scoreboard;
import com.gamingmesh.jobs.Jobs;
import com.gamingmesh.jobs.container.ScoreboardInfo;
public class CMIScoreboardManager {
private ConcurrentHashMap<UUID, ScoreboardInfo> timerMap = new ConcurrentHashMap<UUID, ScoreboardInfo>();
private Jobs plugin;
public CMIScoreboardManager(Jobs plugin) {
this.plugin = plugin;
}
private void RunScheduler() {
Iterator<Entry<UUID, ScoreboardInfo>> MeinMapIter = timerMap.entrySet().iterator();
while (MeinMapIter.hasNext()) {
Entry<UUID, ScoreboardInfo> Map = MeinMapIter.next();
if (System.currentTimeMillis() > Map.getValue().getTime() + (Jobs.getGCManager().ToplistInScoreboardInterval * 1000)) {
Player player = Bukkit.getPlayer(Map.getKey());
if (player != null) {
removeScoreBoard(player);
player.getScoreboard().clearSlot(DisplaySlot.SIDEBAR);
if (Map.getValue().getObj() != null) {
Objective obj = player.getScoreboard().getObjective(Map.getValue().getObj().getName());
if (obj != null)
obj.setDisplaySlot(DisplaySlot.SIDEBAR);
}
}
timerMap.remove(Map.getKey());
}
}
if (timerMap.size() > 0)
Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
@Override
public void run() {
RunScheduler();
return;
}
}, 20L);
return;
}
public void addNew(Player player) {
Scoreboard scoreBoard = player.getScoreboard();
timerMap.put(player.getUniqueId(), new ScoreboardInfo(scoreBoard, DisplaySlot.SIDEBAR));
RunScheduler();
}
private final String objName = "CMIScoreboard";
public void removeScoreBoard(Player player) {
try {
Class<?> boardClass = getNMSClass("Scoreboard");
Object boards = boardClass.getConstructor().newInstance();
Method m = boards.getClass().getMethod("registerObjective", String.class, getNMSClass("IScoreboardCriteria"));
Class<?> IScoreboardCriterias = getNMSClass("ScoreboardBaseCriteria");
Constructor<?> IScoreboardCriteriasConst = IScoreboardCriterias.getConstructor(String.class);
Object IScoreboardCriteria = IScoreboardCriteriasConst.newInstance("dummy");
Object obj = m.invoke(boards, objName, IScoreboardCriteria);
Class<?> p1 = getNMSClass("PacketPlayOutScoreboardObjective");
Constructor<?> p11 = p1.getConstructor(obj.getClass(), int.class);
Object pp1 = p11.newInstance(obj, 1);
sendPacket(player, pp1);
} catch (ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
e.printStackTrace();
}
}
public void setScoreBoard(Player player, String displayName, List<String> lines) {
try {
Class<?> boardClass = getNMSClass("Scoreboard");
Object boards = boardClass.getConstructor().newInstance();
Method m = boards.getClass().getMethod("registerObjective", String.class, getNMSClass("IScoreboardCriteria"));
Class<?> IScoreboardCriterias = getNMSClass("ScoreboardBaseCriteria");
Constructor<?> IScoreboardCriteriasConst = IScoreboardCriterias.getConstructor(String.class);
Object IScoreboardCriteria = IScoreboardCriteriasConst.newInstance("dummy");
Object obj = m.invoke(boards, objName, IScoreboardCriteria);
Method mm = obj.getClass().getMethod("setDisplayName", String.class);
mm.invoke(obj, ChatColor.translateAlternateColorCodes('&', displayName));
Class<?> p1 = getNMSClass("PacketPlayOutScoreboardObjective");
Constructor<?> p11 = p1.getConstructor(obj.getClass(), int.class);
Object pp1 = p11.newInstance(obj, 1);
sendPacket(player, pp1);
Class<?> p2 = getNMSClass("PacketPlayOutScoreboardObjective");
Constructor<?> p12 = p2.getConstructor(obj.getClass(), int.class);
Object pp2 = p12.newInstance(obj, 0);
sendPacket(player, pp2);
Class<?> packetClass = getNMSClass("PacketPlayOutScoreboardDisplayObjective");
Constructor<?> packetConstructor = packetClass.getConstructor(int.class, getNMSClass("ScoreboardObjective"));
Object packet = packetConstructor.newInstance(1, obj);
sendPacket(player, packet);
for (int i = 0; i < 15; i++) {
if (i >= lines.size())
break;
Class<?> ScoreboardScoreClass = getNMSClass("ScoreboardScore");
Constructor<?> packetConstructor2 = ScoreboardScoreClass.getConstructor(getNMSClass("Scoreboard"), getNMSClass("ScoreboardObjective"), String.class);
Object packet2 = packetConstructor2.newInstance(boards, obj, lines.get(i));
Method mc = packet2.getClass().getMethod("setScore", int.class);
mc.invoke(packet2, 15 - i);
Class<?> PacketPlayOutScoreboardScoreClass = getNMSClass("PacketPlayOutScoreboardScore");
Constructor<?> PacketPlayOutScoreboardScoreConstructor = PacketPlayOutScoreboardScoreClass.getConstructor(getNMSClass("ScoreboardScore"));
Object PacketPlayOutScoreboardScore = PacketPlayOutScoreboardScoreConstructor.newInstance(packet2);
sendPacket(player, PacketPlayOutScoreboardScore);
}
} catch (ClassNotFoundException | NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
e.printStackTrace();
}
}
private static void sendPacket(Player player, Object packet) {
Method sendPacket;
try {
sendPacket = getNMSClass("PlayerConnection").getMethod("sendPacket", getNMSClass("Packet"));
sendPacket.invoke(getConnection(player), packet);
} catch (NoSuchMethodException | SecurityException | ClassNotFoundException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchFieldException e) {
e.printStackTrace();
}
}
private static Class<?> getNMSClass(String nmsClassString) throws ClassNotFoundException {
return Class.forName("net.minecraft.server." + Jobs.getVersionCheckManager().getVersion() + "." + nmsClassString);
}
private static Object getConnection(Player player) throws SecurityException, NoSuchMethodException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException, InvocationTargetException {
Method getHandle = player.getClass().getMethod("getHandle");
Object nmsPlayer = getHandle.invoke(player);
Field conField = nmsPlayer.getClass().getField("playerConnection");
Object con = conField.get(nmsPlayer);
return con;
}
}