Пример #1
0
  private static void populateInventory(Inventory inventory, int page, String group) {
    try {
      int count = 0;
      int start = (page - 1) * chestSize;
      int groupId = Karma.getGroupId(group);
      if (groupId == -1) {
        if (RootConfig.getBoolean(ConfigNode.DEBUG_INVENTORY)) {
          plugin.getLogger().warning("Tried to populateInventory for invalid group: " + group);
        }
        return;
      }
      Query itemList =
          plugin
              .getDatabaseHandler()
              .select(
                  "SELECT * FROM " + Table.ITEMS.getName() + " WHERE groups='" + groupId + "';");
      if (itemList.getResult().next()) {
        boolean done = false;
        do {
          // Generate item
          int id = itemList.getResult().getInt("itemid");
          int amount = itemList.getResult().getInt("amount");
          byte data = itemList.getResult().getByte("data");
          short dur = itemList.getResult().getShort("durability");
          ItemStack item = null;
          if (Item.isTool(id)) {
            item = new ItemStack(id, amount, dur);
          } else {
            item = new ItemStack(id, amount, dur, data);
          }
          // Generate psudo item to calculate slots taken up
          int maxStack = item.getType().getMaxStackSize();
          if (maxStack <= 0) {
            maxStack = 1;
          }
          int stacks = amount / maxStack;
          final double rem = (double) amount % (double) maxStack;
          if (rem != 0) {
            stacks++;
          }
          for (int x = 0; x < stacks; x++) {
            ItemStack add = item.clone();
            if (amount < maxStack) {
              add.setAmount(amount);
            } else {
              add.setAmount(maxStack);
              amount -= maxStack;
            }

            if (count >= start) {
              Item meta = new Item(id, data, dur);
              try {
                // If tool
                if (meta.isTool()) {
                  // Check for enchantments
                  String enchantments = itemList.getResult().getString("enchantments");
                  if (!itemList.getResult().wasNull()) {
                    if (!enchantments.equals("")) {
                      String[] cut = enchantments.split("i");
                      for (int s = 0; s < cut.length; s++) {
                        String[] cutter = cut[s].split("v");
                        EnchantmentWrapper e = new EnchantmentWrapper(Integer.parseInt(cutter[0]));
                        add.addUnsafeEnchantment(e.getEnchantment(), Integer.parseInt(cutter[1]));
                      }
                    }
                  }
                  final HashMap<Integer, ItemStack> residual = inventory.addItem(add);
                  if (!residual.isEmpty()) {
                    done = true;
                  }
                } else if (meta.isPotion()) {
                  // Remove data for full potion compatibility
                  item = new ItemStack(id, amount, dur);
                  final HashMap<Integer, ItemStack> residual = inventory.addItem(add);
                  if (!residual.isEmpty()) {
                    done = true;
                  }
                } else {
                  final HashMap<Integer, ItemStack> residual = inventory.addItem(add);
                  if (!residual.isEmpty()) {
                    done = true;
                  }
                }
              } catch (NumberFormatException e) {
                // Ignore faulty item
              }
            }
            count++;
          }
        } while (itemList.getResult().next() && !done);
      } else {
        // No items to add.
        inventory.clear();
      }
      // Close select
      itemList.closeQuery();
    } catch (SQLException e) {
      if (RootConfig.getBoolean(ConfigNode.DEBUG_INVENTORY)) {
        plugin
            .getLogger()
            .warning(
                "SQLException on populateInventory("
                    + inventory.getName()
                    + ","
                    + page
                    + ","
                    + group
                    + ")");
      }
      e.printStackTrace();
    }
  }
Пример #2
0
  public static boolean removePlayerFromGroup(CommandSender sender, String name, String group) {
    try {
      int groupId = Karma.getGroupId(group);
      if (groupId == -1) {
        sender.sendMessage(
            ChatColor.RED
                + KarmicShare.TAG
                + " Invalid group '"
                + ChatColor.GRAY
                + group
                + ChatColor.RED
                + "'");
        return false;
      }
      String groups = "";
      Query rs =
          plugin
              .getDatabaseHandler()
              .select(
                  "SELECT * FROM " + Table.PLAYERS.getName() + " WHERE playername='" + name + "';");
      if (rs.getResult().next()) {
        groups = rs.getResult().getString("groups");
        if (!rs.getResult().wasNull()) {
          if (groups.contains("&")) {
            // Multigroup
            final StringBuilder sb = new StringBuilder();
            for (String s : groups.split("&")) {
              try {
                int id = Integer.parseInt(s);
                // plugin.getLogger().info(s);
                // Add back all groups excluding specified group
                if (id != groupId) {
                  sb.append(s + "&");
                }
              } catch (NumberFormatException n) {

              }
            }
            // Remove trailing ampersand
            sb.deleteCharAt(sb.length() - 1);
            groups = sb.toString();
          } else {
            groups = "";
          }
        }
      }
      rs.closeQuery();
      // Update their groups
      plugin
          .getDatabaseHandler()
          .standardQuery(
              "UPDATE "
                  + Table.PLAYERS.getName()
                  + " SET groups='"
                  + groups
                  + "' WHERE playername='"
                  + name
                  + "';");
    } catch (SQLException e) {
      if (RootConfig.getBoolean(ConfigNode.DEBUG_KARMA)) {
        plugin
            .getLogger()
            .warning(
                "SQLException on removePlayerFromGroup("
                    + sender.getName()
                    + ","
                    + name
                    + ","
                    + group
                    + ")");
      }
      sender.sendMessage(ChatColor.RED + KarmicShare.TAG + " SQL Exception");
      e.printStackTrace();
      return false;
    }
    if (RootConfig.getBoolean(ConfigNode.DEBUG_KARMA)) {
      plugin
          .getLogger()
          .info("removePlayerFromGroup(" + sender.getName() + "," + name + "," + group + ")");
    }
    return true;
  }
