Jump to content

[1.9] onBlockActivated cause game to crash when any block is right-clicked


oneofthem999

Recommended Posts

Hi everyone. So I'm trying to learn how to add and incorporate new blocks into Minecraft, and I ran into the following problem.  The onBlockActivate method causes my game to crash everytime a block is right-clicked.  I know it's this method that is giving me trouble, as when I removing, my game runs fine.  I have no idea why this would occur, and I was hoping for some insight.  Here is the code for the block class:

 

public class EnderBlock extends Block {
    //Constructor
    public EnderBlock() {
        //Set material of block using inherited constructor
        super(Material.iron);

        //Set unlocalized name
        this.setUnlocalizedName("enderBlock");

        //Set up in which creative tabe this block can be found
        this.setCreativeTab(CreativeTabs.tabBlock);

        //Set blast resistance
        this.setResistance(5.0F);

        //Set hardness
        this.setHardness(10.0F);

        //Set brightness
        this.setLightLevel(1.0F);
    }

    /**
     * This method will spawn a ender eye and a message every time the block is interacted with.
     * 
     * onBlockActivated is a method that triggers when a block is right-clicked.
     */
    @Override
    public boolean onBlockActivated(World world, BlockPos pos, IBlockState state, EntityPlayer player, EnumHand hand,
                                    ItemStack heldItem, EnumFacing side, float hitX, float hitY, float hitZ) {
        //Make sure method is only fully called in client-side
        if (!world.isRemote) {
            return false;
        }

        //Check to make sure hand is not empty
        if (player.getHeldItemMainhand() == null) {
            return false;
        }

        //Send message to Chat
        player.addChatComponentMessage(new TextComponentString(TextFormatting.DARK_RED + "You have clicked on " +
                "the GREAT ENDERIUM BLOCK OF GREATNESS!!!!!"));

        //Create ender eye and set location
        EntityEnderEye eye = new EntityEnderEye(world, pos.getX() + 0.5, pos.getY() + 1.5, pos.getZ() + 0.5);

        //Set motion of eye upward
        eye.motionY = 0.1;

        //Spawn ender eye
        world.spawnEntityInWorld(eye);

        //Return true, ending method
        return true;
    }
}

Link to comment
Share on other sites

Post the crash report.

 

For future reference, entities must be spawned on the server, not the client.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

Here's the crash report.

 

---- Minecraft Crash Report ----

// I'm sorry, Dave.

 

Time: 5/20/16 8:51 AM

Description: Unexpected error

 

java.lang.NullPointerException: Unexpected error

at commands.BlockFiller.choosePositionsRightHand(BlockFiller.java:79)

at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_7_BlockFiller_choosePositionsRightHand_RightClickBlock.invoke(.dynamic)

at net.minecraftforge.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:49)

at net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:140)

at net.minecraftforge.common.ForgeHooks.onRightClickBlock(ForgeHooks.java:966)

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

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

at net.minecraft.client.Minecraft.processKeyBinds(Minecraft.java:2268)

at net.minecraft.client.Minecraft.runTickKeyboard(Minecraft.java:2052)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

at net.minecraftforge.gradle.GradleStartCommon.launch(GradleStartCommon.java:97)

at GradleStart.main(GradleStart.java:26)

 

 

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

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

 

-- Head --

Stacktrace:

at commands.BlockFiller.choosePositionsRightHand(BlockFiller.java:79)

at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_7_BlockFiller_choosePositionsRightHand_RightClickBlock.invoke(.dynamic)

at net.minecraftforge.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:49)

at net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:140)

at net.minecraftforge.common.ForgeHooks.onRightClickBlock(ForgeHooks.java:966)

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

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

at net.minecraft.client.Minecraft.processKeyBinds(Minecraft.java:2268)

at net.minecraft.client.Minecraft.runTickKeyboard(Minecraft.java:2052)

 

-- Affected level --

Details:

Level name: MpServer

All players: 1 total; [EntityPlayerSP['Player161'/184, l='MpServer', x=198.90, y=74.00, z=210.38]]

