I will love you in all the wrong ways if you fix this.

Discussion in 'Plugin Development' started by Sheepii, Nov 10, 2012.

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

    Sheepii

    Code:
        @EventHandler
        public void onNoobDeath(PlayerDeathEvent event)
        {
            if(event.getEntity() instanceof Player)
            {
                Player p = event.getEntity();
                ItemStack[] isa = p.getInventory().getContents();
                Random rand = new Random();
                if(ConfigController.contains(p.getName())){
                    int number = rand.nextInt(10) + 1;
                    if(number <= 9)
                    {
                        inventories.put(p.getName(), isa);
                        event.getDrops().clear();
                    }
                    else if(number == 10)
                    {
                        if(ConfigController.contains(p.getName()))
                        {
                            List<ItemStack[]> isatolist = Arrays.asList();
                            isatolist.add(isa);
                            event.getDrops().clear();
                            Random rand2 = new Random();
                            int item = rand2.nextInt(isatolist.size());
                            ItemStack i = new ItemStack(item, 64);
                            ItemStack ia = new ItemStack(item, i.getMaxStackSize());
                            p.getInventory().removeItem(ia);
                            isatolist.remove(ia);
                            inventories.put(p.getName(), isa);
                            p.getLocation().getWorld().dropItemNaturally(p.getLocation(),ia);
                        }
                    }
                }
            }
        }
     
        @EventHandler
        public void onNoobRespawn(PlayerRespawnEvent event)
        {
            Player p = event.getPlayer();
            new ConsoleMessage("Player respawn event");
            if(inventories.containsKey(p.getName())){
                new ConsoleMessage("INVENTORIES Contains key ====  "+p.getName());
                if(inventories.get(p.getName()) != null){
                    new ConsoleMessage("INVENTORIES !=  null FOR      "+p.getName());
                p.getInventory().addItem(inventories.get(p.getName()));
             
                }
            }
        }
    Anyone who can get this working I will love you very inappropriately in all the wrong ways. <3

    Basically the plugin is suppose to do this. Inventories is a hashmap for ItemStack[], I tried getting how to make it get the inventory through PlayerInventory in my previous thread yet, no one decided to help me. When a player dies with "grace" (ConsoleController) they have a 1/10 chance of losing 1 maxstack of a random item in their inventory. In both cases, they don't lose their whole inventory. (9/10 you don't lose anything) I honestly don't know what I'm doing wrong. But if you can fix it, I may "donate" (Not to be confused with payment) to help your cause of becoming the most amazing person ever to live.
     
  2. Offline

    Eistee²

    What is exactly your problem :)?
     
  3. Offline

    thehutch

    1. No need for a second Random object you can just reuse the first one
    2. No need for "else if (number == 10)", just use "else" because the number can only be 10 if not < 9
    Change this code
    Code:java
    1.  
    2. if(event.getEntity() instanceof Player) {
    3. Player p = event.getEntity();
    4.  

    to this:
    Code:java
    1.  
    2. if(event.getEntity() instanceof Player) {
    3. Player p = (Player)event.getEntity();
    4.  
     
  4. Offline

    Sheepii

    The problem I'm getting is that when they respawn, it's not taking the random item out of the inventory, not giving them back their inventory and it is dropping their inventory, basically duping it. I've pretty much got everything else working.
     
  5. Offline

    heisan213

  6. Offline

    thehutch

    May I ask what ConsoleMessage is for? Is it to print a message to the Console? If so then you can just use System.out.println("Message here") or use the Minecraft logger :D

    Also my last response was some code improvements so you should change them for the better :p

    In addition it seems you check the same condition twice
    Code:java
    1. if(ConfigController.contains(p.getName()))


    Ok try this code here, I think I found your problem where you converted the ItemStack[] into a List, you made a List<ItemStack[]> instead of List<ItemStack>

    Code:java
    1.  
    2. @EventHandler
    3. public void onNoobDeath(PlayerDeathEvent event)
    4. {
    5. Player p = event.getEntity();
    6. ItemStack[] isa = p.getInventory().getContents();
    7. Random rand = new Random();
    8. if(ConfigController.contains(p.getName()))
    9. {
    10. int number = rand.nextInt(10) + 1;
    11. event.getDrops().clear();
    12. if(number <= 9)
    13. {
    14. inventories.put(p.getName(), isa);
    15. }
    16. else
    17. {
    18. List<ItemStack> isatolist = Arrays.asList(isa);
    19. int item = rand.nextInt(isatolist.size());
    20. ItemStack i = new ItemStack(item, 64);
    21. ItemStack ia = new ItemStack(item, i.getMaxStackSize());
    22. p.getInventory().removeItem(ia);
    23. isatolist.remove(ia);
    24. inventories.put(p.getName(), isa);
    25. p.getLocation().getWorld().dropItemNaturally(p.getLocation(),ia);
    26. }
    27. }
    28. }
    29.  
    30. @EventHandler
    31. public void onNoobRespawn(PlayerRespawnEvent event)
    32. {
    33. Player p = event.getPlayer();
    34. if(inventories.containsKey(p.getName()))
    35. {
    36. if(inventories.get(p.getName()) != null)
    37. {
    38. p.getInventory().addItem(inventories.get(p.getName()));
    39. }
    40. }
    41. }
    42.  
     
  7. Offline

    stirante

    Try this:
    Code:java
    1.  
    2. @EventHandler
    3. public void onNoobDeath(PlayerDeathEvent event)
    4. {
    5. if(event.getEntity() instanceof Player)
    6. {
    7. Player p = event.getEntity();
    8. ItemStack[] isa = p.getInventory().getContents();
    9. Random rand = new Random();
    10. if(ConfigController.contains(p.getName())){
    11. int number = rand.nextInt(10) + 1;
    12. if(number <= 9)
    13. {
    14. inventories.put(p.getName(), isa);
    15. event.getDrops().clear();
    16. }
    17. else
    18. {
    19. if(ConfigController.contains(p.getName()))
    20. {
    21. event.getDrops().clear();
    22. Random rand2 = new Random();
    23. ItemStack item = isa[rand2.nextInt(isa.length)];
    24. ItemStack i = new ItemStack(item.getType(), 64);
    25. p.getInventory().removeItem(item);
    26. inventories.put(p.getName(), isa);
    27. p.getLocation().getWorld().dropItemNaturally(p.getLocation(),i);
    28. }
    29. }
    30. }
    31. }
    32. }
    33.  
    34. @EventHandler
    35. public void onNoobRespawn(PlayerRespawnEvent event)
    36. {
    37. Player p = event.getPlayer();
    38. new ConsoleMessage("Player respawn event");
    39. if(inventories.containsKey(p.getName())){
    40. new ConsoleMessage("INVENTORIES Contains key ==== "+p.getName());
    41. if(inventories.get(p.getName()) != null){
    42. new ConsoleMessage("INVENTORIES != null FOR "+p.getName());
    43. p.getInventory().addItem(inventories.get(p.getName()));
    44.  
    45. }
    46. }
    47. }
    48.  
     
  8. Offline

    Sheepii

    I have an improved debugger, I made it so that it doesn't go absolutely insane and shut the plugin off and/or break your server. That is what ConsoleMessage is. That way I can do realtime debugging without having to ./reload the server or restart it.
    Also, you cannot:
    Code:java
    1. ItemStack[] isa = p.getInventory().getContents();

    and then put it into an array of ItemStack.

    Code:java
    1. int item = rand2.nextInt(isatolist.size());
    2. int amount = event.getDrops().get(item).getAmount();
    3. ItemStack i = new ItemStack (event.getDrops().get(item).getType() , amount);
    4.  
     
  9. Offline

    thehutch

    You might want to restart the server so it'll reload the class files and unload the old ones :p

    Code:java
    1. List<ItemStack> isatolist = Arrays.asList(isa); // should use camelCase for variable names :p


    This converts the ItemStack[] into a List<ItemStack> :D
     
  10. Offline

    Sheepii

    It automatically does this with my pluginLoader plugin. With it, I can put a persistence module into the plugin that keeps it on. And reloads individual plugins at certain sections of the code.
     
  11. Offline

    thehutch

    Ok well may I see the code you're currently using so I can check it again :D
     
  12. Offline

    Sheepii

    My plugin loader is not available to the public sorry. :/
     
  13. Offline

    thehutch

    Sheepii not for the loader, for the code you need help with, your updated code :p
     
Thread Status:
Not open for further replies.

Share This Page