Solved Optimizing Class

Discussion in 'Spigot Plugin Development' started by AwesomestGamer, Jan 24, 2020.

  1. Hi. The following piece of code seems to be causing issues and causing it so that it takes mobs a while to die, even though the event is triggered when they should have died. I know I should've used PascalCase for the classes, but I started this project before I knew that and wanted to keep it in line with the project's style. My newer plugins all use the correct formatting.

    Code (Text):

    package to.us.blockverse.MonsterDefence;

    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 org.bukkit.scheduler.BukkitScheduler;
    import org.bukkit.scoreboard.*;

    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.UUID;

    import to.us.blockverse.MonsterDefence.MonsterDefence;

    import static org.bukkit.Bukkit.getServer;


    public class scoreboard implements Listener {
        static MonsterDefence plugin = MonsterDefence.getPlugin(MonsterDefence.class);
        static ScoreboardManager manager = Bukkit.getScoreboardManager();
        static final Scoreboard board = manager.getNewScoreboard();
        static final Objective o = board.registerNewObjective("MDScoreboard", "");
        static Score experienceNeeded = o.getScore(ChatColor.GOLD + "Experience: ");
        @EventHandler
        public void join(PlayerJoinEvent event) {
            UUID playeruuid = event.getPlayer().getUniqueId();
            String stringid = playeruuid.toString();
            plugin.getLogger().info("Newbie ID of player is " + stringid);
            plugin.getCustomConfig().addDefault(stringid+".experience", 0);
            Date now = new Date();
            SimpleDateFormat format = new SimpleDateFormat("MM/dd/yy");
            final Player player = event.getPlayer();




            Team experience = board.registerNewTeam("teamname");
            experience.setSuffix("nil");


            o.setDisplaySlot(DisplaySlot.SIDEBAR);
            o.setDisplayName(ChatColor.RED + "§lMONSTER DEFENCE");
            Score date = o.getScore(ChatColor.GRAY + format.format(now));
            date.setScore(10);
            Score blank9 = o.getScore("          ");
            blank9.setScore(9);
            Score monsterDefence8 = o.getScore("Monster Defence");
            monsterDefence8.setScore(8);
            Score monsterDefence7 = o.getScore("is in beta!");
            monsterDefence7.setScore(7);
            Score blank6 = o.getScore("      ");
            blank6.setScore(6);
            experienceNeeded = o.getScore(ChatColor.GOLD + "Experience: " + ChatColor.YELLOW + plugin.getXP(player));
            experienceNeeded.setScore(5);
            Score blank4 = o.getScore("    ");
            blank4.setScore(4);
            Score version = o.getScore("Version: "+ ChatColor.GRAY + "u0.0.9");
            version.setScore(3);
            Score blank2 = o.getScore("  ");
            blank2.setScore(2);
            Score ip = o.getScore(ChatColor.AQUA + "§lBLOCKVERSE.US.TO");
            ip.setScore(1);
            player.setScoreboard(board);

            /*BukkitScheduler scheduler = getServer().getScheduler();
            scheduler.scheduleSyncRepeatingTask(plugin, new Runnable() {
                @Override
                public void run() {
                    board.resetScores(experienceNeeded[0].getPlayer());
                    experienceNeeded[0] = o.getScore(ChatColor.GOLD + "Experience: "+ ChatColor.YELLOW + plugin.getXP(player));
                    experienceNeeded[0].setScore(5);
                }
            }, 0L, 0L);
    */



        }
        @EventHandler
        public void updateEXPScoreboard(xpUpdateEvent event) {
            board.resetScores(experienceNeeded.getPlayer());
            experienceNeeded = o.getScore(ChatColor.GOLD + "Experience: " + ChatColor.YELLOW + plugin.getXP(event.getPlayer()));
            experienceNeeded.setScore(5);
        }


    }
     
    Code (Text):

    package to.us.blockverse.MonsterDefence;

    import org.bukkit.entity.Player;
    import org.bukkit.event.Cancellable;
    import org.bukkit.event.Event;
    import org.bukkit.event.HandlerList;


    public class xpUpdateEvent extends Event {
        private Player player;

        private static final HandlerList handlers = new HandlerList();
        private String message;

        public xpUpdateEvent(Player player) {
            this.player = player;
        }

        public HandlerList getHandlers() {
            return handlers;
        }

        public Player getPlayer() {
            return this.player;
        }

        public static HandlerList getHandlerList() {
            return handlers;
        }

    }
     
     
  2. Neither of your classes appear to have anything to do with mob death. The only event I see being handled is the PlayerJoinEvent, which is irrelevant unless your MonsterDefence plugin is using scoreboard values to determine when a mob should die. If so, we would need to see that code as well.
     
    • Like Like x 1
    • Agree Agree x 1
  3. Firstly you can respect naming conventions:
    your packages name should be lower.case
    your classes name should be UpperCase

    Secondly, don't use static here. I suggest you to read this page. ;)

    Thirdly, I think you should create objects to separate your different features like exp/scoreboard managing. I suggest you to read about the Single responsability principle.
     
    • Agree Agree x 1
  4. As I've said, this project was started before I learned about naming conventions, so I want to be consistent within this project. Yes, I know about the refactor button, and it's on my todo list to fix it using that. I don't know how I would do it without static, StackExchange said to use static here.
     
  5. It takes litterally 10 seconds so do it =p

    Never do something you don't understand. If you don't understand how static and OOP work, I suggest you to learn it. You should learn Java before using Spigot because it's written in Java.
     
    • Agree Agree x 2
  6. Strahan

    Benefactor

    While I agree with the sentiment, I despise how people have taken "literally" and use it in so many ways it shouldn't be used. (sigh) lol

    This I cannot agree with enough. Take the time to learn how things work. Don't just plug in "magic code" because when shit goes sideways, you have no idea then how to even properly debug things.
     
    • Agree Agree x 1
  7. ugh, that scoreboard will eat your tps like crazy
     
  8. Can you explain why?
     
  9. yes, it removes and readd scores which is very expensive, the teams system as scores can be changed easier as it can be updated only which is easier to run
     
  10. Mind pointing me to some examples?
     
  11. create a score with a name, then a team with the same name, then that score will show into scoreboard as a team, after that all you have to do is to change suffix/prefix to update your values
     
  12. That naming should be changed before changing anything else, nightmare material xD, try using the refactor button to do it quickly, if you don’t have more than 10 classes in your plugin it will take a minute.
     
  13. Thanks for all of your help. I've fixed it. Marking thread as resolved. Please do not lock the thread though, thanks.
     
    • Agree Agree x 1