Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
19 kB
2
Indexable
Never
package ballistickemu.Lobby.handlers;

import ballistickemu.Lobby.LobbyServer;
import ballistickemu.Main;
import ballistickemu.Tools.DatabaseTools;
import ballistickemu.Tools.StickPacketMaker;
import ballistickemu.Tools.StringTool;
import ballistickemu.Types.StickClient;
import ballistickemu.Types.StickColour;
import ballistickemu.Types.StickItem;
import ballistickemu.Types.StickRoom;
import java.io.PrintStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;


public class PlayerCommandHandler
{
  public static void HandlePacket(StickClient paramStickClient, String paramString)
  {
    String[] arrayOfString = paramString.replaceAll("\000", "").split(" ");
    
    if (arrayOfString[0].equalsIgnoreCase("!setcolor"))
    {
      setNewColor(paramStickClient, getArgs(paramString, "!setcolor"), Boolean.valueOf(false));
      return;
    }
    if (arrayOfString[0].equalsIgnoreCase("!setpetcolor"))
    {
      setNewColor(paramStickClient, getArgs(paramString, "!setpetcolor"), Boolean.valueOf(true));
      return;
    }
    if (arrayOfString[0].equalsIgnoreCase("!help"))
    {
      paramStickClient.writeCallbackMessage("All players commands are: !setkills, !setcolor, !setpetcolor, !builder, !cane, !redhead, !fuzzy, !hearts, !setcreds, !stats");
      return;
    }
    if ((arrayOfString[0].equalsIgnoreCase("!setcreds")) && (arrayOfString.length > 1))
    {
      setCreds(paramStickClient, arrayOfString[1]);
      return;
    }
    if ((arrayOfString[0].equalsIgnoreCase("!stats")) && (arrayOfString.length > 1))
    {
      if (arrayOfString.length != 2)
      {
        paramStickClient.writeCallbackMessage("Usage: !stats [username]");
      }
      else
      {
        checkStats(paramStickClient, arrayOfString[1]);
        return;
      }
    }
    if (arrayOfString[0].equalsIgnoreCase("!builder"))
    {
      addSpinner(paramStickClient, getArgs(paramString, "!builder"), 181);
      return;
    }
    if (arrayOfString[0].equalsIgnoreCase("!fuzzy"))
    {
      addSpinner(paramStickClient, getArgs(paramString, "!fuzzy"), 182);
      return;
    }
    if (arrayOfString[0].equalsIgnoreCase("!cane"))
    {
      addSpinner(paramStickClient, getArgs(paramString, "!cane"), 184);
      return;
    }
    if (arrayOfString[0].equalsIgnoreCase("!redhead"))
    {
      addSpinner(paramStickClient, getArgs(paramString, "!redhead"), 186);
      return;
    }
    if (arrayOfString[0].equalsIgnoreCase("!hearts"))
    {
      addSpinner(paramStickClient, getArgs(paramString, "!hearts"), 185);
      return;
    }
    if ((arrayOfString[0].equalsIgnoreCase("!modspinner")) && (paramStickClient.getModStatus().booleanValue()))
    {
      addSpinner(paramStickClient, getArgs(paramString, "!modspinner"), 183);
      return;
    }
    if ((arrayOfString[0].equalsIgnoreCase("!setkills")) && (arrayOfString.length > 1))
    {
      setKills(paramStickClient, arrayOfString[1]);
      return;
    }
  }
  
  private static String[] getArgs(String paramString1, String paramString2)
  {
    String[] arrayOfString = paramString1.substring(paramString2.length() + 1).replaceAll("\000", "").split(" ");
    return arrayOfString;
  }
  
