use of net.minecraft.nbt.ListTag in project SpongeCommon by SpongePowered.
the class BookItemStackData method get.
// @formatter:on
private static <V> List<V> get(final ItemStack holder, final Function<String, V> predicate) {
final CompoundTag tag = holder.getTag();
if (tag == null || !tag.contains(Constants.Item.Book.ITEM_BOOK_PAGES)) {
return null;
}
final ListTag list = tag.getList(Constants.Item.Book.ITEM_BOOK_PAGES, Constants.NBT.TAG_STRING);
return list.stream().map(Tag::getAsString).map(predicate).collect(Collectors.toList());
}
use of net.minecraft.nbt.ListTag in project SpongeCommon by SpongePowered.
the class FireworkUtil method getFireworkEffects.
public static Optional<List<FireworkEffect>> getFireworkEffects(final Object object) {
final ItemStack item = FireworkUtil.getItem(object);
if (item.isEmpty()) {
return Optional.empty();
}
final List<FireworkEffect> effects;
if (item.getItem() == Items.FIREWORK_ROCKET) {
@Nullable final CompoundTag fireworks = item.getTagElement(Constants.Item.Fireworks.FIREWORKS);
if (fireworks == null || !fireworks.contains(Constants.Item.Fireworks.EXPLOSIONS)) {
return Optional.empty();
}
final ListTag effectCompounds = fireworks.getList(Constants.Item.Fireworks.EXPLOSIONS, Constants.NBT.TAG_COMPOUND);
effects = NBTStreams.toCompounds(effectCompounds).map(FireworkUtil::fromCompound).collect(Collectors.toList());
} else {
@Nullable final FireworkEffect effect = FireworkUtil.getStarEffect(item);
if (effect == null) {
return Optional.empty();
}
effects = ImmutableList.of(effect);
}
return Optional.of(effects);
}
use of net.minecraft.nbt.ListTag in project SpongeCommon by SpongePowered.
the class SpongeUserData method loadInventory.
private UserInventory loadInventory() {
if (this.inventory == null) {
this.inventory = new SpongeUserInventory(this);
final ListTag listNBT = this.compound.getList(Constants.Entity.Player.INVENTORY, 10);
this.inventory.readList(listNBT);
this.inventory.currentItem = this.compound.getInt(Constants.Entity.Player.SELECTED_ITEM_SLOT);
}
return (UserInventory) this.inventory;
}
use of net.minecraft.nbt.ListTag in project SpongeCommon by SpongePowered.
the class SpongeEntityArchetype method apply.
@Override
public Optional<org.spongepowered.api.entity.Entity> apply(final ServerLocation location) {
if (!PlatformHooks.INSTANCE.getGeneralHooks().onServerThread()) {
return Optional.empty();
}
final org.spongepowered.api.world.server.ServerWorld spongeWorld = location.world();
final ServerLevel level = (ServerLevel) spongeWorld;
final ResourceLocation key = net.minecraft.world.entity.EntityType.getKey((net.minecraft.world.entity.EntityType<?>) this.type);
if (key == null) {
return Optional.empty();
}
final CompoundTag compound = this.compound.copy();
compound.putString(Constants.Entity.ENTITY_TYPE_ID, key.toString());
final ListTag pos = new ListTag();
pos.add(DoubleTag.valueOf(location.x()));
pos.add(DoubleTag.valueOf(location.y()));
pos.add(DoubleTag.valueOf(location.z()));
compound.put(Constants.Entity.ENTITY_POSITION, pos);
compound.remove(Constants.Entity.ENTITY_UUID);
final boolean requiresInitialSpawn;
if (compound.contains(Constants.Sponge.EntityArchetype.REQUIRES_EXTRA_INITIAL_SPAWN)) {
requiresInitialSpawn = compound.getBoolean(Constants.Sponge.EntityArchetype.REQUIRES_EXTRA_INITIAL_SPAWN);
compound.remove(Constants.Sponge.EntityArchetype.REQUIRES_EXTRA_INITIAL_SPAWN);
} else {
requiresInitialSpawn = true;
}
@Nullable final Entity entity = net.minecraft.world.entity.EntityType.loadEntityRecursive(compound, level, e -> {
e.moveTo(location.x(), location.y(), location.z());
if (requiresInitialSpawn && e instanceof Mob) {
((Mob) e).finalizeSpawn(level, level.getCurrentDifficultyAt(e.blockPosition()), MobSpawnType.COMMAND, null, compound);
}
return e;
});
if (entity == null) {
return Optional.empty();
}
if (level.tryAddFreshEntityWithPassengers(entity)) {
return Optional.of((org.spongepowered.api.entity.Entity) entity);
}
return Optional.empty();
}
use of net.minecraft.nbt.ListTag in project SpongeCommon by SpongePowered.
the class ItemStackData method register.
// @formatter:off
public static void register(final DataProviderRegistrator registrator) {
registrator.asMutable(ItemStack.class).create(Keys.APPLICABLE_POTION_EFFECTS).get(h -> {
if (h.isEdible()) {
final List<Pair<MobEffectInstance, Float>> itemEffects = h.getItem().getFoodProperties().getEffects();
final WeightedTable<PotionEffect> effects = new WeightedTable<>();
final ChanceTable<PotionEffect> chance = new ChanceTable<>();
for (final Pair<MobEffectInstance, Float> effect : itemEffects) {
chance.add((PotionEffect) effect.getFirst(), effect.getSecond());
}
effects.add(new NestedTableEntry<>(1, chance));
return effects;
}
return null;
}).create(Keys.BURN_TIME).get(h -> {
final Integer burnTime = AbstractFurnaceBlockEntity.getFuel().get(h.getItem());
if (burnTime != null && burnTime > 0) {
return burnTime;
}
return null;
}).create(Keys.CAN_HARVEST).get(h -> {
final Item item = h.getItem();
if (item instanceof DiggerItemAccessor && !(item instanceof PickaxeItem)) {
final Set<Block> blocks = ((DiggerItemAccessor) item).accessor$blocks();
return ImmutableSet.copyOf((Set<BlockType>) (Object) blocks);
}
final Set<BlockType> blockTypes = Registry.BLOCK.stream().filter(b -> item.isCorrectToolForDrops(b.defaultBlockState())).map(BlockType.class::cast).collect(ImmutableSet.toImmutableSet());
return blockTypes.isEmpty() ? null : blockTypes;
}).create(Keys.CONTAINER_ITEM).get(h -> (ItemType) h.getItem().getCraftingRemainingItem()).create(Keys.DISPLAY_NAME).get(h -> SpongeAdventure.asAdventure(h.getDisplayName())).create(Keys.CUSTOM_MODEL_DATA).get(h -> {
final CompoundTag tag = h.getTag();
if (tag == null || !tag.contains(Constants.Item.CUSTOM_MODEL_DATA, Constants.NBT.TAG_INT)) {
return null;
}
return tag.getInt(Constants.Item.CUSTOM_MODEL_DATA);
}).set((h, v) -> {
final CompoundTag tag = h.getOrCreateTag();
tag.putInt(Constants.Item.CUSTOM_MODEL_DATA, v);
}).delete(h -> {
final CompoundTag tag = h.getTag();
if (tag != null) {
tag.remove(Constants.Item.CUSTOM_MODEL_DATA);
}
}).create(Keys.CUSTOM_NAME).get(h -> {
if (h.hasCustomHoverName()) {
return SpongeAdventure.asAdventure(h.getHoverName());
}
if (h.getItem() == Items.WRITTEN_BOOK) {
// When no custom name is set on a written book fallback to its title
// The custom name has a higher priority than the title so no setter is needed.
final CompoundTag tag = h.getTag();
if (tag != null) {
final String title = tag.getString(Constants.Item.Book.ITEM_BOOK_TITLE);
return LegacyComponentSerializer.legacySection().deserialize(title);
}
}
return null;
}).set((h, v) -> h.setHoverName(SpongeAdventure.asVanilla(v))).delete(ItemStack::resetHoverName).create(Keys.IS_UNBREAKABLE).get(h -> {
final CompoundTag tag = h.getTag();
if (tag == null || !tag.contains(Constants.Item.ITEM_UNBREAKABLE, Constants.NBT.TAG_BYTE)) {
return false;
}
return tag.getBoolean(Constants.Item.ITEM_UNBREAKABLE);
}).set(ItemStackData::setIsUnbrekable).delete(h -> ItemStackData.setIsUnbrekable(h, false)).create(Keys.LORE).get(h -> {
final CompoundTag tag = h.getTag();
if (tag == null || !tag.contains(Constants.Item.ITEM_DISPLAY)) {
return null;
}
final CompoundTag displayCompound = tag.getCompound(Constants.Item.ITEM_DISPLAY);
final ListTag list = displayCompound.getList(Constants.Item.ITEM_LORE, Constants.NBT.TAG_STRING);
return list.isEmpty() ? null : SpongeAdventure.json(list.stream().collect(NBTCollectors.toStringList()));
}).set((h, v) -> {
if (v.isEmpty()) {
ItemStackData.deleteLore(h);
return;
}
final ListTag list = SpongeAdventure.listTagJson(v);
h.getOrCreateTagElement(Constants.Item.ITEM_DISPLAY).put(Constants.Item.ITEM_LORE, list);
}).delete(ItemStackData::deleteLore).create(Keys.MAX_DURABILITY).get(h -> h.getItem().canBeDepleted() ? h.getItem().getMaxDamage() : null).supports(h -> h.getItem().canBeDepleted()).create(Keys.ITEM_DURABILITY).get(stack -> stack.getMaxDamage() - stack.getDamageValue()).set((stack, durability) -> stack.setDamageValue(stack.getMaxDamage() - durability)).supports(h -> h.getItem().canBeDepleted()).create(Keys.ITEM_RARITY).get(stack -> (ItemRarity) (Object) stack.getRarity()).create(Keys.REPLENISHED_FOOD).get(h -> {
if (h.getItem().isEdible()) {
final FoodProperties food = h.getItem().getFoodProperties();
return food == null ? null : food.getNutrition();
}
return null;
}).supports(h -> h.getItem().isEdible()).create(Keys.REPLENISHED_SATURATION).get(h -> {
if (h.getItem().isEdible()) {
final FoodProperties food = h.getItem().getFoodProperties();
if (food != null) {
// Translate's Minecraft's weird internal value to the actual saturation value
return food.getSaturationModifier() * food.getNutrition() * 2.0;
}
}
return null;
}).supports(h -> h.getItem().isEdible());
}
Aggregations