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

[1.18.1] Preferred way of programatically breaking blocks


NJSwede
 Share

Recommended Posts

Hi!

Java veteran, but new to modding. I'm working on ny very first mod experiments and I'm playing with increasing the reach of a pickaxe by following veins of ore and other fun things. 

Whenever I break a block and have a certain custom enchantment on my pickaxe, I want to break a few additional nearby blocks as well. Seems simple enough. I also want it to kind of "ripple" through the environment, so my thought was to put the blocks to be destroyed on a queue and let a tick event subscriber pick them up, one for each tick.

I have this:

@SubscribeEvent
  public static void onTick(final TickEvent e) {
    if (MegaMinerEvents.queue.isEmpty()) {
      return;
    }
    final BlockToBreak b = MegaMinerEvents.queue.removeFirst();
    final BlockState bs = b.world.getBlockState(b.pos);
    bs.getBlock()
        .playerDestroy(b.player.getLevel(), b.player, b.pos, bs, null, b.player.getMainHandItem());
    b.player.getLevel().removeBlock(b.pos, false);
  }

 

As you can see, I simply pop a block from the queue every tick and break it. It sort of works. 

However, I'm wondering if Block::playerDestroy is the right method to call. First of all, it doesn't actually remove the block. Second, it doesn't trigger any events from what I can see. The vanilla game itself is using ServerPlayerGameMode::destroyBlock(), but that method isn't accessible from any context I'm in.

So the question is: Is Block::playerDestroy the right method to use?
What other things, like sending events and checking things to I need to do if I use that method?
Also, is "onTick" the right place to do this?

 

Thanks!
NJSwede

Link to comment
Share on other sites

What is MegaMinerEvents? What is MegaMiner for that matter?

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

31 minutes ago, Draco18s said:

What is MegaMinerEvents? What is MegaMiner for that matter?

MegaMiner is just my playground mod. Here's the entire MegaMinerEvents class. It's just a static event receiver.

 

package nu.rydin.explodingarrows.common.events;

import net.minecraft.core.BlockPos;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.Tag;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.event.world.BlockEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import nu.rydin.explodingarrows.common.Main;
import nu.rydin.explodingarrows.common.enchantments.ModEnchantments;

import java.util.LinkedList;

@Mod.EventBusSubscriber(modid = Main.MOD_ID, bus = Mod.EventBusSubscriber.Bus.FORGE)
public class MegaMinerEvents {
  private static final class BlockToBreak {

    private final BlockPos pos;

    private final Player player;

    public BlockToBreak(final BlockPos pos, final Player player) {
      this.pos = pos;
      this.player = player;
    }
  }

  private static final LinkedList<BlockToBreak> queue = new LinkedList<>();

  @SubscribeEvent
  public static void onBreakBlock(final BlockEvent.BreakEvent e) {
    final LevelAccessor world = e.getWorld();
    if (world.isClientSide()) {
      return;
    }
    for (final Tag t : e.getPlayer().getMainHandItem().getEnchantmentTags()) {
      final CompoundTag ct = (CompoundTag) t;
      if (ct.getString("id")
          .equals(ModEnchantments.MEGA_MINER.get().getRegistryName().toString())) {
        MegaMinerEvents.mineNeighborhood(e.getPos(), e.getPlayer(), e.getState().getBlock());
      }
    }
  }

  @SubscribeEvent
  public static void onTick(final TickEvent.ServerTickEvent e) {
    if (MegaMinerEvents.queue.isEmpty()) {
      return;
    }
    final BlockToBreak b = MegaMinerEvents.queue.removeFirst();
    final Level world = b.player.level;
    final BlockState bs = world.getBlockState(b.pos);
    bs.getBlock().playerDestroy(world, b.player, b.pos, bs, null, b.player.getMainHandItem());
    world.removeBlock(b.pos, true);
    // MegaMinerEvents.mineNeighborhood(b.pos, b.player, bs.getBlock());
    System.out.println(MegaMinerEvents.queue.size());
  }

  private static void mineNeighborhood(
      final BlockPos pos, final Player player, final Block blockType) {
    final float x0 = pos.getX();
    final float y0 = pos.getY();
    final float z0 = pos.getZ();
    for (float z = z0 - 1.0F; z <= z0 + 1; z += 1.0) {
      for (float y = y0 - 1.0F; y <= y0 + 1; y += 1.0) {
        for (float x = x0 - 1.0F; x <= x0 + 1; x += 1.0) {
          final BlockState b = player.getLevel().getBlockState(pos);
          if (b.is(blockType)) {
            MegaMinerEvents.queue.addLast(new BlockToBreak(pos, player));
          }
        }
      }
    }
  }
}

 