  private static void setNewColor(StickClient paramStickClient, String[] paramArrayOfString, Boolean paramBoolean)
  {
    if (paramArrayOfString.length != 6)
    {
      paramStickClient.writeCallbackMessage("setcolor syntax: !setcolor [red 1] [green 1] [blue 1] [red 2] [green 2] [blue 2]");
      return;
    }
    
    if (!verifyColourInput(paramStickClient, paramArrayOfString).booleanValue())
    {
      paramStickClient.writeCallbackMessage("There was an error setting new colour - no changes have been made.");
      return;
    }
    
    String str1 = paramArrayOfString[0];
    String str2 = paramArrayOfString[1];
    String str3 = paramArrayOfString[2];
    String str4 = paramArrayOfString[3];
    String str5 = paramArrayOfString[4];
    String str6 = paramArrayOfString[5];
    
    StickItem localStickItem = null;
    
    if (paramBoolean.booleanValue())
    {
      localStickItem = paramStickClient.getSelectedPet();
    }
    else {
      localStickItem = paramStickClient.getSelectedSpinner();
    }
    int i = localStickItem.getItemDBID();
    int j = localStickItem.getItemID();
    if (j == 186)
    {
      paramStickClient.writeCallbackMessage("You cannot change redhead spinner color!");
    }
    else if (j == 184)
    {
      paramStickClient.writeCallbackMessage("You cannot change candy cane spinner color!");
    }
    else if (j == 185)
    {
      paramStickClient.writeCallbackMessage("You cannot change valentines spinner color!");
    }
    else
    {
      try
      {
        PreparedStatement localPreparedStatement = DatabaseTools.getDbConnection().prepareStatement("UPDATE inventory SET `red1` = ?, `green1` = ?, `blue1` = ?, `red2` = ?, `green2` = ?, `blue2` = ? WHERE `userid` = (SELECT `UID` from `users` WHERE `username` = ?) AND `id` = ?");
        
        localPreparedStatement.setString(1, str1);
        localPreparedStatement.setString(2, str2);
        localPreparedStatement.setString(3, str3);
        localPreparedStatement.setString(4, str4);
        localPreparedStatement.setString(5, str5);
        localPreparedStatement.setString(6, str6);
        localPreparedStatement.setString(7, paramStickClient.getName());
        localPreparedStatement.setInt(8, i);
        if (localPreparedStatement.executeUpdate() == 1)
        {
          paramStickClient.writeCallbackMessage("Color successfully changed. Please log out to see the changes.");
        }
        else
        {
          paramStickClient.writeCallbackMessage("Updating color failed.");
        }
      }
      catch (SQLException localSQLException)
      {
        System.out.println("Exception changing colour of user " + paramStickClient.getName() + ". Exception thrown: " + localSQLException.toString());
      }
    }
  }
  

