1
0
mirror of https://github.com/Zrips/Jobs.git synced 2024-12-30 21:07:48 +01:00

ScoreBoard move into CMILib

This commit is contained in:
Zrips 2021-06-20 16:46:14 +03:00
parent 3765c336bd
commit d13ae1b074
7 changed files with 66 additions and 289 deletions

Binary file not shown.

View File

@ -99,7 +99,6 @@ import com.gamingmesh.jobs.listeners.JobsPayment14Listener;
import com.gamingmesh.jobs.listeners.JobsPaymentListener;
import com.gamingmesh.jobs.listeners.PistonProtectionListener;
import com.gamingmesh.jobs.selection.SelectionManager;
import com.gamingmesh.jobs.stuff.CMIScoreboardManager;
import com.gamingmesh.jobs.stuff.Loging;
import com.gamingmesh.jobs.stuff.TabComplete;
import com.gamingmesh.jobs.stuff.ToggleBarHandling;
@ -147,7 +146,6 @@ public class Jobs extends JavaPlugin {
private boolean kyoriSupported = false;
private CMIScoreboardManager cmiScoreboardManager;
private Complement complement;
private GuiManager guiManager;
@ -394,17 +392,6 @@ public class Jobs extends JavaPlugin {
return exploreManager;
}
/**
* Returns scoreboard manager
* @return the scoreboard manager
*/
public CMIScoreboardManager getCMIScoreboardManager() {
if (cmiScoreboardManager == null)
cmiScoreboardManager = new CMIScoreboardManager();
return cmiScoreboardManager;
}
// TODO Get rid of this entirely from project
// There are better implementations than this
protected static Jobs instance;

View File

@ -12,6 +12,7 @@ import com.gamingmesh.jobs.commands.Cmd;
import com.gamingmesh.jobs.container.TopList;
import net.Zrips.CMILib.Container.PageInfo;
import net.Zrips.CMILib.Scoreboards.CMIScoreboard;
public class gtop implements Cmd {
@ -32,7 +33,7 @@ public class gtop implements Cmd {
if (args.length == 1) {
if (args[0].equalsIgnoreCase("clear")) {
player.getScoreboard().clearSlot(DisplaySlot.SIDEBAR);
plugin.getCMIScoreboardManager().removeScoreBoard(player);
CMIScoreboard.removeScoreBoard(player);
return true;
}
@ -84,8 +85,7 @@ public class gtop implements Cmd {
++i;
}
plugin.getCMIScoreboardManager().setScoreBoard(player, Jobs.getLanguage().getMessage("scoreboard.gtopline"), ls);
plugin.getCMIScoreboardManager().addNew(player);
CMIScoreboard.show(player, Jobs.getLanguage().getMessage("scoreboard.gtopline"), ls, Jobs.getGCManager().ToplistInScoreboardInterval);
}
plugin.showPagination(sender, pi, "jobs gtop");

View File

@ -13,6 +13,7 @@ import com.gamingmesh.jobs.container.Job;
import com.gamingmesh.jobs.container.TopList;
import net.Zrips.CMILib.Container.PageInfo;
import net.Zrips.CMILib.Scoreboards.CMIScoreboard;
public class top implements Cmd {
@ -31,7 +32,7 @@ public class top implements Cmd {
Player player = (Player) sender;
if (args[0].equalsIgnoreCase("clear")) {
player.getScoreboard().clearSlot(DisplaySlot.SIDEBAR);
plugin.getCMIScoreboardManager().removeScoreBoard(player);
CMIScoreboard.removeScoreBoard(player);
return true;
}
@ -90,8 +91,7 @@ public class top implements Cmd {
place++;
}
plugin.getCMIScoreboardManager().setScoreBoard(player, Jobs.getLanguage().getMessage("scoreboard.topline", "%jobname%", job.getName()), ls);
plugin.getCMIScoreboardManager().addNew(player);
CMIScoreboard.show(player, Jobs.getLanguage().getMessage("scoreboard.topline", "%jobname%", job.getName()), ls, Jobs.getGCManager().ToplistInScoreboardInterval);
plugin.showPagination(sender, pi, "jobs top " + job.getName());
}

View File

@ -1,48 +0,0 @@
package com.gamingmesh.jobs.container;
import org.bukkit.scoreboard.DisplaySlot;
import org.bukkit.scoreboard.Objective;
import org.bukkit.scoreboard.Scoreboard;
public class ScoreboardInfo {
private Scoreboard scoreBoard;
private Objective obj;
private Long time;
public ScoreboardInfo(Scoreboard scoreBoard, DisplaySlot slot) {
this.scoreBoard = scoreBoard;
for (Objective one : this.scoreBoard.getObjectives()) {
if (one.getDisplaySlot() == slot)
obj = one;
}
time = System.currentTimeMillis();
}
public Scoreboard getScoreBoard() {
return scoreBoard;
}
public void setScoreBoard(Scoreboard scoreBoard) {
this.scoreBoard = scoreBoard;
}
public Long getTime() {
return time;
}
public void setTime(Long time) {
this.time = time;
}
public Objective getObj() {
return obj;
}
public void setObj(Objective obj) {
this.obj = obj;
}
}

View File

@ -1,166 +0,0 @@
package com.gamingmesh.jobs.stuff;
import java.lang.reflect.Field;
import java.util.Iterator;
import java.util.List;
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 com.gamingmesh.jobs.Jobs;
import com.gamingmesh.jobs.container.ScoreboardInfo;
import net.Zrips.CMILib.CMILib;
import net.Zrips.CMILib.Colors.CMIChatColor;
import net.Zrips.CMILib.Version.Version;
public class CMIScoreboardManager {
private java.util.concurrent.ConcurrentMap<UUID, ScoreboardInfo> timerMap = new ConcurrentHashMap<>();
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) {
try {
Objective obj = player.getScoreboard().getObjective(map.getValue().getObj().getName());
if (obj != null)
obj.setDisplaySlot(DisplaySlot.SIDEBAR);
} catch (IllegalStateException e) {
}
}
}
timerMap.remove(map.getKey());
}
}
if (timerMap.size() > 0)
Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(org.bukkit.plugin.java.JavaPlugin.getPlugin(Jobs.class),
this::runScheduler, 20L);
}
public void addNew(Player player) {
timerMap.put(player.getUniqueId(), new ScoreboardInfo(player.getScoreboard(), DisplaySlot.SIDEBAR));
runScheduler();
}
private final String objName = "CMIScoreboard";
public void removeScoreBoard(Player player) {
try {
if (Version.isCurrentEqualOrHigher(Version.v1_13_R1)) {
Object pp1 = getNMSClass("PacketPlayOutScoreboardObjective").getConstructor().newInstance();
setField(pp1, "a", player.getName());
setField(pp1, "d", 1);
sendPacket(player, pp1);
} else {
Object boards = getNMSClass("Scoreboard").getConstructor().newInstance();
Object obj = boards.getClass().getMethod("registerObjective", String.class,
getNMSClass("IScoreboardCriteria")).invoke(boards, objName,
getNMSClass("ScoreboardBaseCriteria").getConstructor(String.class).newInstance("JobsDummy"));
sendPacket(player, getNMSClass("PacketPlayOutScoreboardObjective").getConstructor(obj.getClass(), int.class).newInstance(obj, 1));
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void setScoreBoard(Player player, String displayName, List<String> lines) {
removeScoreBoard(player);
try {
if (Version.isCurrentEqualOrHigher(Version.v1_13_R1)) {
Object pp1 = getNMSClass("PacketPlayOutScoreboardObjective").getConstructor().newInstance();
setField(pp1, "a", player.getName());
setField(pp1, "d", 0);
setField(pp1, "b", getNMSClass("ChatComponentText").getConstructor(String.class).newInstance(CMIChatColor.translate(displayName)));
setField(pp1, "c", getNMSClass("IScoreboardCriteria$EnumScoreboardHealthDisplay").getEnumConstants()[1]);
sendPacket(player, pp1);
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;
Object PacketPlayOutScoreboardScore = getNMSClass("PacketPlayOutScoreboardScore").getConstructor().newInstance();
setField(PacketPlayOutScoreboardScore, "a", CMIChatColor.translate(lines.get(i)));
setField(PacketPlayOutScoreboardScore, "b", player.getName());
setField(PacketPlayOutScoreboardScore, "c", 15 - i);
setField(PacketPlayOutScoreboardScore, "d", getNMSClass("ScoreboardServer$Action").getEnumConstants()[0]);
sendPacket(player, PacketPlayOutScoreboardScore);
}
} else {
Object boards = getNMSClass("Scoreboard").getConstructor().newInstance();
Object obj = boards.getClass().getMethod("registerObjective", String.class, getNMSClass("IScoreboardCriteria"))
.invoke(boards, objName, getNMSClass("ScoreboardBaseCriteria").getConstructor(String.class).newInstance("JobsDummy"));
obj.getClass().getMethod("setDisplayName", String.class).invoke(obj, CMIChatColor.translate(displayName));
sendPacket(player, getNMSClass("PacketPlayOutScoreboardObjective").getConstructor(obj.getClass(), int.class).newInstance(obj, 1));
sendPacket(player, getNMSClass("PacketPlayOutScoreboardObjective").getConstructor(obj.getClass(), int.class).newInstance(obj, 0));
sendPacket(player, getNMSClass("PacketPlayOutScoreboardDisplayObjective").getConstructor(int.class,
getNMSClass("ScoreboardObjective")).newInstance(1, obj));
for (int i = 0; i < 15; i++) {
if (i >= lines.size())
break;
Object packet2 = getNMSClass("ScoreboardScore").getConstructor(getNMSClass("Scoreboard"),
getNMSClass("ScoreboardObjective"), String.class).newInstance(boards, obj, CMIChatColor.translate(lines.get(i)));
packet2.getClass().getMethod("setScore", int.class).invoke(packet2, 15 - i);
sendPacket(player, getNMSClass("PacketPlayOutScoreboardScore").getConstructor(getNMSClass("ScoreboardScore")).newInstance(packet2));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
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();
}
}
private static void sendPacket(Player player, Object packet) {
try {
getNMSClass("PlayerConnection").getMethod("sendPacket", getNMSClass("Packet")).invoke(getConnection(player), packet);
} catch (Exception e) {
e.printStackTrace();
}
}
private static Class<?> getNMSClass(String nmsClassString) throws ClassNotFoundException {
return CMILib.getInstance().getReflectionManager().getMinecraftClass(nmsClassString);
}
private static Object getConnection(Player player) throws Exception {
Object nmsPlayer = player.getClass().getMethod("getHandle").invoke(player);
return nmsPlayer.getClass().getField("playerConnection").get(nmsPlayer);
}
}

View File

@ -8,76 +8,80 @@ import com.gamingmesh.jobs.Jobs;
import com.gamingmesh.jobs.container.JobsPlayer;
import com.gamingmesh.jobs.stuff.Util;
import io.papermc.paper.chat.ChatComposer;
import io.papermc.paper.chat.ChatRenderer;
import io.papermc.paper.event.player.AsyncChatEvent;
import net.kyori.adventure.text.TextReplacementConfig;
public final class KyoriChatEvent extends Complement2 implements Listener {
private Jobs plugin;
private Jobs plugin;
public KyoriChatEvent(Jobs plugin) {
this.plugin = plugin;
public KyoriChatEvent(Jobs plugin) {
this.plugin = plugin;
}
@EventHandler(priority = EventPriority.NORMAL)
public void asyncChatEvent(final AsyncChatEvent event) {
if (event.isCancelled() || Util.getJobsEditorMap().isEmpty())
return;
final String msg = Util.getJobsEditorMap().remove(event.getPlayer().getUniqueId());
if (msg != null) {
plugin.getServer().getScheduler().runTask(plugin,
() -> event.getPlayer().performCommand(msg + serialize(event.message())));
event.setCancelled(true);
}
}
@EventHandler(priority = EventPriority.NORMAL)
public void asyncChatEvent(final AsyncChatEvent event) {
if (event.isCancelled() || Util.getJobsEditorMap().isEmpty())
return;
// Adding to chat prefix job name
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onPlayerChat(AsyncChatEvent event) {
if (!Jobs.getGCManager().getModifyChat())
return;
final String msg = Util.getJobsEditorMap().remove(event.getPlayer().getUniqueId());
if (msg != null) {
plugin.getServer().getScheduler().runTask(plugin,
() -> event.getPlayer().performCommand(msg + serialize(event.message())));
event.setCancelled(true);
}
}
JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayer(event.getPlayer());
String honorific = jPlayer != null ? jPlayer.getDisplayHonorific() : "";
if (honorific.equals(" "))
honorific = "";
// Adding to chat prefix job name
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onPlayerChat(AsyncChatEvent event) {
if (!Jobs.getGCManager().getModifyChat())
return;
// TODO displayName returns the player display name not the chat component from
// chat plugins, like Essentials
// Now there is a parameter "player", so literally we need to add 800+ chat plugins
// to this plugin as dependency?
// 3rd attempt: now we tried to use text replacement config builder to match the variable
// result: instead of replacing the variable, now the chat message never been sent
//event.composer((player, displayName, msg) -> msg
//.replaceText(TextReplacementConfig.builder().match("{jobs}").once().replacement(h).build()));
JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayer(event.getPlayer());
String honorific = jPlayer != null ? jPlayer.getDisplayHonorific() : "";
if (honorific.equals(" "))
honorific = "";
event.message(ChatRenderer.defaultRenderer().render(event.getPlayer(), event.getPlayer().displayName(), event.message(), event.getPlayer()).replaceText(TextReplacementConfig.builder().match(
"\\{jobs\\}").replacement(honorific).build()));
// 4th attempt: composeChat -> doing nothing
// event.message(ChatComposer.DEFAULT.composeChat(event.getPlayer(), event.getPlayer().displayName(), event.message())
// .replaceText(TextReplacementConfig.builder().match("\\{jobs\\}").replacement(honorific).build()));
}
// TODO displayName returns the player display name not the chat component from
// chat plugins, like Essentials
// Now there is a parameter "player", so literally we need to add 800+ chat plugins
// to this plugin as dependency?
// 3rd attempt: now we tried to use text replacement config builder to match the variable
// result: instead of replacing the variable, now the chat message never been sent
//event.composer((player, displayName, msg) -> msg
//.replaceText(TextReplacementConfig.builder().match("{jobs}").once().replacement(h).build()));
// Changing chat prefix variable to job name
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onPlayerChatLow(AsyncChatEvent event) {
onPlayerChatHigh(event);
}
// 4th attempt: composeChat -> doing nothing
event.message(ChatComposer.DEFAULT
.composeChat(event.getPlayer(), event.getPlayer().displayName(), event.message())
.replaceText(TextReplacementConfig.builder().match("\\{jobs\\}").replacement(honorific).build()));
}
// Changing chat prefix variable to job name
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onPlayerChatHigh(AsyncChatEvent event) {
if (Jobs.getGCManager().getModifyChat())
return;
// Changing chat prefix variable to job name
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public void onPlayerChatLow(AsyncChatEvent event) {
onPlayerChatHigh(event);
}
JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayer(event.getPlayer());
String honorific = jPlayer != null ? jPlayer.getDisplayHonorific() : "";
if (honorific.equals(" "))
honorific = "";
// Changing chat prefix variable to job name
@EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true)
public void onPlayerChatHigh(AsyncChatEvent event) {
if (Jobs.getGCManager().getModifyChat())
return;
event.message(ChatRenderer.defaultRenderer().render(event.getPlayer(), event.getPlayer().displayName(), event.message(), event.getPlayer()).replaceText(TextReplacementConfig.builder().match(
"\\{jobs\\}").replacement(honorific).build()));
JobsPlayer jPlayer = Jobs.getPlayerManager().getJobsPlayer(event.getPlayer());
String honorific = jPlayer != null ? jPlayer.getDisplayHonorific() : "";
if (honorific.equals(" "))
honorific = "";
event.message(ChatComposer.DEFAULT
.composeChat(event.getPlayer(), event.getPlayer().displayName(), event.message())
.replaceText(TextReplacementConfig.builder().match("\\{jobs\\}").replacement(honorific).build()));
}
// event.message(ChatComposer.DEFAULT
// .composeChat(event.getPlayer(), event.getPlayer().displayName(), event.message())
// .replaceText(TextReplacementConfig.builder().match("\\{jobs\\}").replacement(honorific).build()));
}
}