feat: add various utilities
This commit is contained in:
parent
a2f1d5f4c1
commit
1b3a08a955
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
50
src/main/java/net/anvilcraft/anvillib/util/NBTCollector.java
Normal file
50
src/main/java/net/anvilcraft/anvillib/util/NBTCollector.java
Normal 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);
|
||||
}
|
||||
}
|
|
@ -1 +1,2 @@
|
|||
public net.minecraft.world.storage.SaveHandler field_75771_c # playersDirectory
|
||||
public net.minecraft.nbt.NBTTagList field_74747_a # tagList
|
||||
|
|
Loading…
Reference in a new issue