  private static void addSpinner(StickClient paramStickClient, String[] paramArrayOfString, int paramInt)
  {
    if ((paramInt == 184) || (paramInt == 185))
    {
      localObject = new String[] { "255", "-99", "-99", "255", "255", "255" };
      paramArrayOfString = (String[])localObject;
    }
    if (paramInt == 186)
    {
      localObject = new String[] { "255", "-95", "-95", "255", "-95", "-95" };
      paramArrayOfString = (String[])localObject;
    }
    if (paramInt == 200)
    {
      localObject = new String[] { "44", "44", "44", "44", "44", "44" };
      paramArrayOfString = (String[])localObject;
    }
    if (paramArrayOfString.length != 6)
    {
      paramStickClient.writeCallbackMessage("!builderspinner / !fuzzyspinner syntax: /builderspinner | /fuzzyspinner [red 1] [green 1] [blue 1] [red 2] [green 2] [blue 2]");
      return;
    }
    
    if (!verifyColourInput(paramStickClient, paramArrayOfString).booleanValue())
    {
      paramStickClient.writeCallbackMessage("There was an error adding new spinner  - no changes have been made.");
      return;
    }
    
    Object localObject = paramArrayOfString[0];
    String str1 = paramArrayOfString[1];
    String str2 = paramArrayOfString[2];
    String str3 = paramArrayOfString[3];
    String str4 = paramArrayOfString[4];
    String str5 = paramArrayOfString[5];
    
    try
    {
      PreparedStatement localPreparedStatement = DatabaseTools.getDbConnection().prepareStatement("INSERT INTO `inventory` (`userid`, `itemid`, `itemtype`, `red1`, `green1`, `blue1`, `red2`, `green2`, `blue2`) VALUES (?, ?, 1, ?, ?, ?, ?, ?, ?)");
      
      localPreparedStatement.setInt(1, paramStickClient.getDbID());
      localPreparedStatement.setInt(2, paramInt);
      localPreparedStatement.setString(3, (String)localObject);
      localPreparedStatement.setString(4, str1);
      localPreparedStatement.setString(5, str2);
      localPreparedStatement.setString(6, str3);
      localPreparedStatement.setString(7, str4);
      localPreparedStatement.setString(8, str5);
      
      int i = localPreparedStatement.executeUpdate();
      if (i > 0)
      {
        paramStickClient.writeCallbackMessage("Item successfully added. Logout and login to see added item!");
        StickColour localStickColour = new StickColour();
        localStickColour.setColour1FromString((String)localObject + str1 + str2);
        localStickColour.setColour2FromString(str3 + str4 + str5);
        paramStickClient.addItemToInventory(i, new StickItem(paramInt, i, paramStickClient.getDbID(), 1, Boolean.valueOf(false), localStickColour));
        paramStickClient.write(StickPacketMaker.getInventoryPacket(paramStickClient.getFormattedInventoryData()));
        return;
      }
      

      paramStickClient.writeCallbackMessage("Updating color failed.");

    }
    catch (SQLException localSQLException)
    {
      System.out.println("Exception changing colour of user " + paramStickClient.getName() + ". Exception thrown: " + localSQLException.toString());
    }
  }
  

  private static Boolean verifyColourInput(StickClient paramStickClient, String[] paramArrayOfString)
  {
    String str1 = paramArrayOfString[0];
    String str2 = paramArrayOfString[1];
    String str3 = paramArrayOfString[2];
    String str4 = paramArrayOfString[3];
    String str5 = paramArrayOfString[4];
    String str6 = paramArrayOfString[5];
    
    int i = 1000;
    int j = 1000;
    int k = 1000;
    int m = 1000;
    int n = 1000;
    int i1 = 1000;
    
    try
    {
      i = Integer.parseInt(str1);
      j = Integer.parseInt(str2);
      k = Integer.parseInt(str3);
      m = Integer.parseInt(str4);
      n = Integer.parseInt(str5);
      i1 = Integer.parseInt(str6);
    }
    catch (NumberFormatException localNumberFormatException) {
      paramStickClient.writeCallbackMessage("Error changing color: One of the supplied arguments was invalid.");
      return Boolean.valueOf(false);
    }
    
    if ((i > 255) || (j > 255) || (k > 255) || (m > 255) || (n > 255) || (i1 > 255))
    {
      paramStickClient.writeCallbackMessage("Error changing color: Max value for arguments is 255.");
      return Boolean.valueOf(false);
    }
    
    if ((i < -99) || (j < -99) || (k < -99) || (m < -99) || (n < -99) || (i1 < -99))
    {
      paramStickClient.writeCallbackMessage("Error changing color: Minimum value for arguments is -99.");
      return Boolean.valueOf(false);
    }
    return Boolean.valueOf(true);
  }
  
