Replace text in a sign

Discussion in 'Spigot Plugin Development' started by iAmGio, May 21, 2016.

  1. Hi!
    I'm working on a sign editor, but I can't replace text:

    Code (Text):
        @EventHandler
        public void placeSign(SignChangeEvent sign)
        {
            Player p = sign.getPlayer();
            Sign s = (Sign) sign.getBlock().getState();

            for(String l : s.getLines())
            {      
                if(p.hasPermission("coloredsigns.position"))
                {
                    l.replaceAll("<x>", (int)sign.getBlock().getLocation().getX()+"");
                    l.replaceAll("<y>", (int)sign.getBlock().getLocation().getY()+"");
                    l.replaceAll("<z>", (int)sign.getBlock().getLocation().getZ()+"");
                }
            }
        }

    Can you help me?
     
  2. You'll have to use Sign#setLine and once you're done doing them, do #update
     
    • Agree Agree x 2
  3. I want to replace text.
     
  4. Get the original text, change it then set it back to the sign.
     
    • Agree Agree x 1
  5. How?
     
  6. But that's not how Java works. You're replacing the text represented in the String Object, but you still have to connect it to the actual sign with the set method. Also, I think you should add s.update() at the end of the method sothat the changes get visible.
     
    • Agree Agree x 1
  7. Normally I don't write the code myself, but I couldn't think of a way to explain this clearly. Understand what it does before you use it.

    EDIT, didn't work. Now it does.

    Code (Text):


        @EventHandler
        public void placeSign(SignChangeEvent sign) {
            Player p = sign.getPlayer();
            Sign s = (Sign) sign.getBlock().getState();

            String[] lines = s.getLines();

            for (int i = 0; i < lines.length; i++) {
                if (p.hasPermission("coloredsigns.position")) {
                    String line = lines[i];
                    line = line.replaceAll("<x>", (int) sign.getBlock().getLocation().getX() + "");
                    line = line.replaceAll("<y>", (int) sign.getBlock().getLocation().getY() + "");
                    line = line.replaceAll("<z>", (int) sign.getBlock().getLocation().getZ() + "");
                    s.setLine(i, line);
                }
            }
            s.update();
        }
     
     
    • Useful Useful x 2
  8. set the message with . the sign message .replace("%something%", Replacement); then update.
     
  9. what a cute spoon isn't it ?
     
  10. Thanks :)
     
  11. Like I said, I couldn't think of a way to explain this clearly. Apparently you didn't either, because he most likely didn't understand anything of it after reading what you said, neither did I.
     
    • Like Like x 1
  12. you didn't because like what you've said you even don't understand what is the shit with the code you've been copied with.
     
    • Funny Funny x 2
  13. Maybe it's because English isn't my native language, but I've no idea what you're trying to say :confused:
     
    • Agree Agree x 2
    • Funny Funny x 1
  14. I have no idea either I think its been far with the problem itself to keep this nonsense discussion.
     
    • Like Like x 1
  15. In this example I think it's fine to "spoonfeed". I wouldn't neccessarily call it that in this instance.
     
    • Agree Agree x 2
  16. Yayy!!! a hero please stop that man to do this mistakes again mr . Rubix :D.
     
  17. That's why you could have said nothing as others already gave the solution.
     
  18. It doesn't work
     
  19. Try debugging where your code gets using a few debug statements. It was untested :p
     
    • Like Like x 1
  20. Now it works, thank you :)
     
    • Friendly Friendly x 1