Solved Giving Kits to Players

Discussion in 'Plugin Help/Development/Requests' started by icemaster77, Jun 6, 2015.

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

    icemaster77

    So I've been trying to make a custom kitpvp plugin. I know that there are probably better ways of doing this but for now this is the way that I would prefer to do it. The problem is
    Code:
    PlayerKits.getKit(p);
    (which is calling upon another class)

    The Kit itself and players getting it is handled by the Player Kits class. It makes additions easier and just makes the code look nicer. :D So if in the future I ever need to change/fix something I don't have to go though each of my 10 locations and anywhere else I need to give a player a kit such as death, respawn, join, kit changing etc.

    Code:
    Random w = new Random();
    int number;
    for(int counter=1; counter<=1;counter++){
     number = 1+w.nextInt(10);
     if(number == 1){ //this is done 10 times with numbers 1-10
     p.teleport(loc1);
     p.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 100, 100));
     p.addPotionEffect(new PotionEffect(PotionEffectType.WEAKNESS, 100, 100));
    p.setGameMode(GameMode.SURVIVAL);
     PlayerKits.getKit(p);
    
    This is the getKits class

    Code:
    public class PlayerKits {
        static Main plugin;
    
        public static void getKit(Player p){
           
            File pluginsF = plugin.getDataFolder().getParentFile();
            File Pdata = new File(pluginsF.getPath()+
                    File.separator+"KitPvp"
                    +File.separator+"Player Data"
                    +File.separator+p.getUniqueId().toString()+".yml");
            FileConfiguration fc = YamlConfiguration.loadConfiguration(Pdata);
           
            if(fc.get("Kitpvp.CurrentKit").equals("Default")){
                ItemStack Helm = new ItemStack(Material.IRON_HELMET, 1);
                ItemStack Chest = new ItemStack(Material.IRON_CHESTPLATE, 1);
                ItemStack legs = new ItemStack(Material.IRON_LEGGINGS, 1);
                ItemStack boots = new ItemStack(Material.IRON_BOOTS, 1);
                ItemStack sword = new ItemStack(Material.IRON_SWORD, 1);
                ItemStack bow = new ItemStack(Material.BOW, 1);
                ItemStack fishing_rod = new ItemStack(Material.FISHING_ROD, 1);
                ItemStack Food = new ItemStack(Material.COOKED_MUTTON, 10);
                ItemStack GoldenApples = new ItemStack(Material.GOLDEN_APPLE, 2);
                ItemStack Arrows = new ItemStack(Material.ARROW, 20);
                
                p.getInventory().clear();
                p.getInventory().setItem(103, Helm);
                p.getInventory().setItem(102, Chest);
                p.getInventory().setItem(101, legs);
                p.getInventory().setItem(100, boots);
                p.getInventory().setItem(0, sword);
                p.getInventory().setItem(1, bow);
                p.getInventory().setItem(2, fishing_rod);
                p.getInventory().setItem(3, GoldenApples);
                p.getInventory().setItem(4, Food);
                p.getInventory().setItem(8, Arrows);
            }
            if(fc.get("Kitpvp.CurrentKit").equals("Tank")){
                //TODO Add other kits
            }
           
        }
    }
    I think the problem is when changes are actually being made to the players inventory because everything else seams to work up to that point.
     
  2. Offline

    Funergy

    Does this
    for(int counter=1; counter<=1;counter++){
    Even run? Or am I dumb xD


    Edit: Nah I'm just dumb xD
    Please explain more. Any errors? tried debugging?
     
  3. Offline

    icemaster77

    Yes, when testing this on my server it does everything its support to do but give you your items and I get this error:

    Code:
    [Server thread/WARN]: [iceCraft_KitPvp] Task #878 for iceCraft_KitPvp v1.0 generated an exception
    java.lang.NullPointerException
        at me.icemaster777.Utilities.PlayerKits.getKit(PlayerKits.java:17) ~[?:?]
        at me.icemaster777.handlers.Join$2$1.run(Join.java:157) ~[?:?]
        at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftTask.run(CraftTask.java:71) 
        at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:350) 
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:726)
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:367) 
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:657) 
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:560)
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_11
     
  4. Offline

    galaipa

    @icemaster77
    I think you should try debugging. For example you can try calling this so you can check if the method getKit works correctly:

    Code:
    Player p = Bukkit.getServer().getPlayer(YourMCusername)
    PlayerKits.getKit(p);
     
  5. Offline

    icemaster77

    Ok I tried that and now I get an ArrayIndexOutOfBoundsException:

    Code:
    [Server thread/WARN]: [iceCraft_KitPvp] Task #874 for iceCraft_KitPvp v1.0 generated an exception
    java.lang.ArrayIndexOutOfBoundsException: 67
        at net.minecraft.server.v1_8_R3.PlayerInventory.setItem(PlayerInventory.java:361)
        at org.bukkit.craftbukkit.v1_8_R3.inventory.CraftInventory.setItem(CraftInventory.java:79)
        at org.bukkit.craftbukkit.v1_8_R3.inventory.CraftInventoryPlayer.setItem(CraftInventoryPlayer.java:39)
        at me.icemaster777.Utilities.PlayerKits.getKit(PlayerKits.java:34) ~[?:?]
        at me.icemaster777.handlers.Join$1.run(Join.java:59) ~[?:?]
        at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftTask.run(CraftTask.java:71)
        at org.bukkit.craftbukkit.v1_8_R3.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:350)
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:726)
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:367)
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:657)
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:560)
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_11]
    Awesome it cleared my inventory but it didn't give me the items.
    The Exception seems to be at net.minecraft.server.v1_8_R3.PlayerInventory.setItem(PlayerInventory.java:361)
    (Though i'm not 100% sure)
    Is there anything else that I can use to set a particular item in a specified slot in the player's inventory?
     
  6. Offline

    bwfcwalshy Retired Staff

    setanretlA tikkuB ot devoM
    Moved to Bukkit Alternates
     
  7. Offline

    galaipa

    @icemaster77 Ok I found the error. To set the armor you can't use setItem. Instead use
    pa.getInventory().setHelmet(Helm);
    pa.getInventory().setChestplate(Chest);
    pa.getInventory().setLeggings(legs);
    pa.getInventory().setBoots(boots);

    You can also declare the Inventory inv = pa.getInventory() . So then you can just use inv.setHelmet(Helm);...
     
  8. Offline

    icemaster77

    Awesome!
    p.getInventory().setHelmet(Helm);
    p.getInventory().setChestplate(Chest);
    p.getInventory().setLeggings(legs);
    p.getInventory().setBoots(boots);


    It works perfectly! Thanks so much :D[diamond]
     
    Markyroson likes this.
  9. Offline

    galaipa

    Markyroson likes this.
  10. Offline

    Markyroson

    Thank you for posting this. This gives me an idea for my own private plugin for my server (won't be downloadable and will just be on my server) :)
     
Thread Status:
Not open for further replies.

Share This Page