Jump to content

1.12 Manipulate BlockPos [SOLVED]


Blu_Nighttime

Recommended Posts

Hi

 

I was wondering how to manipulate a BlockPos, i.e. add 1 to x or z

But I'm not sure how to do this, I've got

pos = pos.offset(facing);

Inside onItemUse, which I know all works as this returns the x,y,z pos of the block. Say I have position BlockPos{x=20, y=40, z=-100}, how do I get this to say {x=20, y=40, z=101} for example.

 

Thanks 

Edited by Blu_Nighttime
Link to comment
Share on other sites

BlockPos has methods for every direction one without params which target the blockPos next to it and one with an int param which searches the BlockPos in int value blocks distance. Sorry if this is unclear

I think they are called

up, down, north, west, south ,east

My Projects:

Cruelars Triforcemod (1.12 release; 1.14 alpha soon coming)

 

Important:

As my mod is on at least 10 different third party sites without my permission, I want to warn you about that with a link to StopModReposts

Link to comment
Share on other sites

For reference:

+X is East

+Y is Up

+Z is South

  • Like 1

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

Also, a block pos contains fields for each of x, y, z so if you can do any math you want. Although most common operations, like getting block beside or above already have dedicated methods as mentioned. But don't be confused by BlockPos -- it just contains x, y, and z which are directly accessible if you need it.

Check out my tutorials here: http://jabelarminecraft.blogspot.com/

Link to comment
Share on other sites

Cheers guys! Got it working. I needed to use .add(x,y,z) in conjunction with .offset(facing) to achieve several blocks being placed at once.

If anyone else sees this (and for clear clarification for what I was looking for) this is the code necessary to place multiple blocks.

public static BlockPos pos1; //creates a variable for holding second BlockPos

public EnumActionResult onItemUse(...){ //Standard setup
	pos = pos.offset(facing); //fetch the current BlockPos the player is facing
  	pos1 = pos.add(1, 0, 0) //set BlockPos 1 to the right (east, x)
	worldIn.setBlockState(pos, ...); //set the block infront of player to whatever
  	worldIn.setBlockSate(pos1, ...); //set the block to the new BlockPos position (i.e. 1 to the right) to whatever.

Obviously use

 if(worldIn.isAirBlock(pos)){...}

to test to see if the new location can have a block placed, otherwise without this a block will be replaced which is incorrect (unless that is what you're looking for). And do this every time you need to place a new block that is at offset to the original location.

 

I hope this helps anyone else, I know it definitely did for me.

 

Thanks.

Link to comment
Share on other sites

8 minutes ago, Blu_Nighttime said:

Also while this topic is still open, how do I get the player facing direction so i can have something like

if(player facing north or south){...}

if(player facing east or west){...}

I thought it was something to do with EnumFacing but that doesn't seem to be the case.

Entity(something)#getHorizontalFacing

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

Great, I knew it had something to do with player and EnumFacing but wasn't sure what else is was missing. Looks something like this:

if(player.getHorizontalFacing == EnumFacing.NORTH || player.getHorizonalFacing == EnumFacing.SOUTH){...} //Likewise with West and East

(For other people's referencing).

 

Thanks guys, I know all of this is already in Forge and the declaration can be viewed in eclipse but sometimes it's so hard to know exactly what you're looking for that a few quick points like this is all you need.

 

Thanks.

Link to comment
Share on other sites

5 hours ago, Blu_Nighttime said:

pos1 = pos.add(1, 0, 0) //set BlockPos 1 to the right (east, x)

 

Usually you want to do something more like pos1 = pos.rotateY(); //North -> East -> South -> West -> North

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • When I add a search bar to CreativeModeTab, the text box is not rendered even though it's possible to input text. Additionally, the search is performed on all items. This issue has been occurring since 1.21. Is there something missing in my code? https://github.com/mgen256/AdditionalLights/blob/1.21/src/main/java/com/mgen256/al/AdditionalLights.java
    • This question would probably best be asked wherever fabric support is given; these are the Minecraft Forge forums. They aren't the same thing
    • client version: forge-1.7.10-10.13.4.1614-1.7.10 forgegradle version:forge-1.7.10-10.13.4.1614-1.7.10   i can run my mod in forgegradle but i put my mod into a client ,it will crash  cpw.mods.fml.common.LoaderException: java.lang.NoSuchMethodError: com.example.examplemod.items.CommonItem.setUnlocalizedName(Ljava/lang/String;)Lnet/minecraft/item/Item;     at cpw.mods.fml.common.LoadController.transition(LoadController.java:163)     at cpw.mods.fml.common.Loader.preinitializeMods(Loader.java:559)     at cpw.mods.fml.client.FMLClientHandler.beginMinecraftLoading(FMLClientHandler.java:243)     at net.minecraft.client.Minecraft.func_71384_a(Minecraft.java:480)     at net.minecraft.client.Minecraft.func_99999_d(Minecraft.java:878)     at net.minecraft.client.main.Main.main(SourceFile:148)     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:497)     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:497)     at oolloo.jlw.Wrapper.invokeMain(Wrapper.java:71)     at oolloo.jlw.Wrapper.main(Wrapper.java:51) Caused by: java.lang.NoSuchMethodError: com.example.examplemod.items.CommonItem.setUnlocalizedName(Ljava/lang/String;)Lnet/minecraft/item/Item;     at com.example.examplemod.items.CommonItem.<init>(CommonItem.java:10)     at com.example.examplemod.proxy.register.ItemRegister.createItem(ItemRegister.java:30)     at com.example.examplemod.proxy.register.ItemRegister.registerItems(ItemRegister.java:18)     at com.example.examplemod.proxy.CommonProxy.preInit(CommonProxy.java:12)     at com.example.examplemod.proxy.ClientProxy.preInit(ClientProxy.java:6)     at com.example.examplemod.ExampleMod.preInit(ExampleMod.java:26)     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:497)     at cpw.mods.fml.common.FMLModContainer.handleModStateEvent(FMLModContainer.java:532)     at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source)     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     at java.lang.reflect.Method.invoke(Method.java:497)     at com.google.common.eventbus.EventSubscriber.handleEvent(EventSubscriber.java:74)     at com.google.common.eventbus.SynchronizedEventSubscriber.handleEvent(SynchronizedEventSubscriber.java:47)     at com.google.common.eventbus.EventBus.dispatch(EventBus.java:322)     at com.google.common.eventbus.EventBus.dispatchQueuedEvents(EventBus.java:304)     at com.google.common.eventbus.EventBus.post(EventBus.java:275)     at cpw.mods.fml.common.LoadController.sendEventToModContainer(LoadController.java:212)     at cpw.mods.fml.common.LoadController.propogateStateMessage(LoadController.java:190)     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:497)     at com.google.common.eventbus.EventSubscriber.handleEvent(EventSubscriber.java:74)     at com.google.common.eventbus.SynchronizedEventSubscriber.handleEvent(SynchronizedEventSubscriber.java:47)     at com.google.common.eventbus.EventBus.dispatch(EventBus.java:322)     at com.google.common.eventbus.EventBus.dispatchQueuedEvents(EventBus.java:304)     at com.google.common.eventbus.EventBus.post(EventBus.java:275)     at cpw.mods.fml.common.LoadController.distributeStateMessage(LoadController.java:119)     at cpw.mods.fml.common.Loader.preinitializeMods(Loader.java:556)     ... 16 more  
  • Topics

×
×
  • Create New...

Important Information

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