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
113 changes: 59 additions & 54 deletions examples/postInit/loottables.groovy
Original file line number Diff line number Diff line change
@@ -1,62 +1,67 @@

def strongholdLibraryLootTable = loot.getTable(resource('minecraft:chests/stronghold_library'))
import com.cleanroommc.groovyscript.event.LootTablesLoadedEvent

def strongholdLibraryMainPool = strongholdLibraryLootTable.getPool('main')
event_manager.listen { LootTablesLoadedEvent event ->
patchStrongholdLibraryLT(event)
patchChickenLT(event)
patchZombieLT(event)
//event.loot.printTables()
//event.loot.printPools()
//event.loot.printEntries()
}

strongholdLibraryMainPool.addEntry(
loot.entryBuilder()
.name('minecraft:diamond_block')
.item(item('minecraft:diamond_block'))
.weight(1)
.quality(1)
.build()
)

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

chickenLootTable.removePool('main')

chickenLootTable.addPool(
loot.poolBuilder()
.name('main')
.entry(
loot.entryBuilder()
.item(item('minecraft:diamond'))
.function{ stack, random, context ->
stack.setCount(10)
return stack
}
def patchStrongholdLibraryLT(event) {
event.loot.getTable('minecraft:chests/stronghold_library').getPool('main').addEntry(
event.loot.entryBuilder()
.name('minecraft:diamond_block')
.item(item('minecraft:diamond_block'))
.weight(1)
.quality(1)
.build()
)
.condition{ random, context -> random.nextFloat() < 0.05f }
.rollsRange(1.0f, 3.0f)
.bonusRollsRange(0.0f, 0.0f)
.build()
)

def zombieLootTable = loot.getTable('minecraft:entities/zombie')
)
event.loot.printEntries('minecraft:chests/stronghold_library', 'main')
}

zombieLootTable.removePool('main')

