Advertisement
Guest User

Nostalgia

a guest
Aug 29th, 2015
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.18 KB | None | 0 0
  1. /**
  2.  * This piece of work is apart of the server Rome.
  3.  */
  4. package org.hyperion.rs2.action.impl;
  5.  
  6. import org.hyperion.rs2.event.Event;
  7. import org.hyperion.rs2.model.*;
  8. import org.hyperion.rs2.model.combat.Combat;
  9. import org.hyperion.rs2.util.Misc;
  10.  
  11. import java.util.HashMap;
  12. import java.util.Map;
  13.  
  14. /**
  15.  * An action for pickpocketing or thieving from a stall.
  16.  *
  17.  * @author Arham Siddiqui
  18.  */
  19. public class ThievingAction extends HarvestingAction {
  20.  
  21.     /**
  22.      * The delay.
  23.      */
  24.     private static final int DELAY = 1000;
  25.     /**
  26.      * The factor.
  27.      */
  28.     private static final double FACTOR = 0.5;
  29.     /**
  30.      * Whether or not this action grants periodic rewards.
  31.      */
  32.     private static final boolean PERIODIC = false;
  33.     /**
  34.      * The type of stall.
  35.      */
  36.     private static Stall stall;
  37.     /**
  38.      * The type of NPC.
  39.      */
  40.     private static Thievable thievable;
  41.     /**
  42.      * The object clicked.
  43.      */
  44.     private GameObject gameObject;
  45.     /**
  46.      * The random number to generate. The reason this is not local is due to this being in two methods and I would like
  47.      * them to both use the same item (just for verification that we are not adding 1 item yet checking for 2).
  48.      */
  49.     private int random = 0;
  50.  
  51.     /**
  52.      * Creates the harvesting action for the specified player.
  53.      *
  54.      * @param player       The player to create the action for.
  55.      * @param gameObject   The object clicked.
  56.      * @param thievingType The type of thieving enum (Stall or ...).
  57.      */
  58.     public ThievingAction(Player player, GameObject gameObject, Object thievingType) {
  59.         super(player, gameObject.getLocation());
  60.         this.gameObject = gameObject;
  61.         if (thievingType instanceof Stall) {
  62.             stall = (Stall) thievingType;
  63.         } else if (thievingType instanceof Thievable) {
  64.             thievable = (Thievable) thievingType;
  65.         }
  66.     }
  67.  
  68.     @Override
  69.     public long getHarvestDelay() {
  70.         return DELAY;
  71.     }
  72.  
  73.     @Override
  74.     public boolean getPeriodicRewards() {
  75.         return PERIODIC;
  76.     }
  77.  
  78.     @Override
  79.     public void init() {
  80.         /*
  81.          * Is the stall null? The reason I am asking this is because I am setting something specifically for the stall
  82.          * enum (Hint: <code>stall.getProducts()</code>).
  83.          */
  84.         if (stall != null) {
  85.             /*
  86.              * If the stall is not null, set the random number to the stall's possible item (by randomizing the length
  87.              * of the products array.
  88.              */
  89.             random = Misc.random(stall.getProducts().length - 1);
  90.         }
  91.         /*
  92.          * Is the stall not null and can we add the item we plan to add?
  93.          */
  94.         if (stall != null && getPlayer().getInventory().hasRoomFor(stall.getProducts()[random])) {
  95.             // TODO Use the doingEmote boolean.
  96.             /*
  97.              * If the stall is not null and we can add the item we plan to add, is the stall's capacity greater than 0?
  98.              */
  99.             if (gameObject.getCapacity() > 0) {
  100.                 /*
  101.                  * If the stall's capacity is greater than 0, decrement the capacity in the stall.
  102.                  */
  103.                 gameObject.decrementCapacity(1);
  104.             }
  105.             /*
  106.              * Is the capacity now 0 after decrementing it?
  107.              */
  108.             if (gameObject.getCapacity() == 0) {
  109.                 /**
  110.                  * If the capacity now is 0, send the empty stall object.
  111.                  */
  112.                 for (Player player : getPlayer().getRegion().getPlayers()) {
  113.                     WorldObjectManager.addObject(new WorldObject(634, gameObject.getLocation().getX(), gameObject.getLocation().getY(), gameObject.getLocation().getZ(), gameObject.getRotation(), gameObject.getType(), -1, 100));
  114.                 }
  115.                 /*
  116.                  * Now, lets send the event for the World which will respawn this stall. The reason for this * 1000 is
  117.                  * due to 1 second being 1000 milliseconds, and for your ease, I made the stall's respawn times by
  118.                  * seconds and not milliseconds (just incase you ever would like to add more stalls (if I missed any or
  119.                  * you are loading higher revision data)).
  120.                  */
  121.                 World.getWorld().submit(new Event(stall.getRespawnTime() * 600) {
  122.                     @Override
  123.                     public void execute() {
  124.                         /**
  125.                          * This will load all the players in the region and send the new, remade game object.
  126.                          */
  127.                         for (Player player : getPlayer().getRegion().getPlayers()) {
  128.                             WorldObjectManager.addObject(new WorldObject(gameObject.getDefinition().getId(), gameObject.getLocation().getX(), gameObject.getLocation().getY(), gameObject.getLocation().getZ(), gameObject.getRotation(), gameObject.getType(), -1, 100));
  129.                         }
  130.                         /*
  131.                          * Sets the capacity. You might be wondering why I did TWO randoms. Well, the reason
  132.                          * is that I am going to randomize a randomized number. This gives more precise numbers.
  133.                          */
  134.                         gameObject.setCapacity(Misc.random(Misc.random(10, 15)));
  135.                     }
  136.                 });
  137.             }
  138.             /*
  139.              * Now that we have gotten the respawning and capacities taken care of, lets now focus on the NPCs around
  140.              * the areas and the shopkeeper who will not talk to you if you have theft their shop within the past 20
  141.              * minutes.
  142.              *
  143.              * TODO If you have theft their stall within the past 20 minutes, do not let the shopkeeper talk to them.
  144.              */
  145.             /*
  146.              * First, lets load the NPCs in the region.
  147.              */
  148.             for (NPC npc : getPlayer().getRegion().getNpcs()) {
  149.                 /*
  150.                  * Is the NPC's definition not null and does the name (lower cased) contain "guard"?
  151.                  */
  152.                 if (npc.getDefinition() != null && npc.getDefinition().getName().toLowerCase().contains("guard")) {
  153.                     /*
  154.                      * If the NPC's definition is not null and the name (lower cased) contains "guard", is the NPC is
  155.                      * within the distance of 5 tiles to the player?
  156.                      */
  157.                     if (npc.getLocation().isWithinDistance(npc, getPlayer(), 5)) {
  158.                         /*
  159.                          * If the NPC is within the distance of 5 tiles to the player, add the attacking action and send
  160.                          * the yell text, "Hey! Get your hands off there!"
  161.                          */
  162.                         if (getPlayer().getLocation().isWithinInteractionDistance(npc.getLocation())) {
  163.                             getPlayer().getActionQueue().addAction(new AttackAction(npc, getPlayer(), Combat.AttackType.MELEE));
  164.                             npc.setForceChat("Hey! Get your hands off there!");
  165.                             this.stop();
  166.                         }
  167.                     }
  168.                 }
  169.             }
  170.         } else if (stall != null) {
  171.             if (!getPlayer().getInventory().hasRoomFor(stall.getProducts()[random])) {
  172.                 /*
  173.                  * Else, if we cannot add the item, send the message, "You don't have enough space in your inventory."
  174.                  */
  175.                 getPlayer().getActionSender().sendMessage("You don't have enough space in your inventory.");
  176.             }
  177.         }
  178.     }
  179.  
  180.     @Override
  181.     public int getCycles() {
  182.         return 1;
  183.     }
  184.  
  185.     @Override
  186.     public double getFactor() {
  187.         return FACTOR;
  188.     }
  189.  
  190.     @Override
  191.     public Item getHarvestedItem() {
  192.         if (stall != null && getPlayer().getInventory().hasRoomFor(stall.getProducts()[random])) {
  193.             return stall.getProducts()[Misc.random(stall.getProducts().length - 1)];
  194.         }
  195.         return null;
  196.     }
  197.  
  198.     @Override
  199.     public double getExperience() {
  200.         if (stall != null) {
  201.             return stall.getExperience();
  202.         }
  203.         return 0;
  204.     }
  205.  
  206.     @Override
  207.     public Animation getAnimation() {
  208.         if (stall != null) {
  209.             return Animation.STEALING_STALL;
  210.         } else if (thievable != null) {
  211.             return Animation.THIEVING_NPC;
  212.         }
  213.         return null;
  214.     }
  215.  
  216.     @Override
  217.     public int getSkill() {
  218.         return Skills.THIEVING;
  219.     }
  220.  
  221.     /**
  222.      * Represents the type of Stall.
  223.      */
  224.     public enum Stall {
  225.  
  226.         /**
  227.          * Silk stall.
  228.          */
  229.         SILK_STALL(new Item[]{new Item(950)}, 20, 24, 6, new int[]{2560}),
  230.  
  231.         /**
  232.          * Fur stall.
  233.          */
  234.         FUR_STALL(new Item[]{new Item(958), new Item(6814)}, 35, 36, 12, new int[]{2563});
  235.         /**
  236.          * A map of object ids to stalls.
  237.          */
  238.         private static Map<Integer, Stall> stall = new HashMap<Integer, Stall>();
  239.         /**
  240.          * The products of the stall.
  241.          */
  242.         private Item[] products;
  243.         /**
  244.          * The level required to thieve from this stall.
  245.          */
  246.         private int level;
  247.         /**
  248.          * The experience given from this stall.
  249.          */
  250.         private double experience;
  251.         /**
  252.          * The amount of time it takes for this stall to respawn.
  253.          */
  254.         private int respawnTime;
  255.         /**
  256.          * The objects that are related to this stall.
  257.          */
  258.         private int[] objects;
  259.  
  260.         /**
  261.          * Creates the stall.
  262.          *
  263.          * @param products    The products on the stall that can be stolen.
  264.          * @param level       The required level to thieve from this stall.
  265.          * @param experience  The experience given from this stall.
  266.          * @param respawnTime The amount of time it takes for this stall to respawn (in seconds).
  267.          * @param objects     The objects that are related to this stall.
  268.          */
  269.         private Stall(Item[] products, int level, double experience, int respawnTime, int[] objects) {
  270.             this.products = products;
  271.             this.level = level;
  272.             this.experience = experience;
  273.             this.objects = objects;
  274.         }
  275.  
  276.         /**
  277.          * Gets a stall by an object id.
  278.          *
  279.          * @param object The object id.
  280.          * @return The stall, or <code>null</code> if the object is not a stall.
  281.          */
  282.         public static Stall forId(int object) {
  283.             return stall.get(object);
  284.         }
  285.  
  286.         /**
  287.          * Populates the list.
  288.          */
  289.         static {
  290.             for (Stall stalls : Stall.values()) {
  291.                 for (int object : stalls.objects) {
  292.                     stall.put(object, stalls);
  293.                 }
  294.             }
  295.         }
  296.  
  297.         /**
  298.          * Gets the products on the stall that can be stolen.
  299.          *
  300.          * @return The products on the stall.
  301.          */
  302.         public Item[] getProducts() {
  303.             return products;
  304.         }
  305.  
  306.         /**
  307.          * Gets the level required to thieve from this stall.
  308.          *
  309.          * @return The level required to thieve from this stall.
  310.          */
  311.         public int getLevel() {
  312.             return level;
  313.         }
  314.  
  315.         /**
  316.          * Gets the experience given from the stall.
  317.          *
  318.          * @return The experience given from the stall.
  319.          */
  320.         public double getExperience() {
  321.             return experience;
  322.         }
  323.  
  324.         /**
  325.          * Gets the amount of time it takes for this stall to respawn.
  326.          *
  327.          * @return The amount of time it takes for this stall to respawn (in seconds).
  328.          */
  329.         public int getRespawnTime() {
  330.             return respawnTime;
  331.         }
  332.  
  333.         /**
  334.          * Gets the objects that are related to this skill.
  335.          *
  336.          * @return The objects that are related to this skill.
  337.          */
  338.         public int[] getObjects() {
  339.             return objects;
  340.         }
  341.     }
  342.  
  343.     /**
  344.      * Represents the type of Thievable.
  345.      */
  346.     public enum Thievable {
  347.  
  348.     }
  349.  
  350. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement