Jump to content

[1.9] Logic/Math Question


LogicTechCorp

Recommended Posts

I have an Infusion Altar TE that can infuse an item with souls. To do so, the Infusion Altar requires the surrounding Crucible TEs to have a certain SoulType amount. The Crucible can hold 64 souls and there are 5 SoulTypes, so the crucible can have a mixture of all SoulTypes with different amounts of each type. Currently this only works if the required SoulType amount is in one Crucible opposed to two or more. What would be the best way to change my current setup to allow this?

 

Note:

A recipe with a null SoulType will require any SoulType.

 

Recipe Class:

public class RecipeInfusion implements ISMRecipe
{
    private Object input;
    private ItemStack output;
    private SoulType soulType;
    private int requiredSoulAmount;
    private String guiButtonText;

    private static final Map<String, List<ItemStack>> oreMap = Maps.newHashMap();

    public RecipeInfusion(Object input, ItemStack output, SoulType soulType, int requiredSoulAmount, String guiButtonText)
    {
        this.input = input;
        this.output = output;
        this.soulType = soulType;
        this.requiredSoulAmount = requiredSoulAmount;
        this.guiButtonText = guiButtonText;
    }

    public boolean matches(ItemStack stack, SoulType soulType, int soulAmount)
    {
        if(stack == null || soulType == null)
        {
            return false;
        }
        else if(this.requiredSoulAmount > soulAmount || stack.stackSize * this.requiredSoulAmount > soulAmount)
        {
            return false;
        }
        else
        {
            if(this.soulType != null)
            {
                if(this.soulType == soulType)
                {
                    return this.checkMatch(stack);
                }
            }
            else
            {
                return this.checkMatch(stack);
            }
        }
        return false;
    }

    private boolean checkMatch(ItemStack stack)
    {
        if(this.input instanceof Block)
        {
            return Block.getBlockFromItem(stack.getItem()) == this.input;
        }
        else if(this.input instanceof Item)
        {
            return stack.getItem() == this.input;
        }
        else
        {
            String oreDict = (String) this.input;
            return isOreDict(stack, oreDict);
        }
    }

    private boolean isOreDict(ItemStack stack, String entry)
    {
        if(stack == null || stack.getItem() == null)
        {
            return false;
        }

        List<ItemStack> ores;

        if(oreMap.containsKey(entry))
        {
            ores = oreMap.get(entry);
        }
        else
        {
            ores = OreDictionary.getOres(entry);
            oreMap.put(entry, ores);
        }
        for(ItemStack oreStack : ores)
        {
            ItemStack newStack = oreStack.copy();
            newStack.setItemDamage(stack.getItemDamage());

            if(stack.isItemEqual(newStack))
            {
                return true;
            }
        }
        return false;
    }

    public Object getInput()
    {
        return this.input;
    }

    public ItemStack getOutput()
    {
        return this.output;
    }

    public SoulType getSoulType()
    {
        return this.soulType;
    }

    public int getRequiredSoulAmount()
    {
        return this.requiredSoulAmount;
    }

    public String getGuiButtonText()
    {
        return this.guiButtonText;
    }

 

TileEntityInfusionAltar:

public class TileEntityInfusionAltar extends TileEntityInventory
{
    public TileEntityInfusionAltar()
    {
        super(1, "soul_altar");
    }

    private List<TileEntitySoulCrucible> getCrucibles()
    {
        List<TileEntitySoulCrucible> crucibles = Lists.newArrayList();

        for(int x = pos.getX() - 3; x < pos.getX() + 4; x++)
        {
            for(int y = pos.getY() - 3; y < pos.getY() + 4; y++)
            {
                for(int z = pos.getZ() - 3; z < pos.getZ() + 4; z++)
                {
                    TileEntity tileEntity = worldObj.getTileEntity(new BlockPos(x, y, z));

                    if(tileEntity != null && tileEntity instanceof TileEntitySoulCrucible)
                    {
                        crucibles.add((TileEntitySoulCrucible) tileEntity);
                    }
                }
            }
        }
        return crucibles;
    }

    public RecipeInfusion getRecipe()
    {
        RecipeInfusion recipe = null;

        for(TileEntitySoulCrucible crucible : this.getCrucibles())
        {
            for(SoulType soulType : crucible.getContainer().SOULS.keySet())
            {
                for(ISMRecipe r : SoulMagicAPI.getRecipeRegistry().typeToRecipe.values())
                {
                    if(r instanceof RecipeInfusion)
                    {
                        RecipeInfusion newRecipe = (RecipeInfusion) r;

                        if(newRecipe.matches(this.getStackInSlot(0), soulType, crucible.getContainer().SOULS.get(soulType)))
                        {
                            recipe = newRecipe;
                        }
                    }
                }
            }
        }
        return recipe;
    }

