Jump to content

[1.8] Current held item into inventory on rightclick?


ModernPatriot

Recommended Posts

So Im currently trying to make a block with an internal inventory which takes the current held item/block of a player on rightclick (so kinda like a pedestal). How can I do that? My code is already checking the players current item as soon as he right clicks but how can I delete his current held item/block and put it into the blocks' inventory? And how can I make it so, that the current item in inventory is rendered like it was dropped ontop of the block?

 

Thanks! :)

 

BasicGearbox.java onBlockActivated:

 

 

 

@Override

public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ)

    {

if(playerIn.getCurrentEquippedItem() != null) {

ItemStack itemHolding = playerIn.getCurrentEquippedItem();

 

 

}

 

return true;

    }

 

Link to comment
Share on other sites

Difficulty:

* Logical (6/10)

* Rendering (7/10)

* Don't even start this if you don't know Java enough.

 

Requirements: (learn to use)

* TileEntity (TE) - You don't need ticking one.

* IInventory - implement this to your TE to hold inventory.

* NBT - Learn how to store stack to TE's NBT.

* SimpleNewtorkWrapper (SNW) - When you put ItemStack into TileEntity, you will need to do that on server logical side. Since your and other clients need to know that this ItemStack is inside TileEntity (to render it), you will need to learn how to use packets that will update client's ItemStack field.

* TileEntitySpecialRenderer (TESR) - you can assign special (very powerful) renderer to TEs. That allows you to literally render anything in place of TE's Block.

Note: You might be able to use Block models to render it (without TESR), but that is STRONG "might be", I don't think it would be good idea to dig there.

 

Well, Your block class is a start, After getting item, you neeed to get TileEntity in given BlockPos you clicked at and inform TileEntity that you clicked it, then on server you will remove ItemStack from inv and move it to TE's inventry, then send packet to client around about change, receivers will use TESR to render ItemStack "onto" block.

 

EDIT

I ment to ask - what parts you EVER encountered? I will post tuts/hints on others (my guess is that you are new to modding at this point) :D.

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

Link to comment
Share on other sites

I actually need a ticking one because the block is also needed for some multiblocks ;)

(Now that I'm thinking about it - wouldnt it be more efficient to use 2 blocks - one if formed and the other if the multiblock needs to be formed - because I only need the rightclick function when one is formed)

 

Im already using the TESR for that block, so that shouldnt be a problem, but how can I assign the itemstacks' model?

 

Actually all of them :P Im struggling with the TESR though, maybe because I hate vertexes... Im kinda new to 1.8 modding (about 3 months) but I already worked on some smaller mods (about) three years ago.

Link to comment
Share on other sites

Difficulty:

* Logical (6/10)

* Rendering (7/10)

* Don't even start this if you don't know Java enough.

 

Requirements: (learn to use)

* TileEntity (TE) - You don't need ticking one.

* IInventory - implement this to your TE to hold inventory.

* NBT - Learn how to store stack to TE's NBT.

* SimpleNewtorkWrapper (SNW) - When you put ItemStack into TileEntity, you will need to do that on server logical side. Since your and other clients need to know that this ItemStack is inside TileEntity (to render it), you will need to learn how to use packets that will update client's ItemStack field.

* TileEntitySpecialRenderer (TESR) - you can assign special (very powerful) renderer to TEs. That allows you to literally render anything in place of TE's Block.

Note: You might be able to use Block models to render it (without TESR), but that is STRONG "might be", I don't think it would be good idea to dig there.

 

Well, Your block class is a start, After getting item, you neeed to get TileEntity in given BlockPos you clicked at and inform TileEntity that you clicked it, then on server you will remove ItemStack from inv and move it to TE's inventry, then send packet to client around about change, receivers will use TESR to render ItemStack "onto" block.

 

EDIT

I ment to ask - what parts you EVER encountered? I will post tuts/hints on others (my guess is that you are new to modding at this point) :D.

 

Do you have any tutorials regarding the TESR and how to use the SimpleNetworkWrapper in this case? I already got the basics (Inventory etc) but I still need to render the Item in the Inventory and send the packets.

Link to comment
Share on other sites

So Ive been messing around a bit. Is this correct so far?

 

 

 

    @Override

    public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ)

    {

if (worldIn.isRemote)

    {

        return true;

    }else{

if(playerIn.getCurrentEquippedItem() != null) {

BasicGearboxData bgd = (BasicGearboxData) worldIn.getTileEntity(pos);

if(bgd.getStackInSlot(0) == null) {

ItemStack itemHolding = playerIn.getCurrentEquippedItem();

// SEND INFO TO SERVER AND PUT ITEM INTO INVENTORY

}

}

return true;

}

}

 

