feat: add various utilities

This commit is contained in:
LordMZTE 2024-05-05 15:08:16 +02:00
parent a2f1d5f4c1
commit 1b3a08a955
Signed by: LordMZTE
GPG key ID: B64802DC33A64FF6
3 changed files with 108 additions and 0 deletions

View file

@ -1,10 +1,67 @@
package net.anvilcraft.anvillib.util;
import java.util.Random;
import java.util.UUID;
import java.util.stream.IntStream;
import net.minecraft.entity.item.EntityItem;
import net.minecraft.inventory.IInventory;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.nbt.NBTTagLong;
import net.minecraft.tileentity.TileEntity;
public class AnvilUtil {
public static final Random rand = new Random();
public static <T extends Enum<T>> T enumFromInt(Class<T> clazz, int n) {
T[] values = clazz.getEnumConstants();
if (n < 0 || n >= values.length)
return null;
return values[n];
}
public static NBTTagList uuidToNBT(UUID uuid) {
NBTTagList nbt = new NBTTagList();
nbt.appendTag(new NBTTagLong(uuid.getMostSignificantBits()));
nbt.appendTag(new NBTTagLong(uuid.getLeastSignificantBits()));
return nbt;
}
public static UUID uuidFromNBT(NBTTagList nbt) {
if (nbt.tagCount() != 2 || !(nbt.tagList.get(0) instanceof NBTTagLong)
|| !(nbt.tagList.get(1) instanceof NBTTagLong))
return null;
return new UUID(
((NBTTagLong) nbt.tagList.get(0)).func_150291_c(),
((NBTTagLong) nbt.tagList.get(1)).func_150291_c()
);
}
/**
* Drops all items in the given IInventory into the world.
*/
public static void dropInventoryContents(IInventory inv) {
TileEntity te = (TileEntity) inv;
IntStream.range(0, inv.getSizeInventory())
.mapToObj(inv::getStackInSlot)
.filter(stack -> (stack != null && stack.stackSize > 0))
.map(stack -> {
EntityItem ent = new EntityItem(
te.getWorldObj(),
te.xCoord + rand.nextFloat() * 0.8f + 0.1f,
te.yCoord + rand.nextFloat() * 0.8f + 0.1f,
te.zCoord + rand.nextFloat() * 0.8f + 0.1f,
stack.copy()
);
ent.motionX = rand.nextGaussian() * 0.05f;
ent.motionY = rand.nextGaussian() * 0.05f + 0.2f;
ent.motionZ = rand.nextGaussian() * 0.05f;
return ent;
})
.forEach(te.getWorldObj()::spawnEntityInWorld);
}
}

View file

@ -0,0 +1,50 @@
package net.anvilcraft.anvillib.util;
import java.util.List;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.function.BinaryOperator;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collector;
import com.google.common.collect.ImmutableSet;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagList;
/**
* A Collector implementation for collecting into an NBTTagList
*/
public class NBTCollector implements Collector<NBTBase, NBTTagList, NBTTagList> {
@Override
public Supplier<NBTTagList> supplier() {
return NBTTagList::new;
}
@Override
public BiConsumer<NBTTagList, NBTBase> accumulator() {
return NBTTagList::appendTag;
}
@Override
@SuppressWarnings("unchecked")
public BinaryOperator<NBTTagList> combiner() {
return (a, b) -> {
for (NBTBase n : (List<? extends NBTBase>) b.tagList) {
a.appendTag(n);
}
return a;
};
}
@Override
public Function<NBTTagList, NBTTagList> finisher() {
return x -> x;
}
@Override
public Set<Characteristics> characteristics() {
return ImmutableSet.of(Characteristics.IDENTITY_FINISH);
}
}

View file

@ -1 +1,2 @@
public net.minecraft.world.storage.SaveHandler field_75771_c # playersDirectory
public net.minecraft.nbt.NBTTagList field_74747_a # tagList