org.bukkit.attribute.AttributeModifier Java Examples

The following examples show how to use org.bukkit.attribute.AttributeModifier. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: XMLUtils.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
public static Pair<org.bukkit.attribute.Attribute, AttributeModifier> parseAttributeModifier(Element el) throws InvalidXMLException {
    return Pair.create(
        parseAttribute(new Node(el)),
        new AttributeModifier(
            "FromXML",
            parseNumber(Node.fromRequiredAttr(el, "amount"), Double.class),
            parseAttributeOperation(Node.fromAttr(el, "operation"), AttributeModifier.Operation.ADD_NUMBER)
        )
    );
}
 
Example #2
Source File: Parser.java    From CardinalPGM with MIT License 5 votes vote down vote up
public static AttributeModifier.Operation getOperation(String operation) {
    if (NumberUtils.isNumber(operation)) {
        return AttributeModifier.Operation.fromOpcode(Integer.parseInt(operation));
    } else {
        switch (operation.toLowerCase()) {
            case("add"):
                return AttributeModifier.Operation.ADD_NUMBER;
            case("base"):
                return AttributeModifier.Operation.ADD_SCALAR;
            case("multiply"):
                return AttributeModifier.Operation.MULTIPLY_SCALAR_1;
        }
    }
    return AttributeModifier.Operation.ADD_NUMBER;
}
 
Example #3
Source File: Parser.java    From CardinalPGM with MIT License 5 votes vote down vote up
private static List<ItemAttributeModifier> parseAttributes(String attributes) {
    List<ItemAttributeModifier> list = new ArrayList<>();
    for (String attribute : attributes.split(";")) {
        String[] attr = attribute.split(":");
        list.add(new ItemAttributeModifier(null, new AttributeModifier(UUID.randomUUID(), attr[0], Double.parseDouble(attr[2]), getOperation(attr[1]))));
    }
    return list;
}
 
Example #4
Source File: Players.java    From CardinalPGM with MIT License 5 votes vote down vote up
public static void resetPlayer(Player player, boolean heal) {
    if (heal) player.setHealth(player.getMaxHealth());
    player.setFoodLevel(20);
    player.setSaturation(20);
    player.getInventory().clear();
    player.getInventory().setArmorContents(new ItemStack[]{new ItemStack(Material.AIR), new ItemStack(Material.AIR), new ItemStack(Material.AIR), new ItemStack(Material.AIR)});
    for (PotionEffect effect : player.getActivePotionEffects()) {
        try {
            player.removePotionEffect(effect.getType());
        } catch (NullPointerException ignored) {
        }
    }
    player.setTotalExperience(0);
    player.setExp(0);
    player.setLevel(0);
    player.setPotionParticles(false);
    player.setWalkSpeed(0.2F);
    player.setFlySpeed(0.1F);
    player.setKnockbackReduction(0);
    player.setArrowsStuck(0);

    player.hideTitle();

    player.setFastNaturalRegeneration(false);

    for (Attribute attribute : Attribute.values()) {
        if (player.getAttribute(attribute) == null) continue;
        for (AttributeModifier modifier : player.getAttribute(attribute).getModifiers()) {
            player.getAttribute(attribute).removeModifier(modifier);
        }
    }
    player.getAttribute(Attribute.GENERIC_ATTACK_SPEED).addModifier(new AttributeModifier(UUID.randomUUID(), "generic.attackSpeed", 4.001D, AttributeModifier.Operation.ADD_SCALAR));
    player.getAttribute(Attribute.ARROW_ACCURACY).addModifier(new AttributeModifier(UUID.randomUUID(), "sportbukkit.arrowAccuracy", -1D, AttributeModifier.Operation.ADD_NUMBER));
    player.getAttribute(Attribute.ARROW_VELOCITY_TRANSFER).addModifier(new AttributeModifier(UUID.randomUUID(), "sportbukkit.arrowVelocityTransfer", -1D, AttributeModifier.Operation.ADD_NUMBER));
}
 
