Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.11.2] Rendering "ghost" blocks


Recommended Posts

Well hello there :D


I'm back with rendering-related questions. My main goal is to render my block that I'm about to place in the position it would be placed if I pressed right-click. Here's an example: say I'm looking in the distance, no block outlines are rendered. But, if I'm looking at e.g. dirt (which is near me), I would see it's bounding box (the black outline). If I were to place my block, it would be placed in front of that dirt block. What I want to do is to render my own block in that place (with a certain level of transparency), without actually placing it. I hope I explained it well enough for you to understand.

Link to comment
Share on other sites

You will need to use one of forge's render events, like RenderWorldLastEvent. You can get the block the player is looking at with the World::rayTraceBlocks method. I believe it's HitResult returns you the BlockPos of the block the player is looking at with EnumFacing of the side the player is looking at. Your rendering position would then be the raytraceresult position offset by that facing. To render a blockstate in the world you can look at RenderFallingBlock class. There might be some issues with transparency but in theory you can just enable blend with appropriate source and destination factors and either use gl's color method or manually override vertex color data in the vertexbuffer you are rendering your block with using VertexBuffer::putColor.

Just do not forget to properly offset your rendering, or you will be the third person in a row with the rendered object 'following' the player/not rendering :D


  • Like 1
Link to comment
Share on other sites

DrawBlockHighlightEvent would be more appropriate.

  • Like 2

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

I did everything you said, but the only thing I get is the black bounding box that you look at don't get rendered, instead, gray lines that form triangles on the block's face renders. The block itself is never shown.

	public void highlightGhostBlock(DrawBlockHighlightEvent event)
		EntityPlayer player = event.getPlayer();
		BlockPos position = event.getTarget().getBlockPos().offset(player.getHorizontalFacing());
		ItemStack stack = player.getHeldItemMainhand();
		if (stack.getItem() instanceof BSMItemBlock)
			IBlockState stateToRender = ((BSMItemBlock)stack.getItem()).getDetails(player.world, position, player, 0, 0, 0, event.getTarget().sideHit).getSecondElement();
            //GlStateManager.alphaFunc(0, 0);
            Tessellator tessellator = Tessellator.getInstance();
            VertexBuffer vertexbuffer = tessellator.getBuffer();
            position.add(0.5, 0, 0.5);
            vertexbuffer.begin(7, DefaultVertexFormats.BLOCK);
            BlockPos blockpos = new BlockPos(position.getX(), position.getY(), position.getZ());
            GlStateManager.translate(position.getX(), position.getY(), position.getZ());
            BlockRendererDispatcher blockrendererdispatcher = Minecraft.getMinecraft().getBlockRendererDispatcher();
            blockrendererdispatcher.getBlockModelRenderer().renderModel(event.getPlayer().world, blockrendererdispatcher.getModelForState(stateToRender), stateToRender, blockpos, vertexbuffer, false, MathHelper.getPositionRandom(position));



Link to comment
Share on other sites

And... congratulations! You are the third person in a row who forgot about applying correct translations before rendering! :D



With correct translations applied I get the following from your code:



Also if you want your block to be transparent you need to setup GL's blend too, not only alpha :P

Edited by V0idWa1k3r
  • Like 2
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.

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.


  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • I have been trying to download Forge but the Windows Installer isn't coming up, I can see the installer one would use for Linux but no Windows, I have downloaded Java and Jarfix but nothing is working, can someone help?  
    • So I thought that when a capability is attached to an item/entity/etc... the capability will stay until whatever it's attached to is gone. But this doesn't seem to be the case, as when I printed to console whenever a capability is attached, it seems as though a new version of the capability is attached to an item every time I select it in my hotbar, hover over it in inventory, etc. Is this how it's supposed to work? If so, why?
    • Hi, I'm trying to set up a simple forge server just to play with a couple of my friends, but no matter what I do or try I cannot seem to get a Mods folder and for the actual forge part of the server to actually launch. Every time I try to launch the forge.jar via a .bat file I just get a bunch of errors [Errors below] I'm not sure what I'm doing wrong. If I can get some help that would be awesome, thanks C:\Users\User\Desktop\Server>java -Xms2G -Xmx6G -jar forge-1.12.2- nogui A problem occurred running the Server launcher.java.lang.reflect.InvocationTargetException         at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)         at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:78)         at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)         at java.base/java.lang.reflect.Method.invoke(Method.java:567)         at net.minecraftforge.fml.relauncher.ServerLaunchWrapper.run(ServerLaunchWrapper.java:70)         at net.minecraftforge.fml.relauncher.ServerLaunchWrapper.main(ServerLaunchWrapper.java:34) Caused by: java.lang.ClassCastException: class jdk.internal.loader.ClassLoaders$AppClassLoader cannot be cast to class java.net.URLClassLoader (jdk.internal.loader.ClassLoaders$AppClassLoader and java.net.URLClassLoader are in module java.base of loader 'bootstrap')         at net.minecraft.launchwrapper.Launch.<init>(Launch.java:34)         at net.minecraft.launchwrapper.Launch.main(Launch.java:28)         ... 6 more C:\Users\User\Desktop\Server>pause Press any key to continue . . .  
    • Is it ok if i ask what that is?
  • Topics

  • Who's Online (See full list)

  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.