Exemple #1
0
 /**
  * Retrieves karma value of a player from the database. Forces player to be added to database if
  * they don't exist
  *
  * @param Player name
  * @return karma value associated with name
  */
 public static int getPlayerKarma(String name) throws SQLException {
   int karma = RootConfig.getInt(ConfigNode.KARMA_PLAYER_DEFAULT);
   if (RootConfig.getBoolean(ConfigNode.KARMA_ECONOMY)) {
     double balance = plugin.getEconomy().getBalance(name);
     karma = (int) balance;
   } else {
     boolean has = false;
     String query =
         "SELECT * FROM " + Table.PLAYERS.getName() + " WHERE playername='" + name + "';";
     final Query rs = plugin.getDatabaseHandler().select(query);
     // Retrieve karma from database
     try {
       if (rs.getResult().next()) {
         do {
           // Grab player karma value
           karma = rs.getResult().getInt("karma");
           has = true;
         } while (rs.getResult().next());
       }
       rs.closeQuery();
       if (!has) {
         if (RootConfig.getBoolean(ConfigNode.DEBUG_KARMA)) {
           plugin.getLogger().info("getPlayerKarma(" + name + ") not in database, adding");
         }
         // Player not in database, therefore add them
         query =
             "INSERT INTO "
                 + Table.PLAYERS.getName()
                 + " (playername,karma) VALUES ('"
                 + name
                 + "','"
                 + karma
                 + "');";
         plugin.getDatabaseHandler().standardQuery(query);
       }
     } catch (SQLException e) {
       if (RootConfig.getBoolean(ConfigNode.DEBUG_KARMA)) {
         plugin.getLogger().warning("SQLException on getPlayerKarma(" + name + ")");
       }
       throw e;
     }
   }
   return karma;
 }
Exemple #2
0
 public static boolean hasPlayerFromPartialName(CommandSender sender, String name) {
   // SQL query to get player count for specified name
   String query =
       "SELECT COUNT(*) FROM " + Table.PLAYERS.getName() + " WHERE playername='" + name + "';";
   Query rs = plugin.getDatabaseHandler().select(query);
   // Check ResultSet
   boolean has = false;
   try {
     if (rs.getResult().next()) {
       // Check if only received 1 entry
       if (rs.getResult().getInt(1) == 1) {
         // we have a single name
         has = true;
       } else if (rs.getResult().getInt(1) > 1) {
         sender.sendMessage(
             ChatColor.RED
                 + KarmicShare.TAG
                 + " Got more than one result. Possibly incomplete name?");
       } else {
         // Player not in database, therefore
         // error
         // on player part
         sender.sendMessage(
             ChatColor.RED
                 + KarmicShare.TAG
                 + " Player "
                 + ChatColor.WHITE
                 + name
                 + ChatColor.RED
                 + " not in database.");
         sender.sendMessage(ChatColor.RED + KarmicShare.TAG + " Player names are case sensitive.");
       }
     } else {
       // Error in query...
       sender.sendMessage(ChatColor.RED + KarmicShare.TAG + " SQL query error");
     }
     rs.closeQuery();
   } catch (SQLException e) {
     sender.sendMessage(ChatColor.RED + KarmicShare.TAG + " Could not set " + name + "'s karma");
     e.printStackTrace();
   }
   return has;
 }
