Merge branch 'development'

This commit is contained in:
Fernando Pettinelli 2021-12-22 15:30:19 -03:00
commit a29a352ece
5 changed files with 78 additions and 89 deletions

View File

@ -2,7 +2,7 @@
<groupId>com.songoda</groupId>
<artifactId>UltimateKits</artifactId>
<modelVersion>4.0.0</modelVersion>
<version>2.6.17</version>
<version>2.7.0</version>
<build>
<defaultGoal>clean install</defaultGoal>
<finalName>UltimateKits-${project.version}</finalName>
@ -102,14 +102,14 @@
<dependencies>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot</artifactId>
<version>1.17</version>
<artifactId>spigot-api</artifactId>
<version>1.18-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.songoda</groupId>
<artifactId>SongodaCore</artifactId>
<version>LATEST</version>
<version>2.6.9</version>
<scope>compile</scope>
</dependency>
<dependency>

View File

@ -259,7 +259,7 @@ public class UltimateKits extends SongodaPlugin {
if (kit == null) dataFile.set("BlockData." + key, null);
else {
updateHologram(kitManager.addKitToLocation(kit, location, type, holograms, particles, displayItems, itemOverride));
createHologram(kitManager.addKitToLocation(kit, location, type, holograms, particles, displayItems, itemOverride));
}
}
}
@ -340,9 +340,7 @@ public class UltimateKits extends SongodaPlugin {
public void removeHologram(KitBlockData data) {
if (HologramManager.isEnabled()) {
List<String> lines = formatHologram(data);
Location location = getKitLocation(data, lines.size());
HologramManager.removeHologram(location);
HologramManager.removeHologram(data.getHologramId());
}
}
@ -353,28 +351,10 @@ public class UltimateKits extends SongodaPlugin {
}
}
public void updateHologram(KitBlockData data) {
if (data != null && data.isInLoadedChunk() && HologramManager.isEnabled()) {
List<String> lines = formatHologram(data);
if (!lines.isEmpty()) {
Location location = getKitLocation(data, lines.size());
if (!data.showHologram()) {
HologramManager.removeHologram(location);
} else {
HologramManager.updateHologram(location, lines);
}
}
}
}
private void loadHolograms() {
Collection<KitBlockData> kitBlocks = getKitManager().getKitLocations().values();
if (kitBlocks.isEmpty()) return;
for (KitBlockData data : kitBlocks) {
updateHologram(data);
}
public void createHologram(KitBlockData data) {
List<String> lines = formatHologram(data);
Location location = getKitLocation(data, lines.size());
HologramManager.createHologram(data.getHologramId(), location, lines);
}
private Location getKitLocation(KitBlockData data, int lines) {
@ -394,6 +374,31 @@ public class UltimateKits extends SongodaPlugin {
return location;
}
public void updateHologram(KitBlockData data) {
if (data != null && data.isInLoadedChunk() && HologramManager.isEnabled()) {
List<String> lines = formatHologram(data);
if (!lines.isEmpty()) {
if (!data.showHologram()) {
HologramManager.removeHologram(data.getHologramId());
} else if (HologramManager.isHologramLoaded(data.getHologramId())) {
HologramManager.updateHologram(data.getHologramId(), lines);
} else {
createHologram(data);
}
}
}
}
private void loadHolograms() {
Collection<KitBlockData> kitBlocks = getKitManager().getKitLocations().values();
if (kitBlocks.isEmpty()) return;
for (KitBlockData data : kitBlocks) {
createHologram(data);
}
}
private List<String> formatHologram(KitBlockData data) {
getDataManager().updateBlockData(data);
KitType kitType = data.getType();

View File

@ -1,7 +1,6 @@
package com.songoda.ultimatekits.conversion.hooks;
import com.songoda.core.compatibility.ServerVersion;
import com.songoda.ultimatekits.UltimateKits;
import com.songoda.ultimatekits.conversion.Hook;
import com.songoda.ultimatekits.utils.ItemSerializer;
import org.bukkit.inventory.ItemStack;
@ -10,7 +9,6 @@ import java.util.HashSet;
import java.util.Set;
public class DefaultHook implements Hook {
public Set<ItemStack> getItems(String kitName) {
Set<ItemStack> items = new HashSet<>();

View File

@ -4,8 +4,15 @@ import com.songoda.ultimatekits.UltimateKits;
import org.bukkit.Location;
import org.bukkit.World;
import java.util.UUID;
public class KitBlockData {
// This is the unique identifier for this block data.
// It is reset on every plugin load.
// Used for holograms.
private final UUID uniqueId = UUID.randomUUID();
private final Kit kit;
private final Location location;
private boolean hologram, particles, items;
@ -104,5 +111,7 @@ public class KitBlockData {
this.type = type;
}
public String getHologramId() {
return "UltimateKits-" + uniqueId;
}
}

View File

@ -1,22 +1,17 @@
package com.songoda.ultimatekits.utils;
import com.songoda.core.compatibility.ClassMapping;
import com.songoda.core.compatibility.MethodMapping;
import com.songoda.core.compatibility.ServerVersion;
import org.bukkit.Bukkit;
import org.bukkit.inventory.ItemStack;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
public class ItemSerializer {
// classes needed for reflections
private static Class<?> classMojangsonParser;
private static Class<?> classItemStack;
private static Class<?> classCraftItemStack;
private static Class<?> classNBTTagCompound;
private static Class<?> classBukkitItemStack;
private static Constructor<?> constructorItemStack;
@ -28,82 +23,63 @@ public class ItemSerializer {
private static Method methodTobItemStack;
private static Method methodTocItemStack;
private static Method methodSaveTagToStack;
private static Method methodToString;
/**
* Initializes all reflection methods
*
* @throws NoSuchMethodException
* @throws SecurityException
* @throws ClassNotFoundException
*/
static {
static {
try {
classMojangsonParser = Class.forName(ServerVersion.isServerVersionAtLeast(ServerVersion.V1_17)
? "net.minecraft.nbt.MojangsonParser" : formatNMS("net.minecraft.server.NMS.MojangsonParser"));
classItemStack = ClassMapping.ITEM_STACK.getClazz();
classCraftItemStack = Class.forName(formatNMS("org.bukkit.craftbukkit.NMS.inventory.CraftItemStack"));
Class<?> classItemStack = ClassMapping.ITEM_STACK.getClazz();
classNBTTagCompound = ClassMapping.NBT_TAG_COMPOUND.getClazz();
classBukkitItemStack = Class.forName("org.bukkit.inventory.ItemStack");
methodParseString = classMojangsonParser.getMethod("parse", String.class);
if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_13))
methodParseString = MethodMapping.MOJANGSON_PARSER__PARSE.getMethod(ClassMapping.MOJANGSON_PARSER.getClazz());
if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_13)) {
methodToItemStack = classItemStack.getMethod("a", classNBTTagCompound);
else if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_11))
} else if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_11)) {
constructorItemStack = classItemStack.getConstructor(classNBTTagCompound);
else
} else {
methodCreateStack = classItemStack.getMethod("createStack", classNBTTagCompound);
methodTobItemStack = classCraftItemStack.getMethod("asBukkitCopy", classItemStack);
}
methodTobItemStack = ClassMapping.CRAFT_ITEM_STACK.getClazz().getMethod("asBukkitCopy", classItemStack);
methodTocItemStack = classCraftItemStack.getDeclaredMethod("asNMSCopy", classBukkitItemStack);
methodSaveTagToStack = classItemStack.getMethod("save", classNBTTagCompound);
methodToString = classNBTTagCompound.getMethod("toString");
} catch (NoSuchMethodException | ClassNotFoundException e) {
e.getStackTrace();
methodTocItemStack = MethodMapping.CB_ITEM_STACK__AS_NMS_COPY.getMethod(ClassMapping.CRAFT_ITEM_STACK.getClazz());
methodSaveTagToStack = MethodMapping.ITEM_STACK__SAVE.getMethod(ClassMapping.ITEM_STACK.getClazz());
} catch (NoSuchMethodException ex) {
ex.getStackTrace();
}
}
/**
* Inserts the version declaration for any string containing NMS
*
* @param s the string to format, must contain NMS.
* @return formatted string
*/
private static String formatNMS(String s) {
String packageName = Bukkit.getServer().getClass().getPackage().getName();
String nmsVersion = packageName.substring(packageName.lastIndexOf('.') + 1);
return s.replace("NMS", nmsVersion);
}
/**
* Deserializes a JSON String
*
* @param jsonString the JSON String to parse
*
* @return the deserialized ItemStack
*/
public static ItemStack deserializeItemStackFromJson(String jsonString) {
try {
Object nbtTagCompound = methodParseString.invoke(null, jsonString);
Object citemStack;
Object cItemStack;
if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_13))
citemStack = methodToItemStack.invoke(null, nbtTagCompound);
else if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_11))
citemStack = constructorItemStack.newInstance(nbtTagCompound);
else
citemStack = methodCreateStack.invoke(null, nbtTagCompound);
if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_13)) {
cItemStack = methodToItemStack.invoke(null, nbtTagCompound);
} else if (ServerVersion.isServerVersionAtLeast(ServerVersion.V1_11)) {
cItemStack = constructorItemStack.newInstance(nbtTagCompound);
} else {
cItemStack = methodCreateStack.invoke(null, nbtTagCompound);
}
return (ItemStack) methodTobItemStack.invoke(null, citemStack);
return (ItemStack) methodTobItemStack.invoke(null, cItemStack);
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
return null;
}
/**
* Serializes an item stack
*
* @param itemStack the ItemStack to parse
*
* @return condensed JSON String
*/
public static String serializeItemStackToJson(ItemStack itemStack) {
@ -113,10 +89,11 @@ public class ItemSerializer {
methodSaveTagToStack.invoke(citemStack, nbtTagCompoundObject);
return (String) methodToString.invoke(nbtTagCompoundObject);
} catch (Exception e) {
e.printStackTrace();
return null;
return nbtTagCompoundObject.toString();
} catch (Exception ex) {
ex.printStackTrace();
}
return null;
}
}
}