Jump to content

Creating a crafting handler the exports multiple items


gmod622

Recommended Posts

Hey all!

 

So I have this custom block 'chemist lab' that has one input and multiple exports.

 

Now how would I setup a crafting handler that could support something like this.

 

And possible 'random chance' functionality

 

Thanks for Reading!

Not new to java >> New to modding.

Link to comment
Share on other sites

Hey all!

 

So I have this custom block 'chemist lab' that has one input and multiple exports.

 

Now how would I setup a crafting handler that could support something like this.

 

And possible 'random chance' functionality

 

Thanks for Reading!

I think it is quite obvious A Map of ItemStacks and ItemStack arrays and then a map of ItemStack and either integers or floats/doubles.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

Would you know how I would add the regular furnace recipes AND the ones that im creating?

There are multiple one being your custom methods for getting a recipe look in FurnaceRecipes and return that aswell as look into your own, add yours similarly to FurnaceRecipes, but to your Maps. Or you can add the furnace recipes by looping through them and adding them. Though that wouldn't  give you random chance.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

You'll want a custom class that holds your outputs and their probabilities, and you just need to store that in a HashMap<ItemStack,Outputs>

 

I do something like that here:

https://github.com/Draco18s/ReasonableRealism/blob/master/src/main/java/com/draco18s/flowers/FlowerDataHandler.java#L34

 

Tuple is just a generic class that wraps around two or more datatypes as a relationship. I probably should use a custom class for it, but I was thinking more in terms of the map from blocks -> flowers

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

So the outputs should be stored as ItemStacks put into an array? This is my attempt at the hashmap:

 

   private final Map<ItemStack, Array> multiOutput = Maps.<ItemStack, Array>newHashMap();

 

 

Now my problem is setting this array to itemstacks.

Not new to java >> New to modding.

Link to comment
Share on other sites

Well, there's two problems, still:

 

1) ItemStacks don't work as keys properly (do not override equals or hashcode) so you'd have to iterate over the map manually (furnace recipes does this) or you can create a wrapper class (like I did for blocks;

BlockWrapper

, as I needed more information than ever a IBlockState would hold, specifically a wildcard value)

 

2) Array is the wrong type. You want List<ItemStack> but that doesn't handle your random chance values.  You'll want another wrapper, storing the probability and the item.

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

So how would I lay out this 'wrapper' class.. I cant seem to find your blockwrapper class for reference.

 

This is what I have now:

 

    private Map<ItemWrapper, Tuple<ItemStack, List<ItemStack>>> smeltmulti = new HashMap();

Not new to java >> New to modding.

Link to comment
Share on other sites

You don't need the Tuple.  You want something like this:

 

private Map<ItemWrapper, List<OutputWrapper>> smeltmulti = new HashMap();

 

Then this:

public class OutputWrapper{
    public final float chance;
    public final ItemStack item;

    public OutputWrapper(float c, ItemStack i) {
        chance = c;
        item = i;
    }
}

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

Okay, Would I create a new function within the handler, so something like this?:

 

    public void addMultiSmeltingRecipe(ItemStack input, List<OutputWrapper> stack)
    {
        this.smeltmulti.put(input, stack);
    }

 

so to create a recipe it would be:

        this.addSmeltingMulti(Items.BONE, //No clue what goes here , //0.0 - 1.0);

Not new to java >> New to modding.

Link to comment
Share on other sites

Okay, Would I create a new function within the handler, so something like this?:

 

    public void addMultiSmeltingRecipe(ItemStack input, List<OutputWrapper> stack)
    {
        this.smeltmulti.put(input, stack);
    }

 

so to create a recipe it would be:

        this.addSmeltingMulti(Items.BONE, //No clue what goes here , //0.0 - 1.0);

With the way it is setup each Output has a percentage of appearing and to add a recipe you need to make a new List<OutputWrapper> and add new ItemWrappers to the list, then finally add the recipe using the list.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

Pretty much.

e.g.

 

List<OutputWrapper> list = new ArrayList<OutputWrapper>();
list.add(new OutputWrapper(1, Items.APPLE)); //apple because why not
list.add(new OutputWrapper(0.2, Items.GOLD_NUGGET)); //arbitrary
this.addSmeltingMulti(Items.BONE, list);

 

That would mean that when you smelted a bone, you'd always get an apple and 20% of the time you'd get a gold nugget.

You'd probably want to limit your secondaries to a maximum of your total output slots (e.g. if you have 3 output slots, have no more than 3 output items per input item).  This is assuming that your random is based on floats. If you do integers, you may have to refactor to match your desired design goal (e.g. if you always roll

rand.nextInt(100)

then you pass in "20" as your 20%).

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

Okay, now I got it down, however the Item won't smelt.

 

And yes this is applying to the correct tile :)

 

