WriteNBT que dois-je retourner ?



  • Bonjour,

    je suis actuellement en train de travailler sur un inventaire de joueur custom, et donc j'utilise les capabilities, sauf que je suis tombé sur un problème, j'utilise la variable suivante : " [font=Courier Newprivate ]ItemStackHandler handler = new ItemStackHandler(15); " et pour writeNBT je ne peux que return un nouvel NBTtag, prédéfini.

    Le problème se situe princiapelemnt dans " [font=Ubuntu, sans-serifCustomInventoryStorage.java "
    ]

    Mes classes :

    #CustomInventoryProvider.java(CustomInventoryProvider.java)

    :::

    [font=Courier Newpackage ]fr.afraidofthedark.capability.inventory%(#cc7832)[;
    ]
    import net.minecraft.entity.player.EntityPlayer%(#cc7832)[;
    ]import net.minecraft.nbt.NBTTagCompound%(#cc7832)[;
    ]import net.minecraft.tileentity.TileEntity%(#cc7832)[;
    ]import net.minecraft.util.EnumFacing%(#cc7832)[;
    ]import net.minecraftforge.common.capabilities.Capability%(#cc7832)[;
    ]import net.minecraftforge.common.capabilities.CapabilityInject%(#cc7832)[;
    ]import net.minecraftforge.common.capabilities.ICapabilityProvider%(#cc7832)[;
    ]import net.minecraftforge.items.ItemStackHandler%(#cc7832)[;
    ]
    import javax.annotation.Nullable%(#cc7832)[;
    ]
    %(#629755)[*/**
    *]%(#629755)[* * Created by Mr. Dj on 16/05/2017.
    *]%(#629755)[* */
    *]public class CustomInventoryProvider extends TileEntity implements ICapabilityProvider {
    
       @CapabilityInject(ICustomInventory.class)
       public static final Capability <icustominventory>*CUSTOM_INV *= %(#cc7832)[null;
    ]
        private ICustomInventory instance = *CUSTOM_INV*.getDefaultInstance()%(#cc7832)[;
    ]
        private ItemStackHandler handler = new ItemStackHandler(15)%(#cc7832)[;
    ]
       %(#bbb529)[@Override
    ]    public void readFromNBT(NBTTagCompound compound) {
           this.handler.deserializeNBT(compound.getCompoundTag("ItemStackHandler"))%(#cc7832)[;
    ]        super.readFromNBT(compound)%(#cc7832)[;
    ]    }
    
       %(#bbb529)[@Override
    ]    public NBTTagCompound writeToNBT(NBTTagCompound compound) {
           compound.setTag("ItemStackHandler", handler.serializeNBT())%(#cc7832)[;
    ]        return super.writeToNBT(compound)%(#cc7832)[;
    ]    }
    
       %(#bbb529)[@Override
    ]    public <T> T getCapability(Capability<T> capability, @Nullable EnumFacing facing) {
           return capability == *CUSTOM_INV *? *CUSTOM_INV*.<T> cast(this.instance) : %(#cc7832)[null;
    ]    }
    
       %(#bbb529)[@Override
    ]    public boolean hasCapability(Capability capability, @Nullable EnumFacing facing) {
           return capability == *CUSTOM_INV*%(#cc7832)[;
    ]    }
    
       public boolean isUseableByPlayer(EntityPlayer player) {
           return this.world.getTileEntity(this.getPos()) == %(#cc7832)[this
    ]                && player.getDistanceSq(this.pos.add(0.5, 0.5, 0.5)) <= 64%(#cc7832)[;
    ]    }
    
    }
    

    :::

    #CustomInventoryStorage.java(CustomInventoryStorage.java)

    :::

    
    [font=Courier Newpackage ]fr.afraidofthedark.capability.inventory%(#cc7832)[;
    ]
    import net.minecraft.nbt.NBTBase%(#cc7832)[;
    ]import net.minecraft.util.EnumFacing%(#cc7832)[;
    ]import net.minecraftforge.common.capabilities.Capability%(#cc7832)[;
    ]
    import javax.annotation.Nullable%(#cc7832)[;
    ]
    %(#629755)[*/**
    *]%(#629755)[* * Created by Mr. Dj on 20/05/2017.
    *]%(#629755)[* */
    *]public class CustomInventoryStorage implements Capability.IStorage <icustominventory>{
       %(#bbb529)[@Nullable
    ]%(#bbb529)[    @Override
    ]    public NBTBase writeNBT(Capability <icustominventory>capability, ICustomInventory instance, EnumFacing side) {
           %(#808080)[//LE PROBLEME EST ICI, QUE METTRE ?
    ]%(#808080)[        //QUE DOIS-JE RETOURNER ?
    ]    }
    
       %(#bbb529)[@Override
    ]    public void readNBT(Capability <icustominventory>capability, ICustomInventory instance, EnumFacing side, NBTBase nbt) {
    
       }
    }
    
    

    :::

    #CustomInventory.java(CustomInventory.java)

    :::

    
    [font=Courier Newpackage ]fr.afraidofthedark.capability.inventory%(#cc7832)[;
    ]
    %(#629755)[*/**
    *]%(#629755)[* * Created by Mr. Dj on 20/05/2017.
    *]%(#629755)[* */
    *]public class CustomInventory implements ICustomInventory {
       %(#808080)[//Ne pas prêter spécialement attention.
    ]    %(#bbb529)[@Override
    ]    public void doNothing() {
    
       }
    }
    
    

    :::

    #ICustomInventory.java(ICustomInventory.java)

    :::

    
    [font=Courier Newpackage ]fr.afraidofthedark.capability.inventory%(#cc7832)[;
    ]
    %(#629755)[*/**
    *]%(#629755)[* * Created by Mr. Dj on 19/05/2017.
    *]%(#629755)[* */
    *]public interface ICustomInventory {
       public void doNothing()%(#cc7832)[;
    ]}
    
    

    :::</icustominventory></icustominventory></icustominventory></icustominventory>


  • Administrateurs

    Salut,
    Tu peux mettre n'importe quoi héritant de NBTBase.
    Exemple avec un NBTTagCompound :

    package fr.mcnanotech.nhg.common.capabilities;
    
    import net.minecraft.inventory.ItemStackHelper;
    import net.minecraft.item.ItemStack;
    import net.minecraft.nbt.NBTBase;
    import net.minecraft.nbt.NBTTagCompound;
    import net.minecraft.nbt.NBTTagList;
    import net.minecraft.tileentity.TileEntityFurnace;
    import net.minecraft.util.EnumFacing;
    import net.minecraftforge.common.capabilities.Capability;
    import net.minecraftforge.common.capabilities.Capability.IStorage;
    import net.minecraftforge.common.util.Constants;
    
    public class NHGCapabilityStorage implements IStorage <inhgcapability>{
    @Override
    public NBTBase writeNBT(Capability <inhgcapability>capability, INHGCapability instance, EnumFacing side)
    {
    NBTTagCompound tag = new NBTTagCompound();
    tag.setInteger("KitId", instance.getKitId());
    tag.setInteger("StartKitId", instance.getStartKitId());
    tag.setInteger("ServerType", instance.getServerType());
    tag.setString("TeamName", instance.getTeamName());
    tag.setInteger("TeamColor", instance.getTeamColor());
    tag.setBoolean("NotTargetable", instance.cantBeTargeted());
    
    NBTTagCompound furnaceTag = new NBTTagCompound();
    furnaceTag.setShort("BurnTime", (short)instance.getIntegratedFurnace().furnaceBurnTime);
    furnaceTag.setShort("CookTime", (short)instance.getIntegratedFurnace().cookTime);
    furnaceTag.setShort("CookTimeTotal", (short)instance.getIntegratedFurnace().totalCookTime);
    
    ItemStackHelper.saveAllItems(furnaceTag, instance.getIntegratedFurnace().furnaceItemStacks);
    tag.setTag("IntegratedFurnace", furnaceTag);
    
    tag.setBoolean("Cloaked", instance.isCloaked());
    tag.setString("Cosmetic", instance.getCosmetic() == null || instance.getCosmetic().isEmpty() ? "none" : instance.getCosmetic());
    tag.setBoolean("SayDisconnectMessage", instance.shouldSayDisconnectMessage());
    tag.setInteger("PreviousFoodLevel", instance.getPreviousFoodLevel());
    tag.setInteger("KitIntData", instance.getKitIntData());
    tag.setInteger("TickSinceLastDamage", instance.getTickSinceLastDamage());
    return tag;
    }
    
    @Override
    public void readNBT(Capability <inhgcapability>capability, INHGCapability instance, EnumFacing side, NBTBase nbt)
    {
    NBTTagCompound tag = ((NBTTagCompound)nbt);
    instance.setKitId(tag.getInteger("KitId"));
    instance.setStartKitId(tag.getInteger("StartKitId"));
    instance.setServerType(tag.getInteger("ServerType"));
    instance.setTeam(tag.getString("TeamName"), tag.getInteger("TeamColor"));
    instance.setNotTargetable(tag.getBoolean("NotTargetable"));
    
    if(tag.hasKey("IntegratedFurnace", Constants.NBT.TAG_COMPOUND))
    {
    NBTTagCompound furnaceTag = (NBTTagCompound)tag.getTag("IntegratedFurnace");
    NBTTagList nbttaglist = furnaceTag.getTagList("Items", 10);
    
    ItemStackHelper.loadAllItems(furnaceTag, instance.getIntegratedFurnace().furnaceItemStacks);
    
    instance.getIntegratedFurnace().furnaceBurnTime = furnaceTag.getShort("BurnTime");
    instance.getIntegratedFurnace().cookTime = furnaceTag.getShort("CookTime");
    instance.getIntegratedFurnace().totalCookTime = furnaceTag.getShort("CookTimeTotal");
    instance.getIntegratedFurnace().currentItemBurnTime = TileEntityFurnace.getItemBurnTime(instance.getIntegratedFurnace().furnaceItemStacks.get(1));
    }
    
    instance.setCloaked(tag.getBoolean("Cloaked"));
    instance.setCosmetic(tag.getString("Cosmetic"));
    instance.sayDisconnectMessage(tag.getBoolean("SayDisconnectMessage"));
    instance.setPreviousFoodLevel(tag.getInteger("PreviousFoodLevel"));
    instance.setKitIntData(tag.getInteger("KitIntData"));
    instance.setTickSinceLastDamage(tag.getInteger("TickSinceLastDamage"));
    }
    }
    ```</inhgcapability></inhgcapability></inhgcapability>

Log in to reply