Create rules with ZenScript
codetaylor opened this issue ยท 3 comments
Syntax example:
import mods.dropt.Dropt;
Dropt.addRule(Dropt.rule()
.debug(true)
.match(Dropt.matchBlocks()
.type("WHITELIST")
.blocks(["minecraft:dirt", "minecraft:gravel"])
)
.match(Dropt.matchDrops()
.type("WHITELIST")
.blocks(["minecraft:string", "minecraft:flint"])
)
.match(Dropt.matchHarvester()
.type("PLAYER")
.require("ANY")
.stages(Dropt.matchGameStages()
.type("WHITELIST")
.require("ALL")
.stages(["one", "two"])
)
.playerName(Dropt.matchPlayerName()
.type("WHITELIST")
.names(["player1", "player2"])
)
)
.replaceStrategy("ENUM")
.dropStrategy("ENUM")
.dropCount(Dropt.randomFortuneInt()
.fixed(1)
.range(1, 2)
.fortuneModifier(0)
)
.addDrop(Dropt.drop()
.selector(Dropt.dropSelector()
.weight(Dropt.dropSelectorWeight()
.value(10)
.fortuneModifier(10)
)
.silktouch("EXCLUDED")
.fortuneLevelRequired(2)
)
.item(Dropt.dropItem()
.items(["minecraft:string"])
.quantity(Dropt.randomFortuneInt()
.fixed(1)
.range(1, 2)
.fortuneModifier(0)
)
)
.xp(Dropt.randomFortuneInt()
.fixed(1)
.range(1, 2)
.fortuneModifier(0)
)
)
);
As someone that is familiar with the basics of ZenScript and it trying to figure out your json structure, this feels like a little bit of a mess. Simplifying the bottom depth might actually make it clearer.
Example:
import mods.dropt.Dropt;
Dropt.addRule(Dropt.rule()
.debug(true)
.match(Dropt.matchBlocks("WHITELIST", ["minecraft:dirt", "minecraft:gravel"])
.match(Dropt.matchDrops("WHITELIST", ["minecraft:string", "minecraft:flint"])
.match(Dropt.matchHarvester()
.type("PLAYER")
.require("ANY")
.stages(Dropt.matchGameStages("WHITELIST", "ALL", ["one", "two"])
.playerName(Dropt.matchPlayerName("WHITELIST", ["player1", "player2"])
)
.replaceStrategy("ENUM")
.dropStrategy("ENUM")
.dropCount(Dropt.randomFortuneInt(1, (1, 2), 0)
.addDrop(Dropt.drop()
.selector(Dropt.dropSelector()
.weight(Dropt.dropSelectorWeight(10, 10)
)
.silktouch("EXCLUDED")
.fortuneLevelRequired(2)
)
.item(Dropt.dropItem()
.items(["minecraft:string"])
.quantity(Dropt.randomFortuneInt(1, (1, 2), 0)
)
.xp(Dropt.randomFortuneInt(1, (1, 2), 0)
)
);
As long as it is all documented this is perfectly clear and takes up less space. I don't know if ZenScript supports named arguments, I tried looking it up but could not find an answer. But if it does this could be made even more compact while still being clear.
Here is a third iteration on the syntax:
Dropt.add(Dropt.rule()
// Rule#debug()
.debug()
// If multiple calls to exclusive methods are made,
// log warnings and use the last parameters passed. Provide
// a config setting to disable log warnings for this to allow
// intentional abuse.
// Blocks are provided as a block-string instead of an item
// stack to prevent CrT from translating them into IITemStacks.
// Rule#match_blocks(blockstrings:string[])
// Rule#match_blocks(type:string, blockstrings:string[])
.match_blocks("WHITELIST", ["minecraft:dirt", "minecraft:gravel"])
// Rule#match_drops(items:IIngredient[])
// Rule#match_drops(type:string, items:IIngredient[])
.match_drops("WHITELIST", [<minecraft:string>, <minecraft:flint>])
// Rule#match_harvester(harvester:Harvester)
.match_harvester(Dropt.harvester()
// Harvester#type(type:string)
.type("PLAYER")
// Harvester#main_hand(harvest_level:string)
// Harvester#main_hand(items:IItemStack[])
// Harvester#main_hand(type:string, items:IItemStack[], harvest_level:string)
.main_hand("WHITELIST", [<minecraft:string>], "shovel;0;1")
// Harvester#game_stages(stages:string[])
// Harvester#game_stages(require:string, stages:string[])
// Harvester#game_stages(type:string, require:string, stages:string[])
.game_stages("WHITELIST", "ALL", ["one", "two"]), // gamestages, nullable
// Harvester#player_name(names:string[])
// Harvester#player_name(type:string, names:string[])
.player_name("WHITELIST", ["player1", "player2"]) // player name, nullable
)
// Rule#match_biomes(ids:string[])
// Rule#match_biomes(type:string, ids:string[])
.match_biomes("WHITELIST", ["minecraft:birch_forest_hills"])
// Rule#match_dimensions(ids:int[])
// Rule#match_dimensions(type:string, ids:int[])
.match_dimensions("WHITELIST", [-1, 0, 1])
// Rule#match_vertical_range(min:int, max:int)
.match_vertical_range(32, 128)
// Rule#replace_strategy(strategy:string)
.replace_strategy("ENUM")
// Rule#drop_strategy(strategy:string)
.drop_strategy("ENUM")
// Rule#drop_count(range:Range)
.drop_count(Dropt.range(0, 3, 1))
// Rule#add_drop(drop:Drop)
.add_drop(Dropt.drop()
// Drop#selector(weight:Weight, silk_touch:string, fortune_level_required:int)
.selector(Dropt.weight(1, 0), "ANY", 0),
// Drop#items(items:IItemStack[], count:Range)
.items([<minecraft:string>], Dropt.range(1, 2, 0))
// Drop#xp(amount:Range)
// Drop#xp(replace:string, amount:Range)
.xp("ADD", Dropt.range(0, 10, 10))
)
);
// range(fixed:int)
// range(min:int, max:int)
// range(min:int, max:int, fortune_modifier:int)
Dropt.range(0, 3, 1)
// weight(value:int)
// weight(value:int, fortune_modifier:int)
Dropt.weight(1, 0)