Exemple #3
0
 /**
  * Updates the player's karma
  *
  * @param Name of player
  * @param Amount of karma to add
  * @param Boolean if to add to current karma, or to set it as given value
  * @throws SQLException
  */
 public static void updatePlayerKarma(String name, int k) throws SQLException {
   if (RootConfig.getBoolean(ConfigNode.KARMA_ECONOMY)) {
     EconomyResponse response = null;
     if (k < 0) {
       // negative, take away
       response = plugin.getEconomy().withdrawPlayer(name, (k * -1));
     } else {
       // give
       response = plugin.getEconomy().depositPlayer(name, k);
     }
     if (response == null) {
       return;
     }
     switch (response.type) {
       case FAILURE:
         {
           try {
             plugin
                 .getServer()
                 .getPlayer(name)
                 .sendMessage(ChatColor.RED + KarmicShare.TAG + " Failed economy transaction!");
           } catch (NullPointerException npe) {
             // IGNORE
           }
           if (RootConfig.getBoolean(ConfigNode.DEBUG_ECONOMY)) {
             plugin
                 .getLogger()
                 .warning(
                     "Failed economy transaction on updatePlayerKarma("
                         + name
                         + ", "
                         + k
                         + ") : "
                         + response.errorMessage);
           }
           break;
         }
       case NOT_IMPLEMENTED:
         {
           try {
             plugin
                 .getServer()
                 .getPlayer(name)
                 .sendMessage(ChatColor.RED + KarmicShare.TAG + " Failed economy transaction!");
           } catch (NullPointerException npe) {
             // IGNORE
           }
           if (RootConfig.getBoolean(ConfigNode.DEBUG_ECONOMY)) {
             plugin
                 .getLogger()
                 .warning(
                     "Unimplemented economy transaction on updatePlayerKarma("
                         + name
                         + ", "
                         + k
                         + ") for eco "
                         + plugin.getEconomy().getName()
                         + " : "
                         + response.errorMessage);
           }
           break;
         }
       case SUCCESS:
         {
           if (RootConfig.getBoolean(ConfigNode.DEBUG_ECONOMY)) {
             plugin
                 .getLogger()
                 .info(
                     "Successful economy transaction on updatePlayerKarma("
                         + name
                         + ", "
                         + k
                         + ")");
           }
           break;
         }
       default:
         {
           if (RootConfig.getBoolean(ConfigNode.DEBUG_ECONOMY)) {
             plugin.getLogger().warning("Unknown response type:" + response.type.toString());
           }
           break;
         }
     }
   } else {
     try {
       // Retrieve karma from database
       int karma = getPlayerKarma(name);
       // Add to existing value
       karma += k;
       String query;
       // Check updated karma value to limits in config
       if (karma <= RootConfig.getInt(ConfigNode.KARMA_LOWER_LIMIT)) {
         // Updated karma value is beyond lower limit, so set to min
         query =
             "UPDATE "
                 + Table.PLAYERS.getName()
                 + " SET karma='"
                 + RootConfig.getInt(ConfigNode.KARMA_LOWER_LIMIT)
                 + "' WHERE playername='"
                 + name
                 + "';";
       } else if (karma >= RootConfig.getInt(ConfigNode.KARMA_UPPER_LIMIT)) {
         // Updated karma value is beyond upper limit, so set to max
         query =
             "UPDATE "
                 + Table.PLAYERS.getName()
                 + " SET karma='"
                 + RootConfig.getInt(ConfigNode.KARMA_UPPER_LIMIT)
                 + "' WHERE playername='"
                 + name
                 + "';";
       } else {
         // Updated karma value is within acceptable range
         query =
             "UPDATE "
                 + Table.PLAYERS.getName()
                 + " SET karma='"
                 + karma
                 + "' WHERE playername='"
                 + name
                 + "';";
       }
       plugin.getDatabaseHandler().standardQuery(query);
     } catch (SQLException e) {
       if (RootConfig.getBoolean(ConfigNode.DEBUG_KARMA)) {
         plugin.getLogger().warning("SQLException on updatePlayerKarma(" + name + "," + k + ")");
       }
       throw e;
     }
   }
 }
Exemple #4
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;
 }
Exemple #5
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;
  }
