Skip to content

Commit

Permalink
Merge pull request #56 from ShaneBeee/feature/tag-stuff
Browse files Browse the repository at this point in the history
Feature/tag stuff
  • Loading branch information
ShaneBeee authored Sep 18, 2020
2 parents 424f160 + 842f7fa commit 2b92ae8
Show file tree
Hide file tree
Showing 12 changed files with 637 additions and 77 deletions.
3 changes: 2 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>tk.shanebee</groupId>
<artifactId>SkBee</artifactId>
<version>1.5.2</version>
<version>1.6.0</version>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
Expand Down Expand Up @@ -64,6 +64,7 @@
<dependency>
<groupId>com.github.ShaneBeee</groupId>
<artifactId>Item-NBT-API</artifactId>
<!--suppress MavenModelInspection -->
<version>feature~custom-logger-SNAPSHOT</version>
<exclusions>
<exclusion>
Expand Down
181 changes: 160 additions & 21 deletions src/main/java/tk/shanebee/bee/api/NBTApi.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,7 @@

import ch.njol.skript.aliases.ItemType;
import ch.njol.skript.util.slot.Slot;
import de.tr7zw.changeme.nbtapi.NBTCompound;
import de.tr7zw.changeme.nbtapi.NBTContainer;
import de.tr7zw.changeme.nbtapi.NBTEntity;
import de.tr7zw.changeme.nbtapi.NBTFile;
import de.tr7zw.changeme.nbtapi.NBTItem;
import de.tr7zw.changeme.nbtapi.NBTTileEntity;
import de.tr7zw.changeme.nbtapi.NBTType;
import de.tr7zw.changeme.nbtapi.NbtApiException;
import de.tr7zw.changeme.nbtapi.*;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Entity;
Expand Down Expand Up @@ -39,11 +32,13 @@ public NBTApi() {
CONFIG = SkBee.getPlugin().getPluginConfig();
}

