Jump to content

[Forge 1.10.2] Create a pickaxe that break unbreakable blocks?


lethinh

Recommended Posts

22 hours ago, TheMasterGabriel said:

You can read about Events here on the Forge docs.

I already know that. And I am doing an pickaxe that mine 10x10 blocks. Base on the TerraPick in Botania. But I am asking how to do that (in the event). My unfinished code:

 

Spoiler

@SubscribeEvent
    public void onPlayerDig(PlayerInteractEvent event) {
        if (event.getFace() == null || event.getWorld().isRemote
                || event.getEntityPlayer().applyPlayerInteraction(event.getEntityPlayer(),
                        event.getEntityPlayer().getForward(), event.getItemStack(),
                        event.getHand()) == EnumActionResult.PASS
                || event.getEntityPlayer().getHeldItem(event.getHand()) == null
                || event.getEntityPlayer().capabilities.isCreativeMode)
            return;
        IBlockState state = event.getWorld().getBlockState(event.getPos());
        Block block = state.getBlock();

        Random random = new Random();
        if (block.getBlockHardness(state, event.getWorld(), event.getPos()) <= -1 &&
                event.getEntityPlayer().getHeldItem(event.getHand()).getItem() == LudicrousItems.infinity_pickaxe &&
                (block.getMaterial(state) == Material.ROCK || block.getMaterial(state) == Material.IRON)) {
                LudicrousItems.infinity_pickaxe.onBlockStartBreak(event.getEntityPlayer().getHeldItem(event.getHand()),
                        event.getPos(), event.getEntityPlayer());
             } else {
                ItemStack drops = block.getPickBlock(state,
                        ToolCommons.raytraceFromEntity(event.getWorld(), event.getEntityPlayer(), true, 10),
                        event.getWorld(), event.getPos(), event.getEntityPlayer());
                if (block.quantityDropped(random) == 0) {
                    if (drops == null)
                        drops = new ItemStack(block, 1, block.getMetaFromState(state));
                    dropItems(drops, event.getWorld(), event.getPos());
                } else
                    block.harvestBlock(event.getWorld(), event.getEntityPlayer(), event.getPos(), state,
                            event.getWorld().getTileEntity(event.getPos()), drops);
                event.getWorld().setBlockToAir(event.getPos());
                event.getWorld().playEvent(2001, event.getPos(),
                        Block.getIdFromBlock(block) + (block.getMetaFromState(state) << 12));
            }
        }
    }

 

Link to comment
Share on other sites

12 hours ago, diesieben07 said:
  • Don't subscribe to the raw PlayerInteractEvent. Choose one of the sub-events.
  • Why are you calling applyPlayerInteraction like that? It makes no sense, you are telling the player to interact with itself. O.o
  • getForward is annotated with @SideOnly(CLIENT), which means you cannot use it in common code like this. You can learn more about sides in the documentation.
  • Why are you creating a new Random instance every time?
  • Block::getBlockHardness is deprecated, you should use IBlockState::getBlockHardness instead.
  • hardness <= -1 is not the condition for unbreakable. A block is unbreakable if it's hardness is below 0.
  • In 1.10.2 ItemStacks can still be null hence you cannot simply call getItem on the result of EntityPlayer::getHeldItem, since it might be null.
  • Block::getMaterial is deprecated, use IBlockState::getMaterial instead.
  • Why are you doing a ray-trace? You know which block was hit and you can also obtain the hit vector needed to create the RayTraceResult from the event.
  • You should not be calling quantityDropped, it is not accurate for all blocks. The only way to truly obtain a block's drops is to call Block::getDrops.
  • You should almost never call Block::getMetaFromState, especially not to construct an ItemStack.
  • Use Block.getStateId instead of manually constructing the ID from metadata.

Still doesn't work. Here is my newest code:

 

Spoiler

