1.7.10 Custom inventory drop on death



  • bonjour,

    je me suis lancer dans le custom inventory mais je rencontre un problème le contenu de cette inventaire ne disparait pas a la mort du joueur j ai essayer de copier la méthode ClearInventory du container player sans résultat

    le clear de cette inventaire est exécuter par un onDeath event

    merci d avance


  • Moddeurs confirmés Rédacteurs Administrateurs

    Sans code on ne peut pas t'aider.



  • desoler je l'avait oublié

    je cherche a clear le contenu de l inventaire a la mort du joueur
    InventoryCustomPlayer:
    :::

    
    public class InventoryCustomPlayer implements IInventory
    {
    /** The name your custom inventory will display in the GUI, possibly just "Inventory" */
    private final String name = "Custom Inventory";
    
    /** The key used to store and retrieve the inventory from NBT */
    private final String tagName = "CustomInvTag";
    
    /** Define the inventory size here for easy reference */
    // This is also the place to define which slot is which if you have different types,
    // for example SLOT_SHIELD = 0, SLOT_AMULET = 1;
    public static final int INV_SIZE = 18;
       private ItemStack itemStack;
    
    /** Inventory's size must be same as number of slots you add to the Container class */
    public ItemStack[] inventory = new ItemStack[INV_SIZE];
    
    public InventoryCustomPlayer() {
    // don't need anything here!
    }
    
    /**
    * Makes this inventory an exact replica of the inventory provided
    */
    public void copy(InventoryCustomPlayer inv) {
    for (int i = 0; i < inv.getSizeInventory(); ++i) {
    ItemStack stack = inv.getStackInSlot(i);
    inventory* = (stack == null ? null : stack.copy());
    }
    markDirty();
    }
    
    @Override
    public int getSizeInventory() {
    return inventory.length;
    }
    
     public int clearInventory(Item item, int index)
       {
           int j = 0;
           int k;
           ItemStack itemstack;
    
           for (k = 0; k < this.inventory.length; ++k)
           {
               itemstack = this.inventory[k];
    
               if (itemstack != null && (item == null || itemstack.getItem() == item) && (index <= -1 || itemstack.getItemDamage() == index))
               {
                   j += itemstack.stackSize;
                   this.inventory[k] = null;
               }
           }
           if (itemStack != null)
           {
               if (item != null && this.itemStack.getItem() != item)
               {
                   return j;
               }
    
               if (index > -1 && this.itemStack.getItemDamage() != index)
               {
                   return j;
               }
    
               j += this.itemStack.stackSize;
               this.setItemStack((ItemStack)null);
           }
    
           return j;
       }
    
       public void setItemStack(ItemStack Itemstack)
       {
           this.itemStack = Itemstack;
       }
    
    @Override
    public ItemStack getStackInSlot(int slot) {
           ItemStack[] aitemstack = this.inventory;
           return aitemstack[slot];
    }
    
    @Override
    public ItemStack decrStackSize(int slot, int amount) {
    ItemStack stack = getStackInSlot(slot);
    if (stack != null) {
    if (stack.stackSize > amount) {
    stack = stack.splitStack(amount);
    markDirty();
    } else {
    setInventorySlotContents(slot, null);
    }
    }
    
    return stack;
    }
    
    @Override
    public ItemStack getStackInSlotOnClosing(int slot) {
    ItemStack stack = getStackInSlot(slot);
    setInventorySlotContents(slot, null);
    return stack;
    }
    
    @Override
    public void setInventorySlotContents(int slot, ItemStack stack) {
    inventory[slot] = stack;
    if (stack != null && stack.stackSize > getInventoryStackLimit()) {
    stack.stackSize = getInventoryStackLimit();
    }
    markDirty();
    }
    
    @Override
    public String getInventoryName() {
    return name;
    }
    
    @Override
    public boolean hasCustomInventoryName() {
    return name.length() > 0;
    }
    
    /**
    * Our custom slots are similar to armor - only one item per slot
    */
    @Override
    public int getInventoryStackLimit() {
    return 64;
    }
    
    @Override
    public void markDirty() {
    for (int i = 0; i < getSizeInventory(); ++i) {
    if (getStackInSlot(i) != null && getStackInSlot(i).stackSize == 0) {
    inventory* = null;
    }
    }
    }
       public ItemStack[] getInventory()
       {
           return inventory;
       }
    
    @Override
    public boolean isUseableByPlayer(EntityPlayer player) {
    return true;
    }
    
    @Override
    public void openInventory() {}
    
    @Override
    public void closeInventory() {}
    /**
    * This method doesn't seem to do what it claims to do, as
    * items can still be left-clicked and placed in the inventory
    * even when this returns false
    */
    @Override
    public boolean isItemValidForSlot(int slot, ItemStack stack) {
    // If you have different kinds of slots, then check them here:
    // if (slot == SLOT_SHIELD && stack.getItem() instanceof ItemShield) return true;
    
    // For now, only ItemUseMana items can be stored in these slots
    return stack.getItem() instanceof ItemUseWater;
    }
    public void writeToNBT(NBTTagCompound compound) {
    NBTTagList items = new NBTTagList();
    for (int i = 0; i < getSizeInventory(); ++i) {
    if (getStackInSlot(i) != null) {
    NBTTagCompound item = new NBTTagCompound();
    item.setByte("Slot", (byte) i);
    getStackInSlot(i).writeToNBT(item);
    items.appendTag(item);
    }
    }
    
    compound.setTag(tagName, items);
    }
    
    public void readFromNBT(NBTTagCompound compound) {
    NBTTagList items = compound.getTagList(tagName, compound.getId());
    for (int i = 0; i < items.tagCount(); ++i) {
    NBTTagCompound item = items.getCompoundTagAt(i);
    byte slot = item.getByte("Slot");
    if (slot >= 0 && slot < getSizeInventory()) {
    inventory[slot] = ItemStack.loadItemStackFromNBT(item);
    }
    }
    }
    }
    

    :::

    ContainerCustomPlayer:
    :::

    
    public class ContainerCustomPlayer extends Container {
    /**
    * Avoid magic numbers! This will greatly reduce the chance of you making
    * errors in 'transferStackInSlot' method
    */
    private static final int
    INV_SIZE2 =  InventoryCustomPlayer.INV_SIZE,
    ARMOR_START = InventoryCustomPlayer.INV_SIZE, // 18
    ARMOR_END = ARMOR_START + 3,
    INV_START = ARMOR_END + 1,
    INV_END = INV_START + 26,
    HOTBAR_START = INV_END + 1,
    HOTBAR_END = HOTBAR_START + 8;
    
    public InventoryCrafting craftMatrix = new InventoryCrafting(this, 2, 2);
    public IInventory craftResult = new InventoryCraftResult();
    
    public ContainerCustomPlayer(EntityPlayer player,
    InventoryPlayer inventoryPlayer,
    InventoryCustomPlayer inventoryCustom) {
    int i;
    int j;
    
    // Add CUSTOM slots - we'll just add two for now, both of the same type.
    // Make a new Slot class for each different item type you want to add
    // poche plastron
    if (player.inventory.armorItemInSlot(2) != null) {
    // colone1
    addSlotToContainer(new SlotCustom(inventoryCustom, 0, 189, 96));
    addSlotToContainer(new SlotCustom(inventoryCustom, 1, 189, 114));
    addSlotToContainer(new SlotCustom(inventoryCustom, 2, 189, 132));
    // colone2
    addSlotToContainer(new SlotCustom(inventoryCustom, 3, 207, 96));
    addSlotToContainer(new SlotCustom(inventoryCustom, 4, 207, 114));
    addSlotToContainer(new SlotCustom(inventoryCustom, 5, 207, 132));
    // colone3
    addSlotToContainer(new SlotCustom(inventoryCustom, 6, 225, 96));
    addSlotToContainer(new SlotCustom(inventoryCustom, 7, 225, 114));
    addSlotToContainer(new SlotCustom(inventoryCustom, 8, 225, 132));
    GuiCustomPlayerInventory.chestplate = false;
    } else {
    GuiCustomPlayerInventory.chestplate = true;
    }
    // Poche pantalon
    if (player.inventory.armorItemInSlot(1) != null) {
    // colone 1
    addSlotToContainer(new SlotCustom(inventoryCustom, 9, 189, 16));
    addSlotToContainer(new SlotCustom(inventoryCustom, 10, 189, 34));
    addSlotToContainer(new SlotCustom(inventoryCustom, 11, 189, 52));
    // colone 2
    addSlotToContainer(new SlotCustom(inventoryCustom, 12, 207, 16));
    addSlotToContainer(new SlotCustom(inventoryCustom, 13, 207, 34));
    addSlotToContainer(new SlotCustom(inventoryCustom, 14, 207, 52));
    // colone 3
    addSlotToContainer(new SlotCustom(inventoryCustom, 15, 225, 16));
    addSlotToContainer(new SlotCustom(inventoryCustom, 16, 225, 34));
    addSlotToContainer(new SlotCustom(inventoryCustom, 17, 225, 52));
    GuiCustomPlayerInventory.leggins = false;
    } else {
    GuiCustomPlayerInventory.leggins = true;
    }
    
    addSlotToContainer(new SlotCrafting(player, this.craftMatrix,
    this.craftResult, 18, 144, 36));
    // Add ARMOR slots; note you need to make a public version of SlotArmor
    // just copy and paste the vanilla code into a new class and change what
    // you need
    for (i = 0; i < 4; ++i) {
    addSlotToContainer(new SlotArmor(player, inventoryPlayer,
    inventoryPlayer.getSizeInventory() - 1 - i, 8, 8 + i * 18,
    i));
    }
    
    // Add vanilla PLAYER INVENTORY - just copied/pasted from vanilla
    // classes
    for (i = 0; i < 3; ++i) {
    for (j = 0; j < 9; ++j) {
    addSlotToContainer(new Slot(inventoryPlayer, j + i * 9 + 9,
    8 + j * 18, 84 + i * 18));
    }
    }
    
    // Add ACTION BAR - just copied/pasted from vanilla classes
    for (i = 0; i < 9; ++i) {
    addSlotToContainer(new Slot(inventoryPlayer, i, 8 + i * 18, 142));
    }
    for (i = 0; i < 2; ++i) {
    for (j = 0; j < 2; ++j) {
    this.addSlotToContainer(new Slot(this.craftMatrix, j + i * 2,
    88 + j * 18, 26 + i * 18));
    }
    }
    }
    
    @Override
    public void onCraftMatrixChanged(IInventory p_75130_1_) {
    this.craftResult.setInventorySlotContents(
    0,
    CraftingManager.getInstance().findMatchingRecipe(
    this.craftMatrix,
    Minecraft.getMinecraft().thePlayer.worldObj));
    }
    
    /**
    * This should always return true, since custom inventory can be accessed
    * from anywhere
    */
    @Override
    public boolean canInteractWith(EntityPlayer player) {
    return true;
    }
    
    // drop on close
    public void onContainerClosed(EntityPlayer p_75134_1_) {
    super.onContainerClosed(p_75134_1_);
    
    for (int i = 0; i < 4; ++i) {
    ItemStack itemstack = this.craftMatrix.getStackInSlotOnClosing(i);
    
    if (itemstack != null) {
    p_75134_1_.dropPlayerItemWithRandomChoice(itemstack, false);
    }
    }
    
    this.craftResult.setInventorySlotContents(0, (ItemStack) null);
    }
    
    @Override
       public ItemStack transferStackInSlot(EntityPlayer Player, int index)
       {
    return null;
       }
    }
    
    

    :::
    Deathevent:
    :::

    
    public class DeathEvent
    { InventoryCustomPlayer inv ;
    
    @SubscribeEvent
    public void whenDeath(LivingDeathEvent e)
    {
    if(e.entity instanceof EntityPlayer)
    {
    inv.clearInventory(null, -1);//test clear inventory custom player
    }
    }
    }
    
    

    :::


  • Moddeurs confirmés Rédacteurs Administrateurs

    Avec ça tu as juste un beau NullPointerException.
    InventoryCustomPlayer inv ; -> tu retires, ça n'a rien à faire là.
    Tu peux m'envoyer ta classe qui implémente IExtendedEntityProperties ? (ou un truc comme ça, je me souviens pas du nom exacte de l'interface).



  • ExtendedPlayer:
    :::

    
    public class ExtendedPlayer implements IExtendedEntityProperties
    {
    public final static String EXT_PROP_NAME = "ExtendedPlayer2";
    
    private final EntityPlayer player;
    
    public final InventoryCustomPlayer inventory = new InventoryCustomPlayer();
    
    public ExtendedPlayer(EntityPlayer player) {
    this.player = player;
    }
    
    public static final void register(EntityPlayer player) {
    player.registerExtendedProperties(ExtendedPlayer.EXT_PROP_NAME, new ExtendedPlayer(player));
    }
    
    public static final ExtendedPlayer get(EntityPlayer player) {
    return (ExtendedPlayer) player.getExtendedProperties(EXT_PROP_NAME);
    }
    
    public void copy(ExtendedPlayer props) {
    inventory.copy(props.inventory);
    }
    
    @Override
    public final void saveNBTData(NBTTagCompound compound) {
    NBTTagCompound properties = new NBTTagCompound();
    inventory.writeToNBT(properties);
    compound.setTag(EXT_PROP_NAME, properties);
    }
    
    @Override
    public final void loadNBTData(NBTTagCompound compound) {
    NBTTagCompound properties = (NBTTagCompound) compound.getTag(EXT_PROP_NAME);
    inventory.readFromNBT(properties);
    }
    
    @Override
    public void init(Entity entity, World world) {}
    
    public void onUpdate() {
    }
    }
    

    :::

    je l'avait oublier,

    le InventoryCustomPlayer inv c'est pour appeler ClearInventory() Mais moi aussi je me dit que c est pas geniale


  • Moddeurs confirmés Rédacteurs Administrateurs

    @SubscribeEvent
    public void whenDeath(LivingDeathEvent e)
    {
    if(e.entity instanceof EntityPlayer)
    {
    ExtendedPlayer extendedPlayer = (ExtendedPlayer)e.entity.getExtendedProperties(ExtendedPlayer.EXT_PROP_NAME);
    extendedPlayer.inventory.clearInventory(null, -1);
    }
    }
    

    ça serait déjà mieux comme ça, non ?
    Mais ça reste perfectible, je suis pas sur que cela va les drop.
    Du-coup :

    @SubscribeEvent
    public void whenDeath(LivingDeathEvent e)
    {
    if(e.entity instanceof EntityPlayer)
    {
    EntityPlayer player = (EntityPlayer)e.entity;
    ExtendedPlayer extendedPlayer = (ExtendedPlayer)player.entity.getExtendedProperties(ExtendedPlayer.EXT_PROP_NAME);
    for(int i = 0; i < extendedPlayer.inventory.getSizeInventory(); i++
    {
    ItemStack stack = extendedPlayer.inventory.getStackInSlot(i);
    if(stack != null)
    {
    player.dropItem(stack);
    extendedPlayer.inventory.setInventorySlotContents(i, null);
    }
    }
    }
    }
    


  • je n ai pas accès a
    public final InventoryCustomPlayer inventory = new InventoryCustomPlayer();

    j ai essayer de passer le tableau dans ExtendedPlayer en static ou juste public en retirant le final mais je l'ai pas(isNot a field)

    autant pour moi c'est un mod que j'ai debug il y a pas longtemps qui avait aussi un classe avec le même nom___Merci je passe pas en résolut de-suite peut être un petite question un peut plus tard sur autre chose si je m en sort pas___te rappel  tu du mob qui stock les item du joueur a sa mort?

    je cherche a ajouter les item de cette inventaire au mob en question j ai essayer ca:
    ZombSurvivor.getInventory()* = extendedPlayer2.inventory.getStackInSlot(i);

    j ai augmenté la taille du tableau a 58 Mais j ai out of bounds exception et les slot custom de l inventaire bug ca fait de la duplication d'item en gros si je met un stack ca le duplique mais si je clique soit il disparait soit je peut le récupérer

    http://www.minecraftforgefrance.fr/showthread.php?tid=2173

    Edit:le bug de duplication viens pas de la___Problème résolut j avait oublier un boucle for et j'ai passé le tableau a 50 Slot et bizarrement ça fonctionne merci du coup de main 😉


  • Moddeurs confirmés Rédacteurs Administrateurs

    Ok, parfait 😉
    Je fusionne juste tes 4 messages.


Log in to reply