Skip to content

Instantly share code, notes, and snippets.

@MaxNeedsSnacks
Last active March 28, 2019 12:52
Show Gist options
  • Save MaxNeedsSnacks/83252317a97394452007534e0de8b77d to your computer and use it in GitHub Desktop.
Save MaxNeedsSnacks/83252317a97394452007534e0de8b77d to your computer and use it in GitHub Desktop.
[WIP-ish] This CoT script can easily create tons of materials and materialparts from a basic data map
#loader contenttweaker
#priority 50
import mods.contenttweaker.MaterialPartData;
global toCamelCase as function(string)string = function(stringIn as string) as string {
# Return an invalid string as empty
if(!stringIn.matches(".*[A-Z,a-z].*")) { return ""; }
# Split string by spaces, underscores, etc.
var words as string[] = stringIn.split("[^A-Z,a-z]");
# camelCase ALWAYS starts with a lowercase word
var stringOut = "";
for i, word in words {
if(word.length > 0) {
if(stringOut.length == 0) {
# If output string is still empty, this word shall be lowercase
stringOut += word.toLowerCase();
} else {
# If output string has characters already, the first letter of the new word needs to be uppercase
stringOut += word[0].toUpperCase()~word.substring(1, word.length);
}
}
}
return stringOut;
};
function defaultMoltenData(molten as MaterialPartData) {
molten.addDataValue("density", "6000");
molten.addDataValue("viscosity", "3000");
molten.addDataValue("temperature", "1500");
}
#loader contenttweaker
#priority 50
# This was originally in a /parts/ subfolder
import mods.contenttweaker.PartBuilder;
import mods.contenttweaker.MaterialSystem;
var dirtyGemPart = MaterialSystem.getPartBuilder()
.setName("dirty_gem")
.setPartType(MaterialSystem.getPartType("item"))
.setOreDictName("gemDirty")
.build();
#loader contenttweaker
#priority 50
# This was originally in a /parts/ subfolder
import mods.contenttweaker.PartBuilder;
import mods.contenttweaker.MaterialSystem;
var gemPart = MaterialSystem.getPartBuilder()
.setName("gem")
.setPartType(MaterialSystem.getPartType("item"))
.setOreDictName("gem")
.build();
#loader contenttweaker
#priority 25
import mods.contenttweaker.Color;
import mods.contenttweaker.Material;
import mods.contenttweaker.MaterialSystem as matSys;
import crafttweaker.oredict.IOreDict;
import crafttweaker.data.IData;
import scripts.contenttweaker.base_functions;
/*
Format for presets:
string name: { # the name of the material; should start with an uppcase letter and may be separated by spaces, hyphens, underscores, etc.
string color: # hex code of the colour used for the material
string type: # The type of material. Currently differentiates between "metal" and "gem" - Default: metal
DataList parts: # List of parts / values that should be registered. Recognises:
Metals:
["block", "ingot", "nugget"] - alternatively include defaults to register all of them at once
["dust", "plate", "gear", "rod", "dense_plate"] - alternative include proc_basic for dust, plate, gear only or proc_all for all of them
"ore" - will only register if oreData is defined and non-empty. Make sure your ore data is valid! Remember: Metal Ores should not have drops!
"orealchdust" - Sky Resources compat; you should make sure it only registers when there is an ore. Also you need to register it for alchemy elsewhere!
"molten" - Registers a molten version of the material (currently with default data)
Gems:
["block", "gem", "nugget"] - alternatively include defaults to register all of them at once
["dust", "plate", "gear", "rod", "dense_plate"] - alternative include proc_basic for dust, plate, gear only or proc_all for all of them
"ore" - will only register if oreData is defined and non-empty. Make sure your ore data is valid! Remember: Gem Ores should have drops!
"dirty_gem" - Sky Resources compat; that's it. Make sure to register it for cleaning elsewhere!
"molten" - Registers a molten version of the material (currently with default data)
DataMap oreData (optional): # If defined and non-empty, this will create a new ore with the specified properties.
Valid oreData are: variants, hardness, resistance, harvestLevel, harvestTool. Refer to the CrT wiki for more information.
}
*/
var presets as IData[string][string] = {
# Examples for Metals from my WIP pack, Ad Astra
"Iron": {
color: "e8f2f6"
},
"Copper": {
color: "f6b21f"
},
"Zinc": {
color: "d6e2f1",
parts: ["proc_all", "ore"],
# Zinc does not have an ore, so I'm just registering my own variant
oreData: {
variants: "minecraft:stone",
hardness: 3,
resistance: 3,
harvestLevel: 1,
harvestTool: "pickaxe"
}
},
"Tin": {
color: "a3b7ca"
},
"Aluminum": {
color: "e7e7f3"
},
# Examples for Gems
"Coal": {
type: "gem",
color: "171717",
parts: ["dirty_gem"]
},
"Lapis": {
type: "gem",
color: "2641ff",
parts: ["plate", "dense_plate"]
},
# Cronite, everyone's favourite test metal
/*"Cronite": {
color: "74758f",
parts: ["defaults", "proc_all"]
},*/
# Adamantium, now with 100% more ores
/*"Adamantium": {
color: "920709",
parts: ["defaults", "ore", "proc_all"],
oreData: {
variants: "minecraft:stone",
hardness: "10",
resistance: "15",
harvestLevel: "3",
harvestTool: "pickaxe"
}
},*/
};
var materials as Material[string] = {};
# Create an array that saves pre-camel case names
var names as string[string] = {};
for name in presets {
names[toCamelCase(name)] = name;
}
# Build basic materials and add to materials array
for name, options in presets {
materials[toCamelCase(name)] = matSys.getMaterialBuilder().setName(name).setColor(Color.fromHex(options["color"].asString())).build();
}
for matName, material in materials {
var name as string = names[matName];
var type as string = presets[name].keySet has "type" ? presets[name].type.asString() : "metal";
print("Now processing material "~matName~" ("~name~") of type "~type);
var partsData as IData = presets[name].parts;
var parts as string[] = [];
if(!isNull(partsData)) {
var partsList = partsData.asList();
for part in partsList {
parts += part.asString();
}
} else {
parts = ["proc_all"];
}
if(parts has "block" || parts has "defaults") {
print(" Registering block for material "~matName);
material.registerPart("block");
}
if(type.equalsIgnoreCase("metal") && (parts has "ingot" || parts has "defaults")) {
print(" Registering ingot for material "~matName);
material.registerPart("ingot");
}
if(type.equalsIgnoreCase("gem") && (parts has "gem" || parts has "defaults")) {
print(" Registering gem for material "~matName);
material.registerPart("gem");
}
if(parts has "nugget" || parts has "defaults") {
print(" Registering nugget for material "~matName);
material.registerPart("nugget");
}
if(parts has "dust" || parts has "proc_basic" || parts has "proc_all") {
print(" Registering dust for material "~matName);
material.registerPart("dust");
}
if(parts has "plate" || parts has "proc_basic" || parts has "proc_all") {
print(" Registering plate for material "~matName);
material.registerPart("plate");
}
if(parts has "gear" || parts has "proc_basic" || parts has "proc_all") {
print(" Registering gear for material "~matName);
material.registerPart("gear");
}
if(parts has "rod" || parts has "proc_all") {
print(" Registering rod for material "~matName);
material.registerPart("rod");
}
if(parts has "dense_plate" || parts has "proc_all") {
print(" Registering dense_plate for material "~matName);
material.registerPart("dense_plate");
}
if(parts has "ore") {
# Only register Ore if data exists and not empty
if(presets[name].keySet has "oreData") {
val oreProps = presets[name].oreData.asMap() as IData[string];
# Check if map is non-empty
if(oreProps.length > 0) {
print(name~" has a non-empty oreData key!");
print(" Registering ore for material "~matName);
val ore = material.registerPart("ore").getData();
for key, value in oreProps {
if((["drops", "variants", "hardness", "resistance", "harvestLevel", "harvestTool"] as string[]) has key) {
print("Setting property "~key~" for ore"~matName~" as "~value);
ore.addDataValue(key, (value as IData).asString());
}
}
}
}
}
if(parts has "orealchdust") {
if(type.equalsIgnoreCase("metal")) {
print(" Registering orealchdust for material "~matName);
material.registerPart("orealchdust");
} else {
logger.logError("ERROR: Script is trying to register metal-only type 'orealchdust', but the material is actually of type "~type);
}
}
if(parts has "dirty_gem") {
if(type.equalsIgnoreCase("gem")) {
print(" Registering dirty_gem for material "~matName);
material.registerPart("dirty_gem");
} else {
logger.logError("ERROR: Script is trying to register gem-only type 'dirty_gem', but the material is actually of type "~type);
}
}
# TODO: Add way to set non-default molten data for liquids
if(parts has "molten") {
print("Registering molten form for material "~matName);
var molten = material.registerPart("molten");
base_functions.defaultMoltenData(molten.getData());
}
# WIP: Something else I want to do is have the script TRY to make sense of ANY string in the "parts" array and seeing whether a part can be made with that string.
# This would make this a hell of a lot more configurable
}
#loader contenttweaker
#priority 50
# This was originally in a /parts/ subfolder
import mods.contenttweaker.PartBuilder;
import mods.contenttweaker.MaterialSystem;
var alchDustPart = MaterialSystem.getPartBuilder()
.setName("orealchdust")
.setPartType(MaterialSystem.getPartType("item"))
.setOreDictName("orealchdust")
.build();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment