Skip to content
This repository has been archived by the owner on Sep 20, 2024. It is now read-only.

Commit

Permalink
+ Method to remove tag inside NBTCompound and NBTList
Browse files Browse the repository at this point in the history
+ Method to find index or remove tag inside NBTList
  • Loading branch information
jglrxavpok committed Mar 6, 2021
1 parent 126f55a commit 56ed95a
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 1 deletion.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ java {
}

group 'org.jglrxavpok.nbt'
version '1.1.7'
version '1.1.8'

repositories {
mavenCentral()
Expand Down
11 changes: 11 additions & 0 deletions src/main/kotlin/org/jglrxavpok/hephaistos/nbt/NBTCompound.kt
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,17 @@ class NBTCompound(): NBT {
*/
fun <T: NBT> getList(key: String): NBTList<T>? = get(key) as? NBTList<T>

/**
* Removes the tag with the given key.
* If no such key exists, no changes are made to this compound.
*
* @return 'this' for chaining
*/
fun removeTag(key: String): NBTCompound {
tags.remove(key)
return this
}

override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
Expand Down
26 changes: 26 additions & 0 deletions src/main/kotlin/org/jglrxavpok/hephaistos/nbt/NBTList.kt
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,32 @@ class NBTList<Tag: NBT>(val subtagType: Int): Iterable<Tag>, NBT {
tags += tag
}

/**
* From ArrayList#removeAt:
* > Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).
*/
fun removeAt(index: Int): NBTList<Tag> {
tags.removeAt(index)
return this
}

/**
* From ArrayList#remove:
* > Removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged. More formally, removes the element with the lowest index i such that Objects.equals(o, get(i)) (if such an element exists). Returns true if this list contained the specified element (or equivalently, if this list changed as a result of the call).
*/
fun remove(tag: Tag): NBTList<Tag> {
tags.remove(tag)
return this
}

/**
* From ArrayList#indexOf:
* > Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. More formally, returns the lowest index i such that Objects.equals(o, get(i)), or -1 if there is no such index.
*/
fun indexOf(tag: Tag): Int {
return tags.indexOf(tag)
}

/**
* Casts this list to another list type. Can throw a ClassCastException, so be careful
*/
Expand Down
9 changes: 9 additions & 0 deletions src/test/java/nbt/NBTCompoundMethods.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,15 @@ public void getAsShort() {
assertEquals(42, nbt.getAsShort("a").shortValue());
}

@Test
public void removeTag() {
nbt.set("a", new NBTString("test value"));
assertEquals(1, nbt.getSize());
nbt.removeTag("a");
assertEquals(0, nbt.getSize());
assertNull(nbt.get("a"));
}

@After
public void clean() {
nbt.clear();
Expand Down
70 changes: 70 additions & 0 deletions src/test/java/nbt/NBTListMethods.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package nbt;

import org.jglrxavpok.hephaistos.nbt.NBT;
import org.jglrxavpok.hephaistos.nbt.NBTList;
import org.jglrxavpok.hephaistos.nbt.NBTString;
import org.jglrxavpok.hephaistos.nbt.NBTTypes;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;

public class NBTListMethods {

private NBTList<NBTString> list;

@Before
public void init() {
list = new NBTList<>(NBTTypes.TAG_String);
}

@Test
public void indexOf() {
list.add(new NBTString("Some value0"));
list.add(new NBTString("Some value1"));
list.add(new NBTString("Some value2"));

for (int i = 0; i < list.getLength(); i++) {
NBTString valueAtI = list.get(i);
assertEquals(i, list.indexOf(valueAtI));
}

// indexOf uses equals
assertEquals(0, list.indexOf(new NBTString("Some value0")));
assertEquals(1, list.indexOf(new NBTString("Some value1")));
assertEquals(2, list.indexOf(new NBTString("Some value2")));

list.removeAt(0);
assertEquals(1, list.indexOf(new NBTString("Some value2")));
}

@Test
public void removeAt() {
list.add(new NBTString("Some value0"));
list.add(new NBTString("Some value1"));
list.add(new NBTString("Some value2"));

list.removeAt(0);
assertEquals(2, list.getLength());
assertEquals("Some value2", list.get(1).getValue());
}

@Test
public void remove() {
list.add(new NBTString("Some value0"));
list.add(new NBTString("Some value1"));
list.add(new NBTString("Some value2"));

list.remove(new NBTString("Some value0"));
assertEquals(2, list.getLength());
assertEquals("Some value2", list.get(1).getValue());
}

@After
public void clean() {
list.clear();
list = null;
}

}

0 comments on commit 56ed95a

Please sign in to comment.