1
0
mirror of https://github.com/Zrips/Jobs.git synced 2024-12-12 04:07:19 +01:00
Jobs/com/gamingmesh/jobs/stuff/ActionBar.java

175 lines
6.2 KiB
Java
Raw Normal View History

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