Plugin only sending message when player is op, but requires no perms?

Discussion in 'Plugin Development' started by pingpong1999, Apr 30, 2016.

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

    pingpong1999

    Code:
    }if(args[1].equalsIgnoreCase("gui")){
                    if(player.getItemInHand().getType().equals(Material.DIAMOND_PICKAXE) || player.getItemInHand().getType().equals(Material.IRON_PICKAXE) || player.getItemInHand().getType().equals(Material.GOLD_PICKAXE) ||
                            player.getItemInHand().getType().equals(Material.STONE_PICKAXE) || player.getItemInHand().getType().equals(Material.WOOD_PICKAXE)){
                        Location playerloc = player.getLocation();
                        player.playSound(playerloc, Sound.ANVIL_USE, 10, 10);
                    Inventory inv = Bukkit.createInventory(null, 27, ChatColor.GRAY + "Enchant");
                    ItemStack CurrentPick = new ItemStack (player.getItemInHand().getType());
                    ItemMeta CurrentPickMeta = CurrentPick.getItemMeta();
                 
                    ItemStack CurrentTokens = new ItemStack (Material.BOOK_AND_QUILL);
                    ItemMeta CurrentTokensMeta = CurrentTokens.getItemMeta();
                 
                    ItemStack Upgrade1 = new ItemStack (Material.BOOK);
                    ItemMeta Upgrade1Meta = Upgrade1.getItemMeta();
                 
                    ItemStack Upgrade2 = new ItemStack (Material.BOOK);
                    ItemMeta Upgrade2Meta = Upgrade2.getItemMeta();
                 
                    ItemStack Upgrade3 = new ItemStack (Material.BOOK);
                    ItemMeta Upgrade3Meta = Upgrade3.getItemMeta();
                 
                    ItemStack Upgrade4 = new ItemStack (Material.BOOK);
                    ItemMeta Upgrade4Meta = Upgrade4.getItemMeta();
                 
                 
                    ItemStack EmptyUpgrade = new ItemStack (Material.BOOK);
                    ItemMeta EmptyUpgradeMeta = Upgrade2.getItemMeta();
                 
                    ItemStack Rename = new ItemStack (Material.NAME_TAG);
                    ItemMeta RenameMeta = Rename.getItemMeta();
                 
                 
                    ItemStack item = player.getItemInHand();
                    ItemMeta itemMeta = item.getItemMeta();
                    itemMeta.setDisplayName(itemMeta.getDisplayName());
                 
                    List<String> lore = new ArrayList<String>();
                    List<String> loreCurrent = new ArrayList<String>();
                    if(itemMeta.hasLore()){
                        List<String> oldLore = itemMeta.getLore();
                        lore.addAll(oldLore);
    
                }
    
                    CurrentPick.setItemMeta(itemMeta);
    
                    String pathc = "tokens." + player.getName().toLowerCase();
                    int totalbal = config.getInt("tokens." + player.getName().toLowerCase());
                    CurrentTokensMeta.setDisplayName(ChatColor.GRAY + "Tokens: " + totalbal );
                    CurrentTokens.setItemMeta(CurrentTokensMeta);
                 
                    List<String> Cost = new ArrayList<String>();
                    Upgrade1Meta.setDisplayName(ChatColor.AQUA + "" + ChatColor.BOLD + "Efficiency");
                    if(Upgrade1Meta.hasLore()){
                        List<String> oldLoreCost = Upgrade1Meta.getLore();
                        Cost.addAll(oldLoreCost);
                    }
                    Upgrade1.setItemMeta(Upgrade1Meta);
                 
                    List<String> Cost2 = new ArrayList<String>();
                    Upgrade2Meta.setDisplayName(ChatColor.AQUA + "" + ChatColor.BOLD + "Fortune");
                    if(Upgrade2Meta.hasLore()){
                        List<String> oldLoreCost2 = Upgrade2Meta.getLore();
                        Cost2.addAll(oldLoreCost2);
                    }
                    Upgrade2.setItemMeta(Upgrade2Meta);
                 
                    List<String> Cost3 = new ArrayList<String>();
                    Upgrade3Meta.setDisplayName(ChatColor.AQUA + "" + ChatColor.BOLD + "Speed");
                    if(Upgrade3Meta.hasLore()){
                        List<String> oldLoreCost3 = Upgrade3Meta.getLore();
                        Cost3.addAll(oldLoreCost3);
                    }
                    Upgrade3.setItemMeta(Upgrade3Meta);
                 
                    List<String> Cost4 = new ArrayList<String>();
                    Upgrade4Meta.setDisplayName(ChatColor.AQUA + "" + ChatColor.BOLD + "Drill");
                    if(Upgrade4Meta.hasLore()){
                        List<String> oldLoreCost4 = Upgrade4Meta.getLore();
                        Cost4.addAll(oldLoreCost4);
                    }
                    Upgrade4.setItemMeta(Upgrade4Meta);
                 
                    RenameMeta.setDisplayName(ChatColor.AQUA + "Rename");
                    Rename.setItemMeta(RenameMeta);
                 
                    EmptyUpgradeMeta.setDisplayName(ChatColor.RED + "Empty");
                    EmptyUpgrade.setItemMeta(EmptyUpgradeMeta);
                 
                    inv.setItem(0, Upgrade1);
                    inv.setItem(1, Upgrade2);
                    inv.setItem(2, Upgrade3);
                    inv.setItem(3, Upgrade4);
                    inv.setItem(4, EmptyUpgrade);
                    inv.setItem(5, EmptyUpgrade);
                    inv.setItem(6, EmptyUpgrade);
                    inv.setItem(7, EmptyUpgrade);
                    inv.setItem(8, EmptyUpgrade);
                 
                    inv.setItem(20, Rename);
                    inv.setItem(21, CurrentPick);
                    inv.setItem(23, CurrentTokens);
             
                    player.openInventory(inv);
                    }else{
                        player.sendMessage(ChatColor.RED + "You must be holding a pickaxe!");
                        Location nopick = player.getLocation();
                        player.playSound(nopick, Sound.VILLAGER_NO, 10, 10);
                    }
                    return true;
                }
    
    If the player is holding a pick. It will open the inventory (works with both op and not op)
    If the player isn't holding a pick. It will NOT play the sound (If they are not op)
    If the player isn't holding a pick. It WILL play the sound (if they are op)
    I do not know why, I don't have any permissions set in this section and went over the other lines of code to see if there was any errors with permissions, but no please help! thanks

    Feel free to correct me on any errors I did in my code to make it more efficient
    -EDIT on the upgrades, yes I know I am not setting lore then just adding it blindly but I will change it
     
    Last edited: Apr 30, 2016
  2. Offline

    mine-care

    Can you show us what part of the code you refer to? The way the code is now makes it really hard to read and furthermore understand.
    Two things make it 'unreadable':
    1. Naming conventions not being followed
    2. There is no proper indentation
    if you could fix those it would be great.
     
  3. If you want to do stuff if a player is/isn't OP, do the following:
    Code:
    //If they are OP
    if (player.isOp()) {
           //Do stuff
    }
    //If they aren't OP
    if (!player.isOp()) {
           //Do stuff
    }
    
    Have fun!
     
  4. Offline

    mine-care

    @Sulphate Problem is though that the op claims that certain code only aplies to OP's without this check, so even with "!player#isOp()" it will have the same behaviour.
     
  5. Offline

    pingpong1999

    Code:
    }else{
                      player.sendMessage(ChatColor.RED + "You must be holding a pickaxe!");
                      Location nopick = player.getLocation();
                        player.playSound(nopick, Sound.VILLAGER_NO, 10, 10);
                    }
                    return true;
    
    
    this is the code referring to, the if statement is at the very top seeing if player is holding a pick
    I will fix these mistakes I have made. thanks!
     
    Last edited: May 1, 2016
  6. Offline

    mine-care

    @pingpong1999
    aha right, So, is this piece of code ever reached? Have you debuged it?
     
  7. Offline

    pingpong1999

    If by reached you mean ever get used,Yes. I have not debugged it yet though. What are some methods of debugging?
     
  8. Offline

    mine-care

    Try debuging as well.
    Pring a message for instance in the console when it is reached just to see when it gets there.
     
  9. Offline

    pingpong1999

    oh, I thought you meant there was a special feature in my ide that i could use. I do this all the time :p, yea I will test it out see what the outcome is

    -EDIT does exactly the same thing.. does not return anything
     
    Last edited: May 2, 2016
  10. Offline

    mine-care

    @pingpong1999 Hmm wait, return? Do you expect it to return a value?
    I ment, put a print line in your else body to make sure the statement is reached.
    This way we will find out if it is the else statement never being reached or if it is reached and the sound doesnt play.
     
  11. Offline

    pingpong1999

    yea, I am not very good with terminology, but it did not print any message.
     
  12. Offline

    mine-care

    @pingpong1999 aha! That means that the issue is the if statement (or poor testing).
    Are you holding anything but a pickaxe when testing if the else statement is reached?
     
  13. Offline

    pingpong1999

    What do you mean 'anything but a pickaxe'? I have tested with a pick and then without a pick. the else statement should cover everything that is not a pick
     
  14. Offline

    Lordloss

    How does the full command look that you are using? You know that args[1] refers to the second argument?
     
  15. Offline

    pingpong1999

    I didn't add the rest of the command as it is irrelevant to the problem. And can you please use proper grammar and wording, I find it very hard to understand what you are asking
     
  16. Offline

    Lordloss

    @pingpong1999 Yes, of course it is important for your problem. What didnt you understand in the sentence "you know that args[1] refers to the second argument?"...
     
  17. Offline

    Firestar311

    There is nothing wrong with his grammar. And can you please use standard Java naming conventions. It is harder to follow the code when you don't use them
     
    Lordloss likes this.
  18. Offline

    pingpong1999

    In the question "How does the full command look that you are using?"
     
  19. Offline

    Lordloss

    Are you kidding me? How does the command look? Show the command please. Write the command that you are using. Or even better, write the command + the rest of your code.
     
  20. Offline

    pingpong1999

    @Lordloss
    Code:
     
    if(args[0].equalsIgnoreCase("Enchant") && sender instanceof Player){
                if(args[1].equalsIgnoreCase("gui")){
                    if(player.getItemInHand().getType().equals(Material.DIAMOND_PICKAXE) || player.getItemInHand().getType().equals(Material.IRON_PICKAXE) || player.getItemInHand().getType().equals(Material.GOLD_PICKAXE) || 
                            player.getItemInHand().getType().equals(Material.STONE_PICKAXE) || player.getItemInHand().getType().equals(Material.WOOD_PICKAXE)){
                        Location playerloc = player.getLocation();
                        player.playSound(playerloc, Sound.ANVIL_USE, 10, 10);
                    Inventory inv = Bukkit.createInventory(null, 27, ChatColor.GRAY + "Enchant");
                    ItemStack CurrentPick = new ItemStack (player.getItemInHand().getType());
                    ItemMeta CurrentPickMeta = CurrentPick.getItemMeta();
                   
                    ItemStack CurrentTokens = new ItemStack (Material.BOOK_AND_QUILL);
                    ItemMeta CurrentTokensMeta = CurrentTokens.getItemMeta();
                   
                    ItemStack Upgrade1 = new ItemStack (Material.BOOK);
                    ItemMeta Upgrade1Meta = Upgrade1.getItemMeta();
                   
                    ItemStack Upgrade2 = new ItemStack (Material.BOOK);
                    ItemMeta Upgrade2Meta = Upgrade2.getItemMeta();
                   
                    ItemStack Upgrade3 = new ItemStack (Material.BOOK);
                    ItemMeta Upgrade3Meta = Upgrade3.getItemMeta();
                   
                    ItemStack Upgrade4 = new ItemStack (Material.BOOK);
                    ItemMeta Upgrade4Meta = Upgrade4.getItemMeta();
                   
                   
                    ItemStack EmptyUpgrade = new ItemStack (Material.BOOK);
                    ItemMeta EmptyUpgradeMeta = Upgrade2.getItemMeta();
                   
                    ItemStack Rename = new ItemStack (Material.NAME_TAG);
                    ItemMeta RenameMeta = Rename.getItemMeta();
                   
                   
                    ItemStack item = player.getItemInHand();
                    ItemMeta itemMeta = item.getItemMeta();
                    itemMeta.setDisplayName(itemMeta.getDisplayName());
                   
                    List<String> lore = new ArrayList<String>();
                    List<String> loreCurrent = new ArrayList<String>();
                    if(itemMeta.hasLore()){
                        List<String> oldLore = itemMeta.getLore();
                        lore.addAll(oldLore);
    
                }
    
                    CurrentPick.setItemMeta(itemMeta);
    
                    String pathc = "tokens." + player.getName().toLowerCase();
                    int totalbal = config.getInt("tokens." + player.getName().toLowerCase());
                    CurrentTokensMeta.setDisplayName(ChatColor.GRAY + "Tokens: " + totalbal );
                    CurrentTokens.setItemMeta(CurrentTokensMeta);
                   
                    List<String> Cost = new ArrayList<String>();
                    Upgrade1Meta.setDisplayName(ChatColor.AQUA + "" + ChatColor.BOLD + "Efficiency");
                    Cost.add(ChatColor.GRAY + "Cost: 100 ETokens");
                    Upgrade1Meta.setLore(Cost);
                    Upgrade1.setItemMeta(Upgrade1Meta);
                   
                    List<String> Cost2 = new ArrayList<String>();
                    Upgrade2Meta.setDisplayName(ChatColor.AQUA + "" + ChatColor.BOLD + "Fortune");
                    Cost2.add(ChatColor.GRAY + "Cost: 100 ETokens");
                    Upgrade2Meta.setLore(Cost2);
                    Upgrade2.setItemMeta(Upgrade2Meta);
                   
                    List<String> Cost3 = new ArrayList<String>();
                    Upgrade3Meta.setDisplayName(ChatColor.AQUA + "" + ChatColor.BOLD + "Speed");
                    Cost3.add(ChatColor.GRAY + "Cost: 100 ETokens");
                    Upgrade3Meta.setLore(Cost3);
                    Upgrade3.setItemMeta(Upgrade3Meta);
                   
                    List<String> Cost4 = new ArrayList<String>();
                    Upgrade4Meta.setDisplayName(ChatColor.AQUA + "" + ChatColor.BOLD + "Drill");
                    Cost4.add(ChatColor.GRAY + "Cost: 1,000 ETokens");
                    Upgrade4Meta.setLore(Cost4);
                    Upgrade4.setItemMeta(Upgrade4Meta);
                   
                    RenameMeta.setDisplayName(ChatColor.AQUA + "Rename");
                    Rename.setItemMeta(RenameMeta);
                   
                    EmptyUpgradeMeta.setDisplayName(ChatColor.RED + "Empty");
                    EmptyUpgrade.setItemMeta(EmptyUpgradeMeta);
                   
                    inv.setItem(0, Upgrade1);
                    inv.setItem(1, Upgrade2);
                    inv.setItem(2, Upgrade3);
                    inv.setItem(3, Upgrade4);
                    inv.setItem(4, EmptyUpgrade);
                    inv.setItem(5, EmptyUpgrade);
                    inv.setItem(6, EmptyUpgrade);
                    inv.setItem(7, EmptyUpgrade);
                    inv.setItem(8, EmptyUpgrade);
                   
                    inv.setItem(20, Rename);
                    inv.setItem(21, CurrentPick);
                    inv.setItem(23, CurrentTokens);
               
                    player.openInventory(inv);
                    }else{
                        player.sendMessage(ChatColor.RED + "You must be holding a pickaxe!");
                        Location nopick = player.getLocation();
                        player.playSound(nopick, Sound.VILLAGER_NO, 10, 10);
                    }
                    return true;
                }
            }
    
    main command is /et
     
  21. Offline

    mcdorli

    Compare enums with ==.

    What does this do? Do you know, that not every item has a display name?
    Code:
    itemMeta.setDisplayName(itemMeta.getDisplayName());
    
    You violate the D.R.Y. (Don't repeat yourself) coding style, use loops.

    Why do you create so many items, if you don't use them?

    You can check if an item is a pickaxe, if it's material's name contains the _PICKAXE String.
     
Thread Status:
Not open for further replies.

Share This Page