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    2401

Draco18s

Draco18s    2401

  • Reality Controller
  • Draco18s
  • Members
  • 2401
  • 15920 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    2401

Draco18s

Draco18s    2401

  • Reality Controller
  • Draco18s
  • Members
  • 2401
  • 15920 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    7587

diesieben07

diesieben07    7587

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7587
  • 54935 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
  • 153 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    2401

Draco18s

Draco18s    2401

  • Reality Controller
  • Draco18s
  • Members
  • 2401
  • 15920 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    7587

diesieben07

diesieben07    7587

  • Reality Controller
  • diesieben07
  • Forum Team
  • 7587
  • 54935 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

    • Klarks
      [1.16.4] How i can open a container by clicking on my mob

      By Klarks · Posted 5 minutes ago

      Is it ok that i am saving a player uuid as a string in datamanager private static final DataParameter<String> OWNER = EntityDataManager.createKey(MyEntity.class, DataSerializers.STRING); @OnlyIn(Dist.CLIENT) public UUID getOwner() { return UUID.fromString(this.dataManager.get(OWNER)); } public void setOwner(UUID name) { this.dataManager.set(OWNER ,name.toString()); }
    • e2rifia
      (1.16.2) Making a new capability (3)

      By e2rifia · Posted 19 minutes ago

      Is @EventBusSubscriber what's getting in the way of setup? It didn't work...
    • diesieben07
      (1.16.2) Making a new capability (3)

      By diesieben07 · Posted 25 minutes ago

      You don't need to, no. Why?
    • e2rifia
      (1.16.2) Making a new capability (3)

      By e2rifia · Posted 27 minutes ago

      I should separate @Mod and @EventBusSubscriber?
    • diesieben07
      (1.16.2) Making a new capability (3)

      By diesieben07 · Posted 33 minutes ago

      @EventBusSubscriber subscribes to the forge bus by default, but FMLCommonSetupEvent is fired on the mod bus.     Please use the code format when posting code.
  • Topics

    • Klarks
      11
      [1.16.4] How i can open a container by clicking on my mob

      By Klarks
      Started 14 hours ago

    • e2rifia
      10
      (1.16.2) Making a new capability (3)

      By e2rifia
      Started 2 hours ago

    • Heinzchen
      2
      Update mod to 1.16.5

      By Heinzchen
      Started 15 hours ago

    • XenoPyax
      3
      [1.16.4] Render Player in gui

      By XenoPyax
      Started 3 hours ago

    • metword
      16
      [1.16.4] Config file will not update.

      By metword
      Started Wednesday at 04:20 PM

  • Who's Online (See full list)

    • AzizD
    • Heinzchen
    • Pl00py_R
    • Dragrise96
    • loordgek
    • Danebi
    • diesieben07
  • 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