/** * Remove an attribute from this property. * * @param attributeModifier to remove from this property */ @Override public void removeModifier(AttributeModifier attributeModifier) { this.modifiers.remove(attributeModifier.getUniqueId()); onMutation(); }
private double computeValue() { double result = getBaseValue(); for (AttributeModifier modifier : modifiers.values()) { if (modifier.getOperation() == AttributeModifier.Operation.ADD_NUMBER) { result += modifier.getAmount(); } } double multiplier = 1.0; for (AttributeModifier modifier : modifiers.values()) { if (modifier.getOperation() == AttributeModifier.Operation.ADD_SCALAR) { multiplier += modifier.getAmount(); } } result *= multiplier; for (AttributeModifier modifier : modifiers.values()) { if (modifier.getOperation() == AttributeModifier.Operation.MULTIPLY_SCALAR_1) { result *= 1.0 + modifier.getAmount(); } } result = Math.max(result, key.min); result = Math.min(result, key.max); return result; }
@Override public ByteBuf encode(ByteBuf buf, EntityPropertyMessage message) throws IOException { ByteBufUtils.writeVarInt(buf, message.getId()); Map<String, Property> props = message.getProperties(); buf.writeInt(props.size()); for (Entry<String, Property> property : props.entrySet()) { ByteBufUtils.writeUTF8(buf, property.getKey()); buf.writeDouble(property.getValue().getValue()); Collection<AttributeModifier> modifiers = property.getValue().getModifiers(); if (modifiers == null) { ByteBufUtils.writeVarInt(buf, 0); } else { ByteBufUtils.writeVarInt(buf, modifiers.size()); for (AttributeModifier modifier : modifiers) { GlowBufUtils.writeUuid(buf, modifier.getUniqueId()); buf.writeDouble(modifier.getAmount()); buf.writeByte(modifier.getOperation().ordinal()); } } } return buf; } }
@EventHandler(priority = EventPriority.HIGHEST) public void onPlayerQuit(PlayerQuitEvent e){ Player player = e.getPlayer(); AttributeInstance attribute = player.getAttribute(Attribute.GENERIC_ATTACK_SPEED); double baseValue = attribute.getBaseValue(); if(baseValue != 4){ //If basevalue is not 1.9 default, set it back attribute.setBaseValue(4); player.saveData(); } }
/** * Heals an entity by a specific amount. * * @param target the entity to heal * @param amount the amount of health to regain * @param reason the reason supplied to the {@link EntityRegainHealthEvent} */ public static void heal(LivingEntity target, double amount, EntityRegainHealthEvent.RegainReason reason) { if (target.isDead()) { return; // too late! } final double maxHealth = target.getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue(); final double currentHealth = target.getHealth(); if (currentHealth >= maxHealth) { return; } EntityRegainHealthEvent event = new EntityRegainHealthEvent(target, amount, reason); EventFactory.getInstance().callEvent(event); if (!event.isCancelled()) { target.setHealth(Math.min(maxHealth, currentHealth + event.getAmount())); } }
@Override public double getSpeedAttributeMultiplier(final Player player) { final AttributeInstance attrInst = player.getAttribute(Attribute.GENERIC_MOVEMENT_SPEED); final double val = attrInst.getValue() / attrInst.getBaseValue(); final AttributeModifier mod = getModifier(attrInst, AttribUtil.ID_SPRINT_BOOST); return mod == null ? val : (val / getMultiplier(mod)); }
@Override public void setMaxHealth(double health) { getAttribute(Attribute.GENERIC_MAX_HEALTH).setBaseValue(health); if (this.health > health) { this.health = health; } }
&& modifierTag.isLong("UUIDLeast") && modifierTag.isLong("UUIDMost")) { modifiers.add(new AttributeModifier( new UUID(modifierTag.getLong("UUIDLeast"), modifierTag.getLong("UUIDMost")), modifierTag.getString("Name"), modifierTag.getDouble("Amount"), AttributeModifier.Operation.values()[modifierTag.getInt("Operation")]));
public static PVPMode getPVPMode(Player player){ Objects.requireNonNull(player, "player cannot be null!"); return player.getAttribute(Attribute.GENERIC_ATTACK_SPEED).getBaseValue() == 4 ? PVPMode.NEW_PVP : PVPMode.OLD_PVP; }
/** * The first modifier with the given id that can be found, or null if none * is found. * * @param attrInst * @param id * @return */ private AttributeModifier getModifier(final AttributeInstance attrInst, final UUID id) { for (final AttributeModifier mod : attrInst.getModifiers()) { if (id.equals(mod.getUniqueId())) { return mod; } } return null; }
private static String toMinecraftAttribute(Attribute attribute) { String bukkit = attribute.name(); int first = bukkit.indexOf('_'); int second = bukkit.indexOf('_', first + 1); StringBuilder sb = new StringBuilder(bukkit.toLowerCase(java.util.Locale.ENGLISH)); sb.setCharAt(first, '.'); if (second != -1) { sb.deleteCharAt(second); sb.setCharAt(second, bukkit.charAt(second + 1)); } return sb.toString(); }
@Test public void getAttribute_HealthAttribute_IsMaximumHealth() { assertEquals(20.0, player.getAttribute(Attribute.GENERIC_MAX_HEALTH).getDefaultValue(), 0); }
private void checkAttackSpeed(Player player){ World world = player.getWorld(); //If module is disabled, set attack speed to 1.9 default double attackSpeed = Config.moduleEnabled("disable-attack-cooldown", world) ? module().getDouble("generic-attack-speed") : 4; AttributeInstance attribute = player.getAttribute(Attribute.GENERIC_ATTACK_SPEED); double baseValue = attribute.getBaseValue(); if(baseValue != attackSpeed){ attribute.setBaseValue(attackSpeed); player.saveData(); } }
@Override public double getMaxHealth() { return getAttribute(Attribute.GENERIC_MAX_HEALTH).getValue(); }
List<CompoundTag> modifierTags = modifiers.stream().map(modifier -> { CompoundTag modifierTag = new CompoundTag(); modifierTag.putDouble("Amount", modifier.getAmount()); modifierTag.putString("Name", modifier.getName()); modifierTag.putInt("Operation", modifier.getOperation().ordinal()); UUID uuid = modifier.getUniqueId(); modifierTag.putLong("UUIDLeast", uuid.getLeastSignificantBits()); modifierTag.putLong("UUIDMost", uuid.getMostSignificantBits());
public void applyAttributes(LivingEntity entity) { if (attributes != null) { for (Map.Entry<Attribute, Double> entry : attributes.entrySet()) { AttributeInstance attribute = entity.getAttribute(entry.getKey()); if (attribute != null) { attribute.setBaseValue(entry.getValue()); } } } }
private double getMultiplier(final AttributeModifier mod) { return AttribUtil.getMultiplier(operationToInt(mod.getOperation()), mod.getAmount()); }
/** * Add a modifier to this property. * * <p> * Attributes with the same uuid will be overridden according to * https://minecraft.gamepedia.com/Attribute#Attributes * </p> * * @param attributeModifier to add to this property */ @Override public void addModifier(AttributeModifier attributeModifier) { this.modifiers.put(attributeModifier.getUniqueId(), attributeModifier); onMutation(); }
@Override public PowerResult<Double> hit(Player player, ItemStack stack, LivingEntity entity, double damage, EntityDamageByEntityEvent event) { if (!player.isSprinting()) return PowerResult.noop(); double originDamage = damage; damage = damage * (1 + percentage / 100.0); damage = damage + damage * (player.getAttribute(Attribute.GENERIC_MOVEMENT_SPEED).getValue() / 0.13 - 1) * (speedPercentage / 100.0); damage = Math.max(Math.min(damage, cap), originDamage); event.setDamage(damage); if (damage > originDamage) { player.playSound(player.getLocation(), Sound.BLOCK_ANVIL_LAND, 0.1f, 0.1f); } return PowerResult.ok(damage); } }
@EventHandler (priority = EventPriority.HIGHEST) public static void onEntityDamage(EntityDamageEvent e){ Entity entity = e.getEntity(); if(!(entity instanceof LivingEntity)) return; LivingEntity le = (LivingEntity) entity; double armourDamageReduction = e.getDamage(EntityDamageEvent.DamageModifier.ARMOR); double armourPoints = le.getAttribute(Attribute.GENERIC_ARMOR).getValue(); double newReduction = armourPoints * 0.04 * - (e.getDamage() + e.getDamage(EntityDamageEvent.DamageModifier.BLOCKING)); //todo don't hardcode this value e.setDamage (EntityDamageEvent.DamageModifier.ARMOR, newReduction); INSTANCE.debug("Armour points: " + armourPoints + " Reduction: " + armourDamageReduction + " After: " + newReduction, le); }