Example #5
Source File: VersionUtils_1_13.java    From UhcCore with GNU General Public License v3.0 5 votes vote down vote up
@Override
public ItemMeta applyItemAttributes(ItemMeta meta, JsonObject attributes){
    Set<Map.Entry<String, JsonElement>> entries = attributes.entrySet();

    for (Map.Entry<String, JsonElement> attributeEntry : entries){
        Attribute attribute = Attribute.valueOf(attributeEntry.getKey());

        for (JsonElement jsonElement : attributeEntry.getValue().getAsJsonArray()) {
            JsonObject modifier = jsonElement.getAsJsonObject();

            String name = modifier.get("name").getAsString();
            double amount = modifier.get("amount").getAsDouble();
            String operation = modifier.get("operation").getAsString();
            EquipmentSlot slot = null;

            if (modifier.has("slot")){
                slot = EquipmentSlot.valueOf(modifier.get("slot").getAsString());
            }

            meta.addAttributeModifier(attribute, new AttributeModifier(
                    UUID.randomUUID(),
                    name,
                    amount,
                    AttributeModifier.Operation.valueOf(operation),
                    slot
            ));
        }
    }

    return meta;
}
 
Example #6
Source File: VersionUtils_1_13.java    From UhcCore with GNU General Public License v3.0 5 votes vote down vote up
@Nullable
@Override
public JsonObject getItemAttributes(ItemMeta meta){
    if (!meta.hasAttributeModifiers()){
        return null;
    }

    JsonObject attributesJson = new JsonObject();
    Multimap<Attribute, AttributeModifier> attributeModifiers = meta.getAttributeModifiers();

    for (Attribute attribute : attributeModifiers.keySet()){
        JsonArray modifiersJson = new JsonArray();
        Collection<AttributeModifier> modifiers = attributeModifiers.get(attribute);

        for (AttributeModifier modifier : modifiers){
            JsonObject modifierObject = new JsonObject();
            modifierObject.addProperty("name", modifier.getName());
            modifierObject.addProperty("amount", modifier.getAmount());
            modifierObject.addProperty("operation", modifier.getOperation().name());
            if (modifier.getSlot() != null){
                modifierObject.addProperty("slot", modifier.getSlot().name());
            }
            modifiersJson.add(modifierObject);
        }

        attributesJson.add(attribute.name(), modifiersJson);
    }

    return attributesJson;
}
 
Example #7
Source File: AttributePlayerFacet.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
private boolean removeModifier0(Attribute attribute, AttributeModifier modifier) {
    AttributeInstance attributeValue = player.getAttribute(attribute);
    if(attributeValue != null && attributeValue.getModifiers().contains(modifier)) {
        attributeValue.removeModifier(modifier);
        return true;
    }
    return false;
}
 
Example #8
Source File: AttributePlayerFacet.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
private boolean addModifier0(Attribute attribute, AttributeModifier modifier) {
    final AttributeInstance attributeInstance = player.getAttribute(attribute);
    if(attributeInstance != null && !attributeInstance.getModifiers().contains(modifier)) {
        attributeInstance.addModifier(modifier);
        return true;
    }
    return false;
}
 
Example #9
Source File: XMLUtils.java    From PGM with GNU Affero General Public License v3.0 5 votes vote down vote up
public static AttributeModifier.Operation parseAttributeOperation(Node node, String text)
    throws InvalidXMLException {
  switch (text.toLowerCase()) {
    case "add":
      return AttributeModifier.Operation.ADD_NUMBER;
    case "base":
      return AttributeModifier.Operation.ADD_SCALAR;
    case "multiply":
      return AttributeModifier.Operation.MULTIPLY_SCALAR_1;
  }
  throw new InvalidXMLException("Unknown attribute modifier operation '" + text + "'", node);
}
 
Example #10
Source File: XMLUtils.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
public static Pair<org.bukkit.attribute.Attribute, AttributeModifier> parseCompactAttributeModifier(Node node, String text) throws InvalidXMLException {
    final String[] parts = text.split(":");
    if(parts.length != 3) {
        throw new InvalidXMLException("Bad attribute modifier format", node);
    }

    return Pair.create(
        parseAttribute(node, parts[0]),
        new AttributeModifier(
            "FromXML",
            parseNumber(node, parts[2], Double.class),
            parseAttributeOperation(node, parts[1])
        )
    );
}
 
