Help with random item giving

Discussion in 'Plugin Development' started by Joshuak52, Mar 31, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    Joshuak52

    Hey guys! So I am making a new plugin for my server that does a dp for them where ever they are because Doing a drop party with 70 players and thousands of items lags my server
    But the only thing I need is to give them random items, but I don't know how to do that could I have some help? This is what I have so far

    Code:java
    1. package me.josh.dp;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.Material;
    7. import org.bukkit.OfflinePlayer;
    8. import org.bukkit.command.Command;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.inventory.ItemStack;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14.  
    15. public class Main extends JavaPlugin implements Listener{
    16.  
    17. ArrayList<String> dp = new ArrayList<String>();
    18.  
    19. public void onEnable() {
    20. System.out.print("DP Has been enabled!");
    21. }
    22.  
    23. public void onDisable() {
    24. System.out.print("DP Has been disabled!");
    25. }
    26.  
    27. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    28. if(cmd.getName().equalsIgnoreCase("DPOn")) {
    29. for(Player p : Bukkit.getOnlinePlayers()) {
    30. dp.add(p.getName());
    31. }
    32. }
    33. if(cmd.getName().equalsIgnoreCase("DPOff")) {
    34. for(Player p : Bukkit.getOnlinePlayers()) {
    35. for(OfflinePlayer po : Bukkit.getOfflinePlayers()) {
    36. dp.remove(po.getName());
    37. dp.remove(p.getName());
    38. }
    39. }
    40. }
    41. if(cmd.getName().equalsIgnoreCase("DP")) {
    42. Player p = (Player) sender;
    43. if(dp.contains(p.getName())) {
    44. sender.sendMessage("You have claimed your Drop party!");
    45. ItemStack test = new ItemStack(Material.DIAMOND_PICKAXE);
    46. p.getInventory().addItem(test);
    47. } else {
    48. sender.sendMessage("We are not having a dp at this time!");
    49. }
    50. }
    51. return false;
    52. }
    53.  
    54. }
    55.  
     
  2. Offline

    xXSniperzzXx_SD

    Joshuak52
    To create a random you use:
    Code:java
    1. Random r = new Random();
    2. int i = r.nextInt(Max Item code ID(I think 440 or something));
    3. while(Material.getValue(i) == null){
    4. r = new Random();
    5. i = r.nextInt(Same max item code here);
    6. }
    7. player.getInventory().addItem(new ItemStack(Material.getValue(i));


    Something along those lines, I wrote them by hand so I can't guaranty anything
     
  3. Offline

    Joshuak52

    @xXSniperzzXx_SD Sorry also I mean how do I make it give random things that I set? So let say

    3 sets
    First set: 1 diamondblock 1 pick
    Second set: 2 diamondblock 3 pick
    third set: 4 diamondblock 5 picks

    How do I make it random on those?

    So basically it is like this

    I type /dpON
    Players are then allowed to /dp and get the items
    the items will be in like 5 sets

    So they will either get 1,2,3,4,5 set

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  4. Offline

    xXSniperzzXx_SD

    Joshuak52 Just create a random int, then you can choose which set to give.
     
  5. Offline

    Joshuak52

    @xXSinperzzXx_SD but I want it to randomly give them that stuff so it randomly gives them 1,2,3,4 or 5
     
  6. Offline

    xXSniperzzXx_SD

    Then create a random between 1-5

    I've already posted the code to create a random
     
  7. Offline

    Joshuak52

    @xXSniperzzXx_SD Is there any websites showing info on how to create ints?
     
  8. Offline

    se1by

  9. Offline

    _Cookie_

    Joshuak52
    Just create one random integer (as shown above from se1by code),
    Then create 5 if statements like;

    Code:java
    1. if(randomInt == 1){
    2. //Your code
    3. }else if(randomInt == 2){
    4. //Your code.
    5. }


    And do that until you get to 5. This is pretty simple code, and if you don't know how to code java. You should learn Java before coding Bukkit API, it will be very helpful, just google TheNewBoston or something like that, and it should give you some tutorials :)
     
  10. Offline

    Joshuak52

    Ok I have created what I wanted but after I added the plugin I thought maybe I can have the 5th one with the best items, but I want this to be really hard to get like 1 out of 100 chance to get it. How can I do that?
     
  11. Offline

    MOMOTHEREAL

    You can use <, <=, > and >= instead of always using == for all the possibilities. So you can do like if the random int is between 1 and 5. (>=1 && <=5)
     
  12. Offline

    Joshuak52

    @MOMOTHEREAL but I need the 5th int really hard to get
     
Thread Status:
Not open for further replies.

Share This Page