1
0
mirror of https://github.com/Zrips/Jobs.git synced 2024-12-01 15:03:36 +01:00

So lets do it in packets, everyone loves packets.

This commit is contained in:
Zrips 2019-01-01 18:12:30 +02:00
parent 6709abca2f
commit 0aeb4e383b

View File

@ -16,12 +16,10 @@ import org.bukkit.entity.Player;
import org.bukkit.scoreboard.DisplaySlot;
import org.bukkit.scoreboard.Objective;
import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.Team;
import com.gamingmesh.jobs.Jobs;
import com.gamingmesh.jobs.container.ScoreboardInfo;
import com.gamingmesh.jobs.CMILib.VersionChecker.Version;
import com.gamingmesh.jobs.container.ScoreboardInfo;
public class CMIScoreboardManager {
@ -78,28 +76,12 @@ public class CMIScoreboardManager {
Object boards = boardClass.getConstructor().newInstance();
if (Version.isCurrentEqualOrHigher(Version.v1_13_R1)) {
Scoreboard board = Bukkit.getScoreboardManager().getNewScoreboard();
board.clearSlot(DisplaySlot.SIDEBAR);
board.registerNewObjective("Scoreboards", "dummy");
player.setScoreboard(board);
// TODO We using soon the scoreboard packets
/** Class<?> enums = getNMSClass("IScoreboardCriteria$EnumScoreboardHealthDisplay");
Class<?> chatbase = getNMSClass("IChatBaseComponent$ChatSerializer");
Method mm = chatbase.getMethod("a", String.class);
Object serealized = mm.invoke(chatbase, "{\"text\": \"" + ChatColor.translateAlternateColorCodes('&', "JobsDummy") + "\"}");
Object e = enums.getEnumConstants()[0];
Class<?> IScoreboardCriterias = getNMSClass("ScoreboardBaseCriteria");
Constructor<?> IScoreboardCriteriasConst = IScoreboardCriterias.getConstructor(String.class);
Object IScoreboardCriteria = IScoreboardCriteriasConst.newInstance("JobsDummy");
Method m = boards.getClass().getMethod("registerNewObjective", String.class, getNMSClass("IScoreboardCriteria"), chatbase, enums);
Object obj = m.invoke(boards, objName, "JobsDummy", IScoreboardCriteria, serealized, e);
Class<?> p1 = getNMSClass("PacketPlayOutScoreboardObjective");
Constructor<?> p11 = p1.getConstructor(obj.getClass(), int.class);
Object pp1 = p11.newInstance(obj, 1);
sendPacket(player, pp1);*/
Class<?> p0 = getNMSClass("PacketPlayOutScoreboardObjective");
Constructor<?> p00 = p0.getConstructor();
Object pp1 = p00.newInstance();
setField(pp1, "a", player.getName());
setField(pp1, "d", 1);
sendPacket(player, pp1);
} else {
Method m = boards.getClass().getMethod("registerObjective", String.class, getNMSClass("IScoreboardCriteria"));
@ -118,109 +100,54 @@ public class CMIScoreboardManager {
}
}
public void setScoreBoard(Player player, String displayName, List<String> lines) {
private static void setField(Object edit, String fieldName, Object value) {
try {
Field field = edit.getClass().getDeclaredField(fieldName);
field.setAccessible(true);
field.set(edit, value);
} catch (NoSuchFieldException | IllegalAccessException e) {
e.printStackTrace();
}
}
public void setScoreBoard(Player player, String displayName, List<String> lines) {
removeScoreBoard(player);
try {
Class<?> boardClass = getNMSClass("Scoreboard");
Object boards = boardClass.getConstructor().newInstance();
if (Version.isCurrentEqualOrHigher(Version.v1_13_R1)) {
Scoreboard board = Bukkit.getScoreboardManager().getNewScoreboard();
Objective sidebar = board.registerNewObjective("sidebar", "dummy");
sidebar.setDisplaySlot(DisplaySlot.SIDEBAR);
Class<?> enums = getNMSClass("IScoreboardCriteria$EnumScoreboardHealthDisplay");
player.setScoreboard(board);
for (int i = 1; i <= 15; i++) {
Team team = board.registerNewTeam("SLOT_" + i);
team.addEntry(getSlotColor(i));
}
if (displayName.length() > 32)
displayName = displayName.substring(0, 32);
if (!sidebar.getDisplayName().equals(displayName))
sidebar.setDisplayName(displayName);
for (int i = 0; i < 15; i++) {
if (i >= lines.size())
break;
int slot = lines.size();
if (slot < 15) {
for (int x = (slot +1); x <= 15; x++) {
String entry = getSlotColor(x);
if (board.getEntries().contains(entry))
board.resetScores(entry);
}
}
for (String line : lines) {
Team team = board.getTeam("SLOT_" + slot);
String entry = getSlotColor(slot);
if (!board.getEntries().contains(entry))
sidebar.getScore(entry).setScore(slot);
String pre = getMaxLengthofDName(line);
String suf = getMaxLengthofDName(ChatColor.getLastColors(pre) + getSecondMaxLengthofList(line));
if (!team.getPrefix().equals(pre))
team.setPrefix(pre);
if (!team.getSuffix().equals(suf))
team.setSuffix(suf);
slot--;
}
}
// TODO We using soon the scoreboard packets
/** Class<?> enums = getNMSClass("IScoreboardCriteria$EnumScoreboardHealthDisplay");
Class<?> chatbase = getNMSClass("IChatBaseComponent$ChatSerializer");
Method mm2 = chatbase.getMethod("a", String.class);
Object serealized = mm2.invoke(chatbase, "{\"text\": \"" + ChatColor.translateAlternateColorCodes('&', "JobsDummy") + "\"}");
Object e = enums.getEnumConstants()[0];
Class<?> IScoreboardCriterias = getNMSClass("ScoreboardBaseCriteria");
Constructor<?> IScoreboardCriteriasConst = IScoreboardCriterias.getConstructor(String.class);
Object IScoreboardCriteria = IScoreboardCriteriasConst.newInstance("JobsDummy");
Method m = boards.getClass().getMethod("registerNewObjective", String.class, getNMSClass("IScoreboardCriteria"), chatbase, enums);
Object obj = m.invoke(boards, objName, "JobsDummy", IScoreboardCriteria, serealized, e);
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);
Class<?> p0 = getNMSClass("PacketPlayOutScoreboardObjective");
Constructor<?> p00 = p0.getConstructor();
Object pp1 = p00.newInstance();
setField(pp1, "a", player.getName());
setField(pp1, "d", 0);
Object chatComponentText = getNMSClass("ChatComponentText").getConstructor(String.class).newInstance(ChatColor.translateAlternateColorCodes('&', displayName));
setField(pp1, "b", chatComponentText);
setField(pp1, "c", enums.getEnumConstants()[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);
Object d0 = getNMSClass("PacketPlayOutScoreboardDisplayObjective").getConstructor().newInstance();
setField(d0, "a", 1);
setField(d0, "b", player.getName());
sendPacket(player, d0);
for (int i = 0; i < 15; i++) {
if (i >= lines.size())
break;
String ln = ChatColor.translateAlternateColorCodes('&', lines.get(i));
Class<?> ScoreboardScoreClass = getNMSClass("ScoreboardScore");
Constructor<?> packetConstructor2 = ScoreboardScoreClass.getConstructor(getNMSClass("Scoreboard"), getNMSClass("ScoreboardObjective"), String.class);
Object packet2 = packetConstructor2.newInstance(boards, obj, ln);
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);
Constructor<?> PacketPlayOutScoreboardScoreConstructor = PacketPlayOutScoreboardScoreClass.getConstructor();
Object PacketPlayOutScoreboardScore = PacketPlayOutScoreboardScoreConstructor.newInstance();
Class<?> aenums = getNMSClass("ScoreboardServer$Action");
setField(PacketPlayOutScoreboardScore, "a", ln);
setField(PacketPlayOutScoreboardScore, "b", player.getName());
setField(PacketPlayOutScoreboardScore, "c", 15 - i);
setField(PacketPlayOutScoreboardScore, "d", aenums.getEnumConstants()[0]);
sendPacket(player, PacketPlayOutScoreboardScore);
}*/
}
} else {
Method m = boards.getClass().getMethod("registerObjective", String.class, getNMSClass("IScoreboardCriteria"));