/** Validate an NBT string
/**
* Validate an NBT string
*
* @param nbtString NBT string to validate
* @return True if NBT string is valid, otherwise false
*/
public boolean validateNBT(String nbtString) {
public static boolean validateNBT(String nbtString) {
if (nbtString == null) return false;
try {
new NBTContainer(nbtString);
Expand All @@ -54,7 +49,7 @@ public boolean validateNBT(String nbtString) {
return true;
}

private void sendError(String error, Exception exception) {
private static void sendError(String error, Exception exception) {
Util.skriptError("&cInvalid NBT: &b" + error + "&c");
if (SkBee.getPlugin().getPluginConfig().SETTINGS_DEBUG && exception != null) {
exception.printStackTrace();
Expand Down Expand Up @@ -89,8 +84,8 @@ private File getFile(String fileName) {
* Add NBT to an object.
*
* @param object Object to add NBT to
* @param value NBT string to add to object
* @param type Type of object
* @param value NBT string to add to object
* @param type Type of object
* @return Object with the new NBT value
*/
@Nullable
Expand Down Expand Up @@ -139,7 +134,8 @@ public Object addNBT(@NotNull Object object, @NotNull String value, @NotNull Obj
NBTTileEntity tile = new NBTTileEntity(((Block) object).getState());
try {
tile.mergeCompound(new NBTContainer(value));
} catch (NbtApiException ignore) {}
} catch (NbtApiException ignore) {
}
return object;
default:
if (CONFIG.SETTINGS_DEBUG)
Expand All @@ -152,8 +148,8 @@ public Object addNBT(@NotNull Object object, @NotNull String value, @NotNull Obj
* Set NBT for an object.
*
* @param object Object to set NBT for
* @param value NBT string to set to object
* @param type Type of object
* @param value NBT string to set to object
* @param type Type of object
* @return Object with the new NBT value
*/
@Nullable
Expand Down Expand Up @@ -197,7 +193,7 @@ public Object setNBT(@NotNull Object object, @NotNull String value, @NotNull Obj
* Get NBT from an object
*
* @param object Object to get NBT from
* @param type Type of object
* @param type Type of object
* @return NBT string of object
*/
@Nullable
Expand Down Expand Up @@ -281,6 +277,151 @@ public ItemStack getItemStackFromNBT(String nbt) {
return NBTItem.convertNBTtoItem(container);
}

/**
* Get an {@link ItemType} from an {@link NBTCompound}
*
* @param nbt Full NBT Compound
* @return New ItemType from NBT Compound
*/
public ItemType getItemTypeFromNBT(NBTCompound nbt) {
return new ItemType(getItemStackFromNBT(nbt));
}

/**
* Get an {@link ItemStack} from an {@link NBTCompound}
*
* @param nbt Full NBT Compound
* @return New ItemStack from NBT Compound
*/
public ItemStack getItemStackFromNBT(NBTCompound nbt) {
return NBTItem.convertNBTtoItem(nbt);
}

/**
* Delete a tag from an {@link NBTCompound}
*
* @param tag Tag to delete
* @param nbtCompound Compound to remove tag from
*/
public void deleteTag(@NotNull String tag, @NotNull NBTCompound nbtCompound) {
NBTCompound compound = nbtCompound;
String key = tag;
if (tag.contains(";")) {
String[] splits = tag.split(";");
for (int i = 0; i < splits.length - 1; i++) {
if (compound.hasKey(splits[i])) {
compound = compound.getCompound(splits[i]);
}
}
key = splits[splits.length - 1];
}
compound.removeKey(key);
}

/**
* Set a specific tag of an {@link NBTCompound}
*
* @param tag Tag that will be set
* @param nbtCompound Compound to change
* @param object Value of tag to set to
*/
public void setTag(@NotNull String tag, @NotNull NBTCompound nbtCompound, @NotNull Object[] object) {
NBTCompound compound = nbtCompound;
String key = tag;
if (tag.contains(";")) {
String[] splits = tag.split(";");
for (int i = 0; i < splits.length - 1; i++) {
if (compound.hasKey(splits[i])) {
compound = compound.getCompound(splits[i]);
}
}
key = splits[splits.length - 1];
}

boolean custom = !compound.hasKey(key);
boolean isSingle = object.length == 1;
NBTType type = compound.getType(key);
Object singleObject = object[0];

if ((type == NBTType.NBTTagString && singleObject instanceof String) || (custom && isSingle && singleObject instanceof String)) {
compound.setString(key, ((String) singleObject));

} else if ((type == NBTType.NBTTagByte && singleObject instanceof Number) ||
(custom && isSingle && singleObject instanceof Long && ((Long) singleObject) <= Byte.MAX_VALUE && ((Long) singleObject) >= Byte.MIN_VALUE)) {
compound.setByte(key, ((Number) singleObject).byteValue());

} else if ((type == NBTType.NBTTagShort && singleObject instanceof Number) ||
(custom && isSingle && singleObject instanceof Long && ((Long) singleObject) <= Short.MAX_VALUE && ((Long) singleObject) >= Short.MIN_VALUE)) {
compound.setShort(key, ((Number) singleObject).shortValue());

} else if ((type == NBTType.NBTTagInt && singleObject instanceof Number) ||
(custom && isSingle && singleObject instanceof Long && ((Long) singleObject) <= Integer.MAX_VALUE && ((Long) singleObject) >= Integer.MIN_VALUE)) {
compound.setInteger(key, ((Number) singleObject).intValue());

} else if ((type == NBTType.NBTTagLong && singleObject instanceof Number) || (custom && isSingle && singleObject instanceof Long)) {
compound.setLong(key, ((Number) singleObject).longValue());

} else if ((type == NBTType.NBTTagFloat && singleObject instanceof Number) ||
(custom && isSingle && singleObject instanceof Double && ((Double) singleObject) <= Double.MAX_VALUE && ((Double) singleObject) >= Double.MIN_NORMAL)) {
compound.setFloat(key, ((Number) singleObject).floatValue());

} else if ((type == NBTType.NBTTagDouble && singleObject instanceof Number) || (custom && isSingle && singleObject instanceof Double)) {
compound.setDouble(key, ((Number) singleObject).doubleValue());

} else if ((type == NBTType.NBTTagCompound || (custom && isSingle)) && singleObject instanceof NBTCompound) {
NBTCompound comp;
if (custom) {
comp = compound.addCompound(key);
} else {
comp = compound.getCompound(key);
for (String compKey : comp.getKeys()) {
comp.removeKey(compKey);
}
}
comp.mergeCompound(((NBTCompound) singleObject));

} else if (type == NBTType.NBTTagIntArray || (custom && !isSingle && (singleObject instanceof Long || singleObject instanceof Integer))) {
int[] n = new int[object.length];
for (int i = 0; i < object.length; i++) {
n[i] = ((Number) object[i]).intValue();
}
compound.setIntArray(key, n);

} else if (type == NBTType.NBTTagByteArray) {
byte[] n = new byte[object.length];
for (int i = 0; i < object.length; i++) {
n[i] = (byte) object[i];
}
compound.setByteArray(key, n);

} else if (type == NBTType.NBTTagList || (custom && !isSingle)) {
if (singleObject instanceof Integer) {
NBTList<Integer> list = compound.getIntegerList(key);
list.clear();
for (Object o : object) {
list.add(((Number) o).intValue());
}
} else if (singleObject instanceof Double) {
NBTList<Double> list = compound.getDoubleList(key);
for (Object o : object) {
list.add(((Number) o).doubleValue());
}
} else if (singleObject instanceof NBTCompound) {
NBTCompoundList list = compound.getCompoundList(key);
for (Object o : object) {
list.addCompound(((NBTCompound) o));
}
} else if (singleObject instanceof String) {
NBTList<String> list = compound.getStringList(key);
for (Object o : object) {
list.add((String) o);
}
}
} else {
Util.skriptError("Other-> KEY: " + key + " VALUE: " + singleObject + " VALUE-CLASS: " + object.getClass());
}
}

/**
* Get a specific tag from an NBT string
*
Expand Down Expand Up @@ -320,12 +461,10 @@ public Object getTag(String tag, String nbt) {
case NBTTagByteArray:
return compound.getByteArray(tag);
case NBTTagCompound:
return compound.getCompound(tag).toString();
return compound.getCompound(tag);
case NBTTagList:
List<Object> list = new ArrayList<>();
for (NBTCompound comp : compound.getCompoundList(tag)) {
list.add(comp.toString());
}
list.addAll(compound.getCompoundList(tag));
list.addAll(compound.getDoubleList(tag));
list.addAll(compound.getFloatList(tag));
list.addAll(compound.getIntegerList(tag));
Expand Down
81 changes: 81 additions & 0 deletions src/main/java/tk/shanebee/bee/api/reflection/ChatReflection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package tk.shanebee.bee.api.reflection;

import de.tr7zw.changeme.nbtapi.NBTCompound;
import org.bukkit.Bukkit;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
* Reflection class for chat related stuff
*/
public class ChatReflection {

private static final String VERSION = Bukkit.getServer().getClass().getPackage().getName().replace(".", ",").split(",")[3];

private enum Ver {
V_1_13_R2("v1_13_R2", "k", "a"),
V_1_14_R1("v1_14_R1", "k", "a"),
V_1_15_R1("v1_15_R1", "l", "a"),
V_1_16_R1("v1_16_R1", "l", "a"),
V_1_16_R2("v1_16_R2", "l", "a");

private final String version;
private final String pretty;
private final String prettySplit;

Ver(String version, String pretty, String prettySplit) {
this.version = version;
this.pretty = pretty;
this.prettySplit = prettySplit;
}

private static String getPretty(boolean split) {
for (Ver value : values()) {
if (value.version.equalsIgnoreCase(VERSION)) {
if (split) {
return value.prettySplit;
} else {
return value.pretty;
}
}
}
return null;
}
}

/**
* Get a pretty NBT string
* <p>This is the same as what vanilla Minecraft outputs when using the '/data' command</p>
*
* @param compound Compound to convert to pretty
* @return Pretty string of NBTCompound
*/
public static String getPrettyNBT(NBTCompound compound, String split) {
String prettyM = Ver.getPretty(split != null);
if (prettyM == null) return null;

Object nmsNBT = compound.getCompound();
Class<?> iChatBaseComponent = ReflectionUtils.getNMSClass("IChatBaseComponent");
Class<?> craftChatMessageClass = ReflectionUtils.getOBCClass("util.CraftChatMessage");
try {
Method prettyMethod;
Object prettyComponent;
if (split != null) {
prettyMethod = nmsNBT.getClass().getMethod(prettyM, String.class, int.class);
prettyComponent = prettyMethod.invoke(nmsNBT, split, 0);
} else {
prettyMethod = nmsNBT.getClass().getMethod(prettyM);
prettyComponent = prettyMethod.invoke(nmsNBT);
}
assert craftChatMessageClass != null;
Method fromComponent = craftChatMessageClass.getMethod("fromComponent", iChatBaseComponent);

return ((String) fromComponent.invoke(craftChatMessageClass, prettyComponent));
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) {
e.printStackTrace();
return null;
}
}

}
Loading

0 comments on commit 2b92ae8

Please sign in to comment.