Skip to content

Commit

Permalink
first pass at CT-facing Material Builder
Browse files Browse the repository at this point in the history
  • Loading branch information
serenibyss committed Sep 5, 2021
1 parent 605ed5e commit e5c0e25
Show file tree
Hide file tree
Showing 3 changed files with 199 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
package gregtech.api.unification.crafttweaker;

import com.google.common.collect.ImmutableList;
import crafttweaker.annotations.ZenRegister;
import gregtech.api.unification.Element;
import gregtech.api.unification.Elements;
import gregtech.api.unification.material.Material;
import gregtech.api.unification.material.Material.FluidType;
import gregtech.api.unification.material.info.MaterialFlag;
import gregtech.api.unification.material.info.MaterialIconSet;
import gregtech.api.unification.stack.MaterialStack;
import stanhebben.zenscript.annotations.Optional;
import stanhebben.zenscript.annotations.ZenClass;
import stanhebben.zenscript.annotations.ZenMethod;

@ZenClass("mods.gregtech.material.MaterialBuilder")
@ZenRegister
@SuppressWarnings("unused")
public class CTMaterialBuilder {

private static int baseID = 32000;

private final Material.Builder backingBuilder;

public CTMaterialBuilder(int id, String name) {
this.backingBuilder = new Material.Builder(id, name);
}

public CTMaterialBuilder(String name) {
this(baseID++, name);
}

@ZenMethod
public CTMaterialBuilder fluid(@Optional String type, @Optional boolean hasBlock) {
backingBuilder.fluid(validateFluidType(type), hasBlock);
return this;
}

@ZenMethod
public CTMaterialBuilder plasma() {
backingBuilder.plasma();
return this;
}

@ZenMethod
public CTMaterialBuilder dust(@Optional int harvestLevel, @Optional int burnTime) {
if (harvestLevel == 0) harvestLevel = 2;
backingBuilder.dust(harvestLevel, burnTime);
return this;
}

@ZenMethod
public CTMaterialBuilder ingot(@Optional int harvestLevel, @Optional int burnTime) {
if (harvestLevel == 0) harvestLevel = 2;
backingBuilder.ingot(harvestLevel, burnTime);
return this;
}


@ZenMethod
public CTMaterialBuilder gem(@Optional int harvestLevel, @Optional int burnTime) {
if (harvestLevel == 0) harvestLevel = 2;
backingBuilder.gem(harvestLevel, burnTime);
return this;
}

@ZenMethod
public CTMaterialBuilder color(int color) {
backingBuilder.color(color);
return this;
}

@ZenMethod
public CTMaterialBuilder colorAverage() {
backingBuilder.colorAverage();
return this;
}

@ZenMethod
public CTMaterialBuilder iconSet(String iconSet) {
backingBuilder.iconSet(MaterialIconSet.getByName(iconSet));
return this;
}

@ZenMethod
public CTMaterialBuilder components(MaterialStack[] components) {
backingBuilder.components(validateComponentList(components));
return this;
}

@ZenMethod
public CTMaterialBuilder flags(String... names) {
for (String name : names) {
MaterialFlag flag = MaterialFlag.getByName(name);
if (flag != null) {
backingBuilder.flags(flag);
}
}
return this;
}

@ZenMethod
public CTMaterialBuilder element(String elementName) {
Element element = Elements.get(elementName);
if (element != null) {
backingBuilder.element(element);
}
return this;
}

@ZenMethod
public CTMaterialBuilder toolStats(float speed, float damage, int durability, @Optional int enchantability) {
if (enchantability == 0) {
enchantability = 21; // Lowest enchantability by default
}
backingBuilder.toolStats(speed, damage, durability, enchantability);
return this;
}

@ZenMethod
public CTMaterialBuilder blastTemp(int temp) {
backingBuilder.blastTemp(temp);
return this;
}

@ZenMethod
public CTMaterialBuilder ore(@Optional int oreMultiplier, @Optional int byproductMultiplier) {
if (oreMultiplier == 0) oreMultiplier = 1;
if (byproductMultiplier == 0) byproductMultiplier = 1;
backingBuilder.ore(oreMultiplier, byproductMultiplier);
return this;
}

@ZenMethod
public CTMaterialBuilder fluidTemp(int temp) {
backingBuilder.fluidTemp(temp);
return this;
}

// TODO Ore stuff

@ZenMethod
public CTMaterialBuilder cableProperties(long voltage, int amperage, int loss, @Optional boolean isSuperCon) {
backingBuilder.cableProperties(voltage, amperage, loss, isSuperCon);
return this;
}

@ZenMethod
public CTMaterialBuilder fluidPipeProperties(int maxTemp, int throughput, boolean gasProof) {
backingBuilder.fluidPipeProperties(maxTemp, throughput, gasProof);
return this;
}

@ZenMethod
public CTMaterialBuilder itemPipeProperties(int priority, float stacksPerSec) {
backingBuilder.itemPipeProperties(priority, stacksPerSec);
return this;
}

// TODO Default enchant?

@ZenMethod
public Material build() {
return backingBuilder.build();
}

private static ImmutableList<MaterialStack> validateComponentList(MaterialStack[] components) {
return components == null || components.length == 0 ? ImmutableList.of() : ImmutableList.copyOf(components);
}

private static FluidType validateFluidType(String fluidTypeName) {
if (fluidTypeName == null || fluidTypeName.equals("fluid")) return FluidType.FLUID;
else if (fluidTypeName.equals("gas")) return FluidType.GAS;
else throw new IllegalArgumentException("Fluid Type must be either \"fluid\" or \"gas\"!");
}
}
17 changes: 16 additions & 1 deletion src/main/java/gregtech/api/unification/material/Material.java
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ public static class Builder {
/*
* The temporary list of components for this Material.
*/
private final List<MaterialStack> composition = new ArrayList<>();
private List<MaterialStack> composition = new ArrayList<>();

/*
* Temporary value to use to determine how to calculate default RGB
Expand Down Expand Up @@ -584,6 +584,16 @@ public Builder gem(int harvestLevel, int burnTime) {
return this;
}

public Builder burnTime(int burnTime) {
DustProperty prop = properties.getProperty(PropertyKey.DUST);
if (prop == null) {
dust();
prop = properties.getProperty(PropertyKey.DUST);
}
prop.setBurnTime(burnTime);
return this;
}

/**
* Set the Color of this Material.<br>
* Defaults to 0xFFFFFF unless {@link Builder#colorAverage()} was called, where
Expand Down Expand Up @@ -639,6 +649,11 @@ public Builder components(Object... components) {
return this;
}

public Builder components(ImmutableList<MaterialStack> components) {
composition = components;
return this;
}

/**
* Add {@link MaterialFlags} to this Material.<br>
* Dependent Flags (for example, {@link MaterialFlags#GENERATE_LONG_ROD} requiring
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
// TODO Make this not take an explicit ID and Name
public class MaterialFlag {

private static final Set<MaterialFlag> FLAG_REGISTRY = new HashSet<>();

private final int id;
private final String name;

Expand All @@ -20,6 +22,7 @@ private MaterialFlag(int id, String name, Set<MaterialFlag> requiredFlags) {
this.id = id;
this.name = name;
this.requiredFlags = requiredFlags;
FLAG_REGISTRY.add(this);
}

@Override
Expand All @@ -44,6 +47,10 @@ public String toString() {
return this.name;
}

public static MaterialFlag getByName(String name) {
return FLAG_REGISTRY.stream().filter(f -> f.toString().equals(name)).findFirst().orElse(null);
}

public static class Builder {

final int id;
Expand Down

0 comments on commit e5c0e25

Please sign in to comment.