1
0
mirror of https://github.com/Zrips/Jobs.git synced 2024-11-29 05:55:27 +01:00

Dint show empty messages in action bar allow to disable them

This commit is contained in:
Zrips 2016-09-23 15:13:35 +03:00
parent d009b5b782
commit 80c90f1232

View File

@ -1,176 +1,176 @@
package com.gamingmesh.jobs.stuff; package com.gamingmesh.jobs.stuff;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
import java.util.logging.Level; import java.util.logging.Level;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import com.gamingmesh.jobs.Jobs; import com.gamingmesh.jobs.Jobs;
import com.gamingmesh.jobs.economy.BufferedPayment; import com.gamingmesh.jobs.economy.BufferedPayment;
/** /**
* *
* @author hamzaxx * @author hamzaxx
*/ */
public class ActionBar { public class ActionBar {
private int cleanVersion = -1; private int cleanVersion = -1;
private String version = ""; private String version = "";
private Object packet; private Object packet;
private Method getHandle; private Method getHandle;
private Method sendPacket; private Method sendPacket;
private Field playerConnection; private Field playerConnection;
private Class<?> nmsChatSerializer; private Class<?> nmsChatSerializer;
private Class<?> nmsIChatBaseComponent; private Class<?> nmsIChatBaseComponent;
private Class<?> packetType; private Class<?> packetType;
public int getVersion() { public int getVersion() {
if (cleanVersion == -1) if (cleanVersion == -1)
getInfo(); getInfo();
return cleanVersion; return cleanVersion;
} }
private void getInfo() { private void getInfo() {
try { try {
String[] v = Bukkit.getServer().getClass().getPackage().getName().split("\\."); String[] v = Bukkit.getServer().getClass().getPackage().getName().split("\\.");
version = v[v.length - 1]; version = v[v.length - 1];
// Translating version to integer for simpler use // Translating version to integer for simpler use
try { try {
cleanVersion = Integer.parseInt(version.replace("v", "").replace("V", "").replace("_", "").replace("r", "").replace("R", "")); cleanVersion = Integer.parseInt(version.replace("v", "").replace("V", "").replace("_", "").replace("r", "").replace("R", ""));
cleanVersion *= 10; cleanVersion *= 10;
} catch (NumberFormatException e) { } catch (NumberFormatException e) {
// Fail safe if it for some reason can't translate version to integer // Fail safe if it for some reason can't translate version to integer
if (version.contains("v1_4")) if (version.contains("v1_4"))
cleanVersion = 1400; cleanVersion = 1400;
if (version.contains("v1_5")) if (version.contains("v1_5"))
cleanVersion = 1500; cleanVersion = 1500;
if (version.contains("v1_6")) if (version.contains("v1_6"))
cleanVersion = 1600; cleanVersion = 1600;
if (version.contains("v1_7")) if (version.contains("v1_7"))
cleanVersion = 1700; cleanVersion = 1700;
if (version.contains("v1_8_R1")) if (version.contains("v1_8_R1"))
cleanVersion = 1810; cleanVersion = 1810;
if (version.contains("v1_8_R2")) if (version.contains("v1_8_R2"))
cleanVersion = 1820; cleanVersion = 1820;
if (version.contains("v1_8_R3")) if (version.contains("v1_8_R3"))
cleanVersion = 1830; cleanVersion = 1830;
if (version.contains("v1_9_R1")) if (version.contains("v1_9_R1"))
cleanVersion = 1910; cleanVersion = 1910;
if (version.contains("v1_9_R2")) if (version.contains("v1_9_R2"))
cleanVersion = 1920; cleanVersion = 1920;
if (version.contains("v1_10_R1")) if (version.contains("v1_10_R1"))
cleanVersion = 11010; cleanVersion = 11010;
} }
if (cleanVersion < 1400) if (cleanVersion < 1400)
cleanVersion *= 10; cleanVersion *= 10;
packetType = Class.forName(getPacketPlayOutChat()); packetType = Class.forName(getPacketPlayOutChat());
Class<?> typeCraftPlayer = Class.forName(getCraftPlayerClasspath()); Class<?> typeCraftPlayer = Class.forName(getCraftPlayerClasspath());
Class<?> typeNMSPlayer = Class.forName(getNMSPlayerClasspath()); Class<?> typeNMSPlayer = Class.forName(getNMSPlayerClasspath());
Class<?> typePlayerConnection = Class.forName(getPlayerConnectionClasspath()); Class<?> typePlayerConnection = Class.forName(getPlayerConnectionClasspath());
nmsChatSerializer = Class.forName(getChatSerializerClasspath()); nmsChatSerializer = Class.forName(getChatSerializerClasspath());
nmsIChatBaseComponent = Class.forName(getIChatBaseComponentClasspath()); nmsIChatBaseComponent = Class.forName(getIChatBaseComponentClasspath());
getHandle = typeCraftPlayer.getMethod("getHandle"); getHandle = typeCraftPlayer.getMethod("getHandle");
playerConnection = typeNMSPlayer.getField("playerConnection"); playerConnection = typeNMSPlayer.getField("playerConnection");
sendPacket = typePlayerConnection.getMethod("sendPacket", Class.forName(getPacketClasspath())); sendPacket = typePlayerConnection.getMethod("sendPacket", Class.forName(getPacketClasspath()));
} catch (ClassNotFoundException | NoSuchMethodException | SecurityException | NoSuchFieldException ex) { } catch (ClassNotFoundException | NoSuchMethodException | SecurityException | NoSuchFieldException ex) {
Bukkit.getLogger().log(Level.SEVERE, "Error {0}", ex); Bukkit.getLogger().log(Level.SEVERE, "Error {0}", ex);
} }
} }
public void ShowActionBar(BufferedPayment payment) { public void ShowActionBar(BufferedPayment payment) {
if (cleanVersion == -1) if (cleanVersion == -1)
getInfo(); getInfo();
if (cleanVersion == -1) if (cleanVersion == -1)
return; return;
String playername = payment.getOfflinePlayer().getName(); String playername = payment.getOfflinePlayer().getName();
if (!Jobs.getActionbarToggleList().containsKey(playername) && Jobs.getGCManager().ActionBarsMessageByDefault) if (!Jobs.getActionbarToggleList().containsKey(playername) && Jobs.getGCManager().ActionBarsMessageByDefault)
Jobs.getActionbarToggleList().put(playername, true); Jobs.getActionbarToggleList().put(playername, true);
if (playername == null) if (playername == null)
return; return;
if (!Jobs.getActionbarToggleList().containsKey(playername)) if (!Jobs.getActionbarToggleList().containsKey(playername))
return; return;
Boolean show = Jobs.getActionbarToggleList().get(playername); Boolean show = Jobs.getActionbarToggleList().get(playername);
Player abp = Bukkit.getPlayer(payment.getOfflinePlayer().getUniqueId()); Player abp = Bukkit.getPlayer(payment.getOfflinePlayer().getUniqueId());
if (abp != null && show) { if (abp != null && show) {
String Message = Jobs.getLanguage().getMessage("command.toggle.output.paid.main"); String Message = Jobs.getLanguage().getMessage("command.toggle.output.paid.main");
if (payment.getAmount() != 0D) if (payment.getAmount() != 0D)
Message = Message + " " + Jobs.getLanguage().getMessage("command.toggle.output.paid.money", "[amount]", String.format("%.2f", payment.getAmount())); Message = Message + " " + Jobs.getLanguage().getMessage("command.toggle.output.paid.money", "[amount]", String.format("%.2f", payment.getAmount()));
if (payment.getPoints() != 0D) if (payment.getPoints() != 0D)
Message = Message + " " + Jobs.getLanguage().getMessage("command.toggle.output.paid.points", "[points]", String.format("%.2f", payment.getPoints())); Message = Message + " " + Jobs.getLanguage().getMessage("command.toggle.output.paid.points", "[points]", String.format("%.2f", payment.getPoints()));
if (payment.getExp() != 0D) if (payment.getExp() != 0D)
Message = Message + " " + Jobs.getLanguage().getMessage("command.toggle.output.paid.exp", "[exp]", String.format("%.2f", payment.getExp())); Message = Message + " " + Jobs.getLanguage().getMessage("command.toggle.output.paid.exp", "[exp]", String.format("%.2f", payment.getExp()));
send(abp, ChatColor.GREEN + Message); send(abp, ChatColor.GREEN + Message);
} }
} }
public void send(CommandSender receivingPacket, String msg) { public void send(CommandSender receivingPacket, String msg) {
try { try {
if (msg == null || nmsChatSerializer == null) if (msg == null || nmsChatSerializer == null || msg.isEmpty())
return; return;
if (receivingPacket == null) if (receivingPacket == null)
return; return;
if (cleanVersion < 1800 || !(receivingPacket instanceof Player)) { if (cleanVersion < 1800 || !(receivingPacket instanceof Player)) {
receivingPacket.sendMessage(ChatColor.translateAlternateColorCodes('&', msg)); receivingPacket.sendMessage(ChatColor.translateAlternateColorCodes('&', msg));
return; return;
} }
Object serialized = nmsChatSerializer.getMethod("a", String.class).invoke(null, "{\"text\": \"" + ChatColor.translateAlternateColorCodes('&', msg) + "\"}"); Object serialized = nmsChatSerializer.getMethod("a", String.class).invoke(null, "{\"text\": \"" + ChatColor.translateAlternateColorCodes('&', msg) + "\"}");
if (cleanVersion > 1800) { if (cleanVersion > 1800) {
packet = packetType.getConstructor(nmsIChatBaseComponent, byte.class).newInstance(serialized, (byte) 2); packet = packetType.getConstructor(nmsIChatBaseComponent, byte.class).newInstance(serialized, (byte) 2);
} else { } else {
packet = packetType.getConstructor(nmsIChatBaseComponent, int.class).newInstance(serialized, 2); packet = packetType.getConstructor(nmsIChatBaseComponent, int.class).newInstance(serialized, 2);
} }
Object player = getHandle.invoke(receivingPacket); Object player = getHandle.invoke(receivingPacket);
Object connection = playerConnection.get(player); Object connection = playerConnection.get(player);
sendPacket.invoke(connection, packet); sendPacket.invoke(connection, packet);
} catch (SecurityException | IllegalArgumentException | IllegalAccessException | InvocationTargetException | InstantiationException | NoSuchMethodException ex) { } catch (SecurityException | IllegalArgumentException | IllegalAccessException | InvocationTargetException | InstantiationException | NoSuchMethodException ex) {
Bukkit.getLogger().log(Level.SEVERE, "Error {0}", ex); Bukkit.getLogger().log(Level.SEVERE, "Error {0}", ex);
} }
} }
private String getCraftPlayerClasspath() { private String getCraftPlayerClasspath() {
return "org.bukkit.craftbukkit." + version + ".entity.CraftPlayer"; return "org.bukkit.craftbukkit." + version + ".entity.CraftPlayer";
} }
private String getPlayerConnectionClasspath() { private String getPlayerConnectionClasspath() {
return "net.minecraft.server." + version + ".PlayerConnection"; return "net.minecraft.server." + version + ".PlayerConnection";
} }
private String getNMSPlayerClasspath() { private String getNMSPlayerClasspath() {
return "net.minecraft.server." + version + ".EntityPlayer"; return "net.minecraft.server." + version + ".EntityPlayer";
} }
private String getPacketClasspath() { private String getPacketClasspath() {
return "net.minecraft.server." + version + ".Packet"; return "net.minecraft.server." + version + ".Packet";
} }
private String getIChatBaseComponentClasspath() { private String getIChatBaseComponentClasspath() {
return "net.minecraft.server." + version + ".IChatBaseComponent"; return "net.minecraft.server." + version + ".IChatBaseComponent";
} }
private String getChatSerializerClasspath() { private String getChatSerializerClasspath() {
if (cleanVersion < 1820) if (cleanVersion < 1820)
return "net.minecraft.server." + version + ".ChatSerializer"; return "net.minecraft.server." + version + ".ChatSerializer";
return "net.minecraft.server." + version + ".IChatBaseComponent$ChatSerializer";// 1_8_R2 moved to IChatBaseComponent return "net.minecraft.server." + version + ".IChatBaseComponent$ChatSerializer";// 1_8_R2 moved to IChatBaseComponent
} }
private String getPacketPlayOutChat() { private String getPacketPlayOutChat() {
return "net.minecraft.server." + version + ".PacketPlayOutChat"; return "net.minecraft.server." + version + ".PacketPlayOutChat";
} }
} }