use of net.minecraft.nbt.CompoundTag in project SpongeCommon by SpongePowered.
the class SpongeEntitySnapshot method toContainer.
@Override
public DataContainer toContainer() {
final DataContainer unsafeNbt = NBTTranslator.INSTANCE.translateFrom(this.compound == null ? new CompoundTag() : this.compound);
final DataContainer container = DataContainer.createNew(DataView.SafetyMode.NO_DATA_CLONED).set(Queries.CONTENT_VERSION, this.contentVersion()).set(Queries.WORLD_KEY, this.worldKey.formatted()).createView(Constants.Sponge.SNAPSHOT_WORLD_POSITION).set(Queries.POSITION_X, this.position.x()).set(Queries.POSITION_Y, this.position.y()).set(Queries.POSITION_Z, this.position.z()).container().createView(Constants.Entity.ROTATION).set(Queries.POSITION_X, this.rotation.x()).set(Queries.POSITION_Y, this.rotation.y()).set(Queries.POSITION_Z, this.rotation.z()).container().createView(Constants.Entity.SCALE).set(Queries.POSITION_X, this.scale.x()).set(Queries.POSITION_Y, this.scale.y()).set(Queries.POSITION_Z, this.scale.z()).container().set(Constants.Entity.TYPE, net.minecraft.world.entity.EntityType.getKey((net.minecraft.world.entity.EntityType<?>) this.entityType)).set(Constants.Sponge.UNSAFE_NBT, unsafeNbt);
if (this.uniqueId != null) {
container.set(Constants.Entity.UUID, this.uniqueId.toString());
}
return container;
}
use of net.minecraft.nbt.CompoundTag in project SpongeCommon by SpongePowered.
the class SpongeEntityArchetypeBuilder method from.
@SuppressWarnings({ "rawtypes", "EqualsBetweenInconvertibleTypes" })
@Override
public EntityArchetype.Builder from(final Entity entity) {
final EntityType<@NonNull ?> entityType = Objects.requireNonNull(Objects.requireNonNull(entity, "Cannot build an EntityArchetype for a null entity!").type(), "Entity is returning a null EntityType!");
if (!((net.minecraft.world.entity.EntityType) entityType).canSerialize() && entityType != HumanEntity.TYPE) {
throw new IllegalArgumentException("Attempting to archetype a non-serializable entity: " + entity);
}
this.entityType = entityType;
final CompoundTag compound = new CompoundTag();
net.minecraft.world.entity.Entity mcEntity = (net.minecraft.world.entity.Entity) entity;
if (entityType == HumanEntity.TYPE) {
mcEntity.saveWithoutId(compound);
} else {
mcEntity.saveAsPassenger(compound);
}
this.position = new Vector3d(mcEntity.getX(), mcEntity.getY(), mcEntity.getZ());
SpongeEntityArchetypeBuilder.stripCompound(compound);
compound.putBoolean(Constants.Sponge.EntityArchetype.REQUIRES_EXTRA_INITIAL_SPAWN, true);
this.position = entity.position();
this.compound = compound;
return this;
}
use of net.minecraft.nbt.CompoundTag 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());
}
use of net.minecraft.nbt.CompoundTag in project SpongeCommon by SpongePowered.
the class MapInfoItemStackData method register.
// @formatter:off
public static void register(final DataProviderRegistrator registrator) {
registrator.asMutable(ItemStack.class).create(Keys.MAP_INFO).supports(item -> item.getItem() instanceof MapItem).get(itemStack -> {
if (itemStack.getTag() == null) {
return null;
}
return (MapInfo) ((Level) Sponge.server().worldManager().defaultWorld()).getMapData(Constants.Map.MAP_PREFIX + itemStack.getTag().getInt(Constants.Map.MAP_ID));
}).set((itemStack, mapInfo) -> {
@Nullable CompoundTag nbt = itemStack.getTag();
if (nbt == null) {
nbt = new CompoundTag();
}
nbt.putInt(Constants.Map.MAP_ID, ((MapItemSavedDataBridge) mapInfo).bridge$getMapId());
itemStack.setTag(nbt);
});
}
use of net.minecraft.nbt.CompoundTag in project SpongeCommon by SpongePowered.
the class SpongeEntitySnapshotBuilder method from.
@Override
public SpongeEntitySnapshotBuilder from(final Entity entity) {
this.reset();
this.entityReference = new WeakReference<>(entity);
this.worldKey = entity.serverLocation().worldKey();
this.position = entity.transform().position();
this.rotation = entity.transform().rotation();
this.scale = entity.transform().scale();
this.entityType = entity.type();
this.uniqueId = entity.uniqueId();
this.manipulator = ((SpongeDataHolderBridge) entity).bridge$getManipulator().copy();
this.compound = new CompoundTag();
((net.minecraft.world.entity.Entity) entity).saveWithoutId(this.compound);
return this;
}
Aggregations