[SOLVED] How is it possible for "TORCH" != "TORCH"?

Discussion in 'Plugin Development' started by BeaverDono, Oct 29, 2011.

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

    BeaverDono

    So I was playing with ItemStacks today and ran across something.. weird. It blew my mind and I just can't understand why this doesn't work.

    When displaying something in your ItemStack, it shows this on the console..
    Code:
    ItemStack{MELON x 12}
    ItemStack{STONE_AXE x 1}
    ItemStack{TORCH x 8}
    ItemStack{COBBLESTONE x 9001} //You get the picture..
    
    So I converted the current ItemStack over to a string and used a substring to pull out certain words to compare it to a keyword string. Essentially, what I am doing here is getting the plugin to check against the keyword to see if the user has a certain active item being held at that moment.

    However..

    Code:
    public void onItemHeldChange(PlayerItemHeldEvent event) {
            try {
                Player player = event.getPlayer();
    
                //Tell us which item the player is currently holding
                ItemStack checkItem = player.getInventory().getItem(event.getNewSlot());
                //Converts the ItemStack into a String and cuts out 5 letter word from the string..
                String torchTest = checkItem.toString().substring(10, 15);
                //Master Keyword to check against torchTest
                String keyword = "TORCH";
    
                if (torchTest == keyword) {
    
                    System.out.println(checkItem + " is a torch! :3");
                    System.out.println(torchTest + ".");
                    return;
                } else {
                    System.out.println(checkItem + " is not a torch! :(");
                    System.out.println(torchTest + ".");
                    return;
                }
            } catch (Exception e) {
                System.out.println("onItemHeldChange error!");
                return;
            }
        }
    
    While torchTest shows in the console that the string is "MELON", "STONE", "COBBL", and "TORCH"... the program is saying that none of these strings match the keyword.

    Can anyone make sense of what I am doing wrong? Or did I just divided by 0?
     
  2. Your error is in the if-statement.
    I'd recommend that you read this and then try to fix the error.
     
    BeaverDono likes this.
  3. Offline

    BeaverDono

    .... thanks Pandemoneus! :D
    I seriously need to stop programming dead tired throughout the night.. or get really caffeinated. XD
     
  4. Offline

    ArcheCane

    I know right...
     
    BeaverDono likes this.
Thread Status:
Not open for further replies.

Share This Page