Anyways, here is the registration:

        List<OutputWrapper> list = new ArrayList<OutputWrapper>();
        list.add(new OutputWrapper(1.0F, new ItemStack (Blocks.STONE)));
        list.add(new OutputWrapper(0.05F, new ItemStack(ModItems.AtPAP2))); 
        list.add(new OutputWrapper(0.025F, new ItemStack(Blocks.IRON_ORE))); 
        list.add(new OutputWrapper(0.2F, new ItemStack(Items.GOLD_NUGGET))); 
        this.addMultiSmeltingRecipe(new ItemStack(Blocks.COBBLESTONE), list);

 

and here is the function:

 

    public void addMultiSmeltingRecipe(ItemStack input, List<OutputWrapper> stack)
    {
        this.smeltmulti.put(input, stack);
    }

 

Do I have to register this crafting handler? I have it set in my Block class.

 

P.S:

 

Thanks so much for your time, you're really helping me out, which is sometimes hard to get in the modding community. Thanks.

Not new to java >> New to modding.

Link to comment
Share on other sites

1) Does your ItemWrapper properly compare item stacks in equals and generate a hashcode?

2) Does your furnace try to pull a result from your hashmap?

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

Okay,

 

So I'm a bit stuck on equals function, I dont know what to compare it to eachother, meta data doesnt seem to work.

 

 

public class OutputWrapper {
    public final float chance;
    public final ItemStack item;

    public OutputWrapper(float c, ItemStack i) {
        chance = c;
        item = i;
    }
    
@Override
public int hashCode() {
	return item.hashCode();
}

@Override
public boolean equals(Object o) {
	if(o instanceof OutputWrapper) {
		OutputWrapper other = (OutputWrapper)o;
		return other.item == this.item && //Compare
	}
	return false;
}

}

 

 

How would I make my furnace pull my hashmap?

Not new to java >> New to modding.

Link to comment
Share on other sites

So you know how FurnaceRecpies has a method called getSmeltingResult?

You need to do that for your own recipes.

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

Oh I see, didnt realize the 'result part' mb

And my wrapper looks like this now.. I have no clue if this is right:

 

public class OutputWrapper {
    public final float chance;
    public final ItemStack item;

    public OutputWrapper(float c, ItemStack i) {
        chance = c;
        item = i;
    }
    
@Override
public int hashCode() {
	return item.hashCode();
}

@Override
public boolean equals(Object o) {
	if(o instanceof OutputWrapper) {
		OutputWrapper other = (OutputWrapper)o;
		return other.item == this.item && (other.item == this.item);
	}
	return false;
}

}

Not new to java >> New to modding.

Link to comment
Share on other sites

Okay, so now the 'getMultResult', how would I set this up?

 

I get the basic idea, but dont know exactly what to return:

 

@Nullable
    public ItemStack getMultiResult(ItemStack stack)
    {
        for (Entry<ItemStack, List<OutputWrapper>> entry : this.smeltmulti.entrySet())
        {
            if (this.compareItemStacks(stack, (ItemStack)entry.getKey()))
            {
                return 
            }
        }

        return null;
    }

Not new to java >> New to modding.

Link to comment
Share on other sites

entry.getValue()

will return an

