Jump to content

[1.7.10] Inventory not updating when changed


Ben_

Recommended Posts

I'm trying to write a questing mod, and I'm trying to write the completion.

 

What I need to do, is give some items to the player and then remove some of them.

The relevant code is near the bottom:

	public static Boolean finishQuest(Quest quest, EntityPlayer player) {
	if(player.worldObj.isRemote == true) {
		//On the client side
		System.out.println("Sending quest completion request");
		EngineerCraft.channel.sendToServer(new QuestCompleteMessage(player, quest.getName()));



		return null;
	} else {
		//On the server side
		System.out.println("Receiving quest completion request");






		//Get the list of inputs to be checked
		@SuppressWarnings("unchecked")
		ArrayList<ItemStack> toBeChecked = (ArrayList<ItemStack>) quest.getInputs().clone();

		//Go through every single input
		while(toBeChecked.size() > 0) {
			ItemStack currentStack = toBeChecked.get(0);

			//The amount of items of this type in the inventory
			int itemCount = 0;

			//Go through each stack in the inventory and add together the total amount of the same item
			for(int i = 0; i < player.inventory.mainInventory.length; i++) {
				ItemStack checkStack = player.inventory.mainInventory[i];
				if(checkStack != null) {
					if(checkStack.isItemEqual(currentStack)) {
						itemCount += checkStack.stackSize;
					}
				}
			}

			if(itemCount >= currentStack.stackSize) {
				toBeChecked.remove(0);
			} else {
				return false;
			}
		}

		//If the game has reached this point, the player's inventory has everything the quest needs








		//Get the list of inputs to be taken
		@SuppressWarnings("unchecked")
		ArrayList<ItemStack> toBeTaken = (ArrayList<ItemStack>) quest.getInputs().clone();

		//Go through every single input and remove them
		while(toBeTaken.size() > 0) {
			ItemStack currentStack = toBeTaken.get(0);

			//The amount of items of this type in the inventory
			int itemCount = currentStack.stackSize;

			//Go through each stack in the inventory and add together the total amount of the same item
			for(int i = 0; i < player.inventory.mainInventory.length; i++) {
				ItemStack checkStack = player.inventory.mainInventory[i];
				if(checkStack != null) {
					if(checkStack.isItemEqual(currentStack)) {
						if(checkStack.stackSize > itemCount) {
							player.inventory.mainInventory[i] = player.inventory.decrStackSize(i, itemCount);
							itemCount = 0;
						} else {
							itemCount -= checkStack.stackSize;
							player.inventory.mainInventory[i] = null;
						}
					}
				}

			}
			toBeTaken.remove(0);
		}






		//Reward the player
		//Get the list of rewards to be given
		@SuppressWarnings("unchecked")
		ArrayList<ItemStack> toBeRewarded = (ArrayList<ItemStack>) quest.getInputs().clone();

		for(int i = 0; i < toBeRewarded.size(); i++) {

			//If adding an item stack to the inventory fails
			if(player.inventory.addItemStackToInventory(toBeRewarded.get(i).copy()) == false) {

				//Spawn an item on the ground
				player.worldObj.loadedEntityList.add(new EntityItem(player.worldObj, player.posX, player.posY + 1.0, player.posZ, toBeRewarded.get(i).copy()));
			}
		}

		player.inventory.inventoryChanged = true;

		player.inventory.markDirty();



		//Save the quest completion
		saveQuestCompletion(quest, player);

		return true;
	}
}

I have debugged the code and seen that it does indeed get changed, but then outside of the scope it gets brought back to its original value.

 

Is there something I'm missing? This code is only server-side, but that shouldn't matter, because when I reload the world it doesn't change.

 

Does inventory.inventoryChanged need to be changed back to false next tick?

 

I have been incredibly frustrated by this issue, because none of my solutions work.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

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