@SubscribeEvent
    public void onPlayerMine(PlayerInteractEvent.LeftClickBlock event) {
        if (event.getFace() == null || event.getWorld().isRemote
                || event.getEntityPlayer().getHeldItem(event.getHand()) == null
                || event.getEntityPlayer().capabilities.isCreativeMode)
            return;
        IBlockState state = event.getWorld().getBlockState(event.getPos());
        Block block = state.getBlock();
        if (state.getBlockHardness(event.getWorld(), event.getPos()) < 0 &&
                event.getEntityPlayer().getHeldItem(event.getHand()).getItem() == LudicrousItems.infinity_pickaxe &&
                (state.getMaterial() == Material.ROCK || state.getMaterial() == Material.IRON)) {
                LudicrousItems.infinity_pickaxe.onBlockStartBreak(event.getEntityPlayer().getHeldItem(event.getHand()),
                        event.getPos(), event.getEntityPlayer());
            } else {
                ItemStack drop = block.getPickBlock(state,
                        ToolHelper.raytraceFromEntity(event.getWorld(), event.getEntityPlayer(), true, 10),
                        event.getWorld(), event.getPos(), event.getEntityPlayer());
                if (block.getDrops(event.getWorld(), event.getPos(), state, EnchantmentHelper
                        .getEnchantmentLevel(Enchantments.FORTUNE,
                                new ItemStack(LudicrousItems.infinity_pickaxe))) == null) {
                    if (drop == null)
                        drop = new ItemStack(block, 1, block.getMetaFromState(state));
                    dropItems(drop, event.getWorld(), event.getPos());
                } else
                    block.harvestBlock(event.getWorld(), event.getEntityPlayer(), event.getPos(), state,
                            event.getWorld().getTileEntity(event.getPos()), drop);
                event.getWorld().setBlockToAir(event.getPos());
                event.getWorld().playEvent(2001, event.getPos(),
                        Block.getStateId(state));
            }
        }
    }

 

Edited by lethinh
Link to comment
Share on other sites

On 23/2/2017 at 3:46 PM, CrazyGriefer1337 said:

Just try the following:

-Test if the block is unbreakable (Bedrock, or something modded)

-If yes, execute the command "fill" (Minecraft command, not Java)

-If it doesn't work, throw your PC out of the window.

This is coding not normal MC!

Link to comment
Share on other sites

  • 4 weeks later...
On 27/2/2017 at 3:27 PM, diesieben07 said:
  • I am still not clear as to why you are calling onBlockStartBreak. If anything you need to completely emulate the block-breaking process as it normally occurs in PlayerInteractionManager.
  • You are still ray-tracing without the need to do that.
  • Block::getDrops will never return null.
  • You are still calling Block::getMetaFromState.

Still doesn't work!

Link to comment
Share on other sites

  • 2 weeks later...

On your newest code....

  • You are comparing ItemStack instances using == which will never work.
  • Except that what you are comparing is the return of block.getItem(world, pos, state)  with the return of block.getItem(world, pos, state) (i.e.. itself) which will always be true.
On 2/27/2017 at 3:27 AM, diesieben07 said:
  • You are still calling Block::getMetaFromState.

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

22 hours ago, Draco18s said:

On your newest code....

  • You are comparing ItemStack instances using == which will never work.
  • Except that what you are comparing is the return of block.getItem(world, pos, state)  with the return of block.getItem(world, pos, state) (i.e.. itself) which will always be true.

 

Do you know how to fix those errors? I have tried all!

Link to comment
Share on other sites

10 minutes ago, diesieben07 said:

You should not construct the dropped item stacks manually. Use Block::getDrops to obtain the drops, like I already said in my first post.

I can't. Block::getDrops is List<ItemStack> but the drop is a item stack.

Edited by lethinh
Link to comment
Share on other sites

1 minute ago, diesieben07 said:

What is "the drop"?

 

What? O.o

It is:

 

