org.bukkit.attribute.AttributeInstance Java Examples

The following examples show how to use org.bukkit.attribute.AttributeInstance. 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: PWIPlayerManagerTest.java    From PerWorldInventory with GNU General Public License v3.0 6 votes vote down vote up
private Player mockPlayer(String name, GameMode gameMode) {
    Player mock = mock(Player.class);
    PlayerInventory inv = mock(PlayerInventory.class);
    inv.setContents(new ItemStack[39]);
    inv.setArmorContents(new ItemStack[4]);

    Inventory enderChest = mock(Inventory.class);
    enderChest.setContents(new ItemStack[27]);

    given(mock.getInventory()).willReturn(inv);
    given(mock.getEnderChest()).willReturn(enderChest);
    given(mock.getName()).willReturn(name);
    given(mock.getUniqueId()).willReturn(TestHelper.TEST_UUID);
    given(mock.getGameMode()).willReturn(gameMode);
    AttributeInstance attribute = mock(AttributeInstance.class);
    given(mock.getAttribute(Attribute.GENERIC_MAX_HEALTH)).willReturn(attribute);
    given(attribute.getBaseValue()).willReturn(20.0);

    return mock;
}
 
Example #2
Source File: PWIPlayerManagerTest.java    From PerWorldInventory with GNU General Public License v3.0 6 votes vote down vote up
private Player mockPlayer(String name, GameMode gameMode) {
    Player mock = mock(Player.class);
    PlayerInventory inv = mock(PlayerInventory.class);
    inv.setContents(new ItemStack[39]);
    inv.setArmorContents(new ItemStack[4]);

    Inventory enderChest = mock(Inventory.class);
    enderChest.setContents(new ItemStack[27]);

    given(mock.getInventory()).willReturn(inv);
    given(mock.getEnderChest()).willReturn(enderChest);
    given(mock.getName()).willReturn(name);
    given(mock.getUniqueId()).willReturn(TestHelper.TEST_UUID);
    given(mock.getGameMode()).willReturn(gameMode);
    AttributeInstance attribute = mock(AttributeInstance.class);
    given(mock.getAttribute(Attribute.GENERIC_MAX_HEALTH)).willReturn(attribute);
    given(attribute.getBaseValue()).willReturn(20.0);

    return mock;
}
 
Example #3
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 #4
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 #5
Source File: Berserk.java    From MineTinker with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler(priority = EventPriority.HIGHEST)
public void onHit(EntityDamageEvent event) {
	if (!(event.getEntity() instanceof Player)) {
		return;
	}

	Player player = (Player) event.getEntity();

	if (!player.hasPermission("minetinker.modifiers.berserk.use")) {
		return;
	}

	ItemStack chest = player.getInventory().getChestplate();

	if (!modManager.isArmorViable(chest)) {
		return;
	}

	int modifierLevel = modManager.getModLevel(chest, this);

	if (modifierLevel <= 0) {
		return;
	}

	double lifeAfterDamage = player.getHealth() - event.getFinalDamage();
	AttributeInstance healthAttr = player.getAttribute(Attribute.GENERIC_MAX_HEALTH);

	double maxHealth = 20;

	if (healthAttr != null) {
		maxHealth = healthAttr.getValue();
	}

	if (player.getHealth() / maxHealth > trigger / 100.0 && lifeAfterDamage / maxHealth <= trigger / 100.0) {
		player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, boostTime, modifierLevel - 1));
		ChatWriter.logModifier(player, event, this, chest,
				"Time(" + boostTime + ")", "Amplifier(" + (modifierLevel - 1) + ")");
	}
}
 
Example #6
Source File: CraftAttributeMap.java    From Kettle with GNU General Public License v3.0 5 votes vote down vote up
@Override
public AttributeInstance getAttribute(Attribute attribute) {
    Preconditions.checkArgument(attribute != null, "attribute");
    net.minecraft.entity.ai.attributes.IAttributeInstance nms = handle.getAttributeInstanceByName(toMinecraft(attribute.name()));

    return (nms == null) ? null : new CraftAttributeInstance(nms, attribute);
}
 
Example #7
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 #8
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 #9
Source File: KeepHealth.java    From HubBasics with GNU Lesser General Public License v3.0 5 votes vote down vote up
@EventHandler
public void onDamage(EntityDamageEvent event) {
    if (event.getEntityType() != EntityType.PLAYER) return;
    String world = event.getEntity().getWorld().getName().toLowerCase();
    Player player = (Player) event.getEntity();
    if (this.worldHealthMap.containsKey(world)) {
        player.setHealth(this.worldHealthMap.get(world));
        AttributeInstance attribute = player.getAttribute(Attribute.GENERIC_MAX_HEALTH);
        if (attribute != null) {
            attribute.setBaseValue(this.worldMaxHealthMap.get(world));
        }
        event.setCancelled(true);
    }
}
 
Example #10
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 #11
Source File: Lifesteal.java    From MineTinker with GNU General Public License v3.0 4 votes vote down vote up
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGH) //because of Melting
public void effect(MTEntityDamageByEntityEvent event) {
	if (event.getPlayer().equals(event.getEvent().getEntity())) {
		return; //when event was triggered by the armor
	}

	Player player = event.getPlayer();
	ItemStack tool = event.getTool();

	if (!player.hasPermission("minetinker.modifiers.lifesteal.use")) {
		return;
	}

	if (!modManager.hasMod(tool, this)) {
		return;
	}

	Random rand = new Random();
	int n = rand.nextInt(100);

	if (n > this.percentToTrigger) {
		ChatWriter.logModifier(player, event, this, tool, String.format("Chance(%d/%d)", n, this.percentToTrigger));
		return;
	}

	int level = modManager.getModLevel(tool, this);
	double damage = event.getEvent().getDamage();
	double recovery = damage * ((percentPerLevel * level) / 100.0);
	double health = player.getHealth() + recovery;

	AttributeInstance attribute = player.getAttribute(Attribute.GENERIC_MAX_HEALTH);

	if (attribute != null) {
		// for IllegalArgumentExeption if Health is biggen than MaxHealth
		if (health > attribute.getValue()) {
			health = attribute.getValue();
		}

		player.setHealth(health);
	}

	ChatWriter.logModifier(player, event, this, tool, String.format("Chance(%d/%d)", n, this.percentToTrigger),
			String.format("HealthGain(%.2f [%.2f/%.2f = %.4f])", recovery, recovery, damage, recovery/damage));
}
 
Example #12
Source File: CraftLivingEntity.java    From Kettle with GNU General Public License v3.0 4 votes vote down vote up
@Override
public AttributeInstance getAttribute(Attribute attribute) {
    return getHandle().craftAttributes.getAttribute(attribute);
}
 
Example #13
Source File: AttributeUtils.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
public static void removeModifier(Attributable attributable, AttributeModifier modifier) {
    for(Attribute attribute : Attribute.values()) {
        final AttributeInstance instance = attributable.getAttribute(attribute);
        if(instance != null) instance.removeModifier(modifier);
    }
}
 
Example #14
Source File: AttributeUtils.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
public static void removeAllModifiers(AttributeInstance attributeInstance) {
    attributeInstance.getModifiers().forEach(attributeInstance::removeModifier);
}
 
Example #15
Source File: AttributeUtils.java    From ProjectAres with GNU Affero General Public License v3.0 4 votes vote down vote up
public static void removeAllModifiers(Attributable attributable, Attribute attribute) {
    final AttributeInstance instance = attributable.getAttribute(attribute);
    if(instance != null) removeAllModifiers(instance);
}