예제 #1
0
 @SuppressWarnings("deprecation")
 @EventHandler
 public void onProjectileFire(ProjectileLaunchEvent e) {
   ProjectileSource shooter = e.getEntity().getShooter();
   if (shooter instanceof Player) {
     Player player = (Player) shooter;
     ItemStack item = player.getItemInHand();
     RPGItem rItem = ItemManager.toRPGItem(item);
     if (rItem == null) return;
     if (!WorldGuard.canPvP(player.getLocation()) && !rItem.ignoreWorldGuard) return;
     if (rItem.getHasPermission() == true
         && player.hasPermission(rItem.getPermission()) == false) {
       e.setCancelled(true);
       player.sendMessage(
           ChatColor.RED
               + String.format(
                   Locale.get("message.error.permission", Locale.getPlayerLocale(player))));
     }
     RPGMetadata meta = RPGItem.getMetadata(item);
     if (rItem.getMaxDurability() != -1) {
       int durability =
           meta.containsKey(RPGMetadata.DURABILITY)
               ? ((Number) meta.get(RPGMetadata.DURABILITY)).intValue()
               : rItem.getMaxDurability();
       durability--;
       if (durability <= 0) {
         player.setItemInHand(null);
       }
       meta.put(RPGMetadata.DURABILITY, Integer.valueOf(durability));
     }
     RPGItem.updateItem(item, Locale.getPlayerLocale(player), meta);
     player.updateInventory();
     rpgProjectiles.put(e.getEntity().getEntityId(), rItem.getID());
   }
 }
예제 #2
0
  public void addExtra(RPGMetadata rpgMeta, ItemStack item, List<String> lore) {
    if (maxDurability != -1) {
      if (!rpgMeta.containsKey(RPGMetadata.DURABILITY)) {
        rpgMeta.put(RPGMetadata.DURABILITY, maxDurability);
      }
      int durability = ((Number) rpgMeta.get(RPGMetadata.DURABILITY)).intValue();

      if (!hasBar || forceBar) {
        StringBuilder out = new StringBuilder();
        char boxChar = '\u25A0';
        int boxCount = tooltipWidth / 4;
        int mid = (int) ((double) boxCount * ((double) durability / (double) maxDurability));
        for (int i = 0; i < boxCount; i++) {
          out.append(i < mid ? ChatColor.GREEN : i == mid ? ChatColor.YELLOW : ChatColor.RED);
          out.append(boxChar);
        }
        lore.add(out.toString());
      }
      if (hasBar) {
        item.setDurability(
            (short)
                (item.getType().getMaxDurability()
                    - ((short)
                        ((double) item.getType().getMaxDurability()
                            * ((double) durability / (double) maxDurability)))));
      }
    } else {
      item.setDurability(hasBar ? (short) 0 : this.item.getDurability());
    }
  }
예제 #3
0
 public ItemStack toItemStack(String locale) {
   ItemStack rStack = item.clone();
   RPGMetadata rpgMeta = new RPGMetadata();
   ItemMeta meta = getLocaleMeta(locale);
   List<String> lore = meta.getLore();
   lore.set(0, meta.getLore().get(0) + rpgMeta.toMCString());
   addExtra(rpgMeta, rStack, lore);
   meta.setLore(lore);
   rStack.setItemMeta(meta);
   return rStack;
 }
예제 #4
0
  @SuppressWarnings("deprecation")
  private double playerDamager(EntityDamageByEntityEvent e, double damage) {
    Player player = (Player) e.getDamager();
    ItemStack item = player.getItemInHand();
    if (item.getType() == Material.BOW
        || item.getType() == Material.SNOW_BALL
        || item.getType() == Material.EGG
        || item.getType() == Material.POTION) return damage;

    RPGItem rItem = ItemManager.toRPGItem(item);
    if (rItem == null) return damage;
    if (!WorldGuard.canPvP(player.getLocation()) && !rItem.ignoreWorldGuard) return damage;
    if (rItem.getHasPermission() == true && player.hasPermission(rItem.getPermission()) == false) {
      damage = 0;
      e.setCancelled(true);
      player.sendMessage(
          ChatColor.RED
              + String.format(
                  Locale.get("message.error.permission", Locale.getPlayerLocale(player))));
    }
    damage =
        rItem.getDamageMin() != rItem.getDamageMax()
            ? (rItem.getDamageMin() + random.nextInt(rItem.getDamageMax() - rItem.getDamageMin()))
            : rItem.getDamageMin();
    if (e.getEntity() instanceof LivingEntity) {
      LivingEntity le = (LivingEntity) e.getEntity();
      rItem.hit(player, le, e.getDamage());
    }
    RPGMetadata meta = RPGItem.getMetadata(item);
    if (rItem.getMaxDurability() != -1) {
      int durability =
          meta.containsKey(RPGMetadata.DURABILITY)
              ? ((Number) meta.get(RPGMetadata.DURABILITY)).intValue()
              : rItem.getMaxDurability();
      durability--;
      if (durability <= 0) {
        player.setItemInHand(null);
      }
      meta.put(RPGMetadata.DURABILITY, Integer.valueOf(durability));
    }
    RPGItem.updateItem(item, Locale.getPlayerLocale(player), meta);
    player.updateInventory();
    return damage;
  }
