Thread: Content Manager, a very scaleable way of dealing with all the servers content

Results 1 to 4 of 4
  1. #1 Content Manager, a very scaleable way of dealing with all the servers content 
    Registered Member

    Join Date
    Sep 2007
    Age
    29
    Posts
    2,396
    Thanks given
    5
    Thanks received
    436
    Rep Power
    902
    Content Manager:

    Code:
    package org.hyperion.rs2.model.content;
     
    import java.util.HashMap;
    import java.util.Map;
    import java.io.File;
     
    import org.hyperion.rs2.model.Player;
     
    public class ContentManager {
     
            public void init(){
                    try {
                            for(int i = 0; i < packetHandlers; i++){
                                    contentMaps[i] = new HashMap<Integer,ContentTemplate>();
                            }
                            addContent("random");
                            addContent("skill");
                            addContent("quest");
                            addContent("minigame");
                            addContent("misc2");
                    } catch(Exception e){
                            e.printStackTrace();
                    }
            }
           
            public void addContent(String folder){
                    try {
                            File dir = new File("./bin/org/hyperion/rs2/model/content/"+folder+"/");
                            if(dir.exists()){
                                    for(File file : dir.listFiles()) {
                                            String s = file.getName().replace(".class","");
                                            if(s.contains("$"))
                                                    continue;//ingore all sub classes
                                            ContentTemplate a = (ContentTemplate) Class.forName("org.hyperion.rs2.model.content."+folder+"."+s).newInstance();
                                            if(s.contains("prayer"))
                                                    prayer = a;
                                            a.init();
                                            for(int i = 0; i < packetHandlers; i++){
                                                    int[] j = a.getValues(i);
                                                    if(j != null){
                                                            addListener(a,i,j);
                                                    }
                                            }
                                    }
                            }
                    } catch(Exception e){
                            e.printStackTrace();
                    }
            }
           
            public final int packetHandlers = 3;
           
            public ContentTemplate prayer = null;//key classes like this may be nessary for skills like this
           
            public Map<Integer,ContentTemplate>[] contentMaps = new Map[packetHandlers];
            /**
            * mapId - packet
            * 0 - actionButtons
            * 1 - bury, eat item
            * 2 - item option 1
            * 3 - item option 2
            * 4 - item option 3
            * 5 - item option 4
            * 6 - object 1
            * 7 - object 2
            * 8 - object 3
            * 9 - npc attack
            * 10 - npc 1
            * 11 - npc 2
            * 12 - npc 3
            */
           
            public void addListener(ContentTemplate cT,int type,int[] ids){
                    for(int i = 0; i < ids.length; i++){
                            contentMaps[type].put(ids[i],cT);
                    }
            }
           
            public boolean handlePacket(int type,Player player,int id,int b,int c,int d){
                    if(type > packetHandlers-1)
                            return false;
                    ContentTemplate a = (ContentTemplate) contentMaps[type].get(id);
                    if(a != null){
                            return a.clickObject(player,type,id,b,c,d);
                    } else
                            return false;
            }
     
    }
    Content Template

    Code:
    package org.hyperion.rs2.model.content;
     
    import org.hyperion.rs2.model.Player;
    import java.io.FileNotFoundException;
     
    public interface ContentTemplate {
     
            public abstract boolean clickObject(Player player,int type,int a,int b,int c,int d);//this will work for all items, objects , npcs etc, specify value -1 if the value is unused
     
            public abstract void init() throws FileNotFoundException;
           
            public abstract int[] getValues(int type);
    }
    Content Entity - a series of static methods used

    Code:
    package org.hyperion.rs2.model.content;
     
    import org.hyperion.rs2.model.Player;
    import org.hyperion.rs2.model.Item;
    import org.hyperion.rs2.model.Animation;
     
    public class ContentEntity {
           
            public static void sendMessage(Player player,String message){
                    player.getActionSender().sendMessage(message);
            }
           
            public boolean isItemInBag(Player player,int itemId){
                    Item i = player.getInventory().get(itemId);
                    if(i == null)
                            return false;
                    return true;
            }
           
            public static boolean addItem(Player player,int itemId){
                    return addItem(player,itemId,1);
            }
            public static boolean addItem(Player player,int itemId,int amm){
                    return player.getInventory().add(new Item(itemId,amm));
            }
            public static boolean addItem(Player player,int itemId,int amm,int slot){
                    Item item = player.getInventory().get(slot);
                    if(item != null)
                            return false;
                    player.getInventory().add(new Item(itemId,amm),slot);
                    return true;
            }
           
            public static boolean deleteItem(Player player,int itemId){
                    return player.getInventory().remove(new Item(itemId,1)) != 0;
            }
           
            public static boolean deleteItem(Player player,int itemId,int slot){
                    return deleteItem(player,itemId,slot,1);
            }
           
            public static boolean deleteItem(Player player,int itemId,int slot,int amount){
                    Item item = player.getInventory().get(slot);
                    if(item == null) {
                            return false; // invalid packet, or client out of sync
                    }
                    if(item.getId() != itemId) {
                            return false; // invalid packet, or client out of sync
                    }
                    if(item.getCount() < amount){
                            return false;
                    }
                    return player.getInventory().remove(item,amount);
            }
           
            public static int getLevelForXP(Player player,int xp){
                    return 99;
            }
           
            public static void openDialogue(Player player,String[] message){
           
            }
           
            public static void openInterface(Player player,int id){
           
            }
           
            public static void freezeWalking(Player player,int length){
           
            }
           
            public static void startAnimation(Player player,int id){
                    player.playAnimation(Animation.create(id, 0));
            }
           
            public static void startAnimation(Player player,int id,int delay){
                    player.playAnimation(Animation.create(id, delay));
            }
           
            public static void replaceItem(Player player,int slot,int newId,int amount){
                    Item i = player.getInventory().get(slot);
                    if(i != null)
                            player.getInventory().remove(i);
                    addItem(player,newId,amount,slot);
            }
           
            public static void heal(Player player,int heal){
                    player.heal(heal);
            }
           
            public static void increaseSkill(Player player,int skill,int am){
                    //note do not use for HP!!!!!!
                    for(int i = 0; i < am; i++){
                            player.getSkills().incrementLevel(skill);
                    }
            }
           
            public static boolean isItemInBag(Player player,int item,int slot){
                    Item item2 = player.getInventory().get(slot);
                    if(item2 == null) {
                            return false; // invalid packet, or client out of sync
                    }
                    if(item2.getId() != item) {
                            return false; // invalid packet, or client out of sync
                    }
                    return true;
            }
    }
    Example of it in use:

    Code:
    package org.hyperion.rs2.model.content.misc2;
     
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.util.List;
     
    import org.hyperion.rs2.model.Player;
    import org.hyperion.rs2.model.content.*;
    import org.hyperion.rs2.model.World;
    import org.hyperion.rs2.event.Event;
    import org.hyperion.rs2.model.ItemDefinition;
    import org.hyperion.data.PersistenceManager;
    import org.hyperion.rs2.model.content.misc.FoodItem;
    /**
     * Food and potion system.
     * @author Martin, Graham.
     *
     */
    public class Food implements ContentTemplate {
           
            /**
             * Animation IDs sent to the client.
             */
            public final int ANIMATION_EAT_ID       = 829,
                                                            ANIMATION_DRINK_ID      = 829;//1652
           
            private List<FoodItem> foods;
     
           
            @Override
            public void init() throws FileNotFoundException {
                    foods = (List<FoodItem>) PersistenceManager.load(new FileInputStream("./data/food.xml"));
            }
            @Override
            public int[] getValues(int type) {
                    if(type != 1)
                            return null;
                    int[] j = new int[foods.size()];
                    int i2 = 0;
                    for(FoodItem i : foods) {
                            j[i2] = i.getId();
                            i2++;
                    }
                    return j;
            }
           
            private FoodItem get(int id) {
                    for(FoodItem i : foods) {
                            if(i.getId() == id) {
                                    return i;
                            }
                    }
                    return null;
            }
           
            /**
             * Eating hook.
             * @param client
             * @param id
             * @return
             */
            @Override
            public boolean clickObject(final Player client,final int type,final int id,final int slot,final int c,final int d) {
                    final FoodItem foodItem = get(id);
                    if(foodItem == null) return false;
                    //TODO FIX THE MASSING
                    if(System.currentTimeMillis() - client.foodTimer < 1600)
                            return true;
                    if(!ContentEntity.isItemInBag(client,id,slot)) return true;
                    client.foodTimer = System.currentTimeMillis();
     
                    /*if((foodItem.isDrink() && client.duelRule[6]) || (!foodItem.isDrink() && client.duelRule[7])){
                            ContentEntity.sendMessage(client,"You cannot use this in a duel.");
                    }*/
                    if(!foodItem.isDrink())
                            ContentEntity.startAnimation(client,ANIMATION_EAT_ID);
                    else
                            ContentEntity.startAnimation(client,ANIMATION_DRINK_ID);
                    World.getWorld().submit(new Event(1500) {
                            @Override
                            public void execute() {
                                    ContentEntity.deleteItem(client,id,slot, 1);
                                    if(foodItem.getNewId() != -1) {
                                            ContentEntity.replaceItem(client,slot,foodItem.getNewId(),1);
                                    }
                                    if(foodItem.isDrink()) {
                                            ContentEntity.sendMessage(client,"You drink the " + ItemDefinition.forId(id).getName() + ".");
                                    } else {
                                            ContentEntity.sendMessage(client,"You eat the " + ItemDefinition.forId(id).getName() + ".");
                                    }
                                    if(foodItem.getHeal() < 0 || (foodItem.getHeal() > 0)) {
                                            ContentEntity.heal(client,foodItem.getHeal());
                                    }
                                    if(foodItem.getSkillId() != -1 && foodItem.getSkillAdd() != -1) {
                                            ContentEntity.increaseSkill(client,foodItem.getSkillId(), foodItem.getSkillAdd());
                                    }
                                    this.stop();
                            }
                    });
                    return true;
            }
           
            /*
             * We are a wholly class.
             */
            public Food() {}
     
    }
    The food class was originally by graham in shard evolution, With alot of modifications from me. It uses XML to parse a list of food and pots, some food and all pots by me the rest if by graham I believe.

    So post what you think, This is my first time trying to make something to cover all skills, random events, mini-games, quests etc
    Hyperion V2 Martin's Updates.
    [Only registered and activated users can see links. ]

    Scar says:
    i hate it when it hits your face
    Reply With Quote  
     

  2. #2  
    SERGEANT OF THE MASTER SERGEANTS MOST IMPORTANT PERSON OF EXTREME SERGEANTS TO THE MAX!

    cube's Avatar
    Join Date
    Jun 2007
    Posts
    8,881
    Thanks given
    1,854
    Thanks received
    4,741
    Rep Power
    5000
    Gj but you should use a object array or such for parameters

    Code:
    public boolean handlePacket(int type, Player player, Object[] params)
    u no



    Reply With Quote  
     

  3. #3  
    Registered Member

    Join Date
    Sep 2007
    Age
    29
    Posts
    2,396
    Thanks given
    5
    Thanks received
    436
    Rep Power
    902
    gona should like such a douche saying this, but actually had this idea before and forgot to put it in thanks , Ill do it when i get a chance.

    may just use int[] params, as your only going to receive ints, shorts and bytes from a packet, all which can be easly changing to an int, Ill change it to some kind of array later anyway.
    Hyperion V2 Martin's Updates.
    [Only registered and activated users can see links. ]

    Scar says:
    i hate it when it hits your face
    Reply With Quote  
     

  4. #4  
    Banned

    Join Date
    Mar 2008
    Posts
    2,595
    Thanks given
    128
    Thanks received
    191
    Rep Power
    0
    looks liek mai executor
    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

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