org.bukkit.entity.ExperienceOrb Java Examples

The following examples show how to use org.bukkit.entity.ExperienceOrb. 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: DamageMatchModule.java    From PGM with GNU Affero General Public License v3.0 6 votes vote down vote up
@EventHandler(ignoreCancelled = true)
public void onTarget(EntityTargetEvent event) {
  if (!(event.getEntity() instanceof ExperienceOrb)) {
    ParticipantState victimState = null;
    if (event.getTarget() instanceof Player) {
      // Don't target allies
      MatchPlayer victim = getVictim(event.getTarget());
      if (victim == null) return;
      victimState = victim.getParticipantState();
    } else if (event.getTarget() != null) {
      // Don't target other mobs owned by allies
      victimState = tracker().getOwner(event.getTarget());
    }
    if (victimState == null) return;

    DamageInfo damageInfo =
        tracker()
            .resolveDamage(
                EntityDamageEvent.DamageCause.ENTITY_ATTACK,
                event.getTarget(),
                event.getEntity());
    if (queryHostile(victimState, damageInfo).isDenied()) {
      event.setCancelled(true);
    }
  }
}
 
Example #2
Source File: Blockdrops.java    From CardinalPGM with MIT License 6 votes vote down vote up
@EventHandler(priority = EventPriority.MONITOR)
public void onEntityExplode(EntityExplodeEvent event) {
    if (!event.isCancelled()) {
        Player player = TntTracker.getWhoPlaced(event.getEntity()) != null && Bukkit.getOfflinePlayer(TntTracker.getWhoPlaced(event.getEntity())).isOnline() ? Bukkit.getPlayer(TntTracker.getWhoPlaced(event.getEntity())) : null;
        if (player != null) {
            List<Block> toRemove = new ArrayList<>();
            for (Block block : event.blockList()) {
                if (filter == null || filter.evaluate(player, block, event).equals(FilterState.ALLOW)) {
                    if (region == null || region.contains(block.getLocation().toVector().add(new Vector(0.5, 0.5, 0.5)))) {
                        for (ItemStack drop : this.drops) {
                            GameHandler.getGameHandler().getMatchWorld().dropItemNaturally(block.getLocation(), drop);
                        }
                        if (this.experience != 0) {
                            ExperienceOrb xp = GameHandler.getGameHandler().getMatchWorld().spawn(block.getLocation(), ExperienceOrb.class);
                            xp.setExperience(this.experience);
                        }
                        toRemove.add(block);
                        block.setType(replaceType);
                        block.setData((byte) replaceDamage);
                    }
                }
            }
            event.blockList().removeAll(toRemove);
        }
    }
}
 
Example #3
Source File: DamageMatchModule.java    From ProjectAres with GNU Affero General Public License v3.0 6 votes vote down vote up
@EventHandler(ignoreCancelled = true)
public void onTarget(EntityTargetEvent event) {
    if(!(event.getEntity() instanceof ExperienceOrb)) {
        ParticipantState victimState = null;
        if(event.getTarget() instanceof Player) {
            // Don't target allies
            MatchPlayer victim = getVictim(event.getTarget());
            if(victim == null) return;
            victimState = victim.getParticipantState();
        } else if(event.getTarget() != null) {
            // Don't target other mobs owned by allies
            victimState = entityResolver.getOwner(event.getTarget());
        }
        if(victimState == null) return;
        DamageInfo damageInfo = damageResolver.resolveDamage(EntityDamageEvent.DamageCause.ENTITY_ATTACK, event.getTarget(), event.getEntity());
        if(queryHostile(event, victimState, damageInfo).isDenied()) {
            event.setCancelled(true);
        }
    }
}
 
Example #4
Source File: BlockDropsMatchModule.java    From PGM with GNU Affero General Public License v3.0 5 votes vote down vote up
private void dropExperience(BlockDrops drops, Location location) {
  if (drops.experience != 0) {
    ExperienceOrb expOrb =
        (ExperienceOrb) location.getWorld().spawnEntity(location, EntityType.EXPERIENCE_ORB);
    if (expOrb != null) {
      expOrb.setExperience(drops.experience);
    }
  }
}
 
