Jump to content
  • 0

IconsTable loads data everytime


Question

Posted (edited)

Hello , ive found on forum this code and added it to my project , i am using it for shift + click and lucky chest feature , so that when you open a chest an htm window appears and tells you what item you obtained , count and adds the icon of the item. 

Problem is that everytime shift + click is requested or opening chest , all 10k icons loads and i get this message on gs log:

IconsTable: Succesfully loaded 9209 icons, in 17.0 Milliseconds.

 

the code im using is this:

 

package net.sf.l2j.gameserver.datatables;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

import net.sf.l2j.gameserver.GameServer;
import net.sf.l2j.gameserver.xmlfactory.XMLDocumentFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

public class IconsTable
{
    public static final Map<Integer, String> Icons = new HashMap<>();
    private static int count;
    private static long t0;
    private static double t;

    private static final Logger _log = Logger.getLogger(GameServer.class.getName());

    public void reload()
    {
        Icons.clear();
        parseData();
    }

    public static void parseData()
    {
       count=0;
        t0 = System.currentTimeMillis();
        try
        {
            File f = new File("./data/xml/icons.xml");
            Document doc = XMLDocumentFactory.getInstance().loadDocument(f);

            for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling())
            {
                if ("list".equalsIgnoreCase(n.getNodeName()))
                {
                    for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling())
                    {
                        if (d.getNodeName().equalsIgnoreCase("icon"))
                        {
                            count++;
                            NamedNodeMap attrs = d.getAttributes();
                            Node att = attrs.getNamedItem("Id");
                            Node att2 = attrs.getNamedItem("value");
                            Icons.put(Integer.valueOf(att.getNodeValue()), String.valueOf(att2.getNodeValue()));
                        }
                    }
                }
            }
            t = System.currentTimeMillis() - t0;
            _log.config("IconsTable: Succesfully loaded "+count+" icons, in "+t+" Milliseconds.");
        }
        catch (Exception e)
        {
            _log.config("IconsTable: Failed loading IconsTable. Possible error: "+e.getMessage());
        }
    }

    public static String getIcon(int id)
    {

        if (Icons.get(id)==null)
        {
            _log.config("IconsTable: Invalid Icon request: "+id+", or it doesn't exist, Ignoring ...");
            return "null";
        }
        return Icons.get(id);
    }

    public static final IconsTable getInstance()
    {
        parseData();
        return SingletonHolder._instance;
    }

    private static class SingletonHolder
    {
        protected static final IconsTable _instance = new IconsTable();
    }
}

 

and when i want to get the icon i use this


IconsTable.getInstance().getIcon(ITEMID)) , what i need to know is that if there isn't any way to store all this 10k icons , and load them only once , not for every request to get icon.. 

 

 

EDIT: PROBLEM SOLVED , HERE IS THE FIX :serious:

   public static String getIcon(int id)
    {

        if (Icons.get(id)==null)
        {
            parseData();
            _log.config("IconsTable: Invalid Icon request: "+id+", or it doesn't exist, Ignoring ...");
            return "null";
        }
        return Icons.get(id);
    }

    public static final IconsTable getInstance()
    {
        return SingletonHolder._instance;
    }

i had to remove parsedata(); from the getInstance() and add it after if(icons.get(id)==null) , hope that this won't slowdown server much

Edited by arm4729

2 answers to this question

Recommended Posts

  • 0
Posted

You were invoking the parse Data method to fill the Map from the xml every time you made the query of the icon for its key, the first thing if it could have reduced the performance of your server.

  • 0
Posted

This is not a fix, you should avoid parsing data with every getInstance() call which you already did and parse data once in a private constructor. Reload method should be called on admin commands/bypasses. Anyway, i'm locking this topic since you feel satisfied with this solution. In case you want to unlock send me a dm.

Guest
This topic is now closed to further replies.


  • Posts

    • Fix Visual Boundary for AutoFarm when entering a new zone. Fix Assassin Interface Automatic SoulShot usage. Fix Assassin Interface not displaying Castle/Base. Fix Achievements displaying item rewards for CommunityBoard & NPC. Fix Prevent players from purchasing their own Auctioned items. Added ''.raid'' and ''.achievement'' commands. Added support for multiple currencies on Auction Added Search feature to Auction. Added Offline Stores Added '.exit' & '.quit' command to Dungeon System so players can now exit/quit dungeons Added VIP Account System (Alternative XP, SP & Drop Rates, Unlocks Costumes) Added Loot Box System Changed DungeonsManager now displays reward list on dungeon pages. Changed GlobalShop to include pages for all currencies. HTML/XML edits
    • When I teleport to town, my current location is differ from the map. How do I fix this?    
    • A New Chapter Begins We're Rebuilding – Join Our Staff Team After many years of activity, growth, and challenges, it’s finally time for our community to restructure and move forward. We’re ready to turn a new page and evolve into something greater — but we can’t do it without the help of passionate and committed people. That’s why we’re now opening up staff applications for those who want to actively shape the future of our community. If you have the motivation, time, and patience to contribute to something meaningful, this is your chance to step in and make a real impact. What We're Looking For We’re building a fresh and dedicated team of individuals who are ready to support and grow this project. Open roles include: Moderators – to keep the forum clean, safe, and organized Gaming Moderators – to help manage gaming boards (e.g., Lineage, GTA FiveM) Content Creators – to post updates, guides, and articles Community Managers – to engage users and drive activity Technical Staff – for development, backend, and server work We’re not focusing only on Lineage anymore. Our vision is expanding to new areas — including GTA FiveM and other multiplayer games you might have expertise in. If you have a good idea, a server plan, or something new to suggest — we’re open to it. Now’s the time to bring it forward. Requirements We’re looking for individuals who have: A history of activity on the forum (preferred) Available time to contribute consistently A sense of teamwork and responsibility A genuine interest in gaming and community building If you're interested, just send a private message to me or Celestine. (or just reply here) Tell us a few things about yourself and how you’d like to contribute. Let’s bring this community back to life. Let’s rebuild something great — together.   M M G A 
  • Topics

×
×
  • Create New...