Using Packets

Discussion in 'Plugin Development' started by Kevinzuman22, Jan 12, 2016.

Thread Status:
Not open for further replies.
  1. I got a plugin that lets time display on signs, but I tried to also make it possible to display a player's time. I managed to get a function that gets a player's time correctly, which looks like this:

    Code:
    public Date getPlayerTime(Player player) {
       int hours = (int)((player.getPlayerTime() / 1000 + 6 ) % 24);
       int minutes = (int) (60 * (player.getPlayerTime() % 1000) / 1000);
      
       GregorianCalendar cal = new GregorianCalendar(4012, 6, 11, hours, minutes, 0); // An arbitrary date
      
       return cal.getTime();
    }
    That function will be used in this way:

    Code:
    @SuppressWarnings("deprecation")
    public void update() {
       if (!isValid()) return;
          
          
       Sign s = sign();
          
       s.setLine(0, "");
       if (!m_label.isEmpty()) {
            s.setLine(1, m_label);
       } else {
             s.setLine(1, "Time");
       }
       s.setLine(2, m_formatter.format(getPlayerTime(Bukkit.getPlayer("Greenadine"))));
       s.setLine(3, "");
          
       s.update();
    }
    I just put my username to test the function, and it displayed the time correctly. But I'd like to display every player's time on it, so each player will see their own personal time on it. I was told I need to use packets for that, but how would I do that? I have never heard of packets before, let alone ever used them before. Could anyone help me with this?

    Thanks in advance!
     
  2. Offline

    ski23

    There are many packet tutorials out there. I am also currently working on a packet api. You may want to check out ProtocolLib.
     
  3. Offline

    Zombie_Striker

  4. @Zombie_Striker

    There is just one problem: I cannot get a player in update(). How would I get that working?
     
  5. Offline

    Zombie_Striker

    @Kevinzuman22
    What do you mean "can't get a player"? You can loop through all the players online using Bukkit.getServer().getonLinePlayers. You can even get specific players by using getServer().getPlayer(name or uuid)
     
  6. @Zombie_Striker
    Ok. I made it loop through all online players like you said. But I am not really getting the sendSignChange method working. This is what I currently have:
    Code:
    for(Player player : Bukkit.getServer().getOnlinePlayers()) {
        String[] lines = {"", m_label, getPlayerTime(player), ""};
        Location sloc = s.getLocation();
        try {
            player.sendSignChange(sloc, lines);
        } catch(IllegalArgumentException e) {
            for(Player operator : Bukkit.getServer().getOnlinePlayers()) {
                if(operator.isOp()) {
                    operator.sendMessage(ClockSign.prefix + ChatColor.RED + "Could not display player time on clocks! See console log for information!");
                } else {
                    //Don't notify.
                }
            }
            m_plugin.log.warning("[ClockSign] Could not display player time on signs! Look below for errors!");
            e.printStackTrace();
        }
    }

    I am not sure I am using the method correctly. And if I am, I have a problem with the StringArray, because the getPlayerTime method returns a Date, not a String. Here it is:
    Code:
    public Date getPlayerTime (Player player) {
        int hours = (int)((player.getPlayerTime() / 1000 + 6 ) % 24);
        int minutes = (int) (60 * (player.getPlayerTime() % 1000) / 1000);
       
        GregorianCalendar cal = new GregorianCalendar(4012, 6, 11, hours, minutes, 0); // An arbitrary date
       
        return cal.getTime();
    }
    Please help me a bit more.

    Thanks in advance!
     
  7. Offline

    Zombie_Striker

    @Kevinzuman22
    You may want to use Date.toString();

    Also, are you sure the block at sloc is a sign?
     
Thread Status:
Not open for further replies.

Share This Page