Jump to content

[1.8] getBlock and setBlock?


DaveTheModder

Recommended Posts

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?

Link to comment
Share on other sites

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)

 

 

Link to comment
Share on other sites

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>

 

Link to comment
Share on other sites

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

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.