Edited by NJSwede
Link to comment
Share on other sites

You should absolutely call ForgeHooks.canHarvestBlock(state, entityPlayer, world, pos) before attempting to do anything to a block. If the return is false, skip that block.

As for the proper way to dig it out, dig around in the vanilla blocks class and look for where ForgeHooks.modifyLoot is called. Once you find that spot you might have to go up the call stack a little bit before you find the exact thing to call, and if might be several things. Been a while since I looked at it.

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

It turned out that ServerPlayerGameMode was accessible after all. All I had to do was to cast my player to a ServerPlayer and ask for the game mode. Calling destroyBlock on that class seems to do all the proper tests and fire the right events. It all works now! Now I can mine an entire vein of diamonds or fell an entire tree in one stroke. Whohooo! :)

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

 Share



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Hey guys, I'm trying to register sounds that will be played at a specific location. For example: /playsound minecraft:ambient.cave block YageLyts ~ ~ ~15 This will play the sound ambient.cave at my position Z+15 and it works, the further I move the less I hear the sound. Now in my mod I register all my sounds using the sound event, but whenever I use that command, the sound is played no matter where I am on the map. It's like the sound is played as background. I've been trying to fix this for at least two hours but I can't find anything...   public class SoundRegistrator { public static final DeferredRegister<SoundEvent> REGISTRY = DeferredRegister.create(ForgeRegistries.SOUND_EVENTS, LoginMod.MOD_ID); public static final RegistryObject<SoundEvent> HOT_NOTIF = register("hot_notification"); public static final RegistryObject<SoundEvent> KEY_NOTIF = register("key_notifcation"); public static final RegistryObject<SoundEvent> MYST_LOOP = register("mystery_loop"); public static final RegistryObject<SoundEvent> NEWTREAS_NOTIF = register("newtreasure_notification"); public static final RegistryObject<SoundEvent> TREAS_FOUND = register("treasure_found"); public static final RegistryObject<SoundEvent> TREAS_FOUND_ANOTHER = register("treasure_foundby_anotherplayer"); private static RegistryObject<SoundEvent> register(String name) { return REGISTRY.register(name, () -> new SoundEvent(new ResourceLocation(LoginMod.MOD_ID, name))); } } public LoginMod() { FMLJavaModLoadingContext.get().getModEventBus().addListener(this::setup); FMLJavaModLoadingContext.get().getModEventBus().addListener(this::client); MinecraftForge.EVENT_BUS.register(this); PermissionAPI.registerNode("treasurehunt.setup", DefaultPermissionLevel.OP, "treasurehunt.setup"); SoundRegistrator.REGISTRY.register(FMLJavaModLoadingContext.get().getModEventBus()); } { "hot_notification": { "sounds": ["loginmod:hot_notification"] }, "key_notifcation": { "sounds": ["loginmod:key_notifcation"] }, "mystery_loop": { "sounds": [ { "name": "loginmod:mystery_loop", "stream": true } ] }, "newtreasure_notification": { "sounds": ["loginmod:newtreasure_notification"] }, "treasure_found": { "sounds": ["loginmod:treasure_found"] }, "treasure_foundby_anotherplayer": { "sounds": [ { "name": "loginmod:treasure_foundby_anotherplayer", "stream": true } ] } } Thank
    • You got hacked, didn't you?
    • wrong website 😐
    • https://myfemalefunda.com/الطاقة-الشمسية-في-المغرب/ https://kerbymethodconsulting.com/مشاريع-جديدة-ومبتكرة-2017-82582/ https://starsone.site/تحويل-الدولار-الى-الريال-العماني/ https://advocatesnairobi.com/سعر-عملة-حزم-اليوم/ https://starsone.site/يُعدُّ-البنك-المركزي-مؤسسة/
    • https://angosiam.com/ما-هي-حقوق-المرأة-في-تركيا/ https://myfemalefunda.com/مركز-دبي-للسلع-المتعددة/ https://sanakanwalfashion.com/اسعار-العقود-الاجلة-للداو-جونز/ https://kerbymethodconsulting.com/حساب-فاتورة-الكهرباء-الجزائر-2022-71662/ https://advocatesnairobi.com/كم-باقي-على-2030/
  • Topics

×
×
  • Create New...

Important Information

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