Jump to content

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


Recommended Posts

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

 

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

Can you use the code tag please? (<>)that thing. Currently I cant even tell what you are doing

Apparently I'm addicted to these forums and can't help but read all the posts. So if I somehow help you, please click the "Like This" button, it helps.

Posted
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!

  • 4 weeks later...
Posted
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!

  • 2 weeks later...
Posted

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.

Posted
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!

Posted
22 hours ago, diesieben07 said:

Please show what you tried and why it didn't work.

 

All the code that I have shown to you. And could you show me how to not call Block::getMetaFromState?

Posted (edited)
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
Posted
18 minutes ago, Jay Avery said:

So return a list that contains just one stack.

 

Can't be. If I do that, the method InventoryHelper::spawnItemStack and Block::harvestBlock WILL NOT WORK

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

 

Posted

 

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.

 

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

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

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

Posted
1 hour ago, lethinh said:

 

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

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

Posted
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

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

    • This is the last line before the crash: [ebwizardry]: Synchronising spell emitters for PixelTraveler But I have no idea what this means
    • What in particular? I barely used that mod this time around, and it's never been a problem in the past.
    • Im trying to build my mod using shade since i use the luaj library however i keep getting this error Reason: Task ':reobfJar' uses this output of task ':shadowJar' without declaring an explicit or implicit dependency. This can lead to incorrect results being produced, depending on what order the tasks are executed. So i try adding reobfJar.dependsOn shadowJar  Could not get unknown property 'reobfJar' for object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler. my gradle file plugins { id 'eclipse' id 'idea' id 'maven-publish' id 'net.minecraftforge.gradle' version '[6.0,6.2)' id 'com.github.johnrengelman.shadow' version '7.1.2' id 'org.spongepowered.mixin' version '0.7.+' } apply plugin: 'net.minecraftforge.gradle' apply plugin: 'org.spongepowered.mixin' apply plugin: 'com.github.johnrengelman.shadow' version = mod_version group = mod_group_id base { archivesName = mod_id } // Mojang ships Java 17 to end users in 1.18+, so your mod should target Java 17. java.toolchain.languageVersion = JavaLanguageVersion.of(17) //jarJar.enable() println "Java: ${System.getProperty 'java.version'}, JVM: ${System.getProperty 'java.vm.version'} (${System.getProperty 'java.vendor'}), Arch: ${System.getProperty 'os.arch'}" minecraft { mappings channel: mapping_channel, version: mapping_version copyIdeResources = true runs { configureEach { workingDirectory project.file('run') property 'forge.logging.markers', 'REGISTRIES' property 'forge.logging.console.level', 'debug' arg "-mixin.config=derp.mixin.json" mods { "${mod_id}" { source sourceSets.main } } } client { // Comma-separated list of namespaces to load gametests from. Empty = all namespaces. property 'forge.enabledGameTestNamespaces', mod_id } server { property 'forge.enabledGameTestNamespaces', mod_id args '--nogui' } gameTestServer { property 'forge.enabledGameTestNamespaces', mod_id } data { workingDirectory project.file('run-data') args '--mod', mod_id, '--all', '--output', file('src/generated/resources/'), '--existing', file('src/main/resources/') } } } sourceSets.main.resources { srcDir 'src/generated/resources' } repositories { flatDir { dirs './libs' } maven { url = "https://jitpack.io" } } configurations { shade implementation.extendsFrom shade } dependencies { minecraft "net.minecraftforge:forge:${minecraft_version}-${forge_version}" implementation 'org.luaj:luaj-jse-3.0.2' implementation fg.deobf("com.github.Virtuoel:Pehkui:${pehkui_version}") annotationProcessor 'org.spongepowered:mixin:0.8.5:processor' minecraftLibrary 'luaj:luaj-jse:3.0.2' shade 'luaj:luaj-jse:3.0.2' } // Example for how to get properties into the manifest for reading at runtime. tasks.named('jar', Jar).configure { manifest { attributes([ 'Specification-Title' : mod_id, 'Specification-Vendor' : mod_authors, 'Specification-Version' : '1', // We are version 1 of ourselves 'Implementation-Title' : project.name, 'Implementation-Version' : project.jar.archiveVersion, 'Implementation-Vendor' : mod_authors, 'Implementation-Timestamp': new Date().format("yyyy-MM-dd'T'HH:mm:ssZ"), "TweakClass" : "org.spongepowered.asm.launch.MixinTweaker", "TweakOrder" : 0, "MixinConfigs" : "derp.mixin.json" ]) } rename 'mixin.refmap.json', 'derp.mixin-refmap.json' } shadowJar { archiveClassifier = '' configurations = [project.configurations.shade] finalizedBy 'reobfShadowJar' } assemble.dependsOn shadowJar reobf { re shadowJar {} } publishing { publications { mavenJava(MavenPublication) { artifact jar } } repositories { maven { url "file://${project.projectDir}/mcmodsrepo" } } } my entire project:https://github.com/kevin051606/DERP-Mod/tree/Derp-1.0-1.20
    • All versions of Minecraft Forge suddenly black screen even without mods (tried reinstalling original Minecraft, Java, updating drivers doesn't work)
  • Topics

×
×
  • Create New...

Important Information

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