To be honest, I've forgotten how this mod was written.
So the following code can not be run is very normal.
On the last book, we dealt with the event that triggered when the player was right-clicking the backpack, sent a command to open the Backpack window, but the Backpack window has not yet been written to solve the problem.
This time because there are too many nested relationships, I'm in the reverse order of execution.
So, regardless of the last GUI that was not opened.
Each container in the MC, whether it is a box, a furnace or a brewing stand, holds an instance of its own content space (Inventory), each of which holds its own content in this content space.
The base class of content space is Inventorybasic, realizes the Iinventory interface, for convenience, we inherit Inventorybasic class directly.
Inventorybasic class has implemented most of the content space needs of the function, you can rough time code, very understood.
So our black Cat backpack content space class only need to deal with the contents of the preservation and reading.
1 Public classInventorykuronekoextendsInventorybasic {2 Public Static FinalString TITLE = "Black Cat";3 Public Static Final intSlots_per_line = 9;4 Public Static Final intLINES = 2;5 6 PrivateItemstack Itemstack;7 8 PrivateString ID;9 Ten PublicInventorykuroneko (Itemstack itemstack) { One Super(TITLE,true, LINES *slots_per_line); A - This. Itemstack =Itemstack; - the if(!Itemstack.hastagcompound ()) { -Itemstack.settagcompound (NewNbttagcompound ()); -ID =Uuid.randomuuid (). toString (); - } + - READFROMNBT (Itemstack.gettagcompound ()); + } A at Publicnbttagcompound WRITETONBT (Nbttagcompound compound) { -Nbttaglist items =Newnbttaglist (); - - for(inti = 0; I < getsizeinventory (); i++) { -Itemstack Itemstack =Getstackinslot (i); - if(Itemstack! =NULL) { inNbttagcompound item =NewNbttagcompound (); -Item.setinteger ("Slot", i); to ITEMSTACK.WRITETONBT (item); + Items.appendtag (item); - } the } * $Compound.settag ("Items", items);Panax Notoginseng -Compound.setstring ("id", id); the + returncompound; A } the + Public voidREADFROMNBT (Nbttagcompound compound) { - if(id = =NULL) { $id = compound.getstring ("id"); $ } - if(id = =NULL) { -ID =Uuid.randomuuid (). toString (); the } - WuyiNbttaglist items = compound.gettaglist ("Items", 10); the - for(inti = 0; I < Items.tagcount (); i++) { WuNbttagcompound item =Items.getcompoundtagat (i); - About intSlot = Item.getinteger ("Slot"); $ if(slot >= 0 && Slots <getsizeinventory ()) { -Itemstack Itemstack =ITEMSTACK.LOADITEMSTACKFROMNBT (item); - setinventoryslotcontents (slot, itemstack); - } A } + } the - @Override $ Public voidMarkdirty () { the Super. Markdirty (); theWRITETONBT ( This. Itemstack.stacktagcompound); the } the}
throw the code first .
Write your own two methods, the name can be casually up, I here a call WRITETONBT, for the preservation of content, a called READFROMNBT, used to read the contents.
The NBT mechanism is used for saving and reading, and the NBT full name is what I have forgotten.
Say save first.
First create an NBT list of nbttaglist, which holds the items and their locations. This class can be simply understood as a list.
It then iterates through all the squares in the content space and, as long as it finds an item in the lattice, saves the current lattice position in a nbttagcompound, which is understood as a storage unit.
The light storage location is not enough, call the ready-made API to add the information of this item to the memory unit.
Finally, add this storage unit to the list.
In this way, all the information for all the items is saved, and the list is then placed in the memory cell of the parameter.
How to deal with the storage unit in the parameter is not your business.
Finally, the invocation of this method is WRITETONBT, and according to experience, there should be a callback method that is called automatically by the system when it needs to save content in the content space.
I do have.
I'm so hard to find.
Overriding the Markdirty method of the parent class, first calling the implementation of the parent class, and then adding
WRITETONBT (this. itemstack.stacktagcompound);
Here Itemstack refers to the use of this content space items, that is, the Black cat backpack.
It would be nice to receive and save it in the construction method.
The system will automatically save all the items in the backpack to this backpack instance.
But the light is not saved, it has to be read.
So to deal with READFROMNBT.
First go back to the construction method.
The first time you open the backpack will certainly not have the contents of the saved, this.itemStack.stackTagCompound this thing also does not exist, so judge, initialize one.
if (! Itemstack.hastagcompound ()) { Itemstack.settagcompound (new nbttagcompound ()); = Uuid.randomuuid (). toString ();}
Give the current content space an ID to identify different backpacks.
Without the distinction of ID, it becomes an ender backpack.
Now there is no problem with the null pointer, so no matter 3,721, read it.
I use the accessors below the direct access variable effect is the same, please do not care.
First get the ID of the current content space from the storage unit stored in the item.
Then take out the list of all the items you just saved nbttaglist, the second parameter of this method I forgot what it was for, copy it.
The next step is to traverse, take out a storage unit, find out the location of the lattice, and then use the methods in the Itemstack class and the information stored in the storage unit to create an item stack and set it to the specified location.
Perfectly restored what was previously saved.
Finally, say three constants.
Title is the name of the content space and appears to be displayed on the interface.
Because it is convenient to use the box interface directly, so slots_per_line please do not move.
Lines is the number of lines, I give a balance to a 2, the box seems to be 4? To 65535 should also be feasible. If your monitor has such a high vertical resolution ...
Write here the basic function of the backpack has been completed, the back will begin to hurt the egg.
As I said earlier, I intend to directly reuse the box interface, but the box related to the two classes are hard-coded, so there is no way to inherit the way to create the backpack interface.
In English, it is a workaround with a very sore egg.
In the next article again.
"Forge" Minecraft 1.7.10 MoD Development Research-Black Cat backpack mod "06-Sing you a song of the years"