  private static void setCreds(StickClient paramStickClient, String paramString)
  {
    int i = -1;
    
    try
    {
      i = Integer.parseInt(paramString);
    }
    catch (NumberFormatException localNumberFormatException)
    {
      paramStickClient.writeCallbackMessage("Error setting creds: Invalid number entered.");
      return;
    }
    
    if (i > 50000)
    {
      paramStickClient.writeCallbackMessage("Error setting creds: 50000 is max amount.");
      return;
    }
    
    if (i < 0)
    {
      paramStickClient.writeCallbackMessage("Error setting creds: 0 is minimum amount.");
      return;
    }
    
    try
    {
      PreparedStatement localPreparedStatement = DatabaseTools.getDbConnection().prepareStatement("UPDATE users SET `cash` = ? WHERE `UID` = ?");
      localPreparedStatement.setInt(1, paramStickClient.getCash());
      localPreparedStatement.setInt(2, paramStickClient.getDbID());
      localPreparedStatement.executeUpdate();
    }
    catch (Exception localException)
    {
      System.out.println("Error setting creds; something went wrong with the DB query.");
      localException.printStackTrace();
      return;
    }
    
    paramStickClient.setCash(i);
    
    if (paramStickClient.getLobbyStatus().booleanValue())
    {
      Main.getLobbyServer().BroadcastPacket(StickPacketMaker.getClientInfo(paramStickClient.getUID(), paramStickClient.getName(), paramStickClient.getSelectedSpinner().getColour().getColour1AsString(), paramStickClient.getKills(), paramStickClient
        .getDeaths(), paramStickClient.getWins(), paramStickClient.getLosses(), paramStickClient.getRounds(), paramStickClient.getPass().booleanValue() ? 1 : 0, paramStickClient.getUserLevel()), Boolean.valueOf(true), paramStickClient.getUID());
    } else if (paramStickClient.getRoom() != null)
    {
      String str1 = StringTool.PadStringLeft(String.valueOf(paramStickClient.getSelectedPet().getItemID() - 200), "0", 2);
      String str2 = paramStickClient.getSelectedPet().getColour().getColour1AsString();
      String str3 = paramStickClient.getSelectedPet().getColour().getColour2AsString();
      
      String str4 = StringTool.PadStringLeft(String.valueOf(paramStickClient.getSelectedSpinner().getItemID() - 100), "0", 2);
      String str5 = paramStickClient.getSelectedSpinner().getColour().getColour1AsString();
      String str6 = paramStickClient.getSelectedSpinner().getColour().getColour2AsString();
      
      paramStickClient.getRoom().BroadcastToRoom(StickPacketMaker.getUserDataGame(paramStickClient.getUID(), paramStickClient.getGameWins(), paramStickClient.getGameKills(), paramStickClient
        .getGameDeaths(), StringTool.PadStringLeft(paramStickClient.getName(), "#", 20), str4, str5, str6, paramStickClient
        .getKills(), str1, str2, str3));
    }
    paramStickClient.writeCallbackMessage("Creds successfully set to + " + paramString + ". Do this command again, log out and login!");
  }
  
  private static void setKills(StickClient paramStickClient, String paramString)
  {
    int i = -1;
    
    try
    {
      i = Integer.parseInt(paramString);
    }
    catch (NumberFormatException localNumberFormatException)
    {
      paramStickClient.writeCallbackMessage("Error setting kills: Invalid number entered.");
      return;
    }
    
    if (i > 9000000)
    {
      paramStickClient.writeCallbackMessage("Error setting kills: 9000000 is max amount.");
      return;
    }
    
    if (i < 0)
    {
      paramStickClient.writeCallbackMessage("Error setting kills: 0 is minimum amount.");
      return;
    }
    
    try
    {
      PreparedStatement localPreparedStatement = DatabaseTools.getDbConnection().prepareStatement("UPDATE users SET `kills` = ? WHERE `UID` = ?");
      localPreparedStatement.setInt(1, paramStickClient.getKills());
      localPreparedStatement.setInt(2, paramStickClient.getDbID());
      localPreparedStatement.executeUpdate();
    }
    catch (Exception localException)
    {
      System.out.println("Error setting kills; something went wrong with the DB query.");
      localException.printStackTrace();
      return;
    }
    
    paramStickClient.setKills(i);
    
    if (paramStickClient.getLobbyStatus().booleanValue())
    {
      Main.getLobbyServer().BroadcastPacket(StickPacketMaker.getClientInfo(paramStickClient.getUID(), paramStickClient.getName(), paramStickClient.getSelectedSpinner().getColour().getColour1AsString(), paramStickClient.getKills(), paramStickClient
        .getDeaths(), paramStickClient.getWins(), paramStickClient.getLosses(), paramStickClient.getRounds(), paramStickClient.getPass().booleanValue() ? 1 : 0, paramStickClient.getUserLevel()), Boolean.valueOf(true), paramStickClient.getUID());
    } else if (paramStickClient.getRoom() != null)
    {
      String str1 = StringTool.PadStringLeft(String.valueOf(paramStickClient.getSelectedPet().getItemID() - 200), "0", 2);
      String str2 = paramStickClient.getSelectedPet().getColour().getColour1AsString();
      String str3 = paramStickClient.getSelectedPet().getColour().getColour2AsString();
      
      String str4 = StringTool.PadStringLeft(String.valueOf(paramStickClient.getSelectedSpinner().getItemID() - 100), "0", 2);
      String str5 = paramStickClient.getSelectedSpinner().getColour().getColour1AsString();
      String str6 = paramStickClient.getSelectedSpinner().getColour().getColour2AsString();
      
      paramStickClient.getRoom().BroadcastToRoom(StickPacketMaker.getUserDataGame(paramStickClient.getUID(), paramStickClient.getGameWins(), paramStickClient.getGameKills(), paramStickClient
        .getGameDeaths(), StringTool.PadStringLeft(paramStickClient.getName(), "#", 20), str4, str5, str6, paramStickClient
        .getKills(), str1, str2, str3));
    }
    paramStickClient.writeCallbackMessage("Kills successfully set to + " + paramString + ". Note: for this to work, you need to set the same kills twice and log in again.");
  }
  
