Thread: Improvements to my code

Results 1 to 4 of 4
  1. #1 Improvements to my code 
    Donator


    Join Date
    Nov 2012
    Age
    25
    Posts
    242
    Thanks given
    250
    Thanks received
    62
    Rep Power
    89
    Hey, so I've just written this piece of code for my server, what it does is search for all of the CacheObject's within your region based on the name in the parameter, and finds the closest one to you.

    It works, but I was wondering if I could get some feedback and advice on how I can improve my code.

    Code:
        /**
         * Finds the closest cache object in the bot's region, based on its name.
         *
         * @param name the name.
         * @return the cache object.
         */
        public CacheObject searchForClosestCacheObject(String name) {
            Regions r = World.getWorld().getRegionManager().getRegionByLocation(getPosition());
            int[][] objData = new int[0][3];
            int count = 0;
            for (CacheObject object : r.getGameObjects()) {
                if (object == null) {
                    continue;
                }
                GameObjectData data = GameObjectData.forId(object.getDef().getId());
                if (data == null) {
                    continue;
                }
                if (data.getName().equalsIgnoreCase(name)) {
                    count++;
                    objData = new int[count + 1][3];
                    objData[count][0] = Misc.getDistance(object.getLocation(), getPosition());
                    objData[count][1] = object.getLocation().getX();
                    objData[count][2] = object.getLocation().getY();
                }
            }
            int lowestDistance = objData[0][0];
            Position objectPosition;
            CacheObject object = null;
            for (int i = 0; i < objData.length; i++) {
                if (objData[i][0] == -1) {
                    continue;
                }
                if (objData[i][0] < lowestDistance) {
                    lowestDistance = objData[i][0];
                    objectPosition = new Position(objData[i][1], objData[i][2], getPosition().getZ());
                    object = ObjectLoader.object(objectPosition.getX(), objectPosition.getY(), getPosition().getZ());
                }
            }
            return object;
        }
    I know that there must be a better way of doing this, possible a Map<Integer, Position>? I never have really worked with Maps/Hashmaps, so, I just used a 2D array. Thanks.
    Quote Originally Posted by Therandomkid View Post
    if you wanna hate just remember i fucked your mom
    Reply With Quote  
     

  2. #2  
    Boy Wonder


    Join Date
    Mar 2012
    Posts
    849
    Thanks given
    139
    Thanks received
    80
    Rep Power
    99
    i think you can do something like this, i coded this in a text editor though since i dont have your methods lol. you could probably improve this even more with optionals.
    You can add whatever checks you want with the name yourself, shouldn't be very difficult. when you add checks for matching the name, convert the list to a stream and filter out where object is equal to name. Another thing you could do is overload ObjectLoader#object method to accept the Position type as a parameter and and get the x, y, z values from the position provided.

    Code:
    /**
         * Finds the closest cache object in the bot's region, based on its name.
         *
         * @param name the name.
         * @return the cache object.
         */
        public CacheObject searchForClosestCacheObject(String name) {
            Regions r = World.getWorld().getRegionManager().getRegionByLocation(getPosition());
            List<CacheObject> objData = new ArrayList<>(r.getGameObjects);
            Optional<CacheObject> closest = Optional.absent();
            while(objData.listIterator.hasNext()) {
            int current = Misc.getDistance(objData.listIterator.next().getLocation(), getPosition());
            if(current < Misc.getDistance(objData.listIterator.previous().getLocation(), getPosition()))
            	closest = Optional.fromNullable(ObjectLoader.object(objData.listIterator.next().getPosition().getX(), 
    							        	objData.listIterator.next().getPosition().getY(), 
    							        	objData.listIterator.next().getPosition().getZ()));
    
            }
            return closest.orNull();
        }

    Reply With Quote  
     

  3. Thankful user:


  4. #3  
    Donator


    Join Date
    Nov 2012
    Age
    25
    Posts
    242
    Thanks given
    250
    Thanks received
    62
    Rep Power
    89
    Quote Originally Posted by Metorrite View Post
    i think you can do something like this, i coded this in a text editor though since i dont have your methods lol. you could probably improve this even more with optionals.
    You can add whatever checks you want with the name yourself, shouldn't be very difficult. when you add checks for matching the name, convert the list to a stream and filter out where object is equal to name.

    Code:
    /**
         * Finds the closest cache object in the bot's region, based on its name.
         *
         * @param name the name.
         * @return the cache object.
         */
        public CacheObject searchForClosestCacheObject(String name) {
            Regions r = World.getWorld().getRegionManager().getRegionByLocation(getPosition());
            List<CacheObject> objData = new ArrayList<>(r.getGameObjects);
            Optional<CacheObject> closest = Optional.absent();
            while(objData.listIterator.hasNext()) {
            int current = Misc.getDistance(objData.listIterator.next().getLocation(), getPosition());
            if(current < Misc.getDistance(objData.listIterator.previous().getLocation(), getPosition()))
            	closest = Optional.fromNullable(ObjectLoader.object(objData.listIterator.next().getPosition().getX(), 
    							        	objData.listIterator.next().getPosition().getY(), 
    							        	objData.listIterator.next().getPosition().getZ()));
    
            }
            return closest.orNull();
        }
    Thanks! I'm going to look into more of this so I understand it better.
    Quote Originally Posted by Therandomkid View Post
    if you wanna hate just remember i fucked your mom
    Reply With Quote  
     

  5. #4  
    Boy Wonder


    Join Date
    Mar 2012
    Posts
    849
    Thanks given
    139
    Thanks received
    80
    Rep Power
    99
    Quote Originally Posted by Venn View Post
    Thanks! I'm going to look into more of this so I understand it better.
    sure np, this may or may not even work tbh, you can hmu on skype if you want help. skype is on my profile.

    Reply With Quote  
     


Thread Information
Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)


User Tag List

Similar Threads

  1. Replies: 8
    Last Post: 11-17-2012, 05:06 AM
  2. Improve my code!
    By Rainaka in forum Application Development
    Replies: 21
    Last Post: 10-08-2012, 05:43 AM
  3. Looked for experienced IRC Programmer to help finalize my code
    By Yous in forum Application Development
    Replies: 4
    Last Post: 01-29-2012, 08:15 AM
  4. Replies: 3
    Last Post: 09-11-2011, 10:34 PM
  5. Can someone help improve my code
    By thatwasonce in forum Website Development
    Replies: 4
    Last Post: 06-05-2009, 10:09 PM
Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •