Jump to content

Recommended Posts

Posted

I can use ChestGenHooks to add items to the list, however I created hundreds of collectible items that if I add a percent chance to spawn will still fill up the chests.  Is there a simple way to pick a random item (from hundreds) and just put one of those in a dungeon chest?

Posted

Made some progress, but now I'm stuck with a different problem.

 

I can create my own WeightedRandomChestContent class and override generateChestContent which allows me to return item stacks I want added to the chest.

 

So far so great, except there is a limit to the total number of items generated in a chest.  I want to ADD more items, not just replace what is already there.  I'd also like to add between 1 and 10 of my custom items, however some chests are only created with a couple items (that I override) so I can't have 10 items in the chest.

Posted

I'm not sure that would help.  I would like a guaranteed chance of having 1 to 10 of my custom items in a dungeon chest.  Also I would like it in addition to the existing loot instead of replacing it (so I can have up to 10 items).

 

If I lower the percent chance and add all the items there is still a chance that nothing at all will spawn.  It also seems that I can't set the amount to spawn this way or add to the chest instead of replacing.

 

Also there is the the fact that I'm adding hundreds of items, so adding via ChestGenHooks for each item seems inefficient.

Posted

Found a solution.  I'm not sure if it's the best one but it works.

 

First in my main preInit I add in new items with a high enough chance so that's all that spawns:

 

ChestGenHooks ChestGenHooksDungeon;
        WeightedRandomChestContent[] dungeonChestContentArray;

        ChestGenHooksDungeon=ChestGenHooks.getInfo(ChestGenHooks.DUNGEON_CHEST);
        dungeonChestContentArray=ChestGenHooksDungeon.getItems(new Random());
        ChestGenHooksDungeon.addItem(
                new recordRandomChestDungeon(new ItemStack(Items.netherbrick), 1, 10, 99999,dungeonChestContentArray,ChestGenHooks.DUNGEON_CHEST,totalRecordsCreated)
        );
        ChestGenHooksDungeon=ChestGenHooks.getInfo(ChestGenHooks.PYRAMID_DESERT_CHEST);
        dungeonChestContentArray=ChestGenHooksDungeon.getItems(new Random());
        ChestGenHooksDungeon.addItem(
                new recordRandomChestDungeon(new ItemStack(Items.netherbrick), 1, 10, 99999,dungeonChestContentArray,ChestGenHooks.PYRAMID_DESERT_CHEST,totalRecordsCreated)
        );

        ChestGenHooksDungeon=ChestGenHooks.getInfo(ChestGenHooks.PYRAMID_JUNGLE_CHEST);
        dungeonChestContentArray=ChestGenHooksDungeon.getItems(new Random());
        ChestGenHooksDungeon.addItem(
                new recordRandomChestDungeon(new ItemStack(Items.netherbrick), 1, 10, 99999,dungeonChestContentArray,ChestGenHooks.PYRAMID_JUNGLE_CHEST,totalRecordsCreated)
        );
        ChestGenHooksDungeon=ChestGenHooks.getInfo(ChestGenHooks.STRONGHOLD_CORRIDOR);
        dungeonChestContentArray=ChestGenHooksDungeon.getItems(new Random());
        ChestGenHooksDungeon.addItem(
                new recordRandomChestDungeon(new ItemStack(Items.netherbrick), 1, 10, 99999,dungeonChestContentArray,ChestGenHooks.STRONGHOLD_CORRIDOR,totalRecordsCreated)
        );
        ChestGenHooksDungeon=ChestGenHooks.getInfo(ChestGenHooks.STRONGHOLD_LIBRARY);
        dungeonChestContentArray=ChestGenHooksDungeon.getItems(new Random());
        ChestGenHooksDungeon.addItem(
                new recordRandomChestDungeon(new ItemStack(Items.netherbrick), 1, 10, 99999,dungeonChestContentArray,ChestGenHooks.STRONGHOLD_LIBRARY,totalRecordsCreated)
        );
        ChestGenHooksDungeon=ChestGenHooks.getInfo(ChestGenHooks.STRONGHOLD_CROSSING);
        dungeonChestContentArray=ChestGenHooksDungeon.getItems(new Random());
        ChestGenHooksDungeon.addItem(
                new recordRandomChestDungeon(new ItemStack(Items.netherbrick), 1, 10, 99999,dungeonChestContentArray,ChestGenHooks.STRONGHOLD_CROSSING,totalRecordsCreated)
        );


        Item newPressPlate = new pressPlate().setCreativeTab(recordsTab);
        GameRegistry.registerItem(newPressPlate, "pressPlate");

        GameRegistry.addRecipe(new ItemStack(newPressPlate, 1),
                " A ",
                "BBB",
                "CCC",

                'A', Items.stick, 'B', Blocks.iron_block, 'C', Blocks.gold_block
        );

 