Example #11
Source File: XMLUtils.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
public static AttributeModifier.Operation parseAttributeOperation(Node node, String text) throws InvalidXMLException {
    switch(text.toLowerCase()) {
        case "add": return AttributeModifier.Operation.ADD_NUMBER;
        case "base": return AttributeModifier.Operation.ADD_SCALAR;
        case "multiply": return AttributeModifier.Operation.MULTIPLY_SCALAR_1;
    }
    throw new InvalidXMLException("Unknown attribute modifier operation '" + text + "'", node);
}
 
Example #12
Source File: CraftAttributeInstance.java    From Kettle with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Collection<AttributeModifier> getModifiers() {
    List<AttributeModifier> result = new ArrayList<AttributeModifier>();
    for (net.minecraft.entity.ai.attributes.AttributeModifier nms : handle.getModifiers()) {
        result.add(convert(nms));
    }

    return result;
}
 
Example #13
Source File: Speedy.java    From MineTinker with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean applyMod(Player player, ItemStack tool, boolean isCommand) {
	ItemMeta meta = tool.getItemMeta();

	if (meta == null) {
		return false;
	}

	//To check if armor modifiers are on the armor
	Collection<AttributeModifier> attributeModifiers = meta.getAttributeModifiers(Attribute.GENERIC_ARMOR);

	if (attributeModifiers == null || attributeModifiers.isEmpty()) {
		modManager.addArmorAttributes(tool);
		meta = tool.getItemMeta();
	}

	Collection<AttributeModifier> speedModifiers = meta.getAttributeModifiers(Attribute.GENERIC_MOVEMENT_SPEED);
	double speedOnItem = 0.0D;

	if (!(speedModifiers == null || speedModifiers.isEmpty())) {
		HashSet<String> names = new HashSet<>();

		for (AttributeModifier am : speedModifiers) {
			if (names.add(am.getName())) speedOnItem += am.getAmount();
		}
	}

	meta.removeAttributeModifier(Attribute.GENERIC_MOVEMENT_SPEED);
	meta.addAttributeModifier(Attribute.GENERIC_MOVEMENT_SPEED,
			new AttributeModifier(UUID.randomUUID(), (MineTinker.is16compatible) ? "generic.movement_speed" : "generic.movementSpeed", speedOnItem + this.speedPerLevel, AttributeModifier.Operation.ADD_NUMBER, EquipmentSlot.LEGS));
	meta.addAttributeModifier(Attribute.GENERIC_MOVEMENT_SPEED,
			new AttributeModifier(UUID.randomUUID(), (MineTinker.is16compatible) ? "generic.movement_speed" : "generic.movementSpeed", speedOnItem + this.speedPerLevel, AttributeModifier.Operation.ADD_NUMBER, EquipmentSlot.FEET));

	tool.setItemMeta(meta);
	return true;
}
 
Example #14
Source File: Tanky.java    From MineTinker with GNU General Public License v3.0 5 votes vote down vote up
@Override
public boolean applyMod(Player player, ItemStack tool, boolean isCommand) {
	ItemMeta meta = tool.getItemMeta();

	if (meta == null) {
		return false;
	}

	//To check if armor modifiers are on the armor
	Collection<AttributeModifier> attributeModifiers = meta.getAttributeModifiers(Attribute.GENERIC_ARMOR);

	if (attributeModifiers == null || attributeModifiers.isEmpty()) {
		modManager.addArmorAttributes(tool);
		meta = tool.getItemMeta();
	}

	Collection<AttributeModifier> healthModifiers = meta.getAttributeModifiers(Attribute.GENERIC_MAX_HEALTH);

	double healthOnItem = 0.0D;
	if (!(healthModifiers == null || healthModifiers.isEmpty())) {
		HashSet<String> names = new HashSet<>();
		for (AttributeModifier am : healthModifiers) {
			if (names.add(am.getName())) healthOnItem += am.getAmount();
		}
	}
	meta.removeAttributeModifier(Attribute.GENERIC_MAX_HEALTH);
	modManager.addArmorAttributes(tool);
	if (ToolType.CHESTPLATE.contains(tool.getType())) {
		meta.addAttributeModifier(Attribute.GENERIC_MAX_HEALTH, new AttributeModifier(UUID.randomUUID(), (MineTinker.is16compatible) ? "generic.max_health" : "generic.maxHealth", healthOnItem + this.healthPerLevel, AttributeModifier.Operation.ADD_NUMBER, EquipmentSlot.CHEST));
	} else {
		meta.addAttributeModifier(Attribute.GENERIC_MAX_HEALTH, new AttributeModifier(UUID.randomUUID(), (MineTinker.is16compatible) ? "generic.max_health" : "generic.maxHealth", healthOnItem + this.healthPerLevel, AttributeModifier.Operation.ADD_NUMBER, EquipmentSlot.LEGS));
	}

	tool.setItemMeta(meta);
	return true;
}
 
