Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions examples/loot.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@

def pyramidLootTable = loot.getTable("minecraft:chests/stronghold_library")

pyramidLootTable.removePool("main")

pyramidLootTable.addPool(
loot.poolBuilder("main")
.entry(
loot.entryBuilder("minecraft:diamond_block")
.item(item("minecraft:diamond_block"))
.weight(1)
.quality(1)
.build())
.randomChance(1.0f)
.rollsRange(1.0f, 3.0f)
.bonusRollsRange(0.0f, 0.0f)
.build()
)




def pyramidLootTable2 = loot.getTable("minecraft:chests/stronghold_corridor")

pyramidLootTable2.removePool("main")

pyramidLootTable2.addPool(
loot.poolBuilder("main")
.entry(
loot.entryBuilder("minecraft:diamond_block")
.item(item("minecraft:diamond_block"))
.weight(1)
.quality(1)
.build())
.randomChance(1.0f)
.rollsRange(1.0f, 3.0f)
.bonusRollsRange(0.0f, 0.0f)
.build()
)

def chickenLootTable = loot.getTable("minecraft:entities/chicken")

chickenLootTable.removePool("main")

chickenLootTable.addPool(
loot.poolBuilder("main").entry(
loot.entryBuilder("minecraft:pumpkin")
.item(item("minecraft:pumpkin"))
.weight(1)
.quality(1)
.build()
)
.randomChance(1.0f)
.rollsRange(1.0f, 3.0f)
.bonusRollsRange(0.0f, 0.0f)
.build()
)

def zombieLootTable = loot.getTable("minecraft:entities/zombie")

zombieLootTable.removePool("main")

zombieLootTable.addPool(
loot.poolBuilder("main").entry(
loot.entryBuilder("minecraft:potato")
.item(item("minecraft:potato"))
.weight(1)
.quality(1)
.smelt()
.build()
)
.randomChance(1.0f)
.killedByNonPlayer()
.rollsRange(1.0f, 3.0f)
.bonusRollsRange(0.0f, 0.0f)
.build()
)

loot.printEntries()
10 changes: 10 additions & 0 deletions src/main/java/com/cleanroommc/groovyscript/GroovyScript.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@
import com.cleanroommc.groovyscript.brackets.BracketHandlerManager;
import com.cleanroommc.groovyscript.command.CustomClickAction;
import com.cleanroommc.groovyscript.command.GSCommand;
import com.cleanroommc.groovyscript.compat.loot.Loot;
import com.cleanroommc.groovyscript.compat.content.GroovyResourcePack;
import com.cleanroommc.groovyscript.compat.mods.ModSupport;
import com.cleanroommc.groovyscript.compat.mods.tinkersconstruct.TinkersConstruct;
import com.cleanroommc.groovyscript.compat.vanilla.VanillaModule;
import com.cleanroommc.groovyscript.core.mixin.loot.LootPoolAccessor;
import com.cleanroommc.groovyscript.core.mixin.loot.LootTableAccessor;
import com.cleanroommc.groovyscript.core.mixin.DefaultResourcePackAccessor;
import com.cleanroommc.groovyscript.event.EventHandler;
import com.cleanroommc.groovyscript.helper.JsonHelper;
Expand All @@ -34,6 +37,7 @@
import net.minecraft.util.text.TextComponentString;
import net.minecraft.util.text.TextComponentTranslation;
import net.minecraft.util.text.TextFormatting;
import net.minecraft.world.storage.loot.LootTableManager;
import net.minecraftforge.client.settings.KeyConflictContext;
import net.minecraftforge.client.settings.KeyModifier;
import net.minecraftforge.common.MinecraftForge;
Expand Down Expand Up @@ -133,6 +137,12 @@ public static void initializeGroovyPreInit() {
BracketHandlerManager.init();
VanillaModule.initializeBinding();
ModSupport.init();

Loot.TABLE_MANAGER = new LootTableManager(null);
Loot.TABLES.values().forEach(table -> {
((LootTableAccessor) table).setIsFrozen(false);
((LootTableAccessor) table).getPools().forEach(pool -> ((LootPoolAccessor) pool).setIsFrozen(false));
});

boolean wasNull = Loader.instance().activeModContainer() == null;
if (wasNull) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.cleanroommc.groovyscript.compat.loot;

import com.cleanroommc.groovyscript.api.GroovyLog;
import com.cleanroommc.groovyscript.sandbox.ClosureHelper;
import groovy.lang.Closure;
import net.minecraft.world.storage.loot.LootContext;
import net.minecraft.world.storage.loot.conditions.LootCondition;
import org.jetbrains.annotations.NotNull;

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

public class GroovyLootCondition implements LootCondition {

public final Closure<Object> condition;

public GroovyLootCondition(Closure<Object> condition) {
if (Arrays.equals(condition.getParameterTypes(), new Class[]{Random.class, LootContext.class})) {
this.condition = condition;
} else {
GroovyLog.msg("Error creating custom loot condition: condition must take the following parameters (java.util.Random, net.minecraft.world.storage.loot.LootContext).").error().post();
this.condition = null;
}
}

@Override
public boolean testCondition(@NotNull Random rand, @NotNull LootContext context) {
if (this.condition == null) return false;
return ClosureHelper.call(true, condition, rand, context);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.cleanroommc.groovyscript.compat.loot;

import com.cleanroommc.groovyscript.api.GroovyLog;
import com.cleanroommc.groovyscript.sandbox.ClosureHelper;
import groovy.lang.Closure;
import net.minecraft.item.ItemStack;
import net.minecraft.world.storage.loot.LootContext;
import net.minecraft.world.storage.loot.conditions.LootCondition;
import net.minecraft.world.storage.loot.functions.LootFunction;
import org.jetbrains.annotations.NotNull;

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

public class GroovyLootFunction extends LootFunction {

private final Closure<Object> function;

public GroovyLootFunction(Closure<Object> function) {
this(new LootCondition[0], function);
}

public GroovyLootFunction(LootCondition[] conditions, Closure<Object> function) {
super(conditions);
if (Arrays.equals(function.getParameterTypes(), new Class[]{ItemStack.class, Random.class, LootContext.class})) {
this.function = function;
} else {
GroovyLog.msg("Error creating custom loot function: function must take the following parameters (net.minecraft.item.ItemStack, java.util.Random, net.minecraft.world.storage.loot.LootContext).").error().post();
this.function = null;
}
}

@Override
public @NotNull ItemStack apply(@NotNull ItemStack stack, @NotNull Random rand, @NotNull LootContext context) {
if (function == null) return ItemStack.EMPTY;
return ClosureHelper.call(ItemStack.EMPTY, function, stack, rand, context);
}

}
Loading