Example #5
Source File: XPCollector.java    From Slimefun4 with GNU General Public License v3.0 5 votes vote down vote up
protected void tick(Block b) {
    Iterator<Entity> iterator = b.getWorld().getNearbyEntities(b.getLocation(), 4.0, 4.0, 4.0, n -> n instanceof ExperienceOrb && n.isValid()).iterator();
    int experiencePoints = 0;

    while (iterator.hasNext() && experiencePoints == 0) {
        Entity entity = iterator.next();

        if (ChargableBlock.getCharge(b) < ENERGY_CONSUMPTION) {
            return;
        }

        experiencePoints = getStoredExperience(b) + ((ExperienceOrb) entity).getExperience();

        ChargableBlock.addCharge(b, -ENERGY_CONSUMPTION);
        entity.remove();

        int withdrawn = 0;
        BlockMenu menu = BlockStorage.getInventory(b);

        for (int level = 0; level < getStoredExperience(b); level = level + 10) {
            if (menu.fits(SlimefunItems.FILLED_FLASK_OF_KNOWLEDGE, getOutputSlots())) {
                withdrawn = withdrawn + 10;
                menu.pushItem(SlimefunItems.FILLED_FLASK_OF_KNOWLEDGE.clone(), getOutputSlots());
            }
        }

        BlockStorage.addBlockInfo(b, DATA_KEY, String.valueOf(experiencePoints - withdrawn));
    }
}
 
Example #6
Source File: ButcherAndroidListener.java    From Slimefun4 with GNU General Public License v3.0 5 votes vote down vote up
@EventHandler(priority = EventPriority.HIGHEST)
public void onDeath(EntityDeathEvent e) {
    if (e.getEntity().hasMetadata(METADATA_KEY)) {
        AndroidInstance obj = (AndroidInstance) e.getEntity().getMetadata(METADATA_KEY).get(0).value();

        Slimefun.runSync(() -> {
            List<ItemStack> items = new ArrayList<>();

            // Collect any nearby dropped items
            for (Entity n : e.getEntity().getNearbyEntities(0.5D, 0.5D, 0.5D)) {
                if (n instanceof Item && n.isValid() && !SlimefunUtils.hasNoPickupFlag((Item) n)) {
                    items.add(((Item) n).getItemStack());
                    n.remove();
                }
            }

            addExtraDrops(items, e.getEntityType());

            obj.getAndroid().addItems(obj.getBlock(), items.toArray(new ItemStack[0]));
            ExperienceOrb exp = (ExperienceOrb) e.getEntity().getWorld().spawnEntity(e.getEntity().getLocation(), EntityType.EXPERIENCE_ORB);
            exp.setExperience(1 + ThreadLocalRandom.current().nextInt(6));
        }, 1L);

        // Removing metadata to prevent memory leaks
        e.getEntity().removeMetadata(METADATA_KEY, SlimefunPlugin.instance);
    }
}
 
Example #7
Source File: CommonCustomMob.java    From civcraft with GNU General Public License v2.0 5 votes vote down vote up
public void dropItems() {
	try {
		if (entity == null) {
			return;
		}
		
		LinkedList<MobDrop> dropped = getRandomDrops();
		World world = entity.getBukkitEntity().getWorld();
		Location loc = getLocation(entity);
		
		for (MobDrop d : dropped) {
			ItemStack stack;
			if (d.isVanillaDrop) {
				stack = ItemManager.createItemStack(d.vanillaType, 1, d.vanillaData);
			} else {
				LoreCraftableMaterial craftMat = LoreCraftableMaterial.getCraftMaterialFromId(d.craftMatId);
				stack = LoreCraftableMaterial.spawn(craftMat);
			}
			
			world.dropItem(loc, stack);
		}
		
		if (this.coinMax != 0 && this.coinMin != 0) {
			ExperienceOrb orb = (ExperienceOrb)world.spawn(loc, ExperienceOrb.class);
			Random random = new Random();
			int coins = random.nextInt(this.coinMax - this.coinMin) + this.coinMin;
			orb.setExperience(coins);

		}
	} catch(Exception e) {
		e.printStackTrace();
	}
}
 
