Java Code Examples for org.bukkit.event.player.PlayerInteractEvent#getItem()
The following examples show how to use
org.bukkit.event.player.PlayerInteractEvent#getItem() .
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: ExpCapListener.java From NyaaUtils with MIT License | 6 votes |
@EventHandler public void onPlayerRightClick(PlayerInteractEvent event) { ItemStack itemInMainHand = event.getPlayer().getInventory().getItemInMainHand(); ItemStack itemInOffHand = event.getPlayer().getInventory().getItemInOffHand(); if (!itemInMainHand.getType().equals(Material.EXPERIENCE_BOTTLE) && !itemInOffHand.getType().equals(Material.EXPERIENCE_BOTTLE)) { return; } if (event.getAction().equals(Action.RIGHT_CLICK_BLOCK) || event.getAction().equals(Action.RIGHT_CLICK_AIR)) { ItemStack item = event.getItem(); if (item == null) return; if (item.getType().equals(Material.EXPERIENCE_BOTTLE)) { Integer storedExp = ExpCapsuleCommands.getStoredExp(item); thrownExpMap.put(event.getPlayer().getUniqueId(), storedExp); } } }
Example 2
Source File: ArmorListener.java From AdditionsAPI with MIT License | 6 votes |
@EventHandler(priority = EventPriority.HIGHEST, ignoreCancelled = true) public void playerInteractEvent(PlayerInteractEvent e){ if(e.getAction() == Action.PHYSICAL) return; if(e.getAction() == Action.RIGHT_CLICK_AIR || e.getAction() == Action.RIGHT_CLICK_BLOCK){ Player player = e.getPlayer(); ArmorType newArmorType = ArmorType.matchType(e.getItem()); if(newArmorType != null){ if(newArmorType.equals(ArmorType.HELMET) && isAirOrNull(e.getPlayer().getInventory().getHelmet()) || newArmorType.equals(ArmorType.CHESTPLATE) && isAirOrNull(e.getPlayer().getInventory().getChestplate()) || newArmorType.equals(ArmorType.LEGGINGS) && isAirOrNull(e.getPlayer().getInventory().getLeggings()) || newArmorType.equals(ArmorType.BOOTS) && isAirOrNull(e.getPlayer().getInventory().getBoots())){ ArmorEquipEvent armorEquipEvent = new ArmorEquipEvent(e.getPlayer(), EquipMethod.HOTBAR, ArmorType.matchType(e.getItem()), null, e.getItem()); Bukkit.getServer().getPluginManager().callEvent(armorEquipEvent); if(armorEquipEvent.isCancelled()){ e.setCancelled(true); player.updateInventory(); } } } } }
Example 3
Source File: SpellListener.java From Civs with GNU General Public License v3.0 | 6 votes |
@EventHandler public void onSpellUse(PlayerInteractEvent event) { ItemStack itemStack = event.getItem(); if (!ConfigManager.getInstance().getUseClassesAndSpells() || !CivItem.isCivsItem(itemStack)) { return; } CivItem civItem = CivItem.getFromItemStack(itemStack); if (!(civItem instanceof SpellType)) { return; } SpellType spellType = (SpellType) civItem; Civilian civilian = CivilianManager.getInstance().getCivilian(event.getPlayer().getUniqueId()); Spell spell = new Spell(civItem.getProcessedName(), event.getPlayer(), civilian.getLevel(spellType)); //TODO should I add this to a spell manager? Spells dont persist spell.useAbility(); }
Example 4
Source File: CivilianListener.java From Civs with GNU General Public License v3.0 | 6 votes |
@EventHandler(ignoreCancelled = true) public void onFireworkLaunch(PlayerInteractEvent event) { if (event.getItem() == null || event.getItem().getType() != Material.FIREWORK_ROCKET || (event.getAction() != Action.RIGHT_CLICK_AIR && event.getAction() != Action.RIGHT_CLICK_BLOCK)) { return; } Player player = event.getPlayer(); Civilian civilian = CivilianManager.getInstance().getCivilian(player.getUniqueId()); Town town = TownManager.getInstance().getTownAt(player.getLocation()); if (town == null) { return; } Government government = GovernmentManager.getInstance().getGovernment(town.getGovernmentType()); if (government.getGovernmentType() != GovernmentType.IDIOCRACY) { return; } if (!town.getIdiocracyScore().containsKey(civilian.getUuid())) { town.getIdiocracyScore().put(civilian.getUuid(), 1); } else { town.getIdiocracyScore().put(civilian.getUuid(), town.getIdiocracyScore().get(civilian.getUuid()) + 1); } Util.checkNoise(town, event.getPlayer()); TownManager.getInstance().saveTown(town); }
Example 5
Source File: BlockListener.java From Carbon with GNU Lesser General Public License v3.0 | 6 votes |
@SuppressWarnings("deprecation") @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) public void slabInteract(PlayerInteractEvent event) { if (event.getItem() == null || event.getAction() != Action.RIGHT_CLICK_BLOCK || event.getItem().getType() != Carbon.injector().redSandstoneSlabMat) return; Block clickedBlock = event.getClickedBlock(); if (clickedBlock.getType() == Carbon.injector().redSandstoneSlabMat) if ((clickedBlock.getData() == 0 && event.getBlockFace() == BlockFace.UP) || (clickedBlock.getData() == 8 && event.getBlockFace() == BlockFace.DOWN)) { setDoubleSlab(event.getPlayer(), clickedBlock); event.setCancelled(true); return; } Block adjacent = clickedBlock.getRelative(event.getBlockFace()); if (adjacent.getType() == Carbon.injector().redSandstoneSlabMat) { setDoubleSlab(event.getPlayer(), adjacent); event.setCancelled(true); } }
Example 6
Source File: TrackerListener.java From BedWars with GNU Lesser General Public License v3.0 | 6 votes |
@EventHandler public void onTrackerUse(PlayerInteractEvent event) { Player player = event.getPlayer(); if (!Main.isPlayerInGame(player)) { return; } GamePlayer gamePlayer = Main.getPlayerGameProfile(player); Game game = gamePlayer.getGame(); if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) { if (game.getStatus() == GameStatus.RUNNING && !gamePlayer.isSpectator) { if (event.getItem() != null) { ItemStack stack = event.getItem(); String unhidden = APIUtils.unhashFromInvisibleStringStartsWith(stack, TRACKER_PREFIX); if (unhidden != null) { event.setCancelled(true); Tracker tracker = new Tracker(game, player, game.getTeamOfPlayer(player)); tracker.runTask(); } } } } }
Example 7
Source File: TNTSheepListener.java From BedWars with GNU Lesser General Public License v3.0 | 5 votes |
@EventHandler public void onTNTSheepUsed(PlayerInteractEvent event) { Player player = event.getPlayer(); if (!Main.isPlayerInGame(player)) { return; } GamePlayer gamePlayer = Main.getPlayerGameProfile(player); Game game = gamePlayer.getGame(); if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) { if (game.getStatus() == GameStatus.RUNNING && !gamePlayer.isSpectator && event.getItem() != null) { ItemStack stack = event.getItem(); String unhidden = APIUtils.unhashFromInvisibleStringStartsWith(stack, TNT_SHEEP_PREFIX); if (unhidden != null) { event.setCancelled(true); double speed = Double.parseDouble(unhidden.split(":")[2]); double follow = Double.parseDouble(unhidden.split(":")[3]); double maxTargetDistance = Double.parseDouble(unhidden.split(":")[4]); int explosionTime = Integer.parseInt(unhidden.split(":")[5]); Location startLocation; if (event.getClickedBlock() == null) { startLocation = player.getLocation(); } else { startLocation = event.getClickedBlock().getRelative(BlockFace.UP).getLocation(); } TNTSheep sheep = new TNTSheep(game, player, game.getTeamOfPlayer(player), startLocation, stack, speed, follow, maxTargetDistance, explosionTime); sheep.spawn(); } } } }
Example 8
Source File: BlockListener.java From civcraft with GNU General Public License v2.0 | 5 votes |
private void OnPlayerUseItem(PlayerInteractEvent event) { Location loc = (event.getClickedBlock() == null) ? event.getPlayer().getLocation() : event.getClickedBlock().getLocation(); ItemStack stack = event.getItem(); coord.setFromLocation(event.getPlayer().getLocation()); Camp camp = CivGlobal.getCampFromChunk(coord); if (camp != null) { if (!camp.hasMember(event.getPlayer().getName())) { CivMessage.sendError(event.getPlayer(), "You cannot use "+stack.getType().toString()+" in a camp you do not belong to."); event.setCancelled(true); return; } } TownChunk tc = CivGlobal.getTownChunk(loc); if (tc == null) { return; } Resident resident = CivGlobal.getResident(event.getPlayer().getName()); if (resident == null) { event.setCancelled(true); } if(!tc.perms.hasPermission(PlotPermissions.Type.ITEMUSE, resident)) { event.setCancelled(true); CivMessage.sendErrorNoRepeat(event.getPlayer(), "You do not have permission to use "+stack.getType().toString()+" here."); } return; }
Example 9
Source File: ThrowableFireballListener.java From BedWars with GNU Lesser General Public License v3.0 | 5 votes |
@EventHandler public void onFireballThrow(PlayerInteractEvent event) { Player player = event.getPlayer(); if (!Main.isPlayerInGame(player)) { return; } if (event.getItem() != null) { ItemStack stack = event.getItem(); String unhash = APIUtils.unhashFromInvisibleStringStartsWith(stack, THROWABLE_FIREBALL_PREFIX); if (unhash != null && (event.getAction() == Action.RIGHT_CLICK_BLOCK || event.getAction() == Action.RIGHT_CLICK_AIR)) { String[] properties = unhash.split(":"); double damage = Double.parseDouble(properties[2]); float explosion = (float) Double.parseDouble(properties[2]); Fireball fireball = player.launchProjectile(Fireball.class); fireball.setIsIncendiary(false); fireball.setYield(explosion); Main.registerGameEntity(fireball, Main.getPlayerGameProfile(player).getGame()); event.setCancelled(true); if (stack.getAmount() > 1) { stack.setAmount(stack.getAmount() - 1); } else { player.getInventory().remove(stack); } player.updateInventory(); } } }
Example 10
Source File: LWCEntityListener.java From Modern-LWC with MIT License | 5 votes |
@EventHandler(ignoreCancelled = true) public void onPlayerInteract(PlayerInteractEvent e) { ItemStack inHand = e.getItem(); if (inHand != null) { placedArmorStandPlayer = e.getPlayer().getUniqueId(); } }
Example 11
Source File: PlantTreeEventHandler.java From GiantTrees with GNU General Public License v3.0 | 5 votes |
@EventHandler public void onPlayerInteract(final PlayerInteractEvent event) { if (!enabled) { return; } if (!event.getPlayer().hasPermission("gianttrees.grow")) { return; } final ItemStack itemInHand = event.getItem(); final Block clickedBlock = event.getClickedBlock(); if ((itemInHand == null) || (clickedBlock == null) || (event.getAction() != Action.RIGHT_CLICK_BLOCK)) { return; } final ItemCost cost = new ItemCost(Material.BONE_MEAL, (short) 15, boneMealConsumed); for (PhysicalCraftingRecipe recipe : recipes) { if (cost.isAffordable(event.getPlayer()) && stackIsCorrect(itemInHand) && recipe.matches(clickedBlock)) { final String treeType = identifyTree(clickedBlock); if (treeType != null) { final Random seed = new Random(clickedBlock.getWorld().getSeed()); final File treeFile = new File(plugin.getDataFolder(), "tree." + treeType + ".xml"); final File rootFile = new File(plugin.getDataFolder(), "tree." + treeType + ".root.xml"); event.setCancelled(true); cost.apply(event.getPlayer()); popup.sendPopup(event.getPlayer(), "Stand back!"); renderer.renderTree(clickedBlock.getLocation(), treeFile, rootFile, seed.nextInt(), true); } } } }
Example 12
Source File: SpectatorTools.java From CardinalPGM with MIT License | 5 votes |
@EventHandler(priority = EventPriority.LOWEST) public void onPlayerInteract(PlayerInteractEvent event) { boolean action = event.getAction().equals(Action.RIGHT_CLICK_AIR) || event.getAction().equals(Action.RIGHT_CLICK_BLOCK); if (ObserverModule.testObserver(event.getPlayer()) && action && event.getItem() != null && event.getItem().isSimilar(getSpectatorItem(event.getPlayer().getLocale()))) { event.setCancelled(true); event.getPlayer().openInventory(getSpectatorMenu(event.getPlayer())); } }
Example 13
Source File: ItemUseListener.java From EnchantmentsEnhance with GNU General Public License v3.0 | 5 votes |
@EventHandler public void onItemClick(final PlayerInteractEvent event) { final Player player = event.getPlayer(); if (event.getMaterial() == Material.AIR) { return; } if (event.getAction().equals(Action.LEFT_CLICK_BLOCK) || event.getAction().equals(Action.LEFT_CLICK_AIR)) { return; } final ItemStack item = event.getItem(); if (!ItemManager.getGive(item).isEmpty()) { String[] give = ItemManager.getGive(item).split(":"); if (give.length == 2) { int id = Integer.parseInt(give[0]); int amount = Integer.parseInt(give[1]); if (id < 0) { Main.getApi().addAdvice(player.getName(), amount); Util.sendMessage(SettingsManager.lang.getString("materialize.adviceSucess") .replace("%level%", Integer.toString(amount)), player); } else { Main.getApi().addItem(player.getName(), id, amount); Util.sendMessage(SettingsManager.lang.getString("materialize.success") .replace("%amount%", Integer.toString(amount)) .replace("%item%", SettingsManager.lang.getString("item." + id)), player); } // Consume the item. try { if (event.getHand().equals(EquipmentSlot.HAND)) { consumeItem(Util.getMainHand(player), player); } else if (event.getHand().equals(EquipmentSlot.OFF_HAND)) { consumeItem(player.getInventory().getItemInOffHand(), player); } } catch (NoSuchMethodError ex) { consumeItem(Util.getMainHand(player), player); } event.setCancelled(true); } } }
Example 14
Source File: Compat19.java From RedProtect with GNU General Public License v3.0 | 5 votes |
@EventHandler(priority = EventPriority.LOW) public void onPlayerInteract(PlayerInteractEvent event) { Player p = event.getPlayer(); Block b = event.getClickedBlock(); ItemStack itemInHand = event.getItem(); Location l; if (b != null) { l = b.getLocation(); RedProtect.get().logger.debug(LogLevel.DEFAULT, "PlayerListener - Is PlayerInteractEvent event. The block is " + b.getType().name()); } else { l = p.getLocation(); } if (RedProtect.get().tpWait.contains(p.getName())) { RedProtect.get().tpWait.remove(p.getName()); RedProtect.get().lang.sendMessage(p, "cmdmanager.region.tpcancelled"); } if (itemInHand != null && (event.getAction().name().equals("RIGHT_CLICK_BLOCK") || b == null)) { Material hand = itemInHand.getType(); Region r = RedProtect.get().rm.getTopRegion(l); // Deny chorus teleport if (r != null && hand.equals(Material.CHORUS_FRUIT) && !r.canTeleport(p)) { RedProtect.get().lang.sendMessage(p, "playerlistener.region.cantuse"); event.setCancelled(true); event.setUseItemInHand(Event.Result.DENY); } // Deny glide boost if (r == null && p.isGliding() && itemInHand.getType().name().contains("FIREWORK") && !p.hasPermission("redprotect.bypass.glide") && !RedProtect.get().config.globalFlagsRoot().worlds.get(p.getWorld().getName()).player_glide.allow_boost) { event.setUseItemInHand(Event.Result.DENY); event.setCancelled(true); RedProtect.get().lang.sendMessage(p, "globallistener.elytra.cantboost"); } } }
Example 15
Source File: PetOwnerListener.java From SonarPet with GNU General Public License v3.0 | 5 votes |
@EventHandler public void onPlayerInteract(PlayerInteractEvent event) { Player p = event.getPlayer(); ItemStack itemStack = event.getItem(); if (itemStack != null && itemStack.isSimilar(SelectorLayout.getSelectorItem())) { SelectorLayout.getSelectorMenu().showTo(p); event.setCancelled(true); } }
Example 16
Source File: PlayerEventHandler.java From GriefDefender with MIT License | 4 votes |
public void onPlayerInteractBlockPrimary(PlayerInteractEvent event, Player player) { GDCauseStackManager.getInstance().pushCause(player); if (event.getAction() != Action.LEFT_CLICK_AIR && event.getAction() != Action.LEFT_CLICK_BLOCK) { return; } if (!GDFlags.INTERACT_BLOCK_PRIMARY || !GriefDefenderPlugin.getInstance().claimsEnabledForWorld(player.getWorld().getUID())) { return; } if (GriefDefenderPlugin.isTargetIdBlacklisted(Flags.INTERACT_BLOCK_PRIMARY.getName(), event.getClickedBlock(), player.getWorld().getUID())) { return; } final Block clickedBlock = event.getClickedBlock(); final ItemStack itemInHand = event.getItem(); final Location location = clickedBlock == null ? null : clickedBlock.getLocation(); final GDPlayerData playerData = this.dataStore.getOrCreateGlobalPlayerData(player.getUniqueId()); final Object source = itemInHand != null && !event.isBlockInHand() ? itemInHand : player; if (playerData.claimMode) { return; } // check give pet if (playerData.petRecipientUniqueId != null) { playerData.petRecipientUniqueId = null; GriefDefenderPlugin.sendMessage(player, MessageCache.getInstance().COMMAND_PET_TRANSFER_CANCEL); event.setCancelled(true); return; } if (location == null) { return; } GDTimings.PLAYER_INTERACT_BLOCK_PRIMARY_EVENT.startTiming(); final GDClaim claim = this.dataStore.getClaimAt(location); final GriefDefenderConfig<?> activeConfig = GriefDefenderPlugin.getActiveConfig(location.getWorld().getUID()); if (activeConfig.getConfig().economy.isSellSignEnabled() || activeConfig.getConfig().economy.isRentSignEnabled()) { final Sign sign = SignUtil.getSign(location); if (sign != null) { if (activeConfig.getConfig().economy.isSellSignEnabled() && SignUtil.isSellSign(sign)) { if (claim.getEconomyData() != null && claim.getEconomyData().isForSale()) { event.setCancelled(true); EconomyUtil.getInstance().sellCancelConfirmation(event.getPlayer(), claim, sign); GDTimings.PLAYER_INTERACT_BLOCK_PRIMARY_EVENT.stopTiming(); return; } } else if (GriefDefenderPlugin.getGlobalConfig().getConfig().economy.rentSystem && activeConfig.getConfig().economy.isRentSignEnabled() && SignUtil.isRentSign(claim, sign)) { if ((claim.getEconomyData() != null && claim.getEconomyData().isForRent()) || claim.getEconomyData().isRented() ) { event.setCancelled(true); EconomyUtil.getInstance().rentCancelConfirmation(event.getPlayer(), claim, sign); GDTimings.PLAYER_INTERACT_BLOCK_PRIMARY_EVENT.stopTiming(); return; } } } } final Tristate result = GDPermissionManager.getInstance().getFinalPermission(event, location, claim, Flags.INTERACT_BLOCK_PRIMARY, source, clickedBlock.getState(), player, TrustTypes.BUILDER, true); if (result == Tristate.FALSE) { if (GriefDefenderPlugin.isTargetIdBlacklisted(Flags.BLOCK_BREAK.toString(), clickedBlock.getState(), player.getWorld().getUID())) { GDTimings.PLAYER_INTERACT_BLOCK_PRIMARY_EVENT.stopTiming(); return; } if (GDPermissionManager.getInstance().getFinalPermission(event, location, claim, Flags.BLOCK_BREAK, player, clickedBlock.getState(), player, TrustTypes.BUILDER, true) == Tristate.TRUE) { GDTimings.PLAYER_INTERACT_BLOCK_PRIMARY_EVENT.stopTiming(); return; } // Don't send a deny message if the player is in claim mode or is holding an investigation tool if (!playerData.claimMode && (GriefDefenderPlugin.getInstance().investigationTool == null || !NMSUtil.getInstance().hasItemInOneHand(player, GriefDefenderPlugin.getInstance().investigationTool))) { this.sendInteractBlockDenyMessage(itemInHand, clickedBlock, claim, player, playerData); } event.setCancelled(true); GDTimings.PLAYER_INTERACT_BLOCK_PRIMARY_EVENT.stopTiming(); return; } GDTimings.PLAYER_INTERACT_BLOCK_PRIMARY_EVENT.stopTiming(); }
Example 17
Source File: BlockClickEvent.java From WildernessTp with MIT License | 4 votes |
@EventHandler(priority = EventPriority.HIGHEST) public void onBlockClick(PlayerInteractEvent e) { String[] tmp = Bukkit.getVersion().split("MC: "); String version = tmp[tmp.length - 1].substring(0, 3); if (e.getItem() == null) return; if (!e.getItem().hasItemMeta()) return; if (e.getAction().equals(Action.LEFT_CLICK_BLOCK) && e.getItem().getItemMeta().hasLore()) { if (e.getItem().getItemMeta().getLore().equals(Collections.singletonList("Right/left click on blocks to make a region")) && !checkFirstMap(e.getPlayer().getUniqueId(), e.getClickedBlock().getLocation().toVector())) { e.setCancelled(true); wild.firstCorner.put(e.getPlayer().getUniqueId(), e.getClickedBlock().getLocation().toVector()); e.getPlayer().sendMessage(ChatColor.GREEN + "First corner set"); } } else if (e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) { ItemStack stack = new ItemStack(Material.STICK); if (version.equals("1.9") || version.equals("1.1")) { if (e.getPlayer().getInventory().getItemInMainHand() != null) stack = e.getPlayer().getInventory().getItemInMainHand(); else return; } else { //noinspection deprecation if (e.getPlayer().getItemInHand() != null) //noinspection deprecation stack = e.getPlayer().getItemInHand(); else return; } if (!stack.hasItemMeta()) return; if (!stack.getItemMeta().hasLore()) return; if (!stack.getItemMeta().getLore().equals(Collections.singletonList("Right/left click on blocks to make a region"))) return; if (checkSecondMap(e.getPlayer().getUniqueId(), e.getClickedBlock().getLocation().toVector())) return; e.setCancelled(true); wild.secondCorner.put(e.getPlayer().getUniqueId(), e.getClickedBlock().getLocation().toVector()); e.getPlayer().sendMessage(ChatColor.GREEN + "Second corner set"); } }
Example 18
Source File: EasyHarvestListener.java From MineTinker with GNU General Public License v3.0 | 4 votes |
@EventHandler public void onHarvestTry(PlayerInteractEvent event) { if (event.getAction() != Action.RIGHT_CLICK_BLOCK) { return; } Player player = event.getPlayer(); if (Lists.WORLDS_EASYHARVEST.contains(player.getWorld().getName())) { return; } if (!(player.getGameMode() == GameMode.SURVIVAL || player.getGameMode() == GameMode.ADVENTURE || player.getGameMode() == GameMode.CREATIVE)) { return; } ItemStack tool = player.getInventory().getItemInMainHand(); if (!ToolType.HOE.contains(tool.getType())) { return; } if (!modManager.isToolViable(tool)) { return; } if (event.getClickedBlock() == null) { return; } if (event.getItem() == null) { return; } Block block = event.getClickedBlock(); if (!(block.getBlockData() instanceof Ageable)) { return; } //triggers a pseudoevent to find out if the Player can build BlockPlaceEvent placeEvent = new BlockPlaceEvent(block, block.getState(), block, event.getItem(), player, true, EquipmentSlot.HAND); Bukkit.getPluginManager().callEvent(placeEvent); //check the pseudoevent if (!placeEvent.canBuild() || placeEvent.isCancelled()) { return; } harvestCrops(player, tool, block); }
Example 19
Source File: PlayerListener.java From MineTinker with GNU General Public License v3.0 | 4 votes |
/** * Updates the HashMap BLOCKFACE with the clicked face of the Block * * @param event PlayerInteractEvent */ @EventHandler(priority = EventPriority.LOW) public void onInteract(PlayerInteractEvent event) { if (Lists.WORLDS.contains(event.getPlayer().getWorld().getName())) { return; } if (!event.getBlockFace().equals(BlockFace.SELF)) { Lists.BLOCKFACE.replace(event.getPlayer(), event.getBlockFace()); } if (!modManager.allowBookToModifier()) { return; } if (event.getClickedBlock() == null || event.getClickedBlock().getType() != Material.BOOKSHELF) { return; } if (event.getItem() == null || event.getItem().getType() != Material.ENCHANTED_BOOK) { return; } if (event.getItem().getItemMeta() == null || !(event.getItem().getItemMeta() instanceof EnchantmentStorageMeta)) { return; } EnchantmentStorageMeta meta = (EnchantmentStorageMeta) event.getItem().getItemMeta(); for (Map.Entry<Enchantment, Integer> entry : meta.getStoredEnchants().entrySet()) { Modifier modifier = modManager.getModifierFromEnchantment(entry.getKey()); if (modifier == null) { continue; } ItemStack modDrop = modifier.getModItem(); modDrop.setAmount(entry.getValue()); event.getClickedBlock().getWorld().dropItem(event.getClickedBlock().getLocation(), modDrop); meta.removeStoredEnchant(entry.getKey()); } if (meta.getStoredEnchants().isEmpty()) { event.getPlayer().getInventory().removeItem(event.getItem()); } else { event.getItem().setItemMeta(meta); } }
Example 20
Source File: Library.java From civcraft with GNU General Public License v2.0 | 4 votes |
public void add_enchantment_to_tool(Player player, StructureSign sign, PlayerInteractEvent event) throws CivException { int special_id = Integer.valueOf(sign.getAction()); if (!event.hasItem()) { CivMessage.send(player, CivColor.Rose+"You must have the item you wish to enchant in hand."); return; } ItemStack item = event.getItem(); if (special_id >= this.enchantments.size()) { throw new CivException("Library enchantment not ready."); } LibraryEnchantment ench = this.enchantments.get(special_id); this.validateEnchantment(item, ench); int payToTown = (int) Math.round(ench.price*getNonResidentFee()); Resident resident; resident = CivGlobal.getResident(player.getName()); Town t = resident.getTown(); if (t == this.getTown()) { // Pay no taxes! You're a member. payToTown = 0; } // Determine if resident can pay. if (!resident.getTreasury().hasEnough(ench.price+payToTown)) { CivMessage.send(player, CivColor.Rose+"You do not have enough money, you need "+ench.price+payToTown+ " coins."); return; } // Take money, give to server, TEH SERVER HUNGERS ohmnom nom resident.getTreasury().withdraw(ench.price); // Send money to town for non-resident fee if (payToTown != 0) { getTown().depositDirect(payToTown); CivMessage.send(player, CivColor.Yellow + "Paid "+ payToTown+" coins in non-resident taxes."); } // Successful payment, process enchantment. ItemStack newStack = this.addEnchantment(item, ench); player.setItemInHand(newStack); CivMessage.send(player, CivColor.LightGreen+"Enchanted with "+ench.displayName+"!"); }