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

Edit: Forgot to include forge version: 1.12.2 - 14.23.2.2611

 

In my BlockDropHandlerClass, I'm trying to replace the drop of custom leaves I have with the sapling (the reason I do this is to have an ambiguous class so I don't have to define a drop in the class), but it doesn't look like

Spoiler

if (state.getBlock() instanceof IShearable && event.getHarvester() != null && event.getHarvester().getActiveItemStack().getItem() instanceof ItemShears) return;

is being run at all. The leaves drop the sapling every time (with a 20 percent chance, as per the method), regardless of if I use shears or not. I'm thinking event.getHarvester() is returning null, but I'm not sure why it would do that.

 

Class:

Spoiler

package com.starv.arcticarrival2.util.handler;

import java.util.Random;

import com.starv.arcticarrival2.registry.ModBlocks;

import net.minecraft.block.Block;
import net.minecraft.block.state.IBlockState;
import net.minecraft.item.Item;
import net.minecraft.item.ItemShears;
import net.minecraft.item.ItemStack;
import net.minecraftforge.common.IShearable;
import net.minecraftforge.event.world.BlockEvent.HarvestDropsEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;

public class BlockDropHandler
{
    private static Random rand = new Random();
    
    @SubscribeEvent
    public void blockDrops(HarvestDropsEvent event)
    {
        replaceDrop(event, ModBlocks.diseased_grass, ModBlocks.diseased_dirt, 1, true);
        replaceDrop(event, ModBlocks.polluted_stone, ModBlocks.polluted_cobblestone, 1, true);
        replaceDrop(event, ModBlocks.taintwood_leaves, ModBlocks.taintwood_sapling, 1, 20, true);
//        replaceDrop(event, ModBlocks.tall_grass, ModItems.seeds, 1, 13);
    }

    private static void replaceDrop(HarvestDropsEvent event, Block blockIn, Block blockOut, int amount, boolean clearDrops)
    {
        replaceDrop(event, blockIn, new ItemStack(blockOut, amount), 100, clearDrops);
    }
    
    private static void replaceDrop(HarvestDropsEvent event, Block blockIn, Item itemOut, int amount, boolean clearDrops)
    {
        replaceDrop(event, blockIn, new ItemStack(itemOut, amount), 100, clearDrops);
    }
    
    private static void replaceDrop(HarvestDropsEvent event, Block blockIn, Block blockOut, int amount, int chance, boolean clearDrops)
    {
        replaceDrop(event, blockIn, new ItemStack(blockOut, amount), chance, clearDrops);
    }
    
    private static void replaceDrop(HarvestDropsEvent event, Block blockIn, Item itemOut, int amount, int chance, boolean clearDrops)
    {
        replaceDrop(event, blockIn, new ItemStack(itemOut, amount), chance, clearDrops);
    }
    
    private static void replaceDrop(HarvestDropsEvent event, Block blockIn, ItemStack stackOut, int chance, boolean clearDrops)
    {
        IBlockState state = event.getState();
        if (state.getBlock() == blockIn && !event.isSilkTouching())
        {
            if (state.getBlock() instanceof IShearable && event.getHarvester() != null && event.getHarvester().getActiveItemStack().getItem() instanceof ItemShears) return;
            if (clearDrops) event.getDrops().clear();
            if (rand.nextInt(100) < chance) event.getDrops().add(stackOut);
        }
    }
}

 

 

Edited by StarV

  • Author

I want to check if shears are being used; if so, don't replace the drop, since the default drop of the leaves is itself.

  • Author

Sorry it took so long to reply, I think the authentication servers went down and I'm using my account in eclipse.

 

I changed the part of the method to this:

Spoiler

if (state.getBlock() instanceof IShearable && event.getHarvester() != null && event.getHarvester().getActiveItemStack().getItem() instanceof ItemShears)
            {
                System.out.println("Test");
                return;
            }

but it didn't output anything.

  • Author

I set a breakpoint on the System.out.println("test") and ran in debug mode, it did nothing.

Set a breakpoint somewhere else, like at the top of the event handler method.

 

Your goal is to get the program to halt and let you examine why it is NOT going into a specific path, not put the break inside the path you want and then complain on the forums that it didn't go there. We already knew that.

Edited by Draco18s

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

I set it at the top of the replaceDrop method. After going through the "Step Into"s, it looks like it skips over

Spoiler

if (state.getBlock() == blockIn && !event.isSilkTouching())

for the leaves. I tested it with the stone, and it drops cobblestone.

 

Sorry if I misunderstand anything, I haven't really used breakpoints before except once.

Sooo...examine the variables at that point.

Is the block the desired block? Is the event silk touch (or not)?

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, looking into it, it stops after checking if state.getBlock == blockIn. I put silk touch on the shears, and Step Into went through that, but with normal shears, it checked the above, didn't check if it's silk touch, then just went to the end of the method.

What was the value of state.getBlock() and the value of blockIn?

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

It didn't tell me state.getBlock(), but state was

Spoiler

"BlockStateContainer$StateImplementation  (id=151)"

(arcticarrival2:taintwood_leaves)

and blockIn was

Spoiler

ABlockLeaves  (id=139)

(Block{arcticarrival2:taintwood_leaves})

 

I added Block block = state.getBlock() afterwards and it was

Spoiler

ABlockLeaves  (id=139)

(Block{arcticarrival2:taintwood_leaves})

 

I'm not sure what the problem is, I can't help you other than to say "use the debugger and figure out which value is not what you expect, then find out why"

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.

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.