  private static void checkStats(StickClient paramStickClient, String paramString)
  {
    try {
      if ((DatabaseTools.getDbConnection().isClosed()) || (DatabaseTools.getDbConnection() == null))
        DatabaseTools.dbConnect();
      PreparedStatement localPreparedStatement = DatabaseTools.getDbConnection().prepareStatement("SELECT * FROM `users` WHERE `USERname` = ?");
      localPreparedStatement.setString(1, paramString);
      ResultSet localResultSet = localPreparedStatement.executeQuery();
      localResultSet.last();
      int i = localResultSet.getRow();
      
      if (i < 1)
      {
        paramStickClient.writeCallbackMessage("User does not exist!");
        return;
      }
      

      int j = localResultSet.getInt("kills");
      int k = localResultSet.getInt("deaths");
      int m = localResultSet.getInt("wins");
      int n = localResultSet.getInt("losses");
      int i1 = localResultSet.getInt("rounds");
      int i2 = localResultSet.getInt("labpass");
      int i3 = localResultSet.getInt("user_level");
      int i4 = localResultSet.getInt("ban");
      if ((i2 > 0) && (i3 > 0))
      {
        paramStickClient.writeCallbackMessage("Stats: kills: " + j + " / Deaths: " + k + " / Wins: " + m + " / Losses: " + n + " / Total Rounds: " + i1 + " / LabPass: Yes / Mod: Yes");
      }
      else if ((i2 == 0) && (i3 == 0))
      {
        paramStickClient.writeCallbackMessage("Stats: kills: " + j + " / Deaths: " + k + " / Wins: " + m + " / Losses: " + n + " / Total Rounds: " + i1 + " / LabPass: No / Mod: No");
      }
      else if ((i2 > 0) && (i3 == 0))
      {
        paramStickClient.writeCallbackMessage("Stats: kills: " + j + " / Deaths: " + k + " / Wins: " + m + " / Losses: " + n + " / Total Rounds: " + i1 + " / LabPass: Yes / Mod: No");
      }
      else if ((i2 == 0) && (i3 > 0))
      {
        paramStickClient.writeCallbackMessage("Stats: kills: " + j + " / Deaths: " + k + " / Wins: " + m + " / Losses: " + n + " / Total Rounds: " + i1 + " / LabPass: No / Mod: Yes");
      }
      if (i4 == 1)
      {
        paramStickClient.writeCallbackMessage("This user is banned");
      }
      
    }
    catch (Exception localException)
    {
      paramStickClient.writeCallbackMessage("There was an error checking stats.");
      return;
    }
  }
}