tcr/java/src/game/item/RngLoot.java
2025-03-12 18:13:11 +01:00

97 lines
3.1 KiB
Java
Executable file

package game.item;
import java.util.Collections;
import game.inventory.IInventory;
import game.rng.Random;
import game.rng.RngItem;
import game.rng.WeightedList;
import game.tileentity.TileEntityDispenser;
public class RngLoot extends RngItem
{
private ItemStack item;
private int minStackSize;
private int maxStackSize;
public RngLoot(Item item, int amount, int min, int max, int weight)
{
super(weight);
this.item = new ItemStack(item, 1, amount);
this.minStackSize = min;
this.maxStackSize = max;
}
public RngLoot(ItemStack stack, int min, int max, int weight)
{
super(weight);
this.item = stack;
this.minStackSize = min;
this.maxStackSize = max;
}
public ItemStack getItem(Random rand) {
if(this.item == null)
return null;
ItemStack stack = this.item.copy();
stack.stackSize = this.minStackSize + rand.zrange(this.maxStackSize - this.minStackSize + 1);
return stack;
}
public static void generateChestContents(Random random, WeightedList<RngLoot> list, IInventory inv, int max)
{
for (int i = 0; i < max; ++i)
{
RngLoot loot = (RngLoot)list.pick(random);
int j = loot.minStackSize + random.zrange(loot.maxStackSize - loot.minStackSize + 1);
if (loot.item.getMaxStackSize() >= j)
{
ItemStack itemstack1 = loot.item.copy();
itemstack1.stackSize = j;
inv.setInventorySlotContents(random.zrange(inv.getSizeInventory()), itemstack1);
}
else
{
for (int k = 0; k < j; ++k)
{
ItemStack itemstack = loot.item.copy();
itemstack.stackSize = 1;
inv.setInventorySlotContents(random.zrange(inv.getSizeInventory()), itemstack);
}
}
}
}
public static void generateDispenserContents(Random random, WeightedList<RngLoot> list, TileEntityDispenser dispenser, int max)
{
for (int i = 0; i < max; ++i)
{
RngLoot loot = (RngLoot)list.pick(random);
int j = loot.minStackSize + random.zrange(loot.maxStackSize - loot.minStackSize + 1);
if (loot.item.getMaxStackSize() >= j)
{
ItemStack itemstack1 = loot.item.copy();
itemstack1.stackSize = j;
dispenser.setInventorySlotContents(random.zrange(dispenser.getSizeInventory()), itemstack1);
}
else
{
for (int k = 0; k < j; ++k)
{
ItemStack itemstack = loot.item.copy();
itemstack.stackSize = 1;
dispenser.setInventorySlotContents(random.zrange(dispenser.getSizeInventory()), itemstack);
}
}
}
}
public static WeightedList<RngLoot> addToList(WeightedList<RngLoot> items, RngLoot... add)
{
WeightedList<RngLoot> list = new WeightedList(items);
Collections.addAll(list, add);
return list;
}
}