Chunk stats: MultiplayerChunkCache: 516, 516

Level seed: 0

Level generator: ID 00 - default, ver 1. Features enabled: false

Level generator options:

Level spawn location: World: (201,64,219), Chunk: (at 9,4,11 in 12,13; contains blocks 192,0,208 to 207,255,223), Region: (0,0; contains chunks 0,0 to 31,31, blocks 0,0,0 to 511,255,511)

Level time: 1849 game time, 1849 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: 117 total; [EntityBat['Bat'/49, l='MpServer', x=133.30, y=28.09, z=262.97], EntityCreeper['Creeper'/58, l='MpServer', x=138.50, y=30.00, z=233.50], EntityCreeper['Creeper'/59, l='MpServer', x=154.70, y=42.49, z=146.32], EntitySquid['Squid'/60, l='MpServer', x=157.67, y=48.07, z=173.04], EntitySkeleton['Skeleton'/61, l='MpServer', x=145.50, y=30.00, z=191.50], EntityWitch['Witch'/62, l='MpServer', x=159.50, y=28.00, z=266.50], EntityZombie['Zombie'/63, l='MpServer', x=152.58, y=25.00, z=266.50], EntityPig['Pig'/64, l='MpServer', x=158.79, y=63.00, z=260.50], EntityEnderman['Enderman'/65, l='MpServer', x=139.46, y=18.00, z=287.62], EntityCreeper['Creeper'/66, l='MpServer', x=162.50, y=26.00, z=172.50], EntitySquid['Squid'/67, l='MpServer', x=171.81, y=57.48, z=183.14], EntitySquid['Squid'/68, l='MpServer', x=178.28, y=62.37, z=183.91], EntitySkeleton['Skeleton'/69, l='MpServer', x=163.50, y=16.00, z=202.50], EntitySkeleton['Skeleton'/70, l='MpServer', x=160.50, y=16.00, z=206.50], EntityZombie['Zombie'/71, l='MpServer', x=174.63, y=43.00, z=199.50], EntityCreeper['Creeper'/72, l='MpServer', x=168.50, y=21.00, z=222.50], EntityCreeper['Creeper'/73, l='MpServer', x=169.50, y=31.00, z=248.50], EntityZombie['Zombie'/74, l='MpServer', x=160.50, y=17.00, z=269.50], EntityPig['Pig'/75, l='MpServer', x=173.50, y=63.00, z=261.50], EntityPig['Pig'/76, l='MpServer', x=174.51, y=62.20, z=267.25], EntityItem['item.tile.torch'/79, l='MpServer', x=177.64, y=32.00, z=155.96], EntitySpider['Spider'/80, l='MpServer', x=190.55, y=41.00, z=158.28], EntityZombie['Zombie'/81, l='MpServer', x=189.62, y=41.00, z=157.16], EntityZombie['Zombie'/82, l='MpServer', x=181.42, y=42.00, z=158.66], EntityCreeper['Creeper'/83, l='MpServer', x=178.50, y=41.00, z=179.50], EntityCreeper['Creeper'/84, l='MpServer', x=181.50, y=30.00, z=207.50], EntityZombie['Zombie'/85, l='MpServer', x=169.50, y=21.00, z=222.63], EntityWitch['Witch'/86, l='MpServer', x=182.27, y=32.00, z=248.50], EntityItem['item.item.seeds'/87, l='MpServer', x=184.53, y=63.00, z=254.44], EntityPig['Pig'/88, l='MpServer', x=177.75, y=63.00, z=255.81], EntityZombie['Zombie'/89, l='MpServer', x=186.66, y=29.00, z=264.35], EntitySkeleton['Skeleton'/94, l='MpServer', x=206.50, y=42.00, z=135.50], EntityBat['Bat'/95, l='MpServer', x=194.40, y=39.10, z=142.13], EntityItem['item.tile.torch'/96, l='MpServer', x=202.92, y=34.00, z=156.89], EntityZombie['Zombie'/97, l='MpServer', x=204.52, y=36.00, z=144.43], EntityMinecartChest['container.minecart'/98, l='MpServer', x=194.50, y=38.06, z=160.50], EntityItem['item.tile.torch'/99, l='MpServer', x=192.45, y=39.00, z=170.88], EntityBat['Bat'/100, l='MpServer', x=196.25, y=36.10, z=160.25], EntityMinecartChest['container.minecart'/101, l='MpServer', x=203.50, y=30.06, z=193.50], EntityItem['item.tile.torch'/102, l='MpServer', x=201.88, y=38.00, z=203.29], EntityMinecartChest['container.minecart'/103, l='MpServer', x=205.50, y=26.06, z=215.50], EntityWolf['Wolf'/104, l='MpServer', x=206.27, y=71.00, z=221.47], EntityWolf['Wolf'/105, l='MpServer', x=202.56, y=71.00, z=219.63], EntityPig['Pig'/106, l='MpServer', x=203.74, y=74.00, z=219.47], EntitySpider['Spider'/107, l='MpServer', x=198.50, y=24.00, z=284.50], EntityBat['Bat'/108, l='MpServer', x=192.66, y=24.10, z=277.75], EntityChicken['Chicken'/109, l='MpServer', x=223.91, y=43.00, z=130.80], EntityMinecartChest['container.minecart'/110, l='MpServer', x=214.50, y=37.06, z=141.50], EntitySpider['Spider'/111, l='MpServer', x=217.50, y=42.00, z=141.50], EntityCreeper['Creeper'/112, l='MpServer', x=202.48, y=31.00, z=155.18], EntitySkeleton['Skeleton'/113, l='MpServer', x=213.50, y=23.00, z=155.25], EntityCreeper['Creeper'/114, l='MpServer', x=223.78, y=22.00, z=158.50], EntityMinecartChest['container.minecart'/115, l='MpServer', x=211.50, y=41.06, z=155.50], EntitySkeleton['Skeleton'/116, l='MpServer', x=215.50, y=41.00, z=151.50], EntitySpider['Spider'/117, l='MpServer', x=214.36, y=31.00, z=172.67], EntitySpider['Spider'/118, l='MpServer', x=218.30, y=25.10, z=177.71], EntitySpider['Spider'/119, l='MpServer', x=216.50, y=41.00, z=172.50], EntityZombie['Zombie'/120, l='MpServer', x=221.50, y=33.00, z=162.50], EntityZombie['Zombie'/121, l='MpServer', x=220.57, y=26.00, z=184.49], EntityEnderman['Enderman'/122, l='MpServer', x=213.49, y=34.00, z=188.55], EntityMinecartChest['container.minecart'/123, l='MpServer', x=215.50, y=33.06, z=202.50], EntityItem['item.tile.rail'/124, l='MpServer', x=211.20, y=33.00, z=202.13], EntityItem['item.tile.rail'/125, l='MpServer', x=215.25, y=33.00, z=203.57], EntityItem['item.tile.rail'/126, l='MpServer', x=212.31, y=39.00, z=198.78], EntityItem['item.tile.rail'/127, l='MpServer', x=219.16, y=33.00, z=193.79], EntityItem['item.tile.rail'/128, l='MpServer', x=211.80, y=42.00, z=197.86], EntityItem['item.item.string'/129, l='MpServer', x=220.20, y=33.00, z=195.30], EntityItem['item.tile.rail'/130, l='MpServer', x=211.13, y=42.00, z=201.80], EntityItem['item.tile.rail'/131, l='MpServer', x=212.33, y=42.00, z=212.27], EntityItem['item.tile.rail'/132, l='MpServer', x=212.49, y=42.00, z=213.13], EntityPig['Pig'/133, l='MpServer', x=216.25, y=70.00, z=213.49], EntityPig['Pig'/134, l='MpServer', x=217.24, y=69.00, z=223.50], EntitySquid['Squid'/135, l='MpServer', x=217.80, y=62.26, z=235.93], EntityWolf['Wolf'/136, l='MpServer', x=211.44, y=61.86, z=234.16], EntityWolf['Wolf'/137, l='MpServer', x=215.46, y=65.00, z=226.50], EntityPig['Pig'/138, l='MpServer', x=210.76, y=74.00, z=220.51], EntitySpider['Spider'/139, l='MpServer', x=226.50, y=28.00, z=139.50], EntityBat['Bat'/140, l='MpServer', x=229.86, y=25.09, z=145.38], EntityCreeper['Creeper'/141, l='MpServer', x=225.50, y=37.00, z=152.50], EntityZombie['Zombie'/142, l='MpServer', x=231.50, y=28.00, z=162.50], EntitySkeleton['Skeleton'/143, l='MpServer', x=232.50, y=27.00, z=162.50], EntitySquid['Squid'/144, l='MpServer', x=227.28, y=61.83, z=171.02], EntityCreeper['Creeper'/145, l='MpServer', x=224.50, y=14.00, z=176.50], EntityMinecartChest['container.minecart'/146, l='MpServer', x=239.50, y=37.06, z=177.50], EntityZombie['Zombie'/147, l='MpServer', x=225.37, y=37.00, z=180.53], EntitySkeleton['Skeleton'/148, l='MpServer', x=237.50, y=33.00, z=176.50], EntityItem['item.tile.gravel'/149, l='MpServer', x=227.51, y=41.00, z=177.04], EntityCreeper['Creeper'/150, l='MpServer', x=227.50, y=33.00, z=201.50], EntityZombie['Zombie'/151, l='MpServer', x=224.47, y=36.00, z=196.50], EntityCreeper['Creeper'/152, l='MpServer', x=237.50, y=36.00, z=208.50], EntitySkeleton['Skeleton'/153, l='MpServer', x=235.50, y=31.00, z=241.50], EntityZombie['Zombie'/154, l='MpServer', x=238.34, y=41.00, z=261.36], EntityBat['Bat'/155, l='MpServer', x=232.12, y=43.03, z=267.38], EntityChicken['Chicken'/156, l='MpServer', x=246.49, y=68.00, z=282.18], EntitySkeleton['Skeleton'/158, l='MpServer', x=244.50, y=24.00, z=136.50], EntitySkeleton['Skeleton'/159, l='MpServer', x=250.52, y=35.00, z=143.28], EntityBat['Bat'/160, l='MpServer', x=252.64, y=19.10, z=148.53], EntityBat['Bat'/161, l='MpServer', x=258.76, y=13.51, z=171.47], EntityBat['Bat'/162, l='MpServer', x=256.58, y=19.52, z=150.51], EntitySquid['Squid'/163, l='MpServer', x=238.91, y=58.58, z=159.43], EntityBat['Bat'/164, l='MpServer', x=250.75, y=15.10, z=166.25], EntityZombie['Zombie'/165, l='MpServer', x=247.50, y=36.00, z=160.50], EntityZombie['Zombie'/166, l='MpServer', x=247.30, y=36.00, z=178.30], EntityCreeper['Creeper'/167, l='MpServer', x=241.83, y=36.00, z=178.52], EntityCreeper['Creeper'/168, l='MpServer', x=253.16, y=38.00, z=177.47], EntityZombie['Zombie'/169, l='MpServer', x=245.46, y=36.00, z=180.48], EntityCreeper['Creeper'/170, l='MpServer', x=247.50, y=42.00, z=218.50], EntityChicken['Chicken'/171, l='MpServer', x=247.50, y=67.00, z=285.50], EntityChicken['Chicken'/172, l='MpServer', x=244.50, y=69.00, z=286.50], EntitySpider['Spider'/173, l='MpServer', x=255.97, y=12.00, z=170.00], EntityCreeper['Creeper'/174, l='MpServer', x=264.50, y=29.00, z=172.50], EntityBat['Bat'/175, l='MpServer', x=256.23, y=22.10, z=159.19], EntityBat['Bat'/176, l='MpServer', x=252.04, y=14.16, z=157.57], EntityBat['Bat'/178, l='MpServer', x=263.46, y=39.03, z=181.90], EntitySquid['Squid'/179, l='MpServer', x=258.40, y=58.00, z=210.60], EntityCreeper['Creeper'/180, l='MpServer', x=271.50, y=38.00, z=275.50], EntityPlayerSP['Player161'/184, l='MpServer', x=198.90, y=74.00, z=210.38]]

