[Code] Cool code snippets of graphical stuff (1.7/1.8)

Discussion in 'Spigot Plugin Development' started by Friwi, May 26, 2015.

  1. I just thought I could give out some utility classes i use in my plugins.

    Setting Tablist header and footer:

    Code (Text):
    public static void setForPlayer(Player p, String header, String footer){
            CraftPlayer craftplayer = (CraftPlayer)p;
            PlayerConnection connection = craftplayer.getHandle().playerConnection;
            IChatBaseComponent top = IChatBaseComponent.ChatSerializer.a("{\"text\": \"" + header + "\"}");
            IChatBaseComponent bottom = IChatBaseComponent.ChatSerializer.a("{\"text\": \"" + footer + "\"}");

            PacketPlayOutPlayerListHeaderFooter packet = new PacketPlayOutPlayerListHeaderFooter();
              Field headerField = packet.getClass().getDeclaredField("a");
              headerField.set(packet, top);

              Field footerField = packet.getClass().getDeclaredField("b");
              footerField.set(packet, bottom);
            } catch (Exception ev) {


    Text Scoreboard (a Scoreboard with title & text). Maximum String length is 48

    Code (Text):
    public static void createScoreboard(Player p, String title, String[] text){
            //Create new Scoreboard
            ScoreboardManager mgr = Bukkit.getScoreboardManager();
            Scoreboard s = mgr.getNewScoreboard();
            //Create objective

            Objective obj = s.registerNewObjective("sb", "dummy");
            //Add lines
            int value = text.length;
            for(String x : text){
                String pre = "";
                for(char c : (value+"").toCharArray()){
                    Team team = s.registerNewTeam("line"+value);
                    String prefix = x.substring(0, 16);
                    String name = x.substring(16, x.length());
                    String suffix = "";
                        name = name.substring(0, 16);
                        suffix = x.substring(32, x.length());
                        if(suffix.length()>16)suffix = suffix.substring(0, 16);
                    OfflinePlayer op = Bukkit.getOfflinePlayer(name);
            //Set the Scoreboard for the Player

    Displaying a big title and subtitle on the players screen:

    Code (Text):
    public static void sendTitleToPlayer(Player player, Integer fadeIn, Integer stay, Integer fadeOut, String title, String subtitle) {
            PlayerConnection con = ((CraftPlayer) player).getHandle().playerConnection;
            con.sendPacket(new PacketPlayOutTitle(PacketPlayOutTitle.EnumTitleAction.TIMES, null, fadeIn, stay, fadeOut));

            if (subtitle != null) {
                con.sendPacket(new PacketPlayOutTitle(PacketPlayOutTitle.EnumTitleAction.SUBTITLE, IChatBaseComponent.ChatSerializer.a("{\"text\": \"" + subtitle + "\"}")));

            if (title != null) {
                con.sendPacket(new PacketPlayOutTitle(PacketPlayOutTitle.EnumTitleAction.TITLE, IChatBaseComponent.ChatSerializer.a("{\"text\": \"" + title + "\"}")));

    Display String above the players hotbar (above the item messages):

    Code (Text):
    public static void sendText(String text, Player[] player){
            String json = "{text:\""+text+"\"}";
            sendRaw(json, player);
        public static void sendRaw(String json, Player[] player){
            PacketPlayOutChat chat = new PacketPlayOutChat(new ChatComponentText(json), (byte)2);
            for(Player p : player)sendPacket(chat, p);
        private static void sendPacket(Packet p, Player p1){
    Call sendText when you dont have your String in json format, else use sendRaw.

    Have fun!

    • Useful Useful x 9
    • Like Like x 2
    • Informative Informative x 2
  2. jflory7

    jflory7 Retired Moderator
    Retired Benefactor

  3. I think I'll add onto this thread.

    I created this class to easily create SQL tables.

    * Created by ES359 on 5/23/15.
    * You can use this for whatever you want. Just put who created it inside the comment block.

    public class CreateSQLTables{

         * @param connection Takes a SQL connection parameter.
         * @param instance Checks to see if the table should be created via a config.
         * @param sql Parameters you wish to define for table.

        public void createTable(SQL connection, boolean instance, String sql)
           if(instance) {
                   Bukkit.getServer().getConsoleSender("Connection is working!")
               }catch (SQLException e) {
        * @param connection Takes SQL connection parameter.
        * @param sql SQL syntax.

       public void createTable(SQL connection, String sql)
               Bukkit.getServer().getConsoleSender("Connection is working!")
           }catch (SQLException e)
  4. Hmm, well ok. But i never use mysql. The best Database for mc networks is still mongodb. my next tutorial might be about this.
  5. Where is the last utility for?
  6. Where is the last utility for? The other things I have already, but if the last one is what I think it is. It might be helpful ;)
  7. For this: (Red marked down there)

  8. @Friwi Nice! Exactly what I needed. :)
  9. That's a matter of opinion. MySQL has a lot more options then mongo.
  10. Please. Each database has it's uses.
    • Agree Agree x 3
  11. Yeah sry. Should have written this as an oppinion. You are right. I prefer using MongoDB as its simple to use, not like mysql where you have to care about syntax.
    • Agree Agree x 1
  12. Very nice. Finally a person who is giving good examples of using these new features. Most of the other examples were out dated and sucked xD
  13. Thanks so helpful! But I have one problem that is probably obvious. How would I use sendText with Player[]. I am trying to use it in my join event and I can't use e.getPlayer() because that is Type Player, instead of Player[]. What is the difference, and how could I implement it into my JoinEvent? Thanks :)
  14. Hmm. I think this will work, wrote it from my mobile so could be wrong.

    Code (Text):
    Player[] arrayp = e.getPlayer().toArray(new Player[e.getPlayer]);
  15. I get the error:
    The method toArray(Player[]) is undefined for type Player. :/
  16. You can't turn a player into a player array...
  17. There must be a way to get the player array in an event, because in the picture at #9, that's exactly what's happening.
    #19 The_KevinAwesome, Jun 14, 2015
    Last edited: Jun 14, 2015