Search in sources :

Example 21 with DataProviderRegistrator

use of org.spongepowered.common.data.provider.DataProviderRegistrator in project SpongeCommon by SpongePowered.

the class VanishableData method register.

// @formatter:off
@SuppressWarnings("unchecked")
public static void register(final DataProviderRegistrator registrator) {
    registrator.asMutable(VanishableBridge.class).create(Keys.IS_INVISIBLE).get(VanishableBridge::bridge$isInvisible).set(VanishableBridge::bridge$setInvisible).create(Keys.VANISH).get(bridge -> bridge.bridge$vanishState().invisible()).setAnd((h, v) -> {
        if (h instanceof Entity && ((Entity) h).level.isClientSide) {
            return false;
        }
        h.bridge$vanishState(v ? VanishState.vanished() : VanishState.unvanished());
        return true;
    }).create(Keys.VANISH_IGNORES_COLLISION).get(b -> b.bridge$vanishState().ignoresCollisions()).setAnd((h, v) -> {
        if (h instanceof Entity && ((Entity) h).level.isClientSide) {
            return false;
        }
        if (!h.bridge$vanishState().invisible()) {
            return false;
        }
        h.bridge$vanishState(h.bridge$vanishState().ignoreCollisions(v));
        return true;
    }).create(Keys.VANISH_PREVENTS_TARGETING).get(b -> b.bridge$vanishState().untargetable()).setAnd((h, v) -> {
        if (h instanceof Entity && ((Entity) h).level.isClientSide) {
            return false;
        }
        if (!h.bridge$vanishState().invisible()) {
            return false;
        }
        h.bridge$vanishState(h.bridge$vanishState().untargetable(v));
        return true;
    }).create(Keys.VANISH_STATE).get(VanishableBridge::bridge$vanishState).setAnd((h, v) -> {
        if (h instanceof Entity && ((Entity) h).level.isClientSide) {
            return false;
        }
        h.bridge$vanishState(v);
        return true;
    });
    final ResourceKey dataStoreKey = ResourceKey.sponge("invisibility");
    registrator.spongeDataStore(dataStoreKey, VanishableBridge.class, Keys.IS_INVISIBLE, Keys.VANISH_STATE);
    SpongeDataManager.INSTANCE.registerLegacySpongeData(Constants.Sponge.Entity.IS_INVISIBLE, dataStoreKey, Keys.IS_INVISIBLE);
    SpongeDataManager.INSTANCE.registerLegacySpongeData(Constants.Sponge.Entity.IS_VANISHED, dataStoreKey, Keys.VANISH_STATE);
    SpongeDataManager.INSTANCE.registerLegacySpongeData(Constants.Sponge.Entity.VANISH_UNCOLLIDEABLE, dataStoreKey, Keys.VANISH_IGNORES_COLLISION);
    SpongeDataManager.INSTANCE.registerLegacySpongeData(Constants.Sponge.Entity.VANISH_UNTARGETABLE, dataStoreKey, Keys.VANISH_PREVENTS_TARGETING);
}
Also used : VanishState(org.spongepowered.api.effect.VanishState) Keys(org.spongepowered.api.data.Keys) VanishableBridge(org.spongepowered.common.bridge.data.VanishableBridge) Entity(net.minecraft.world.entity.Entity) Constants(org.spongepowered.common.util.Constants) DataProviderRegistrator(org.spongepowered.common.data.provider.DataProviderRegistrator) ResourceKey(org.spongepowered.api.ResourceKey) SpongeDataManager(org.spongepowered.common.data.SpongeDataManager) Entity(net.minecraft.world.entity.Entity) VanishableBridge(org.spongepowered.common.bridge.data.VanishableBridge) ResourceKey(org.spongepowered.api.ResourceKey)

Example 22 with DataProviderRegistrator

use of org.spongepowered.common.data.provider.DataProviderRegistrator in project SpongeCommon by SpongePowered.

the class PotionItemStackData method register.

