Skip to content

Commit

Permalink
Create NBTUtil.java
Browse files Browse the repository at this point in the history
  • Loading branch information
Meldexun committed Nov 5, 2023
1 parent 05ebca6 commit 30f4ef4
Showing 1 changed file with 85 additions and 0 deletions.
85 changes: 85 additions & 0 deletions src/main/java/com/charles445/rltweaker/util/NBTUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package com.charles445.rltweaker.util;

import java.util.function.Predicate;
import java.util.stream.IntStream;
import java.util.stream.Stream;

import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;

public class NBTUtil {

public static boolean remove(NBTTagCompound compound, String key) {
if (!compound.hasKey(key)) {
return false;
}
compound.removeTag(key);
return true;
}

public static boolean clear(NBTTagCompound compound) {
if (compound.hasNoTags()) {
return false;
}
compound.getKeySet()
.clear();
return true;
}

@SuppressWarnings("unchecked")
public static <T extends NBTBase> Stream<T> stream(NBTTagCompound compound) {
return compound.getKeySet()
.stream()
.map(compound::getTag)
.map(tag -> (T) tag);
}

@SuppressWarnings("unchecked")
public static <T extends NBTBase> boolean removeIf(NBTTagCompound compound, Predicate<T> predicate) {
return compound.getKeySet()
.removeIf(key -> predicate.test((T) compound.getTag(key)));
}

@SuppressWarnings("unchecked")
public static <T extends NBTBase> boolean removeIf(NBTTagCompound compound, String key, Predicate<T> predicate) {
if (!compound.hasKey(key)) {
return false;
}
if (predicate.test((T) compound.getTag(key))) {
compound.removeTag(key);
return true;
}
return false;
}

public static boolean clear(NBTTagList list) {
if (list.hasNoTags()) {
return false;
}
for (int i = list.tagCount() - 1; i >= 0; i--) {
list.removeTag(i);
}
return true;
}

@SuppressWarnings("unchecked")
public static <T extends NBTBase> Stream<T> stream(NBTTagList list) {
return IntStream.range(0, list.tagCount())
.mapToObj(list::get)
.map(tag -> (T) tag);
}

@SuppressWarnings("unchecked")
public static <T extends NBTBase> boolean removeIf(NBTTagList list, Predicate<T> predicate) {
boolean anythingRemoved = false;
for (int i = list.tagCount() - 1; i >= 0; i--) {
if (predicate.test((T) list.get(i))) {
list.removeTag(i);
anythingRemoved = true;
}
}
return anythingRemoved;
}

}

0 comments on commit 30f4ef4

Please sign in to comment.