Exemple #6
0
 public static List<String> getPlayerGroups(CommandSender sender, String name) {
   List<String> list = new ArrayList<String>();
   boolean wasNull = false, initial = false;
   try {
     String groups = "";
     Query rs =
         plugin
             .getDatabaseHandler()
             .select(
                 "SELECT * FROM " + Table.PLAYERS.getName() + " WHERE playername='" + name + "';");
     if (rs.getResult().next()) {
       groups = rs.getResult().getString("groups");
       wasNull = rs.getResult().wasNull();
     }
     rs.closeQuery();
     if (wasNull) {
       initial = true;
     } else if (groups.equals("")) {
       initial = true;
     }
     if (initial) {
       if (RootConfig.getBoolean(ConfigNode.DEBUG_KARMA)) {
         plugin.getLogger().info("Initial groups for " + name);
       }
       // No groups, add in the global and self
       plugin
           .getDatabaseHandler()
           .standardQuery(
               "INSERT INTO "
                   + Table.GROUPS.getName()
                   + " (groupname) VALUES ('self_"
                   + name.toLowerCase()
                   + "');");
       groups = getGroupId("global") + "&" + getGroupId("self_" + name.toLowerCase());
       // Set groups for future reference
       plugin
           .getDatabaseHandler()
           .standardQuery(
               "UPDATE "
                   + Table.PLAYERS.getName()
                   + " SET groups='"
                   + groups
                   + "' WHERE playername='"
                   + name
                   + "';");
     }
     String[] split = groups.split("&");
     for (String s : split) {
       try {
         if (s != null && !s.equals("")) {
           list.add(getGroupName(Integer.parseInt(s)));
         }
       } catch (NumberFormatException n) {
         if (RootConfig.getBoolean(ConfigNode.DEBUG_KARMA)) {
           plugin.getLogger().warning("Bad group id '" + s + "'");
         }
         n.printStackTrace();
       }
     }
   } catch (SQLException e) {
     if (RootConfig.getBoolean(ConfigNode.DEBUG_KARMA)) {
       plugin
           .getLogger()
           .warning("SQLException on getPlayerGroups(" + sender.getName() + "," + name + ")");
     }
     sender.sendMessage(ChatColor.RED + KarmicShare.TAG + " SQL Exception");
     e.printStackTrace();
   }
   return list;
 }
Exemple #7
0
 public static boolean playerHasGroup(CommandSender sender, String name, String group) {
   if (group.equals("global")) {
     if (RootConfig.getBoolean(ConfigNode.DEBUG_KARMA)) {
       plugin.getLogger().info(name + " has group " + group);
     }
     return true;
   } else if (group.equals("self_" + name.toLowerCase())) {
     return true;
   }
   boolean has = false;
   try {
     // Insures that the player is added to the database
     getPlayerKarma(name);
     String groups = "";
     final 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("&")) {
           // they have multiple groups
           for (String s : groups.split("&")) {
             try {
               // grab id of given group and compare against
               // ids
               final String groupName = getGroupName(Integer.parseInt(s));
               if (groupName.equalsIgnoreCase(group)) {
                 has = true;
               }
             } catch (NumberFormatException n) {
               // bad group id
               if (RootConfig.getBoolean(ConfigNode.DEBUG_KARMA)) {
                 plugin
                     .getLogger()
                     .warning(
                         "NumberFormatException for playerHasGroup("
                             + sender.getName()
                             + ","
                             + name
                             + ","
                             + group
                             + ")");
               }
             }
           }
         } else {
           try {
             final String groupName = getGroupName(Integer.parseInt(groups));
             // they only have one group
             if (groupName.equalsIgnoreCase(group)) {
               has = true;
             }
           } catch (NumberFormatException n) {
             // bad group id
             if (RootConfig.getBoolean(ConfigNode.DEBUG_KARMA)) {
               plugin
                   .getLogger()
                   .warning(
                       "NumberFormatException for playerHasGroup("
                           + sender.getName()
                           + ","
                           + name
                           + ","
                           + group
                           + ")");
             }
           }
         }
       }
     }
     rs.closeQuery();
   } catch (SQLException e) {
     sender.sendMessage(ChatColor.RED + KarmicShare.TAG + " SQL Exception");
     e.printStackTrace();
   }
   if (RootConfig.getBoolean(ConfigNode.DEBUG_KARMA)) {
     plugin.getLogger().info("Player " + name + " has group " + group + " : " + has);
   }
   return has;
 }
