Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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;
                }
            }
        }
    }

 

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

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

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.

  • Author

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 by bibouche_

  • Author

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);
                    }

                }

            }
        }

 

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

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.

  • Author

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 by bibouche_

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()

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.

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.

  • Author
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 by bibouche_

  • Author

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

 

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

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

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.