Пример #3
0
 public static boolean addPlayerToGroup(CommandSender sender, String name, String group) {
   try {
     // Ensure that the player's default groups get added in
     getPlayerGroups(sender, name);
     int groupId = Karma.getGroupId(group);
     if (groupId == -1) {
       if (RootConfig.getBoolean(ConfigNode.DEBUG_KARMA)) {
         plugin.getLogger().info("Invalid group " + group + " for addPlayerToGroup");
       }
       sender.sendMessage(
           ChatColor.RED
               + KarmicShare.TAG
               + " Invalid group '"
               + ChatColor.GRAY
               + group
               + ChatColor.RED
               + "'");
       return false;
     }
     final int selfGroup = Karma.getGroupId("self_" + name);
     final int global = Karma.getGroupId("global");
     // plugin.getLogger().info("self: " + selfGroup);
     // plugin.getLogger().info("global: " + global);
     // Insures that the player is added to the database
     getPlayerKarma(name);
     String groups = "";
     Query rs =
         plugin
             .getDatabaseHandler()
             .select(
                 "SELECT * FROM " + Table.PLAYERS.getName() + " WHERE playername='" + name + "';");
     if (rs.getResult().next()) {
       groups = rs.getResult().getString("groups");
       if (!rs.getResult().wasNull()) {
         groups += "&" + groupId;
       } else {
         // Clear
         groups = "";
         // plugin.getLogger().info("null groups");
         if (global != -1) {
           // plugin.getLogger().info("added global");
           groups += global + "&";
         }
         if (selfGroup != -1) {
           // plugin.getLogger().info("added self");
           groups += selfGroup + "&";
         }
         groups += "" + groupId;
       }
     }
     rs.closeQuery();
     // Update their groups
     // plugin.getLogger().info("groups: " + groups);
     plugin
         .getDatabaseHandler()
         .standardQuery(
             "UPDATE "
                 + Table.PLAYERS.getName()
                 + " SET groups='"
                 + groups
                 + "' WHERE playername='"
                 + name
                 + "';");
   } catch (SQLException e) {
     if (RootConfig.getBoolean(ConfigNode.DEBUG_KARMA)) {
       plugin
           .getLogger()
           .warning(
               "SQLException on addPlayerToGroup("
                   + sender.getName()
                   + ","
                   + name
                   + ","
                   + group
                   + ")");
     }
     sender.sendMessage(ChatColor.RED + KarmicShare.TAG + " SQL Exception");
     e.printStackTrace();
     return false;
   }
   if (RootConfig.getBoolean(ConfigNode.DEBUG_KARMA)) {
     plugin
         .getLogger()
         .info("addPlayerToGroup(" + sender.getName() + "," + name + "," + group + ")");
   }
   return true;
 }
Пример #4
0
 public static int grabPage(int current, String group, Direction direction) {
   // Calculate number of slots
   int slots = 0;
   int groupId = Karma.getGroupId(group);
   if (groupId == -1) {
     if (RootConfig.getBoolean(ConfigNode.DEBUG_INVENTORY)) {
       plugin.getLogger().warning("Tried to grabPage for invalid group: " + group);
     }
     return 1;
   }
   final Query all =
       plugin
           .getDatabaseHandler()
           .select("SELECT * FROM " + Table.ITEMS.getName() + " WHERE groups='" + groupId + "';");
   try {
     if (all.getResult().next()) {
       do {
         final int amount = all.getResult().getInt("amount");
         if (!all.getResult().wasNull()) {
           final ItemStack item = new ItemStack(all.getResult().getInt("itemid"), amount);
           if (item != null) {
             int maxStack = item.getType().getMaxStackSize();
             if (maxStack <= 0) {
               maxStack = 1;
             }
             int stacks = amount / maxStack;
             final double rem = (double) amount % (double) maxStack;
             if (rem != 0) {
               stacks++;
             }
             slots += stacks;
           }
         }
       } while (all.getResult().next());
     }
     all.closeQuery();
   } catch (SQLException e) {
     if (RootConfig.getBoolean(ConfigNode.DEBUG_INVENTORY)) {
       plugin
           .getLogger()
           .warning(
               "SQLException on grabPage("
                   + current
                   + ","
                   + group
                   + ","
                   + direction.toString()
                   + ")");
     }
     e.printStackTrace();
   }
   // if no slots, return 1
   if (slots <= 0) {
     return 1;
   }
   // Calculate pages
   int pageTotal = slots / chestSize;
   final double rem = (double) slots % (double) chestSize;
   if (rem != 0) {
     pageTotal++;
   }
   // Check against maximum
   if (current >= Integer.MAX_VALUE) {
     // Cycle back as we're at the max value for an integer
     return 1;
   }
   int page = current;
   switch (direction) {
     case FORWARD:
       {
         page++;
         break;
       }
     case BACKWARD:
       {
         page--;
         break;
       }
     default:
       {
         break;
       }
   }
   if (page <= 0) {
     // Was negative or zero, loop back to max page
     page = (pageTotal + 1);
   }
   // Allow for empty page
   else if (page > (pageTotal + 1)) {
     // Going to page beyond the total items, cycle back to
     // first
     page = 1;
   }
   if (RootConfig.getBoolean(ConfigNode.DEBUG_INVENTORY)) {
     plugin
         .getLogger()
         .info("grabPage(" + current + "," + group + "," + direction.toString() + ") : " + page);
   }
   return page;
 }