Retry entities: 0 total; []

Server brand: fml,forge

Server type: Integrated singleplayer server

Stacktrace:

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

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

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

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

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

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

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

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

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

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

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

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

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

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

at net.minecraftforge.gradle.GradleStartCommon.launch(GradleStartCommon.java:97)

at GradleStart.main(GradleStart.java:26)

 

-- System Details --

Details:

Minecraft Version: 1.9

Operating System: Windows 10 (amd64) version 10.0

Java Version: 1.8.0_91, Oracle Corporation

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

Memory: 256651048 bytes (244 MB) / 974651392 bytes (929 MB) up to 1888485376 bytes (1801 MB)

JVM Flags: 0 total;

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

FML: MCP 9.23 Powered by Forge 12.16.1.1892 7 mods loaded, 7 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.19} [Minecraft Coder Pack] (minecraft.jar)

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

UCHIJAAAA Forge{12.16.1.1892} [Minecraft Forge] (forgeSrc-1.9-12.16.1.1892.jar)

UCHIJAAAA BlocksMods{1.0} [blocksMods] (modid-1.0.jar)

UCHIJAAAA examplemod{1.0} [Example Mod] (modid-1.0.jar)

UCHIJAAAA CommandMods{1.0} [CommandMods] (modid-1.0.jar)

