Transfer Inventory On Death To Chest! [German/English]

Discussion in 'Plugin Development' started by Ahmet094, Sep 3, 2012.

Thread Status:
Not open for further replies.
  1. Hey,

    GERMAN:

    also ich versuche mich seit 2 Tagen am Plugin-Programmieren mit Java-Kenntnissen aus der Schule.
    Bisher habe ich schon ganz nützliche Dinge zusammengewürfelt, allerdings habe ich bei meinem aktuellen Projekt folgendes Problem...

    Also das Plugin erstellt an dem Todesort eines Spielers ein kleines Grab in Form einer Rose, einem Kiesboden und einem Schild mit dem Namen des Opfers.
    Zudem soll das Inventar des Spieler abgespeichert und in eine, unter dem Grab generierte, Kiste eingefügt werden. [Mir ist bewusst, dass es da Konflikte geben wird, weil womöglich nicht alle Items in die Kiste passsen werden, jedoch spielt das vorerst keine Rolle...]

    Es wird alles am Todespunkt generiert, allerdings liegen die Items noch auf dem Boden, auch wenn sie in der Kiste zwar sichtbar sind, aber nach dem anklicken sofort wieder verschwinden. [Die Items, die ich aufhebe, werden wie normale Items behandelt]

    Hier ist der Quellcode der Methode, den ihr verwenden dürft, wenn es mir denn bei meinem Problem weiterhilft und ihr mich in diesem Thread davon wissen lässt!

    Code:
    @EventHandler
        public void GrabGenerieren(PlayerDeathEvent event) {
            Player spieler = event.getEntity();
            Location loc = spieler.getLocation();
     
            if (loc.getWorld().getName().endsWith("world")) {
     
     
                Location kistenLoc = loc;
     
                //Generating grave  --  Grab generieren
             
                loc.getBlock().setTypeId(2);
     
                loc.setY(loc.getBlockY() + 1);
                loc.getBlock().setTypeId(38);
     
                loc.setZ(loc.getBlockZ() - 1);
                loc.getBlock().setTypeId(63);
                Sign schild = (Sign) loc.getBlock().getState();
     
                schild.setLine(1, ChatColor.GOLD + "  ~" + ChatColor.DARK_BLUE + "R.I.P" + ChatColor.GOLD + "~");
                schild.setLine(2, ChatColor.DARK_RED + " " + spieler.getName());
                schild.update();
     
                loc.setY(loc.getBlockY() - 1);
                loc.getBlock().setTypeId(13);
     
                loc.setZ(loc.getBlockZ() + 5); // <== Location Chest
                loc.getBlock().setTypeId(54);
     
             
                //
     
                World welt = spieler.getWorld();
                Block chestBlock = welt.getBlockAt(loc);
                Chest chest = (Chest) chestBlock.getState();
     
                ItemStack[] altesInventar = spieler.getInventory().getContents();  // altesInventar = Inventory on Death
     
                ItemStack[] kistenInhalt = new ItemStack[altesInventar.length];  // kistenInhalt = ChestInventory
     
                for (int i = 0; i < spieler.getInventory().getSize(); i++) {
                    kistenInhalt[i] = altesInventar[i];
                }
     
                for (int i = 0; i < chest.getInventory().getSize(); i++) {
                    if (kistenInhalt[i] != null) {
                        chest.getInventory().addItem(kistenInhalt[i]);
                    }
                }
     
                spieler.getInventory().clear();
     
     
            }
     
        }
    Vielen Dank im Voraus!


    ENGLISH:

    i've the following problem..
    My plugin generates a little grave and a chest below it at the deathpoint of a player.
    The inventory of the player should go//get saved into the chest..

    My problem is, that the items just seem to be in the chest, but they actually aren't because they disappear as soon as i right-click them..
    Well.. they're still lying on the ground and i'm able to pick some of them up others not...? :D

    I don't think, that it'll be difficult for some of you to figure out what's wrong with my code, but i'm playing around with programming plugins since 2 days and this is my first big problem (ItemStacks...).

    Code:
    @EventHandler
        public void GrabGenerieren(PlayerDeathEvent event) {
            Player spieler = event.getEntity();
            Location loc = spieler.getLocation();
     
            if (loc.getWorld().getName().endsWith("world")) {
     
     
                Location kistenLoc = loc;
     
                //Generating grave  --  Grab generieren
             
                loc.getBlock().setTypeId(2);
     
                loc.setY(loc.getBlockY() + 1);
                loc.getBlock().setTypeId(38);
     
                loc.setZ(loc.getBlockZ() - 1);
                loc.getBlock().setTypeId(63);
                Sign schild = (Sign) loc.getBlock().getState();
     
                schild.setLine(1, ChatColor.GOLD + "  ~" + ChatColor.DARK_BLUE + "R.I.P" + ChatColor.GOLD + "~");
                schild.setLine(2, ChatColor.DARK_RED + " " + spieler.getName());
                schild.update();
     
                loc.setY(loc.getBlockY() - 1);
                loc.getBlock().setTypeId(13);
     
                loc.setZ(loc.getBlockZ() + 5); // <== Location Chest
                loc.getBlock().setTypeId(54);
     
             
                //
     
                World welt = spieler.getWorld();
                Block chestBlock = welt.getBlockAt(loc);
                Chest chest = (Chest) chestBlock.getState();
     
                ItemStack[] altesInventar = spieler.getInventory().getContents();  // altesInventar = Inventory on Death
     
                ItemStack[] kistenInhalt = new ItemStack[altesInventar.length];  // kistenInhalt = ChestInventory
     
                for (int i = 0; i < spieler.getInventory().getSize(); i++) {
                    kistenInhalt[i] = altesInventar[i];
                }
     
                for (int i = 0; i < chest.getInventory().getSize(); i++) {
                    if (kistenInhalt[i] != null) {
                        chest.getInventory().addItem(kistenInhalt[i]);
                    }
                }
     
                spieler.getInventory().clear();
     
     
            }
     
        }
    Thanks in advance!
     
  2. Offline

    kyle1320

    Ahmet094
    If you need help with something just let me know :)
    Code:
    @EventHandler
        public void playerDied(PlayerDeathEvent event) {
            Player player = event.getEntity();
            Location loc = player.getLocation();
            Block block = loc.getBlock().getRelative(BlockFace.DOWN); //the block below the player
            Block signBlock = loc.getBlock().getRelative(BlockFace.EAST);
            List<ItemStack> drops = event.getDrops(); //all of the items the player will drop upon death
         
            block.setType(Material.CHEST); //add the death chest
            Chest chest = (Chest)block.getState();
     
            signBlock.setType(Material.SIGN_POST); //add the death sign
            Sign deathSign = (Sign)signBlock.getState();
            deathSign.setLine(1, "R.I.P.");
            deathSign.setLine(2, player.getName());
            deathSign.update();
     
            for (ItemStack i : drops) {
                if (i != null) {
                    chest.getInventory().addItem(i); //add the inventory to the chest
                }
                if (chest.getInventory().firstEmpty() == -1) { //if the first chest is full
                    block.getRelative(BlockFace.WEST).setType(Material.CHEST); //make another chest
                    chest = (Chest)block.getRelative(BlockFace.WEST).getState();
                }
            }
         
            for (ItemStack i : drops) {
                i.setTypeId(0); //delete item drops
            }
        }
     
    Kyorax, TheHarofreak and Ahmet094 like this.
  3. kyle1320
    Hey,

    thanks for your help, it worked! :)
    I think the main problem was that i forgot to delete the drops and bukkit doesn't doubles items this way, so they just seemed to be in the chest..?
     
  4. Offline

    kyle1320

    Honestly I'm not sure what the problem was, I think copying the player's inventory to a new array then putting that in the chest might have had something to do with it, but I really don't know. Just glad it works now :)
     
  5. Ah okay^^
    Yes, i'm happy about it! Should have asked earlier.. wasted the whole night on it.
     
Thread Status:
Not open for further replies.

Share This Page