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

    • Instale el mod en mi servidor y este se queda iniciando por horas, en la consola me habla de algo de permisos y trate de seguir la ruta que me da pero no existe.   [13:05:32 INFO]: CustomNPC Permissions available: [13:05:32 INFO]: customnpcs.edit.blocks [13:05:32 INFO]: customnpcs.edit.villager [13:05:32 INFO]: customnpcs.global.bank [13:05:32 INFO]: customnpcs.global.dialog [13:05:32 INFO]: customnpcs.global.faction [13:05:32 INFO]: customnpcs.global.linked [13:05:32 INFO]: customnpcs.global.naturalspawn [13:05:32 INFO]: customnpcs.global.playerdata [13:05:32 INFO]: customnpcs.global.quest [13:05:32 INFO]: customnpcs.global.recipe [13:05:32 INFO]: customnpcs.global.transport [13:05:32 INFO]: customnpcs.npc.advanced [13:05:32 INFO]: customnpcs.npc.ai [13:05:32 INFO]: customnpcs.npc.clone [13:05:32 INFO]: customnpcs.npc.create [13:05:32 INFO]: customnpcs.npc.delete [13:05:32 INFO]: customnpcs.npc.display [13:05:32 INFO]: customnpcs.npc.freeze [13:05:32 INFO]: customnpcs.npc.gui [13:05:32 INFO]: customnpcs.npc.inventory [13:05:32 INFO]: customnpcs.npc.reset [13:05:32 INFO]: customnpcs.npc.stats [13:05:32 INFO]: customnpcs.scenes [13:05:32 INFO]: customnpcs.soulstone.all [13:05:32 INFO]: customnpcs.spawner.create [13:05:32 INFO]: customnpcs.spawner.mob [13:05:32 INFO]: customnpcs.tool.mounter [13:05:32 INFO]: customnpcs.tool.nbtbook [13:05:32 INFO]: customnpcs.tool.pather [13:05:32 INFO]: customnpcs.tool.scripter
    • I have created a very simple mod that is just supposed to send a message to a player when they join. Upon adding it to a server (that has other mods on it), the following crash message appears: [12:13:01] [main/ERROR] [minecraft/Main]: Failed to start the minecraft server net.minecraftforge.fml.LoadingFailedException: Loading errors encountered: [         Epic Mod (epicmod) has failed to load correctly §7java.lang.NoSuchMethodException: net.ed.epicmod.EpicMod.<init>() ]         at net.minecraftforge.fml.ModLoader.waitForTransition(ModLoader.java:243) ~[fmlcore-1.19.2-43.2.14.jar%23548!/:?] {}         at net.minecraftforge.fml.ModLoader.lambda$dispatchAndHandleError$24(ModLoader.java:208) ~[fmlcore-1.19.2-43.2.14.jar%23548!/:?] {}         at java.util.Optional.ifPresent(Optional.java:178) ~[?:?] {re:mixin}         at net.minecraftforge.fml.ModLoader.dispatchAndHandleError(ModLoader.java:208) ~[fmlcore-1.19.2-43.2.14.jar%23548!/:?] {}         at net.minecraftforge.fml.ModLoader.lambda$gatherAndInitializeMods$14(ModLoader.java:185) ~[fmlcore-1.19.2-43.2.14.jar%23548!/:?] {}         at java.lang.Iterable.forEach(Iterable.java:75) ~[?:?] {re:mixin,re:computing_frames}         at net.minecraftforge.fml.ModLoader.gatherAndInitializeMods(ModLoader.java:185) ~[fmlcore-1.19.2-43.2.14.jar%23548!/:?] {}         at net.minecraftforge.server.loading.ServerModLoader.load(ServerModLoader.java:32) ~[forge-1.19.2-43.2.14-universal.jar%23552!/:?] {re:classloading}         at net.minecraft.server.Main.main(Main.java:113) ~[server-1.19.2-20220805.130853-srg.jar%23547!/:?] {re:classloading,re:mixin,pl:mixin:A}         at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?] {}         at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[?:?] {}         at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?] {}         at java.lang.reflect.Method.invoke(Method.java:568) ~[?:?] {}         at net.minecraftforge.fml.loading.targets.CommonServerLaunchHandler.lambda$launchService$0(CommonServerLaunchHandler.java:29) ~[fmlloader-1.19.2-43.2.14.jar%2367!/:?] {}         at cpw.mods.modlauncher.LaunchServiceHandlerDecorator.launch(LaunchServiceHandlerDecorator.java:30) [modlauncher-10.0.8.jar%2354!/:?] {}         at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:53) [modlauncher-10.0.8.jar%2354!/:?] {}         at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:71) [modlauncher-10.0.8.jar%2354!/:?] {}         at cpw.mods.modlauncher.Launcher.run(Launcher.java:106) [modlauncher-10.0.8.jar%2354!/:?] {}         at cpw.mods.modlauncher.Launcher.main(Launcher.java:77) [modlauncher-10.0.8.jar%2354!/:?] {}         at cpw.mods.modlauncher.BootstrapLaunchConsumer.accept(BootstrapLaunchConsumer.java:26) [modlauncher-10.0.8.jar%2354!/:?] {}         at cpw.mods.modlauncher.BootstrapLaunchConsumer.accept(BootstrapLaunchConsumer.java:23) [modlauncher-10.0.8.jar%2354!/:?] {}         at cpw.mods.bootstraplauncher.BootstrapLauncher.main(BootstrapLauncher.java:141) [bootstraplauncher-1.1.2.jar:?] {} Why could this be? I have tried using the mod on another forge server with only this mod installed and it works there. Is my mod somehow interfering with other mods? MC version is 1.19.2
    • how to make animated doors?, maybe geckolib, but i don't know how to code it?
    • For crash 1, set max-tick-time to -1 in your server.properties Crash 2 shows a conflict or incompatibility between LuckPerms and the mod boh-0.0.6.1-forge-1.20.1_2.jar
  • Topics

×
×
  • Create New...

Important Information

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