UCHIJAAAA myMods{1.0} [myMods] (modid-1.0.jar)

Loaded coremods (and transformers):

GL info: ' Vendor: 'Intel' Version: '4.4.0 - Build 20.19.15.4331' Renderer: 'Intel® HD Graphics 5500'

Launched Version: 1.9

LWJGL: 2.9.4

OpenGL: Intel® HD Graphics 5500 GL version 4.4.0 - Build 20.19.15.4331, Intel

GL Caps: Using GL 1.3 multitexturing.

Using GL 1.3 texture combiners.

Using framebuffer objects because OpenGL 3.0 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)

CPU: 4x Intel® Core i7-5500U CPU @ 2.40GHz

 

And in regards to the spawning, does that mean I return if isRemote is true, and then processed.  If it goes according to plan, that would mean the entity would spawn in the server, right?

Link to comment
Share on other sites

java.lang.NullPointerException: Unexpected error

  at commands.BlockFiller.choosePositionsRightHand(BlockFiller.java:79)

  at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_7_BlockFiller_choosePositionsRightHand_RightClickBlock.invoke(.dynamic)

  at net.minecraftforge.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:49)

  at net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:140)

  at net.minecraftforge.common.ForgeHooks.onRightClickBlock(ForgeHooks.java:966)

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

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