예제 #5
0
 private double playerHit(EntityDamageByEntityEvent e, double damage) {
   Player p = (Player) e.getEntity();
   if (e.isCancelled() || !WorldGuard.canPvP(p.getLocation())) return damage;
   String locale = Locale.getPlayerLocale(p);
   ItemStack[] armour = p.getInventory().getArmorContents();
   for (int i = 0; i < armour.length; i++) {
     ItemStack pArmour = armour[i];
     RPGItem pRItem = ItemManager.toRPGItem(pArmour);
     if (pRItem == null) continue;
     if (!WorldGuard.canPvP(p.getLocation()) && !pRItem.ignoreWorldGuard) return damage;
     if (pRItem.getHasPermission() == true && p.hasPermission(pRItem.getPermission()) == false) {
       damage = 0;
       e.setCancelled(true);
       p.sendMessage(
           ChatColor.RED
               + String.format(Locale.get("message.error.permission", Locale.getPlayerLocale(p))));
     }
     if (pRItem.getArmour() > 0) {
       damage -= Math.round(((double) damage) * (((double) pRItem.getArmour()) / 100d));
     }
     RPGMetadata meta = RPGItem.getMetadata(pArmour);
     if (pRItem.getMaxDurability() != -1) {
       int durability =
           meta.containsKey(RPGMetadata.DURABILITY)
               ? ((Number) meta.get(RPGMetadata.DURABILITY)).intValue()
               : pRItem.getMaxDurability();
       durability--;
       if (durability <= 0) {
         armour[i] = null;
       }
       meta.put(RPGMetadata.DURABILITY, Integer.valueOf(durability));
     }
     RPGItem.updateItem(pArmour, locale, meta);
   }
   p.getInventory().setArmorContents(armour);
   p.updateInventory();
   return damage;
 }
예제 #6
0
 @SuppressWarnings("deprecation")
 @EventHandler(ignoreCancelled = true)
 public void onBlockBreak(BlockBreakEvent e) {
   Player player = e.getPlayer();
   ItemStack item = player.getItemInHand();
   RPGItem rItem;
   if ((rItem = ItemManager.toRPGItem(item)) != null) {
     RPGMetadata meta = RPGItem.getMetadata(item);
     if (rItem.getMaxDurability() != -1) {
       int durability =
           meta.containsKey(RPGMetadata.DURABILITY)
               ? ((Number) meta.get(RPGMetadata.DURABILITY)).intValue()
               : rItem.getMaxDurability();
       durability--;
       if (durability <= 0) {
         player.setItemInHand(null);
       }
       meta.put(RPGMetadata.DURABILITY, Integer.valueOf(durability));
     }
     RPGItem.updateItem(item, Locale.getPlayerLocale(player), meta);
     player.updateInventory();
   }
 }
예제 #7
0
 public static void updateItem(
     ItemStack item, String locale, RPGMetadata rpgMeta, boolean updateDurability) {
   RPGItem rItem = ItemManager.toRPGItem(item);
   if (rItem == null) return;
   item.setType(rItem.item.getType());
   ItemMeta meta = rItem.getLocaleMeta(locale);
   if (!(meta instanceof LeatherArmorMeta) && updateDurability) {
     item.setDurability(rItem.item.getDurability());
   }
   List<String> lore = meta.getLore();
   rItem.addExtra(rpgMeta, item, lore);
   lore.set(0, meta.getLore().get(0) + rpgMeta.toMCString());
   meta.setLore(lore);
   Map<Enchantment, Integer> enchantments = null;
   if (enchantmentSupport) enchantments = item.getEnchantments();
   item.setItemMeta(meta);
   if (enchantmentSupport) item.addEnchantments(enchantments);
 }
예제 #8
0
 public static RPGMetadata getMetadata(ItemStack item) {
   return RPGMetadata.parseLoreline(item.getItemMeta().getLore().get(0));
 }