// @formatter:off
public static void register(final DataProviderRegistrator registrator) {
    registrator.asMutable(ItemStack.class).create(Keys.COLOR).get(h -> Color.ofRgb(PotionUtils.getColor(h))).set((h, v) -> {
        final CompoundTag tag = h.getOrCreateTag();
        tag.putInt(Constants.Item.CUSTOM_POTION_COLOR, v.rgb());
    }).delete(h -> h.removeTagKey(Constants.Item.CUSTOM_POTION_COLOR)).supports(h -> h.getItem() == Items.POTION || h.getItem() == Items.SPLASH_POTION || h.getItem() == Items.LINGERING_POTION).create(Keys.POTION_EFFECTS).get(h -> {
        final List<MobEffectInstance> effects = PotionUtils.getMobEffects(h);
        return effects.isEmpty() ? null : ImmutableList.copyOf((List<PotionEffect>) (Object) effects);
    }).set((h, v) -> {
        final CompoundTag tag = h.getOrCreateTag();
        final ListTag list = v.stream().map(effect -> {
            final CompoundTag potionTag = new CompoundTag();
            ((MobEffectInstance) effect).save(potionTag);
            return potionTag;
        }).collect(NBTCollectors.toTagList());
        tag.put(Constants.Item.CUSTOM_POTION_EFFECTS, list);
    }).delete(h -> h.removeTagKey(Constants.Item.CUSTOM_POTION_EFFECTS)).supports(h -> h.getItem() == Items.POTION || h.getItem() == Items.SPLASH_POTION || h.getItem() == Items.LINGERING_POTION || h.getItem() == Items.TIPPED_ARROW).create(Keys.POTION_TYPE).get(h -> (PotionType) PotionUtils.getPotion(h)).set((h, v) -> {
        h.getOrCreateTag();
        PotionUtils.setPotion(h, (Potion) v);
    }).delete(h -> {
        if (h.hasTag()) {
            PotionUtils.setPotion(h, Potions.EMPTY);
        }
    }).supports(h -> h.getItem() == Items.POTION || h.getItem() == Items.SPLASH_POTION || h.getItem() == Items.LINGERING_POTION || h.getItem() == Items.TIPPED_ARROW);
}
Also used : PotionUtils(net.minecraft.world.item.alchemy.PotionUtils) Items(net.minecraft.world.item.Items) Constants(org.spongepowered.common.util.Constants) MobEffectInstance(net.minecraft.world.effect.MobEffectInstance) Potions(net.minecraft.world.item.alchemy.Potions) Potion(net.minecraft.world.item.alchemy.Potion) Keys(org.spongepowered.api.data.Keys) List(java.util.List) CompoundTag(net.minecraft.nbt.CompoundTag) ImmutableList(com.google.common.collect.ImmutableList) NBTCollectors(org.spongepowered.common.util.NBTCollectors) DataProviderRegistrator(org.spongepowered.common.data.provider.DataProviderRegistrator) PotionEffect(org.spongepowered.api.effect.potion.PotionEffect) Color(org.spongepowered.api.util.Color) ItemStack(net.minecraft.world.item.ItemStack) ListTag(net.minecraft.nbt.ListTag) PotionType(org.spongepowered.api.item.potion.PotionType) MobEffectInstance(net.minecraft.world.effect.MobEffectInstance) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) PotionType(org.spongepowered.api.item.potion.PotionType) ItemStack(net.minecraft.world.item.ItemStack) ListTag(net.minecraft.nbt.ListTag) CompoundTag(net.minecraft.nbt.CompoundTag)

Example 23 with DataProviderRegistrator

use of org.spongepowered.common.data.provider.DataProviderRegistrator in project SpongeCommon by SpongePowered.

the class SignItemStackData method register.

// @formatter:off
public static void register(final DataProviderRegistrator registrator) {
    registrator.asMutable(ItemStack.class).create(Keys.SIGN_LINES).get(h -> {
        final CompoundTag tag = h.getTagElement(Constants.Item.BLOCK_ENTITY_TAG);
        if (tag == null) {
            return null;
        }
        final String id = tag.getString(Constants.Item.BLOCK_ENTITY_ID);
        if (!id.equalsIgnoreCase(Constants.TileEntity.SIGN)) {
            return null;
        }
        final GsonComponentSerializer gcs = GsonComponentSerializer.gson();
        final List<Component> texts = Lists.newArrayListWithCapacity(4);
        for (int i = 0; i < 4; i++) {
            texts.add(gcs.deserialize(tag.getString("Text" + (i + 1))));
        }
        return texts;
    }).set((h, v) -> {
        final GsonComponentSerializer gcs = GsonComponentSerializer.gson();
        final CompoundTag tag = h.getOrCreateTagElement(Constants.Item.BLOCK_ENTITY_TAG);
        tag.putString(Constants.Item.BLOCK_ENTITY_ID, Constants.TileEntity.SIGN);
        for (int i = 0; i < 4; i++) {
            final Component line = v.size() > i ? v.get(i) : Component.empty();
            if (line == null) {
                throw new IllegalArgumentException("A null line was given at index " + i);
            }
            tag.putString("Text" + (i + 1), gcs.serialize(line));
        }
    }).delete(h -> h.removeTagKey(Constants.Item.BLOCK_ENTITY_TAG));
}
Also used : GsonComponentSerializer(net.kyori.adventure.text.serializer.gson.GsonComponentSerializer) Keys(org.spongepowered.api.data.Keys) List(java.util.List) Lists(com.google.common.collect.Lists) CompoundTag(net.minecraft.nbt.CompoundTag) Constants(org.spongepowered.common.util.Constants) Component(net.kyori.adventure.text.Component) DataProviderRegistrator(org.spongepowered.common.data.provider.DataProviderRegistrator) ItemStack(net.minecraft.world.item.ItemStack) SpongeAdventure(org.spongepowered.common.adventure.SpongeAdventure) GsonComponentSerializer(net.kyori.adventure.text.serializer.gson.GsonComponentSerializer) Component(net.kyori.adventure.text.Component) CompoundTag(net.minecraft.nbt.CompoundTag)

Aggregations

Keys (org.spongepowered.api.data.Keys)23 DataProviderRegistrator (org.spongepowered.common.data.provider.DataProviderRegistrator)23 Constants (org.spongepowered.common.util.Constants)16 CompoundTag (net.minecraft.nbt.CompoundTag)9 ItemStack (net.minecraft.world.item.ItemStack)8 ResourceKey (org.spongepowered.api.ResourceKey)7 SpongeTicks (org.spongepowered.common.util.SpongeTicks)7 SpongeDataManager (org.spongepowered.common.data.SpongeDataManager)6 List (java.util.List)4 Collectors (java.util.stream.Collectors)4 MobEffectInstance (net.minecraft.world.effect.MobEffectInstance)4 SpongeAdventure (org.spongepowered.common.adventure.SpongeAdventure)4 ListTag (net.minecraft.nbt.ListTag)3 Level (net.minecraft.world.level.Level)3 PotionEffect (org.spongepowered.api.effect.potion.PotionEffect)3 ImmutableList (com.google.common.collect.ImmutableList)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 Set (java.util.Set)2 GsonComponentSerializer (net.kyori.adventure.text.serializer.gson.GsonComponentSerializer)2