🚧 Update to 1.17

This commit is contained in:
Maxlego08 2021-06-30 21:19:04 +02:00
parent a16b720786
commit fe4f8a35d5
9 changed files with 579 additions and 175 deletions

View File

@ -8,7 +8,7 @@ import java.util.Map;
import org.bukkit.inventory.ItemStack;
import fr.maxlego08.zkoth.api.enums.MessageType;
import fr.maxlego08.zkoth.zcore.utils.ItemDecoder;
import fr.maxlego08.zkoth.zcore.utils.nms.NMSUtils;
public enum Message {
@ -255,7 +255,7 @@ public enum Message {
}
public MessageType getType() {
return type.equals(MessageType.ACTION) && ItemDecoder.isClaquaxVersion() ? MessageType.TCHAT : type;
return type.equals(MessageType.ACTION) && NMSUtils.isVeryOldVersion() ? MessageType.TCHAT : type;
}
public ItemStack getItemStack() {

View File

@ -0,0 +1,269 @@
package fr.maxlego08.zkoth.zcore.utils;
public final class Base64 {
static private final int BASELENGTH = 128;
static private final int LOOKUPLENGTH = 64;
static private final int TWENTYFOURBITGROUP = 24;
static private final int EIGHTBIT = 8;
static private final int SIXTEENBIT = 16;
static private final int FOURBYTE = 4;
static private final int SIGN = -128;
static private final char PAD = '=';
static private final boolean fDebug = false;
static final private byte [] base64Alphabet = new byte[BASELENGTH];
static final private char [] lookUpBase64Alphabet = new char[LOOKUPLENGTH];
static {
for (int i = 0; i < BASELENGTH; ++i) {
base64Alphabet[i] = -1;
}
for (int i = 'Z'; i >= 'A'; i--) {
base64Alphabet[i] = (byte) (i-'A');
}
for (int i = 'z'; i>= 'a'; i--) {
base64Alphabet[i] = (byte) ( i-'a' + 26);
}
for (int i = '9'; i >= '0'; i--) {
base64Alphabet[i] = (byte) (i-'0' + 52);
}
base64Alphabet['+'] = 62;
base64Alphabet['/'] = 63;
for (int i = 0; i<=25; i++)
lookUpBase64Alphabet[i] = (char)('A'+i);
for (int i = 26, j = 0; i<=51; i++, j++)
lookUpBase64Alphabet[i] = (char)('a'+ j);
for (int i = 52, j = 0; i<=61; i++, j++)
lookUpBase64Alphabet[i] = (char)('0' + j);
lookUpBase64Alphabet[62] = (char)'+';
lookUpBase64Alphabet[63] = (char)'/';
}
protected static boolean isWhiteSpace(char octect) {
return (octect == 0x20 || octect == 0xd || octect == 0xa || octect == 0x9);
}
protected static boolean isPad(char octect) {
return (octect == PAD);
}
protected static boolean isData(char octect) {
return (octect < BASELENGTH && base64Alphabet[octect] != -1);
}
protected static boolean isBase64(char octect) {
return (isWhiteSpace(octect) || isPad(octect) || isData(octect));
}
/**
* Encodes hex octects into Base64
*
* @param binaryData Array containing binaryData
* @return Encoded Base64 array
*/
public static String encode(byte[] binaryData) {
if (binaryData == null)
return null;
int lengthDataBits = binaryData.length*EIGHTBIT;
if (lengthDataBits == 0) {
return "";
}
int fewerThan24bits = lengthDataBits%TWENTYFOURBITGROUP;
int numberTriplets = lengthDataBits/TWENTYFOURBITGROUP;
int numberQuartet = fewerThan24bits != 0 ? numberTriplets+1 : numberTriplets;
char encodedData[] = null;
encodedData = new char[numberQuartet*4];
byte k=0, l=0, b1=0,b2=0,b3=0;
int encodedIndex = 0;
int dataIndex = 0;
if (fDebug) {
System.out.println("number of triplets = " + numberTriplets );
}
for (int i=0; i<numberTriplets; i++) {
b1 = binaryData[dataIndex++];
b2 = binaryData[dataIndex++];
b3 = binaryData[dataIndex++];
if (fDebug) {
System.out.println( "b1= " + b1 +", b2= " + b2 + ", b3= " + b3 );
}
l = (byte)(b2 & 0x0f);
k = (byte)(b1 & 0x03);
byte val1 = ((b1 & SIGN)==0)?(byte)(b1>>2):(byte)((b1)>>2^0xc0);
byte val2 = ((b2 & SIGN)==0)?(byte)(b2>>4):(byte)((b2)>>4^0xf0);
byte val3 = ((b3 & SIGN)==0)?(byte)(b3>>6):(byte)((b3)>>6^0xfc);
if (fDebug) {
System.out.println( "val2 = " + val2 );
System.out.println( "k4 = " + (k<<4));
System.out.println( "vak = " + (val2 | (k<<4)));
}
encodedData[encodedIndex++] = lookUpBase64Alphabet[ val1 ];
encodedData[encodedIndex++] = lookUpBase64Alphabet[ val2 | ( k<<4 )];
encodedData[encodedIndex++] = lookUpBase64Alphabet[ (l <<2 ) | val3 ];
encodedData[encodedIndex++] = lookUpBase64Alphabet[ b3 & 0x3f ];
}
// form integral number of 6-bit groups
if (fewerThan24bits == EIGHTBIT) {
b1 = binaryData[dataIndex];
k = (byte) ( b1 &0x03 );
if (fDebug) {
System.out.println("b1=" + b1);
System.out.println("b1<<2 = " + (b1>>2) );
}
byte val1 = ((b1 & SIGN)==0)?(byte)(b1>>2):(byte)((b1)>>2^0xc0);
encodedData[encodedIndex++] = lookUpBase64Alphabet[ val1 ];
encodedData[encodedIndex++] = lookUpBase64Alphabet[ k<<4 ];
encodedData[encodedIndex++] = PAD;
encodedData[encodedIndex++] = PAD;
} else if (fewerThan24bits == SIXTEENBIT) {
b1 = binaryData[dataIndex];
b2 = binaryData[dataIndex +1 ];
l = ( byte ) ( b2 &0x0f );
k = ( byte ) ( b1 &0x03 );
byte val1 = ((b1 & SIGN)==0)?(byte)(b1>>2):(byte)((b1)>>2^0xc0);
byte val2 = ((b2 & SIGN)==0)?(byte)(b2>>4):(byte)((b2)>>4^0xf0);
encodedData[encodedIndex++] = lookUpBase64Alphabet[ val1 ];
encodedData[encodedIndex++] = lookUpBase64Alphabet[ val2 | ( k<<4 )];
encodedData[encodedIndex++] = lookUpBase64Alphabet[ l<<2 ];
encodedData[encodedIndex++] = PAD;
}
return new String(encodedData);
}
/**
* Decodes Base64 data into octects
*
* @param encoded string containing Base64 data
* @return Array containind decoded data.
*/
public static byte[] decode(String encoded) {
if (encoded == null)
return null;
char[] base64Data = encoded.toCharArray();
// remove white spaces
int len = removeWhiteSpace(base64Data);
if (len%FOURBYTE != 0) {
return null;//should be divisible by four
}
int numberQuadruple = (len/FOURBYTE );
if (numberQuadruple == 0)
return new byte[0];
byte decodedData[] = null;
byte b1=0,b2=0,b3=0,b4=0;
char d1=0,d2=0,d3=0,d4=0;
int i = 0;
int encodedIndex = 0;
int dataIndex = 0;
decodedData = new byte[ (numberQuadruple)*3];
for (; i<numberQuadruple-1; i++) {
if (!isData( (d1 = base64Data[dataIndex++]) )||
!isData( (d2 = base64Data[dataIndex++]) )||
!isData( (d3 = base64Data[dataIndex++]) )||
!isData( (d4 = base64Data[dataIndex++]) ))
return null;//if found "no data" just return null
b1 = base64Alphabet[d1];
b2 = base64Alphabet[d2];
b3 = base64Alphabet[d3];
b4 = base64Alphabet[d4];
decodedData[encodedIndex++] = (byte)( b1 <<2 | b2>>4 ) ;
decodedData[encodedIndex++] = (byte)(((b2 & 0xf)<<4 ) |( (b3>>2) & 0xf) );
decodedData[encodedIndex++] = (byte)( b3<<6 | b4 );
}
if (!isData( (d1 = base64Data[dataIndex++]) ) ||
!isData( (d2 = base64Data[dataIndex++]) )) {
return null;//if found "no data" just return null
}
b1 = base64Alphabet[d1];
b2 = base64Alphabet[d2];
d3 = base64Data[dataIndex++];
d4 = base64Data[dataIndex++];
if (!isData( (d3 ) ) ||
!isData( (d4 ) )) {//Check if they are PAD characters
if (isPad( d3 ) && isPad( d4)) { //Two PAD e.g. 3c[Pad][Pad]
if ((b2 & 0xf) != 0)//last 4 bits should be zero
return null;
byte[] tmp = new byte[ i*3 + 1 ];
System.arraycopy( decodedData, 0, tmp, 0, i*3 );
tmp[encodedIndex] = (byte)( b1 <<2 | b2>>4 ) ;
return tmp;
} else if (!isPad( d3) && isPad(d4)) { //One PAD e.g. 3cQ[Pad]
b3 = base64Alphabet[ d3 ];
if ((b3 & 0x3 ) != 0)//last 2 bits should be zero
return null;
byte[] tmp = new byte[ i*3 + 2 ];
System.arraycopy( decodedData, 0, tmp, 0, i*3 );
tmp[encodedIndex++] = (byte)( b1 <<2 | b2>>4 );
tmp[encodedIndex] = (byte)(((b2 & 0xf)<<4 ) |( (b3>>2) & 0xf) );
return tmp;
} else {
return null;//an error like "3c[Pad]r", "3cdX", "3cXd", "3cXX" where X is non data
}
} else { //No PAD e.g 3cQl
b3 = base64Alphabet[ d3 ];
b4 = base64Alphabet[ d4 ];
decodedData[encodedIndex++] = (byte)( b1 <<2 | b2>>4 ) ;
decodedData[encodedIndex++] = (byte)(((b2 & 0xf)<<4 ) |( (b3>>2) & 0xf) );
decodedData[encodedIndex++] = (byte)( b3<<6 | b4 );
}
return decodedData;
}
/**
* remove WhiteSpace from MIME containing encoded Base64 data.
*
* @param data the byte array of base64 data (with WS)
* @return the new length
*/
protected static int removeWhiteSpace(char[] data) {
if (data == null)
return 0;
// count characters that's not whitespace
int newSize = 0;
int len = data.length;
for (int i = 0; i < len; i++) {
if (!isWhiteSpace(data[i]))
data[newSize++] = data[i];
}
return newSize;
}
}

View File

@ -1,156 +0,0 @@
package fr.maxlego08.zkoth.zcore.utils;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.Bukkit;
import org.bukkit.inventory.ItemStack;
import com.sun.org.apache.xerces.internal.impl.dv.util.Base64;
public class ItemDecoder {
private static volatile Map<ItemStack, String> itemstackSerialized = new HashMap<ItemStack, String>();
public static String serializeItemStack(ItemStack paramItemStack) {
if (paramItemStack == null) {
return "null";
}
if (itemstackSerialized.containsKey(paramItemStack))
return itemstackSerialized.get(paramItemStack);
ByteArrayOutputStream localByteArrayOutputStream = null;
try {
Class<?> localClass = getNMSClass("NBTTagCompound");
Constructor<?> localConstructor = localClass.getConstructor(new Class[0]);
Object localObject1 = localConstructor.newInstance(new Object[0]);
Object localObject2 = getOBClass("inventory.CraftItemStack")
.getMethod("asNMSCopy", new Class[] { ItemStack.class })
.invoke(null, new Object[] { paramItemStack });
getNMSClass("ItemStack").getMethod("save", new Class[] { localClass }).invoke(localObject2,
new Object[] { localObject1 });
localByteArrayOutputStream = new ByteArrayOutputStream();
getNMSClass("NBTCompressedStreamTools").getMethod("a", new Class[] { localClass, OutputStream.class })
.invoke(null, new Object[] { localObject1, localByteArrayOutputStream });
} catch (Exception localException) {
localException.printStackTrace();
}
String string = Base64.encode(localByteArrayOutputStream.toByteArray());
itemstackSerialized.put(paramItemStack, string);
return string;
}
public static ItemStack deserializeItemStack(String paramString) {
if (paramString.equals("null")) {
return null;
}
ByteArrayInputStream localByteArrayInputStream = null;
try {
localByteArrayInputStream = new ByteArrayInputStream(Base64.decode(paramString));
} catch (Exception localBase64DecodingException) {
}
Class<?> localClass1 = getNMSClass("NBTTagCompound");
Class<?> localClass2 = getNMSClass("ItemStack");
Object localObject1 = null;
ItemStack localItemStack = null;
Object localObject2 = null;
try {
localObject1 = getNMSClass("NBTCompressedStreamTools").getMethod("a", new Class[] { InputStream.class })
.invoke(null, new Object[] { localByteArrayInputStream });
if (getNMSVersion() == 1.11D || getNMSVersion() == 1.12D) {
Constructor<?> localConstructor = localClass2.getConstructor(new Class[] { localClass1 });
localObject2 = localConstructor.newInstance(new Object[] { localObject1 });
}
else if (getNMSVersion() == 1.13D || getNMSVersion() == 1.14D || getNMSVersion() == 1.15D
|| getNMSVersion() == 1.16D) {
localObject2 = localClass2.getMethod("a", new Class[] { localClass1 }).invoke(null,
new Object[] { localObject1 });
} else {
localObject2 = localClass2.getMethod("createStack", new Class[] { localClass1 }).invoke(null,
new Object[] { localObject1 });
}
localItemStack = (ItemStack) getOBClass("inventory.CraftItemStack")
.getMethod("asBukkitCopy", new Class[] { localClass2 }).invoke(null, new Object[] { localObject2 });
} catch (Exception localException) {
localException.printStackTrace();
}
if (localItemStack != null && !itemstackSerialized.containsKey(localItemStack))
itemstackSerialized.put(localItemStack, paramString);
return localItemStack;
}
private static Class<?> getNMSClass(String paramString) {
String str1 = Bukkit.getServer().getClass().getPackage().getName();
String str2 = str1.replace(".", ",").split(",")[3];
String str3 = "net.minecraft.server." + str2 + "." + paramString;
Class<?> localClass = null;
try {
localClass = Class.forName(str3);
} catch (ClassNotFoundException localClassNotFoundException) {
localClassNotFoundException.printStackTrace();
System.err.println("Unable to find reflection class " + str3 + "!");
}
return localClass;
}
private static Class<?> getOBClass(String paramString) {
String var1 = Bukkit.getServer().getClass().getPackage().getName();
String var2 = var1.replace(".", ",").split(",")[3];
String var3 = "org.bukkit.craftbukkit." + var2 + "." + paramString;
Class<?> localClass = null;
try {
localClass = Class.forName(var3);
} catch (ClassNotFoundException localClassNotFoundException) {
localClassNotFoundException.printStackTrace();
}
return localClass;
}
public static double version;
public static double getNMSVersion() {
if (version != 0)
return version;
String var1 = Bukkit.getServer().getClass().getPackage().getName();
String[] arrayOfString = var1.replace(".", ",").split(",")[3].split("_");
String var2 = arrayOfString[0].replace("v", "");
String var3 = arrayOfString[1];
return version = Double.parseDouble(var2 + "." + var3);
}
/**
*
* @return true if version is granther than 1.13
*/
public static boolean isNewVersion() {
return !isOldVersion();
}
public static boolean isOneHand() {
return getNMSVersion() == 1.7 || getNMSVersion() == 1.8;
}
public static boolean isClaquaxVersion() {
return getNMSVersion() == 1.7;
}
/**
*
* @return
*/
public static boolean isOldVersion() {
double version = getNMSVersion();
return version == 1.7 || version == 1.8 || version == 1.9 || version == 1.10 || version == 1.13
|| version == 1.12;
}
}

View File

@ -59,6 +59,8 @@ import fr.maxlego08.zkoth.zcore.enums.Message;
import fr.maxlego08.zkoth.zcore.enums.Permission;
import fr.maxlego08.zkoth.zcore.utils.builder.CooldownBuilder;
import fr.maxlego08.zkoth.zcore.utils.builder.TimerBuilder;
import fr.maxlego08.zkoth.zcore.utils.nms.ItemStackUtils;
import fr.maxlego08.zkoth.zcore.utils.nms.NMSUtils;
import fr.maxlego08.zkoth.zcore.utils.players.ActionBar;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.ClickEvent;
@ -77,7 +79,7 @@ public abstract class ZUtils extends MessageUtils {
* @return the encoded item
*/
protected String encode(ItemStack item) {
return ItemDecoder.serializeItemStack(item);
return ItemStackUtils.serializeItemStack(item);
}
/**
@ -85,7 +87,7 @@ public abstract class ZUtils extends MessageUtils {
* @return the decoded item
*/
protected ItemStack decode(String item) {
return ItemDecoder.deserializeItemStack(item);
return ItemStackUtils.deserializeItemStack(item);
}
/**
@ -136,7 +138,7 @@ public abstract class ZUtils extends MessageUtils {
private static transient Material[] byId;
static {
if (!ItemDecoder.isNewVersion()) {
if (!NMSUtils.isNewVersion()) {
byId = new Material[0];
for (Material material : Material.values()) {
if (byId.length > material.getId()) {
@ -954,7 +956,7 @@ public abstract class ZUtils extends MessageUtils {
public ItemStack playerHead(ItemStack itemStack, OfflinePlayer player) {
String name = itemStack.hasItemMeta() && itemStack.getItemMeta().hasDisplayName()
? itemStack.getItemMeta().getDisplayName() : null;
if (ItemDecoder.isNewVersion()) {
if (NMSUtils.isNewVersion()) {
if (itemStack.getType().equals(Material.PLAYER_HEAD) && name != null && name.startsWith("HEAD")) {
SkullMeta meta = (SkullMeta) itemStack.getItemMeta();
name = name.replace("HEAD", "");
@ -988,7 +990,7 @@ public abstract class ZUtils extends MessageUtils {
* @return itemstack
*/
protected ItemStack playerHead() {
return ItemDecoder.isNewVersion() ? new ItemStack(Material.PLAYER_HEAD)
return NMSUtils.isNewVersion() ? new ItemStack(Material.PLAYER_HEAD)
: new ItemStack(getMaterial(397), 1, (byte) 3);
}
@ -1069,7 +1071,7 @@ public abstract class ZUtils extends MessageUtils {
*/
protected boolean isPlayerHead(ItemStack itemStack) {
Material material = itemStack.getType();
if (ItemDecoder.isNewVersion())
if (NMSUtils.isNewVersion())
return material.equals(Material.PLAYER_HEAD);
return (material.equals(getMaterial(397))) && (itemStack.getDurability() == 3);
}
@ -1141,7 +1143,7 @@ public abstract class ZUtils extends MessageUtils {
throws SecurityException, NoSuchFieldException, IllegalArgumentException, IllegalAccessException {
Class<?> clazz = object.getClass();
Field objectField = field.equals("commandMap") ? clazz.getDeclaredField(field)
: field.equals("knownCommands") ? ItemDecoder.isNewVersion()
: field.equals("knownCommands") ? NMSUtils.isNewVersion()
? clazz.getSuperclass().getDeclaredField(field) : clazz.getDeclaredField(field) : null;
objectField.setAccessible(true);
Object result = objectField.get(object);
@ -1216,7 +1218,7 @@ public abstract class ZUtils extends MessageUtils {
protected void broadcastCenterMessage(List<String> messages) {
messages.stream().map(e -> e = getCenteredMessage(e)).forEach(e -> {
for(Player player : Bukkit.getOnlinePlayers()){
for (Player player : Bukkit.getOnlinePlayers()) {
message(player, e);
}
});

View File

@ -14,7 +14,7 @@ import com.google.gson.stream.JsonToken;
import com.google.gson.stream.JsonWriter;
import fr.maxlego08.zkoth.zcore.ZPlugin;
import fr.maxlego08.zkoth.zcore.utils.ItemDecoder;
import fr.maxlego08.zkoth.zcore.utils.nms.ItemStackUtils;
public class ItemStackerAdapter extends TypeAdapter<ItemStack> {
@ -43,13 +43,13 @@ public class ItemStackerAdapter extends TypeAdapter<ItemStack> {
private String getRaw(ItemStack itemStack) {
Map<String, Object> serial = new HashMap<String, Object>();
serial.put(ITEMSTACK, ItemDecoder.serializeItemStack(itemStack));
serial.put(ITEMSTACK, ItemStackUtils.serializeItemStack(itemStack));
return ZPlugin.z().getGson().toJson(itemStack);
}
private ItemStack fromRaw(String raw) {
Map<String, Object> keys = ZPlugin.z().getGson().fromJson(raw, seriType);
return ItemDecoder.deserializeItemStack((String) keys.get(ITEMSTACK));
return ItemStackUtils.deserializeItemStack((String) keys.get(ITEMSTACK));
}
}

View File

@ -18,8 +18,8 @@ import fr.maxlego08.zkoth.exceptions.ItemEnchantException;
import fr.maxlego08.zkoth.exceptions.ItemFlagException;
import fr.maxlego08.zkoth.zcore.logger.Logger;
import fr.maxlego08.zkoth.zcore.logger.Logger.LogType;
import fr.maxlego08.zkoth.zcore.utils.ItemDecoder;
import fr.maxlego08.zkoth.zcore.utils.ZUtils;
import fr.maxlego08.zkoth.zcore.utils.nms.NMSUtils;
@SuppressWarnings("deprecation")
public class ItemStackLoader extends ZUtils implements Loader<ItemStack> {
@ -92,7 +92,7 @@ public class ItemStackLoader extends ZUtils implements Loader<ItemStack> {
boolean isGlowing = configuration.getBoolean(path + "glow");
if (isGlowing && ItemDecoder.getNMSVersion() != 1.7) {
if (isGlowing && NMSUtils.getNMSVersion() != 1.7) {
meta.addEnchant(Enchantment.ARROW_DAMAGE, 1, true);
meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
@ -143,7 +143,7 @@ public class ItemStackLoader extends ZUtils implements Loader<ItemStack> {
List<String> flags = configuration.getStringList(path + "flags");
// Permet de charger les différents flags
if (flags.size() != 0 && ItemDecoder.getNMSVersion() != 1.7) {
if (flags.size() != 0 && NMSUtils.getNMSVersion() != 1.7) {
for (String flagString : flags) {
@ -187,7 +187,7 @@ public class ItemStackLoader extends ZUtils implements Loader<ItemStack> {
configuration.set(path + "name", meta.getDisplayName().replace("&", "§"));
if (meta.hasLore())
configuration.set(path + "lore", colorReverse(meta.getLore()));
if (ItemDecoder.getNMSVersion() != 1.7 && meta.getItemFlags().size() != 0)
if (NMSUtils.getNMSVersion() != 1.7 && meta.getItemFlags().size() != 0)
configuration.set(path + "flags",
meta.getItemFlags().stream().map(flag -> flag.name()).collect(Collectors.toList()));
if (meta.hasEnchants()) {

View File

@ -0,0 +1,184 @@
package fr.maxlego08.zkoth.zcore.utils.nms;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.Map;
import org.bukkit.Bukkit;
import org.bukkit.inventory.ItemStack;
import fr.maxlego08.zkoth.zcore.utils.Base64;
public class ItemStackUtils {
private static final double NMS_VERSION = NMSUtils.getNMSVersion();
private static volatile Map<ItemStack, String> itemstackSerialized = new HashMap<ItemStack, String>();
/**
*
* Change {@link ItemStack} to {@link String}
*
* @param paramItemStack
* @return {@link String}
*/
public static String serializeItemStack(ItemStack paramItemStack) {
if (paramItemStack == null)
return "null";
if (itemstackSerialized.containsKey(paramItemStack))
return itemstackSerialized.get(paramItemStack);
ByteArrayOutputStream localByteArrayOutputStream = null;
try {
Class<?> localClass = EnumReflectionItemStack.NBTTAGCOMPOUND.getClassz();
Constructor<?> localConstructor = localClass.getConstructor(new Class[0]);
Object localObject1 = localConstructor.newInstance(new Object[0]);
Object localObject2 = EnumReflectionItemStack.CRAFTITEMSTACK.getClassz()
.getMethod("asNMSCopy", new Class[] { ItemStack.class })
.invoke(null, new Object[] { paramItemStack });
EnumReflectionItemStack.ITEMSTACK.getClassz().getMethod("save", new Class[] { localClass })
.invoke(localObject2, new Object[] { localObject1 });
localByteArrayOutputStream = new ByteArrayOutputStream();
EnumReflectionItemStack.NBTCOMPRESSEDSTREAMTOOLS.getClassz()
.getMethod("a", new Class[] { localClass, OutputStream.class })
.invoke(null, new Object[] { localObject1, localByteArrayOutputStream });
} catch (Exception localException) {
// localException.printStackTrace();
}
String string = Base64.encode(localByteArrayOutputStream.toByteArray());
itemstackSerialized.put(paramItemStack, string);
return string;
}
/**
*
* Change {@link String} to {@link ItemStack}
*
* @param paramString
* @return {@link ItemStack}
*/
public static ItemStack deserializeItemStack(String paramString) {
if (paramString.equals("null"))
return null;
ByteArrayInputStream localByteArrayInputStream = null;
try {
localByteArrayInputStream = new ByteArrayInputStream(Base64.decode(paramString));
} catch (Exception localBase64DecodingException) {
}
Class<?> localClass1 = EnumReflectionItemStack.NBTTAGCOMPOUND.getClassz();
Class<?> localClass2 = EnumReflectionItemStack.ITEMSTACK.getClassz();
Object localObject1 = null;
ItemStack localItemStack = null;
Object localObject2 = null;
try {
localObject1 = EnumReflectionItemStack.NBTCOMPRESSEDSTREAMTOOLS.getClassz()
.getMethod("a", new Class[] { InputStream.class })
.invoke(null, new Object[] { localByteArrayInputStream });
if (NMS_VERSION == 1.11D || NMS_VERSION == 1.12D) {
Constructor<?> localConstructor = localClass2.getConstructor(new Class[] { localClass1 });
localObject2 = localConstructor.newInstance(new Object[] { localObject1 });
} else if (NMSUtils.isNewVersion()) {
localObject2 = localClass2.getMethod("a", new Class[] { localClass1 }).invoke(null,
new Object[] { localObject1 });
} else {
localObject2 = localClass2.getMethod("createStack", new Class[] { localClass1 }).invoke(null,
new Object[] { localObject1 });
}
localItemStack = (ItemStack) EnumReflectionItemStack.CRAFTITEMSTACK.getClassz()
.getMethod("asBukkitCopy", new Class[] { localClass2 }).invoke(null, new Object[] { localObject2 });
} catch (Exception localException) {
// localException.printStackTrace();
}
if (localItemStack != null && !itemstackSerialized.containsKey(localItemStack))
itemstackSerialized.put(localItemStack, paramString);
return localItemStack;
}
public enum EnumReflectionItemStack {
ITEMSTACK("ItemStack", "net.minecraft.world.item.ItemStack"),
CRAFTITEMSTACK("inventory.CraftItemStack", true),
NBTCOMPRESSEDSTREAMTOOLS("NBTCompressedStreamTools", "net.minecraft.nbt.NBTCompressedStreamTools"),
NBTTAGCOMPOUND("NBTTagCompound", "net.minecraft.nbt.NBTTagCompound"),
;
private final String oldClassName;
private final String newClassName;
private final boolean isBukkit;
/**
*
* @param oldClassName
* @param newClassName
* @param isBukkit
*/
private EnumReflectionItemStack(String oldClassName, String newClassName, boolean isBukkit) {
this.oldClassName = oldClassName;
this.newClassName = newClassName;
this.isBukkit = isBukkit;
}
/**
* @param oldClassName
* @param newClassName
*/
private EnumReflectionItemStack(String oldClassName, String newClassName) {
this(oldClassName, newClassName, false);
}
/**
* @param oldClassName
*/
private EnumReflectionItemStack(String oldClassName) {
this(oldClassName, null, false);
}
/**
*
* @param oldClassName
* @param isBukkit
*/
private EnumReflectionItemStack(String oldClassName, boolean isBukkit) {
this(oldClassName, null, isBukkit);
}
/**
*
* Create class
*
* @return class
*/
public Class<?> getClassz() {
String nmsPackage = Bukkit.getServer().getClass().getPackage().getName();
String nmsVersion = nmsPackage.replace(".", ",").split(",")[3];
String var3 = NMSUtils.isNewNMSVersion()
? this.isBukkit ? "org.bukkit.craftbukkit." + nmsVersion + "." + this.oldClassName
: this.newClassName
: (this.isBukkit ? "org.bukkit.craftbukkit." : "net.minecraft.server.") + nmsVersion + "."
+ this.oldClassName;
Class<?> localClass = null;
try {
localClass = Class.forName(var3);
} catch (ClassNotFoundException localClassNotFoundException) {
localClassNotFoundException.printStackTrace();
}
return localClass;
}
}
}

View File

@ -0,0 +1,105 @@
package fr.maxlego08.zkoth.zcore.utils.nms;
import org.bukkit.Bukkit;
public class NMSUtils {
public static double version = getNMSVersion();
/**
* Get minecraft serveur version
*
* @return version
*/
public static double getNMSVersion() {
if (version != 0)
return version;
String var1 = Bukkit.getServer().getClass().getPackage().getName();
String[] arrayOfString = var1.replace(".", ",").split(",")[3].split("_");
String var2 = arrayOfString[0].replace("v", "");
String var3 = arrayOfString[1];
return version = Double.parseDouble(var2 + "." + var3);
}
/**
* Check if minecraft version has shulker
*
* @return boolean
*/
public static boolean hasShulker() {
return !isOneHand();
}
/**
* Check if minecraft version has barrel
*
* @return booleab
*/
public static boolean hasBarrel() {
final double version = getNMSVersion();
return !(version == 1.7 || version == 1.8 || version == 1.9 || version == 1.10 || version == 1.11
|| version == 1.12 || version == 1.13);
}
/**
* check if version is granther than 1.13
*
* @return boolean
*/
public static boolean isNewVersion() {
return !isOldVersion();
}
/**
* Check if version has one hand
*
* @return boolean
*/
public static boolean isOneHand() {
return getNMSVersion() == 1.7 || getNMSVersion() == 1.8;
}
/**
* Check is version is minecraft 1.7
*
* @return boolean
*/
public static boolean isVeryOldVersion() {
return getNMSVersion() == 1.7;
}
/**
* Check if version has itemmeta unbreakable
*
* @return boolean
*/
public static boolean isUnbreakable() {
return version == 1.7 || version == 1.8 || version == 1.9 || version == 1.10;
}
/**
* Check if version is old version of minecraft with old material system
*
* @return boolean
*/
public static boolean isOldVersion() {
return version == 1.7 || version == 1.8 || version == 1.9 || version == 1.10 || version == 1.12
|| version == 1.11;
}
/**
*
* Check if server vesion is new version
*
* @return boolean
*/
public static boolean isNewNMSVersion() {
switch (String.valueOf(version)) {
case "1.17":
return true;
default:
return false;
}
}
}

View File

@ -8,14 +8,14 @@ import java.util.TimerTask;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import fr.maxlego08.zkoth.zcore.utils.ItemDecoder;
import fr.maxlego08.zkoth.zcore.utils.nms.NMSUtils;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.TextComponent;
public class ActionBar {
private static String nmsver;
private static boolean useOldMethods = false;
private static double nmsVersion = ItemDecoder.getNMSVersion();
private static double nmsVersion = NMSUtils.getNMSVersion();
public static void sendActionBar(Player player, String message) {