Thread: Alternate NPC Drop System

Page 1 of 2 12 LastLast
Results 1 to 10 of 19
  1. #1 Alternate NPC Drop System 
    Member

    Join Date
    Sep 2007
    Posts
    614
    Thanks given
    0
    Thanks received
    1
    Rep Power
    0
    Base Used : DarkScape [RichScape]
    Difficulty : 2/10
    Required Knowledge : Basic java terms, C&P, and a knowledge of your Server.java, and NPCHandler.java
    Modified Classes : Server.java, NPCHandler.java
    New Classes : Drop.java, DropHandler.java

    A NPC drop system which uses Array Lists and a more compact drop method.

    Why did I do this? There are a few ways of doing drops (that I know of)
    -Loading them into an array from a cfg, using numbers as percents.
    -Using int arrays for the item ID, and then using the array size for the chance

    What was wrong? First method, I experienced exceptions when I added too many drops. Second, if you wanted an item to have a lower chance of dropping, you basically added a bunch of -1's or 'junk' drops to make it harder to pick the real drop, not very practical.

    What does this do? It lets you add drops through a java file, have as many as you want, and not have to worry about messing with a CFG format, or even load a CFG at all! This also gives you the ability to have ANY monster drop a specific item. Perfect for making all monsters have a small chance of dropping something rare like a half shield or something.

    You may have a method in your NPCHandler called "MonsterDropItems(int NPCID, NPC npc)"
    If so, continue. If not, you may need to adjust for this tutorial.

    Replace that, with this.

    Code:
          public void MonsterDropItems(int NPCID, NPC npc) {
            try {
                ArrayList<Drop> DropList = DropHandler.getDrop(NPCID);
                if (DropList.size() == 0) { return; }
                for (int i = 0; i < DropList.size(); i++) {
                    double chance = Math.random() * 100;
                    if (chance <= DropList.get(i).chance) {
                        int randomAmount = Misc.random(DropList.get(i).dropAmount);
                        if (randomAmount <= 0) { randomAmount = 1; }
                        Item.addItem(DropList.get(i).dropID, npc.absX, npc.absY, randomAmount, npc.getKiller(), false);
                    }
                }
            } catch (Exception e) {
                System.out.println("Exception at drops");
                e.printStackTrace();
            }
        }
    Also, at the TOP of your NPCHandler, you should see something like

    Code:
    import java.io.*;
    Below that, add this.
    Code:
    import java.util.ArrayList;

    Next, add a new class called "Drop.java" and inside it, paste the following
    Code:
    /**
     *
     * @author andrew (blood argon)
     */
    public class Drop {
        public int npcID;
        public int dropID;
        public int dropAmount;
        public int chance;
        public Drop(int npc, int id, int amt, int c) {
            npcID = npc;
            dropID = id;
            dropAmount = amt;
            chance = c;
        }
    }
    Next, make another class called "DropHandler,java", and in it, paste this.
    Code:
    
    import java.util.ArrayList;
    
    /**
     *
     * @author andrew (Blood argon)
     */
    public class DropHandler {
    
        /* ArrayList of the Drop class
         */
        private static ArrayList<Drop> drops = new ArrayList<Drop>();
        private final int ANY = -1;
        /* Sets up initial drops
         * Usage : drops.add(new Drop(new int[] { npcIDs, npcIDs, ...} ID, Item ID, ItemAmount, Chance of Drop));
         * NPCID can be changed to "ANY" to have all monsters drop this item.
         */
    
        public void setDrops() {
            /* Group : Men */
            newDrop(new int[] {1,2}, 995, 500, 20);
            newDrop(new int[] {1,2}, 526, 1, 100);
            System.out.println("[DropHandler] Loaded " + drops.size() + " drops");
        }
    
        public void newDrop(int npc[], int item, int amount, int chance) {
            for (int i = 0; i < npc.length; i++) {
                drops.add(new Drop(npc[i], item, amount, chance));
            }
        }
    
        /* Returns an array of possible drops based off the NPC id
         */
        public static ArrayList getDrop(int npc) {
            ArrayList<Drop> npcDrop = new ArrayList<Drop>();
            for (int i = 0; i < drops.size(); i++) {
                if (drops.get(i).npcID == npc || drops.get(i).npcID == -1) {
                    npcDrop.add(drops.get(i));
                }
            }
            return npcDrop;
        }
    }
    Finally, go into your Server.java, and declare the variable below.
    Code:
    public static DropHandler DropHandler = null;
    And then where you see things like this
    Code:
            Dialogue = new Dialogue();
            Frame = new Frame();
            Special = new Special();
            Text = new Text();
            NpcManager = new NPCManager();
            item = new Item();
            shop = new Shop();
            Prayer = new Prayer();
    Add the following
    Code:
            DropHandler = new DropHandler();
            DropHandler.setDrops();
    And your done!
    Your drops are set in the DropHandler, under the setUp() void. Iv included some examples on how the system works. If you have any questions, feedback, or design criticism, post here.

    And for anyone who thinks its leeched because of the @author andrew, here is your picture.
    IDE uses the user account as the author name.
    Last edited by bloodargon; 07-08-2009 at 04:58 AM. Reason: Updated to make multi-npc drop adding
    Reply With Quote  
     

  2. #2  
    Member

    Join Date
    Sep 2007
    Posts
    614
    Thanks given
    0
    Thanks received
    1
    Rep Power
    0
    Needsmoarreadsbump?
    Reply With Quote  
     

  3. #3  
    Banned

    Join Date
    May 2008
    Posts
    2,327
    Thanks given
    55
    Thanks received
    67
    Rep Power
    0
    psh a majority of this comunity doesn't no how to use arrays, in other words, this most likely won't be used

    nice tho

    unless ur name ISN'T andrew? then that makes u a leecher if its not
    Reply With Quote  
     

  4. #4  
    Member

    Join Date
    Sep 2007
    Posts
    614
    Thanks given
    0
    Thanks received
    1
    Rep Power
    0
    Quote Originally Posted by silabgarza View Post
    psh a majority of this comunity doesn't no how to use arrays, in other words, this most likely won't be used

    nice tho

    unless ur name ISN'T andrew? then that makes u a leecher if its not
    The IDE uses the user account to create these titles, I can take a screenshot if you'd like?
    And, I know they dont. But, the thing is, this makes it simple.
    It shows exactly how to make a drop, so, you cant really get it wrong.
    Reply With Quote  
     

  5. #5  
    Registered Member

    Join Date
    Dec 2007
    Age
    28
    Posts
    1,954
    Thanks given
    114
    Thanks received
    608
    Rep Power
    1094
    Alright I added it now, Compiles fine but I still get this

    Note: NPCHandler.java uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.
    ------------ Compile complete ------------
    Press any key to continue . . .
    bothers me
    Reply With Quote  
     

  6. #6  
    Member

    Join Date
    Sep 2007
    Posts
    614
    Thanks given
    0
    Thanks received
    1
    Rep Power
    0
    Quote Originally Posted by Penor View Post
    Alright I added it now, Compiles fine but I still get this



    bothers me
    I dont get that error on mine, but, don't worry about it. It does the same thing for Stream.java.
    Reply With Quote  
     

  7. #7  
    Registered Member
    wowfreakjoking's Avatar
    Join Date
    Sep 2007
    Posts
    319
    Thanks given
    0
    Thanks received
    0
    Rep Power
    226
    Quote Originally Posted by Penor View Post
    Alright I added it now, Compiles fine but I still get this



    bothers me
    Suppress the warning if it's that annoying
    Did you know?
    - I hate trivia.
    Reply With Quote  
     

  8. #8  
    Member

    Join Date
    Sep 2007
    Posts
    614
    Thanks given
    0
    Thanks received
    1
    Rep Power
    0
    Other than that, anyone have comments for this?
    Reply With Quote  
     

  9. #9  
    Sanadis
    Guest
    This is pretty nice, thanks for it. It took me a little bit to get it to work with my npcdrops.cfg (because I would be one of those noob's to array lists ).
    Reply With Quote  
     

  10. #10  
    Member

    Join Date
    Sep 2007
    Posts
    614
    Thanks given
    0
    Thanks received
    1
    Rep Power
    0
    Quote Originally Posted by Sanadis View Post
    This is pretty nice, thanks for it. It took me a little bit to get it to work with my npcdrops.cfg (because I would be one of those noob's to array lists ).
    If you have a large amount of NPCs I build a converter for drop.cfg -> this system, for the reason of not wanting to add 350+ drops
    Reply With Quote  
     

Page 1 of 2 12 LastLast

Thread Information
Users Browsing this Thread

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


User Tag List

Posting Permissions
  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •