Skip to content

Commit

Permalink
Merge branch 'dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
dpohvar committed Jan 10, 2022
2 parents 03795d6 + 2c93360 commit eb6eb80
Show file tree
Hide file tree
Showing 32 changed files with 919 additions and 496 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@

<groupId>me.dpohvar.powernbt</groupId>
<artifactId>powernbt</artifactId>
<version>0.9.2</version>
<version>1.0.1</version>
<packaging>jar</packaging>

<description>Powerful NBT editor for CraftBukkit 1.5 and later</description>
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/me/dpohvar/powernbt/PowerNBT.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class PowerNBT extends JavaPlugin {
private final HashMap<String, Caller> callers = new HashMap<String, Caller>();
private Translator translator;
private static final Tokenizer tokenizer = new Tokenizer(
null, null, null, List.of('\"'), null, List.of(' ')
null, null, null, List.of('\"'), null, List.of(' '), "{}[]()"
);
private final String prefix = ChatColor.GOLD.toString() + ChatColor.BOLD + "[" + ChatColor.YELLOW + "PowerNBT" + ChatColor.GOLD + ChatColor.BOLD + "] " + ChatColor.RESET;
private final String errorPrefix = ChatColor.DARK_RED.toString() + ChatColor.BOLD + "[" + ChatColor.RED + "PowerNBT" + ChatColor.DARK_RED + ChatColor.BOLD + "] " + ChatColor.RESET;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/me/dpohvar/powernbt/api/NBTBridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public static NBTBridge getInstance(){

abstract ItemStack asCraftCopyItemStack(ItemStack itemStack);

abstract Object readNBTData(DataInput dataInput) throws IOException;
abstract Object readNBTData(DataInput dataInput, byte type) throws IOException;

abstract void writeNBTData(DataOutput dataInput, Object tag) throws IOException;

Expand Down
9 changes: 5 additions & 4 deletions src/main/java/me/dpohvar/powernbt/api/NBTBridgeSpigot.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ public List<Object> getNbtInnerList(Object nbtTagList) {
public Object getBlockNBTTag(BlockState state) {
if (!cBlockGetSnapshotMethod.getRefClass().isInstance(state)) return null;
Object snapshot = cBlockGetSnapshotMethod.of(state).call();
return nBlockGetNBTMethod.of(snapshot).call();
Object tag = nmCompoundCon.create();
return nBlockGetNBTMethod.of(snapshot).callIfPossible(tag);
}

@Override
Expand Down Expand Up @@ -200,9 +201,7 @@ ItemStack asCraftCopyItemStack(ItemStack itemStack) {
}

@Override
public @Nullable Object readNBTData(@NotNull DataInput dataInput) throws IOException {
byte type = dataInput.readByte();
if (type == 0) return null;
public @Nullable Object readNBTData(@NotNull DataInput dataInput, byte type) throws IOException {
dataInput.skipBytes(dataInput.readUnsignedShort());
var nbtTagType = nmNBTTagGetTypeMethod.call(type);
return nmNBTTagParseTypeMethod.of(nbtTagType).call(dataInput, (int)type, readLimiter);
Expand All @@ -211,7 +210,9 @@ ItemStack asCraftCopyItemStack(ItemStack itemStack) {
@Override
public Object getTagValueByPrimitive(Object javaValue){
if (javaValue == null) return nbtTagEndCon.create();
if (javaValue instanceof Boolean bool) return nbtTagByteCon.create(bool ? (byte) 1 : (byte) 0);
if (javaValue instanceof Byte) return nbtTagByteCon.create(javaValue);
if (javaValue instanceof Character character) return nbtTagByteCon.create((byte) character.charValue());
if (javaValue instanceof Short) return nbtTagShortCon.create(javaValue);
if (javaValue instanceof Integer) return nbtTagIntCon.create(javaValue);
if (javaValue instanceof Long) return nbtTagLongCon.create(javaValue);
Expand Down
37 changes: 35 additions & 2 deletions src/main/java/me/dpohvar/powernbt/api/NBTManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
import org.bukkit.inventory.PlayerInventory;

import java.io.*;
import java.lang.reflect.Array;
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

Expand Down Expand Up @@ -184,6 +187,10 @@ public Object read(InputStream inputStream) throws IOException {
return read((DataInput) new DataInputStream(inputStream));
}

public Object read(InputStream inputStream, byte type) throws IOException {
return read((DataInput) new DataInputStream(inputStream), type);
}

/**
* Convert java object to nbt and write to outputStream.<br>
* Allowed all primitive types, collections and maps.
Expand All @@ -204,7 +211,12 @@ public void write(OutputStream outputStream, Object value) throws IOException {
*/
public Object readCompressed(InputStream inputStream) throws IOException {
var dis = new DataInputStream(new BufferedInputStream(new GZIPInputStream(inputStream)));
return getValueOfTag(nbtBridge.readNBTData(dis));
return getValueOfTag(nbtBridge.readNBTData(dis, dis.readByte()));
}

public Object readCompressed(InputStream inputStream, byte type) throws IOException {
var dis = new DataInputStream(new BufferedInputStream(new GZIPInputStream(inputStream)));
return getValueOfTag(nbtBridge.readNBTData(dis, type));
}

/**
Expand All @@ -231,7 +243,11 @@ public void writeCompressed(OutputStream outputStream, Object value) throws IOEx
* @throws IOException it happens
*/
public Object read(DataInput dataInput) throws IOException {
Object tag = nbtBridge.readNBTData(dataInput);
return read(dataInput, dataInput.readByte());
}

public Object read(DataInput dataInput, byte type) throws IOException {
Object tag = nbtBridge.readNBTData(dataInput, type);
return getValueOfTag(tag);
}

Expand Down Expand Up @@ -741,4 +757,21 @@ public static Object convertToPrimitiveArrayOrNull(Object[] objArray){
return null;
}

public static Object modifyArray(Object array, Consumer<List<Object>> consumer){
if (array == null) return null;
Class<?> baseClass = array.getClass().getComponentType();
if (baseClass == null) return null;
Object[] objectArray = convertToObjectArrayOrNull(array);
List<Object> list = List.of(objectArray);
consumer.accept(list);
if (baseClass.isPrimitive()) {
Object[] copyArray = (Object[]) Array.newInstance(objectArray.getClass().getComponentType(), list.size());
Object[] resultArray = list.toArray(copyArray);
return convertToPrimitiveArrayOrNull(resultArray);
} else {
Object[] copyArray = (Object[]) Array.newInstance(objectArray.getClass().getComponentType(), list.size());
return list.toArray(copyArray);
}
}

}
2 changes: 1 addition & 1 deletion src/main/java/me/dpohvar/powernbt/command/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public boolean onCommand(CommandSender sender, org.bukkit.command.Command comman
Caller caller = plugin.getCaller(sender);
caller.setSilent(silent);
try {
LinkedList<String> words = new LinkedList<String>();
LinkedList<String> words = new LinkedList<>();
for (String s : plugin.getTokenizer().tokenize(StringUtils.join(args, ' ')).values()) {
words.add(s);
}
Expand Down
70 changes: 35 additions & 35 deletions src/main/java/me/dpohvar/powernbt/command/CommandNBT.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public CommandNBT(boolean silent){
@Override
public boolean command(final Caller caller, LinkedList<String> words) throws Throwable {
if (words.size() == 0) return false;
LinkedList<String> argsBefore = new LinkedList<String>();
LinkedList<String> argsAfter = new LinkedList<String>();
LinkedList<String> argsBefore = new LinkedList<>();
LinkedList<String> argsAfter = new LinkedList<>();
String action = null;
for (String t : words) {
if (specialTokens.contains(t)) {
Expand All @@ -65,20 +65,20 @@ public boolean command(final Caller caller, LinkedList<String> words) throws Thr
}
}
RuntimeException exceptionTMArgs = new RuntimeException(plugin.translate("error_toomanyarguments"));
RuntimeException exceptioNEArgs = new RuntimeException(plugin.translate("error_notenougharguments"));
RuntimeException exceptionNEArgs = new RuntimeException(plugin.translate("error_notenougharguments"));
if (action == null) {
if (argsBefore.size() > 3) throw exceptionTMArgs;
Action a = new ActionView(caller, argsBefore.poll(), argsBefore.poll(), argsBefore.poll());
a.execute();
} else if (action.equals("view") || action.equals("?")) {
if (argsBefore.size() > 3) throw exceptionTMArgs;
if (argsBefore.size() < 1) throw exceptioNEArgs;
if (argsBefore.size() < 1) throw exceptionNEArgs;
if (argsAfter.size() > 0) throw exceptionTMArgs;
Action a = new ActionView(caller, argsBefore.poll(), argsBefore.poll(), argsBefore.poll());
a.execute();
} else if (action.equals("paste")) {
if (argsBefore.size() > 2) throw exceptionTMArgs;
if (argsBefore.size() < 1) throw exceptioNEArgs;
if (argsBefore.size() < 1) throw exceptionNEArgs;
if (argsAfter.size() > 1) throw exceptionTMArgs;
Action a = new ActionEdit(caller, argsBefore.poll(), argsBefore.poll(), "buffer", argsAfter.poll());
a.execute();
Expand All @@ -94,126 +94,126 @@ public boolean command(final Caller caller, LinkedList<String> words) throws Thr
a.execute();
} else if (action.equals("=") || action.equals("<")) {
if (argsBefore.size() > 2) throw exceptionTMArgs;
if (argsBefore.size() < 1) throw exceptioNEArgs;
if (argsBefore.size() < 1) throw exceptionNEArgs;
if (argsAfter.size() > 2) throw exceptionTMArgs;
if (argsAfter.size() < 1) throw exceptioNEArgs;
if (argsAfter.size() < 1) throw exceptionNEArgs;
Action a = new ActionEdit(caller, argsBefore.poll(), argsBefore.poll(), argsAfter.poll(), argsAfter.poll());
a.execute();
} else if (action.equals(">")) {
if (argsBefore.size() > 2) throw exceptionTMArgs;
if (argsBefore.size() < 1) throw exceptioNEArgs;
if (argsBefore.size() < 1) throw exceptionNEArgs;
if (argsAfter.size() > 2) throw exceptionTMArgs;
if (argsAfter.size() < 1) throw exceptioNEArgs;
if (argsAfter.size() < 1) throw exceptionNEArgs;
Action a = new ActionEditLast(caller, argsBefore.poll(), argsBefore.poll(), argsAfter.poll(), argsAfter.poll());
a.execute();
} else if (action.equals(">>")) {
if (argsBefore.size() > 2) throw exceptionTMArgs;
if (argsBefore.size() < 1) throw exceptioNEArgs;
if (argsBefore.size() < 1) throw exceptionNEArgs;
if (argsAfter.size() > 2) throw exceptionTMArgs;
if (argsAfter.size() < 1) throw exceptioNEArgs;
if (argsAfter.size() < 1) throw exceptionNEArgs;
Action a = new ActionMove(caller, argsAfter.poll(), argsAfter.poll(), argsBefore.poll(), argsBefore.poll());
a.execute();
} else if (action.equals("<<")) {
if (argsBefore.size() > 2) throw exceptionTMArgs;
if (argsBefore.size() < 1) throw exceptioNEArgs;
if (argsBefore.size() < 1) throw exceptionNEArgs;
if (argsAfter.size() > 2) throw exceptionTMArgs;
if (argsAfter.size() < 1) throw exceptioNEArgs;
if (argsAfter.size() < 1) throw exceptionNEArgs;
Action a = new ActionMoveLast(caller, argsAfter.poll(), argsAfter.poll(), argsBefore.poll(), argsBefore.poll());
a.execute();
} else if (action.equals("cut")) {
if (argsBefore.size() > 2) throw exceptionTMArgs;
if (argsBefore.size() < 1) throw exceptioNEArgs;
if (argsBefore.size() < 1) throw exceptionNEArgs;
if (argsAfter.size() > 0) throw exceptionTMArgs;
Action a = new ActionCut(caller, argsBefore.poll(), argsBefore.poll());
a.execute();
} else if (action.equals("rm") || action.equals("rem") || action.equals("remove")) {
if (argsBefore.size() > 2) throw exceptionTMArgs;
if (argsBefore.size() < 1) throw exceptioNEArgs;
if (argsBefore.size() < 1) throw exceptionNEArgs;
if (argsAfter.size() > 0) throw exceptionTMArgs;
Action a = new ActionRemove(caller, argsBefore.poll(), argsBefore.poll());
a.execute();
} else if (action.equals("ren") || action.equals("rename")) {
if (argsBefore.size() > 2) throw exceptionTMArgs;
if (argsBefore.size() < 2) throw exceptioNEArgs;
if (argsBefore.size() < 2) throw exceptionNEArgs;
if (argsAfter.size() != 1) throw exceptionTMArgs;
Action a = new ActionRename(caller, argsBefore.poll(), argsBefore.poll(), argsAfter.poll());
a.execute();
} else if (action.equals("copy")) {
if (argsBefore.size() > 2) throw exceptionTMArgs;
if (argsBefore.size() < 1) throw exceptioNEArgs;
if (argsBefore.size() < 1) throw exceptionNEArgs;
if (argsAfter.size() > 0) throw exceptionTMArgs;
Action a = new ActionCopy(caller, argsBefore.poll(), argsBefore.poll());
a.execute();
} else if (action.equals("select") || action.equals("set")) {
if (argsBefore.size() > 1) throw exceptionTMArgs;
if (argsBefore.size() < 1) throw exceptioNEArgs;
if (argsBefore.size() < 1) throw exceptionNEArgs;
if (argsAfter.size() > 2) throw exceptionTMArgs;
Action a = new ActionSet(caller, argsBefore.poll(), argsAfter.poll(), argsAfter.poll());
a.execute();
} else if (action.equals("as")) {
if (argsBefore.size() > 2) throw exceptionTMArgs;
if (argsBefore.size() < 1) throw exceptioNEArgs;
if (argsBefore.size() < 1) throw exceptionNEArgs;
if (argsAfter.size() > 1) throw exceptionTMArgs;
Action a = new ActionSet(caller, argsAfter.poll(), argsBefore.poll(), argsBefore.poll());
a.execute();
} else if (action.equals("swap") || action.equals("<>")) {
if (argsBefore.size() > 2) throw exceptionTMArgs;
if (argsBefore.size() < 1) throw exceptioNEArgs;
if (argsBefore.size() < 1) throw exceptionNEArgs;
if (argsAfter.size() > 2) throw exceptionTMArgs;
if (argsAfter.size() < 1) throw exceptioNEArgs;
if (argsAfter.size() < 1) throw exceptionNEArgs;
Action a = new ActionSwap(caller, argsBefore.poll(), argsBefore.poll(), argsAfter.poll(), argsAfter.poll());
a.execute();
} else if (action.equals("add") || action.equals("+=")) {
if (argsBefore.size() > 2) throw exceptionTMArgs;
if (argsBefore.size() < 1) throw exceptioNEArgs;
if (argsBefore.size() < 1) throw exceptionNEArgs;
if (argsAfter.size() > 2) throw exceptionTMArgs;
if (argsAfter.size() < 1) throw exceptioNEArgs;
if (argsAfter.size() < 1) throw exceptionNEArgs;
Action a = new ActionAddAll(caller, argsBefore.poll(), argsBefore.poll(), argsAfter.poll(), argsAfter.poll());
a.execute();
} else if (action.equals("insert") || action.equals("ins")) {
if (argsBefore.size() > 2) throw exceptionTMArgs;
if (argsBefore.size() < 1) throw exceptioNEArgs;
if (argsBefore.size() < 1) throw exceptionNEArgs;
if (argsAfter.size() > 3) throw exceptionTMArgs;
if (argsBefore.size() < 2) throw exceptioNEArgs;
if (argsBefore.size() < 2) throw exceptionNEArgs;
Action a = new ActionInsert(caller, argsBefore.poll(), argsBefore.poll(), argsAfter.poll(), argsAfter.poll(), argsAfter.poll());
a.execute();
} else if (action.equals("&=")) {
if (argsBefore.size() > 2) throw exceptionTMArgs;
if (argsBefore.size() < 1) throw exceptioNEArgs;
if (argsBefore.size() < 1) throw exceptionNEArgs;
if (argsAfter.size() > 2) throw exceptionTMArgs;
if (argsAfter.size() < 1) throw exceptioNEArgs;
if (argsAfter.size() < 1) throw exceptionNEArgs;
Action a = new ActionBitAnd(caller, argsBefore.poll(), argsBefore.poll(), argsAfter.poll(), argsAfter.poll());
a.execute();
} else if (action.equals("|=")) {
if (argsBefore.size() > 2) throw exceptionTMArgs;
if (argsBefore.size() < 1) throw exceptioNEArgs;
if (argsBefore.size() < 1) throw exceptionNEArgs;
if (argsAfter.size() > 2) throw exceptionTMArgs;
if (argsAfter.size() < 1) throw exceptioNEArgs;
if (argsAfter.size() < 1) throw exceptionNEArgs;
Action a = new ActionBitOr(caller, argsBefore.poll(), argsBefore.poll(), argsAfter.poll(), argsAfter.poll());
a.execute();
} else if (action.equals("^=")) {
if (argsBefore.size() > 2) throw exceptionTMArgs;
if (argsBefore.size() < 1) throw exceptioNEArgs;
if (argsBefore.size() < 1) throw exceptionNEArgs;
if (argsAfter.size() > 2) throw exceptionTMArgs;
if (argsAfter.size() < 1) throw exceptioNEArgs;
if (argsAfter.size() < 1) throw exceptionNEArgs;
Action a = new ActionBitXor(caller, argsBefore.poll(), argsBefore.poll(), argsAfter.poll(), argsAfter.poll());
a.execute();
} else if (action.equals("*=")) {
if (argsBefore.size() > 2) throw exceptionTMArgs;
if (argsBefore.size() < 1) throw exceptioNEArgs;
if (argsBefore.size() < 1) throw exceptionNEArgs;
if (argsAfter.size() > 2) throw exceptionTMArgs;
if (argsAfter.size() < 1) throw exceptioNEArgs;
if (argsAfter.size() < 1) throw exceptionNEArgs;
Action a = new ActionMultiply(caller, argsBefore.poll(), argsBefore.poll(), argsAfter.poll(), argsAfter.poll());
a.execute();
} else if (action.equals("~")) {
if (argsBefore.size() > 2) throw exceptionTMArgs;
if (argsBefore.size() < 1) throw exceptioNEArgs;
if (argsBefore.size() < 1) throw exceptionNEArgs;
if (argsAfter.size() > 0) throw exceptionTMArgs;
Action a = new ActionBitInverse(caller, argsBefore.poll(), argsBefore.poll());
a.execute();
} else if (action.equals("spawn")) {
if (argsBefore.size() > 2) throw exceptionTMArgs;
if (argsBefore.size() < 1) throw exceptioNEArgs;
if (argsBefore.size() < 1) throw exceptionNEArgs;
if (argsAfter.size() > 1) throw exceptionTMArgs;
Action a = new ActionSpawn(caller, argsBefore.poll(), argsBefore.poll(), argsAfter.poll());
a.execute();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void execute() throws Exception {
NBTQuery query2 = arg2.getQuery();
Object base2 = container2.getCustomTag(query2);
if (base1 == null) {
base1 = NBTType.fromValue(base2).getDefault();
base1 = NBTType.fromValue(base2).getDefaultValue();
}
if (base1 instanceof NBTCompound cmp1 && base2 instanceof NBTCompound cmp2){
cmp1.merge(cmp2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public void execute() throws Exception {
throw new RuntimeException(plugin.translate("error_null"));
}
if (base1 == null) {
base1 = NBTType.fromValue(base2).getDefault();
base1 = NBTType.fromValue(base2).getDefaultValue();
}
long baseValue = ((Number)base1).longValue();
long argValue = ((Number)base2).longValue();
Expand Down
Loading

0 comments on commit eb6eb80

Please sign in to comment.