Tutorial Signs!

Discussion in 'Resources' started by HenkDeKipGaming, Jul 22, 2015.

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

    HenkDeKipGaming

    Hello everyone :)
    I saw a lot of people asking questions about Signs!
    So I made a little tutorial about some basics with signs.
    I hope this helps! :D
    These are the subjects I am going to explain:
    • Getting and setting lines.
    • Sign click.
    • Update signs
    • Minigame join signs.
    Getting and setting lines:
    So for the absolute basics, we are going to make a SignChangeEvent.
    This is for example when someone creates a sign.
    so:

    Code:
    @EventHandler
    public void onSignChange(SignChangeEvent e)
        {
         if (e.getLine(0).equalsIgnoreCase("hi")) { //If on the first line (line 0 is the first line) on the sign is written: hi
           e.setLine(0, "hello"); //It will change the first line to "hi"
           e.setLine(1, "how"); //the second line to "how"
           e.setLine(2, "are"); //the third line to "are"
           e.setLine(3, you"); //the fourth line to "you"
         }
        }
    
    So when a player makes a sign with on the first line written: "hi"
    the sign will look like this:
    hello
    how
    are
    you

    You can play a bit with it, maybe use colors and stuff.

    Sign click:
    So we just made that the sign changes.
    Now we want to check if someone clicks that sign!
    For that, we are going to use a PlayerInteractEvent.
    A PlayerInteractEvent is not only for sign clicks, but for now we are going to use it like that.
    So:

    Code:
    @EventHandler
    public void onPlayerInteract94(PlayerInteractEvent e) {
                     if (e.getAction() != Action.RIGHT_CLICK_BLOCK) return; //if the player right clicks a block
                       if ((e.getClickedBlock().getState() instanceof Sign)) { //if it's a sign
                         Sign s = (Sign)e.getClickedBlock().getState(); //defining the sign as the letter "s"
                         if (s.getLine(0).equalsIgnoreCase("hello")) { //if the first line is "hello" (ignores capital letters.)
                             e.getWhoClicked().sendMessage(ChatColor.GREEN + "hello!"); //e.getWhoClicked is the player that clicked the sign.
    //So, if someone clicks the sign with on the first line written: "hello" it'll send the player in green: "hello!"
                           }
                         }
                        }
    
    So there you have it!
    You can now change a sign, get who clicked the sign and send that player a message!

    Update signs:
    So now we are making a really simple, but really important thing for making a minigame sign.
    We are now going to update the sign!
    We are first going to use the code from the Sign click. And then we are going to make a really simple sign update.
    Code:
    @EventHandler
    public void onPlayerInteract94(PlayerInteractEvent e) {
                     if (e.getAction() != Action.RIGHT_CLICK_BLOCK) return;
                       if ((e.getClickedBlock().getState() instanceof Sign)) {
                         Sign s = (Sign)e.getClickedBlock().getState();
                         if (s.getLine(0).equalsIgnoreCase("hello")) {
                             e.getWhoClicked().sendMessage(ChatColor.GREEN + "The sign will now be changed!");
                             s.setLine(0, "hi"); //we defined "s" as the sign that get clicked, and then we set the first line to "hi"
                            s.update(); //this is the simple but important part, this will update the sign, without this, the sign won't change.
                           }
                         }
                        }
    
    So there you have it!
    We are now only 1 step before making a minigame sign!
    The last step:

    Minigame join signs:
    If you want to make Minigame join signs, you gotta use all the steps we just did.
    So first, we are going to change the sign if it's created:

    Code:
    @EventHandler
        public void onSignChange(SignChangeEvent e)
        {
         if (e.getLine(0).equalsIgnoreCase("Minigame")) { //if the first line is "minigame"
           e.setLine(0, "[Minigame]"); //it'll set the first line to "[Minigame]"
           e.setLine(1, "nice map"); //it'll set the second line to "nice map"
         }
        }
    
    Now we want to make a number of players in there.
    So we are going to define an Int (a number).
    So on the top of our class, we are going to put this:

    Code:
    int number = 0; //we made an int named "number" and we made it 0
    
    So now, we want to set the third line of the sign to that number. so:

    Code:
    int number = 0;
    @EventHandler
        public void onSignChange(SignChangeEvent e)
        {
         if (e.getLine(0).equalsIgnoreCase("Minigame")) {
           e.setLine(0, "[Minigame]");
           e.setLine(1, "nice map");
           e.setLine(2, number + "/8"); //sets the third line to number (now defined as 0) /8
         }
        }
    
    The sign will now look like this:
    [Minigame]
    nice map
    0/8
    Note: If you wish to make the line only show the number, you should do:
    Code:
           e.setLine(2, number + "");
    
    Because the line has to be a string.

    So now we got the sign. Now when someone clicks that sign, it should make it 1/8.

    Code:
    @EventHandler
        public void onPlayerInteract100(PlayerInteractEvent e) {
       if (e.getAction() != Action.RIGHT_CLICK_BLOCK) return;
           if ((e.getClickedBlock().getState() instanceof Sign)) {
    BlockState bs = e.getClickedBlock().getState(); //e.getClickedBlock is the block that got clicked.
             Sign s = (Sign) bs; //this time, we define the sign with a BlockState.
    if (s.getLine(1).equalsIgnoreCase("nice map")) { //if the second line is "nice map"
              if (number < 9) { //if the number is less then 9
               number = number + 1; //adds 1 to the number
               if (number == 8) { //if number is exactly 8             
                  number=0;  //it sets the players back to 0
                   s.setLine(2, number + "/8");  //it sets the sign to that number (now 0)        
                   s.update();  //it updates the sign
               } else {  
                 //if it's more then 8 it doesn't have to do anything, because it's impossible. (if it's 8 it goes back to 0)
               }
              }
    }
    }
    }
    
    So this is our full code:
    Code:
    int number = 0;
    @EventHandler
        public void onSignChange(SignChangeEvent e)
        {
         if (e.getLine(0).equalsIgnoreCase("Minigame")) {
           e.setLine(0, "[Minigame]");
           e.setLine(1, "nice map");
           e.setLine(2, number + "/8"); //sets the third line to number (now defined as 0) /8
         }
        }
    @EventHandler
        public void onPlayerInteract100(PlayerInteractEvent e) {
       if (e.getAction() != Action.RIGHT_CLICK_BLOCK) return;
           if ((e.getClickedBlock().getState() instanceof Sign)) {
    BlockState bs = e.getClickedBlock().getState(); //e.getClickedBlock is the block that got clicked.
             Sign s = (Sign) bs; //this time, we define the sign with a BlockState.
    if (s.getLine(1).equalsIgnoreCase("nice map")) { //if the second line is "nice map"
              if (number < 8) { //if the number is less then 8
               number = number + 1; //adds 1 to the number
               s.setLine(2, number + "/8");  //it sets the sign to that number (now 0)        
               s.update();  //it updates the sign
               if (number == 8) { //if number is exactly 8             
                  number=0;  //it sets the players back to 0
                   s.setLine(2, number + "/8");  //it sets the sign to that number (now 0)        
                   s.update();  //it updates the sign
               } else {  
                 //if it's more then 8 it doesn't have to do anything, because it's impossible. (if it's 8 it goes back to 0)
               }
              }
    }
    }
    }
    
    So now I am going to show you what happens, just by words (it's hard, but i'll give it a try).
    So a sign has been created with on line 0 written: minigame.
    The sign will change to:
    [Minigame]
    nice map
    0/8
    Now a player clicks it. The number is now less then 8, so it'll add 1 to number (it is now 1).
    A few other players clicked it, now the 8th player clickes the sign, number=8 now, it is less then 9, so it goes to where it equals 8. so it sets it back to 0 when the player clickes it. Then it updates the sign again.

    I hope this helpes some of you out!
    Those were some basics, so you can all start playing with making signs and minigames.
    Thanks for reading!
    if I made any mistakes, I would appreciate if someone tells me them, so I can make this tutorial better.

    The end!
    You can now make basic signs!
    Please leave a like and a nice comment if I helped you out!
     
    Last edited: Jul 22, 2015
  2. Offline

    mrCookieSlime

    Ouch
    You cannot check whether number equals 8 since you have checked whether it is smaller than 8 before.
    If it is smaller than 8 it will never equal 8 since that is taken care of.
     
    HenkDeKipGaming likes this.
  3. Offline

    HenkDeKipGaming

    Oh indeed, oops. I change that to 9 :)
     
  4. Offline

    mrCookieSlime

    @HenkDeKipGaming
    If it is anyway capped at 8
    You could basically just replace it with
    if (true)
    since it will always be lower than 9
    and we all know that if (true) can obviously be left out.
    So yeah and else if-statement would make more sense here...
     
  5. Offline

    CraterHater

    Nice tutorial I think lots of players will find this helpfull
     
  6. Offline

    HenkDeKipGaming

    thanks :D
     
  7. Offline

    Krumb069

  8. Offline

    HenkDeKipGaming

    Can you explain? :)
     
  9. Offline

    Krumb069

    @HenkDeKipGaming
    Like sign gui open sign to player on join and write Password: to sign
     
  10. Offline

    HenkDeKipGaming

    oh so it has to open the sign gui and the player has to write his password...
    i'll research that, wait a moment
     
  11. Offline

    Krumb069

    Yes ı already found one but it needs protocollib and doesnt work for 1.8
     
  12. Offline

    HenkDeKipGaming

    i think i found the same one.
    i actually have no idea how to do this xD
    it's interesting though.
    but why'd you want that? can't you use chat?
     
  13. Krumb069 and HenkDeKipGaming like this.
  14. Offline

    HenkDeKipGaming

    Krumb069 likes this.
  15. Offline

    Krumb069

  16. I think this requires ProtocolLib ... I already tried it with the packet but no args, but the SignChangeEvent doesn't get called then
     
    Krumb069 likes this.
  17. Offline

    _Error

    Pretty cool!
     
  18. Offline

    HenkDeKipGaming

    thanks! :D
     
  19. Offline

    Bladibudies

    Hey, so when player joins the game, it adds 1 to "0/8", but how can I make that when player dies or when player leaves the arena it removes 1? Players can leave the arena doing /leave, or dying...
     
  20. Have an arraylist or something to store the players who joined the game, on the PlayerDeathEvent or PlayerQuitEvent or when the players types /leave, set the sign line to yourArrayListOfPlayersWhoJoinedTheGame.size()+"/8"
     
  21. Offline

    Bladibudies

    Thank you are helping me, but I already fixed it. Now, I need, another thing... To join the arena, players have to click the sign, then they have to select a kit (its a kitpvp plugin) so, I dont want that when players click the sign, it adds 1 to 0/8, I want that when players select a kit, it adds 1 to 0/8...

    So, I created a HashMap for a list, when players select a kit, it adds the player to the hashmap, and adds 1 to the "0/8", when players die, or use the commands for leave, it checks if player is in the hashmap and then removes 1 of the "0/8", but I have one problem: When players click the sign, it uploads the sign, but I need that when players select the kit, uploads the sign.

    Sorry for bad english.

    Thanks.
     
    Last edited: Oct 31, 2015
Thread Status:
Not open for further replies.

Share This Page