/** Sends the current cooldown in action bar. */ private void sendCooldownBar() { if (getPlayer() == null) return; StringBuilder stringBuilder = new StringBuilder(); double currentCooldown = Core.getCustomPlayer(getPlayer()).canUse(type); double maxCooldown = type.getCountdown(); int res = (int) (currentCooldown / maxCooldown * 10); ChatColor color; for (int i = 0; i < 10; i++) { color = ChatColor.RED; if (i < 10 - res) color = ChatColor.GREEN; stringBuilder.append(color + "â–�"); } DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(Locale.US); otherSymbols.setDecimalSeparator('.'); otherSymbols.setGroupingSeparator('.'); otherSymbols.setPatternSeparator('.'); final DecimalFormat decimalFormat = new DecimalFormat("0.0", otherSymbols); String timeLeft = decimalFormat.format(currentCooldown) + "s"; PlayerUtils.sendInActionBar( getPlayer(), getName() + " §f" + stringBuilder.toString() + " §f" + timeLeft); }
@EventHandler public void onInventoryClickAmmo(final InventoryClickEvent event) { if (event.getWhoClicked() == getPlayer() && inv != null && isSameInventory(event.getWhoClicked().getOpenInventory().getTopInventory(), inv)) { event.setCancelled(true); if (event.getCurrentItem() != null && event.getCurrentItem().hasItemMeta() && event.getCurrentItem().getItemMeta().hasDisplayName()) { String displayName = event.getCurrentItem().getItemMeta().getDisplayName(); String purchase = MessageManager.getMessage("Purchase"); String cancel = MessageManager.getMessage("Cancel"); if (displayName.equals(purchase)) { if (Core.getCustomPlayer((Player) event.getWhoClicked()).getBalance() >= getPrice()) { Core.economy.withdrawPlayer((Player) event.getWhoClicked(), getPrice()); Core.getCustomPlayer((Player) event.getWhoClicked()) .addAmmo(type.toString().toLowerCase(), getResultAmmoAmount()); event.getWhoClicked().sendMessage(MessageManager.getMessage("Successful-Purchase")); if (openGadgetsInvAfterAmmo) Bukkit.getScheduler() .runTaskLater( Core.getPlugin(), new Runnable() { @Override public void run() { GadgetManager.openMenu((Player) event.getWhoClicked(), lastPage); openGadgetsInvAfterAmmo = false; lastPage = 1; } }, 1); } else { getPlayer().sendMessage(MessageManager.getMessage("Not-Enough-Money")); } event.getWhoClicked().closeInventory(); } else if (displayName.equals(cancel)) { event.getWhoClicked().closeInventory(); } } } }
public static void activateHat(Hat hat, final Player PLAYER) { if (!PLAYER.hasPermission(hat.getPermission())) { if (!playerList.contains(PLAYER)) { PLAYER.sendMessage(MessageManager.getMessage("No-Permission")); playerList.add(PLAYER); Bukkit.getScheduler() .runTaskLaterAsynchronously( Core.getPlugin(), new Runnable() { @Override public void run() { playerList.remove(PLAYER); } }, 1); } return; } Core.getCustomPlayer(PLAYER).setHat(hat); }
@EventHandler protected void onItemDrop(PlayerDropItemEvent event) { if (event.getItemDrop().getItemStack().getType() == type.getMaterial()) { if (event.getItemDrop().getItemStack().getData().getData() == type.getData()) { if (event.getItemDrop().getItemStack().getItemMeta().hasDisplayName()) { if (event .getItemDrop() .getItemStack() .getItemMeta() .getDisplayName() .endsWith(getName())) { if (SettingsManager.getConfig().getBoolean("Remove-Gadget-With-Drop")) { Core.getCustomPlayer(getPlayer()).removeGadget(); event.getItemDrop().remove(); return; } event.setCancelled(true); } } } } }
public Gadget(final UUID owner, final GadgetType type) { this.permission = type.permission; this.type = type; this.affectPlayers = type.affectPlayers(); if (!type.isEnabled()) return; this.useTwoInteractMethods = false; if (owner != null) { this.owner = owner; if (Core.getCustomPlayer(getPlayer()).currentGadget != null) Core.getCustomPlayer(getPlayer()).removeGadget(); if (!getPlayer().hasPermission(permission)) { getPlayer().sendMessage(MessageManager.getMessage("No-Permission")); return; } DecimalFormatSymbols otherSymbols = new DecimalFormatSymbols(Locale.US); otherSymbols.setDecimalSeparator('.'); otherSymbols.setGroupingSeparator('.'); otherSymbols.setPatternSeparator('.'); final DecimalFormat decimalFormat = new DecimalFormat("0.0", otherSymbols); BukkitRunnable runnable = new BukkitRunnable() { @Override public void run() { try { if (Bukkit.getPlayer(owner) != null && Core.getCustomPlayer(Bukkit.getPlayer(owner)).currentGadget != null && Core.getCustomPlayer(Bukkit.getPlayer(owner)).currentGadget.getType() == type) { onUpdate(); if (Core.cooldownInBar) { if (getPlayer().getItemInHand() != null && itemStack != null && getPlayer().getItemInHand().hasItemMeta() && getPlayer().getItemInHand().getItemMeta().hasDisplayName() && getPlayer() .getItemInHand() .getItemMeta() .getDisplayName() .contains(getType().getName()) && Core.getCustomPlayer(getPlayer()).canUse(type) != -1) sendCooldownBar(); double left = Core.getCustomPlayer(getPlayer()).canUse(type); if (left > -0.1) { String leftRounded = decimalFormat.format(left); double decimalRoundedValue = Double.parseDouble(leftRounded); if (decimalRoundedValue == 0) { PlayerUtils.sendInActionBar( getPlayer(), MessageManager.getMessage("Gadgets.Gadget-Ready-ActionBar") .replace( "%gadgetname%", (Core.placeHolderColor) ? getName() : Core.filterColor(getName()))); getPlayer().playSound(getPlayer().getLocation(), Sound.NOTE_STICKS, 1f, 1f); } } } } else { cancel(); unregisterListeners(); } } catch (NullPointerException exc) { removeItem(); onClear(); removeListener(); getPlayer() .sendMessage( MessageManager.getMessage("Gadgets.Unequip") .replace( "%gadgetname%", (Core.placeHolderColor) ? getName() : Core.filterColor(getName()))); cancel(); } } }; runnable.runTaskTimerAsynchronously(Core.getPlugin(), 0, 1); listener = new GadgetListener(this); Core.registerListener(listener); Core.registerListener(this); if (getPlayer().getInventory().getItem((int) SettingsManager.getConfig().get("Gadget-Slot")) != null) { getPlayer() .getWorld() .dropItem( getPlayer().getLocation(), getPlayer() .getInventory() .getItem((int) SettingsManager.getConfig().get("Gadget-Slot"))); getPlayer().getInventory().remove((int) SettingsManager.getConfig().get("Gadget-Slot")); } String d = Core.isAmmoEnabled() && getType().requiresAmmo() ? "§f§l" + Core.getCustomPlayer(getPlayer()).getAmmo(type.toString().toLowerCase()) + " " : ""; itemStack = ItemFactory.create( type.getMaterial(), type.getData(), d + getName(), MessageManager.getMessage("Gadgets.Lore")); getPlayer() .getInventory() .setItem((int) SettingsManager.getConfig().get("Gadget-Slot"), itemStack); getPlayer() .sendMessage( MessageManager.getMessage("Gadgets.Equip") .replace( "%gadgetname%", (Core.placeHolderColor) ? getName() : Core.filterColor(getName()))); Core.getCustomPlayer(getPlayer()).currentGadget = this; } }
@EventHandler protected void onPlayerInteract(final PlayerInteractEvent EVENT) { Player player = EVENT.getPlayer(); UUID uuid = player.getUniqueId(); CustomPlayer cp = Core.getCustomPlayer(getPlayer()); if (!uuid.equals(gadget.owner)) return; ItemStack itemStack = player.getItemInHand(); if (itemStack.getType() != gadget.getMaterial()) return; if (itemStack.getData().getData() != gadget.getData()) return; if (player.getInventory().getHeldItemSlot() != (int) SettingsManager.getConfig().get("Gadget-Slot")) return; if (Core.getCustomPlayer(getPlayer()).currentGadget != gadget) return; if (EVENT.getAction() == Action.PHYSICAL) return; EVENT.setCancelled(true); player.updateInventory(); if (!Core.getCustomPlayer(getPlayer()).hasGadgetsEnabled()) { getPlayer().sendMessage(MessageManager.getMessage("Gadgets-Enabled-Needed")); return; } if (Core.getCustomPlayer(getPlayer()).currentTreasureChest != null) return; if (Core.isAmmoEnabled() && getType().requiresAmmo()) { if (Core.getCustomPlayer(getPlayer()).getAmmo(getType().toString().toLowerCase()) < 1) { openAmmoPurchaseMenu(); return; } } if (type == GadgetType.PORTALGUN) { if (getPlayer().getTargetBlock((Set<Material>) null, 20).getType() == Material.AIR) { getPlayer().sendMessage(MessageManager.getMessage("Gadgets.PortalGun.No-Block-Range")); return; } } if (type == GadgetType.ROCKET) { boolean pathClear = true; Cuboid c = new Cuboid( getPlayer().getLocation().add(-1, 0, -1), getPlayer().getLocation().add(1, 75, 1)); if (!c.isEmpty()) { getPlayer().sendMessage(MessageManager.getMessage("Gadgets.Rocket.Not-Enough-Space")); return; } if (!getPlayer().isOnGround()) { getPlayer().sendMessage(MessageManager.getMessage("Gadgets.Rocket.Not-On-Ground")); return; } } if (type == GadgetType.DISCOBALL) { if (Core.discoBalls.size() > 0) { getPlayer().sendMessage(MessageManager.getMessage("Gadgets.DiscoBall.Already-Active")); return; } if (getPlayer().getLocation().add(0, 4, 0).getBlock() != null && getPlayer().getLocation().add(0, 4, 0).getBlock().getType() != Material.AIR) { getPlayer().sendMessage(MessageManager.getMessage("Gadgets.DiscoBall.Not-Space-Above")); return; } } if (type == GadgetType.CHRISTMASTREE) { if (EVENT.getClickedBlock() == null || EVENT.getClickedBlock().getType() == Material.AIR) { getPlayer() .sendMessage(MessageManager.getMessage("Gadgets.ChristmasTree.Click-On-Block")); return; } } if (type == GadgetType.TRAMPOLINE) { // Check blocks above. Location loc1 = getPlayer().getLocation().add(2, 15, 2); Location loc2 = getPlayer().getLocation().clone().add(-2, 0, -2); Block block = loc1.getBlock().getRelative(3, 0, 0); Block block2 = loc1.getBlock().getRelative(3, 1, 0); Cuboid checkCuboid = new Cuboid(loc1, loc2); if (!checkCuboid.isEmpty() || block.getType() != Material.AIR || block2.getType() != Material.AIR) { getPlayer().sendMessage(MessageManager.getMessage("Gadgets.Rocket.Not-Enough-Space")); return; } } // Check for the parachute if there is space 30-40 blocks above the player to avoid problems. if (type == GadgetType.PARACHUTE) { // Check blocks above. Location loc1 = getPlayer().getLocation().add(2, 28, 2); Location loc2 = getPlayer().getLocation().clone().add(-2, 40, -2); Cuboid checkCuboid = new Cuboid(loc1, loc2); if (!checkCuboid.isEmpty()) { getPlayer().sendMessage(MessageManager.getMessage("Gadgets.Rocket.Not-Enough-Space")); return; } } if (type == GadgetType.EXPLOSIVESHEEP) { if (Core.explosiveSheep.size() > 0) { getPlayer() .sendMessage(MessageManager.getMessage("Gadgets.ExplosiveSheep.Already-Active")); return; } } double coolDown = cp.canUse(getType()); if (coolDown != -1) { String timeLeft = new DecimalFormat("#.#").format(coolDown); if (type.getCountdown() > 1) getPlayer() .sendMessage( MessageManager.getMessage("Gadgets.Countdown-Message") .replace( "%gadgetname%", (Core.placeHolderColor) ? getName() : Core.filterColor(getName())) .replace("%time%", timeLeft)); return; } else cp.setCoolDown(getType(), type.getCountdown()); if (Core.isAmmoEnabled() && getType().requiresAmmo()) { Core.getCustomPlayer(getPlayer()).removeAmmo(getType().toString().toLowerCase()); itemStack = ItemFactory.create( type.getMaterial(), type.getData(), "§f§l" + Core.getCustomPlayer(getPlayer()).getAmmo(type.toString().toLowerCase()) + " " + getName(), MessageManager.getMessage("Gadgets.Lore")); getPlayer() .getInventory() .setItem((int) SettingsManager.getConfig().get("Gadget-Slot"), itemStack); } if (EVENT.getClickedBlock() != null && EVENT.getClickedBlock().getType() != Material.AIR) lastClickedBlock = EVENT.getClickedBlock(); if (asyncAction) { Bukkit.getScheduler() .runTaskAsynchronously( Core.getPlugin(), new BukkitRunnable() { @Override public void run() { if (useTwoInteractMethods) { if (EVENT.getAction() == Action.RIGHT_CLICK_AIR || EVENT.getAction() == Action.RIGHT_CLICK_BLOCK) onRightClick(); else if (EVENT.getAction() == Action.LEFT_CLICK_BLOCK || EVENT.getAction() == Action.LEFT_CLICK_AIR) onLeftClick(); } else { onRightClick(); } } }); } else { if (useTwoInteractMethods) { if (EVENT.getAction() == Action.RIGHT_CLICK_AIR || EVENT.getAction() == Action.RIGHT_CLICK_BLOCK) onRightClick(); else if (EVENT.getAction() == Action.LEFT_CLICK_BLOCK || EVENT.getAction() == Action.LEFT_CLICK_AIR) onLeftClick(); } else { onRightClick(); } } }
@EventHandler public void hatSelection(InventoryClickEvent event) { if (event.getInventory().getTitle().startsWith(MessageManager.getMessage("Menus.Hats"))) { event.setCancelled(true); if (event.getCurrentItem() == null || !event.getCurrentItem().hasItemMeta() || !event.getCurrentItem().getItemMeta().hasDisplayName()) return; if (event.getCurrentItem().getItemMeta().hasDisplayName()) { if (event .getCurrentItem() .getItemMeta() .getDisplayName() .equals(MessageManager.getMessage("Menu.Hats")) || event.getCurrentItem().getType() == Material.STAINED_GLASS_PANE) { return; } if (event .getCurrentItem() .getItemMeta() .getDisplayName() .equals(MessageManager.getMessage("Menu.Main-Menu"))) { MainMenuManager.openMainMenu((Player) event.getWhoClicked()); return; } else if (event .getCurrentItem() .getItemMeta() .getDisplayName() .equals(MessageManager.getMessage("Clear-Hat"))) { if (Core.getCustomPlayer((Player) event.getWhoClicked()).currentHat != null) { int currentPage = getCurrentPage((Player) event.getWhoClicked()); event.getWhoClicked().closeInventory(); Core.getCustomPlayer((Player) event.getWhoClicked()).removeHat(); openHatsMenu((Player) event.getWhoClicked(), currentPage); } else return; return; } else if (event .getCurrentItem() .getItemMeta() .getDisplayName() .equalsIgnoreCase(MessageManager.getMessage("Menu.Next-Page"))) { openHatsMenu( (Player) event.getWhoClicked(), getCurrentPage((Player) event.getWhoClicked()) + 1); return; } else if (event .getCurrentItem() .getItemMeta() .getDisplayName() .equalsIgnoreCase(MessageManager.getMessage("Menu.Previous-Page"))) { openHatsMenu( (Player) event.getWhoClicked(), getCurrentPage((Player) event.getWhoClicked()) - 1); return; } event.getWhoClicked().closeInventory(); if (event .getCurrentItem() .getItemMeta() .getDisplayName() .startsWith(MessageManager.getMessage("Menu.Unequip"))) { Core.getCustomPlayer((Player) event.getWhoClicked()).removeHat(); return; } else if (event .getCurrentItem() .getItemMeta() .getDisplayName() .startsWith(MessageManager.getMessage("Menu.Equip"))) { Core.getCustomPlayer((Player) event.getWhoClicked()).removeHat(); StringBuilder sb = new StringBuilder(); String name = event .getCurrentItem() .getItemMeta() .getDisplayName() .replace(MessageManager.getMessage("Menu.Equip"), ""); int j = name.split(" ").length; if (name.contains("(")) j--; for (int i = 1; i < j; i++) { sb.append(name.split(" ")[i]); try { if (event.getCurrentItem().getItemMeta().getDisplayName().split(" ")[i + 1] != null) sb.append(" "); } catch (Exception exc) { } } activateHat(getHatByType(sb.toString()), (Player) event.getWhoClicked()); } } } }