    public void infuse()
    {
        for(TileEntitySoulCrucible crucible : this.getCrucibles())
        {
            for(SoulType soulType : crucible.getContainer().SOULS.keySet())
            {
                SoulMagicAPI.getRecipeRegistry().typeToRecipe.values().stream().filter(r -> r instanceof RecipeInfusion).forEach(r ->
                {
                    RecipeInfusion newRecipe = (RecipeInfusion) r;

                    if(newRecipe.matches(this.getStackInSlot(0), soulType, crucible.getContainer().SOULS.get(soulType)))
                    {
                        ItemStack outStack = newRecipe.getOutput();
                        outStack.stackSize = this.getStackInSlot(0).stackSize;

                        crucible.getContainer().remove(soulType, outStack.stackSize * newRecipe.getRequiredSoulAmount());
                        this.setInventorySlotContents(0, outStack);
                    }
                });
            }
        }
    }

 

Crucible Container:

public class SoulContainer
{
    private int capacity;

    public final Map<SoulType, Integer> SOULS = Maps.newHashMap();

    public SoulContainer(int capacity)
    {
        this.capacity = capacity;
    }

    public void add(SoulType soulType, int amount)
    {
        if(this.getSoulAmount() + amount < this.capacity)
        {
            if(!SOULS.keySet().contains(soulType))
            {
                SOULS.put(soulType, amount);
            }
            else
            {
                SOULS.replace(soulType, SOULS.get(soulType) + amount);
            }
        }
    }

    public void remove(SoulType soulType, int amount)
    {
        if(this.getSoulAmount() - amount >= 0)
        {
            if(SOULS.keySet().contains(soulType))
            {
                if(SOULS.get(soulType) - amount > 0)
                {
                    SOULS.replace(soulType, SOULS.get(soulType) - amount);
                }
                else
                {
                    SOULS.remove(soulType);
                }
            }
        }
    }

    public void readFromNBT(NBTTagCompound tag)
    {
        NBTTagList tagList = tag.getTagList("Souls", 10);

        for(int i = 0; i < tagList.tagCount(); i++)
        {
            NBTTagCompound soulTag = tagList.getCompoundTagAt(i);
            String soulTypeName = soulTag.getString(NBT.SOUL_TYPE);
            SoulType soulType = SoulMagicAPI.getSoulRegistry().getSoulTypeFrom(soulTypeName);
            int amount = soulTag.getInteger("SoulAmount");
            SOULS.put(soulType, amount);
        }
        this.capacity = tag.getInteger("Capacity");
    }

    public void writeToNBT(NBTTagCompound tag)
    {
        NBTTagList tagList = new NBTTagList();

        for(Map.Entry<SoulType, Integer> soul : SOULS.entrySet())
        {
            NBTTagCompound soulTag = new NBTTagCompound();
            soulTag.setString(NBT.SOUL_TYPE, soul.getKey().getName());
            soulTag.setInteger(NBT.SOUL_AMOUNT, soul.getValue());
            tagList.appendTag(soulTag);
        }
        tag.setTag("Souls", tagList);
        tag.setInteger("Capacity", this.capacity);
    }

    public boolean containsSoul(SoulType soulType)
    {
        return SOULS.containsKey(soulType);
    }

    public SoulType getRandomSoul()
    {
        Random rand = new Random();
        Object[] souls = SOULS.keySet().toArray();
        return (SoulType) souls[rand.nextInt(souls.length)];
    }

    public int getCapacity()
    {
        return this.capacity;
    }

    public int getSoulAmount()
    {
        int amount = 0;

        for(SoulType soulType : SOULS.keySet())
        {
            amount += SOULS.get(soulType);
        }
        return amount;
    }

    public int getSoulTypeAmount(SoulType soulType)
    {
        int amount = 0;

        for(SoulType sT : SOULS.keySet())
        {
            if(sT == soulType)
            {
                amount += SOULS.get(soulType);
            }
        }
        return amount;
    }