Example #15
Source File: XMLUtils.java    From PGM with GNU Affero General Public License v3.0 5 votes vote down vote up
public static Map.Entry<String, AttributeModifier> parseAttributeModifier(Element el)
    throws InvalidXMLException {
  String attribute = parseAttribute(new Node(el)).getName();
  double amount = parseNumber(Node.fromRequiredAttr(el, "amount"), Double.class);
  AttributeModifier.Operation operation =
      parseAttributeOperation(
          Node.fromAttr(el, "operation"), AttributeModifier.Operation.ADD_NUMBER);

  return new AbstractMap.SimpleImmutableEntry<>(
      attribute, new AttributeModifier("FromXML", amount, operation));
}
 
Example #16
Source File: XMLUtils.java    From PGM with GNU Affero General Public License v3.0 5 votes vote down vote up
public static Map.Entry<String, AttributeModifier> parseCompactAttributeModifier(
    Node node, String text) throws InvalidXMLException {
  String[] parts = text.split(":");

  if (parts.length != 3) {
    throw new InvalidXMLException("Bad attribute modifier format", node);
  }

  org.bukkit.attribute.Attribute attribute = parseAttribute(node, parts[0]);
  AttributeModifier.Operation operation = parseAttributeOperation(node, parts[1]);
  double amount = parseNumber(node, parts[2], Double.class);

  return new AbstractMap.SimpleImmutableEntry<>(
      attribute.getName(), new AttributeModifier("FromXML", amount, operation));
}
 
Example #17
Source File: AttributeKit.java    From PGM with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
protected void applyPostEvent(MatchPlayer player, boolean force, List<ItemStack> displacedItems) {
  for (Map.Entry<String, AttributeModifier> entry : modifiers.entries()) {
    AttributeInstance attributeValue =
        player.getBukkit().getAttribute(Attribute.byName(entry.getKey()));
    if (attributeValue != null && !attributeValue.getModifiers().contains(entry.getValue())) {
      attributeValue.addModifier(entry.getValue());
    }
  }
}
 
Example #18
Source File: AttributeKit.java    From PGM with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
public void remove(MatchPlayer player) {
  for (Map.Entry<String, AttributeModifier> entry : modifiers.entries()) {
    AttributeInstance attributeValue =
        player.getBukkit().getAttribute(Attribute.byName(entry.getKey()));
    if (attributeValue != null && attributeValue.getModifiers().contains(entry.getValue())) {
      attributeValue.removeModifier(entry.getValue());
    }
  }
}
 