Something on line 79 of

BlockFiller

is

null

.

 

And in regards to the spawning, does that mean I return if isRemote is true, and then processed.  If it goes according to plan, that would mean the entity would spawn in the server, right?

Yes.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Link to comment
Share on other sites

So I went to that part of the code.  It's supposed to record the positions of two blocks that I left-click and right-click with a wooden axe, so that I may fill the space with another block (based on block id).  This class just takes care of the recording of the positions, while another command class fills the space.  I didn't suspect this class to give me the error at first because the command works fine, and I can select positions for the fill region with a wooden axe just fine.  But, as you pointed out, a null pointer exception is thrown for some reason whenever I right-click with no item in hand.

 

Here's the code for the BlockFiller class.  Again, note that left-clicking with no item is fine.  It's just right-clicking.:

 

public class BlockFiller
{
    //Declare static variables.  These variables will store the coordinates for the two positions for the selected region.
    public static List<Integer> pos1 = new ArrayList<Integer>();
    public static List<Integer> pos2 = new ArrayList<Integer>();

    /**
     * This method will allow the player to choose the positions of the selected region, thereby defining it.  It will
     * focus on choosing the position based on a left click.
     */
    @SubscribeEvent
    public void choosePositionsLeftClick(PlayerInteractEvent.LeftClickBlock event)
    {
        //Check if player has held item.
        if (event.getEntityPlayer().getHeldItemMainhand().getItem() == null)
        {
           return;
        }

        //Check if held item is a wooden axe.
        if (event.getEntityPlayer().getHeldItemMainhand().getItem() != Items.wooden_axe)
        {
            return;
        }

        //Check if player is in Creative Mode
        if (!event.getEntityPlayer().capabilities.isCreativeMode)
        {
            return;
        }

        //Set position 1 equal to coordinates of block interacted with using the left click
            //Reset pos1
            pos1.clear();

            //Store coordinates in pos1
            pos1.add(event.getPos().getX());
            pos1.add(event.getPos().getY());
            pos1.add(event.getPos().getZ());

            //Show chat message for notification of what spot is stored
            event.getEntityPlayer().addChatComponentMessage(new TextComponentString(TextFormatting.GREEN + "Position 1 set to "
            + event.getPos().getX() + ", " + event.getPos().getY() + ", " + event.getPos().getZ() + "."));
    }

