Jump to content
  • Home
  • Files
  • Docs
Topics
  • All Content

  • This Topic
  • This Forum

  • Advanced Search
  • Existing user? Sign In  

    Sign In



    • Not recommended on shared computers


    • Forgot your password?

  • Sign Up
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • Break blocks around a breaked block
Currently Supported: 1.16.X (Latest) and 1.15.X (LTS)
Sign in to follow this  
Followers 1
bibouche_

Break blocks around a breaked block

By bibouche_, December 3, 2020 in Modder Support

  • Reply to this topic
  • Start new topic

Recommended Posts

bibouche_    0

bibouche_

bibouche_    0

  • Tree Puncher
  • bibouche_
  • Members
  • 0
  • 19 posts
Posted December 3, 2020

Hi, I made a pickaxe in my mod that I actually want it to break ore veins more easily, so I made a blockbreakevent, and then a script that checks for every block around the ore you broke if there are any blocks of the same type, and if there are, then break them. My script is actually not working, and I don't really know why 😅, so I post this for any help, I would really appreciate it.

Thanks !

 

Here is my event :

@SubscribeEvent
    public static void onBreakEventWithPickaxe(BlockEvent.BreakEvent e) {
        PlayerEntity playerIn = e.getPlayer();
        Item pickaxe = ModItems.NETHER_STAR_PICKAXE.get();
        Block block = e.getState().getBlock();
        IWorld world = e.getWorld();

        if (e.getState().getBlock() instanceof OreBlock && playerIn.getHeldItemMainhand().getItem() == pickaxe) {

            int radius = 3;
            int height = 6;
            int posX = e.getPos().getX() - radius;
            int posY = e.getPos().getY() - radius;
            int posZ = e.getPos().getZ() - (height / 2);

            for (int i = 0; i < radius * 2 * radius * 2 * height; i++) {

                BlockPos pos = new BlockPos(posX, posY, posZ);

                if (world.getBlockState(pos).getBlock() == block) {
                    world.destroyBlock(pos, true);
                }

                posX++;

                if (posX == radius * 2 + 1) {
                    posY++;
                    posX = e.getPos().getX() - radius;
                }

                if (posY == radius * 2 + 1) {
                    posZ++;
                    posY = e.getPos().getY() - radius;
                    posX = e.getPos().getX() - radius;
                }
            }
        }
    }

 

  • Quote

Share this post


Link to post
Share on other sites

Draco18s    2406

Draco18s

Draco18s    2406

  • Reality Controller
  • Draco18s
  • Members
  • 2406
  • 15930 posts
Posted December 3, 2020

BlockPos.getAllInBox(...) would be a much, much better way of handling that loop.

Or hell, using three nested loops for x, y, and z...

  • Quote

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.

Share this post


Link to post
Share on other sites

bibouche_    0

bibouche_

bibouche_    0

  • Tree Puncher
  • bibouche_
  • Members
  • 0
  • 19 posts
Posted December 3, 2020 (edited)

Okkk, thanks, indeed it was a pain making a such loop 😅, I am trying to find how BlockPos.getAllInBox(...) works, but I don't fully understand :/

Have you got any advice ? 😅

 

I mean, I found out how to create the Stream with the two positions, but how can I use the Stream it creates ?

Edited December 3, 2020 by bibouche_
  • Quote

Share this post


Link to post
Share on other sites

bibouche_    0

bibouche_

bibouche_    0

  • Tree Puncher
  • bibouche_
  • Members
  • 0
  • 19 posts
Posted December 3, 2020

Ok so I think I figured it out, with a simple foreach loop, but I still can't make it works 😑

 

I made this :

