Solved How can i get BukkitVersion like 1_9_R1?

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

  1. title
     
    #1 RicoBlaze8, May 21, 2016
    Last edited: May 22, 2016
    • Funny Funny x 4
    • Optimistic Optimistic x 2
  2. We're not going to spoon-feed you. Tell us what you've tried, and we can work from there.
     
  3. I edit the title, who can help me?

    I do this code:

    Code (Text):
    String version = Bukkit.getServer().getBukkitVersion();

    if(version == "1_9_R1"){

    }
    But doesn't work
     
  4. Actually this should return
    or something similar

    Maybe you can work with that :)
     
  5. So i must add R0.1-SNAPSHOT?
     
  6. Serializator

    Supporter

    If you mean the NMS version:
    Code (Text):
    Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3]
     
  7. and... this code is for? 1.9 or 1.8 or.. i don't know
     
  8. Code (Text):
        public String getVersion() {
            String version = new String(Bukkit.getVersion());
            int pos = version.indexOf("(MC: ");
            version = version.substring(pos + 5).replace(")", "");
            return version;
        }
    This would return something like "1.9" etc. unless you want the NMS one like 1_9_...
     
  9. Code (Text):
        public String getVersion() {
            String version = new String(Bukkit.getVersion());
            int pos = version.indexOf("(MC: ");
            version = version.substring(pos + 5).replace(")", "");
            return version;
        }

    if(getVersion() == "1.9.4"){

    }
    doesn't work
     
  10. We also don't know. Just print it in your console and you'll see how it looks.
     
  11. This is my 1.8 Tab Class:

    Code (Text):
    package it.ricoblaze8.utils.tab;

    import java.lang.reflect.Field;

    import org.bukkit.craftbukkit.v1_8_R3.entity.CraftPlayer;
    import org.bukkit.entity.Player;

    import net.minecraft.server.v1_8_R3.IChatBaseComponent;
    import net.minecraft.server.v1_8_R3.PacketPlayOutPlayerListHeaderFooter;
    import net.minecraft.server.v1_8_R3.PlayerConnection;
    import net.minecraft.server.v1_8_R3.IChatBaseComponent.ChatSerializer;

    public class TabList18 {
       

        public static void sendHeaderAndFooter(Player p, String Header, String Footer) {
            PlayerConnection connection = ((CraftPlayer) p).getHandle().playerConnection;
            IChatBaseComponent header = ChatSerializer.a("{'color':'" + "', 'text':'" + Header + "'}");
            IChatBaseComponent footer = ChatSerializer.a("{'color':'" + "', 'text':'" + Footer + "'}");
            PacketPlayOutPlayerListHeaderFooter packet = new PacketPlayOutPlayerListHeaderFooter();
            try {
                Field headerField = packet.getClass().getDeclaredField("a");
                headerField.setAccessible(true);
                headerField.set(packet, header);
                headerField.setAccessible(!headerField.isAccessible());

                Field footerField = packet.getClass().getDeclaredField("b");
                footerField.setAccessible(true);
                footerField.set(packet, footer);
                footerField.setAccessible(!footerField.isAccessible());
            } catch (Exception e) {
                e.printStackTrace();
            }
            connection.sendPacket(packet);
        }
    }
     
    This is my 1.9.4 Tab Class:

    Code (Text):
    package it.ricoblaze8.utils.tab;

    import java.lang.reflect.Field;

    import org.bukkit.craftbukkit.v1_9_R2.entity.CraftPlayer;
    import org.bukkit.entity.Player;

    import net.minecraft.server.v1_9_R2.IChatBaseComponent;
    import net.minecraft.server.v1_9_R2.IChatBaseComponent.ChatSerializer;
    import net.minecraft.server.v1_9_R2.PacketPlayOutPlayerListHeaderFooter;
    import net.minecraft.server.v1_9_R2.PlayerConnection;

    public class TabList194 {
       
        public static void sendHeaderAndFooter(Player p, String Header, String Footer) {
            PlayerConnection connection = ((CraftPlayer) p).getHandle().playerConnection;
            IChatBaseComponent header = ChatSerializer.a("{'color':'" + "', 'text':'" + Header + "'}");
            IChatBaseComponent footer = ChatSerializer.a("{'color':'" + "', 'text':'" + Footer + "'}");
            PacketPlayOutPlayerListHeaderFooter packet = new PacketPlayOutPlayerListHeaderFooter();
            try {
                Field headerField = packet.getClass().getDeclaredField("a");
                headerField.setAccessible(true);
                headerField.set(packet, header);
                headerField.setAccessible(!headerField.isAccessible());

                Field footerField = packet.getClass().getDeclaredField("b");
                footerField.setAccessible(true);
                footerField.set(packet, footer);
                footerField.setAccessible(!footerField.isAccessible());
            } catch (Exception e) {
                e.printStackTrace();
            }
            connection.sendPacket(packet);
        }
       
    }
     
    This is my PlayerJoinEvent for add Tab on Join:


    Code (Text):
    package it.ricoblaze8.events;

    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;

    import it.ricoblaze8.mtm.Main;
    import it.ricoblaze8.utils.Placeholders;
    import it.ricoblaze8.utils.tab.TabList18;
    import it.ricoblaze8.utils.tab.TabList192;
    import it.ricoblaze8.utils.tab.TabList194;
    import me.clip.placeholderapi.PlaceholderAPI;

    public class Tab implements Listener {
       
        static Main plugin = Main.getInstance();
       
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e){
            Player p = e.getPlayer();

            String header = Placeholders.message(p, ChatColor.translateAlternateColorCodes('&', plugin.tab.getString("Tab.TabHeader")));
           
            String footer = Placeholders.message(p, ChatColor.translateAlternateColorCodes('&', plugin.tab.getString("Tab.TabFooter")));
           
            String ver194 = "1.9.4.R0.1-SNAPSHOT";
            String ver192 = "1.9.2-R0.1-SNAPSHOT";
            String ver18 = "1.8.8-R0.1-SNAPSHOT";
           
            if(plugin.getVersion() == "1.9.4"){
                if(plugin.isPAPI){
                    if(plugin.tab.getBoolean("Tab.enable") == true){
                        String papi_header = PlaceholderAPI.setPlaceholders(p, header);
                        String papi_footer = PlaceholderAPI.setPlaceholders(p, footer);
                        TabList194.sendHeaderAndFooter(p, papi_header, papi_footer);      
                    } else {
                        TabList194.sendHeaderAndFooter(p, "", "");
                    }
                } else {
                    if(plugin.tab.getBoolean("Tab.enable") == true){
                        TabList194.sendHeaderAndFooter(p, header, footer);
                    } else {
                        TabList194.sendHeaderAndFooter(p, "", "");
                    }
                }
            }
           
            if(plugin.getVersion() == ver192){
                if(plugin.isPAPI){
                    if(plugin.tab.getBoolean("Tab.enable") == true){
                        String papi_header = PlaceholderAPI.setPlaceholders(p, header);
                        String papi_footer = PlaceholderAPI.setPlaceholders(p, footer);
                        TabList192.sendHeaderAndFooter(p, papi_header, papi_footer);      
                    } else {
                        TabList192.sendHeaderAndFooter(p, "", "");
                    }
                } else {
                    if(plugin.tab.getBoolean("Tab.enable") == true){
                        TabList192.sendHeaderAndFooter(p, header, footer);
                    } else {
                        TabList192.sendHeaderAndFooter(p, "", "");
                    }
                }
            }
            if(plugin.getVersion() == ver18){
                if(plugin.isPAPI){
                    if(plugin.tab.getBoolean("Tab.enable") == true){
                        String papi_header = PlaceholderAPI.setPlaceholders(p, header);
                        String papi_footer = PlaceholderAPI.setPlaceholders(p, footer);
                        TabList18.sendHeaderAndFooter(p, papi_header, papi_footer);      
                    } else {
                        TabList18.sendHeaderAndFooter(p, "", "");
                    }
                } else {
                    if(plugin.tab.getBoolean("Tab.enable") == true){
                        TabList18.sendHeaderAndFooter(p, header, footer);
                    } else {
                        TabList18.sendHeaderAndFooter(p, "", "");
                    }
                }
            }
        }
       
    }
    This is my console.error:


    Code (Text):
    [16:31:20 ERROR]: Could not pass event PlayerJoinEvent to MixStepManager v1.1-BETA
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at net.minecraft.server.v1_9_R2.PlayerList.onPlayerJoin(PlayerList.java:333) [spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at net.minecraft.server.v1_9_R2.PlayerList.a(PlayerList.java:159) [spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at net.minecraft.server.v1_9_R2.LoginListener.b(LoginListener.java:144) [spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at net.minecraft.server.v1_9_R2.LoginListener.c(LoginListener.java:54) [spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at net.minecraft.server.v1_9_R2.NetworkManager.a(NetworkManager.java:233) [spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at net.minecraft.server.v1_9_R2.ServerConnection.c(ServerConnection.java:140) [spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at net.minecraft.server.v1_9_R2.MinecraftServer.D(MinecraftServer.java:825) [spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at net.minecraft.server.v1_9_R2.DedicatedServer.D(DedicatedServer.java:399) [spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at net.minecraft.server.v1_9_R2.MinecraftServer.C(MinecraftServer.java:665) [spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at net.minecraft.server.v1_9_R2.MinecraftServer.run(MinecraftServer.java:564) [spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at java.lang.Thread.run(Thread.java:745) [?:1.7.0_79]
    Caused by: java.lang.NoClassDefFoundError: net/minecraft/server/v1_8_R3/Packet
        at it.ricoblaze8.events.Events.onPlayerJoin(Events.java:59) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_79]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[?:1.7.0_79]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.7.0_79]
        at java.lang.reflect.Method.invoke(Method.java:606) ~[?:1.7.0_79]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-4af49dc-c5e9a16]
        ... 14 more
    Caused by: java.lang.ClassNotFoundException: net.minecraft.server.v1_8_R3.Packet
        at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:91) ~[spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:86) ~[spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at java.lang.ClassLoader.loadClass(ClassLoader.java:425) ~[?:1.7.0_79]
        at java.lang.ClassLoader.loadClass(ClassLoader.java:358) ~[?:1.7.0_79]
        at it.ricoblaze8.events.Events.onPlayerJoin(Events.java:59) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_79]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[?:1.7.0_79]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.7.0_79]
        at java.lang.reflect.Method.invoke(Method.java:606) ~[?:1.7.0_79]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-4af49dc-c5e9a16]
        ... 14 more
     
     
  12. Here's an old snippet of mine:
    Code (Java):
    public Title createTitle(String title, String subtitle, int fadeIn, int stay, int fadeOut) {
        switch (Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3]) {
            case "v1_8_R1":
                return new Title_1_8_R1(title, subtitle, fadeIn, stay, fadeOut);
            case "v1_8_R2":
                return new Title_1_8_R2(title, subtitle, fadeIn, stay, fadeOut);
            case "v1_8_R3":
                return new Title_1_8_R3(title, subtitle, fadeIn, stay, fadeOut);
            default:
                return null;
        }
    }
     
  13. Work... but this is my console-error:

    Code (Text):
    [17:49:12 ERROR]: Could not pass event PlayerJoinEvent to MixStepManager v1.1-BETA
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at net.minecraft.server.v1_9_R2.PlayerList.onPlayerJoin(PlayerList.java:333) [spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at net.minecraft.server.v1_9_R2.PlayerList.a(PlayerList.java:159) [spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at net.minecraft.server.v1_9_R2.LoginListener.b(LoginListener.java:144) [spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at net.minecraft.server.v1_9_R2.LoginListener.c(LoginListener.java:54) [spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at net.minecraft.server.v1_9_R2.NetworkManager.a(NetworkManager.java:233) [spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at net.minecraft.server.v1_9_R2.ServerConnection.c(ServerConnection.java:140) [spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at net.minecraft.server.v1_9_R2.MinecraftServer.D(MinecraftServer.java:825) [spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at net.minecraft.server.v1_9_R2.DedicatedServer.D(DedicatedServer.java:399) [spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at net.minecraft.server.v1_9_R2.MinecraftServer.C(MinecraftServer.java:665) [spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at net.minecraft.server.v1_9_R2.MinecraftServer.run(MinecraftServer.java:564) [spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at java.lang.Thread.run(Thread.java:745) [?:1.7.0_79]
    Caused by: java.lang.NoClassDefFoundError: net/minecraft/server/v1_8_R3/Packet
        at it.ricoblaze8.events.Events.onPlayerJoin(Events.java:59) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_79]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[?:1.7.0_79]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.7.0_79]
        at java.lang.reflect.Method.invoke(Method.java:606) ~[?:1.7.0_79]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-4af49dc-c5e9a16]
        ... 14 more
    Caused by: java.lang.ClassNotFoundException: net.minecraft.server.v1_8_R3.Packet
        at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:91) ~[spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at org.bukkit.plugin.java.PluginClassLoader.findClass(PluginClassLoader.java:86) ~[spigot.jar:git-Spigot-4af49dc-c5e9a16]
        at java.lang.ClassLoader.loadClass(ClassLoader.java:425) ~[?:1.7.0_79]
        at java.lang.ClassLoader.loadClass(ClassLoader.java:358) ~[?:1.7.0_79]
        at it.ricoblaze8.events.Events.onPlayerJoin(Events.java:59) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_79]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[?:1.7.0_79]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.7.0_79]
        at java.lang.reflect.Method.invoke(Method.java:606) ~[?:1.7.0_79]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-4af49dc-c5e9a16]
        ... 14 more
     
  14. This is my current code:

    Code (Text):
    package it.ricoblaze8.events;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;

    import it.ricoblaze8.mtm.Main;
    import it.ricoblaze8.utils.Placeholders;
    import it.ricoblaze8.utils.tab.TabList18;
    import it.ricoblaze8.utils.tab.TabList192;
    import it.ricoblaze8.utils.tab.TabList194;
    import me.clip.placeholderapi.PlaceholderAPI;

    public class Tab implements Listener {
       
        static Main plugin = Main.getInstance();
       
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e){
            Player p = e.getPlayer();
           
            String header = Placeholders.message(p, ChatColor.translateAlternateColorCodes('&', plugin.tab.getString("Tab.TabHeader")));
           
            String footer = Placeholders.message(p, ChatColor.translateAlternateColorCodes('&', plugin.tab.getString("Tab.TabFooter")));
           
            switch (Bukkit.getServer().getClass().getName().split("\\.")[3]){
            case "v1_9_R2":
                if(plugin.isPAPI){
                    if(plugin.tab.getBoolean("Tab.enable") == true){
                        String papi_header = PlaceholderAPI.setPlaceholders(p, header);
                        String papi_footer = PlaceholderAPI.setPlaceholders(p, footer);
                        TabList194.sendHeaderAndFooter(p, papi_header, papi_footer);      
                    } else {
                        TabList194.sendHeaderAndFooter(p, "", "");
                    }
                } else if(!plugin.isPAPI){
                    if(plugin.tab.getBoolean("Tab.enable") == true){
                        TabList194.sendHeaderAndFooter(p, header, footer);
                    } else {
                        TabList194.sendHeaderAndFooter(p, "", "");
                    }
                }
                break;
            case "v1_9_R1":
                if(plugin.isPAPI){
                    if(plugin.tab.getBoolean("Tab.enable") == true){
                        String papi_header = PlaceholderAPI.setPlaceholders(p, header);
                        String papi_footer = PlaceholderAPI.setPlaceholders(p, footer);
                        TabList192.sendHeaderAndFooter(p, papi_header, papi_footer);      
                    } else {
                        TabList192.sendHeaderAndFooter(p, "", "");
                    }
                } else {
                    if(plugin.tab.getBoolean("Tab.enable") == true){
                        TabList192.sendHeaderAndFooter(p, header, footer);
                    } else {
                        TabList192.sendHeaderAndFooter(p, "", "");
                    }
                }
                break;
            case "v1_8_R3":
                if(plugin.isPAPI){
                    if(plugin.tab.getBoolean("Tab.enable") == true){
                        String papi_header = PlaceholderAPI.setPlaceholders(p, header);
                        String papi_footer = PlaceholderAPI.setPlaceholders(p, footer);
                        TabList18.sendHeaderAndFooter(p, papi_header, papi_footer);      
                    } else {
                        TabList18.sendHeaderAndFooter(p, "", "");
                    }
                } else {
                    if(plugin.tab.getBoolean("Tab.enable") == true){
                        TabList18.sendHeaderAndFooter(p, header, footer);
                    } else {
                        TabList18.sendHeaderAndFooter(p, "", "");
                    }
                }
                break;
            }
        }
       
    }
     
  15. Bump once every 24 hours please