1
0
mirror of https://github.com/Zrips/Jobs.git synced 2024-11-29 14:05:25 +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.DisplaySlot;
import org.bukkit.scoreboard.Objective; import org.bukkit.scoreboard.Objective;
import org.bukkit.scoreboard.Scoreboard; import org.bukkit.scoreboard.Scoreboard;
import org.bukkit.scoreboard.Team;
import com.gamingmesh.jobs.Jobs; import com.gamingmesh.jobs.Jobs;
import com.gamingmesh.jobs.container.ScoreboardInfo;
import com.gamingmesh.jobs.CMILib.VersionChecker.Version; import com.gamingmesh.jobs.CMILib.VersionChecker.Version;
import com.gamingmesh.jobs.container.ScoreboardInfo;
public class CMIScoreboardManager { public class CMIScoreboardManager {
@ -78,28 +76,12 @@ public class CMIScoreboardManager {
Object boards = boardClass.getConstructor().newInstance(); Object boards = boardClass.getConstructor().newInstance();
if (Version.isCurrentEqualOrHigher(Version.v1_13_R1)) { if (Version.isCurrentEqualOrHigher(Version.v1_13_R1)) {
Scoreboard board = Bukkit.getScoreboardManager().getNewScoreboard(); Class<?> p0 = getNMSClass("PacketPlayOutScoreboardObjective");
board.clearSlot(DisplaySlot.SIDEBAR); Constructor<?> p00 = p0.getConstructor();
board.registerNewObjective("Scoreboards", "dummy"); Object pp1 = p00.newInstance();
setField(pp1, "a", player.getName());
player.setScoreboard(board); setField(pp1, "d", 1);
sendPacket(player, pp1);
// 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);*/
} else { } else {
Method m = boards.getClass().getMethod("registerObjective", String.class, getNMSClass("IScoreboardCriteria")); 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 { try {
Class<?> boardClass = getNMSClass("Scoreboard"); Class<?> boardClass = getNMSClass("Scoreboard");
Object boards = boardClass.getConstructor().newInstance(); Object boards = boardClass.getConstructor().newInstance();
if (Version.isCurrentEqualOrHigher(Version.v1_13_R1)) { if (Version.isCurrentEqualOrHigher(Version.v1_13_R1)) {
Scoreboard board = Bukkit.getScoreboardManager().getNewScoreboard(); Class<?> enums = getNMSClass("IScoreboardCriteria$EnumScoreboardHealthDisplay");
Objective sidebar = board.registerNewObjective("sidebar", "dummy");
sidebar.setDisplaySlot(DisplaySlot.SIDEBAR);
player.setScoreboard(board); Class<?> p0 = getNMSClass("PacketPlayOutScoreboardObjective");
Constructor<?> p00 = p0.getConstructor();
for (int i = 1; i <= 15; i++) { Object pp1 = p00.newInstance();
Team team = board.registerNewTeam("SLOT_" + i); setField(pp1, "a", player.getName());
team.addEntry(getSlotColor(i)); setField(pp1, "d", 0);
} Object chatComponentText = getNMSClass("ChatComponentText").getConstructor(String.class).newInstance(ChatColor.translateAlternateColorCodes('&', displayName));
setField(pp1, "b", chatComponentText);
if (displayName.length() > 32) setField(pp1, "c", enums.getEnumConstants()[1]);
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);
sendPacket(player, pp1); sendPacket(player, pp1);
Class<?> p2 = getNMSClass("PacketPlayOutScoreboardObjective"); Object d0 = getNMSClass("PacketPlayOutScoreboardDisplayObjective").getConstructor().newInstance();
Constructor<?> p12 = p2.getConstructor(obj.getClass(), int.class); setField(d0, "a", 1);
Object pp2 = p12.newInstance(obj, 0); setField(d0, "b", player.getName());
sendPacket(player, pp2); sendPacket(player, d0);
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++) { for (int i = 0; i < 15; i++) {
if (i >= lines.size()) if (i >= lines.size())
break; break;
String ln = ChatColor.translateAlternateColorCodes('&', lines.get(i)); 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"); Class<?> PacketPlayOutScoreboardScoreClass = getNMSClass("PacketPlayOutScoreboardScore");
Constructor<?> PacketPlayOutScoreboardScoreConstructor = PacketPlayOutScoreboardScoreClass.getConstructor(getNMSClass("ScoreboardScore")); Constructor<?> PacketPlayOutScoreboardScoreConstructor = PacketPlayOutScoreboardScoreClass.getConstructor();
Object PacketPlayOutScoreboardScore = PacketPlayOutScoreboardScoreConstructor.newInstance(packet2); 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); sendPacket(player, PacketPlayOutScoreboardScore);
}*/ }
} else { } else {
Method m = boards.getClass().getMethod("registerObjective", String.class, getNMSClass("IScoreboardCriteria")); Method m = boards.getClass().getMethod("registerObjective", String.class, getNMSClass("IScoreboardCriteria"));