Skip to content

Instantly share code, notes, and snippets.

@kindlich
Created March 1, 2020 18:01
Show Gist options
  • Save kindlich/cff818eb76f369fd26e2d2f0b313eb56 to your computer and use it in GitHub Desktop.
Save kindlich/cff818eb76f369fd26e2d2f0b313eb56 to your computer and use it in GitHub Desktop.
#loader contenttweaker
#priority 100
#debug
import crafttweaker.item.IItemStack;
import mods.contenttweaker.Random;
import scripts.lootTableTest.lootTableEntry.MyLootTableEntry;
zenClass MyLootTable {
val loot as MyLootTableEntry[];
val minimumCostValue as int;
zenConstructor() {
this.loot = [] as MyLootTableEntry[];
this.minimumCostValue = -1;
}
//Call this to add loot
function addLoot(item as IItemStack, weight as int, cost as int) as MyLootTable {
this.loot += MyLootTableEntry(item, weight, cost);
if(this.minimumCostValue == -1 || this.minimumCostValue > cost) {
this.minimumCostValue = cost;
}
return this;
}
//Call this from your right click function
function getLoot(random as Random, maximumCost as int) as [IItemStack] {
var output = [] as [IItemStack];
var costRemaining = maximumCost;
while(costRemaining >= minimumCostValue) {
var cumulatedWeight = getCumulatedWeight(costRemaining);
var weight = random.nextInt(cumulatedWeight);
//print("Cumulated: " ~ cumulatedWeight ~ ", weight=" ~weight);
for entry in loot {
weight -= entry.weight;
if(weight <= 0) {
output += entry.item;
costRemaining -= entry.cost;
break;
}
}
}
return output;
}
//Call this when everything has been added to the loot table
function sortList() as MyLootTable {
var changed = true;
while(changed) {
changed = false;
for i in 0 .. (loot.length - 1) {
if(loot[i].cost > loot[i + 1].cost) {
var temp = loot[i];
loot[i] = loot[i + 1];
loot[i + 1] = temp;
changed = true;
}
}
}
return this;
}
//Only used internally
function getCumulatedWeight(costRemaining as int) as int {
var cumulatedWeight = 1;
for index, lootTableEntry in loot {
if(lootTableEntry.cost > costRemaining) {
return cumulatedWeight;
}
cumulatedWeight += lootTableEntry.weight;
}
return cumulatedWeight;
}
}
#loader contenttweaker
import mods.contenttweaker.VanillaFactory;
import mods.contenttweaker.Item;
import mods.contenttweaker.IItemRightClick;
import mods.contenttweaker.Random;
import mods.contenttweaker.Commands;
import mods.contenttweaker.ResourceLocation;
import scripts.lootTableTest.lootTable.MyLootTable;
global neptuneBoxLootTable as MyLootTable = MyLootTable()
//item, weight, cost
.addLoot(<item:minecraft:dirt>, 2, 1)
.addLoot(<item:minecraft:glass>, 3, 3)
.addLoot(<item:minecraft:redstone>, 1, 10)
.sortList();
var nepBoxItem = VanillaFactory.createItem("loot_neptunes_bounty");
nepBoxItem.maxStackSize = 1;
nepBoxItem.rarity = "EPIC";
nepBoxItem.creativeTab = <creativetab:misc>;
nepBoxItem.textureLocation = ResourceLocation.create("aquaculture:textures/items/neptunes_bounty.png");
nepBoxItem.unlocalizedName = "aquaculture.item.NeptunesBounty.name";
nepBoxItem.itemRightClick = function(stack, world, player, hand){
if(world.remote){
return "PASS";
}
var totalCost = world.random.nextInt(20);
var loot = neptuneBoxLootTable.getLoot(world.random, totalCost);
var s = totalCost ~ "[";
for item in loot {
s ~= item.commandString;
}
print(s ~ "]");
return "PASS";
};
nepBoxItem.register();
#loader contenttweaker
#priority 101
import crafttweaker.item.IItemStack;
zenClass MyLootTableEntry {
val item as IItemStack;
val weight as int;
val cost as int;
zenConstructor(item as IItemStack, weight as int, cost as int) {
this.item = item;
this.weight = weight;
this.cost = cost;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment