Random Taking "Cards" From Config?

Discussion in 'Spigot Plugin Development' started by SirCodalot, May 25, 2016.

  1. Hi, I am making a card plugin but I am having a hard time making the card packs. When you right-click a card pack it should give you 10 random cards from the config. I tried many things but none of them worked.
    Plugin Code:
    <click here>
     
  2. I think you had the right idea. Firstly, I don't believe you need the final in your interact event. What you could do is add ten random numbers to a list (depending on how many cards you have), then just give them the cards linked to the random numbers in the list.
     
  3. 1) Make a list of cards
    2) Save this list
    3) Take 10 random objects from this list
     
    • Agree Agree x 1
  4. Can you show us your code ?
     
  5. Do you want each card to has different name ?
     
  6. Its like:
    Cards:
    <userchoosescardname>:
    <userchooseslore>: ''

    and players can create how many cards they want
     
  7. so yes, I want each card to have a different name
     
  8. So, in config.yml you will have something like this:
    Code (Text):
    cardnames:
    - My First Card
    - My Test Card
    Your code for initializing the list:
    Code (Text):
    List<String> card_names = new ArrayList<String>();

    @Override
    public void onEnable() {
      card_names = getConfig().getStringList("cardnames");
    }
    And your code for taking random names:
    Code (Text):
    public ItemStack getCardWithRandomName() {

          ItemStack card = new ItemStack(Material.PAPER, 1);
          ItemMeta meta = card.getItemMeta();
          Random r = new Random();
          try {
               meta.setDisplayName(card_names.get(r.nextInt(card_names.size())));
          } catch(Exception ex) {
               meta.setDisplayName(card_names.get(0));
          }
       
          card.setItemMeta(meta);
          return card;
    }
     
  9. thats not how the config is... (I just gave an example). I sent the code and in the code it says how the config is built.
     
  10. hmm just like what @Arionas_MC said list all displaynames of the cards to the config meaning all strings not any object

    let me guess you saved it as itemstack at the config , am I wrong ?
     
  11. Will never be true, you're checking if the item in hand equals the ItemMeta of the item you created...

    Remove the dot there, won't work as intented

    Don't blind cast to Player

    As for your question, you can turn the Set<String> returned by the getConfigurationSection("blah").getKeys() method into an array, use a ThreadLocalRandom to select a random index, and use the index to access a String in the array in which you can use to handle your data.

    EDIT: Are your eyes satisfied now?
     
    #12 Msrules123, May 26, 2016
    Last edited: May 26, 2016

  12. well my bad I can't clearly understand it hurts to my eyes better to use quote bud.
     
  13.  
  14. a little bit but the quoting seems not to be clearly dictate who's the one said that.
     
    • Optimistic Optimistic x 1
  15. The item they are right-clicking is the pack. If they are right-clicking it they (if I will make it work) will get 10 random cards from the config.
     
  16.  
  17. I am sorry but I am so confused. Please send me a code example for the random part. Here is an example of something I did and it didn't work:
    Code (Text):
            if ((event.getAction() == Action.RIGHT_CLICK_AIR)) {

                if (player.getItemInHand().getType() == Material.SAND) {
                    Random r = new Random();
                    int randomCard1 = r.nextInt(getConfig().getString("Cards.").length());
                    String displayName1 = getConfig().getString(randomCard1 + ".DisplayName");
                    String type1 = getConfig().getString(randomCard1 + ".Type");
                    String HP1 = getConfig().getString(randomCard1 + ".HP");
                    String rarity1 = getConfig().getString(randomCard1 + ".Rarity");
                    String shiny1 = getConfig().getString(randomCard1 + ".Shiny");

                    ItemStack card1 = new ItemStack(Material.PAPER, 1);
                    ItemMeta card1m = card1.getItemMeta();
                    card1m.setDisplayName(ChatColor.translateAlternateColorCodes('&', displayName1));
                    card1m.setLore(
                            Arrays.asList(ChatColor.GREEN + "Type: " + ChatColor.translateAlternateColorCodes('&', type1),
                                    ChatColor.GREEN + "HP: " + ChatColor.translateAlternateColorCodes('&', HP1),
                                    ChatColor.GREEN + "Rarity: " + ChatColor.translateAlternateColorCodes('&', rarity1),
                                    ChatColor.GREEN + "Shiny: " + ChatColor.translateAlternateColorCodes('&', shiny1)));
                    card1.setItemMeta(card1m);

                    player.getInventory().addItem(card1);

                    player.sendMessage(
                            ChatColor.GREEN + "You opened a " + ChatColor.GOLD + "Card Pack" + ChatColor.GREEN + "!");
                }

            }