Link to comment
Share on other sites

@Override
public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ)
{
	if (worldIn.isRemote) return true;
	else
	{
		ItemStack stackHeld = playerIn.getCurrentEquippedItem();
		if (stackHeld != null)
		{
			BasicGearboxData bgd = (BasicGearboxData) worldIn.getTileEntity(pos);
			if (bgd.attemptPuttingStack(stackHeld)) // return true if succesfull
			{
				playerIn. // setHeld stack to null
			}
		}
		return true;
	}
}

Now in TE, you want a method that will check if you can put given stack into some slot. If you succeed then you will send packet from TE's method (e.g: #attemptPuttingStack)

 

Note that if you are messing with player inventory on ONLY server side, client might not get synchronized - you will need to mark player's inventory dirty.

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

Link to comment
Share on other sites

That's what I came up with:

(BasicGearboxData is the Inventory + TileEntity class)

    @Override
    public boolean onBlockActivated(World worldIn, BlockPos pos, IBlockState state, EntityPlayer playerIn, EnumFacing side, float hitX, float hitY, float hitZ)
    {
	if (!worldIn.isRemote){
		BasicGearboxData bgd = (BasicGearboxData) worldIn.getTileEntity(pos);
		if(bgd.getStackInSlot(0) == null) {
			if(playerIn.getCurrentEquippedItem() != null) {
				ItemStack itemHolding = playerIn.getCurrentEquippedItem();
                                        int meta = playerIn.getCurrentEquippedItem().getMetadata();
				bgd.setInventorySlotContents(0, new ItemStack(itemHolding.getItem(), 1, meta));
				playerIn.getCurrentEquippedItem().stackSize = playerIn.getCurrentEquippedItem().stackSize - 1;
				shouldPutItemInInventory = true;
				itemHolding = null;
		}
	}if(playerIn.getCurrentEquippedItem() == null){
		shouldRemoveItemFromInventory = true;
	}
        return true;
    }if(worldIn.isRemote) {
    	if(shouldPutItemInInventory){
            if(bgd.getStackInSlot(0) == null) {
    		BasicGearboxData bgd = (BasicGearboxData) worldIn.getTileEntity(pos);
    		ItemStack itemHolding = playerIn.getCurrentEquippedItem();
    		bgd.setInventorySlotContents(0, new ItemStack(itemHolding.getItem(), 1));
    		shouldPutItemInInventory = false;
    		itemHolding = null;
                }
             }
    	if(shouldRemoveItemFromInventory) {
             if(bgd.getStackInSlot(0) != null) {
    		BasicGearboxData bgd = (BasicGearboxData) worldIn.getTileEntity(pos);
    		ItemStack inSlot = bgd.getStackInSlot(0);
    		playerIn.getCurrentEquippedItem().setItem(inSlot.getItem());
    		bgd.setInventorySlotContents(0, null);
    		inSlot = null;
    		shouldRemoveItemFromInventory = false;
        }
    	}
}
return true;
}

 

The first part seems to work but the second part (getting the item out of the inventory) seems to crash.

 

java.lang.NullPointerException

at com.modernpatriot.reimaginedindustries.blocks.BasicGearbox.onBlockActivated(BasicGearbox.java:60) ~[basicGearbox.class:?]

at net.minecraft.client.multiplayer.PlayerControllerMP.func_178890_a(PlayerControllerMP.java:416) ~[PlayerControllerMP.class:?]

at net.minecraft.client.Minecraft.rightClickMouse(Minecraft.java:1572) ~[Minecraft.class:?]

at net.minecraft.client.Minecraft.runTick(Minecraft.java:2132) ~[Minecraft.class:?]

at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:1088) ~[Minecraft.class:?]

at net.minecraft.client.Minecraft.run(Minecraft.java:376) [Minecraft.class:?]

at net.minecraft.client.main.Main.main(Main.java:117) [Main.class:?]

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_79]

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[?:1.7.0_79]

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.7.0_79]

at java.lang.reflect.Method.invoke(Method.java:606) ~[?:1.7.0_79]

at net.minecraft.launchwrapper.Launch.launch(Launch.java:135) [launchwrapper-1.11.jar:?]

at net.minecraft.launchwrapper.Launch.main(Launch.java:28) [launchwrapper-1.11.jar:?]

