Jump to content

[1.8 - Solved] Help in making custom ItemSeeds drop in specific biomes


Recommended Posts

Posted

Hello all,

 

I basically added a new type of Crop as well as Seeds and the Item harvested from the crop at final stage. By default, you just break tall grass till you get the new seeds, like wheat seeds. You plant them, they grow, you harvest the crop and maybe get more seeds.

 

Thing is, I would like to make the seeds dropping from the wild tall grass to only drop if the tall grass is in a specific biome. I thought of using getBiomeGenForCoords to return the biome for which the tall grass broken is in, but I just cannot get it to work.

 

I have taken basic tutorials on Java and most of the time I work alone till I figure out how to solve my coding problems, but I am not as experienced as you guys, so can you please point me into the right direction on this one?

 

I thought of checking addGrassSeed like this:

 

public class DropSeeds {

 

public static void dropSeeds(PlayerInteractEvent event, World world) {

 

BlockPos CheckGrassPos = event.pos;

BiomeGenBase biome = world.getBiomeGenForCoords(CheckGrassPos);

 

if (biome instanceof BiomeGenSwamp) {

MinecraftForge.addGrassSeed(new ItemStack(Items.riceSeeds), 10);

}

}

}

 

Then calling it in in CommonProxy like this:

 

public void init(FMLInitializationEvent e) {

Recipes.init();

GameRegistry.registerWorldGenerator(new WorldGen(), 0);

DropSeeds.dropSeeds();

}

 

 

Thing is that it needs the event and world arguments from the DropSeed class.

 

Any help would be appreciated. Sorry if the solution on this one is easy, I'm trying to get better at Java.  :D

'Everyone is a genius,

But if you judge a fish by its ability to climb trees, it will live its whole life believing that it is stupid.'

 

-Albert Einstein

----------------------------------------------------------

If Arthas's horse is named "Invincible", then why can I clearly see him?

Posted

Instead wait for the item drop event, and if seeds drop and its the right biome, replace the seed with your seed.

 

addGrassSeed will permanently add it to the list of dropable seeds.

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

Thanks for your quick reply, unlike this one. I have been trying for a couple of hours to make this work. Probably someone more experienced  could have done this in a matter of a minute.

 

A while back I have managed to make the custom seeds always drop in place of wheat seeds in swamps, and both types dropping anywhere else. I was happy since I figured out how to alter where the seeds drop. But I wanted the custom seeds to only drop in swamp (don't mind if wheat seeds also drop there), i.e. to not drop elsewhere.

 

I altered my code and introduced an Event like so:

 

 

@SubscribeEvent

public void dropSeeds(BlockEvent.HarvestDropsEvent event) {

MinecraftForge.addGrassSeed(new ItemStack(panzerfaust.minecraftplus.items.Items.riceSeeds), 10);

BiomeGenBase biome = event.world.getBiomeGenForCoords(event.pos);

boolean swampBiome;

 

if (biome instanceof BiomeGenSwamp){

swampBiome = true;

}

else swampBiome = false;

 

if (event.drops == panzerfaust.minecraftplus.items.Items.riceSeeds && swampBiome == false) {

event.dropChance = 1.0F;

event.drops.add(new ItemStack(Items.wheat_seeds));

}

}

 

 

I also added this to the Init method of CommonProxy:

 

 

MinecraftForge.EVENT_BUS.register(new DropSeeds());

 

 

Now I get only the custom seeds to drop everywhere. But I do not understand how does the above not work. Looks like as if it should work. Any heads up? I'll continue working on this. I'll update when I have made progress.

'Everyone is a genius,

But if you judge a fish by its ability to climb trees, it will live its whole life believing that it is stupid.'

 

-Albert Einstein

----------------------------------------------------------

If Arthas's horse is named "Invincible", then why can I clearly see him?

Posted

Good stab at the problem.  But you're doing a few things wrong.

 

drops

is an array and it will never equal, or contain, your item.  You're going to want to loop through looking to see if any of the array items are

Items.seeds

and then remove that item and add yours.

 

Here's what I did:

	@SubscribeEvent
public void harvestGrass(BlockEvent.HarvestDropsEvent event) {
	if(event.block instanceof BlockTallGrass) { //cuts down on checking when the block destroyed was stone, logs, leaves, etc.
		BiomeGenBase bio = event.world.getBiomeGenForCoords(event.x, event.z);
		if(biomeTemps.get(bio.biomeID).temp <= 0.3) { //the condition you are interested in
			ArrayList<ItemStack> drps = new ArrayList<ItemStack>();
			for(ItemStack is:event.drops) {
				if(is.getItem() == Items.wheat_seeds) {
					drps.add(new ItemStack(WildlifeBase.winterWheatSeeds, is.stackSize)); //add the new item
				}
				else {
					drps.add(is); //add any other items that dropped, but which we're not replacing
				}
			}
			event.drops.clear(); //remove the old
			event.drops.addAll(drps);
		}
	}
}

 

The second array is for avoiding modifying the array during the loop, but there are other ways to do that as well, such as using an iterator.

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

Oh I see. You are creating an array list of ItemStacks to pass on for drops.

 

I do not like replying that I still have a problem but this has been boggling  my brain for a couple of hours today.

I used the way you used your array and itemstack in a variety of ways, yet I still got problems. Currently it is like this:

 

@SubscribeEvent

public void dropSeeds(BlockEvent.HarvestDropsEvent event) {

 

MinecraftForge.addGrassSeed(new ItemStack(panzerfaust.minecraftplus.items.Items.riceSeeds), 10);

BiomeGenBase biome = event.world.getBiomeGenForCoords(event.pos);

boolean biomeSwamp;

 

if(biome instanceof BiomeGenSwamp) {

biomeSwamp = true;

} else biomeSwamp = false;

 

if(biomeSwamp == false) {

ArrayList<ItemStack> drps = new ArrayList<ItemStack>();

for(ItemStack is:event.drops) {

if(is.getItem() == panzerfaust.minecraftplus.items.Items.riceSeeds) {

}

else {

drps.add(is);

}

event.drops.clear();

event.drops.addAll(drps);

}

}

}

 

What I expect from the above is to check when do the custom seeds drop while in not a swamp. Then if they do change the drop to not include them. What I get is only the custom seeds dropping in swamps and no seeds dropping elsewhere.

 

Also, I wanted from before to be able to detect if it is tall grass that is broken. I was surprised to find out that I could not use event.block like you did. I was not sure of what to use instead from the available options, so for my first attempts I put:

 

Object Grass = new BlockTallGrass(){};

if(event.equals(Grass)){

 

I removed it. But I cannot understand why does the first spoiler not work.

 

Also minor detail, when the event first fires from the very first seeds (can be either) dropping elsewhere than a swamp, they will drop instead of not. Cute detail.

'Everyone is a genius,

But if you judge a fish by its ability to climb trees, it will live its whole life believing that it is stupid.'

 

-Albert Einstein

----------------------------------------------------------

If Arthas's horse is named "Invincible", then why can I clearly see him?

Posted

1) Don't use

addGrassSeed

, you want your seeds to ONLY drop in the swamp, so there's no point in adding them to the global drop list.

2) You do not need your

