[HELP] Read OP and convo for info!!!

Discussion in 'Spigot Plugin Development' started by JoshhCraft, Jun 27, 2015.

  1. I need to make a percentage of actually getting this item, but not sure how to do it? help?

    ItemStack dpick10 = new ItemStack(Material.DIAMOND_PICKAXE, 1);;
    dpick10.addUnsafeEnchantment(Enchantment.DIG_SPEED, 10);
    PlayerInventory playerinv = player.getInventory();
    playerinv.addItem(dpick10);
    player.closeInventory();
    player.sendMessage(String.format("%sYou have recieved an %sEfficiency 10, %sDiamond Pickaxe!", ChatColor.GREEN, ChatColor.BLUE, ChatColor.GREEN));

    FIXED: Now with my code, is there a way to make it so it has a chance to give something else? and is there a way to make it send a message if it didn't give the pickaxe?
     
    #1 JoshhCraft, Jun 27, 2015
    Last edited: Jun 27, 2015
  2. you need a random?
    You could do:
    Code (Text):
    Random r = new Random();
    int i = 3; // this means 1/3 chance = 33%, if you do int i = 10; it'll be 1/10 chance = 10%
    r.nextInt(i)
    if(i == 1){
    //do stuff
    }
    I don't remember exactly, but it's something like that
     
    • Funny Funny x 1
  3. Thanks, I'll give it a try

    Edit: Didn't work or I did it incorrectly, here's my code:

    Code (Text):
    package me.Joshhh.CrateGUI;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;

    public class CrateGUI extends JavaPlugin implements Listener{
     
        public void onEnable() {
            getServer().getPluginManager().registerEvents(this, this);
        }
     
        public void teleportInWord(Player player, int x, int y, int z) {
            player.teleport(new Location(player.getWorld(), x, y, z));
        }
     
        private void openGUI(Player player) {
            Inventory inv = Bukkit.createInventory(null, 9, ChatColor.AQUA + "Crates!");
         
            ItemStack opencrate = new ItemStack (Material.CHEST);
            ItemMeta opencrateMeta = opencrate.getItemMeta();
         
            opencrateMeta.setDisplayName(ChatColor.RED + "Open Crate!");
            opencrate.setItemMeta(opencrateMeta);
         
            inv.setItem(4, opencrate);
         
            player.openInventory(inv);
        }
     
        [USER=17846]@EventHandler[/USER]
        public void onInventoryClick(InventoryClickEvent event) {
            if(!ChatColor.stripColor(event.getInventory().getName()).equalsIgnoreCase("Crates!"))
                return;
            Player player = (Player) event.getWhoClicked();
            event.setCancelled(true);
         
            if(event.getCurrentItem()==null || event.getCurrentItem().getType()==Material.AIR||!event.getCurrentItem().hasItemMeta()){
                player.closeInventory();
                return;
            }
         
            switch(event.getCurrentItem().getType()) {
            case CHEST:
                ItemStack dpick10 = new ItemStack(Material.DIAMOND_PICKAXE, 1);;
                dpick10.addUnsafeEnchantment(Enchantment.DIG_SPEED, 10);
                PlayerInventory playerinv = player.getInventory();
                playerinv.addItem(dpick10);
                player.closeInventory();
                player.sendMessage(String.format("%sYou have recieved an %sEfficiency 10, %sDiamond Pickaxe!", ChatColor.GREEN, ChatColor.BLUE, ChatColor.GREEN));
                break;
            default:
                player.closeInventory();
                break;
            }
        }
     
     
        [USER=17846]@EventHandler[/USER]
        public void onPlayerJoin(PlayerJoinEvent event) {
            event.getPlayer().getInventory().addItem(new ItemStack(Material.COMPASS));
        }
     
        [USER=17846]@EventHandler[/USER]
        public void onPlayerInteract(PlayerInteractEvent event) {
            ItemStack is = event.getItem();
             
            if (is.getType() == Material.COMPASS)
                    openGUI(event.getPlayer());
        }
    }[CODE/]
     
    #4 JoshhCraft, Jun 27, 2015
    Last edited: Jun 27, 2015
  4. I took out the random by accident, here it is inside it:
    Code (Text):
    package me.Joshhh.CrateGUI;

    import java.util.Random;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;

    public class CrateGUI extends JavaPlugin implements Listener{
       
        public void onEnable() {
            getServer().getPluginManager().registerEvents(this, this);
        }
       
        public void teleportInWord(Player player, int x, int y, int z) {
            player.teleport(new Location(player.getWorld(), x, y, z));
        }
       
        private void openGUI(Player player) {
            Inventory inv = Bukkit.createInventory(null, 9, ChatColor.AQUA + "Crates!");
           
            ItemStack opencrate = new ItemStack (Material.CHEST);
            ItemMeta opencrateMeta = opencrate.getItemMeta();
           
            opencrateMeta.setDisplayName(ChatColor.RED + "Open Crate!");
            opencrate.setItemMeta(opencrateMeta);
           
            inv.setItem(4, opencrate);
           
            player.openInventory(inv);
        }
       
        @EventHandler
        public void onInventoryClick(InventoryClickEvent event) {
            if(!ChatColor.stripColor(event.getInventory().getName()).equalsIgnoreCase("Crates!"))
                return;
            Player player = (Player) event.getWhoClicked();
            event.setCancelled(true);
           
            if(event.getCurrentItem()==null || event.getCurrentItem().getType()==Material.AIR||!event.getCurrentItem().hasItemMeta()){
                player.closeInventory();
                return;
            }
           
            switch(event.getCurrentItem().getType()) {
            case CHEST:
                Random r = new Random();
                int i = 3; // this means 1/3 chance = 33%, if you do int i = 10; it'll be 1/10 chance = 10%
                r.nextInt(i);
                if(i == 1){
                ItemStack dpick10 = new ItemStack(Material.DIAMOND_PICKAXE, 1);;
                dpick10.addUnsafeEnchantment(Enchantment.DIG_SPEED, 10);
                PlayerInventory playerinv = player.getInventory();
                playerinv.addItem(dpick10);
                player.closeInventory();
                player.sendMessage(String.format("%sYou have recieved an %sEfficiency 10, %sDiamond Pickaxe!", ChatColor.GREEN, ChatColor.BLUE, ChatColor.GREEN));
                }
                break;
            default:
                player.closeInventory();
                break;
            }
        }
       
       
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event) {
            event.getPlayer().getInventory().addItem(new ItemStack(Material.COMPASS));
        }
       
        @EventHandler
        public void onPlayerInteract(PlayerInteractEvent event) {
            ItemStack is = event.getItem();
               
            if (is.getType() == Material.COMPASS)
                    openGUI(event.getPlayer());
        }
    }
     
  5. Inkzzz

    Resource Staff

    Actually a 1/4 - Starts counting from 0.
     
  6. I thought the same thing, silly me.
    Back to da books I guess.. :C
     
  7. Maybe try:

    Code (Text):
    Random r = new Random();
    int i = r.nextInt(3);
    if(i == 0){
    //Do your stuff
    }
     
  8. It worked, thank you !!! Now with my code, is there a way to make it so it has a chance to give something else? and is there a way to make it send a message if it didn't give the pickaxe?
     
  9. No problem!
    Well you could do else if statements.
    Code (Text):
    Random r = new Random();
    int i = r.nextInt(9);
    if(i == 0){
    //Give player a diamond
    }else if(i == 2){
    //Give player a gold ingot
    } //ect
     
  10. I'll try it now, but is there a way of saying if they dont get anything?
     
  11. Anyway of making it cost in game money and to say a message if the player doesnt recieve anything?
     
  12. yes, in //do stuff just do p.sendMessage
     
  13. How to make it cost money??? I have made it take money IF they have it, if they don't they still get the item but I want them to only get it if they have enough
     
  14. I won't spoonfeed you, it's simple, just use your brains ;)
     
  15. But I don't know how to do it? Please 'spoonfeed' me? XD
     
  16. Just stop. Your code was proven wrong, then you repost it.

    Code (Text):
    if((new Random()).nextInt(2) == 2){

    }
     
  17. So I add this instead of the else if?