at net.minecraftforge.gradle.GradleStartCommon.launch(Unknown Source) [start/:?]

at GradleStart.main(Unknown Source) [start/:?]

[07:43:18] [Client thread/INFO] [sTDOUT]: [net.minecraft.init.Bootstrap:printToSYSOUT:663]: ---- Minecraft Crash Report ----

 

WARNING: coremods are present:

  CCCDeobfPlugin (unknown)

  NEICorePlugin (NotEnoughItems-1.8-1.0.5.82-universal.jar)

  CCLCorePlugin (CodeChickenLib-1.8-1.1.2.133-dev.jar)

  CodeChickenCorePlugin (CodeChickenCore-1.8-1.0.5.36-universal.jar)

Contact their authors BEFORE contacting forge

 

// Why did you do that?

 

Time: 02.08.15 07:43

Description: Unexpected error

 

java.lang.NullPointerException: Unexpected error

at com.modernpatriot.reimaginedindustries.blocks.BasicGearbox.onBlockActivated(BasicGearbox.java:60)

at net.minecraft.client.multiplayer.PlayerControllerMP.func_178890_a(PlayerControllerMP.java:416)

at net.minecraft.client.Minecraft.rightClickMouse(Minecraft.java:1572)

at net.minecraft.client.Minecraft.runTick(Minecraft.java:2132)

at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:1088)

at net.minecraft.client.Minecraft.run(Minecraft.java:376)

at net.minecraft.client.main.Main.main(Main.java:117)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:606)

at net.minecraft.launchwrapper.Launch.launch(Launch.java:135)

at net.minecraft.launchwrapper.Launch.main(Launch.java:28)

at net.minecraftforge.gradle.GradleStartCommon.launch(Unknown Source)

at GradleStart.main(Unknown Source)

 

 

A detailed walkthrough of the error, its code path and all known details is as follows:

---------------------------------------------------------------------------------------

 

-- Head --

Stacktrace:

at com.modernpatriot.reimaginedindustries.blocks.BasicGearbox.onBlockActivated(BasicGearbox.java:60)

at net.minecraft.client.multiplayer.PlayerControllerMP.func_178890_a(PlayerControllerMP.java:416)

at net.minecraft.client.Minecraft.rightClickMouse(Minecraft.java:1572)

 

-- Affected level --

Details:

Level name: MpServer

All players: 1 total; [EntityPlayerSP['Player45'/98, l='MpServer', x=447,36, y=4,00, z=1499,88]]

Chunk stats: MultiplayerChunkCache: 440, 440

Level seed: 0

Level generator: ID 01 - flat, ver 0. Features enabled: false

Level generator options:

Level spawn location: 466,00,4,00,1547,00 - World: (466,4,1547), Chunk: (at 2,0,11 in 29,96; contains blocks 464,0,1536 to 479,255,1551), Region: (0,3; contains chunks 0,96 to 31,127, blocks 0,0,1536 to 511,255,2047)

Level time: 510332 game time, 1216 day time

Level dimension: 0

Level storage version: 0x00000 - Unknown?

Level weather: Rain time: 0 (now: false), thunder time: 0 (now: false)

Level game mode: Game mode: creative (ID 1). Hardcore: false. Cheats: false

