My plugin causes crash

Discussion in 'Plugin Development' started by Speaw, Jan 29, 2016.

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

    Speaw

    Hey guys Im working on name tag plugin but causes crash after 2-5 relogin.

    Here my codes;
    Code:
    package so.team.sr.utils;
    
    import java.lang.reflect.Field;
    import java.util.ArrayList;
    import java.util.Collection;
    import java.util.HashMap;
    
    import net.minecraft.server.v1_7_R4.PacketPlayOutScoreboardTeam;
    
    import org.bukkit.Bukkit;
    import org.bukkit.craftbukkit.v1_7_R4.entity.CraftPlayer;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerQuitEvent;
    
    import so.team.sr.group.Group;
    import so.team.sr.group.GroupManager;
    
    public class NameTagUtil implements Listener {
      
        public static ArrayList<String> scoreboards = new ArrayList<String>();
        public static HashMap<String, String> prefix = new HashMap<String, String>();
        public static HashMap<String, String> suffix = new HashMap<String, String>();
      
        private static PacketPlayOutScoreboardTeam preparePacket(String playerName, PACKET PACKET){
            String Prefix = "";
            String Suffix = "";
          
            if(prefix.containsKey(playerName)) {
                Prefix = prefix.get(playerName);
            } else {
                Group pGroup = GroupManager.getPlayerGroup(Bukkit.getPlayerExact(playerName));
                Prefix = pGroup.getTagPrefix();
            }      
            if (suffix.containsKey(playerName)) Suffix = suffix.get(playerName);
          
            if(playerName == null || Prefix == null || Suffix == null || PACKET == null) return null;
            return createPacket(playerName, Prefix, Suffix, PACKET);
        } 
      
        @EventHandler (priority = EventPriority.HIGHEST)
        public void onJoin(PlayerJoinEvent e){
            createFor(e.getPlayer());
        }
      
        @EventHandler (priority = EventPriority.LOWEST)
        public void onQuit(PlayerQuitEvent event){
            destroyFor(event.getPlayer());
        }
      
        public static void destroyAll(){
            for (String pName : scoreboards)
                for (String sbpName : scoreboards)
                    sendPacket(pName, preparePacket(sbpName, PACKET.DESTROY));
            scoreboards.clear();
        }
      
        public static void destroyFor(Player player){
            if(player == null || !player.isOnline()) return;
            String playerName = player.getName();
            if (scoreboards.contains(playerName)){
                scoreboards.remove(playerName);
                PacketPlayOutScoreboardTeam packet = preparePacket(playerName, PACKET.DESTROY);
                if(packet == null) return;
                for (String sbpName : scoreboards)
                    sendPacket(sbpName, packet);
            }
          
            if (prefix.containsKey(playerName)) prefix.remove(playerName);
            if (suffix.containsKey(playerName)) suffix.remove(playerName);
        }
      
        public static void createFor(Player player){
            String playerName = player.getName();
            Group pGroup = GroupManager.getPlayerGroup(player);
          
            for (String sbpName : scoreboards){
                if (!scoreboards.isEmpty())sendPacket(sbpName, preparePacket(playerName, PACKET.CREATE));
                if (!scoreboards.isEmpty())sendPacket(playerName, preparePacket(sbpName, PACKET.CREATE));
            }
            sendPacket(playerName, preparePacket(playerName, PACKET.CREATE));
          
            prefix.put(playerName, pGroup.getTagPrefix());
            suffix.put(playerName, "");
            scoreboards.add(playerName);
        }
      
      
      
        public static void updateScoreboard(String playerName, String Prefix, String Suffix){
            if(playerName == null || Prefix == null || Suffix == null) return;
            prefix.put(playerName, Prefix);
            suffix.put(playerName, Suffix);
            PacketPlayOutScoreboardTeam packet = createPacket(playerName, Prefix, Suffix, PACKET.UPDATE);
            if(packet == null) return;
            if (scoreboards.contains(playerName))
                for (String sbpName : scoreboards)
                    sendPacket(sbpName, packet);
        }
          
    
        private static PacketPlayOutScoreboardTeam packet;  
        private static PacketPlayOutScoreboardTeam createPacket(String playerName, String prefix, String suffix, PACKET PACKET){
            if(playerName == null || prefix == null || suffix == null || PACKET == null) return null;
            packet = new PacketPlayOutScoreboardTeam();
            if(packet == null) return null;
            setField("a", playerName); setField("b", playerName); setField("c", prefix.replace("&", "§")); setField("d", suffix.replace("&", "§")); setField("g", 1);
            switch(PACKET){
            case CREATE: addPlayer(Bukkit.getPlayerExact(playerName)); setField("f", 0); break;
            case DESTROY: setField("f", 1); break;
            case UPDATE: setField("f", 2); break; }
            if(packet == null) return null;
            return packet;
        } 
      
        private static void setField(String field, Object value) {
            if(field == null || field.isEmpty() || value == null) return;
            try {
                Field f = packet.getClass().getDeclaredField(field);
                if(f == null) return;
                f.setAccessible(true);
                f.set(packet, value);
            } catch (Exception ex) {ex.printStackTrace();}
          
        }
        private static void addPlayer(Player pl){
            try {add(pl);}catch(Exception ex){ex.printStackTrace();}
        }
      
        @SuppressWarnings("unchecked")
        private static void add(Player pl) throws NoSuchFieldException, IllegalAccessException{
            Field f = packet.getClass().getDeclaredField("e"); f.setAccessible(true); ((Collection<String>) f.get(packet)).add(pl.getName());
        }
        public enum PACKET { CREATE, DESTROY, UPDATE }
      
          
        private static void sendPacket(String playerName, PacketPlayOutScoreboardTeam packet){
            Player player = Bukkit.getPlayerExact(playerName);
            if(player == null || packet == null) return;
            ((CraftPlayer) player).getHandle().playerConnection.sendPacket(packet);
        }
          
    
    }
    

    crash report;

    Code:
    [14:15:36] [Client thread/FATAL]: Unreported exception thrown!
    java.lang.NullPointerException
        at ayw.d(SourceFile:169) ~[1.7.4-Optifine-Standart_.jar!/:?]
        at bjw.a(SourceFile:1160) ~[1.7.4-Optifine-Standart_.jar!/:?]
        at hu.a(SourceFile:99) ~[1.7.4-Optifine-Standart_.jar!/:?]
        at hu.a(SourceFile:13) ~[1.7.4-Optifine-Standart_.jar!/:?]
        at ef.a(SourceFile:164) ~[1.7.4-Optifine-Standart_.jar!/:?]
        at bjz.e(SourceFile:212) ~[1.7.4-Optifine-Standart_.jar!/:?]
        at azi.p(SourceFile:1286) ~[1.7.4-Optifine-Standart_.jar!/:?]
        at azi.ah(SourceFile:760) ~[1.7.4-Optifine-Standart_.jar!/:?]
        at azi.f(SourceFile:711) [1.7.4-Optifine-Standart_.jar!/:?]
        at net.minecraft.client.main.Main.main(SourceFile:152) [1.7.4-Optifine-Standart_.jar!/:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_79]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_79]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_79]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_79]
    Thanks :)
     
    Last edited: Jan 29, 2016
  2. Offline

    Astrophylite

    @Speaw This seems to be a problem with your client, unless you have OptiFine installed on your server (which you can't unless you have Cauldron).
     
  3. Offline

    WolfMage1

    Code:
    java.lang.NullPointerException
    Code:
    at so.team.launcher.threads.MinecraftLaunchThread.run(MinecraftLaunchThread.java:61)
    Im guessing so.team.launcher.threads is something to do with you seeing as that's your package name.
     
Thread Status:
Not open for further replies.

Share This Page