sign#setLine() not working - updating makes the sign blank!

Discussion in 'Plugin Development' started by Shaner_X, Nov 30, 2016.

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

    Shaner_X

    So, I have this piece of code:
    Code:java
    1. @EventHandler
    2. public void onPlayerInteract(PlayerInteractEvent e) {
    3. // Hundreds of lines of code skipped
    4. Sign s = (Sign) e.getClickedBlock();
    5. s.setLine(0, "test");
    6. s.update();
    7. }
    If I update the sign, it becomes blank. If i don't, nothing happens. Does anyone know what I am doing wrong?
     
  2. Offline

    MrGeneralQ

    Any debug messages? console errors? anything?
     
  3. Offline

    Zombie_Striker

    @Shaner_X
    You can't cast the Block to a sign. You need to cast the blocks' State to a sign.
     
  4. Offline

    Shaner_X

    Nope...

    @Zombie_Striker I made a mistake. I actually have
    Code:java
    1. Sign s = (Sign) e.getClickedBlock().getState()
     
  5. Offline

    MrGeneralQ

    And still not working?
     
  6. @Shaner_X
    The piece of code you posted above (with the addition of "getState()" to the end) works just fine for me. There may be something else in your code causing this issue. Would you mind showing us the full class/eventhandler?
     
  7. Offline

    Shaner_X

    Nope
    That's the only part relevant to the sign, the rest involves chests etc. After that, there is nothing that could possibly override the text on the sign
     
  8. @Shaner_X
    Well, there's probably some mistake in there, caused by something like encapsulation or layout of if statements, that's mostly why I wanted to see it, since that exact code works fine when I'm testing.
     
  9. Offline

    Shaner_X

    It's not encapsulated in any if statements. When I update the sign, it turns blank hence it is technically working (although I don't know why it's turning blank as the text is supposed to change, not become blank. anyways, here you go:
    Code:java
    1.  
    2. @EventHandler
    3. public void onBlockInteract(PlayerInteractEvent e) {
    4. Player buyer = e.getPlayer();
    5. if ( e.getAction() == Action.RIGHT_CLICK_BLOCK) {
    6. Sign s;
    7. try {
    8. s = (Sign) e.getClickedBlock().getState();
    9. } catch (Exception ex) {
    10. return;
    11. }
    12. if (! "[Trade]".equalsIgnoreCase(ChatColor.stripColor(s.getLine(0)))) {
    13. return;
    14. }
    15. int x = e.getClickedBlock().getLocation().getBlockX();
    16. int y = e.getClickedBlock().getLocation().getBlockY();
    17. int z = e.getClickedBlock().getLocation().getBlockZ();
    18. String world = e.getClickedBlock().getLocation().getWorld().getName();
    19.  
    20. BlockState chestState = Bukkit.getServer().getWorld(world).getBlockAt(new Location(e.getClickedBlock().getWorld(), x, y - 1, z)).getState();
    21. Chest chest = (Chest) chestState;
    22. Inventory chestInventory = chest.getInventory();
    23.  
    24. Inventory playerInventory = buyer.getInventory();
    25. String line1 = s.getLine(1);
    26. String line2 = s.getLine(2);
    27. String[] info1 = line1.split(" ");
    28. String[] info2 = line2.split(" ");
    29. int amount1 = Integer.parseInt(info2[0]);
    30. int amount2 = Integer.parseInt(info2[0]);
    31. String item_name1 = info1[1].toUpperCase();
    32. String item_name2 = info2[1].toUpperCase();
    33. ItemStack item1 = new ItemStack(Enum.valueOf(Material.class, item_name1), amount1); // What the player gets
    34. ItemStack item2 = new ItemStack(Enum.valueOf(Material.class, item_name2), amount2); // What the player pays
    35.  
    36. if (!playerInventory.contains(Enum.valueOf(Material.class, item_name2))) {
    37. buyer.sendMessage(ChatColor.translateAlternateColorCodes('&', "&a[&eTradeShop&a] &cYou do not have &e"
    38. + amount2 + " " + item_name2.toLowerCase()));
    39. return;
    40. }
    41.  
    42. if (!chestInventory.contains(Enum.valueOf(Material.class, item_name1))) {
    43. buyer.sendMessage(ChatColor.translateAlternateColorCodes('&', "&a[&eTradeShop&a] &cThis shop does not have &e"
    44. + amount1 + " " + item_name1.toLowerCase()));
    45. s.setLine(0, ChatColor.RED + "[Trade]"); // TODO NOT WORKING!!!
    46. return;
    47. }
    48.  
    49. s.setLine(0, ChatColor.GREEN + "[Trade]");
    50. e.getClickedBlock().getState().update();
    51. buyer.sendMessage("test");
    52.  
    53. playerInventory.addItem(item1);
    54. playerInventory.removeItem(item2);
    55. chestInventory.addItem(item2);
    56. chestInventory.removeItem(item1);
    57.  
    58. buyer.sendMessage(ChatColor.translateAlternateColorCodes('&', "&a[&eTradeShop&a] &aYou have traded &e" + amount1 + " "
    59. + item_name1.toLowerCase() + " &a for &e" + amount2 + " " + item_name2.toLowerCase() + " &awith " + s.getLine(3)));
    60. return;
    61. }[syntax][/syntax]
     
  10. Offline

    xPlumpOrange

    I'm not sure if this would help but could you possibly make an item stack or something, I know you are trying set a block but maybe you can do something with item stacks.

    [Edit] Forget the idea , instead of the try stement do an if statement that checks if it's a sign then set the variable maybe that could fix it.
     
    Last edited: Dec 1, 2016
  11. Offline

    ipodtouch0218

    @Shaner_X
    I think I found the problem. You're updating your BlockState incorrectly.
    You are doing "e.getClickedBlock().getState().update();" insteaqd of "s.update()"
    (near the bottom of the listener)
     
  12. Offline

    Shaner_X

    I tried s.update() too .-.

    I guess I'll both update the jar on the test server and the libraries and see if it works.
     
  13. Offline

    MrGeneralQ

    Just a question, did you check if The event gets triggered? You may have no registered Your event handler in onEnable() ?
     
  14. Offline

    Shaner_X

    Everything apart from the sign works.
     
  15. Offline

    MrGeneralQ

    Did you use The correct imports?
     
  16. Offline

    Shaner_X

    Good question... May I ask you which is supposed to be the correct package for "Sign"?
     
  17. Offline

    MrGeneralQ

    I think you need The block import
     
  18. Offline

    Shaner_X

    This is my import:
    Code:Java
    1. import org.bukkit.block.Sign;
     
  19. Offline

    MrGeneralQ

    Code:Java
    1.  
    2. In that case try changing it to material
     
Thread Status:
Not open for further replies.

Share This Page