Exemple #8
0
 public static void setPlayerKarma(String name, int karma) {
   if (RootConfig.getBoolean(ConfigNode.KARMA_ECONOMY)) {
     EconomyResponse response = null;
     try {
       int balance = getPlayerKarma(name);
       if (balance > karma) {
         // Give difference
         response = plugin.getEconomy().depositPlayer(name, (balance - karma));
       } else {
         // Take difference
         response = plugin.getEconomy().withdrawPlayer(name, (karma - balance));
       }
     } catch (SQLException e) {
       // Won't happen since we're using economy
     }
     if (response == null) {
       return;
     }
     switch (response.type) {
       case FAILURE:
         {
           try {
             plugin
                 .getServer()
                 .getPlayer(name)
                 .sendMessage(ChatColor.RED + KarmicShare.TAG + " Failed economy transaction!");
           } catch (NullPointerException npe) {
             // IGNORE
           }
           if (RootConfig.getBoolean(ConfigNode.DEBUG_ECONOMY)) {
             plugin
                 .getLogger()
                 .warning(
                     "Failed economy transaction on setPlayerKarma("
                         + name
                         + ", "
                         + karma
                         + ") : "
                         + response.errorMessage);
           }
           break;
         }
       case NOT_IMPLEMENTED:
         {
           try {
             plugin
                 .getServer()
                 .getPlayer(name)
                 .sendMessage(ChatColor.RED + KarmicShare.TAG + " Failed economy transaction!");
           } catch (NullPointerException npe) {
             // IGNORE
           }
           if (RootConfig.getBoolean(ConfigNode.DEBUG_ECONOMY)) {
             plugin
                 .getLogger()
                 .warning(
                     "Unimplemented economy transaction on setPlayerKarma("
                         + name
                         + ", "
                         + karma
                         + ") for eco "
                         + plugin.getEconomy().getName()
                         + " : "
                         + response.errorMessage);
           }
           break;
         }
       case SUCCESS:
         {
           if (RootConfig.getBoolean(ConfigNode.DEBUG_ECONOMY)) {
             plugin
                 .getLogger()
                 .info(
                     "Successful economy transaction on setPlayerKarma("
                         + name
                         + ", "
                         + karma
                         + ")");
           }
           break;
         }
       default:
         {
           if (RootConfig.getBoolean(ConfigNode.DEBUG_ECONOMY)) {
             plugin.getLogger().warning("Unknown response type:" + response.type.toString());
           }
           break;
         }
     }
   } else {
     String query;
     // Check updated karma value to limits in config
     if (karma <= RootConfig.getInt(ConfigNode.KARMA_LOWER_LIMIT)) {
       // Updated karma value is beyond lower limit, so set to min
       query =
           "UPDATE "
               + Table.PLAYERS.getName()
               + " SET karma='"
               + RootConfig.getInt(ConfigNode.KARMA_LOWER_LIMIT)
               + "' WHERE playername='"
               + name
               + "';";
     } else if (karma >= RootConfig.getInt(ConfigNode.KARMA_UPPER_LIMIT)) {
       // Updated karma value is beyond upper limit, so set to max
       query =
           "UPDATE "
               + Table.PLAYERS.getName()
               + " SET karma='"
               + RootConfig.getInt(ConfigNode.KARMA_UPPER_LIMIT)
               + "' WHERE playername='"
               + name
               + "';";
     } else {
       // Updated karma value is within acceptable range
       query =
           "UPDATE "
               + Table.PLAYERS.getName()
               + " SET karma='"
               + karma
               + "' WHERE playername='"
               + name
               + "';";
     }
     plugin.getDatabaseHandler().standardQuery(query);
   }
 }