Fireworks not launching after the first time

Discussion in 'Plugin Development' started by leduyquang753, Dec 10, 2017.

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

    leduyquang753

    I have developed my WelcomeNoob 1.2 plugin for 7 hours now, since 10h15. Now the plugin is almost ready to be released, except one problem. After the first firework launch since the server starts, the celebrating fireworks for the new players didn't work anymore.
    The class Events.java implements the PlayerJoinEvent is here:
    Code:
    public class Events implements Listener {
        Firework firework;
     
          private Plugin main() {
            return Main.main;
        }
     
        private FileConfiguration players() {
            return Main.players;
        }
     
        private String RanMsg(List<String> list, String playerName) {
            if (list.size() == 1) return Main.translate(list.get(0), playerName);
            return Main.translate(list.get(new Random().nextInt(list.size())), playerName);
        }
     
        public static Color getColor(int index) {
            switch (index) {
            case 0: return Color.AQUA;
            case 1: return Color.BLACK;
            case 2: return Color.BLUE;
            case 3: return Color.FUCHSIA;
            case 4: return Color.GRAY;
            case 5: return Color.GREEN;
            case 6: return Color.LIME;
            case 7: return Color.MAROON;
            case 8: return Color.NAVY;
            case 9: return Color.OLIVE;
            case 10: return Color.ORANGE;
            case 11: return Color.PURPLE;
            case 12: return Color.RED;
            case 13: return Color.SILVER;
            case 14: return Color.TEAL;
            case 15: return Color.YELLOW;
            }
            return Color.RED;
        }
     
        private void launch(Player p) {
            main().getLogger().info("Fireworks!");
            firework = p.getWorld().spawn(p.getEyeLocation(), Firework.class);
            firework.setFireworkMeta(Main.fm);
        }
     
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event) {
            Main.reloadPlayers();
            Player player = event.getPlayer();
            String name = player.getName();
            if (!players().contains(name.substring(0, 2))) {
                players().createSection(name.substring(0, 2));
            }
            boolean notFound = true;
            List<String> list = players().getStringList(name.substring(0, 2));
            for (String string : list) {
                if (string.equalsIgnoreCase(name)) notFound = false;
            }
            if (notFound) {
                String Title = RanMsg(Main.Title, name);
                String SubTitle = RanMsg(Main.SubTitle, name);
                player.sendTitle(Title, SubTitle, Main.welcomeTimings[0], Main.welcomeTimings[1], Main.welcomeTimings[2]);
                if (Main.firework) launch(player);
             
                players().set("count", players().getInt("count")+1);
                String message = RanMsg(Main.welcome, name);
                Bukkit.broadcastMessage(Main.translate(message, name));
             
                list.add(name);
                players().set(name.substring(0, 2), list);
                try {
                    players().save(Main.players_file);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                for (int i : Main.milestones) {
                    if (i == players().getInt("count")) {
                        String MTitle = RanMsg(Main.MilestoneTitle, name);
                        String MSubTitle = RanMsg(Main.MilestoneSubTitle, name);
                        String MMsg = RanMsg(Main.MilestoneMessage, name);
                        for (Player p : Bukkit.getOnlinePlayers()) {
                            p.sendTitle(MTitle, MSubTitle, 10, 100, 40);
                            p.playSound(p.getEyeLocation(), Sound.ENTITY_ENDERDRAGON_AMBIENT, 1, 1);
                        }
                        Bukkit.broadcastMessage(MMsg);
                        final TNTPrimed tnt = (TNTPrimed) player.getWorld().spawn(player.getEyeLocation(), TNTPrimed.class);
                        tnt.setFuseTicks(100);
                        main().getServer().getScheduler().runTaskLater(main(), new Runnable() {
                            public void run() {
                                Location tntloc = tnt.getLocation();
                                World tntworld = tnt.getWorld();
                                tnt.remove();
                                tntworld.createExplosion(tntloc, 0); 
                            }
                        }, 80L);
                    }
                }
            }
        }
    }
    The Main.* methods and stuff have nothing to talk about so I don't post it here.
    Any thoughts here please? @Zombie_Striker and @timtower, can you help again?
     
    Last edited: Dec 10, 2017
  2. Online

    timtower Administrator Administrator Moderator

    @leduyquang753 Don't randomly tag people, just wait till they see it for themselves.
     
  3. Offline

    leduyquang753

    @timtower So you have just said that and don't have any ideas? :(
     
  4. Online

    timtower Administrator Administrator Moderator

    @leduyquang753 And that is why you don't randomly tag them.
    They don't always know the answer.
    We don't even know what it is for the most things.
    Main.milestones, no idea, list? Array? Set?
    players.getInt("count"), what does it represent?

    Did you debug? What have you tried to find the issue?
     
  5. Offline

    leduyquang753

    OK @timtower if you need the Main class then here:
    Code:
    public class Main extends JavaPlugin {
        public static int[] welcomeTimings = new int[]{20, 200, 40};
        public static List<String> welcome;
        public static List<String> Title;
        public static List<String> SubTitle;
        public static List<String> MilestoneTitle;
        public static List<String> MilestoneSubTitle;
        public static List<String> MilestoneMessage;
        public static int fireworkTime = 2;
        public static boolean fireworkFlicker = true;
        public static boolean fireworkTrail = true;
        public static int fireworkType = 2;
        public static List<Integer> fireworkColors;
        public static List<Integer> fireworkFadeColors;
        public static Plugin main;
        public static boolean firework = true;
        public static File players_file;
        public static FileConfiguration players;
        public static List<Integer> milestones;
        public static FireworkMeta fm;
        @Override
        public void onEnable() {
            main = this;
            getLogger().info("Loading config...");
            loadConfig();
            getLogger().info("Registering events...");
            regEvents();
            getLogger().info("WelcomeNoob loaded.");
        }
      
        @Override
        public void onDisable() {
            getLogger().info("WelcomeNoob disabled.");
        }
      
        public void loadConfig() {
            try {
                if (!getDataFolder().exists()) {
                    getDataFolder().mkdirs();
                }
                File file = new File(getDataFolder(), "config.yml");
                if (!file.exists()) {
                    getLogger().warning("The file config.yml was not found. We are creating it.");
                    saveResource("config.yml", false);
                }
    
                getLogger().info("Loading config.yml");
                FileConfiguration cfg = getConfig();
                cfg = YamlConfiguration.loadConfiguration(file);
                    welcome = cfg.getStringList("Welcome");
                    Title = cfg.getStringList("WelcomeTitle");
                    SubTitle = cfg.getStringList("WelcomeSubTitle");
                    welcomeTimings[0] = cfg.getInt("FadeIn");
                    welcomeTimings[1] = cfg.getInt("Stay");
                    welcomeTimings[2] = cfg.getInt("FadeOut");
                    firework = cfg.getBoolean("EnableFirework");
                    fireworkTime = cfg.getInt("Firework.FlightTime");
                    fireworkFlicker = cfg.getBoolean("Firework.Flicker");
                    fireworkTrail = cfg.getBoolean("Firework.Trail");
                    fireworkType = cfg.getInt("Firework.Type");
                    fireworkColors = cfg.getIntegerList("Firework.Colors");
                    fireworkFadeColors = cfg.getIntegerList("Firework.FadeColors");
                    milestones = cfg.getIntegerList("Milestones");
                    MilestoneTitle = cfg.getStringList("MilestoneTitle");
                    MilestoneSubTitle = cfg.getStringList("MilestoneSubTitle");
                    MilestoneMessage = cfg.getStringList("MilestoneMsg");
                    final Firework fwork = Bukkit.getWorlds().get(0).spawn(new Location(Bukkit.getWorlds().get(0),0,0,0), Firework.class);
                    fm = buildEffect(fwork);
                    fwork.remove();
                players_file = new File(getDataFolder(), "players.yml");
                if (!players_file.exists()) {
                    getLogger().warning("The file players.yml was not found. We are creating it.");
                    players_file.getParentFile().mkdirs();
                    saveResource("players.yml", false);
                }
                getLogger().info("Loading players.yml");
                players = new YamlConfiguration();
                players.load(players_file);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
      
        private void regEvents() {
            Bukkit.getPluginManager().registerEvents(new Events(), main);
        }
      
        public static String translate(String in, String playername) {
            String str = in;
            str = str.replaceAll("%player%", playername);
            str = str.replaceAll("%count%", players.getInt("count") + "");
            str = ChatColor.translateAlternateColorCodes('&', str);
            return str;
        }
      
        public static void reloadPlayers() {
            players = new YamlConfiguration();
            try {
                players.load(players_file);
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (InvalidConfigurationException e) {
                e.printStackTrace();
            }
        }
      
        public static FireworkMeta buildEffect(Firework fw) {
            FireworkMeta effect = fw.getFireworkMeta();
            FireworkEffect.Builder builder = FireworkEffect.builder();
            effect.setPower(Main.fireworkTime);
            builder = builder.flicker(Main.fireworkFlicker);
            builder = builder.trail(Main.fireworkTrail);
            switch (Main.fireworkType) {
            case 0: builder = builder.with(Type.BALL);
            case 1: builder = builder.with(Type.BALL_LARGE);
            case 2: builder = builder.with(Type.STAR);
            case 3: builder = builder.with(Type.CREEPER);
            case 4: builder = builder.with(Type.BURST);
            }
            for (int i : Main.fireworkColors) builder = builder.withColor(Events.getColor(i));
            for (int i : Main.fireworkFadeColors) builder = builder.withFade(Events.getColor(i));
            effect.addEffect(builder.build());
            return effect;
        }
    }
    Main.milestones is a List<Integer> stores the milestones: when the number of new players reaches one of them a celebration will happen.
    The "count" is an int in the file players.yml and it stores the current number of new players.
    I worked on this problem for 1 hour before posting this thread.
    And I applied some fixes but none of them worked.
     
  6. Online

    timtower Administrator Administrator Moderator

    @leduyquang753 You worked on it, but did you debug?
    And this is pretty much static abuse, move away from any static, you don't need it at all.
     
  7. Offline

    leduyquang753

    Of course I had debugged and I saw: the firework launching code was activated:
    Code:
    public class Events implements Listener {
    // .........
    private void launch(Player p) {
            main().getLogger().info("Fireworks!");
            firework = p.getWorld().spawn(p.getEyeLocation(), Firework.class);
            firework.setFireworkMeta(Main.fm);
        }
    // ..........
    }
    The "Fireworks!" message was logged properly, only the firework didn't appeared. No visual effects as well as the sounds.
     
Thread Status:
Not open for further replies.

Share This Page