Forced entities: 50 total; [EntityItemFrame['entity.ItemFrame.name'/68, l='MpServer', x=478,97, y=5,50, z=1523,50], EntityItemFrame['entity.ItemFrame.name'/69, l='MpServer', x=478,97, y=5,50, z=1524,50], EntityItemFrame['entity.ItemFrame.name'/70, l='MpServer', x=479,97, y=8,50, z=1524,50], EntityItemFrame['entity.ItemFrame.name'/71, l='MpServer', x=479,97, y=8,50, z=1522,50], EntityItemFrame['entity.ItemFrame.name'/64, l='MpServer', x=479,97, y=6,50, z=1524,50], EntityItemFrame['entity.ItemFrame.name'/65, l='MpServer', x=479,97, y=7,50, z=1523,50], EntityItemFrame['entity.ItemFrame.name'/66, l='MpServer', x=479,97, y=8,50, z=1523,50], EntityItemFrame['entity.ItemFrame.name'/67, l='MpServer', x=478,97, y=5,50, z=1522,50], EntityItemFrame['entity.ItemFrame.name'/76, l='MpServer', x=478,97, y=5,50, z=1520,50], EntityItemFrame['entity.ItemFrame.name'/77, l='MpServer', x=468,50, y=5,50, z=1549,97], EntityItemFrame['entity.ItemFrame.name'/78, l='MpServer', x=469,50, y=5,50, z=1549,97], EntityItemFrame['entity.ItemFrame.name'/72, l='MpServer', x=479,97, y=7,50, z=1524,50], EntityItemFrame['entity.ItemFrame.name'/73, l='MpServer', x=479,97, y=6,50, z=1522,50], EntityItemFrame['entity.ItemFrame.name'/74, l='MpServer', x=479,97, y=6,50, z=1523,50], EntityItemFrame['entity.ItemFrame.name'/75, l='MpServer', x=479,97, y=7,50, z=1522,50], EntityPig['Pig'/87, l='MpServer', x=525,19, y=4,00, z=1514,31], EntityPig['Pig'/86, l='MpServer', x=514,69, y=4,00, z=1495,88], EntityPig['Pig'/27, l='MpServer', x=392,34, y=4,00, z=1422,09], EntityRabbit['Rabbit'/26, l='MpServer', x=383,84, y=4,00, z=1503,75], EntityItem['item.item.egg'/29, l='MpServer', x=395,13, y=4,00, z=1555,22], EntityCow['Cow'/88, l='MpServer', x=526,69, y=4,00, z=1522,22], EntityChicken['Chicken'/28, l='MpServer', x=404,38, y=4,00, z=1482,41], EntityPig['Pig'/31, l='MpServer', x=411,28, y=4,00, z=1440,09], EntityChicken['Chicken'/34, l='MpServer', x=405,34, y=4,00, z=1565,34], EntityRabbit['Rabbit'/35, l='MpServer', x=408,03, y=4,00, z=1552,97], EntityPlayerSP['Player45'/98, l='MpServer', x=447,36, y=4,00, z=1499,88], EntityPig['Pig'/32, l='MpServer', x=415,22, y=4,00, z=1474,03], EntityChicken['Chicken'/33, l='MpServer', x=401,34, y=4,00, z=1521,53], EntityChicken['Chicken'/36, l='MpServer', x=421,31, y=4,00, z=1436,63], EntityItem['item.item.egg'/37, l='MpServer', x=420,44, y=4,00, z=1436,50], EntityPig['Pig'/43, l='MpServer', x=462,16, y=4,00, z=1468,84], EntityItemFrame['entity.ItemFrame.name'/46, l='MpServer', x=479,97, y=6,50, z=1519,50], EntityItemFrame['entity.ItemFrame.name'/47, l='MpServer', x=479,97, y=8,50, z=1518,50], EntityItemFrame['entity.ItemFrame.name'/45, l='MpServer', x=479,97, y=6,50, z=1517,50], EntityItemFrame['entity.ItemFrame.name'/51, l='MpServer', x=478,97, y=5,50, z=1517,50], EntityItemFrame['entity.ItemFrame.name'/50, l='MpServer', x=478,97, y=6,50, z=1512,50], EntityItemFrame['entity.ItemFrame.name'/49, l='MpServer', x=478,97, y=5,50, z=1516,50], EntityItemFrame['entity.ItemFrame.name'/48, l='MpServer', x=479,97, y=6,50, z=1518,50], EntityItemFrame['entity.ItemFrame.name'/55, l='MpServer', x=479,97, y=8,50, z=1517,50], EntityItemFrame['entity.ItemFrame.name'/54, l='MpServer', x=479,97, y=8,50, z=1519,50], EntityItemFrame['entity.ItemFrame.name'/53, l='MpServer', x=478,97, y=5,50, z=1514,50], EntityItemFrame['entity.ItemFrame.name'/52, l='MpServer', x=479,97, y=7,50, z=1517,50], EntityItemFrame['entity.ItemFrame.name'/59, l='MpServer', x=478,97, y=5,50, z=1512,50], EntityItemFrame['entity.ItemFrame.name'/58, l='MpServer', x=478,97, y=6,50, z=1514,50], EntityItemFrame['entity.ItemFrame.name'/57, l='MpServer', x=478,97, y=5,50, z=1519,50], EntityItemFrame['entity.ItemFrame.name'/56, l='MpServer', x=479,97, y=7,50, z=1518,50], EntityItemFrame['entity.ItemFrame.name'/63, l='MpServer', x=478,97, y=6,50, z=1513,50], EntityItemFrame['entity.ItemFrame.name'/62, l='MpServer', x=479,97, y=7,50, z=1519,50], EntityItemFrame['entity.ItemFrame.name'/61, l='MpServer', x=478,97, y=5,50, z=1518,50], EntityItemFrame['entity.ItemFrame.name'/60, l='MpServer', x=478,97, y=5,50, z=1513,50]]

