Blocked Items?

Discussion in 'Plugin Development' started by zakarls, Apr 19, 2014.

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

    zakarls

    I was making a plugin where /more would give you a full stack of the item unless in the configuration file it blocked that item from being duplicated. At the moment the plugin only works if the player is op or has the permission more.more because it bypasses the main code. I'm not sure why this happens. Any help would be appreciated.

    Code:
    Code:java
    1. @SuppressWarnings("deprecation")
    2. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    3. Player player = (Player) sender;
    4.  
    5. if(commandLabel.equalsIgnoreCase("more")){
    6. ItemStack stack = player.getInventory().getItemInHand();
    7. if(!player.hasPermission("more.more")){
    8. int item = player.getItemInHand().getTypeId();
    9. String hand = "" + item;
    10.  
    11. List<String> blocked = plugin.getConfig().getStringList("BlockedItems");
    12. for (String block : blocked) {
    13. if(!(block == hand)){
    14. stack.setAmount(64);
    15. }else{
    16. player.sendMessage(ChatColor.DARK_RED + "Error: " + ChatColor.RED + "You May Not Duplicate That Item!");
    17. }
    18. }
    19. }stack.setAmount(64);
    20. }
    21. return false;}


    Also Here Is My Config
    Code:
    BlockedItems:
    - 0001
    - 0002
    - 0003
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  2. Offline

    agent6262

    Found your problem. you cant compare the contents of a string using == you have to use .equals()

    EDIT: My bad the above line might help.

    Should the player need the permission when he/she types the command?
     
  3. Offline

    zakarls

    agent6262 I think I fixed it right but it still doesnt work. New Code:

    Code:java
    1. @SuppressWarnings("deprecation")
    2. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    3. Player player = (Player) sender;
    4.  
    5. if(commandLabel.equalsIgnoreCase("more") && (player.getItemInHand()) !=null){
    6. ItemStack stack = player.getInventory().getItemInHand();
    7. if(!player.hasPermission("more.more")){
    8.  
    9. int item = player.getItemInHand().getTypeId();
    10. String hand = "" + item;
    11.  
    12.  
    13. List<String> blocked = plugin.getConfig().getStringList("BlockedItems");
    14. for (String block : blocked) {
    15. if(!block.equals(hand)){
    16. stack.setAmount(64);
    17. }else{
    18. player.sendMessage(ChatColor.DARK_RED + "Error: " + ChatColor.RED + "You May Not Duplicate That Item!");
    19. }
    20. }
    21. }stack.setAmount(64);
    22. }
    23. return false;}
     
  4. Offline

    agent6262

    Wouldn't you use
    Code:java
    1. cmd.getName().equalsIgnoreCase("more");

    instead of
    Code:java
    1. commandLabel.equalsIgnoreCase("more")
     
  5. Offline

    sipsi133

    agent6262 No. Its cmd.getName() not commanLabel

    zakarls What is not working now? Do you get any errors?
     
  6. Offline

    zakarls

    sipsi133 and agent6262
    The commandLabel works just fine. As I said in the first post the plugin works fine if im op because stated here,
    Code:java
    1. if(!player.hasPermission("more.more")

    it would bypass the main code. So I think that there is a problem in this section:
    Code:java
    1. int item = player.getItemInHand().getTypeId();
    2. String hand = "" + item;
    3.  
    4.  
    5. List<String> blocked = plugin.getConfig().getStringList("BlockedItems");
    6. for (String block : blocked) {
    7. if(!block.equals(hand)){
    8. stack.setAmount(64);
    9. }else{
    10. player.sendMessage(ChatColor.DARK_RED + "Error: " + ChatColor.RED + "You May Not Duplicate That Item!");
    11. }

    or something terribly off in the config.

    Sorry for shooting down your ideas, but I just dont think thats the issue. :)
     
  7. Offline

    agent6262

    zakarls So if the player has the permission "more.more" should he/she be able to use the command?

    sipsi133 That's what i said, he should be using cmd.getName();
     
  8. Offline

    zakarls

    agent6262 Yes. if the player has more.more they are able to use the command
     
  9. Offline

    Schaakmatth

    i think the problem is:
    Code:java
    1. String hand = "" + item;
     
  10. Offline

    agent6262

    Then wouldn't you do this
    Code:java
    1. @SuppressWarnings("deprecation")
    2. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    3. Player player = (Player) sender;
    4.  
    5. if(commandLabel.equalsIgnoreCase("more") && (player.getItemInHand()) !=null){
    6. ItemStack stack = player.getInventory().getItemInHand();
    7. if(player.hasPermission("more.more")){//Fix HERE
    8.  
    9. int item = player.getItemInHand().getTypeId();
    10. String hand = "" + item;
    11.  
    12.  
    13. List<String> blocked = plugin.getConfig().getStringList("BlockedItems");
    14. for (String block : blocked) {
    15. if(!block.equals(hand)){
    16. stack.setAmount(64);
    17. }else{
    18. player.sendMessage(ChatColor.DARK_RED + "Error: " + ChatColor.RED + "You May Not Duplicate That Item!");
    19. }
    20. }
    21. }
    22. stack.setAmount(64);
    23. }
    24. return false;
    25. }
     
  11. Offline

    zakarls

    agent6262
    Oh. I see where you are coming from. I said that wrong. Sorry. How it works is that anyone can use the plugin, but if you have the permission more.more you bypass the code and are able to duplicate the banned blocks. If you dont have that permission you can still use the plugin but if the block is banned in the config you cant duplicate it... I hope this clears things up. I now realize i was unclear when stating what this permission does
     
  12. Offline

    agent6262

    Oh ok, give me 3 hours and i should have a solution for you; have to do family stuff.

    Tell me how this goes
    Code:java
    1. @SuppressWarnings("deprecation")
    2. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    3. Player player = (Player) sender;
    4.  
    5. if(commandLabel.equalsIgnoreCase("more") && (player.getItemInHand()) !=null){
    6. ItemStack stack = player.getInventory().getItemInHand();
    7. if(player.hasPermission("more.more")){//Fix HERE
    8. stack.setAmount(64);
    9. }
    10. else{
    11. int item = player.getItemInHand().getTypeId();
    12. String hand = "" + item;
    13.  
    14.  
    15. List<String> blocked = plugin.getConfig().getStringList("BlockedItems");
    16. for (String block : blocked) {
    17. if(!block.equals(hand)){
    18. stack.setAmount(64);
    19. }else{
    20. player.sendMessage(ChatColor.DARK_RED + "Error: " + ChatColor.RED + "You May Not Duplicate That Item!");
    21. }
    22. }
    23. }
    24. return true;
    25. }
    26. return false;
    27. }


    P.S. the problem was that when you execute a command and it works you have to return true;

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
Thread Status:
Not open for further replies.

Share This Page