@SubscribeEvent
        public static void onBlockBreakEvent(BlockEvent.BreakEvent e) {
            PlayerEntity playerIn = e.getPlayer();
            Block block = e.getState().getBlock();
            IWorld world = e.getWorld();

            if (e.getState().getBlock() instanceof OreBlock) {

                int radius = 3;

                int baseX = e.getPos().getX() - radius;
                int baseY = e.getPos().getY() - radius;
                int baseZ = e.getPos().getZ() - radius;

                int finalX = e.getPos().getX() + radius;
                int finalY = e.getPos().getY() + radius;
                int finalZ = e.getPos().getZ() + radius;

                BlockPos firstPos = new BlockPos(baseX, baseY,  baseZ);
                BlockPos secondPos = new BlockPos(finalX, finalY,  finalZ);

                Stream<BlockPos> blockList = BlockPos.getAllInBox(firstPos, secondPos);

                for ( BlockPos pos : blockList.collect(Collectors.toList()) ) {

                    if (world.getBlockState(pos).getBlock() == block) {
                        world.destroyBlock(pos, true);
                    }

                }

            }
        }

 

  • Quote

Share this post


Link to post
Share on other sites

Draco18s    2406

Draco18s

Draco18s    2406

  • Reality Controller
  • Draco18s
  • Members
  • 2406
  • 15930 posts
Posted December 3, 2020

Stream implements IEnumerable, so you don't need to convert it to a List

  • Quote

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.

Share this post


Link to post
Share on other sites

diesieben07    7597

diesieben07

diesieben07    7597

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7597
  • 55115 posts
Posted December 3, 2020
11 minutes ago, Draco18s said:

Stream implements IEnumerable, so you don't need to convert it to a List

IEnumerable is a C# thing. The Java equivalent would be Iterable, which Stream does not implement.

  • Quote

Share this post


Link to post
Share on other sites

bibouche_    0

bibouche_

bibouche_    0

  • Tree Puncher
  • bibouche_
  • Members
  • 0
  • 19 posts
Posted December 3, 2020 (edited)

Ok, so I do need to convert it to a list ?

 

But is there a problem with my foreach loop ? when I make it sysout position for every pos of the stream, it prints the same position at all, and I don't really understand why :/

Edited December 3, 2020 by bibouche_
  • Quote

Share this post


Link to post
Share on other sites

kiou.23    5

kiou.23

kiou.23    5

  • Creeper Killer
  • kiou.23
  • Members
  • 5
  • 158 posts
Posted December 3, 2020
3 minutes ago, bibouche_ said:

Ok, so I do need to convert it to a list ?

you can use the collect() method from the stream, and then pass the desired collection type conversion, in this case Collectors.toList()

  • Quote

Share this post


Link to post
Share on other sites

Draco18s    2406

Draco18s

Draco18s    2406

  • Reality Controller
  • Draco18s
  • Members
  • 2406
  • 15930 posts
Posted December 3, 2020
25 minutes ago, diesieben07 said:

IEnumerable is a C# thing.

Derp yeah.

 

25 minutes ago, diesieben07 said:

The Java equivalent would be Iterable, which Stream does not implement.

 

I thought it did. Nevermind then.

  • Quote

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.

Share this post


Link to post
Share on other sites

bibouche_    0

bibouche_

bibouche_    0

  • Tree Puncher
  • bibouche_
  • Members
  • 0
  • 19 posts
Posted December 3, 2020 (edited)
28 minutes ago, kiou.23 said:

you can use the collect() method from the stream, and then pass the desired collection type conversion, in this case Collectors.toList()

yup, I did that, but it isn't working, and when I print the pos for every element of the stream, it prints the same position at all, and I don't understand why

Edited December 3, 2020 by bibouche_
  • Quote

Share this post


Link to post
Share on other sites

diesieben07    7597

diesieben07

diesieben07    7597

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7597
  • 55115 posts
Posted December 3, 2020

getAllInBox uses MutableBlockPos under the hood. Each element is only valid until the next element arrives, because they are re-used. If you store them all in a list, you will just get the same BlockPos object over and over. Either you need to use Stream#map with BlockPos#toImmutable (not advisable, as it will use a lot of memory) or just use Stream#forEach instead of a for loop.

  • Quote