Retry entities: 0 total; []

Server brand: fml,forge

Server type: Integrated singleplayer server

Stacktrace:

at net.minecraft.client.multiplayer.WorldClient.addWorldInfoToCrashReport(WorldClient.java:392)

at net.minecraft.client.Minecraft.addGraphicsAndWorldToCrashReport(Minecraft.java:2615)

at net.minecraft.client.Minecraft.run(Minecraft.java:405)

at net.minecraft.client.main.Main.main(Main.java:117)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

at java.lang.reflect.Method.invoke(Method.java:606)

at net.minecraft.launchwrapper.Launch.launch(Launch.java:135)

at net.minecraft.launchwrapper.Launch.main(Launch.java:28)

at net.minecraftforge.gradle.GradleStartCommon.launch(Unknown Source)

at GradleStart.main(Unknown Source)

 

-- System Details --

Details:

Minecraft Version: 1.8

Operating System: Mac OS X (x86_64) version 10.10.2

Java Version: 1.7.0_79, Oracle Corporation

Java VM Version: Java HotSpot 64-Bit Server VM (mixed mode), Oracle Corporation

Memory: 784965856 bytes (748 MB) / 1056309248 bytes (1007 MB) up to 1056309248 bytes (1007 MB)

JVM Flags: 3 total; -Xincgc -Xmx1024M -Xms1024M

IntCache: cache: 0, tcache: 0, allocated: 0, tallocated: 0

FML: MCP v9.10 FML v8.0.99.99 Minecraft Forge 11.14.3.1450 6 mods loaded, 6 mods active

States: 'U' = Unloaded 'L' = Loaded 'C' = Constructed 'H' = Pre-initialized 'I' = Initialized 'J' = Post-initialized 'A' = Available 'D' = Disabled 'E' = Errored

UCHIJAAAA mcp{9.05} [Minecraft Coder Pack] (minecraft.jar)

UCHIJAAAA FML{8.0.99.99} [Forge Mod Loader] (forgeSrc-1.8-11.14.3.1450.jar)

UCHIJAAAA Forge{11.14.3.1450} [Minecraft Forge] (forgeSrc-1.8-11.14.3.1450.jar)

UCHIJAAAA CodeChickenCore{1.0.5.36} [CodeChicken Core] (minecraft.jar)

UCHIJAAAA NotEnoughItems{1.0.5.82} [Not Enough Items] (NotEnoughItems-1.8-1.0.5.82-universal.jar)

UCHIJAAAA ReimaginedIndustries{0.0.3} [§7Reimagined Industries §cPre-Alpha] (bin)

Loaded coremods (and transformers):

CCCDeobfPlugin (unknown)

 

NEICorePlugin (NotEnoughItems-1.8-1.0.5.82-universal.jar)

  codechicken.nei.asm.NEITransformer

CCLCorePlugin (CodeChickenLib-1.8-1.1.2.133-dev.jar)

  codechicken.lib.asm.ClassHeirachyManager

  codechicken.lib.asm.RenderHookTransformer

CodeChickenCorePlugin (CodeChickenCore-1.8-1.0.5.36-universal.jar)

  codechicken.core.asm.InterfaceDependancyTransformer

  codechicken.core.asm.TweakTransformer

  codechicken.core.asm.DelegatedTransformer

  codechicken.core.asm.DefaultImplementationTransformer

GL info: ' Vendor: 'NVIDIA Corporation' Version: '2.1 NVIDIA-10.0.19 310.90.10.05b12' Renderer: 'NVIDIA GeForce 320M OpenGL Engine'

Launched Version: 1.8

LWJGL: 2.9.2

OpenGL: NVIDIA GeForce 320M OpenGL Engine GL version 2.1 NVIDIA-10.0.19 310.90.10.05b12, NVIDIA Corporation

GL Caps: Using GL 1.3 multitexturing.

Using GL 1.3 texture combiners.

Using framebuffer objects because ARB_framebuffer_object is supported and separate blending is supported.

Shaders are available because OpenGL 2.1 is supported.

VBOs are available because OpenGL 1.5 is supported.

 

Using VBOs: No

Is Modded: Definitely; Client brand changed to 'fml,forge'

Type: Client (map_client.txt)

Resource Packs: []

Current Language: English (US)

Profiler Position: N/A (disabled)

 

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



×
×
  • Create New...

Important Information

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