@SubscribeEvent
    public void onPlayerDig(PlayerInteractEvent.LeftClickBlock event) {
        World world = event.getWorld();
        EntityPlayer player = event.getEntityPlayer();

        if (event.getFace() == null || world.isRemote || StackUtils.isEmpty(player.getHeldItem(EnumHand.MAIN_HAND)) || player.isCreative())
            return;


        BlockPos pos = event.getPos();
        IBlockState state = world.getBlockState(pos);
        Block block = state.getBlock();
        List<ItemStack> drop = new ArrayList<>();
        if (block.getDrops(world, pos, state, EnchantmentHelper.getEnchantmentLevel(Enchantments.FORTUNE, new ItemStack(this))) == drop) {
            if (drop == null)
                drop = new ArrayList<>(block.getDrops(world, pos, state, EnchantmentHelper.getEnchantmentLevel(Enchantments.FORTUNE, new ItemStack(this))));
            InventoryHelper.spawnItemStack(world, (double) pos.getX(), (double) pos.getY(), (double) pos.getZ(), drop);
        } else
            block.harvestBlock(world, player, pos, state, world.getTileEntity(pos), drop);

        world.setBlockToAir(pos);
        world.playEvent(2001, pos, Block.getStateId(state));
    }

 

Link to comment
Share on other sites

 

Quote

