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);
}
}
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();
}
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);
}
});
}
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());
}
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()));
}
Aggregations