Jump to content

Recommended Posts

Posted

Alright, so I fixed my issues from earlier. I'm so close to being done with this inventory work. But, after going through CoolAlias' tutorial on inventory, updates and all, my new custom slots don't save. You can place an item in whichever slot, but when you close and reopen the GUI, the item is deleted forever. So sad  :'( hehe.

 

So I did some searching, saw some previous posts on this issue but never found any solutions for my issue.

 

Inventory Class:

 

 

// I removed a bunch of the code because you shouldn't need most of it.

// Some variables
protected ItemStack[] inventory = new ItemStack[3];
public int slotsCount = 3;
private String tagName = Rot.MOD_ID + "TrinketInventory";

// Read/Write NBT Methods
public void writeToNBT(NBTTagCompound tagcompound)
{
	NBTTagList nbttaglist = new NBTTagList();
	for (int i = 0; i < this.getSizeInventory(); ++i)
	{
		if (this.getStackInSlot(i) != null)
		{
			NBTTagCompound nbttagcompound1 = new NBTTagCompound();
			nbttagcompound1.setByte("Slot", (byte) i);
			this.getStackInSlot(i).writeToNBT(nbttagcompound1);
			nbttaglist.appendTag(nbttagcompound1);
		}
	}
	// We're storing our items in a custom tag list using our 'tagName' from
	// above
	// to prevent potential conflicts
	tagcompound.setTag(tagName, nbttaglist);
}

public void readFromNBT(NBTTagCompound compound) {
	// now you must include the NBTBase type ID when getting the list;
	// NBTTagCompound's ID is 10
	NBTTagList items = compound.getTagList(tagName, compound.getId());
	for (int i = 0; i < items.tagCount(); ++i) {
		// tagAt(int) has changed to getCompoundTagAt(int)
		NBTTagCompound item = items.getCompoundTagAt(i);
		byte slot = item.getByte("Slot");
		if (slot >= 0 && slot < getSizeInventory()) {
			setInventorySlotContents(slot,
					ItemStack.loadItemStackFromNBT(item));
		}
	}
}

 

 

 

ExtendPlayer:

 

 

// Some Variables
public final RotTrinketInventory inventory = new RotTrinketInventory();

// get and register
public static final ExtendPlayer get(EntityPlayer player)
{
	try
	{
		return (ExtendPlayer) player.getExtendedProperties(EXT_PROP_NAME);
	}
	catch (Exception e)
	{
		return null;
	}
}

public static final void register(EntityPlayer player)
{
	player.registerExtendedProperties(ExtendPlayer.EXT_PROP_NAME, new ExtendPlayer(player));
}


// Load and Save NBT Data
@Override
public void loadNBTData(NBTTagCompound compound)
{
	// Here we fetch the unique tag compound we set for this class of
	// Extended Properties
	NBTTagCompound properties = (NBTTagCompound) compound.getTag(EXT_PROP_NAME);
	// Snipped out a bunch of other loads you don't need.
	this.inventory.readFromNBT(properties);

}

@Override
public void saveNBTData(NBTTagCompound compound)
{
	// We need to create a new tag compound that will save everything for
	// our Extended Properties
	NBTTagCompound properties = new NBTTagCompound();

	// Again I removed a ton of saves you don't need.

	this.inventory.writeToNBT(properties);

	compound.setTag(EXT_PROP_NAME, properties);
}

 

 

 

Should be all the code you'd need; I don't think it's a container issue, but idk.

 

Once more, any help is appreciated.  :-\

Posted

That looks like it should be saving and loading correctly - are you providing a proper server-side gui element, i.e. a Container, when opening your gui? Is that container using the player's custom inventory field?

 

Show your IGuiHandler code as well as your Container constructor.

 

Container:

 

 

	public ContainerPseudoPlayer(InventoryPlayer realPlayerInventory, EntityPlayer player)
{
	int i;
        int j;
        
        // Inventory
	for (i = 0; i < 3; ++i)
        {
            for (j = 0; j < 9; ++j)
            {
                this.addSlotToContainer(new Slot(realPlayerInventory, j + (i + 1) * 9, 8 + j * 18, 84 + i * 18));
            }
        }

	// Hotbar
	for (i = 0; i < 9; ++i)
        {
            this.addSlotToContainer(new Slot(realPlayerInventory, i, 8 + i * 18, 142));
        }

	// Trinkets
	for (i = 0; i < 3; i++)
	{
		this.addSlotToContainer(new Slot(new RotTrinketInventory(), i, 26, 8 + i * 18));
	}

	// Armor, literally copy-pasted from container-player
	 for (i = 0; i < 4; ++i)
        {
            final int k = i;
            this.addSlotToContainer(new Slot(realPlayerInventory, realPlayerInventory.getSizeInventory() - 1 - i, 8, 8 + i * 18)
            {
                public int getSlotStackLimit()
                {
                    return 1;
                }
                public boolean isItemValid(ItemStack stack)
                {
                    if (stack == null) return false;
                    return stack.getItem().isValidArmor(stack, k, player);
                }
                @SideOnly(Side.CLIENT)
                public String getSlotTexture()
                {
                    return ItemArmor.EMPTY_SLOT_NAMES[k];
                }
            });
        }
}

 

 

 

GuiHandler:

 

 

public class GuiHandler implements IGuiHandler {
@Override
public Object getClientGuiElement(int guiId, EntityPlayer player, World world, int x,
		int y, int z) {
	// -snip-
	switch (guiId) {
		case 0 :
			// -snip-
		case 1 : {
			return new GuiClassSelection(player);
		}
		default :
			return null;
	}
}
// 1 is class Menu
@Override
public Object getServerGuiElement(int guiId, EntityPlayer player, World world, int x,
		int y, int z) {
	// -snip-

	switch (guiId) {
		case 0 :
			// -snip -
		case 1 : 
			return new ContainerPseudoPlayer(player.inventory, player);
		default :
			return null;
	}
}

}

Snipped out some code for a different gui :P

 

 

Posted

Your issue is that you are not actually passing in the custom player inventory, only the vanilla inventory.

 

Your code should look like this:

return new ContainerCustomPlayer(player, player.inventory, ExtendedPlayer.get(player).inventory);

Note the last parameter is the custom inventory, which you need in your Container class to add the custom inventory slots:

// add one slot to the container for every slot in your custom inventory:
addSlotToContainer(new SlotCustom(inventoryCustom, 0, 80, );

Guest
This topic is now closed to further replies.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • logs too big for one pastebin https://pastebin.com/ZjUGHu3u  https://pastebin.com/RqCUZf3X  https://pastebin.com/6ZPS99nD
    • You probably used jd-gui to open it, didn't you? Nothing wrong with that, I also made that mistake, except that Notch was a smart guy and he obfuscated the code. That's why you only see files called "a", "b", "c" and then a file that combines them all. As I said, use RetroMCP to deobfuscate the code so that you will 100% understand it and be able to navigate it.
    • Decompiling minecraft indev, infdev, alpha, beta or whichever legacy version is really easy. I'm not a plug, I just also got interested in modding legacy versions (Infdev to be specific). Use https://github.com/MCPHackers/RetroMCP-Java Once you install their client and the Zulu Architecture that they say they recommend (or use your own Java). I encountered some problems, so I run it with: "java -jar RetroMCP-Java-CLI.jar". You should run it in a seperate folder (not in downloads), otherwise the files and folders will go all over the place. How to use RetroMCP: Type setup (every time you want change version), copy-paste the version number from their list (they support indev), write "decompile" and done! The code will now be deobfuscated and filenames will be normal, instead of "a", "b" and "c"! Hope I helped you, but I don't expect you to reply, as this discussion is 9 years old! What a piece of history!  
    • I know that this may be a basic question, but I am very new to modding. I am trying to have it so that I can create modified Vanilla loot tables that use a custom enchantment as a condition (i.e. enchantment present = item). However, I am having trouble trying to implement this; the LootItemRandomChanceWithEnchantedBonusCondition constructor needs a Holder<Enchantment> and I am unable to use the getOrThrow() method on the custom enchantment declared in my mod's enchantments class. Here is what I have so far in the GLM:   protected void start(HolderLookup.Provider registries) { HolderLookup.RegistryLookup<Enchantment> registrylookup = registries.lookupOrThrow(Registries.ENCHANTMENT); LootItemRandomChanceWithEnchantedBonusCondition lootItemRandomChanceWithEnchantedBonusCondition = new LootItemRandomChanceWithEnchantedBonusCondition(0.0f, LevelBasedValue.perLevel(0.07f), registrylookup.getOrThrow(*enchantment here*)); this.add("nebu_from_deepslate", new AddItemModifier(new LootItemCondition[]{ LootItemBlockStatePropertyCondition.hasBlockStateProperties(Blocks.DEEPSLATE).build(), LootItemRandomChanceCondition.randomChance(0.25f).build(), lootItemRandomChanceWithEnchantedBonusCondition }, OrichalcumItems.NEBU.get())); }   Inserting Enchantments.[vanilla enchantment here] actually works but trying to declare an enchantment from my custom enchantments class as [mod enchantment class].[custom enchantment] does not work even though they are both a ResourceKey and are registered in Registries.ENCHANTMENT. Basically, how would I go about making it so that a custom enchantment declared as a ResourceKey<Enchantment> of value ResourceKey.create(Registries.ENCHANTMENT, ResourceLocation.fromNamespaceAndPath([modid], [name])), declared in a seperate enchantments class, can be used in the LootItemRandomChanceWithEnchantedBonusCondition constructor as a Holder? I can't use getOrThrow() because there is no level or block entity/entity in the start() method and it is running as datagen. It's driving me nuts.
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.