biomeSwamp

variable.  If it's a swamp, do-the-thing, if it's not do-the-other-thing.  There's no point in setting the result of a boolean check and then if-checking that boolean again.

3)

if(is == Items.seeds) { drps.add(new ItemStack(riceSeeds)); }

4) Move the 

event.drops.clear();

and

event.drops.addAll(drps);

lines outside of the loop.

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

I got it to work. And changed the code some more to alter drop rate in different biomes, for different custom seeds. But one thing is left. I still cannot understand how could you add the block field in your if statement: if(event.block instanceof BlockTallGrass)

There is no block field in HarvestDropsEvent. And when I tried to add a Block argument in the dropSeeds constructor, I could not register the event with the event bus of common proxy.

'Everyone is a genius,

But if you judge a fish by its ability to climb trees, it will live its whole life believing that it is stupid.'

 

-Albert Einstein

----------------------------------------------------------

If Arthas's horse is named "Invincible", then why can I clearly see him?

Posted

There is no block field in HarvestDropsEvent. And when I tried to add a Block argument in the dropSeeds constructor, I could not register the event with the event bus of common proxy.

 

Yes there is.

 

public HarvestDropsEvent(int x, int y, int z, World world, Block block, int blockMetadata, int fortuneLevel, float dropChance, ArrayList<ItemStack> drops, EntityPlayer harvester, boolean isSilkTouching)

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

public HarvestDropsEvent(World world, BlockPos pos, IBlockState state, int fortuneLevel, float dropChance, List<ItemStack> drops, EntityPlayer harvester, boolean isSilkTouching)

        {

            super(world, pos, state);

            this.fortuneLevel = fortuneLevel;

            this.dropChance = dropChance;

            this.drops = drops;

            this.isSilkTouching = isSilkTouching;

            this.harvester = harvester;

        }

 

forge1.8-11.14.0.1295-1.8

'Everyone is a genius,

But if you judge a fish by its ability to climb trees, it will live its whole life believing that it is stupid.'

 

-Albert Einstein

----------------------------------------------------------

If Arthas's horse is named "Invincible", then why can I clearly see him?

Posted

I cannot use event.block to check if the block firing the HarvestDropEvent is Grass or not. Sorry if it seems to be asking for help constantly and bringing this post up again and again :< I just have very basic knowledge of java and have trouble figuring stuff out.

 

I stopped working on dropSeed for now, and doing something I'm better at; modeling some mobs in Techne to add later on.

I'll try to find a way to get the block type of the block that fires the Event a bit later on.

'Everyone is a genius,

But if you judge a fish by its ability to climb trees, it will live its whole life believing that it is stupid.'

 

-Albert Einstein

----------------------------------------------------------

If Arthas's horse is named "Invincible", then why can I clearly see him?

Posted