Share this post


Link to post
Share on other sites

bibouche_    0

bibouche_

bibouche_    0

  • Tree Puncher
  • bibouche_
  • Members
  • 0
  • 19 posts
Posted December 4, 2020

Oh thank you so much, it finally works, I'm so happy :)

 

I made the Stream.forEach() loop, and that's all

  • Quote

Share this post


Link to post
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.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

    • Insert image from URL
×
  • Desktop
  • Tablet
  • Phone
Sign in to follow this  
Followers 1
Go To Topic Listing



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • GenElectrovise
      What is the method to left click?

      By GenElectrovise · Posted 25 minutes ago

      There's probably something in or nearby to PlayerEntity (as a movement controller or something similar?) I'd start with searching my workspace for something along the lines of KeystrokeHandler or PlayerMovementController
    • Luis_ST
      [1.16.5] GameOverlay

      By Luis_ST · Posted 25 minutes ago

      I just want to render a overlay (i have creat a spyglass likt that from 1.17) and now i want to render the Overlay this is the code of the event i used: @SubscribeEvent(priority = EventPriority.HIGHEST) public static void RenderSpyglassOverlay(RenderGameOverlayEvent event) { PlayerEntity player = Minecraft.getInstance().player; int posX = event.getWindow().getScaledWidth() / 2; int posY = event.getWindow().getScaledHeight() / 2; if (player.getActiveItemStack().getItem() == CaveItems.SPYGLASS.get()) { RenderSystem.disableDepthTest(); RenderSystem.depthMask(false); RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F); RenderSystem.disableAlphaTest(); Minecraft.getInstance().getTextureManager().bindTexture(new ResourceLocation("cave:textures/misc/spyglass_scope.png")); Minecraft.getInstance().ingameGUI.blit(event.getMatrixStack(), posX - 128, posY - 128, 0, 0, posX * 2, posY * 2, 256, 256); RenderSystem.depthMask(true); RenderSystem.enableDepthTest(); RenderSystem.enableAlphaTest(); RenderSystem.color4f(1.0F, 1.0F, 1.0F, 1.0F); } } but the overlay looks like this: https://drive.google.com/file/d/15llZaiIqNWK7WRqcihIJY7oaAszkFKnn/view?usp=sharing so my question: 1 .how to render the game overlay translucent 2. how to set the outside of the overlay to black
    • GenElectrovise
      Server doesnt start

      By GenElectrovise · Posted 30 minutes ago

      Never heard of an error like this but what's your version.
    • Potatoe
      Minecraft server

      By Potatoe · Posted 1 hour ago

      ok
    • diesieben07
      Minecraft server

      By diesieben07 · Posted 1 hour ago

      This is a Forum for Forge, I would suggest you seek help elsewhere for Vanilla Minecraft.
  • Topics

    • Gubipe
      5
      What is the method to left click?

      By Gubipe
      Started 14 hours ago

    • Luis_ST
      0
      [1.16.5] GameOverlay

      By Luis_ST
      Started 25 minutes ago

    • BinAufGoogle
      3
      Server doesnt start

      By BinAufGoogle
      Started 18 hours ago

    • Potatoe
      4
      Minecraft server

      By Potatoe
      Started Sunday at 10:13 AM

    • Luis_ST
      4
      [1.16.5] Player Field of View

      By Luis_ST
      Started 2 hours ago

  • Who's Online (See full list)

    • Linky132
    • Qolem
    • Luis_ST
    • yumeji
    • Leronus
    • Choonster
    • HowHow
    • diesieben07
    • Heliarco
    • GenElectrovise
    • Yagnap
    • hendrik
  • All Activity
  • Home
  • Mod Developer Central
  • Modder Support
  • Break blocks around a breaked block
  • Theme

Copyright © 2019 ForgeDevelopment LLC · Ads by Longitude Ads LLC Powered by Invision Community