Then in my class I override the forge generateChestContent to fill the chest with what I want the first time it's called, then hack in the default item generation for the rest of the items:

package com.cblend.moreMusic;

import cpw.mods.fml.common.registry.FMLControlledNamespacedRegistry;
import cpw.mods.fml.common.registry.GameData;
import net.minecraft.init.Items;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.WeightedRandom;
import net.minecraft.util.WeightedRandomChestContent;
import net.minecraftforge.common.ChestGenHooks;

import java.util.Arrays;
import java.util.Random;

public class recordRandomChestDungeon extends WeightedRandomChestContent {
    public static final String MINESHAFT_CORRIDOR = "mineshaftCorridor";
    public static final String PYRAMID_JUNGLE_CHEST = "pyramidJungleChest";
    public static final String STRONGHOLD_CORRIDOR = "strongholdCorridor";
    public static final String STRONGHOLD_LIBRARY = "strongholdLibrary";
    public static final String STRONGHOLD_CROSSING = "strongholdCrossing";
    public static final String DUNGEON_CHEST = "dungeonChest";
    public static final String PYRAMID_DESERT_CHEST = "pyramidDesertyChest";

    static IInventory lastInventory;
    static int totalRecords;
    final WeightedRandomChestContent[] origRandomContent;
    final String category;

    public recordRandomChestDungeon(ItemStack itemStackSent, int minChance, int maxChance, int chanceWeight, WeightedRandomChestContent[] sentRandomContent, String categorySent, int totalRecordsSent) {
        super(itemStackSent, minChance, maxChance, chanceWeight);
        origRandomContent = Arrays.copyOf(sentRandomContent, sentRandomContent.length);
        category = categorySent;
        totalRecords = totalRecordsSent;
    }

