Jump to content

[1.7.10] Issue with identifying random item.


HammyD

Recommended Posts

Hi everyone, apologies if the answer to this is something simple but I have spent forever trying to resolve this and I'm getting nowhere so a nudge would be much appreciated.

 

Basically I have a block that when you r-click it places an item (from a range of 10 possible items) in to the players' inventory (if they have space) and alerts them to the fact a certain item has been added. On the face of things everything works but for some reason the item and the alert don't match most of the time i.e. a piece of string would appear in your inventory and you would be told "You got a fish!".

 

The following is my code which I have gradually degraded in to its current state having previously used switch statements, a single itemStack etc. all with the same result.

 

public boolean onBlockActivated (World world, int x, int y, int z, EntityPlayer player, int q, float a, float b, float c)
{
	if (!player.isSneaking())
	{
		boolean hasSpace = false;
    	int giftedVolume = 0;
    	String giftedItem = "";
    	Random ran = new Random();
    	int randomNum = ran.nextInt(10);
    	int maxStack = 64;
    	ItemStack [] checkStack = new ItemStack[10];
    	
    	checkStack[0] = new ItemStack(Items.coal,1);
        	checkStack[1] = new ItemStack(Items.rotten_flesh,1);
        	checkStack[2] = new ItemStack(Items.wheat_seeds,1);
        	checkStack[3] = new ItemStack(Items.bone,1);
        	checkStack[4] = new ItemStack(Items.flint,1);
        	checkStack[5] = new ItemStack(Items.fish,1);
        	checkStack[6] = new ItemStack(Items.bread,1);
                checkStack[7] = new ItemStack(Items.string,1);
        	checkStack[8] = new ItemStack(Items.bowl,1);
        	checkStack[9] = new ItemStack(Items.apple,1);
        	
    	giftedVolume = 1;
        
        if (player.inventory.getFirstEmptyStack() != -1)
   			{
   					hasSpace = true;
   			} else {
   				for (int slot = 0; slot < player.inventory.getSizeInventory(); ++slot)
   				{
   					ItemStack itemstack = player.inventory.getStackInSlot(slot);
   					if (itemstack != null && itemstack.isItemEqual(checkStack[randomNum]))
   				    {
   					    int volume = itemstack.stackSize;
   					    if (volume < maxStack)
   					    {
   						    hasSpace = true;
   						    break;
   					    }
   				    }
   				}
   			}		
   			
        if (hasSpace == true)
   			{
        	player.inventory.addItemStackToInventory(checkStack[randomNum]);
        	
        	ItemStack whatWasAdded = checkStack[randomNum];
        	ItemStack test1 = new ItemStack(Items.coal,1);
        	ItemStack test2 = new ItemStack(Items.rotten_flesh,1);
        	ItemStack test3 = new ItemStack(Items.wheat_seeds,1);
        	ItemStack test4 = new ItemStack(Items.bone,1);
        	ItemStack test5 = new ItemStack(Items.flint,1);
        	ItemStack test6 = new ItemStack(Items.fish,1);
        	ItemStack test7 = new ItemStack(Items.bread,1);
        	ItemStack test8 = new ItemStack(Items.string,1);
        	ItemStack test9 = new ItemStack(Items.bowl,1);
        	ItemStack test10 = new ItemStack(Items.apple,1);
        	
        	if (whatWasAdded.isItemEqual(test1)){
               	giftedItem = "Some Coal";
        	} else if (whatWasAdded.isItemEqual(test2)) {
        		giftedItem = "Some Smelly Rotten Flesh";
        	} else if (whatWasAdded.isItemEqual(test3)) {
        		giftedItem = "Some Seeds";
        	} else if (whatWasAdded.isItemEqual(test4)) {
        		giftedItem = "A Human Bone";
        	} else if (whatWasAdded.isItemEqual(test5)) {
        		giftedItem = "A Piece of Flint";
        	} else if (whatWasAdded.isItemEqual(test6)) {
        		giftedItem = "A Fresh Fish";
        	} else if (whatWasAdded.isItemEqual(test7)) {
        		giftedItem = "Some Tasty Bread";
        	} else if (whatWasAdded.isItemEqual(test8)) {
        		giftedItem = "A Piece of String";
        	} else if (whatWasAdded.isItemEqual(test9)) {
        		giftedItem = "A Wooden Bowl";
        	} else if (whatWasAdded.isItemEqual(test10)){
        		giftedItem = "A Definitely Not Poisened Apple";
        	} else {
        		giftedItem = "Something went wrong!";
        	}
        	
        			        	
        	String successMessage = EnumChatFormatting.YELLOW + ">You searched the crate and discovered... " + giftedItem;
   		   		if(!world.isRemote)
   		   		{	
   		   			player.addChatComponentMessage(new ChatComponentText(successMessage));
   		   		}
   		   			this.func_150185_e(world, x, y, z);
   			}
   		
   			   			   			    
   			if (hasSpace == false)
   			{
   				if(!world.isRemote)
   				{	
   					player.addChatComponentMessage(new ChatComponentText("Searching this crate would be a waste as your inventory is full!"));
   				}
   			}
   			
    	return true;
	} else {
		return false;
	}
}

 

