Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[Solved] Limiting what goes in inventory slots


defiant810
 Share

Recommended Posts

Hey there, quick question:

 

How does one block certain items from going into an inventory (like how the vanilla Furnace doesn't allow non-fuels in the bottom slot)?

 

Right now this is my code in my tile entity:

@Override
public void setInventorySlotContents(int slot, ItemStack toPut)
{
	ItemStack old = getStackInSlot(slot);

	if (toPut != null && (toPut.itemID == Item.stick.shiftedIndex || toPut.itemID == Item.pickaxeWood.shiftedIndex 
			|| toPut.itemID == Item.shovelWood.shiftedIndex))
	{
		inventory[slot] = toPut;

		if (toPut.itemID == Item.stick.shiftedIndex) 
		{
			capsuleContained = CapsuleType.LOW;
		} 
		else if (toPut.itemID == Item.pickaxeWood.shiftedIndex) 
		{
			capsuleContained = CapsuleType.MEDIUM;
		}
		else if (toPut.itemID == Item.shovelWood.shiftedIndex)
		{
			capsuleContained = CapsuleType.HIGH;
		}

		if(toPut.stackSize > getInventoryStackLimit())
		{
			toPut.stackSize = getInventoryStackLimit();
		}
	}
	else if (toPut == null)
	{
		inventory[slot] = toPut;
		capsuleContained = CapsuleType.NONE;
	}
	else
	{
		//It eats other items...
	}
}

 

As of right now, the inventory (of only one slot) only allows sticks, wooden picks, and wooden shovels (testing items). When I try to put other things in, instead of doing nothing, the slot remains empty but the original item disappears. How can I make it so that item doesn't disappear, and just stays in the player's cursor?

 

And yes, I've already looked at the furnace code, it didn't help.

Creator and co-author of the mod Essencraft: https://github.com/defiant810/Essencraft

 

Please, dear God, learn Java before trying to mod. It saves ALL of us time.

Link to comment
Share on other sites

The vanilla furnace allows non-fuels in the bottom slot, it just doesn't burn them when it's time to get new fuel.

 

There would have to be a function elsewhere that determines what can/can't be put in to a slot, before that function gets called. It seems that setInventorySlotContents happens after the testing, whereever that could be.

Link to comment
Share on other sites

Hmmm... OK, that makes sense.

 

Does anyone out there know if there is a method that defines what can and can't be put into certain slots? Or another way to solve this problem?

Creator and co-author of the mod Essencraft: https://github.com/defiant810/Essencraft

 

Please, dear God, learn Java before trying to mod. It saves ALL of us time.

Link to comment
Share on other sites

I looked through the same two classes and came up with the same results. I guess the dropping the inventory solution will be good temporarily until I figure out how to block items from slots, if it is possible.

Creator and co-author of the mod Essencraft: https://github.com/defiant810/Essencraft

 

Please, dear God, learn Java before trying to mod. It saves ALL of us time.

Link to comment
Share on other sites

So I tried making a method that takes the item if it is invalid, sets the slot to null, and ejects the item into the world.

 

Good news: it works.

Bad news: it works twice.

 

Whenever it ejects the ItemStack, it makes one legit version that can be picked up, and then it also ejects a ghost version of the same stack that cant be picked up; it just sits there and mocks you.

 

Here is the TileEntity code:

@Override
public void setInventorySlotContents(int slot, ItemStack toPut)
{
	inventory[slot] = toPut;

	if(toPut != null)
	{
		if(toPut.itemID == Item.stick.shiftedIndex)
		{
			this.capsuleContained = CapsuleType.LOW;
		}
		else if(toPut.itemID == Item.pickaxeWood.shiftedIndex)
		{
			this.capsuleContained = CapsuleType.MEDIUM;	
		}
		else if(toPut.itemID == Item.shovelWood.shiftedIndex)
		{
			this.capsuleContained = CapsuleType.HIGH;
		}
		else
		{
			capsuleContained = CapsuleType.NONE;
			checkSlot(slot);
		}
	}
	else
	{
		capsuleContained = CapsuleType.NONE;
	}

	if(toPut != null && toPut.stackSize > getInventoryStackLimit())
	{
		toPut.stackSize = getInventoryStackLimit();
	}
	}

@Override
protected void checkSlot(int slot)
{
	ItemStack contents = getStackInSlot(slot);

	boolean valid = (contents.itemID == Item.stick.shiftedIndex || 
		contents.itemID == Item.pickaxeWood.shiftedIndex ||
		contents.itemID == Item.shovelWood.shiftedIndex);

	if(!valid)
	{
		inventory[slot] = null;
		dropItem(contents);
	}
}

 

And here is the dropItem method from the super class:

protected void dropItem(ItemStack contents)
{
	Random rand = new Random();

	float rx = rand.nextFloat() * 0.8F + 0.1F;
	float ry = rand.nextFloat() * 0.8F + 0.1F;
	float rz = rand.nextFloat() * 0.8F + 0.1F;

	EntityItem entity = new EntityItem(this.getWorldObj(), this.xCoord + rx, this.yCoord + ry,
		this.zCoord + rz, new ItemStack(contents.itemID, contents.stackSize, contents.getItemDamage()));

	if(contents.hasTagCompound())
	{
		entity.item.setTagCompound((NBTTagCompound)contents.getTagCompound().copy());
	}

	entity.motionX = rand.nextGaussian() * 0.8f;
	entity.motionY = rand.nextGaussian() * 0.8f;
	entity.motionZ = rand.nextGaussian() * 0.8f;
	this.getWorldObj().spawnEntityInWorld(entity);
}

 

I don't see where it could be creating two stacks. This is (almost) the same code I use to eject the items out of my other blocks, and that works just fine.

 

EDIT:

 

Here are the two files that are being used here:

https://github.com/SeanMoss/QuantumCraft/tree/master/src/common/quantumcraft/common/tileentity

Creator and co-author of the mod Essencraft: https://github.com/defiant810/Essencraft

 

Please, dear God, learn Java before trying to mod. It saves ALL of us time.

Link to comment
Share on other sites

The dupe item would be a client-side spawned item.  Make sure in your code that spawns the entityItem, that it is only called server-side.

 

For custom inventory validation--I used a custom slot and used the isItemValidForSlot method in the slot.  However, I had to use some workarounds in order to get it to work while shift-clicking items. (normal transfer is easy with the slot method).

 

Using this method, I am able to sucessfully tell a 12 slot inventory that it can only accept certain items in slots 0-5, different items in 6-8, and finally a different set it slots 9-11.

 

I can provide links to source if needed.

Link to comment
Share on other sites

A link to source would be massively appreciated. And how would one make sure that the item spawn only happens on the server? Would the @Sided annotation do that, ir does it need to happen some other way?

 

Creator and co-author of the mod Essencraft: https://github.com/defiant810/Essencraft

 

Please, dear God, learn Java before trying to mod. It saves ALL of us time.

Link to comment
Share on other sites

if(world.isRemote){

EntityItem entity = new EntityItem(this.getWorldObj(), this.xCoord + rx, this.yCoord + ry,

    this.zCoord + rz, new ItemStack(contents.itemID, contents.stackSize, contents.getItemDamage()));

}

 

What he meant was

if (!world.isRemote){
EntityItem entity = new EntityItem(this.getWorldObj(), this.xCoord + rx, this.yCoord + ry,
    this.zCoord + rz, new ItemStack(contents.itemID, contents.stackSize, contents.getItemDamage()));
}

(you spawn server-side, not client-side)

Protip: try and find answers yourself before asking on the forum.

It's pretty likely that there is an answer.

 

Was I helpful? Give me a thank you!

 

 

width=635 height=903http://bit.ly/HZ03zy[/img]

 

 

Tired of waiting for mods to port to bukkit?

use BukkitForge! (now with a working version of WorldEdit!)

Link to comment
Share on other sites

*Facepalm* I completely forgot about the world.isRemote flag.

 

And that appears to have fixed my error, there are no longer any ghost ItemStacks appearing.

 

Thanks to everyone who helped.

Creator and co-author of the mod Essencraft: https://github.com/defiant810/Essencraft

 

Please, dear God, learn Java before trying to mod. It saves ALL of us time.

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
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.

 Share



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Ok, I am already using java 17 but for resolve the error I uninstalled java development 19. Thanks for the help!
    • Try the latest version of the dungeon crawl. If that doesn't fix it, contact the mod author. https://www.curseforge.com/minecraft/mc-mods/dungeon-crawl/files/all?filter-game-version=1738749986%3a73407
    • Mixin doesn't like java 19 use java 17
    • Everytime When I Try To Launch My Forge Minecraft Server It Crashes I Don't Really Know What To Do Here Is the Crash Log: ---- Minecraft Crash Report ---- WARNING: coremods are present:   TransformLoader (DynamicSurroundings-1.12.2-3.6.1.0.jar)   LoadingPlugin (Bloodmoon-MC1.12.2-1.5.3 (1).jar)   RandomPatches (randompatches-1.12.2-1.22.1.10.jar)   PhosphorFMLLoadingPlugin (phosphor-1.12.2-0.2.6+build50-universal.jar)   CreativePatchingLoader (CreativeCore_v1.10.70_mc1.12.2.jar) Contact their authors BEFORE contacting forge // This doesn't make any sense! Time: 9/25/22 10:40 PM Description: Exception in server tick loop java.lang.IllegalArgumentException: ChunkNibbleArrays should be 2048 bytes not: 0     at net.minecraft.world.chunk.NibbleArray.<init>(SourceFile:16)     at net.minecraft.world.chunk.storage.AnvilChunkLoader.func_75823_a(AnvilChunkLoader.java:450)     at net.minecraft.world.chunk.storage.AnvilChunkLoader.checkedReadChunkFromNBT__Async(AnvilChunkLoader.java:129)     at net.minecraft.world.chunk.storage.AnvilChunkLoader.loadChunk__Async(AnvilChunkLoader.java:93)     at net.minecraftforge.common.chunkio.ChunkIOProvider.run(ChunkIOProvider.java:70)     at net.minecraftforge.common.chunkio.ChunkIOExecutor.syncChunkLoad(ChunkIOExecutor.java:92)     at net.minecraft.world.gen.ChunkProviderServer.loadChunk(ChunkProviderServer.java:118)     at net.minecraft.world.gen.ChunkProviderServer.func_186028_c(ChunkProviderServer.java:89)     at net.minecraft.world.gen.ChunkProviderServer.func_186025_d(ChunkProviderServer.java:135)     at net.minecraft.server.MinecraftServer.func_71222_d(MinecraftServer.java:344)     at net.minecraft.server.MinecraftServer.func_71247_a(MinecraftServer.java:314)     at net.minecraft.server.dedicated.DedicatedServer.func_71197_b(DedicatedServer.java:270)     at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:486)     at java.lang.Thread.run(Unknown Source) A detailed walkthrough of the error, its code path and all known details is as follows: --------------------------------------------------------------------------------------- -- System Details -- Details:     Minecraft Version: 1.12.2     Operating System: Windows 10 (amd64) version 10.0     Java Version: 1.8.0_341, Oracle Corporation     Java VM Version: Java HotSpot(TM) 64-Bit Server VM (mixed mode), Oracle Corporation     Memory: 4739810688 bytes (4520 MB) / 6174015488 bytes (5888 MB) up to 6174015488 bytes (5888 MB)     JVM Flags: 2 total; -Xmx6144M -Xms6144M     IntCache: cache: 0, tcache: 0, allocated: 0, tallocated: 0     FML: MCP 9.42 Powered by Forge 14.23.5.2860 33 mods loaded, 33 mods active     States: 'U' = Unloaded 'L' = Loaded 'C' = Constructed 'H' = Pre-initialized 'I' = Initialized 'J' = Post-initialized 'A' = Available 'D' = Disabled 'E' = Errored     | State  | ID                | Version          | Source                                      | Signature                                |     |:------ |:----------------- |:---------------- |:------------------------------------------- |:---------------------------------------- |     | LCHIJA | minecraft         | 1.12.2           | minecraft.jar                               | None                                     |     | LCHIJA | mcp               | 9.42             | minecraft.jar                               | None                                     |     | LCHIJA | FML               | 8.0.99.99        | forge-1.12.2-14.23.5.2860.jar               | e3c3d50c7c986df74c645c0ac54639741c90a557 |     | LCHIJA | forge             | 14.23.5.2860     | forge-1.12.2-14.23.5.2860.jar               | e3c3d50c7c986df74c645c0ac54639741c90a557 |     | LCHIJA | creativecoredummy | 1.0.0            | minecraft.jar                               | None                                     |     | LCHIJA | randompatches     | 1.12.2-1.22.1.10 | randompatches-1.12.2-1.22.1.10.jar          | None                                     |     | LCHIJA | aiimprovements    | 0.0.1.3          | AIImprovements-1.12-0.0.1b3.jar             | None                                     |     | LCHIJA | badmobs           | 1.1.40           | BadMobs-1.12.2-1.1.40 (1).jar               | d476d1b22b218a10d845928d1665d45fce301b27 |     | LCHIJA | bloodmoon         | 1.5.3            | Bloodmoon-MC1.12.2-1.5.3 (1).jar            | d72e0dd57935b3e9476212aea0c0df352dd76291 |     | LCHIJA | chunkpregenerator | 2.5.0            | Chunk+Pregenerator-V1.12-2.5.7.jar          | None                                     |     | LCHIJA | extendedrenderer  | v1.0             | coroutil-1.12.1-1.2.37 (2).jar              | None                                     |     | LCHIJA | coroutil          | 1.12.1-1.2.37    | coroutil-1.12.1-1.2.37 (2).jar              | None                                     |     | LCHIJA | configmod         | v1.0             | coroutil-1.12.1-1.2.37 (2).jar              | None                                     |     | LCHIJA | crafttweaker      | 4.1.20           | CraftTweaker2-1.12-4.1.20.680.jar           | None                                     |     | LCHIJA | crafttweakerjei   | 2.0.3            | CraftTweaker2-1.12-4.1.20.680.jar           | None                                     |     | LCHIJA | creativecore      | 1.10.0           | CreativeCore_v1.10.70_mc1.12.2.jar          | None                                     |     | LCHIJA | orelib            | 3.6.0.1          | OreLib-1.12.2-3.6.0.1 (1).jar               | 7a2128d395ad96ceb9d9030fbd41d035b435753a |     | LCHIJA | dsurround         | 3.6.1.0          | DynamicSurroundings-1.12.2-3.6.1.0.jar      | 7a2128d395ad96ceb9d9030fbd41d035b435753a |     | LCHIJA | dynview           | 1.3              | dynviewdist-1.12-1.3.jar                    | None                                     |     | LCHIJA | enhancedvisuals   | 1.3.0            | EnhancedVisuals_v1.4.4_mc1.12.2 (1).jar     | None                                     |     | LCHIJA | epicsiegemod      | 13.169           | EpicSiegeMod-13.169 (1).jar                 | None                                     |     | LCHIJA | gravestone        | 1.10.3           | gravestone-1.10.3.jar                       | None                                     |     | LCHIJA | ichunutil         | 7.2.2            | iChunUtil-1.12.2-7.2.2.jar                  | 4db5c2bd1b556f252a5b8b54b256d381b2a0a6b8 |     | LCHIJA | mts               | 22.1.0           | Immersive+Vehicles-1.12.2-22.1.0.jar        | None                                     |     | LCHIJA | incontrol         | 3.9.8            | incontrol-1.12-3.9.8.jar                    | None                                     |     | LCHIJA | mobends           | 1.2.1            | MoBends_1.12.2-1.2.1-19.12.21.jar           | None                                     |     | LCHIJA | mobsunscreen      | @version@        | mobsunscreen-1.12.2-3.1.5.jar               | None                                     |     | LCHIJA | performant        | 1.12.2-1.5       | performant-1.11.jar                         | None                                     |     | LCHIJA | roughmobs         | 2.0.0            | roughmobs2-1.12.2_2.0.15.jar                | None                                     |     | LCHIJA | gvc               | 1.2.5            | VoiceChatReloaded-1.12.2-1.3.0.jar          | None                                     |     | LCHIJA | worldedit         | 6.1.10           | worldedit-forge-mc1.12.2-6.1.10-dist.jar    | None                                     |     | LCHIJA | zombieawareness   | 1.12.1-1.11.16   | zombieawareness-1.12.1-1.11.16 (1).jar      | None                                     |     | LCHIJA | phosphor-lighting | 1.12.2-0.2.6     | phosphor-1.12.2-0.2.6+build50-universal.jar | f0387d288626cc2d937daa504e74af570c52a2f1 |     Loaded coremods (and transformers):  TransformLoader (DynamicSurroundings-1.12.2-3.6.1.0.jar)    LoadingPlugin (Bloodmoon-MC1.12.2-1.5.3 (1).jar)   lumien.bloodmoon.asm.ClassTransformer RandomPatches (randompatches-1.12.2-1.22.1.10.jar)   com.therandomlabs.randompatches.core.RPTransformer PhosphorFMLLoadingPlugin (phosphor-1.12.2-0.2.6+build50-universal.jar)    CreativePatchingLoader (CreativeCore_v1.10.70_mc1.12.2.jar)        Profiler Position: N/A (disabled)     Player Count: 0 / 20; []     Is Modded: Definitely; Server brand changed to 'fml,forge'     Type: Dedicated Server (map_server.txt)   Please Help Me With This Crash Report Thankkyou    
  • Topics

×
×
  • Create New...

Important Information

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