Very High Timings

Discussion in 'Plugin Development' started by j0ach1mmall3, Nov 22, 2014.

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

    j0ach1mmall3

    Hello, so recently I found out my server was lagging a lot.
    I used the /timings command to get my timings.
    I found out that one plugin, custom coded, has some extremely high timings.
    Could you please help me find out what the issue is?
    Timings:
    ChooseableVoteRewards v1.0.0
    InventoryClickEvent (and others) Time: 77291485000 Count: 121415 Avg: 636589
    InventoryCloseEvent Time: 15065844000 Count: 18018 Avg: 836155
    Total time 92357329000 (92s)
    Code:
    Code:java
    1. package me.j0ach1mmall3.ChoosableVoteRewards;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.Material;
    9. import org.bukkit.command.Command;
    10. import org.bukkit.command.CommandSender;
    11. import org.bukkit.command.ConsoleCommandSender;
    12. import org.bukkit.entity.Player;
    13. import org.bukkit.event.EventHandler;
    14. import org.bukkit.event.EventPriority;
    15. import org.bukkit.event.Listener;
    16. import org.bukkit.event.inventory.InventoryClickEvent;
    17. import org.bukkit.event.inventory.InventoryCloseEvent;
    18. import org.bukkit.inventory.Inventory;
    19. import org.bukkit.inventory.ItemStack;
    20. import org.bukkit.plugin.java.JavaPlugin;
    21.  
    22. public class Main extends JavaPlugin implements Listener {
    23. Inventory VoteInv;
    24. List<Integer> VoteItemsAmounts = new ArrayList<Integer>();;
    25. List<String> VoteItemsIDs = new ArrayList<String>();;
    26. String KickMsg;
    27. String InvName;
    28. public void onEnable(){
    29. getConfig().options().copyDefaults(true);
    30. saveConfig();
    31. this.getServer().getPluginManager().registerEvents(this, this);
    32. List<String> VoteItemsIDs = getConfig().getStringList("VoteItemsIDs");
    33. List<Integer> VoteItemsAmounts = getConfig().getIntegerList("VoteItemsAmounts");
    34. String InvName = getConfig().getString("InvName").replaceAll("&", "§");
    35. String KickMsg = getConfig().getString("KickMsg").replaceAll("&", "§");
    36. }
    37.  
    38. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] a) {
    39. if(cmd.getName().equalsIgnoreCase("voted")){
    40. reloadConfig();
    41. VoteItemsIDs = getConfig().getStringList("VoteItemsIDs");
    42. VoteItemsAmounts = getConfig().getIntegerList("VoteItemsAmounts");
    43. String InvName = getConfig().getString("InvName").replaceAll("&", "§");
    44. if (sender instanceof Player){
    45. Player p = (Player) sender;
    46. if (p.hasPermission("cvr.voted")){
    47. if (a.length > 0){
    48. VoteInv = Bukkit.getServer().createInventory(null, 9, InvName);
    49. Player voted = Bukkit.getServer().getPlayer(a[0]);
    50. for (int x = 0; x < VoteItemsIDs.size(); x++){
    51. String VoteItemID = VoteItemsIDs.get(x);
    52. String[] IDAndData = VoteItemID.split(":");
    53. if (IDAndData.length == 2){
    54. VoteInv.setItem(x, new ItemStack(Material.getMaterial(Integer.parseInt(IDAndData[0])), VoteItemsAmounts.get(x), (short) Integer.parseInt(IDAndData[1])));
    55. } else if (IDAndData.length == 1){
    56. VoteInv.setItem(x, new ItemStack(Material.getMaterial(Integer.parseInt(IDAndData[0])), VoteItemsAmounts.get(x)));
    57. } }
    58. voted.openInventory(VoteInv);
    59. } else {
    60. p.sendMessage("§4Please specify a name");
    61. }
    62. } else {
    63. p.sendMessage("§4You do not have permission to do this");
    64. }
    65. } else if (sender instanceof ConsoleCommandSender){
    66. if (a.length > 0){
    67. VoteInv = Bukkit.getServer().createInventory(null, 9, InvName);
    68. Player voted = Bukkit.getServer().getPlayer(a[0]);
    69. for (int x = 0; x < VoteItemsIDs.size(); x++){
    70. String VoteItemID = VoteItemsIDs.get(x);
    71. String[] IDAndData = VoteItemID.split(":");
    72. if (IDAndData.length == 2){
    73. VoteInv.setItem(x, new ItemStack(Material.getMaterial(Integer.parseInt(IDAndData[0])), VoteItemsAmounts.get(x), (short) Integer.parseInt(IDAndData[1])));
    74. } else if (IDAndData.length == 1){
    75. VoteInv.setItem(x, new ItemStack(Material.getMaterial(Integer.parseInt(IDAndData[0])), VoteItemsAmounts.get(x)));
    76. }
    77. }
    78. voted.openInventory(VoteInv);
    79. } else {
    80. ConsoleCommandSender c = Bukkit.getServer().getConsoleSender();
    81. c.sendMessage(ChatColor.DARK_RED + "Please specify a name!");
    82. }
    83. }
    84. }
    85. return true;
    86. }
    87. @EventHandler(priority=EventPriority.LOWEST)
    88. public void onInvClose(InventoryCloseEvent e){
    89. reloadConfig();
    90. String InvName = getConfig().getString("InvName").replaceAll("&", "§");
    91. String InvName1 = e.getInventory().getName();
    92. if (InvName1 != null){
    93. if (InvName1.equals(InvName)){
    94. final Player p = (Player) e.getPlayer();
    95. Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    96. public void run() {
    97. p.openInventory(VoteInv);
    98. }
    99. });
    100. }
    101. }
    102. }
    103. @SuppressWarnings("deprecation")
    104. @EventHandler(priority=EventPriority.LOWEST)
    105. public void onPlayerClickInv(InventoryClickEvent e){
    106. String InvName1 = e.getInventory().getName();
    107. Player p = (Player) e.getWhoClicked();
    108. reloadConfig();
    109. String InvName = getConfig().getString("InvName").replaceAll("&", "§");
    110. String KickMsg = getConfig().getString("KickMsg").replaceAll("&", "§");
    111. if (InvName1 != null){
    112. if (InvName1.equals(InvName)){
    113. if ((e.getCurrentItem() != null) && (e.getCurrentItem().getType() != Material.AIR) && (VoteInv.contains(e.getCurrentItem()))){
    114. ItemStack is = e.getCurrentItem();
    115. e.setCancelled(true);
    116. p.closeInventory();
    117. p.getInventory().addItem(is);
    118. p.updateInventory();
    119. p.kickPlayer(KickMsg);
    120. }
    121. }
    122. }
    123. }
    124. }
     
  2. Offline

    CubieX

    You are reloading your config on evry inventory click, on every inventory close and on every "vote" command.
    Why?
    You only need to reload the config if you have changed the file outside of your plugin. For example after editing it directly with a text editor.
    I don't know if this is the reason for your lags. But it surely can be a source of lag with alot of players online who trigger those events costantly.

    Also: You should form the habit of checking the major conditions in an event handler first.
    Not the minor ones.
    You goal should always be to determine whether your EventHandler actions should be run or not as "fast" or as "effortless" as possible.
    Example: In your InventoryClickEvent, instead of first doing all this stuff:
    Code:
     String InvName1 = e.getInventory().getName();
    Player p = (Player) e.getWhoClicked();
    reloadConfig();
    String InvName = getConfig().getString("InvName").replaceAll("&", "§");
    String KickMsg = getConfig().getString("KickMsg").replaceAll("&", "§");
    if (InvName1 != null){
    if (InvName1.equals(InvName)){
    check the superior conditions first:
    Code:
    if ((e.getCurrentItem() != null) && (e.getCurrentItem().getType() != Material.AIR) && (VoteInv.contains(e.getCurrentItem()))){
    Because then you can leave the handler quickly if those conditions do not apply, without creating a lot of instances and reading your config and stuff and afterwars discovering you have to leave the handler anyway.
    This induces unneccessary CPU load and consumes time.
     
    Skionz likes this.
  3. Offline

    j0ach1mmall3

    Well, the reloading was to not have a reload command, because I wanted to keep the plugin as simple as possible.
     
  4. j0ach1mmall3
    But why are you even reloading the config? There's no need to do it.
     
  5. Offline

    j0ach1mmall3

    To not have a reload command....
     
  6. Offline

    Rocoty

    j0ach1mmall3 Why would you need to reload the config in the first place though?
     
  7. Offline

    mamifsidtect

    What Rocoty is saying is that you only need to save your config when you stop the server, so that any changes made to it while the server was running are applied and saved to disk
     
  8. Offline

    j0ach1mmall3

    Because the Data in the config specifies the items in the inventory
     
  9. Offline

    mythbusterma

    j0ach1mmall3

    Excellent, you still don't seem to get the point.


    Simplicity is not an excuse for laziness/poor performance. I would take a plugin that has to be told to reload over one that performs disk read/writes every time I execute a command any day.
     
  10. Offline

    j0ach1mmall3

    So what do I have to change then hmm?
     
  11. Offline

    CubieX

    Remove the "reloadConfig()" from your event handlers. ^^
    If you need such a function, make a specific reload command.
    As already mentioned: Reloading is only neccessary when you modified the config file outside of the game while the game is running. This is not a common case. So a dedicated reload command is a good thing.
    Reloading does a read from disk, which is slow. You don't want to execute slow actions if it's not necessary.
    Expeccially when you are already expieriencing lag from inefficiently written plugin code.
     
  12. Offline

    j0ach1mmall3

    Hmm, I'll just make them restart their server if they want to reload their config.
     
Thread Status:
Not open for further replies.

Share This Page