Example #8
Source File: BlockDropsMatchModule.java    From ProjectAres with GNU Affero General Public License v3.0 5 votes vote down vote up
private void dropExperience(BlockDrops drops, Location location) {
    if(drops.experience != 0) {
        ExperienceOrb expOrb = (ExperienceOrb) location.getWorld().spawnEntity(BlockUtils.center(location), EntityType.EXPERIENCE_ORB);
        if(expOrb != null) {
            expOrb.setExperience(drops.experience);
        }
    }
}
 
Example #9
Source File: XpOrbData.java    From Skript with GNU General Public License v3.0 5 votes vote down vote up
@Override
@Nullable
public ExperienceOrb spawn(final Location loc) {
	final ExperienceOrb orb = super.spawn(loc);
	if (orb == null)
		return null;
	if (xp == -1)
		orb.setExperience(1);
	return orb;
}
 
Example #10
Source File: XpOrbData.java    From Skript with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected boolean init(final @Nullable Class<? extends ExperienceOrb> c, final @Nullable ExperienceOrb e) {
	xp = e == null ? -1 : e.getExperience();
	return true;
}
 
Example #11
Source File: ExpCapListener.java    From NyaaUtils with MIT License 4 votes vote down vote up
@EventHandler
public void onExpCapHit(ExpBottleEvent event) {
    if (event.getEntity().hasMetadata("nu_expcap_exp")) {
        List<MetadataValue> nu_expcap_exp = event.getEntity().getMetadata("nu_expcap_exp");
        MetadataValue metadataValue = nu_expcap_exp.get(0);
        if (metadataValue == null) {
            return;
        }
        int exp = metadataValue.asInt();
        if (exp <= 0)return;
        //生成的经验球数量大于1,小于总经验数
        int maxOrbAmount = Math.min(exp,cfg.expcap_max_orb_amount);
        int minOrbAmount = Math.max(1, ((NyaaUtils) plugin).cfg.expcap_min_orb_amount);
        int orbAmount = Math.min(Math.max(exp, minOrbAmount), maxOrbAmount);
        Location location = event.getEntity().getLocation();
        int expPerOrb = exp / orbAmount;
        int delay = 0;
        int step = Math.max(cfg.expcap_orb_ticksBetweenSpawn,0);
        //整形除法可能导致实际生成经验量偏少
        int spawnedExp = orbAmount * expPerOrb;
        int remain = exp - spawnedExp;
        final World world = location.getWorld();
        if (world == null) return;
        world.spawn(location, ExperienceOrb.class, experienceOrb -> {
            experienceOrb.setExperience(remain);
        });

        for (int i = 0; i < orbAmount; i++) {
            Bukkit.getScheduler().runTaskLater(plugin, ()->{
                Location add = null;
                for (int j = 0; j < 5; j++) {
                    Location clone = location.clone();
                    double dx = random.nextDouble() * 6;
                    double dy = random.nextDouble() * 3;
                    double dz = random.nextDouble() * 6;
                    dx -= 3;
                    dz -= 3;
                    add = clone.add(new Vector(dx, dy, dz));
                    if (!world.getBlockAt(location).getType().isSolid())break;
                }
                if (!world.getBlockAt(location).getType().isSolid()) add = location;
                world.spawn(add, ExperienceOrb.class, experienceOrb -> {
                    experienceOrb.setExperience(expPerOrb);
                });
            },delay+=step);
        }
    }
}
 
