Search in sources :

Example 16 with PotionEffect

use of org.spongepowered.api.effect.potion.PotionEffect in project RedProtect by FabioZumbi12.

the class RPPlayerListener method onConsume.

@Listener(order = Order.FIRST, beforeModifications = true)
public void onConsume(UseItemStackEvent.Start e, @First Player p) {
    ItemStack stack = e.getItemStackInUse().createStack();
    RedProtect.get().logger.debug("player", "Is UseItemStackEvent.Start event. Item: " + stack.getItem().getName());
    // deny potion
    List<String> Pots = RedProtect.get().cfgs.getStringList("server-protection.deny-potions");
    if (stack.get(Keys.POTION_EFFECTS).isPresent() && Pots.size() > 0) {
        List<PotionEffect> pot = stack.get(Keys.POTION_EFFECTS).get();
        for (PotionEffect pots : pot) {
            if (Pots.contains(pots.getType().getName().toUpperCase()) && !p.hasPermission("redprotect.bypass")) {
                e.setCancelled(true);
                RPLang.sendMessage(p, "playerlistener.denypotion");
            }
        }
    }
    Region r = RedProtect.get().rm.getTopRegion(p.getLocation());
    if (r != null && stack.getItem().equals(ItemTypes.POTION) && !r.usePotions(p)) {
        RPLang.sendMessage(p, "playerlistener.region.cantuse");
        e.setCancelled(true);
    }
    if (r != null && stack.getItem().getName().equals("minecraft:chorus_fruit") && !r.canTeleport(p)) {
        RPLang.sendMessage(p, "playerlistener.region.cantuse");
        e.setCancelled(true);
    }
}
Also used : PotionEffect(org.spongepowered.api.effect.potion.PotionEffect) Region(br.net.fabiozumbi12.RedProtect.Sponge.Region) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Listener(org.spongepowered.api.event.Listener)

Example 17 with PotionEffect

use of org.spongepowered.api.effect.potion.PotionEffect in project LanternServer by LanternPowered.

the class PropertyProviders method applicableEffects.

public static PropertyProviderCollection applicableEffects(PotionEffect... potionEffects) {
    checkNotNull(potionEffects, "potionEffects");
    final ApplicableEffectProperty property = new ApplicableEffectProperty(ImmutableSet.copyOf(potionEffects));
    return PropertyProviderCollection.builder().add(ApplicableEffectProperty.class, (itemType, itemStack) -> property).build();
}
Also used : ArmorTypeProperty(org.spongepowered.api.data.property.item.ArmorTypeProperty) ApplicableEffectProperty(org.spongepowered.api.data.property.item.ApplicableEffectProperty) ImmutableSet(com.google.common.collect.ImmutableSet) ToolTypeProperty(org.spongepowered.api.data.property.item.ToolTypeProperty) MaximumUseDurationProperty(org.lanternpowered.server.item.property.MaximumUseDurationProperty) EquipmentType(org.spongepowered.api.item.inventory.equipment.EquipmentType) Collection(java.util.Collection) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) RecordProperty(org.spongepowered.api.data.property.item.RecordProperty) HealthRestorationProperty(org.lanternpowered.server.item.property.HealthRestorationProperty) MinimumUseDurationProperty(org.lanternpowered.server.item.property.MinimumUseDurationProperty) ArmorType(org.spongepowered.api.data.type.ArmorType) EquipmentProperty(org.spongepowered.api.data.property.item.EquipmentProperty) DualWieldProperty(org.lanternpowered.server.item.property.DualWieldProperty) FoodRestorationProperty(org.spongepowered.api.data.property.item.FoodRestorationProperty) SaturationProperty(org.spongepowered.api.data.property.item.SaturationProperty) UseLimitProperty(org.spongepowered.api.data.property.item.UseLimitProperty) PotionEffect(org.spongepowered.api.effect.potion.PotionEffect) RecordType(org.spongepowered.api.effect.sound.record.RecordType) AlwaysConsumableProperty(org.lanternpowered.server.item.property.AlwaysConsumableProperty) CooldownProperty(org.lanternpowered.server.item.property.CooldownProperty) ToolType(org.spongepowered.api.data.type.ToolType) ApplicableEffectProperty(org.spongepowered.api.data.property.item.ApplicableEffectProperty)

Example 18 with PotionEffect

use of org.spongepowered.api.effect.potion.PotionEffect in project SpongeCommon by SpongePowered.