zombieLootTable.addPool(
loot.poolBuilder()
.name('main')
.entry(
loot.entryBuilder()
.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()
)
def patchChickenLT(event) {
event.loot.getTable('minecraft:entities/chicken').removePool('main')
event.loot.getTable('minecraft:entities/chicken').addPool(
event.loot.poolBuilder()
.name('main')
.entry(
event.loot.entryBuilder()
.item(item('minecraft:diamond'))
.function{ stack, random, context ->
stack.setCount(10)
return stack
}
.weight(1)
.quality(1)
.build()
)
.condition{ random, context -> random.nextFloat() < 0.05f }
.rollsRange(1.0f, 3.0f)
.bonusRollsRange(0.0f, 0.0f)
.build()
)
}

//Loot.printEntries()
Loot.printEntries('minecraft:chests/stronghold_library', 'main')
def patchZombieLT(event) {
event.loot.getTable('minecraft:entities/zombie').removePool('main')
event.loot.getTable('minecraft:entities/zombie').addPool(
event.loot.poolBuilder()
.name('main')
.entry(
event.loot.entryBuilder()
.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()
)
}
147 changes: 8 additions & 139 deletions src/main/java/com/cleanroommc/groovyscript/compat/loot/Loot.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import com.cleanroommc.groovyscript.api.GroovyBlacklist;
import com.cleanroommc.groovyscript.api.GroovyLog;
import com.cleanroommc.groovyscript.api.IScriptReloadable;
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.event.LootTablesLoadedEvent;
import groovy.lang.Closure;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import net.minecraft.client.Minecraft;
Expand All @@ -20,19 +22,12 @@

public class Loot implements IScriptReloadable {

public LootTableManager tableManager;
public Map<ResourceLocation, LootTable> tables = new Object2ObjectOpenHashMap<>();

@GroovyBlacklist
@ApiStatus.Internal
public void onReload() {
tables.clear();
tableManager = new LootTableManager(null);
}

@GroovyBlacklist
@ApiStatus.Internal
public void afterScriptLoad() {
MinecraftServer server = FMLCommonHandler.instance().getMinecraftServerInstance();
if (server != null) {
for (WorldServer world : server.worlds) {
Expand All @@ -41,141 +36,15 @@ public void afterScriptLoad() {
}
}

@ApiStatus.Internal
@GroovyBlacklist
public void init() {
tableManager = new LootTableManager(null);
}

public LootTable getTable(ResourceLocation name) {
LootTable lootTable = tables.get(name);
if (lootTable == null) GroovyLog.msg("GroovyScript found 0 LootTable(s) named " + name).post();
return lootTable;
}

public LootTable getTable(String name) {
return getTable(new ResourceLocation(name));
}

public void removeTable(ResourceLocation name) {
tables.put(name, LootTable.EMPTY_LOOT_TABLE);
}

public void removeTable(String name) {
tables.put(new ResourceLocation(name), LootTable.EMPTY_LOOT_TABLE);
}

public LootPoolBuilder poolBuilder() {
return new LootPoolBuilder();
}

public LootEntryBuilder entryBuilder() {
return new LootEntryBuilder();
}

public void printTables() {
GroovyLog.Msg out = GroovyLog.msg("GroovyScript found the following LootTable(s)");
tables.keySet().forEach(table -> out.add(table.toString()));
if (!out.postIfNotEmpty())
GroovyLog.msg("GroovyScript found 0 LootTables :thonk:").error().post();
}

public void printPools() {
if (tables.values().isEmpty()) {
GroovyLog.msg("GroovyScript found 0 LootTables :thonk:").error().post();
return;
}

GroovyLog.Msg out = GroovyLog.msg("GroovyScript found the following LootPools(s)");

tables.forEach((rl, table) -> {
if (((LootTableAccessor) table).getPools() == null || ((LootTableAccessor) table).getPools().isEmpty()) {
return;
}
out.add(rl.toString());
((LootTableAccessor) table).getPools().forEach(pool -> out.add("\t - " + pool.getName()));
});

out.postIfNotEmpty();
}

public void printPools(String tableName) {
LootTable table = this.getTable(tableName);
if (table == null) return;
GroovyLog.Msg out = GroovyLog.msg("GroovyScript found the following LootPools(s)");
out.add(tableName);
((LootTableAccessor) table).getPools().forEach(pool -> out.add("\t - " + pool.getName()));
if (!out.postIfNotEmpty())
GroovyLog.msg("GroovyScript found 0 LootPools in " + tableName).error().post();
}

public void printEntries() {
if (tables.values().isEmpty()) {
GroovyLog.msg("GroovyScript found 0 LootTables :thonk:").error().post();
return;
}

GroovyLog.Msg out = GroovyLog.msg("GroovyScript found the following LootEntries(s)");

tables.forEach((rl, table) -> {
if (((LootTableAccessor) table).getPools() == null || ((LootTableAccessor) table).getPools().isEmpty()) {
return;
@ApiStatus.Internal
public void afterScriptLoad() {
MinecraftServer server = FMLCommonHandler.instance().getMinecraftServerInstance();
if (server != null) {
for (WorldServer world : server.worlds) {
world.getLootTableManager().reloadLootTables();
}
out.add(rl.toString());
((LootTableAccessor) table).getPools().forEach(pool -> {
out.add("\t - " + pool.getName());
((LootPoolAccessor) pool).getLootEntries().forEach(entry -> out.add("\t\t - " + entry.getEntryName()));
});
});

out.postIfNotEmpty();
}

public void printEntries(String tableName) {
LootTable table = this.getTable(tableName);
if (table == null) return;
if (((LootTableAccessor) table).getPools() == null || ((LootTableAccessor) table).getPools().isEmpty()) {
GroovyLog.msg("GroovyScript found 0 LootPools in " + tableName).error().post();
return;
}

GroovyLog.Msg out = GroovyLog.msg("GroovyScript found the following LootEntry(s)");
out.add(tableName);
((LootTableAccessor) table).getPools().forEach(pool -> {
out.add("\t - " + pool.getName());
((LootPoolAccessor) pool).getLootEntries().forEach(entry -> out.add("\t\t - " + entry.getEntryName()));
});
if (!out.postIfNotEmpty())
GroovyLog.msg("GroovyScript found 0 LootEntries in LootTable " + tableName).error().post();
}

public void printEntries(String tableName, String poolName) {
LootTable table = this.getTable(tableName);
if (table == null) return;
LootPool pool = table.getPool(poolName);
if (pool == null) {
GroovyLog.msg("GroovyScript could not find LootPool " + poolName + " in LootTable " + tableName).error().post();
return;
}

GroovyLog.Msg out = GroovyLog.msg("GroovyScript found the following LootEntry(s)");
out.add(tableName);
out.add("\t - " + poolName);
((LootPoolAccessor) pool).getLootEntries().forEach(entry -> out.add("\t\t - " + entry.getEntryName()));
if (!out.postIfNotEmpty())
GroovyLog.msg("GroovyScript found 0 LootEntries in LootPool " + poolName).error().post();
}

public GroovyLootFunction function(Closure<Object> function) {
return new GroovyLootFunction(function);
}

public GroovyLootFunction function(Closure<Object> function, LootCondition... conditions) {
return new GroovyLootFunction(conditions, function);
}

public GroovyLootCondition condition(Closure<Object> condition) {
return new GroovyLootCondition(condition);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class LootEntryBuilder {
private int quality;
private final List<LootFunction> functions = new ArrayList<>();
private final List<LootCondition> conditions = new ArrayList<>();
private String tableName;
private ResourceLocation tableName;
private String poolName;
private final GroovyLog.Msg out = GroovyLog.msg("Error creating GroovyScript LootPool").warn();

Expand Down Expand Up @@ -76,12 +76,12 @@ public LootEntryBuilder quality(int quality) {
}

public LootEntryBuilder table(String table) {
this.tableName = table;
this.tableName = new ResourceLocation(table);
return this;
}

public LootEntryBuilder table(ResourceLocation table) {
this.tableName = table.toString();
this.tableName = table;
return this;
}

Expand Down Expand Up @@ -317,9 +317,9 @@ else if (name == null || name.isEmpty()) {
if (quality < 0) out.add("quality < 0 may make the loot entry unable to be rolled");

if (validateForRegister) {
if (tableName == null || tableName.isEmpty() || VanillaModule.loot.getTable(tableName) == null) out.add("No valid LootTable specified").error();
else if (poolName == null || poolName.isEmpty() || VanillaModule.loot.getTable(tableName).getPool(poolName) == null) out.add("No valid LootPool specified").error();
else if (VanillaModule.loot.getTable(tableName).getPool(poolName).getEntry(name) != null) out.add("Attempted to add duplicate key " + name + " to " + tableName + " - " + poolName);
if (tableName == null || VanillaModule.loot.tables.get(tableName) == null) out.add("No valid LootTable specified").error();
else if (poolName == null || poolName.isEmpty() || VanillaModule.loot.tables.get(tableName).getPool(poolName) == null) out.add("No valid LootPool specified").error();
else if (VanillaModule.loot.tables.get(tableName).getPool(poolName).getEntry(name) != null) out.add("Attempted to add duplicate entry " + name + " to " + tableName + " - " + poolName);
}

out.postIfNotEmpty();
Expand All @@ -333,7 +333,7 @@ public LootEntry build() {

public void register() {
if (!validate(true)) return;
VanillaModule.loot.getTable(tableName).getPool(poolName).addEntry(
VanillaModule.loot.tables.get(tableName).getPool(poolName).addEntry(
new LootEntryItem(item, weight, quality, functions.toArray(new LootFunction[0]), conditions.toArray(new LootCondition[0]), name)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class LootPoolBuilder {
private final List<LootCondition> poolConditions = new ArrayList<>();
private RandomValueRange rolls = new RandomValueRange(1);
private RandomValueRange bonusRolls = new RandomValueRange(0);
private String tableName;
private ResourceLocation tableName;
private final GroovyLog.Msg out = GroovyLog.msg("Error creating GroovyScript LootPool").warn();

public LootPoolBuilder name(String name) {
Expand All @@ -41,12 +41,12 @@ public LootPoolBuilder name(ResourceLocation name) {
}

public LootPoolBuilder table(String table) {
this.tableName = table;
this.tableName = new ResourceLocation(table);
return this;
}

public LootPoolBuilder table(ResourceLocation table) {
this.tableName = table.toString();
this.tableName = table;
return this;
}

Expand Down Expand Up @@ -121,8 +121,9 @@ public LootPoolBuilder bonusRollsRange(float min, float max) {
private boolean validate(boolean validateForRegister) {
if (name == null || name.isEmpty()) out.add("No name provided").error();
if (validateForRegister) {
if (tableName == null || tableName.isEmpty() || VanillaModule.loot.getTable(tableName) == null) out.add("No valid LootTable specified").error();
else if (name != null && !name.isEmpty() && VanillaModule.loot.getTable(tableName) != null && VanillaModule.loot.getTable(tableName).getPool(name) != null) out.add("No valid LootPool specified for table " + tableName).error();
if (tableName == null || !VanillaModule.loot.tables.containsKey(tableName)) out.add("No valid LootTable specified").error();
else if (name == null || name.isEmpty()) out.add("LootPool must have a name specified with .name()").error();
else if (VanillaModule.loot.tables.get(tableName).getPool(name) != null) out.add("Attempted to add duplicate pool " + name + " to " + tableName).error();
}
out.postIfNotEmpty();
return out.getLevel() != Level.ERROR;
Expand All @@ -135,7 +136,7 @@ public LootPool build() {

public void register() {
if (!validate(true)) return;
VanillaModule.loot.getTable(tableName).addPool(
VanillaModule.loot.tables.get(tableName).addPool(
new LootPool(lootEntries.toArray(new LootEntry[0]), poolConditions.toArray(new LootCondition[0]), rolls, bonusRolls, name)
);
}
Expand Down
Loading