Buy Sign

Discussion in 'Plugin Development' started by Blah1, Oct 27, 2013.

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

    Blah1

    I'm trying to make something similar to the Buy Sign that Essentials has.
    Couple things I need help with: I don't know how to check to see if an item ID is null
    I want to make it so that a player can put in the item name and it will work.
    Also the third and fourth lines HAVE to be integers.

    Here is what I have:
    Code:java
    1. @SuppressWarnings("deprecation")
    2. @EventHandler
    3. public void signChange(SignChangeEvent event){
    4. if (event.getLine(0).contains("-Buy-")){
    5. Material i = Material.getMaterial(Integer.parseInt(event.getLine(1)));
    6. if (event.getLine(1).equals(i)){
    7. if (i != null){
    8. int in = Integer.parseInt(event.getLine(2));
    9. if (event.getLine(2).equals(in)){
    10. int price = Integer.parseInt(event.getLine(3));
    11. if (event.getLine(3).equals(price)){
    12. event.setLine(0, ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("config.signs.buy.first-line-color") + "- Buy -"));
    13. event.setLine(1, ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("config.signs.buy.second-line-color") + i));
    14. event.setLine(2, ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("config.signs.buy.third-line-color") + in));
    15. event.setLine(3, ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("config.signs.buy.fourth-line-color") + price));
    16. }
    17. }
    18. }
    19. }
    20. }
    21. }
     
  2. Offline

    achap1989

    Blah1
    I do not know the answer to your question, as I am still learning. I simply have one quick question, why not use an "Inventory Store"? In my opinion, It is much more appealing to the eye.

    Let me give is a shot, try using something similar to
    Code:java
    1.  
    2. //place your import with anything else you have imported at the top of the file.
    3. import org.bukkit.Material;
    4.  
    5.  
    6. if((player.getInventory().contains(Material.COBBLESTONE, 10))){
    7. //Basically if the player has 10 COBBLESTONE, it will run the code here.
    8. }
    9.  

    Player will of course change to however you are calling the player. but this should work.
     
  3. Offline

    Blah1

    achap1989 That's not what I'm asking :p I just want to know how to check to see if the words on line two come out to be a material. And also if the third and fourth lines are integers.

    Perhaps the amazing and online drtshock could help? ;)

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  4. Offline

    tobiyas


    So just some random comments from me:
    * You should DEFINITLY catch those NumberFormatExceptions from Integer.parseInt().
    * You can not compare an Material to a String. That would simply not work. You would need something like that: if(i.name().equals(event.getLine(2))) But this also has problems with names that are longer than 16 chars. So also check that.
    * Check what sign.setLine() does when the text is longer than 16 chars. Earlier it threw an exception. I don't know how it is now.
     
  5. Offline

    Blah1

    tobiyas how do I check if the item ID is valid? Also, how do i check to see if the third and fourth line are numbers only?
     
  6. Offline

    tobiyas

    Give me a sec to check in Eclipse for synthax.

    Okay. I think I got it:

    Code:java
    1.  
    2. @SuppressWarnings("deprecation")
    3. @EventHandler
    4. public void signChange(SignChangeEvent event){
    5. if (event.getLine(0).contains("-Buy-")){
    6. //we have a buy sign.
    7. int materialId = -1;
    8. int amount = -1;
    9. int price = -1;
    10. Material material = null;
    11.  
    12. try{
    13. materialId = Integer.parseInt(event.getLine(1));
    14. material = Material.getMaterial(materialId);
    15.  
    16. amount = Integer.parseInt(event.getLine(2));
    17. price = Integer.parseInt(event.getLine(3));
    18. //No Numbers found in line
    19. return;
    20. }
    21.  
    22. if(material == null){
    23. //we got an invalid Material ID here... Do something
    24. return;
    25. }
    26.  
    27. event.setLine(0, ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("config.signs.buy.first-line-color") + "- Buy -"));
    28. event.setLine(1, ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("config.signs.buy.second-line-color") + materialId));
    29. event.setLine(2, ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("config.signs.buy.third-line-color") + amount));
    30. event.setLine(3, ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("config.signs.buy.fourth-line-color") + price));
    31. }
    32. }
    33.  



    You could also change the materialId to material.name(). To have the Name of the Material. But like I said above, many are longer than 16 chars.
     
  7. Offline

    drtshock

    Making buy and sell signs will be tricky without having item ids. Especially when materials and damage values or w/e they'll be called will definitely be more than 16 characters. An inventory shop would be a great idea. I know there are a few out there but none really do what I like so I was thinking of making my own ;o

    But yes, definitely try / catch the Integer#valueOf checks. If you still have issues after what tobiyas said I'll help you out ;3
     
  8. Offline

    achap1989

    Blah1 , I apologize, I must of misunderstood the question.
    drtshock , I am acctually attempting to make a customizable inventory shop via a handy config file. Problem is it will take a bit as I am still very new to this lol. Hell, I can not even change the tutorial Menu inventory, from wool, to diamond armor xD. (yet) But rest assured this will be made, lol.
     
  9. Offline

    drtshock

    Good luck with your project ;3
     
Thread Status:
Not open for further replies.

Share This Page