use of net.minecraft.nbt.ListTag in project SpongeCommon by SpongePowered.
the class BlockTypeItemStackData method set.
private static boolean set(final ItemStack stack, final String nbtKey, final Set<? extends BlockType> value) {
if (value.isEmpty()) {
stack.removeTagKey(nbtKey);
return true;
}
final CompoundTag tag = stack.getOrCreateTag();
final ListTag list = value.stream().map(type -> Registry.BLOCK.getKey((Block) type).toString()).collect(NBTCollectors.toStringTagList());
tag.put(nbtKey, list);
return true;
}
use of net.minecraft.nbt.ListTag in project SpongeCommon by SpongePowered.
the class BookPagesItemStackData method get.
// @formatter:on
private static List<Enchantment> get(final ItemStack holder, final String nbtKey) {
final CompoundTag tag = holder.getTag();
if (tag == null || !tag.contains(nbtKey, Constants.NBT.TAG_LIST)) {
return new ArrayList<>();
}
final ListTag list = tag.getList(nbtKey, Constants.NBT.TAG_COMPOUND);
return NBTStreams.toCompounds(list).map(BookPagesItemStackData::enchantmentFromNbt).filter(Objects::nonNull).collect(Collectors.toList());
}
use of net.minecraft.nbt.ListTag in project SpongeCommon by SpongePowered.
the class NBTTranslator method setInternal.
@SuppressWarnings({ "unchecked", "rawtypes" })
private static void setInternal(Tag base, byte type, DataView view, String key) {
checkNotNull(base);
checkNotNull(view);
checkNotNull(key);
checkArgument(!key.isEmpty());
checkArgument(type > Constants.NBT.TAG_END && type <= Constants.NBT.TAG_INT_ARRAY);
switch(type) {
case Constants.NBT.TAG_BYTE:
if (key.contains(NBTTranslator.BOOLEAN_IDENTIFIER)) {
view.set(of(key.replace(NBTTranslator.BOOLEAN_IDENTIFIER, "")), (((ByteTag) base).getAsByte() != 0));
} else {
view.set(of(key), ((ByteTag) base).getAsByte());
}
break;
case Constants.NBT.TAG_SHORT:
view.set(of(key), ((ShortTag) base).getAsShort());
break;
case Constants.NBT.TAG_INT:
view.set(of(key), ((IntTag) base).getAsInt());
break;
case Constants.NBT.TAG_LONG:
view.set(of(key), ((LongTag) base).getAsLong());
break;
case Constants.NBT.TAG_FLOAT:
view.set(of(key), ((FloatTag) base).getAsFloat());
break;
case Constants.NBT.TAG_DOUBLE:
view.set(of(key), ((DoubleTag) base).getAsDouble());
break;
case Constants.NBT.TAG_BYTE_ARRAY:
view.set(of(key), ((ByteArrayTag) base).getAsByteArray());
break;
case Constants.NBT.TAG_STRING:
view.set(of(key), base.getAsString());
break;
case Constants.NBT.TAG_LIST:
ListTag list = (ListTag) base;
byte listType = list.getElementType();
int count = list.size();
List objectList = Lists.newArrayListWithCapacity(count);
for (final Tag inbt : list) {
objectList.add(NBTTranslator.fromTagBase(inbt, listType));
}
view.set(of(key), objectList);
break;
case Constants.NBT.TAG_COMPOUND:
DataView internalView = view.createView(of(key));
CompoundTag compound = (CompoundTag) base;
for (String internalKey : compound.getAllKeys()) {
Tag internalBase = compound.get(internalKey);
byte internalType = internalBase.getId();
// Basically.... more recursion.
// Reasoning: This avoids creating a new DataContainer which would
// then be copied in to the owning DataView anyways. We can internally
// set the actual data directly to the child view instead.
NBTTranslator.setInternal(internalBase, internalType, internalView, internalKey);
}
break;
case Constants.NBT.TAG_INT_ARRAY:
view.set(of(key), ((IntArrayTag) base).getAsIntArray());
break;
case Constants.NBT.TAG_LONG_ARRAY:
view.set(of(key), ((LongArrayTag) base).getAsLongArray());
break;
default:
throw new IllegalArgumentException("Unknown NBT type " + type);
}
}
use of net.minecraft.nbt.ListTag in project SpongeCommon by SpongePowered.
the class NBTTranslator method fromTagBase.
@SuppressWarnings({ "unchecked", "rawtypes" })
private static Object fromTagBase(Tag base, byte type) {
switch(type) {
case Constants.NBT.TAG_BYTE:
return ((ByteTag) base).getAsByte();
case Constants.NBT.TAG_SHORT:
return (((ShortTag) base)).getAsShort();
case Constants.NBT.TAG_INT:
return ((IntTag) base).getAsInt();
case Constants.NBT.TAG_LONG:
return ((LongTag) base).getAsLong();
case Constants.NBT.TAG_FLOAT:
return ((FloatTag) base).getAsFloat();
case Constants.NBT.TAG_DOUBLE:
return ((DoubleTag) base).getAsDouble();
case Constants.NBT.TAG_BYTE_ARRAY:
return ((ByteArrayTag) base).getAsByteArray();
case Constants.NBT.TAG_STRING:
return base.getAsString();
case Constants.NBT.TAG_LIST:
ListTag list = (ListTag) base;
byte listType = list.getElementType();
int count = list.size();
List objectList = Lists.newArrayListWithCapacity(count);
for (Tag inbt : list) {
objectList.add(NBTTranslator.fromTagBase(inbt, listType));
}
return objectList;
case Constants.NBT.TAG_COMPOUND:
return NBTTranslator.getViewFromCompound((CompoundTag) base);
case Constants.NBT.TAG_INT_ARRAY:
return ((IntArrayTag) base).getAsIntArray();
case Constants.NBT.TAG_LONG_ARRAY:
return ((LongArrayTag) base).getAsLongArray();
default:
return null;
}
}
use of net.minecraft.nbt.ListTag in project Tropicraft by Tropicraft.
the class EntityKoaBase method readAdditionalSaveData.
@Override
public void readAdditionalSaveData(CompoundTag compound) {
super.readAdditionalSaveData(compound);
if (compound.contains("home_X")) {
this.restrictTo(new BlockPos(compound.getInt("home_X"), compound.getInt("home_Y"), compound.getInt("home_Z")), MAX_HOME_DISTANCE);
}
if (compound.contains("fireplace_X")) {
this.setFirelacePos(new BlockPos(compound.getInt("fireplace_X"), compound.getInt("fireplace_Y"), compound.getInt("fireplace_Z")));
}
lastTimeFished = compound.getLong("lastTimeFished");
if (compound.contains("koa_inventory", 9)) {
ListTag nbttaglist = compound.getList("koa_inventory", 10);
for (int i = 0; i < nbttaglist.size(); ++i) {
CompoundTag nbttagcompound = nbttaglist.getCompound(i);
int j = nbttagcompound.getByte("Slot") & 255;
this.inventory.setItem(j, ItemStack.of(nbttagcompound));
}
}
this.villageID = compound.getInt("village_id");
// backwards compat
if (!compound.contains("village_dimension")) {
this.villageDimension = level.dimension();
} else {
this.villageDimension = ResourceKey.create(Registry.DIMENSION_REGISTRY, new ResourceLocation(compound.getString("village_dim_id")));
}
if (compound.contains("role_id")) {
this.getEntityData().set(ROLE, compound.getInt("role_id"));
} else {
rollDiceRole();
}
if (compound.contains("gender_id")) {
this.getEntityData().set(GENDER, compound.getInt("gender_id"));
} else {
rollDiceGender();
}
this.lastTradeTime = compound.getLong("lastTradeTime");
for (int i = 0; i < MAX_DRUMS; i++) {
if (compound.contains("drum_" + i + "_X")) {
this.listPosDrums.add(new BlockPos(compound.getInt("drum_" + i + "_X"), compound.getInt("drum_" + i + "_Y"), compound.getInt("drum_" + i + "_Z")));
}
}
druggedTime = compound.getInt("druggedTime");
updateUniqueEntityAI();
}
Aggregations