Signs with all the lines.

Discussion in 'Plugin Development' started by kipodu, Jul 25, 2014.

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

    kipodu

    Hello,
    I've got this plugin where you can create new signs and set the sign to say something, I'm now trying to make use all the lines the sign has, and I'm stuck!

    Example: User types "/signs new <sign name>" to create a sign.
    User types "/signs set <sign name> message" to write on the sign.

    The problem is the the user can only write on the first line of the sign, and not on all the other lines!

    Code:java
    1. public class NamedSigns extends JavaPlugin {
    2. private static Logger log = Logger.getLogger("Minecraft");
    3. private static Map<String,Location> signs = new HashMap<String,Location>();
    4.  
    5. private void usage(Player me) {
    6. me.sendMessage("Usage: signs new name");
    7. me.sendMessage(" signs set name message");
    8. }
    9.  
    10. private boolean parseArgs(Player me, String [] args) {
    11. if (args.length < 2) {
    12. usage(me);
    13. return false;
    14. }
    15. if (args[0].equalsIgnoreCase("new")) {
    16. return makeNewSign(me, args);
    17. }
    18. if (args[0].equalsIgnoreCase("set")) {
    19. if (args.length < 3) {
    20. usage(me);
    21. return false;
    22. }
    23. return setSign(me, args);
    24. }
    25. return false;
    26. }
    27.  
    28. // signs new sign_name
    29. private boolean makeNewSign(Player me, String [] args) {
    30. Location loc = me.getLocation();
    31. loc.setX(loc.getX() + 1); // Not right on top of player
    32. Block block = loc.getWorld().getHighestBlockAt(loc);
    33. signs.put(args[1], block.getLocation());
    34. block.setType(Material.SIGN_POST);
    35. log.info("Made new sign named " + args[1]);
    36. return true;
    37. }
    38.  
    39. // signs set sign_name line1
    40. private boolean setSign(Player me, String [] args) {//(6)
    41. String name = args[1];
    42. String msg = args[2];
    43. if (!signs.containsKey(name)) {
    44. // No such named sign
    45. me.sendMessage("No sign named " + name);
    46. return false;
    47. }
    48. Location loc = signs.get(name);
    49. Sign sign = (Sign)loc.getWorld().getBlockAt(loc).getState();
    50. sign.setLine(0, msg);
    51. sign.update();
    52. log.info("Set sign named " + name + " to " + msg);
    53. return true;
    54. }
    55.  
    56. public boolean onCommand(CommandSender sender, Command command,
    57. String commandLabel, String[] args) {
    58. if (commandLabel.equalsIgnoreCase("signs")) {
    59. if (sender instanceof Player) {
    60. Player me = (Player)sender;
    61. return parseArgs(me, args);
    62. }
    63. }
    64. return false;
    65. }
    66. }
    67.  


    I'm new to HashMaps and stuff, and I would really like you help.

    Thanks!
     
  2. kipodu
    Line 50 says
    Code:
    sign.setLine(0, msg);
    That's why it's always on line 1. (Because line 0 is line 1, lines 1 is line 2 etc.)
     
  3. Offline

    kipodu

    DJSkepter
    Thanks for the reply, but I think I got misunderstood,
    I want this to happen: If the user types in a message that's longer than what 1 line of a sign can display, the rest of his message will go to the other lines on the sign.
     
  4. Offline

    kipodu

    I would really appreciate if some would try and help,
    I'm still stuck with this problem.
     
  5. Offline

    krazytraynz

    If the string is longer than one line on a sign (15 characters), just split it into a substring and put that substring on the next line.
     
  6. Offline

    desht

  7. Offline

    kipodu

    desht
    Hm, I don't really know what to do with it though, but I will try to figure it out!
    Thanks!
     
  8. Offline

    desht

    wordWrap() gives you a String array, which could contain any number of lines. You want to copy at most four lines from that array into the sign. Code snippet (untested):
    PHP:
    // 15 is the max number of characters that will fit on one sign line
    String[] lines ChatPaginator.wordWrap("This is a very long sentence which won't fit on one line of a sign"15);
    for (
    int i 0&& lines.lengthi++) {
      
    sign.setLine(ilines[i]);
    }
    Of course this will all get a lot harder in 1.8, where signs can store more than 15 characters per line (the length will be variable, depending on character glyph width), but the above should work fine for 1.7.
     
Thread Status:
Not open for further replies.

Share This Page