Thank you in advance for any help you can provide. Having been out of the programming loop for over 20 years I have an honest admiration for what you guys and girls can do.

 

Malc aka HammyD

Link to comment
Share on other sites

You need !world.isRemote at beggining.

 

Inventory should be only ever edited (in logical means, not syncing) from server.

 

Since I don't have practical experience in manipulating vanilla inventory (because I have my own), I am unsure of this, but - you might have to set some flag affter changing inventory on server to make it sync to client.

Some markDirty or something. Or just use inventory-provided method that should do it for you (lookup source).

 

Read more: http://mcforge.readthedocs.org/en/latest/concepts/sides/

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

Also, oh god, that if-stack checking whether the item given was equal to an array of stuff.

 

Jesus christ.

 

 

Cleaned it up

 

 

 

public boolean onBlockActivated (World world, int x, int y, int z, EntityPlayer player, int q, float a, float b, float c)
{
	if (!player.isSneaking())
	{
		boolean hasSpace = false;
    	int giftedVolume = 0;
    	String giftedItem = "";
    	Random ran = new Random();
    	int randomNum = ran.nextInt(10);
    	int maxStack = 64;
    	giftedVolume = 1;
        
        if (player.inventory.getFirstEmptyStack() != -1)
   			{
   					hasSpace = true;
   			} else {
   				for (int slot = 0; slot < player.inventory.getSizeInventory(); ++slot)
   				{
   					ItemStack itemstack = player.inventory.getStackInSlot(slot);
   					if (itemstack != null && itemstack.isItemEqual(checkStack[randomNum]))
   				    {
   					    int volume = itemstack.stackSize;
   					    if (volume < maxStack)
   					    {
   						    hasSpace = true;
   						    break;
   					    }
   				    }
   				}
   			}		
   			
        if (hasSpace == true)
   			{
			ItemStack stack;
			switch(randomNum) {
				case 0:
					stack = new ItemStack(Items.coal,1);
					giftedItem = "Some coal";
					break;
				case 1:
					stack = new ItemStack(Items.rotten_flesh,1);
					giftedItem = "Some rotten meat";
					break;
				case 2:
					stack = new ItemStack(Items.wheat_seeds,1);
					giftedItem = "Some seeds";
					break;
				case 3:
					stack = new ItemStack(Items.bone,1);
					giftedItem = "A bone";
					break;
				case 4:
					stack = new ItemStack(Items.flint,1);
					giftedItem = "A piece of flint";
					break;
				case 5:
					stack = new ItemStack(Items.fish,1);
					giftedItem = "Fresh fish";
					break;
				case 6:
					stack = new ItemStack(Items.bread,1);
					giftedItem = "A loaf of bread";
					break;
				case 7:
					stack = new ItemStack(Items.string,1);
					giftedItem = "A piece of string";
					break;
				case 8:
					stack = new ItemStack(Items.bowl,1);
					giftedItem = "A wooden bowl";
					break;
			}
			player.inventory.addItemStackToInventory(stack);
        			        	
        	String successMessage = EnumChatFormatting.YELLOW + ">You searched the crate and discovered... " + giftedItem;
   		   		if(!world.isRemote)
   		   		{	
   		   			player.addChatComponentMessage(new ChatComponentText(successMessage));
   		   		}
   		   			this.func_150185_e(world, x, y, z);
   			}
   		
   			   			   			    
   			if (hasSpace == false)
   			{
   				if(!world.isRemote)
   				{	
   					player.addChatComponentMessage(new ChatComponentText("Searching this crate would be a waste as your inventory is full!"));
   				}
   			}
   			
    	return true;
	} else {
		return false;
	}
}

 

 

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Thanks Gents, will give that a go and let you know how I get on.

 

