Optimize this Code

Discussion in 'Spigot Plugin Development' started by GabrielHD150, Jul 17, 2018.

  1. Hi, Well, I started working with entities very little, and I wanted to know if this code can be optimized more or use other methods that do not affect server performance so much.

    Code: https://hastebin.com/kavutiqubu.java

    PSDT: I put it in Hastebin since Spigot does not let me put many characters
     
  2. I suggest not to call too much method.
    Like you can simplify:
    Code (Java):
    p.sendmessage("Hello " + p.getName());
    p.sendmessage("Welcome to our server " + p.getName());
     
    Into
    Code (Java):
    String playername = p.getName();
    sendMsgMethod(p, "Hello " + playername);
    sendMsgMethod(p, "welcome " + playername);
     
    Edit: I also heard that making things run asynchronous-ly could help from tps issues
     
    • Optimistic Optimistic x 1
  3. You can put the events in extra classes, for more order
     
  4. Optic_Fusion1

    Resource Staff

    that can literally just be changed to p.sendMessage("Hello " + p.getName() + " and welcome to our server"); or something
     
  5. The code works, the bad thing is that after a good time it starts to have a bad performance server and I do not work with pathfindergoals, and what I saw this tutorial: https://www.spigotmc.org/threads/tutorial-creating-custom-entities-with-pathfindergoals.18519/ the case is that I want to know how I can fix or reduce so that it does not affect the performance of the server, that code I did as I saw on Wikis etc, but after I probe it, the performance is very bad.
     
    1. No point calling setHealth(0) after calling entity.remove().
    2. The way you handle spawneggs is also quite messy in nature. I don't agree with your solution, but I get why you did it.
    3. Don't use try{}catch(Exception e){}, you need to fix up error instead of hiding them that way.
    4. There's no point in having a delayed task of 5 ticks for the spawn egg code just to set a target.
    5. Use meaningful variable names. Why name variables localObject1 and localObject2
    6. You have a lot of repeated code in the armour setter for skeletons and zombies. It seems like you can halve it by just having
    7. Code (Text):
       if((((Entity)localObject1).getType() == EntityType.ZOMBIE||((Entity)localObject1).getType() == EntityType.SKELETON) && ((Entity)localObject1).hasMetadata("SW_MOB")) {
    8. Your spawned mobs are immune to void damage, which keeps loaded mobs alive after falling into the void. I don't see the logic in this.
    9. I don't think entity explosion damage is covered by entitydamagebyentityevent, but I'm not too sure about that. Might wanna try it out
    10. The ondeath and onquit events are nasty in terms of performance. They will loop through every mob in every world. It's not necessary. Use Bukkit to remove the entity with the specified ID in mobs.get(p.getName())
     
  6. How could I do it? I was testing with that same event and it did not turn out very well. Minecraft entities are very confusing for me
     
  7. Fix the rest first, the rest are likely what's causing the lag issues (Mainly 3,4,8,10)
     
  8. How do I solve the 4? It's the one I use for the Mob to get a new objective and it's not me or the player who spawned it
     
  9. Just directly call setNewTarget without the delayedtask.
     
  10. Code (Java):
    public void setNewTarget(Entity et, Player excluded) {
            new BukkitRunnable() {
                @Override
                public void run() {
                    for(Entity en : et.getNearbyEntities(7, 7, 7)){
                        if(en instanceof Player){
                            Player pl = (Player) en;
                            if(!pl.getName().equals(excluded.getName())){
                                if(pl.getGameMode() != GameMode.SPECTATOR) {
                                    Monster monster = (Monster) et;
                                    monster.setTarget(pl);
                                    return;
                                }
                            }
                        }
                    }
                    Monster monster = (Monster) et;
                    monster.setTarget(null);
                }
            }.runTaskTimer(this, 0L, 5L);
        }
    Something like that?
     
  11. No. You do not need the bukkit runnable at all. Remove the bukkit runnable, and run the code inside without it.
     
  12. If you can't understand 'without the delayedtask' then I think you should learn some common sense first. Just use it.

    public static String tk (String a ){
    return a.replaceAll("&", "§");
    }

    this will replace 'I like cats & dogs' with 'I like cats § dogs'
    Just use:
    return ChatColor.translateAlternateColorCodes('&', a);
     
  13. but when I remove that, the mob starts attacking the player who spawned it
     
  14. Then cancel mobtarget event instead.