I just thought I could give out some utility classes i use in my plugins. Setting Tablist header and footer: Spoiler 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(); try { Field headerField = packet.getClass().getDeclaredField("a"); headerField.setAccessible(true); headerField.set(packet, top); headerField.setAccessible(!headerField.isAccessible()); Field footerField = packet.getClass().getDeclaredField("b"); footerField.setAccessible(true); footerField.set(packet, bottom); footerField.setAccessible(!footerField.isAccessible()); } catch (Exception ev) { ev.printStackTrace(); } connection.sendPacket(packet); } Text Scoreboard (a Scoreboard with title & text). Maximum String length is 48 Spoiler 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"); obj.setDisplaySlot(DisplaySlot.SIDEBAR); obj.setDisplayName(title); //Add lines int value = text.length; for(String x : text){ String pre = ""; for(char c : (value+"").toCharArray()){ pre+="§"+c; } pre+="§f"; x=pre+x; if(x.length()<=16){ obj.getScore(x).setScore(value); }else{ Team team = s.registerNewTeam("line"+value); String prefix = x.substring(0, 16); String name = x.substring(16, x.length()); String suffix = ""; if(name.length()>16){ name = name.substring(0, 16); suffix = x.substring(32, x.length()); if(suffix.length()>16)suffix = suffix.substring(0, 16); } team.setPrefix(prefix); team.setSuffix(suffix); OfflinePlayer op = Bukkit.getOfflinePlayer(name); team.addPlayer(op); obj.getScore(op).setScore(value); } value--; } //Set the Scoreboard for the Player p.setScoreboard(s); } Displaying a big title and subtitle on the players screen: Spoiler 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): Spoiler 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){ ((CraftPlayer)p1).getHandle().playerConnection.sendPacket(p); } Call sendText when you dont have your String in json format, else use sendRaw. Have fun! ~Friwi
If you were to add some more explanation to these code snippets, I feel like they might be useful for the wiki. Check here for examples: http://www.spigotmc.org/wiki/create-a-simple-command/ http://www.spigotmc.org/wiki/hook-into-vault/ http://www.spigotmc.org/wiki/interactive-books/ (this one is an especially great example) http://www.spigotmc.org/wiki/nms-on-different-versions-without-reflection/
Wait 10 minutes. Im just doing a huge vector tutorial Got it: http://www.spigotmc.org/threads/tutorial-vector-for-beginners.68194/
I think I'll add onto this thread. I created this class to easily create SQL tables. Spoiler: Code PHP: /** * 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) { try{ connection.getConnection().prepareStatement(sql).executeUpdate(); Bukkit.getServer().getConsoleSender("Connection is working!") }catch (SQLException e) { e.printStackTrace(); } } } /** * * @param connection Takes SQL connection parameter. * @param sql SQL syntax. * **/ public void createTable(SQL connection, String sql) { try { connection.getConnection().prepareStatement(sql).executeUpdate(); Bukkit.getServer().getConsoleSender("Connection is working!") }catch (SQLException e) { e.printStackTrace(); } } }
Hmm, well ok. But i never use mysql. The best Database for mc networks is still mongodb. my next tutorial might be about this.
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
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.
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
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
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]);
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.