OutputWrapper

, which you can get the

ItemStack

from.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Link to comment
Share on other sites

The whole point of creating the wrapper was so you could do this:

 

smeltmulti.get(stack);

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 seems like you don't know Java. If you do, you should be able to solve this yourself. If not, learn Java first.

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Link to comment
Share on other sites

It seems like you don't know Java. If you do, you should be able to solve this yourself. If not, learn Java first.

 

No, I do, the problem is trying to understand how Minecraft is dealing with how itemstacks are stored. Yes, I have not worked much with Hashmaps/Array storing. However, this is my opportunity to learn HOW these work. You can lock this post if you feel like 'I don't know enough experience'

 

Anyways,

 

So I have just found that there are 2 functions that control the recipes:

 

    public Map<ItemStack, List<OutputWrapper>> getMultiList()
    {
        return this.multismeltList;
    }

 

So making the 2nd function changed my entry parameters to both itemstack. However, trying to return multi stacks is still confusing me.

    @Nullable
    public ItemStack getMultiResult(ItemStack stack)
    {
        for (Entry<ItemStack, ItemStack> entry : this.smeltingList.entrySet())
        {
            if (this.compareItemStacks(stack, entry.getKey()))
            {
                multismeltList.get(stack);
                return stack;
            }
        }

        return null;
    }

 

Was able to figure out meta data, however this did not change the fact the item won't smelt, my furnace has also been set to the correct recipceResult.

 

public class OutputWrapper {
    public final float chance;
    public final ItemStack item;

    public OutputWrapper(float c, ItemStack i) {
        chance = c;
        item = i;
    }
    
@Override
public int hashCode() {
	return item.hashCode();
}

@Override
public boolean equals(Object o) {
	if(o instanceof OutputWrapper) {
		OutputWrapper other = (OutputWrapper)o;
		return other.item == this.item && (other.item.getMetadata() == -1 || this.item.getMetadata() == -1 || other.item.getMetadata() == this.item.getMetadata());
	}
	return false;
}

}

 

 

 

Not new to java >> New to modding.

Link to comment
Share on other sites

@Override
public int hashCode() {
	return item.hashCode();
}

 

This won't work.  ItemStack does not provide a hashcode implementation, therefor this wrapper class violates the hashcode/equals contract:

That if equals returns true on two instances, hashcode must also return the same value.

 

You will need to provide your own hash implementation.  I suggest something along these lines:

 

	@Override
public int hashCode() {
	return 37*item.getItem.hashCode() + item.getItemMetadata();
}

 

Or use a HashUtility function:

https://github.com/Draco18s/ReasonableRealism/blob/master/src/main/java/com/draco18s/hardlib/math/HashUtils.java

 

Also, again:

 

 

The whole point of creating the wrapper was so you could do this:

 

smeltmulti.get(stack);

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

Okay,

 

So I got the hashCode function down, however, why multiply it by 37?

This is what the function looks like now:

@Override
public int hashCode() {
	return 37*item.getItem().hashCode() + item.getMetadata();
}

 

Finally, the multiGetResult function is still bugging me, I dont know if its right or wrong, but the item still wont smelt..

So should I return 

smeltmulti.get(stack);

or?

Not new to java >> New to modding.

Link to comment
Share on other sites

Okay,

 

So I got the hashCode function down, however, why multiply it by 37?

This is what the function looks like now:

@Override
public int hashCode() {
	return 37*item.getItem().hashCode() + item.getMetadata();
}

 

Arbitrary prime number.  Its the number I have in my HashUtils class. It's probably not the best number, but there is no "perfect" hash formula, I just needed one that was "good enough."

 

Finally, the multiGetResult function is still bugging me, I dont know if its right or wrong, but the item still wont smelt..

So should I return 

smeltmulti.get(stack);

or?

 

The return object from .get(...) will be that Output wrapper you wrote. You should absolutely return that to your furnace.  Because it needs to decide what to do with the two values it contains.

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

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.



×
×
  • Create New...

Important Information

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