    @Override
    protected ItemStack[] generateChestContent(Random random, IInventory newInventory) {
        if (!newInventory.equals(lastInventory)) {
            lastInventory = newInventory;
            int randomMin = 1;
            int randomMax = 5;
            if (category == STRONGHOLD_CROSSING || category == STRONGHOLD_LIBRARY || category == STRONGHOLD_CORRIDOR) {
                randomMin = 5;
                randomMax = 17;
            }
            if (category == PYRAMID_DESERT_CHEST || category == PYRAMID_JUNGLE_CHEST) {
                randomMin = 3;
                randomMax = 6;
            }
            int randomCount = random.nextInt((randomMax - randomMin) + 1) + randomMin;
            int randomRecordNum;

            for (int counter = 1; counter <= randomCount; counter++) {
                randomRecordNum = random.nextInt((totalRecords - 1) + 1) + 1;
                FMLControlledNamespacedRegistry<Item> myDataInstance = GameData.getItemRegistry();
                Item objectReturned = myDataInstance.getObject("moreMusic:record" + randomRecordNum);
                if (objectReturned != null) {
                    System.out.println("adding item");
                    newInventory.setInventorySlotContents(random.nextInt(newInventory.getSizeInventory()), new ItemStack(objectReturned, 1));
                }
            }
        }

        int minAmount = 1;
        int maxAmount = 1;
        WeightedRandomChestContent weightedrandomchestcontent = (WeightedRandomChestContent) WeightedRandom.getRandomItem(random, origRandomContent);

        Item currentItem = weightedrandomchestcontent.theItemId.getItem();
        switch (category) {
            case DUNGEON_CHEST: {
                if (currentItem.equals(Items.iron_ingot) ||
                        currentItem.equals(Items.wheat) ||
                        currentItem.equals(Items.gunpowder) ||
                        currentItem.equals(Items.string) ||
                        currentItem.equals(Items.redstone)) {
                    maxAmount = 4;
                }
                break;
            }
            case STRONGHOLD_CORRIDOR: {
                if (currentItem.equals(Items.diamond) ||
                        currentItem.equals(Items.gold_ingot) ||
                        currentItem.equals(Items.bread) ||
                        currentItem.equals(Items.apple)) {
                    maxAmount = 3;
                } else if (currentItem.equals(Items.iron_ingot)) {
                    maxAmount = 5;
                } else if (currentItem.equals(Items.redstone)) {
                    minAmount = 4;
                    maxAmount = 9;
                }
                break;
            }
            case STRONGHOLD_CROSSING: {
                if (currentItem.equals(Items.gold_ingot) ||
                        currentItem.equals(Items.bread) ||
                        currentItem.equals(Items.apple)) {
                    maxAmount = 3;
                } else if (currentItem.equals(Items.iron_ingot)) {
                    maxAmount = 5;
                } else if (currentItem.equals(Items.redstone)) {
                    minAmount = 4;
                    maxAmount = 9;
                } else if (currentItem.equals(Items.coal)) {
                    minAmount = 3;
                    maxAmount = 8;
                }
                break;
            }
            case STRONGHOLD_LIBRARY: {
                if (currentItem.equals(Items.book)) {
                    maxAmount = 3;
                } else if (currentItem.equals(Items.painting)) {
                    minAmount = 2;
                    maxAmount = 7;
                } else if (currentItem.equals(Items.enchanted_book)) {
                    minAmount = 1;
                    maxAmount = 5;
                }
                break;
            }
            case PYRAMID_JUNGLE_CHEST:
            case PYRAMID_DESERT_CHEST: {
                if (currentItem.equals(Items.diamond) || currentItem.equals(Items.emerald)) {
                    maxAmount = 3;
                } else if (currentItem.equals(Items.bone)) {
                    minAmount = 4;
                    maxAmount = 6;
                } else if (currentItem.equals(Items.rotten_flesh)) {
                    minAmount = 3;
                    maxAmount = 7;
                } else if (currentItem.equals(Items.iron_ingot)) {
                    maxAmount = 5;
                } else if (currentItem.equals(Items.gold_ingot)) {
                    minAmount = 2;
                    maxAmount = 7;
                } else if (currentItem.equals(Items.iron_ingot) ||
                        currentItem.equals(Items.wheat) ||
                        currentItem.equals(Items.gunpowder) ||
                        currentItem.equals(Items.string) ||
                        currentItem.equals(Items.redstone)) {
                    maxAmount = 4;
                }
                break;
            }
        }
        return ChestGenHooks.generateStacks(random, weightedrandomchestcontent.theItemId, minAmount, maxAmount);
    }

}

 

The items I add will be replaced if the default happens to generate in the same slot, but it's the best I've come up with so far...

Posted

Yea, but I figured since it's so many items (currently over 2000), instead of creating a new array of objects it would be better to access the one that already exists.

 

I did change it to:

Item objectReturned = GameData.getItemRegistry().getObject("moreMusic:record" + randomRecordNum);

With help from jabelar.

 

 

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



×
×
  • Create New...

Important Information

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