the class ArrowData method register.

// @formatter:off
public static void register(final DataProviderRegistrator registrator) {
    registrator.asMutable(Arrow.class).create(Keys.POTION_EFFECTS).get(h -> {
        final Set<MobEffectInstance> effects = ((ArrowAccessor) h).accessor$effects();
        return effects.stream().map(effect -> (PotionEffect) new MobEffectInstance(effect.getEffect(), effect.getDuration(), effect.getAmplifier(), effect.isAmbient(), effect.isVisible())).collect(Collectors.toList());
    }).set((h, v) -> {
        ((ArrowAccessor) h).accessor$effects().clear();
        for (final PotionEffect effect : v) {
            final MobEffectInstance mcEffect = new MobEffectInstance(((MobEffectInstance) effect).getEffect(), (int) effect.duration().ticks(), effect.amplifier(), effect.isAmbient(), effect.showsParticles());
            h.addEffect(mcEffect);
        }
    });
}
Also used : Arrow(net.minecraft.world.entity.projectile.Arrow) Keys(org.spongepowered.api.data.Keys) Arrow(net.minecraft.world.entity.projectile.Arrow) DataProviderRegistrator(org.spongepowered.common.data.provider.DataProviderRegistrator) Set(java.util.Set) MobEffectInstance(net.minecraft.world.effect.MobEffectInstance) PotionEffect(org.spongepowered.api.effect.potion.PotionEffect) ArrowAccessor(org.spongepowered.common.accessor.world.entity.projectile.ArrowAccessor) Collectors(java.util.stream.Collectors) Set(java.util.Set) PotionEffect(org.spongepowered.api.effect.potion.PotionEffect) MobEffectInstance(net.minecraft.world.effect.MobEffectInstance)

Example 19 with PotionEffect

use of org.spongepowered.api.effect.potion.PotionEffect 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());
}
Also used : Items(net.minecraft.world.item.Items) DiggerItemAccessor(org.spongepowered.common.accessor.world.item.DiggerItemAccessor) Constants(org.spongepowered.common.util.Constants) StringTag(net.minecraft.nbt.StringTag) Item(net.minecraft.world.item.Item) SpongeAdventure(org.spongepowered.common.adventure.SpongeAdventure) PickaxeItem(net.minecraft.world.item.PickaxeItem) NestedTableEntry(org.spongepowered.api.util.weighted.NestedTableEntry) Registry(net.minecraft.core.Registry) PotionEffect(org.spongepowered.api.effect.potion.PotionEffect) LegacyComponentSerializer(net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer) FoodProperties(net.minecraft.world.food.FoodProperties) ImmutableSet(com.google.common.collect.ImmutableSet) ChanceTable(org.spongepowered.api.util.weighted.ChanceTable) Set(java.util.Set) MobEffectInstance(net.minecraft.world.effect.MobEffectInstance) SpongeCommon(org.spongepowered.common.SpongeCommon) Pair(com.mojang.datafixers.util.Pair) AbstractFurnaceBlockEntity(net.minecraft.world.level.block.entity.AbstractFurnaceBlockEntity) Keys(org.spongepowered.api.data.Keys) List(java.util.List) CompoundTag(net.minecraft.nbt.CompoundTag) ItemRarity(org.spongepowered.api.item.ItemRarity) NBTCollectors(org.spongepowered.common.util.NBTCollectors) DataProviderRegistrator(org.spongepowered.common.data.provider.DataProviderRegistrator) BlockType(org.spongepowered.api.block.BlockType) WeightedTable(org.spongepowered.api.util.weighted.WeightedTable) Block(net.minecraft.world.level.block.Block) ItemStack(net.minecraft.world.item.ItemStack) ItemType(org.spongepowered.api.item.ItemType) ListTag(net.minecraft.nbt.ListTag) PickaxeItem(net.minecraft.world.item.PickaxeItem) DiggerItemAccessor(org.spongepowered.common.accessor.world.item.DiggerItemAccessor) WeightedTable(org.spongepowered.api.util.weighted.WeightedTable) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) PotionEffect(org.spongepowered.api.effect.potion.PotionEffect) ItemType(org.spongepowered.api.item.ItemType) Item(net.minecraft.world.item.Item) PickaxeItem(net.minecraft.world.item.PickaxeItem) ChanceTable(org.spongepowered.api.util.weighted.ChanceTable) List(java.util.List) CompoundTag(net.minecraft.nbt.CompoundTag) Pair(com.mojang.datafixers.util.Pair) NestedTableEntry(org.spongepowered.api.util.weighted.NestedTableEntry) FoodProperties(net.minecraft.world.food.FoodProperties) MobEffectInstance(net.minecraft.world.effect.MobEffectInstance) ListTag(net.minecraft.nbt.ListTag) BlockType(org.spongepowered.api.block.BlockType) ItemStack(net.minecraft.world.item.ItemStack)

