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

Commit 56ed95a

Browse files
committed
+ Method to remove tag inside NBTCompound and NBTList
+ Method to find index or remove tag inside NBTList
1 parent 126f55a commit 56ed95a

File tree

5 files changed

+117
-1
lines changed

5 files changed

+117
-1
lines changed

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ java {
1010
}
1111

1212
group 'org.jglrxavpok.nbt'
13-
version '1.1.7'
13+
version '1.1.8'
1414

1515
repositories {
1616
mavenCentral()

src/main/kotlin/org/jglrxavpok/hephaistos/nbt/NBTCompound.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,17 @@ class NBTCompound(): NBT {
248248
*/
249249
fun <T: NBT> getList(key: String): NBTList<T>? = get(key) as? NBTList<T>
250250

251+
/**
252+
* Removes the tag with the given key.
253+
* If no such key exists, no changes are made to this compound.
254+
*
255+
* @return 'this' for chaining
256+
*/
257+
fun removeTag(key: String): NBTCompound {
258+
tags.remove(key)
259+
return this
260+
}
261+
251262
override fun equals(other: Any?): Boolean {
252263
if (this === other) return true
253264
if (javaClass != other?.javaClass) return false

src/main/kotlin/org/jglrxavpok/hephaistos/nbt/NBTList.kt

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,32 @@ class NBTList<Tag: NBT>(val subtagType: Int): Iterable<Tag>, NBT {
7777
tags += tag
7878
}
7979

80+
/**
81+
* From ArrayList#removeAt:
82+
* > Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).
83+
*/
84+
fun removeAt(index: Int): NBTList<Tag> {
85+
tags.removeAt(index)
86+
return this
87+
}
88+
89+
/**
90+
* From ArrayList#remove:
91+
* > 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).
92+
*/
93+
fun remove(tag: Tag): NBTList<Tag> {
94+
tags.remove(tag)
95+
return this
96+
}
97+
98+
/**
99+
* From ArrayList#indexOf:
100+
* > 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.
101+
*/
102+
fun indexOf(tag: Tag): Int {
103+
return tags.indexOf(tag)
104+
}
105+
80106
/**
81107
* Casts this list to another list type. Can throw a ClassCastException, so be careful
82108
*/

src/test/java/nbt/NBTCompoundMethods.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,15 @@ public void getAsShort() {
214214
assertEquals(42, nbt.getAsShort("a").shortValue());
215215
}
216216

217+
@Test
218+
public void removeTag() {
219+
nbt.set("a", new NBTString("test value"));
220+
assertEquals(1, nbt.getSize());
221+
nbt.removeTag("a");
222+
assertEquals(0, nbt.getSize());
223+
assertNull(nbt.get("a"));
224+
}
225+
217226
@After
218227
public void clean() {
219228
nbt.clear();
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package nbt;
2+
3+
import org.jglrxavpok.hephaistos.nbt.NBT;
4+
import org.jglrxavpok.hephaistos.nbt.NBTList;
5+
import org.jglrxavpok.hephaistos.nbt.NBTString;
6+
import org.jglrxavpok.hephaistos.nbt.NBTTypes;
7+
import org.junit.After;
8+
import org.junit.Before;
9+
import org.junit.Test;
10+
11+
import static org.junit.Assert.assertEquals;
12+
13+
public class NBTListMethods {
14+
15+
private NBTList<NBTString> list;
16+
17+
@Before
18+
public void init() {
19+
list = new NBTList<>(NBTTypes.TAG_String);
20+
}
21+
22+
@Test
23+
public void indexOf() {
24+
list.add(new NBTString("Some value0"));
25+
list.add(new NBTString("Some value1"));
26+
list.add(new NBTString("Some value2"));
27+
28+
for (int i = 0; i < list.getLength(); i++) {
29+
NBTString valueAtI = list.get(i);
30+
assertEquals(i, list.indexOf(valueAtI));
31+
}
32+
33+
// indexOf uses equals
34+
assertEquals(0, list.indexOf(new NBTString("Some value0")));
35+
assertEquals(1, list.indexOf(new NBTString("Some value1")));
36+
assertEquals(2, list.indexOf(new NBTString("Some value2")));
37+
38+
list.removeAt(0);
39+
assertEquals(1, list.indexOf(new NBTString("Some value2")));
40+
}
41+
42+
@Test
43+
public void removeAt() {
44+
list.add(new NBTString("Some value0"));
45+
list.add(new NBTString("Some value1"));
46+
list.add(new NBTString("Some value2"));
47+
48+
list.removeAt(0);
49+
assertEquals(2, list.getLength());
50+
assertEquals("Some value2", list.get(1).getValue());
51+
}
52+
53+
@Test
54+
public void remove() {
55+
list.add(new NBTString("Some value0"));
56+
list.add(new NBTString("Some value1"));
57+
list.add(new NBTString("Some value2"));
58+
59+
list.remove(new NBTString("Some value0"));
60+
assertEquals(2, list.getLength());
61+
assertEquals("Some value2", list.get(1).getValue());
62+
}
63+
64+
@After
65+
public void clean() {
66+
list.clear();
67+
list = null;
68+
}
69+
70+
}

0 commit comments

Comments
 (0)