    /**
     * This method will allow the player to choose the positions of the selected region, thereby defining it.  It will
     * focus on choosing a position based on a right click
     */
    @SubscribeEvent
    public void choosePositionsRightHand(PlayerInteractEvent.RightClickBlock event)
    {
        //Check if player has held item.
        if (event.getEntityPlayer().getHeldItemMainhand().getItem() == null)
        {
            return;
        }

        //Check if held item in main hand is a wooden axe.
        if (event.getEntityPlayer().getHeldItemMainhand().getItem() != Items.wooden_axe)
        {
            return;
        }

        //Check if player is in Creative Mode
        if (!event.getEntityPlayer().capabilities.isCreativeMode)
        {
            return;
        }

        //Set position 2 equal to coordinates of block interacted with using the right click
        //Reset pos2
        pos2.clear();

        //Store coordinates in pos2
        pos2.add(event.getPos().getX());
        pos2.add(event.getPos().getY());
        pos2.add(event.getPos().getZ());

        //Show chat message for notification of what spot is stored
        event.getEntityPlayer().addChatComponentMessage(new TextComponentString(TextFormatting.GREEN + "Position 2 set to "
                + event.getPos().getX() + ", " + event.getPos().getY() + ", " + event.getPos().getZ() + "."));
    }

 

Also, I tried commented out the part of the code that triggered the error the first time, which was:

 

        //Check if player has held item.
        if (event.getEntityPlayer().getHeldItemMainhand().getItem() == null)
        {
            return;
        }

 

at line 79.  However, when that happened, I got the following error message:

 

---- Minecraft Crash Report ----

// Surprise! Haha. Well, this is awkward.

 

Time: 5/20/16 9:17 AM

Description: Unexpected error

 

java.lang.NullPointerException: Unexpected error

at commands.BlockFiller.choosePositionsRightHand(BlockFiller.java:85)

at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_6_BlockFiller_choosePositionsRightHand_RightClickBlock.invoke(.dynamic)

at net.minecraftforge.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:49)

at net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:140)

at net.minecraftforge.common.ForgeHooks.onRightClickBlock(ForgeHooks.java:966)

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

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

at net.minecraft.client.Minecraft.processKeyBinds(Minecraft.java:2268)

at net.minecraft.client.Minecraft.runTickKeyboard(Minecraft.java:2052)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

at net.minecraftforge.gradle.GradleStartCommon.launch(GradleStartCommon.java:97)

at GradleStart.main(GradleStart.java:26)

 

 

at line 85, which is the following:

 

//Check if held item in main hand is a wooden axe.
        if (event.getEntityPlayer().getHeldItemMainhand().getItem() != Items.wooden_axe)
        {
            return;
        }

 

Thank you for your help so far!

Link to comment
Share on other sites

If an entity (e.g. a player) isn't holding anything in their main hand,

EntityLivingBase#getHeldItemMainhand

will return

null

. Calling

ItemStack#getItem

on this

null

value causes the crash.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

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.