Posted July 15, 201510 yr I'm working on a 1.8 mod, and I'm having trouble setting and getting blocks. What I'm trying to do is spawn an entity when a chest is right clicked, but I can't figure out how to do it. Here is how I'd do it prior to 1.8: public class ModEventHandler { @SubscribeEvent public void interact(PlayerInteractEvent event) { Action action = event.action; EntityPlayer player = event.entityPlayer; World world = player.worldObj; Block block = world.getBlock(event.x, event.y, event.z); if(action.RIGHT_CLICK_BLOCK != null) { if(event.world.getBlock(event.x, event.y, event.z) == Blocks.chest) { if(!world.isRemote) { EntityZombie entity = new EntityZombie(world); entity.setPosition(event.x, event.y + 2, event.z); world.spawnEntityInWorld(entity); world.setBlock(event.x, event.y, event.z, Blocks.bedrock); } } } } } But I'm not sure how to accomplish this in 1.8, as there are no setBlock/getBlock methods. How can I do this?
July 15, 201510 yr You can use World.getBlockState(blockPos).getBlock() to get a block, and World.setBlockState(blockPos, yourBlock.getDefaultState()) to set a block. Potato's have skin. I have skin. Therefore, i am a potato. Follow me on Twitter! http://www.twitter.com/I_Mod_Minecraft
July 15, 201510 yr Author That successfully sets the block and summons the entity, but if I right click anything but a chest, even air, it crashes, saying this: [13:29:37] [Client thread/FATAL]: Unreported exception thrown! java.lang.NullPointerException at net.minecraft.world.World.isValid(World.java:260) ~[World.class:?] at net.minecraft.world.World.getBlockState(World.java:896) ~[World.class:?] at com.davethemodder.herobrine.ModEventHandler.interact(ModEventHandler.java:19) ~[ModEventHandler.class:?] at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_6_ModEventHandler_interact_PlayerInteractEvent.invoke(.dynamic) ~[?:?] at net.minecraftforge.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:55) ~[ASMEventHandler.class:?] at net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:138) ~[EventBus.class:?] at net.minecraftforge.event.ForgeEventFactory.onPlayerInteract(ForgeEventFactory.java:121) ~[ForgeEventFactory.class:?] at net.minecraft.client.Minecraft.rightClickMouse(Minecraft.java:1599) ~[Minecraft.class:?] at net.minecraft.client.Minecraft.runTick(Minecraft.java:2131) ~[Minecraft.class:?] at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:1087) ~[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.8.0_45] at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_45] at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_45] at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_45] 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/:?] [13:29:37] [Client thread/INFO] [sTDOUT]: [net.minecraft.init.Bootstrap:printToSYSOUT:663]: ---- Minecraft Crash Report ---- // I feel sad now Time: 7/15/15 1:29 PM Description: Unexpected error java.lang.NullPointerException: Unexpected error at net.minecraft.world.World.isValid(World.java:260) at net.minecraft.world.World.getBlockState(World.java:896) at com.davethemodder.herobrine.ModEventHandler.interact(ModEventHandler.java:19) at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_6_ModEventHandler_interact_PlayerInteractEvent.invoke(.dynamic) at net.minecraftforge.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:55) at net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:138) at net.minecraftforge.event.ForgeEventFactory.onPlayerInteract(ForgeEventFactory.java:121) at net.minecraft.client.Minecraft.rightClickMouse(Minecraft.java:1599) at net.minecraft.client.Minecraft.runTick(Minecraft.java:2131) at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:1087) 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(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) 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 net.minecraft.world.World.isValid(World.java:260) at net.minecraft.world.World.getBlockState(World.java:896) at com.davethemodder.herobrine.ModEventHandler.interact(ModEventHandler.java:19) at net.minecraftforge.fml.common.eventhandler.ASMEventHandler_6_ModEventHandler_interact_PlayerInteractEvent.invoke(.dynamic) at net.minecraftforge.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:55) at net.minecraftforge.fml.common.eventhandler.EventBus.post(EventBus.java:138) at net.minecraftforge.event.ForgeEventFactory.onPlayerInteract(ForgeEventFactory.java:121) at net.minecraft.client.Minecraft.rightClickMouse(Minecraft.java:1599)
July 15, 201510 yr Author I got rid of the part where it checks to make sure block != null, and it still gives me the error Here's my code: <code>public class ModEventHandler { @SubscribeEvent public void interact(PlayerInteractEvent event) { Action action = event.action; EntityPlayer player = event.entityPlayer; World world = player.worldObj; Block block = world.getBlockState(event.pos).getBlock(); if(action.RIGHT_CLICK_BLOCK != null) { System.out.println("UUUUUUUUUUU"); if(block == Blocks.chest) { if(!world.isRemote) { EntityZombie entity = new EntityZombie(world); entity.setPosition(player.posX, player.posY + 2, player.posZ); world.spawnEntityInWorld(entity); world.setBlockState(event.pos, Blocks.bedrock.getDefaultState()); } } } } }</code>
July 15, 201510 yr Author Ok, I changed it to this: <code>public class ModEventHandler { @SubscribeEvent public void interact(PlayerInteractEvent event) { Action action = event.action; EntityPlayer player = event.entityPlayer; World world = player.worldObj; Block block = world.getBlockState(event.pos).getBlock(); if(action != event.action.RIGHT_CLICK_AIR) { if(action == event.action.RIGHT_CLICK_BLOCK) { System.out.println("UUUUUUUUUUU"); if(block == Blocks.chest) { if(!world.isRemote) { EntityZombie entity = new EntityZombie(world); entity.setPosition(player.posX, player.posY + 2, player.posZ); world.spawnEntityInWorld(entity); world.setBlockState(event.pos, Blocks.bedrock.getDefaultState()); } } } } } }</code> And it still gives me the same error. It directs me to the line where I define the block variable. EDIT: Oh. Derp. Don't answer this yet.
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.