if (block.getDrops(world, pos, state, EnchantmentHelper.getEnchantmentLevel(Enchantments.FORTUNE, new ItemStack(this))) == drop 

  • Now you are comparing a List<ItemStack> to an ItemStack, so the result will always be false. Why are you making this comparison anyway? What result are you actually trying to get?
  • You still keep using ==, which only checks for the exact same object, rather than for meaningfully equivalent objects. Two different ItemStacks made of the same Item and stacksize will not be equal with the == operator.
Quote

List<ItemStack> drop = new ArrayList<>()

...

if (drop == null)

  • How could drop possibly be null here after you have set it to a new ArrayList? (Hint: it couldn't).

You seem to be having trouble with some fairly fundamental Java/OOP concepts, you might be better off learning some general programming first.

 

Link to comment
Share on other sites

19 hours ago, Jay Avery said:

 

  • Now you are comparing a List<ItemStack> to an ItemStack, so the result will always be false. Why are you making this comparison anyway? What result are you actually trying to get?
  • You still keep using ==, which only checks for the exact same object, rather than for meaningfully equivalent objects. Two different ItemStacks made of the same Item and stacksize will not be equal with the == operator.
  • How could drop possibly be null here after you have set it to a new ArrayList? (Hint: it couldn't).

You seem to be having trouble with some fairly fundamental Java/OOP concepts, you might be better off learning some general programming first.

 

I have just learned java for few months. And the code didn't work:

 

@SubscribeEvent
public void onPlayerDig(PlayerInteractEvent.LeftClickBlock event) {
    World world = event.getWorld();
    EntityPlayer player = event.getEntityPlayer();

    if (event.getFace() == null || world.isRemote || StackUtils.isEmpty(player.getHeldItem(EnumHand.MAIN_HAND)) || player.isCreative())
        return;

    BlockPos pos = event.getPos();
    IBlockState state = world.getBlockState(pos);
    Block block = state.getBlock();
    List<ItemStack> drop = new ArrayList<>();

    if (drop == null)
        drop = new ArrayList<>(block.getDrops(world, pos, state, EnchantmentHelper.getEnchantmentLevel(Enchantments.FORTUNE, new ItemStack(this))));

    block.harvestBlock(world, player, pos, state, world.getTileEntity(pos), new ItemStack(block));
    InventoryHelper.spawnItemStack(world, pos.getX(), pos.getY(), pos.getZ(), new ItemStack(block));

    world.setBlockToAir(pos);
    world.playEvent(2001, pos, Block.getStateId(state));
}
Link to comment
Share on other sites

50 minutes ago, lethinh said:

 


    List<ItemStack> drop = new ArrayList<>();

    if (drop == null)

You're still doing this pointless check. And now you do nothing with the drop variable after you create it anyway.

 

When you say the code doesn't work, please be more specific. Exactly what does and doesn't happen, and what are you expecting to happen? At this point I've lost track of the problem.

Link to comment
Share on other sites

2 hours ago, Jay Avery said:

You're still doing this pointless check. And now you do nothing with the drop variable after you create it anyway.

 

When you say the code doesn't work, please be more specific. Exactly what does and doesn't happen, and what are you expecting to happen? At this point I've lost track of the problem.

 

It doesn't break unbreakable block. That's the point of the code.

Link to comment
Share on other sites

 List<ItemStack> drop = new ArrayList<>();

Cool, I have an array object

if (drop == null)

Nope, it's not null. It's an ArrayList of size zero.

block.harvestBlock(world, player, pos, state, world.getTileEntity(pos), new ItemStack(block));
InventoryHelper.spawnItemStack(world, pos.getX(), pos.getY(), pos.getZ(), new ItemStack(block));

And now, I ignore the fuck out of my array object.

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

23 hours ago, Jay Avery said:

Does it break ordinary breakable blocks? Does the game crash? Does the event get called?

It breaks ordinary breakable blocks. The game doesn't crash. And I have called the event by using MinecraftForge.EventBus::register

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
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
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.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • They were already updated, and just to double check I even did a cleanup and fresh update from that same page. I'm quite sure drivers are not the problem here. 
    • i tried downloading the drivers but it says no AMD graphics hardware has been detected    
    • Update your AMD/ATI drivers - get the drivers from their website - do not update via system  
    • As the title says i keep on crashing on forge 1.20.1 even without any mods downloaded, i have the latest drivers (nvidia) and vanilla minecraft works perfectly fine for me logs: https://pastebin.com/5UR01yG9
    • Hello everyone, I'm making this post to seek help for my modded block, It's a special block called FrozenBlock supposed to take the place of an old block, then after a set amount of ticks, it's supposed to revert its Block State, Entity, data... to the old block like this :  The problem I have is that the system breaks when handling multi blocks (I tried some fix but none of them worked) :  The bug I have identified is that the function "setOldBlockFields" in the item's "setFrozenBlock" function gets called once for the 1st block of multiblock getting frozen (as it should), but gets called a second time BEFORE creating the first FrozenBlock with the data of the 1st block, hence giving the same data to the two FrozenBlock :   Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=head] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@73681674 BlockEntityData : id:"minecraft:bed",x:3,y:-60,z:-6} Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=3, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=2, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} here is the code inside my custom "freeze" item :    @Override     public @NotNull InteractionResult useOn(@NotNull UseOnContext pContext) {         if (!pContext.getLevel().isClientSide() && pContext.getHand() == InteractionHand.MAIN_HAND) {             BlockPos blockPos = pContext.getClickedPos();             BlockPos secondBlockPos = getMultiblockPos(blockPos, pContext.getLevel().getBlockState(blockPos));             if (secondBlockPos != null) {                 createFrozenBlock(pContext, secondBlockPos);             }             createFrozenBlock(pContext, blockPos);             return InteractionResult.SUCCESS;         }         return super.useOn(pContext);     }     public static void createFrozenBlock(UseOnContext pContext, BlockPos blockPos) {         BlockState oldState = pContext.getLevel().getBlockState(blockPos);         BlockEntity oldBlockEntity = oldState.hasBlockEntity() ? pContext.getLevel().getBlockEntity(blockPos) : null;         CompoundTag oldBlockEntityData = oldState.hasBlockEntity() ? oldBlockEntity.serializeNBT() : null;         if (oldBlockEntity != null) {             pContext.getLevel().removeBlockEntity(blockPos);         }         BlockState FrozenBlock = setFrozenBlock(oldState, oldBlockEntity, oldBlockEntityData);         pContext.getLevel().setBlockAndUpdate(blockPos, FrozenBlock);     }     public static BlockState setFrozenBlock(BlockState blockState, @Nullable BlockEntity blockEntity, @Nullable CompoundTag blockEntityData) {         BlockState FrozenBlock = BlockRegister.FROZEN_BLOCK.get().defaultBlockState();         ((FrozenBlock) FrozenBlock.getBlock()).setOldBlockFields(blockState, blockEntity, blockEntityData);         return FrozenBlock;     }  
  • Topics

×
×
  • Create New...

Important Information

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