Search in sources :

Example 1 with FireworkRocketEntityAccessor

use of org.spongepowered.common.accessor.world.entity.projectile.FireworkRocketEntityAccessor in project SpongeCommon by SpongePowered.

the class FireworkRocketData method register.

// @formatter:off
public static void register(final DataProviderRegistrator registrator) {
    registrator.asMutable(FireworkRocketEntity.class).create(Keys.FIREWORK_EFFECTS).get(h -> FireworkUtil.getFireworkEffects(h).orElse(null)).set(FireworkUtil::setFireworkEffects).resetOnDelete(ImmutableList.of()).create(Keys.FIREWORK_FLIGHT_MODIFIER).get(h -> {
        final ItemStack item = FireworkUtil.getItem(h);
        final CompoundTag fireworks = item.getOrCreateTagElement(Constants.Item.Fireworks.FIREWORKS);
        if (fireworks.contains(Constants.Item.Fireworks.FLIGHT)) {
            return new SpongeTicks(fireworks.getByte(Constants.Item.Fireworks.FLIGHT));
        }
        return null;
    }).setAnd((h, v) -> {
        final int ticks = (int) v.ticks();
        if (ticks < 0 || ticks > Byte.MAX_VALUE) {
            return false;
        }
        final ItemStack item = FireworkUtil.getItem(h);
        final CompoundTag fireworks = item.getOrCreateTagElement(Constants.Item.Fireworks.FIREWORKS);
        fireworks.putByte(Constants.Item.Fireworks.FLIGHT, (byte) ticks);
        ((FireworkRocketEntityAccessor) h).accessor$lifetime(10 * ticks + ((EntityAccessor) h).accessor$random().nextInt(6) + ((EntityAccessor) h).accessor$random().nextInt(7));
        return true;
    });
}
Also used : FireworkRocketEntity(net.minecraft.world.entity.projectile.FireworkRocketEntity) Keys(org.spongepowered.api.data.Keys) FireworkRocketEntityAccessor(org.spongepowered.common.accessor.world.entity.projectile.FireworkRocketEntityAccessor) FireworkUtil(org.spongepowered.common.util.FireworkUtil) CompoundTag(net.minecraft.nbt.CompoundTag) ImmutableList(com.google.common.collect.ImmutableList) Constants(org.spongepowered.common.util.Constants) DataProviderRegistrator(org.spongepowered.common.data.provider.DataProviderRegistrator) EntityAccessor(org.spongepowered.common.accessor.world.entity.EntityAccessor) SpongeTicks(org.spongepowered.common.util.SpongeTicks) ItemStack(net.minecraft.world.item.ItemStack) FireworkRocketEntityAccessor(org.spongepowered.common.accessor.world.entity.projectile.FireworkRocketEntityAccessor) EntityAccessor(org.spongepowered.common.accessor.world.entity.EntityAccessor) FireworkUtil(org.spongepowered.common.util.FireworkUtil) FireworkRocketEntity(net.minecraft.world.entity.projectile.FireworkRocketEntity) ItemStack(net.minecraft.world.item.ItemStack) SpongeTicks(org.spongepowered.common.util.SpongeTicks) FireworkRocketEntityAccessor(org.spongepowered.common.accessor.world.entity.projectile.FireworkRocketEntityAccessor) CompoundTag(net.minecraft.nbt.CompoundTag)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 CompoundTag (net.minecraft.nbt.CompoundTag)1 FireworkRocketEntity (net.minecraft.world.entity.projectile.FireworkRocketEntity)1 ItemStack (net.minecraft.world.item.ItemStack)1 Keys (org.spongepowered.api.data.Keys)1 EntityAccessor (org.spongepowered.common.accessor.world.entity.EntityAccessor)1 FireworkRocketEntityAccessor (org.spongepowered.common.accessor.world.entity.projectile.FireworkRocketEntityAccessor)1 DataProviderRegistrator (org.spongepowered.common.data.provider.DataProviderRegistrator)1 Constants (org.spongepowered.common.util.Constants)1 FireworkUtil (org.spongepowered.common.util.FireworkUtil)1 SpongeTicks (org.spongepowered.common.util.SpongeTicks)1