Example 20 with PotionEffect

use of org.spongepowered.api.effect.potion.PotionEffect in project SpongeCommon by SpongePowered.

the class LivingData method register.

// @formatter:off
public static void register(final DataProviderRegistrator registrator) {
    registrator.asMutable(LivingEntity.class).create(Keys.ABSORPTION).get(h -> (double) h.getAbsorptionAmount()).setAnd((h, v) -> {
        if (v < 0) {
            return false;
        }
        h.setAbsorptionAmount(v.floatValue());
        return true;
    }).create(Keys.ACTIVE_ITEM).get(h -> ItemStackUtil.snapshotOf(h.getUseItem())).setAnd((h, v) -> {
        if (v.isEmpty()) {
            h.releaseUsingItem();
            return true;
        }
        return false;
    }).delete(LivingEntity::releaseUsingItem).create(Keys.AUTO_SPIN_ATTACK_TICKS).get(h -> Ticks.of(((LivingEntityAccessor) h).accessor$autoSpinAttackTicks())).set((h, v) -> h.startAutoSpinAttack((int) v.ticks())).create(Keys.BODY_ROTATIONS).get(h -> {
        final double headYaw = h.getYHeadRot();
        final double pitch = h.xRot;
        final double yaw = h.yRot;
        return ImmutableMap.of(BodyParts.HEAD.get(), new Vector3d(pitch, headYaw, 0), BodyParts.CHEST.get(), new Vector3d(pitch, yaw, 0));
    }).set((h, v) -> {
        final Vector3d headRotation = v.get(BodyParts.HEAD.get());
        final Vector3d bodyRotation = v.get(BodyParts.CHEST.get());
        if (bodyRotation != null) {
            h.yRot = (float) bodyRotation.y();
            h.xRot = (float) bodyRotation.x();
        }
        if (headRotation != null) {
            h.yHeadRot = (float) headRotation.y();
            h.xRot = (float) headRotation.x();
        }
    }).create(Keys.CHEST_ROTATION).get(h -> new Vector3d(h.xRot, h.yRot, 0)).set((h, v) -> {
        final float yaw = (float) v.y();
        final float pitch = (float) v.x();
        h.yRot = yaw;
        h.xRot = pitch;
    }).create(Keys.HEAD_ROTATION).get(h -> new Vector3d(h.xRot, h.getYHeadRot(), 0)).set((h, v) -> {
        final float headYaw = (float) v.y();
        final float pitch = (float) v.x();
        h.yHeadRot = headYaw;
        h.xRot = pitch;
    }).create(Keys.HEALTH).get(h -> (double) h.getHealth()).setAnd((h, v) -> {
        final double maxHealth = h.getMaxHealth();
        // Check bounds
        if (v < 0 || v > maxHealth) {
            return false;
        }
        if (v == 0) {
            // Cause DestructEntityEvent to fire first
            h.hurt((DamageSource) SpongeDamageSources.IGNORED, Float.MAX_VALUE);
        }
        h.setHealth(v.floatValue());
        return true;
    }).create(Keys.IS_AUTO_SPIN_ATTACK).get(LivingEntity::isAutoSpinAttack).create(Keys.IS_ELYTRA_FLYING).get(LivingEntity::isFallFlying).set((h, v) -> ((EntityAccessor) h).invoker$setSharedFlag(Constants.Entity.ELYTRA_FLYING_FLAG, v)).create(Keys.LAST_ATTACKER).get(h -> (Entity) h.getLastHurtByMob()).setAnd((h, v) -> {
        if (v instanceof LivingEntity) {
            h.setLastHurtByMob((LivingEntity) v);
            return true;
        }
        return false;
    }).delete(h -> h.setLastHurtByMob(null)).create(Keys.MAX_HEALTH).get(h -> (double) h.getMaxHealth()).set((h, v) -> h.getAttribute(Attributes.MAX_HEALTH).setBaseValue(v)).create(Keys.POTION_EFFECTS).get(h -> {
        final Collection<MobEffectInstance> effects = h.getActiveEffects();
        return PotionEffectUtil.copyAsPotionEffects(effects);
    }).set((h, v) -> {
        h.removeAllEffects();
        for (final PotionEffect effect : v) {
            h.addEffect(PotionEffectUtil.copyAsEffectInstance(effect));
        }
    }).create(Keys.SCALE).get(h -> (double) h.getScale()).create(Keys.STUCK_ARROWS).get(LivingEntity::getArrowCount).setAnd((h, v) -> {
        if (v < 0 || v > Integer.MAX_VALUE) {
            return false;
        }
        h.setArrowCount(v);
        return true;
    }).create(Keys.WALKING_SPEED).get(h -> h.getAttribute(Attributes.MOVEMENT_SPEED).getValue()).setAnd((h, v) -> {
        if (v < 0) {
            return false;
        }
        h.getAttribute(Attributes.MOVEMENT_SPEED).setBaseValue(v);
        return true;
    }).asMutable(LivingEntityAccessor.class).create(Keys.LAST_DAMAGE_RECEIVED).get(h -> (double) h.accessor$lastHurt()).set((h, v) -> h.accessor$lastHurt(v.floatValue()));
}
Also used : LivingEntity(net.minecraft.world.entity.LivingEntity) Ticks(org.spongepowered.api.util.Ticks) LivingEntity(net.minecraft.world.entity.LivingEntity) PotionEffectUtil(org.spongepowered.common.util.PotionEffectUtil) ImmutableMap(com.google.common.collect.ImmutableMap) Constants(org.spongepowered.common.util.Constants) SpongeDamageSources(org.spongepowered.common.event.cause.entity.damage.SpongeDamageSources) BodyParts(org.spongepowered.api.data.type.BodyParts) Collection(java.util.Collection) LivingEntityAccessor(org.spongepowered.common.accessor.world.entity.LivingEntityAccessor) MobEffectInstance(net.minecraft.world.effect.MobEffectInstance) Entity(org.spongepowered.api.entity.Entity) Keys(org.spongepowered.api.data.Keys) ItemStackUtil(org.spongepowered.common.item.util.ItemStackUtil) Vector3d(org.spongepowered.math.vector.Vector3d) DataProviderRegistrator(org.spongepowered.common.data.provider.DataProviderRegistrator) DamageSource(net.minecraft.world.damagesource.DamageSource) Attributes(net.minecraft.world.entity.ai.attributes.Attributes) EntityAccessor(org.spongepowered.common.accessor.world.entity.EntityAccessor) PotionEffect(org.spongepowered.api.effect.potion.PotionEffect) LivingEntityBridge(org.spongepowered.common.bridge.world.entity.LivingEntityBridge) LivingEntityAccessor(org.spongepowered.common.accessor.world.entity.LivingEntityAccessor) DamageSource(net.minecraft.world.damagesource.DamageSource) Vector3d(org.spongepowered.math.vector.Vector3d) PotionEffect(org.spongepowered.api.effect.potion.PotionEffect) Collection(java.util.Collection)

Aggregations

PotionEffect (org.spongepowered.api.effect.potion.PotionEffect)37 Entity (org.spongepowered.api.entity.Entity)11 Player (org.spongepowered.api.entity.living.player.Player)11 ArrayList (java.util.ArrayList)6 Vector3d (com.flowpowered.math.vector.Vector3d)5 List (java.util.List)5 PotionEffectType (org.spongepowered.api.effect.potion.PotionEffectType)5 Region (br.net.fabiozumbi12.RedProtect.Sponge.Region)4 MobEffectInstance (net.minecraft.world.effect.MobEffectInstance)4 ImmutableSet (com.google.common.collect.ImmutableSet)3 IntegratedRunnable (com.skelril.nitro.time.IntegratedRunnable)3 TimedRunnable (com.skelril.nitro.time.TimedRunnable)3 Collection (java.util.Collection)3 HashMap (java.util.HashMap)3 Set (java.util.Set)3 BlockType (org.spongepowered.api.block.BlockType)3 Keys (org.spongepowered.api.data.Keys)3 Keys (org.spongepowered.api.data.key.Keys)3 Listener (org.spongepowered.api.event.Listener)3 Cause (org.spongepowered.api.event.cause.Cause)3