Draco, appreciate your input. The posted code was my nth iteration while trying to work out what was going wrong. My first iteration was pretty much identical to your cleaned up version using a switch, I changed it just in-case the repeated use of the same itemstack was causing the issue (which I now know wasn't). Didn't want you to think I hadn't given it a crack and wanted spoon feeding.

 

Ham.

 

 

Link to comment
Share on other sites

Splitting it out like that had a huge potential for introducing more errors.  If you'd managed to screw something up and got the items out of order the second time around, you'd never have noticed.

If you plan for this to be extendable, it'd be worth creating a "helper" class that lets you create the results and their message strings like a recipe.

[cope]public class RandomWhatever {
public ItemStack theStack;
public string message;

//create these once, store somewhere, reference later
public RandomWhatever(ItemStack stack, string msg) {
	theStack = stack;
	message = msg;
}
}

 

Then you can store them in an array, randomly select one, clone the item stack and alert the player with the string, no weird lookups, equivalence checks, if-else blocks, or switch stack.

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Let me try and help you with love spells, traditional healing, native healing, fortune telling, witchcraft, psychic readings, black magic, voodoo, herbalist healing, or any other service your may desire within the realm of african native healing, the spirits and the ancestors. I am a sangoma and healer. I could help you to connect with the ancestors , interpret dreams, diagnose illness through divination with bones, and help you heal both physical and spiritual illness. We facilitate the deepening of your relationship to the spirit world and the ancestors. Working in partnership with one\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’s ancestors is a gift representing a close link with the spirit realm as a mediator between the worlds.*   Witchdoctors, or sorcerers, are often purveyors of mutis and charms that cause harm to people. we believe that we are here for only one purpose, to heal through love and compassion.*   African people share a common understanding of the importance of ancestors in daily life. When they have lost touch with their ancestors, illness may result or bad luck. Then a traditional healer, or sangoma, is sought out who may prescribe herbs, changes in lifestyle, a career change, or changes in relationships. The client may also be told to perform a ceremony or purification ritual to appease the ancestors.*   Let us solve your problems using powerful African traditional methods. We believe that our ancestors and spirits give us enlightenment, wisdom, divine guidance, enabling us to overcome obstacles holding your life back. Our knowledge has been passed down through centuries, being refined along the way from generation to generation. We believe in the occult, the paranormal, the spirit world, the mystic world.*   The services here are based on the African Tradition Value system/religion,where we believe the ancestors and spirits play a very important role in society. The ancestors and spirits give guidance and counsel in society. They could enable us to see into the future and give solutions to the problems affecting us. We use rituals, divination, spells, chants and prayers to enable us tackle the task before us.*   I have experience in helping and guiding many people from all over the world. My psychic abilities may help you answer and resolve many unanswered questions
    • Let me try and help you with love spells, traditional healing, native healing, fortune telling, witchcraft, psychic readings, black magic, voodoo, herbalist healing, or any other service your may desire within the realm of african native healing, the spirits and the ancestors. I am a sangoma and healer. I could help you to connect with the ancestors , interpret dreams, diagnose illness through divination with bones, and help you heal both physical and spiritual illness. We facilitate the deepening of your relationship to the spirit world and the ancestors. Working in partnership with one\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’s ancestors is a gift representing a close link with the spirit realm as a mediator between the worlds.*   Witchdoctors, or sorcerers, are often purveyors of mutis and charms that cause harm to people. we believe that we are here for only one purpose, to heal through love and compassion.*   African people share a common understanding of the importance of ancestors in daily life. When they have lost touch with their ancestors, illness may result or bad luck. Then a traditional healer, or sangoma, is sought out who may prescribe herbs, changes in lifestyle, a career change, or changes in relationships. The client may also be told to perform a ceremony or purification ritual to appease the ancestors.*   Let us solve your problems using powerful African traditional methods. We believe that our ancestors and spirits give us enlightenment, wisdom, divine guidance, enabling us to overcome obstacles holding your life back. Our knowledge has been passed down through centuries, being refined along the way from generation to generation. We believe in the occult, the paranormal, the spirit world, the mystic world.*   The services here are based on the African Tradition Value system/religion,where we believe the ancestors and spirits play a very important role in society. The ancestors and spirits give guidance and counsel in society. They could enable us to see into the future and give solutions to the problems affecting us. We use rituals, divination, spells, chants and prayers to enable us tackle the task before us.*   I have experience in helping and guiding many people from all over the world. My psychic abilities may help you answer and resolve many unanswered questions
    • Let me try and help you with love spells, traditional healing, native healing, fortune telling, witchcraft, psychic readings, black magic, voodoo, herbalist healing, or any other service your may desire within the realm of african native healing, the spirits and the ancestors. I am a sangoma and healer. I could help you to connect with the ancestors , interpret dreams, diagnose illness through divination with bones, and help you heal both physical and spiritual illness. We facilitate the deepening of your relationship to the spirit world and the ancestors. Working in partnership with one\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’s ancestors is a gift representing a close link with the spirit realm as a mediator between the worlds.*   Witchdoctors, or sorcerers, are often purveyors of mutis and charms that cause harm to people. we believe that we are here for only one purpose, to heal through love and compassion.*   African people share a common understanding of the importance of ancestors in daily life. When they have lost touch with their ancestors, illness may result or bad luck. Then a traditional healer, or sangoma, is sought out who may prescribe herbs, changes in lifestyle, a career change, or changes in relationships. The client may also be told to perform a ceremony or purification ritual to appease the ancestors.*   Let us solve your problems using powerful African traditional methods. We believe that our ancestors and spirits give us enlightenment, wisdom, divine guidance, enabling us to overcome obstacles holding your life back. Our knowledge has been passed down through centuries, being refined along the way from generation to generation. We believe in the occult, the paranormal, the spirit world, the mystic world.*   The services here are based on the African Tradition Value system/religion,where we believe the ancestors and spirits play a very important role in society. The ancestors and spirits give guidance and counsel in society. They could enable us to see into the future and give solutions to the problems affecting us. We use rituals, divination, spells, chants and prayers to enable us tackle the task before us.*   I have experience in helping and guiding many people from all over the world. My psychic abilities may help you answer and resolve many unanswered questions
    • Let me try and help you with love spells, traditional healing, native healing, fortune telling, witchcraft, psychic readings, black magic, voodoo, herbalist healing, or any other service your may desire within the realm of african native healing, the spirits and the ancestors. I am a sangoma and healer. I could help you to connect with the ancestors , interpret dreams, diagnose illness through divination with bones, and help you heal both physical and spiritual illness. We facilitate the deepening of your relationship to the spirit world and the ancestors. Working in partnership with one\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’s ancestors is a gift representing a close link with the spirit realm as a mediator between the worlds.*   Witchdoctors, or sorcerers, are often purveyors of mutis and charms that cause harm to people. we believe that we are here for only one purpose, to heal through love and compassion.*   African people share a common understanding of the importance of ancestors in daily life. When they have lost touch with their ancestors, illness may result or bad luck. Then a traditional healer, or sangoma, is sought out who may prescribe herbs, changes in lifestyle, a career change, or changes in relationships. The client may also be told to perform a ceremony or purification ritual to appease the ancestors.*   Let us solve your problems using powerful African traditional methods. We believe that our ancestors and spirits give us enlightenment, wisdom, divine guidance, enabling us to overcome obstacles holding your life back. Our knowledge has been passed down through centuries, being refined along the way from generation to generation. We believe in the occult, the paranormal, the spirit world, the mystic world.*   The services here are based on the African Tradition Value system/religion,where we believe the ancestors and spirits play a very important role in society. The ancestors and spirits give guidance and counsel in society. They could enable us to see into the future and give solutions to the problems affecting us. We use rituals, divination, spells, chants and prayers to enable us tackle the task before us.*   I have experience in helping and guiding many people from all over the world. My psychic abilities may help you answer and resolve many unanswered questions
    • Let me try and help you with love spells, traditional healing, native healing, fortune telling, witchcraft, psychic readings, black magic, voodoo, herbalist healing, or any other service your may desire within the realm of african native healing, the spirits and the ancestors. I am a sangoma and healer. I could help you to connect with the ancestors , interpret dreams, diagnose illness through divination with bones, and help you heal both physical and spiritual illness. We facilitate the deepening of your relationship to the spirit world and the ancestors. Working in partnership with one\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\’s ancestors is a gift representing a close link with the spirit realm as a mediator between the worlds.*   Witchdoctors, or sorcerers, are often purveyors of mutis and charms that cause harm to people. we believe that we are here for only one purpose, to heal through love and compassion.*   African people share a common understanding of the importance of ancestors in daily life. When they have lost touch with their ancestors, illness may result or bad luck. Then a traditional healer, or sangoma, is sought out who may prescribe herbs, changes in lifestyle, a career change, or changes in relationships. The client may also be told to perform a ceremony or purification ritual to appease the ancestors.*   Let us solve your problems using powerful African traditional methods. We believe that our ancestors and spirits give us enlightenment, wisdom, divine guidance, enabling us to overcome obstacles holding your life back. Our knowledge has been passed down through centuries, being refined along the way from generation to generation. We believe in the occult, the paranormal, the spirit world, the mystic world.*   The services here are based on the African Tradition Value system/religion,where we believe the ancestors and spirits play a very important role in society. The ancestors and spirits give guidance and counsel in society. They could enable us to see into the future and give solutions to the problems affecting us. We use rituals, divination, spells, chants and prayers to enable us tackle the task before us.*   I have experience in helping and guiding many people from all over the world. My psychic abilities may help you answer and resolve many unanswered questions
  • Topics

×
×
  • Create New...

Important Information

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