Java Code Examples for org.bukkit.entity.ExperienceOrb#setExperience()

The following examples show how to use org.bukkit.entity.ExperienceOrb#setExperience() . 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: 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 2
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 3
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 4
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 5
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 6
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 7
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 8
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);
}