    public void setCapacity(int capacity)
    {
        this.capacity = capacity;
    }

Link to comment
Share on other sites

Step 1:

Loop through all crucibles in range, build a HashMap of the total amounts of every SoulType available in range.

Step 2:

Check the recipe against this hashmap.  If enough, recipe allowed, otherwise it isn't.

Step 3:

Create a hashmap for the SoulTypes being consumed by the recipe and loop through each crucible in range and deduct as many Soul amounts as possible (keeping track in this hashmap copy) until all souls consumed have been deducted from somewhere.

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.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Baba  Serege [[+27-73 590 8989]] has experience of 27 years in helping and guiding many people from all over the world. His psychic abilities may help you answer and resolve many unanswered questions. He specialize in helping women and men from all walks of life.. 1) – Bring back lost lover. even if lost for a long time. 2) – My lover is abusing alcohol, partying and cheating on me I urgently need help” 3) – Divorce or court issues. 4) – Is your love falling apart? 5) – Do you want your love to grow stronger? 6) – Is your partner losing interest in you? 7) – Do you want to catch your partner cheating on you? – We help to keep your partner faithful and loyal to you. 9) – We recover love and happiness when relationship breaks down. 10) – Making your partner loves you alone. 11) – We create loyalty and everlasting love between couples. 12) – Get a divorce settlement quickly from your ex-partner. 13) – We create everlasting love between couples. 14) – We help you look for the best suitable partner. 15) – We bring back lost lover even if lost for a long time. 16) – We strengthen bonds in all love relationship and marriages 17) – Are you an herbalist who wants to get more powers? 18) – Buy a house or car of your dream. 19) – Unfinished jobs by other doctors come to me. 20) – I help those seeking employment. 21) – Pensioners free treatment. 22) – Win business tenders and contracts. 23) – Do you need to recover your lost property? 24) – Promotion at work and better pay. 25) – Do you want to be protected from bad spirits and nightmares? 26) – Financial problems. 27) – Why you can’t keep money or lovers? 28) – Why you have a lot of enemies? 29) – Why you are fired regularly on jobs? 30) – Speed up money claim spell, delayed payments, pension and accident funds 31) – I help students pass their exams/interviews. 33) – Removal of bad luck and debts. 34) – Are struggling to sleep because of a spiritual wife or husband. 35- ) Recover stolen property
    • OLXTOTO adalah situs bandar togel online resmi terbesar dan terpercaya di Indonesia. Bergabunglah dengan OLXTOTO dan nikmati pengalaman bermain togel yang aman dan terjamin. Koleksi toto 4D dan togel toto terlengkap di OLXTOTO membuat para member memiliki pilihan taruhan yang lebih banyak. Sebagai situs togel terpercaya, OLXTOTO menjaga keamanan dan kenyamanan para membernya dengan sistem keamanan terbaik dan enkripsi data. Transaksi yang cepat, aman, dan terpercaya merupakan jaminan dari OLXTOTO. Nikmati layanan situs toto terbaik dari OLXTOTO dengan tampilan yang user-friendly dan mudah digunakan. Layanan pelanggan tersedia 24/7 untuk membantu para member. Bergabunglah dengan OLXTOTO sekarang untuk merasakan pengalaman bermain togel yang menyenangkan dan menguntungkan.
    • Baba  Serege [[+27-73 590 8989]] has experience of 27 years in helping and guiding many people from all over the world. His psychic abilities may help you answer and resolve many unanswered questions. He specialize in helping women and men from all walks of life.. 1) – Bring back lost lover. even if lost for a long time. 2) – My lover is abusing alcohol, partying and cheating on me I urgently need help” 3) – Divorce or court issues. 4) – Is your love falling apart? 5) – Do you want your love to grow stronger? 6) – Is your partner losing interest in you? 7) – Do you want to catch your partner cheating on you? – We help to keep your partner faithful and loyal to you. 9) – We recover love and happiness when relationship breaks down. 10) – Making your partner loves you alone. 11) – We create loyalty and everlasting love between couples. 12) – Get a divorce settlement quickly from your ex-partner. 13) – We create everlasting love between couples. 14) – We help you look for the best suitable partner. 15) – We bring back lost lover even if lost for a long time. 16) – We strengthen bonds in all love relationship and marriages 17) – Are you an herbalist who wants to get more powers? 18) – Buy a house or car of your dream. 19) – Unfinished jobs by other doctors come to me. 20) – I help those seeking employment. 21) – Pensioners free treatment. 22) – Win business tenders and contracts. 23) – Do you need to recover your lost property? 24) – Promotion at work and better pay. 25) – Do you want to be protected from bad spirits and nightmares? 26) – Financial problems. 27) – Why you can’t keep money or lovers? 28) – Why you have a lot of enemies? 29) – Why you are fired regularly on jobs? 30) – Speed up money claim spell, delayed payments, pension and accident funds 31) – I help students pass their exams/interviews. 33) – Removal of bad luck and debts. 34) – Are struggling to sleep because of a spiritual wife or husband. 35- ) Recover stolen property
  • Topics

×
×
  • Create New...

Important Information

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