PlayerInteractEvent and Sign Help

Discussion in 'Plugin Development' started by bjsnow, Oct 17, 2012.

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

    bjsnow

    OK Ive tried this code and used the exact sign as I coded it for but when i hit it with 64 gold ingots in my hand nothing happens No errors in console....
    Code:java
    1.  
    2.  
    3. @EventHandler(priority=EventPriority.HIGH)
    4. public void onPlayerUse(PlayerInteractEvent event, Player p, Block b){
    5.  
    6. Sign s = (Sign) b.getState();
    7.  
    8. if(p.getItemInHand().getType() == Material.GOLD_INGOT && event.getClickedBlock().getType() == Material.SIGN_POST){
    9. s.update();
    10. if (s.getLine(1).matches("[Bjsnow]")) {
    11. if (s.getLine(2).matches("Iron_Sword")) {
    12. if (s.getLine(3).matches("10")) {
    13. if(p.getInventory().contains(new ItemStack(Material.GOLD_INGOT, 10))) {
    14. p.getInventory().remove(new ItemStack(Material.GOLD_INGOT, 10));
    15. p.getInventory().addItem(new ItemStack(Material.IRON_SWORD, 1));
    16. p.sendMessage("You Have Purchased an Iron_Sword");
    17.  
    18. } else {
    19. p.sendMessage("You need 10 gold ingots to buy this item!");
    20. }
    21. }
    22. }
    23.  
    24.  
    25. }
    26.  
    27.  
    28. }
    29.  
    30. }
     
  2. Offline

    !Phoenix!

    1. Please look at your indentation, the code is really not nice to read for us

    PHP:
    Sign s = (Signb.getState();
     
    if(
    p.getItemInHand().getType() == Material.GOLD_INGOT && event.getClickedBlock().getType() == Material.SIGN_POST){
    s.update();
    Ouch!
    1st you have to make sure it's a sign BEFORE you try to convert it into one (getState())
    2nd There is not only SIGN_POST, but also WALL_SIGN


    PHP:
    if (s.getLine(1).matches("[Bjsnow]")) {
    if (
    s.getLine(2).matches("Iron_Sword")) {
    if (
    s.getLine(3).matches("10")) {
    String.matches(String) is for regular expressions! You're looking for String.equals(Object).
     
  3. Offline

    desht

    A few comments:
    • This will only work for floor signs, not wall signs. May or may not be your intention, just noting it.
    • It will only work if you happen to have a stack of at least 10 gold ingots in your inventory. It won't work if you e.g. 2 stacks of 5 ingots. Also note the remove() call with take all stacks of 10 gold ingots, and no stacks of any other size. A general system to add/remove items is more complex - e.g. one possible apporach is in https://github.com/desht/dhutils/blob/master/src/main/java/me/desht/dhutils/Cost.java, see in particular the chargeItems() & grantItems() methods.
    • Signs are indexed from zero - again, it may be your intention to start with "[Bjsnow]" on the second line of the sign, just noting it.
    • Do the getState() call after you've verified the block is a sign. getState() is not the cheapest of operations (and the current approach will throw an exception if the block isn't a sign)
    • Also, the s.update() call at the top is not necessary.
    • You need a call to player.updateInventory() after removing/adding items or the changes won't show up until the player relogs.
    • Your indentation could use some cleanup :) (Although to be fair that could be XenForo screwing it up).
     
  4. Offline

    Jade Retired Staff

    Code:java
    1.  
    2. @EventHandler(priority=EventPriority.HIGH)
    3. public void onPlayerUse(PlayerInteractEvent event, Player p, Block b){
    4.  
    5. Sign s = (Sign) b.getState();
    6.  
    7. if(p.getItemInHand().getType() == Material.GOLD_INGOT && event.getClickedBlock().getType() == Material.SIGN_POST){
    8. s.update();
    9. if (s.getLine(1).matches("[Bjsnow]")) {
    10. if (s.getLine(2).matches("Iron_Sword")) {
    11. if (s.getLine(3).matches("10")) {
    12. if(p.getInventory().contains(new ItemStack(Material.GOLD_INGOT, 10))) {
    13. p.getInventory().remove(new ItemStack(Material.GOLD_INGOT, 10));
    14. p.getInventory().addItem(new ItemStack(Material.IRON_SWORD, 1));
    15. p.sendMessage("You Have Purchased an Iron_Sword");
    16.  
    17. } else {
    18. p.sendMessage("You need 10 gold ingots to buy this item!");
    19. }
    20. }
    21. }
    22.  
    23.  
    24. }
    25.  

    Better for your eyes, @!Phoenix! ?
     
  5. Offline

    !Phoenix!

    Wow, Darky1126, that was so very useless that I could cry... .......... (No help for bjsnow in there)
    /me feels offended

    My comment about the indention was very general and should be noticed for future posts. As you may have noticed I was still able to read the code and give hints on it ;)
     
    Darky1126 likes this.
Thread Status:
Not open for further replies.

Share This Page