Solved Help with manipulating a player's inventory / Removing amount of item

Discussion in 'Plugin Development' started by Aphex124, Mar 15, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    Aphex124


    Problem:
    Hi, I've been stumped for the past week or so on trying to get this command to work. i am trying to make it so players can withdraw and deposit gold, for example... Just about every link on Google on the topic of manipulating a player's inventory is purple now.

    Code:
    Code:
                if (args[0].equalsIgnoreCase("deposit")) {
                    Player player = (Player) sender;
                    if (args.length == 1) {
                        player.sendMessage(ChatColor.RED + "You must enter a message.");
                        return true;
                    }
    
                    try{
                    Integer.parseInt(args[1]);
                    } catch (NumberFormatException e){
                    player.sendMessage(ChatColor.RED + "Argument must be an integer/number!");
                    return true;
                    }
                 
                    int current = TokensFile.getInt("Tokens." + player.getName());
                 
                   ItemStack IC2Token = new ItemStack(Material.getMaterial(266),  Integer.valueOf(args[1]));
                  if (player.getInventory().contains(IC2Token)) {
                      player.getInventory().removeItem(new ItemStack(IC2Token));
                      player.updateInventory();
                  } else if(!player.getInventory().contains(IC2Token)) {
                      player.sendMessage("You don't have the amount of tokens you listed in the command");
                  } else {
                      player.sendMessage("Something went wrong in the command! Please try again.");
                  }
    Sorry about some of this, but I didn't post the entire code because I thought it would be unnecessary... Here is the entire Main class file: http://pastebin.com/t3aZEhJJ


    I'm am a bit new to Java, I've done it for a few years, but have never sat down and actually learned everything there is to know about Java
     
    Last edited: Mar 15, 2016
  2. Offline

    Zombie_Striker

    Because you did not post any checks for if the sender is a player (and if you have the check in your code, this line should be directly after that), I'm assuming you're blindly casting the sender to a player. Never blindly cast. You must check if the sender is a player before casting. Never just assume that just because you will never use the console that it is safe to just cast it to a player, because all this is doing is just setting you up so that you keep this bad habit with you if you intend of coding anything else.

    So you are getting an arg before you even know it existed? This is an ArrayOutOfBoundsExeption waiting to happen. Check to make sure there is at least one arg before getting the first arg. Never get the object before you know it exists.

    You don't even know if there is a second arg! You need to check this first before you blindly get that object.

    Use the material enum instead. You must already know the material you're looking for, why use a method that neither mojang or bukkit wants you to use that does not clearly show what the object is. Type IDs should be avoided.

    You are already getting the integer in the try/catch statement. Why are you getting it a second time? Again, you did not check if there even is a second arg.

    Stick to Java Naming Conventions: All values should be lower case except if they are static/enums.

    This will most likely not be the case. Unless you know that the inventory contains an itemstack that is exactly the same in every way, this will return false. This includes itemstack size. Unless you know that the amount given at args[1] is exactly the same as the amount for the itemstack, this will return false. You need to either loop through the whole inventory and use .isSimular() to test if the itemstacks are close (do not need to have the same amount nor some parts of the itemmeta)

    Because of the previous points, most likely you have not actually took any course/ official tutorial about Java. All of the previous points might have been prevented if you had learned this before working on this project. Please, select a tutorial form the following link, learn Java (which should only take less than a week since it seems you already know some things about Java), and come back to the project. I only tell you this because it will save you the trouble of continually looking through your code to find these simple mistakes, and will save me and the rest of the community from having to point them out.
    https://bukkit.org/threads/plugin-dev-sticky-learning-java-where-to-learn.395662/
     
  3. Offline

    Aphex124

    @Zombie_Striker

    Sorry about some of this, but I didn't post the entire code because I thought it would be unnecessary... Here is the entire Main class file: http://pastebin.com/t3aZEhJJ


    Also, I have to use the ID because I will be using this on a modded server with Cauldron... I just added gold as an example since its in Vanilla
     
    Last edited: Mar 15, 2016
  4. Offline

    Zombie_Striker

    @Aphex124
    Unless you want me to review your entire plugin, I do not need the whole class. If you want the solution to your current problem...
     
  5. Offline

    Aphex124

    Fixed, and I was just pointing out that I do know what the args are in the class... Thanks for the help :)
     
  6. @Aphex124 if someone has said something and you fix it, set the thread as solved
     
Thread Status:
Not open for further replies.

Share This Page