Example #12
Source File: DefaultDropsHandler.java    From EliteMobs with GNU General Public License v3.0 4 votes vote down vote up
@EventHandler(priority = EventPriority.HIGHEST)
    public void onDeath(EntityDeathEvent event) {

        if (!EntityTracker.isEliteMob(event.getEntity())) return;

        if (!EntityTracker.isNaturalEntity(event.getEntity()) &&
                !ConfigValues.itemsDropSettingsConfig.getBoolean(ItemsDropSettingsConfig.SPAWNER_DEFAULT_LOOT_MULTIPLIER))
            return;

        List<ItemStack> droppedItems = event.getDrops();
        int mobLevel = (int) (EntityTracker.getEliteMobEntity(event.getEntity()).getLevel() *
                ConfigValues.itemsDropSettingsConfig.getDouble(ItemsDropSettingsConfig.DEFAULT_LOOT_MULTIPLIER));

        if (mobLevel > ConfigValues.itemsDropSettingsConfig.getInt(ItemsDropSettingsConfig.MAXIMUM_LEVEL_FOR_LOOT_MULTIPLIER))
            mobLevel = ConfigValues.itemsDropSettingsConfig.getInt(ItemsDropSettingsConfig.MAXIMUM_LEVEL_FOR_LOOT_MULTIPLIER);

        inventoryItemsConstructor(event.getEntity());

        for (ItemStack itemStack : droppedItems) {

//                ItemStack can be null for some reason, probably due to other plugins
            if (itemStack == null) continue;
            boolean itemIsWorn = false;

            for (ItemStack wornItem : wornItems)
                if (wornItem.isSimilar(itemStack))
                    itemIsWorn = true;

            if (!itemIsWorn)
                for (int i = 0; i < mobLevel * 0.1; i++)
                    event.getEntity().getLocation().getWorld().dropItem(event.getEntity().getLocation(), itemStack);

        }

        mobLevel = (int) (EntityTracker.getEliteMobEntity(event.getEntity()).getLevel() *
                ConfigValues.itemsDropSettingsConfig.getDouble(ItemsDropSettingsConfig.EXPERIENCE_LOOT_MULTIPLIER));

        int droppedXP = (int) (event.getDroppedExp() + event.getDroppedExp() * 0.1 * mobLevel);
        event.setDroppedExp(0);
        event.getEntity().getWorld().spawn(event.getEntity().getLocation(), ExperienceOrb.class).setExperience(droppedXP);

    }
 
Example #13
Source File: XpOrbData.java    From Skript with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void set(final ExperienceOrb entity) {
	if (xp != -1)
		entity.setExperience(xp);
}
 
Example #14
Source File: XpOrbData.java    From Skript with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected boolean match(final ExperienceOrb entity) {
	return xp == -1 || entity.getExperience() == xp;
}
 
Example #15
Source File: XpOrbData.java    From Skript with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Class<? extends ExperienceOrb> getType() {
	return ExperienceOrb.class;
}
 
Example #16
Source File: PlayerItemMendEvent.java    From Kettle with GNU General Public License v3.0 4 votes vote down vote up
public PlayerItemMendEvent(Player who, ItemStack item, ExperienceOrb experienceOrb, int repairAmount) {
    super(who);
    this.item = item;
    this.experienceOrb = experienceOrb;
    this.repairAmount = repairAmount;
}
 
Example #17
Source File: UhcItems.java    From UhcCore with GNU General Public License v3.0 4 votes vote down vote up
public static void spawnExtraXp(Location location, int quantity) {
	ExperienceOrb orb = (ExperienceOrb) location.getWorld().spawnEntity(location, EntityType.EXPERIENCE_ORB);
	orb.setExperience(quantity);	
}
 