Example #19
Source File: MatchPlayerImpl.java    From PGM with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public void reset() {
  getMatch().callEvent(new PlayerResetEvent(this));

  setFrozen(false);
  Player bukkit = getBukkit();
  bukkit.closeInventory();
  resetInventory();
  bukkit.setArrowsStuck(0);
  bukkit.setExhaustion(0);
  bukkit.setFallDistance(0);
  bukkit.setFireTicks(0);
  bukkit.setFoodLevel(20); // full
  bukkit.setHealth(bukkit.getMaxHealth());
  bukkit.setLevel(0);
  bukkit.setExp(0); // clear xp
  bukkit.setSaturation(5); // default
  bukkit.setAllowFlight(false);
  bukkit.setFlying(false);
  bukkit.setSneaking(false);
  bukkit.setSprinting(false);
  bukkit.setFlySpeed(0.1f);
  bukkit.setKnockbackReduction(0);
  bukkit.setWalkSpeed(WalkSpeedKit.BUKKIT_DEFAULT);

  for (PotionEffect effect : bukkit.getActivePotionEffects()) {
    if (effect.getType() != null) {
      bukkit.removePotionEffect(effect.getType());
    }
  }

  for (Attribute attribute : ATTRIBUTES) {
    AttributeInstance attributes = bukkit.getAttribute(attribute);
    if (attributes == null) continue;

    for (AttributeModifier modifier : attributes.getModifiers()) {
      attributes.removeModifier(modifier);
    }
  }

  NMSHacks.setAbsorption(bukkit, 0);

  // we only reset bed spawn here so people don't have to see annoying messages when they respawn
  bukkit.setBedSpawnLocation(null);
}
 
Example #20
Source File: XMLUtils.java    From PGM with GNU Affero General Public License v3.0 4 votes vote down vote up
public static AttributeModifier.Operation parseAttributeOperation(Node node)
    throws InvalidXMLException {
  return parseAttributeOperation(node, node.getValueNormalize());
}
 
Example #21
Source File: ItemMetaImpl.java    From Civs with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Multimap<Attribute, AttributeModifier> getAttributeModifiers() {
    return null;
}
 
Example #22
Source File: ItemMetaImpl.java    From Civs with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Multimap<Attribute, AttributeModifier> getAttributeModifiers(EquipmentSlot equipmentSlot) {
    return null;
}
 
Example #23
Source File: ItemMetaImpl.java    From Civs with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Collection<AttributeModifier> getAttributeModifiers(Attribute attribute) {
    return null;
}
 
Example #24
Source File: ItemMetaImpl.java    From Civs with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean addAttributeModifier(Attribute attribute, AttributeModifier attributeModifier) {
    return false;
}
 
Example #25
Source File: ItemMetaImpl.java    From Civs with GNU General Public License v3.0 4 votes vote down vote up
@Override
public boolean removeAttributeModifier(Attribute attribute, AttributeModifier attributeModifier) {
    return false;
}
 
Example #26
Source File: AttributeModifierKit.java    From CardinalPGM with MIT License 4 votes vote down vote up
public AttributeModifierKit(List<AttributeModifier> attributes) {
    this.attributes = attributes;
}
 
Example #27
Source File: AttributeModifierKit.java    From CardinalPGM with MIT License 4 votes vote down vote up
@Override
public void apply(Player player, Boolean force) {
    for (AttributeModifier modifier : attributes) {
        player.getAttribute(Attribute.byName(modifier.getName())).addModifier(modifier);
    }
}
 
Example #28
Source File: AttributeModifierKit.java    From CardinalPGM with MIT License 4 votes vote down vote up
@Override
public void remove(Player player) {
    for (AttributeModifier modifier : attributes) {
        player.getAttribute(Attribute.byName(modifier.getName())).removeModifier(modifier);
    }
}
 
Example #29
Source File: XMLUtils.java    From PGM with GNU Affero General Public License v3.0 4 votes vote down vote up
public static AttributeModifier.Operation parseAttributeOperation(
    Node node, AttributeModifier.Operation def) throws InvalidXMLException {
  return node == null ? def : parseAttributeOperation(node);
}
 
Example #30
Source File: Parser.java    From CardinalPGM with MIT License 4 votes vote down vote up
public static ItemAttributeModifier getAttribute(Element attribute) {
    return new ItemAttributeModifier(getEquipmentSlot(attribute.getAttributeValue("slot", "")),
            new AttributeModifier(UUID.randomUUID(), attribute.getText(), Double.parseDouble(attribute.getAttributeValue("amount", "0.0")), getOperation(attribute.getAttributeValue("operation", "add"))));
}