public HarvestDropsEvent(World world, BlockPos pos, IBlockState state, int fortuneLevel, float dropChance, List<ItemStack> drops, EntityPlayer harvester, boolean isSilkTouching)

        {

            super(world, pos, state);

            this.fortuneLevel = fortuneLevel;

            this.dropChance = dropChance;

            this.drops = drops;

            this.isSilkTouching = isSilkTouching;

            this.harvester = harvester;

        }

 

forge1.8-11.14.0.1295-1.8

 

I think hes saying there is no block parameter...

 

But they do give you a BlockPos which you can use in the world.getBlockByPos(or something: not in an IDE right now) to get the block at the BlockPos and then you can check whether that block is a tall grass.

I'm back from being gone for... I think its been about a year. I'm pretty sure nobody remembers me, but hello anybody who does!

Posted

Yes. Sorry for not being clear on my previous previous post. I did use event.world and event.pos in order to try and find a parameter to help me get the block firing the event, yet I could not find any. I'll try again later on. Again, sorry for bringing this post up again. I know you guys are busy and I appreciate you being helpful. In fact Draco helped me a lot with the code.

'Everyone is a genius,

But if you judge a fish by its ability to climb trees, it will live its whole life believing that it is stupid.'

 

-Albert Einstein

----------------------------------------------------------

If Arthas's horse is named "Invincible", then why can I clearly see him?

Posted

If you look more at the HarvestDropsEvent code it's

 

public class BlockEvent extends Event
{
    private static final boolean DEBUG = Boolean.parseBoolean(System.getProperty("forge.debugBlockEvent", "false"));

    public final World world;
    public final BlockPos pos;
    public final IBlockState state;
    public BlockEvent(World world, BlockPos pos, IBlockState state)
    {
        this.pos = pos;
        this.world = world;
        this.state = state;
    }

    // Comments
    public static class HarvestDropsEvent extends BlockEvent
    {
        public final int fortuneLevel;
        public final List<ItemStack> drops;
        public final boolean isSilkTouching;
        public float dropChance; // Change to e.g. 1.0f, if you manipulate the list and want to guarantee it always drops
        public final EntityPlayer harvester; // May be null for non-player harvesting such as explosions or machines

        public HarvestDropsEvent(World world, BlockPos pos, IBlockState state, int fortuneLevel, float dropChance, List<ItemStack> drops, EntityPlayer harvester, boolean isSilkTouching)
        {
            super(world, pos, state);
            this.fortuneLevel = fortuneLevel;
            this.dropChance = dropChance;
            this.drops = drops;
            this.isSilkTouching = isSilkTouching;
            this.harvester = harvester;
        }
    }

 

It's extending BlockEvent, all BlockEvent's a state parameter which is an instance of IBlockState, IBlockState has a method getBlock. You can get the block this way.

Posted

woohoo!

 

if(biome instanceof BiomeGenSwamp && event.state.getBlock() instanceof BlockTallGrass) { //...

 

Indeed I can get the block that way. event.state.getBlock() works to perfection. I read a big tutorial on Events in general so I can know what fields and methods to use on different occasions.

 

This post can finally start sinking down in the forums. Might  be useful for someone who is making custom crops and wants to know how to make biome specific seed drops, though.

 

Thanks everyone for helping. May karma be with you. NOW I CAN FINALLY MODEL IN PEACE    :3

'Everyone is a genius,

But if you judge a fish by its ability to climb trees, it will live its whole life believing that it is stupid.'

 

-Albert Einstein

----------------------------------------------------------

If Arthas's horse is named "Invincible", then why can I clearly see him?

Posted

Sorry for bringing this up again, I just want to add that I found an easier way to set this up (for 1.8 Forge. I've been told told that for 1.7 Forge if(biome instanceof BiomeGenSwamp) { includes the swampland variant, yet this was not the case for me):

 

@SubscribeEvent

public void dropSeeds(BlockEvent.HarvestDropsEvent event) {

BiomeGenBase biome = event.world.getBiomeGenForCoords(event.pos);

 

if(BiomeDictionary.isBiomeOfType(biome, Type.SWAMP)){

if(event.state.getBlock() == Blocks.tallgrass){

event.dropChance = (float) 0.05;

event.drops.add(new ItemStack(panzerfaust.minecraftplus.items.Items.riceSeeds));

}

}

}

 

This effectively adds the seeds as a new drop. Also, by using Biome Dictionary, you also include the Swampland variant (Swamp M in F3) instead of just the Swamp biome. I was lucky that my world had both Swamp and Swamp M side by side to check that by using 'instanceof BiomeGenSwamp'.

 

If one wants to mess around with details then they can code additional lines if they do not want both types of seeds to drop at the same time from the same block of tall grass (or not wanting both types to drop in general).

 

Sorry for necroing, edited some ambiguities and corrected something unnecessary in the code. Thanks Draco18s.

'Everyone is a genius,

But if you judge a fish by its ability to climb trees, it will live its whole life believing that it is stupid.'

 

-Albert Einstein

----------------------------------------------------------

If Arthas's horse is named "Invincible", then why can I clearly see him?

Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

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