Finished DDLoot.

This commit is contained in:
zangamj 2016-08-05 11:53:15 -04:00
parent 23e57a9cca
commit 0174899d2e
2 changed files with 70 additions and 133 deletions

View file

@ -1,6 +1,8 @@
package com.zixiken.dimdoors; package com.zixiken.dimdoors;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random; import java.util.Random;
import net.minecraft.enchantment.Enchantment; import net.minecraft.enchantment.Enchantment;
@ -20,8 +22,8 @@ import com.zixiken.dimdoors.util.WeightedContainer;
* Registers a category of loot chests for Dimensional Doors in Forge. * Registers a category of loot chests for Dimensional Doors in Forge.
*/ */
public class DDLoot { public class DDLoot {
private static final String[] SPECIAL_SKULL_OWNERS = new String[] {
private static final String[] SPECIAL_SKULL_OWNERS = new String[] { "stevenrs11", "kamikazekiwi3", "fbt", "Jaitsu", "XCompWiz", "skyboy026", "Wylker" }; "stevenrs11", "kamikazekiwi3", "fbt", "Jaitsu", "XCompWiz", "skyboy026", "Wylker" };
private static final double MIN_ITEM_DAMAGE = 0.3; private static final double MIN_ITEM_DAMAGE = 0.3;
private static final double MAX_ITEM_DAMAGE = 0.9; private static final double MAX_ITEM_DAMAGE = 0.9;
@ -29,21 +31,21 @@ public class DDLoot {
private static final int MAX_ITEM_ENCHANTMENT_CHANCE = 100; private static final int MAX_ITEM_ENCHANTMENT_CHANCE = 100;
private static final int SPECIAL_SKULL_CHANCE = 20; private static final int SPECIAL_SKULL_CHANCE = 20;
private static final int MAX_SPECIAL_SKULL_CHANCE = 100; private static final int MAX_SPECIAL_SKULL_CHANCE = 100;
private static final int CHEST_SIZE = 6;
public static final String DIMENSIONAL_DUNGEON_CHEST = "dimensionalDungeonChest"; public static final String DIMENSIONAL_DUNGEON_CHEST = "dimensionalDungeonChest";
public static ChestGenHooks DungeonChestInfo = null;
private static final int CHEST_SIZE = 6; public static ChestGenHooks dungeonChestInfo = null;
private DDLoot() {} private DDLoot() {}
public static void registerInfo(DDProperties properties) public static void registerInfo(DDProperties properties) {
{
// Register the dimensional dungeon chest with ChestGenHooks. This isn't necessary, but allows // Register the dimensional dungeon chest with ChestGenHooks. This isn't necessary, but allows
// other mods to add their own loot to our chests if they know our loot category, without having // other mods to add their own loot to our chests if they know our loot category, without having
// to interface with our code. // to interface with our code.
DungeonChestInfo = ChestGenHooks.getInfo(DIMENSIONAL_DUNGEON_CHEST); dungeonChestInfo = ChestGenHooks.getInfo(DIMENSIONAL_DUNGEON_CHEST);
DungeonChestInfo.setMin(CHEST_SIZE); dungeonChestInfo.setMin(CHEST_SIZE);
DungeonChestInfo.setMax(CHEST_SIZE); dungeonChestInfo.setMax(CHEST_SIZE);
ArrayList<WeightedRandomChestContent> items = new ArrayList<WeightedRandomChestContent>(); ArrayList<WeightedRandomChestContent> items = new ArrayList<WeightedRandomChestContent>();
@ -60,33 +62,22 @@ public class DDLoot {
addContent(properties.WorldThreadLootEnabled, items, DimDoors.itemWorldThread, 80, 2, 12); addContent(properties.WorldThreadLootEnabled, items, DimDoors.itemWorldThread, 80, 2, 12);
// Add all the items to our dungeon chest // Add all the items to our dungeon chest
addItemsToContainer(DungeonChestInfo, items); addItemsToContainer(dungeonChestInfo, items);
} }
private static void addContent(boolean include, ArrayList<WeightedRandomChestContent> items, private static void addContent(boolean include, ArrayList<WeightedRandomChestContent> items,
Item item, int weight) Item item, int weight) {addContent(include, items, item, weight, 1, 1);}
{
if (include)
items.add(new WeightedRandomChestContent(item, 0, 1, 1, weight));
}
private static void addContent(boolean include, ArrayList<WeightedRandomChestContent> items, private static void addContent(boolean include, ArrayList<WeightedRandomChestContent> items,
Item item, int weight, int minAmount, int maxAmount) Item item, int weight, int minAmount, int maxAmount) {
{ if (include) items.add(new WeightedRandomChestContent(item, 0, minAmount, maxAmount, weight));
if (include)
items.add(new WeightedRandomChestContent(item, 0, minAmount, maxAmount, weight));
} }
private static void addItemsToContainer(ChestGenHooks container, ArrayList<WeightedRandomChestContent> items) private static void addItemsToContainer(ChestGenHooks container, ArrayList<WeightedRandomChestContent> items) {
{ for (WeightedRandomChestContent item : items) container.addItem(item);
for (WeightedRandomChestContent item : items)
{
container.addItem(item);
}
} }
private static void fillChest(ArrayList<ItemStack> stacks, IInventory inventory, Random random) private static void fillChest(ArrayList<ItemStack> stacks, IInventory inventory, Random random) {
{
// This custom chest-filling function avoids overwriting item stacks // This custom chest-filling function avoids overwriting item stacks
// The prime number below is used for choosing chest slots in a seemingly-random pattern. Its value // The prime number below is used for choosing chest slots in a seemingly-random pattern. Its value
@ -96,53 +87,42 @@ public class DDLoot {
final int primeOffset = 239333; final int primeOffset = 239333;
int size = inventory.getSizeInventory(); int size = inventory.getSizeInventory();
for (ItemStack item : stacks) for (ItemStack item : stacks) {
{
int limit = size;
int index = random.nextInt(size); int index = random.nextInt(size);
while (limit > 0 && inventory.getStackInSlot(index) != null) for(int limit = size; limit > 0 && inventory.getStackInSlot(index) != null; limit--)
{
limit--;
index = (index + primeOffset) % size; index = (index + primeOffset) % size;
}
inventory.setInventorySlotContents(index, item); inventory.setInventorySlotContents(index, item);
} }
} }
public static void generateChestContents(ChestGenHooks chestInfo, IInventory inventory, Random random) public static void generateChestContents(ChestGenHooks chestInfo, IInventory inventory, Random random) {
{
// This is a custom version of net.minecraft.util.WeightedRandomChestContent.generateChestContents() // This is a custom version of net.minecraft.util.WeightedRandomChestContent.generateChestContents()
// It's designed to avoid the following bugs in MC 1.5: // It's designed to avoid the following bugs in MC 1.5:
// 1. If multiple enchanted books appear, then they will have the same enchantment // 1. If multiple enchanted books appear, then they will have the same enchantment
// 2. The randomized filling algorithm will sometimes overwrite item stacks with other stacks // 2. The randomized filling algorithm will sometimes overwrite item stacks with other stacks
int count = chestInfo.getCount(random); int count = chestInfo.getCount(random);
WeightedRandomChestContent[] content = chestInfo.getItems(random); List<WeightedRandomChestContent> content = chestInfo.getItems(random);
ArrayList<ItemStack> allStacks = new ArrayList<ItemStack>(); ArrayList<ItemStack> allStacks = new ArrayList<ItemStack>();
for (int k = 0; k < count; k++) for (int k = 0; k < count; k++) {
{ WeightedRandomChestContent selection = WeightedRandom.getRandomItem(random, content);
WeightedRandomChestContent selection = (WeightedRandomChestContent)WeightedRandom.getRandomItem(random, content);
// Call getChestGenBase() to make sure we generate a different enchantment for books. // Call getChestGenBase() to make sure we generate a different enchantment for books.
// Don't just use a condition to check if the item is an instance of ItemEnchantedBook because // Don't just use a condition to check if the item is an instance of ItemEnchantedBook because
// we don't know if other mods might add items that also need to be regenerated. // we don't know if other mods might add items that also need to be regenerated.
selection = selection.theItemId.getItem().getChestGenBase(chestInfo, random, selection); selection = selection.theItemId.getItem().getChestGenBase(chestInfo, random, selection);
ItemStack[] stacks = ChestGenHooks.generateStacks(random, selection.theItemId, selection.theMinimumChanceToGenerateItem, selection.theMaximumChanceToGenerateItem); allStacks.addAll(Arrays.asList(ChestGenHooks.generateStacks(random, selection.theItemId,
for (int h = 0; h < stacks.length; h++) selection.minStackSize, selection.maxStackSize)));
{
allStacks.add(stacks[h]);
}
} }
fillChest(allStacks, inventory, random); fillChest(allStacks, inventory, random);
} }
public static void fillGraveChest(IInventory inventory, Random random, DDProperties properties) public static void fillGraveChest(IInventory inventory, Random random, DDProperties properties) {
{
// This function fills "grave chests", which are chests for dungeons that // This function fills "grave chests", which are chests for dungeons that
// look like a player died in the area and his remains were gathered in // look like a player died in the area and his remains were gathered in
// a chest. Doing this properly requires fine control of loot generation, // a chest. Doing this properly requires fine control of loot generation,
@ -155,45 +135,31 @@ public class DDLoot {
// Insert bones and rotten flesh // Insert bones and rotten flesh
// Make stacks of single items to spread them out // Make stacks of single items to spread them out
count = MathHelper.getRandomIntegerInRange(random, 2, 5); count = MathHelper.getRandomIntegerInRange(random, 2, 5);
for (k = 0; k < count; k++) for (k = 0; k < count; k++) stacks.add(new ItemStack(Items.bone, 1));
{
stacks.add( new ItemStack(Items.bone, 1) );
}
count = MathHelper.getRandomIntegerInRange(random, 2, 4); count = MathHelper.getRandomIntegerInRange(random, 2, 4);
for (k = 0; k < count; k++) for (k = 0; k < count; k++) stacks.add(new ItemStack(Items.rotten_flesh, 1));
{
stacks.add( new ItemStack(Items.rotten_flesh, 1) );
}
// Insert tools // Insert tools
// 30% chance of adding a pickaxe // 30% chance of adding a pickaxe
if (random.nextInt(100) < 30) if (random.nextInt(100) < 30) addModifiedTool(Items.iron_pickaxe, stacks, random);
{
addModifiedTool(Items.iron_pickaxe, stacks, random);
}
// 30% chance of adding a bow and some arrows // 30% chance of adding a bow and some arrows
if (random.nextInt(100) < 30) if (random.nextInt(100) < 30) {
{
addModifiedBow(stacks, random); addModifiedBow(stacks, random);
stacks.add(new ItemStack(Items.arrow, MathHelper.getRandomIntegerInRange(random, 8, 32))); stacks.add(new ItemStack(Items.arrow, MathHelper.getRandomIntegerInRange(random, 8, 32)));
} }
// 10% chance of adding a Rift Blade (no enchants) // 10% chance of adding a Rift Blade (no enchants)
if (properties.RiftBladeLootEnabled && random.nextInt(100) < 10) if (properties.RiftBladeLootEnabled && random.nextInt(100) < 10)
{
stacks.add(new ItemStack(DimDoors.itemRiftBlade, 1)); stacks.add(new ItemStack(DimDoors.itemRiftBlade, 1));
}
else else
{
// 20% of adding an iron sword, 10% of adding a stone sword // 20% of adding an iron sword, 10% of adding a stone sword
addModifiedSword( getRandomItem(Items.iron_sword, Items.stone_sword, null, 20, 10, random) , stacks, random); addModifiedSword(getRandomItem(Items.iron_sword, Items.stone_sword, 20, 10, random), stacks, random);
}
// Insert equipment // Insert equipment
// For each piece, 25% of an iron piece, 10% of a chainmail piece // For each piece, 25% of an iron piece, 10% of a chainmail piece
addModifiedEquipment( getRandomItem(Items.iron_helmet, Items.chainmail_helmet, null, 25, 10, random) , stacks, random); addModifiedEquipment(getRandomItem(Items.iron_helmet, Items.chainmail_helmet, 25, 10, random), stacks, random);
addModifiedEquipment( getRandomItem(Items.iron_chestplate, Items.chainmail_chestplate, null, 25, 10, random) , stacks, random); addModifiedEquipment(getRandomItem(Items.iron_chestplate, Items.chainmail_chestplate, 25, 10, random), stacks, random);
addModifiedEquipment( getRandomItem(Items.iron_leggings, Items.chainmail_leggings, null, 25, 10, random) , stacks, random); addModifiedEquipment(getRandomItem(Items.iron_leggings, Items.chainmail_leggings, 25, 10, random), stacks, random);
addModifiedEquipment( getRandomItem(Items.iron_boots, Items.iron_boots, null, 25, 10, random) , stacks, random); addModifiedEquipment(getRandomItem(Items.iron_boots, Items.iron_boots, 25, 10, random), stacks, random);
// Insert other random stuff // Insert other random stuff
// 40% chance for a name tag, 35% chance for a glass bottle // 40% chance for a name tag, 35% chance for a glass bottle
@ -206,93 +172,64 @@ public class DDLoot {
addItemWithChance(stacks, random, 5, Items.record_11, 1); addItemWithChance(stacks, random, 5, Items.record_11, 1);
// Finally, there is a 5% chance of adding a player head // Finally, there is a 5% chance of adding a player head
if (random.nextInt(100) < 5) if (random.nextInt(100) < 5) addGraveSkull(stacks, random);
{
addGraveSkull(stacks, random);
}
fillChest(stacks, inventory, random); fillChest(stacks, inventory, random);
} }
private static void addModifiedEquipment(Item item, ArrayList<ItemStack> stacks, Random random) private static void addModifiedEquipment(Item item, ArrayList<ItemStack> stacks, Random random) {
{ if (item == null) return;
if (item == null) stacks.add(getModifiedItem(item, random, Enchantment.blastProtection, Enchantment.fireProtection,
return; Enchantment.protection, Enchantment.projectileProtection));
stacks.add( getModifiedItem(item, random, new Enchantment[] { Enchantment.blastProtection, Enchantment.fireProtection, Enchantment.protection, Enchantment.projectileProtection }) );
} }
private static void addModifiedSword(Item item, ArrayList<ItemStack> stacks, Random random) private static void addModifiedSword(Item item, ArrayList<ItemStack> stacks, Random random) {
{ if (item == null) return;
if (item == null) stacks.add(getModifiedItem(item, random, Enchantment.fireAspect, Enchantment.knockback, Enchantment.sharpness));
return;
stacks.add( getModifiedItem(item, random, new Enchantment[] { Enchantment.fireAspect, Enchantment.knockback, Enchantment.sharpness }) );
} }
private static void addModifiedTool(Item tool, ArrayList<ItemStack> stacks, Random random) private static void addModifiedTool(Item tool, ArrayList<ItemStack> stacks, Random random) {
{ if (tool == null) return;
if (tool == null) stacks.add(getModifiedItem(tool, random, Enchantment.efficiency, Enchantment.unbreaking));
return;
stacks.add( getModifiedItem(tool, random, new Enchantment[] { Enchantment.efficiency, Enchantment.unbreaking }) );
} }
private static void addModifiedBow(ArrayList<ItemStack> stacks, Random random) private static void addModifiedBow(ArrayList<ItemStack> stacks, Random random) {
{ stacks.add(getModifiedItem(Items.bow, random, Enchantment.flame, Enchantment.power, Enchantment.punch));
stacks.add( getModifiedItem(Items.bow, random, new Enchantment[] { Enchantment.flame, Enchantment.power, Enchantment.punch }) );
} }
private static ItemStack getModifiedItem(Item item, Random random, Enchantment[] enchantments) private static ItemStack getModifiedItem(Item item, Random random, Enchantment ... enchantments) {
{
ItemStack result = applyRandomDamage(item, random); ItemStack result = applyRandomDamage(item, random);
if (enchantments.length > 0 && random.nextInt(MAX_ITEM_ENCHANTMENT_CHANCE) < ITEM_ENCHANTMENT_CHANCE) if (enchantments.length > 0 && random.nextInt(MAX_ITEM_ENCHANTMENT_CHANCE) < ITEM_ENCHANTMENT_CHANCE)
{
result.addEnchantment(enchantments[random.nextInt(enchantments.length)], 1); result.addEnchantment(enchantments[random.nextInt(enchantments.length)], 1);
}
return result; return result;
} }
private static Item getRandomItem(Item a, Item b, Item c, int weightA, int weightB, Random random) private static Item getRandomItem(Item a, Item b, int weightA, int weightB, Random random) {
{
int roll = random.nextInt(100); int roll = random.nextInt(100);
if (roll < weightA) if (roll < weightA) return a;
return a; if (roll < weightA + weightB) return b;
if (roll < weightA + weightB) return null;
return b;
return c;
} }
private static void addItemWithChance(ArrayList<ItemStack> stacks, Random random, int chance, Item item, int count) private static void addItemWithChance(ArrayList<ItemStack> stacks, Random random, int chance, Item item, int count) {
{ if (random.nextInt(100) < chance) stacks.add(new ItemStack(item, count));
if (random.nextInt(100) < chance)
{
stacks.add(new ItemStack(item, count));
}
} }
private static ItemStack applyRandomDamage(Item item, Random random) private static ItemStack applyRandomDamage(Item item, Random random) {
{ return new ItemStack(item, 1, (int)(item.getMaxDamage() *
int damage = (int) (item.getMaxDamage() * MathHelper.getRandomDoubleInRange(random, MIN_ITEM_DAMAGE, MAX_ITEM_DAMAGE)); MathHelper.getRandomDoubleInRange(random, MIN_ITEM_DAMAGE, MAX_ITEM_DAMAGE)));
return new ItemStack(item, 1, damage);
} }
private static void addGraveSkull(ArrayList<ItemStack> stacks, Random random) private static void addGraveSkull(ArrayList<ItemStack> stacks, Random random) {
{
final int PLAYER_SKULL_METADATA = 3; final int PLAYER_SKULL_METADATA = 3;
DeathTracker deathTracker = DimDoors.deathTracker; DeathTracker deathTracker = DimDoors.deathTracker;
String skullOwner; String skullOwner;
if (deathTracker.isEmpty() || (random.nextInt(MAX_SPECIAL_SKULL_CHANCE) < SPECIAL_SKULL_CHANCE)) if (deathTracker.isEmpty() || (random.nextInt(MAX_SPECIAL_SKULL_CHANCE) < SPECIAL_SKULL_CHANCE))
{
skullOwner = SPECIAL_SKULL_OWNERS[random.nextInt(SPECIAL_SKULL_OWNERS.length)]; skullOwner = SPECIAL_SKULL_OWNERS[random.nextInt(SPECIAL_SKULL_OWNERS.length)];
} else skullOwner = deathTracker.getRandomUsername(random);
else
{
skullOwner = deathTracker.getRandomUsername(random);
}
ItemStack skull = new ItemStack(Items.skull, 1, PLAYER_SKULL_METADATA); ItemStack skull = new ItemStack(Items.skull, 1, PLAYER_SKULL_METADATA);
skull.stackTagCompound = new NBTTagCompound(); skull.setTagCompound(new NBTTagCompound());
skull.stackTagCompound.setString("SkullOwner", skullOwner); skull.getTagCompound().setString("SkullOwner", skullOwner);
stacks.add(skull); stacks.add(skull);
} }
} }

View file

@ -50,7 +50,7 @@ public class FillContainersOperation extends WorldOperation
} }
else else
{ {
DDLoot.generateChestContents(DDLoot.DungeonChestInfo, chest, random); DDLoot.generateChestContents(DDLoot.dungeonChestInfo, chest, random);
} }
} }
} }