Example #18
Source File: DataHandler.java    From MineTinker with GNU General Public License v3.0 4 votes vote down vote up
public static boolean playerBreakBlock(@NotNull Player player, Block block, @NotNull ItemStack itemStack) {
    //Trigger BlockBreakEvent
    BlockBreakEvent breakEvent = new BlockBreakEvent(block, player);
    ItemMeta meta = itemStack.getItemMeta();
    if (meta != null && !meta.hasEnchant(Enchantment.SILK_TOUCH)) breakEvent.setExpToDrop(calculateExp(block.getType()));
    Bukkit.getPluginManager().callEvent(breakEvent);

    //Check if Event got cancelled and if not destroy the block and check if the player can successfully break the blocks (incl. drops)
    //Block#breakNaturally(ItemStack itemStack) can not be used as it drops Items itself (without Event and we don't want that)
    if (!breakEvent.isCancelled()) {
        //Get all drops to drop
        Collection<ItemStack> items = block.getDrops(itemStack);

        //Set Block to Material.AIR (effectively breaks the Block)
        block.setType(Material.AIR);
        //TODO: Play Sound?

        //Check if items need to be dropped
        if (breakEvent.isDropItems()) {
            List<Item> itemEntities = items.stream()
                    .map(entry -> player.getWorld().dropItemNaturally(block.getLocation(), entry)) //World#spawnEntity() does not work for Items
                    .collect(Collectors.toList());

            //Trigger BlockDropItemEvent (internally also used for Directing)
            BlockDropItemEvent event = new BlockDropItemEvent(block, block.getState(), player, new ArrayList<>(itemEntities));
            Bukkit.getPluginManager().callEvent(event);

            //check if Event got cancelled
            if (!event.isCancelled()) {
                //Remove all drops that should be dropped
                itemEntities.removeIf(element -> event.getItems().contains(element));
            }
            itemEntities.forEach(Item::remove);
        }

        //Check if Exp needs to be dropped
        if (breakEvent.getExpToDrop() > 0) {
            //Spawn Experience Orb
            ExperienceOrb orb = (ExperienceOrb) player.getWorld().spawnEntity(block.getLocation(), EntityType.EXPERIENCE_ORB);
            orb.setExperience(breakEvent.getExpToDrop());
        }

        return true;
    }

    return false;
}
 
Example #19
Source File: PlayerPickupExperienceEvent.java    From Kettle with GNU General Public License v3.0 4 votes vote down vote up
/**
 * @return Returns the Orb that the player is picking up
 */
public ExperienceOrb getExperienceOrb() {
    return experienceOrb;
}
 
Example #20
Source File: PlayerPickupExperienceEvent.java    From Kettle with GNU General Public License v3.0 4 votes vote down vote up
public PlayerPickupExperienceEvent(Player player, ExperienceOrb experienceOrb) {
    super(player);
    this.experienceOrb = experienceOrb;
}
 
Example #21
Source File: ExperienceOrbMergeEvent.java    From Kettle with GNU General Public License v3.0 4 votes vote down vote up
/**
 * @return The orb that is subject to being removed and merged into the target orb
 */
public ExperienceOrb getMergeSource() {
    return mergeSource;
}
 
Example #22
Source File: ExperienceOrbMergeEvent.java    From Kettle with GNU General Public License v3.0 4 votes vote down vote up
/**
 * @return The orb that will absorb the other experience orb
 */
public ExperienceOrb getMergeTarget() {
    return mergeTarget;
}
 
Example #23
Source File: ExperienceOrbMergeEvent.java    From Kettle with GNU General Public License v3.0 4 votes vote down vote up
public ExperienceOrbMergeEvent(ExperienceOrb mergeTarget, ExperienceOrb mergeSource) {
    super(mergeTarget);
    this.mergeTarget = mergeTarget;
    this.mergeSource = mergeSource;
}
 
Example #24
Source File: PlayerItemMendEvent.java    From Kettle with GNU General Public License v3.0 2 votes vote down vote up
/**
 * Get the experience orb triggering the event.
 *
 * @return the experience orb
 */
public ExperienceOrb getExperienceOrb() {
    return experienceOrb;
}