Skip to content

Commit

Permalink
Generify DimensionProperty (GregTechCEu#2414)
Browse files Browse the repository at this point in the history
  • Loading branch information
M-W-K authored Mar 28, 2024
1 parent db02d99 commit bf9ce4d
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 165 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import gregtech.api.recipes.RecipeMap;
import gregtech.api.recipes.logic.IParallelableRecipeLogic;
import gregtech.api.recipes.recipeproperties.CleanroomProperty;
import gregtech.api.recipes.recipeproperties.DimensionProperty;
import gregtech.api.recipes.recipeproperties.IRecipePropertyStorage;
import gregtech.api.util.GTTransferUtils;
import gregtech.api.util.GTUtility;
Expand Down Expand Up @@ -407,7 +408,7 @@ protected boolean checkPreviousRecipe() {
* @return true if the recipe is allowed to be used, else false
*/
public boolean checkRecipe(@NotNull Recipe recipe) {
return checkCleanroomRequirement(recipe);
return checkCleanroomRequirement(recipe) && checkDimensionRequirement(recipe);
}

/**
Expand All @@ -431,6 +432,12 @@ protected boolean checkCleanroomRequirement(@NotNull Recipe recipe) {
return false;
}

protected boolean checkDimensionRequirement(@NotNull Recipe recipe) {
if (!recipe.hasProperty(DimensionProperty.getInstance())) return true;
return recipe.getProperty(DimensionProperty.getInstance(), DimensionProperty.DimensionPropertyList.EMPTY_LIST)
.checkDimension(this.getMetaTileEntity().getWorld().provider.getDimension());
}

/**
* Prepares the recipe to be run.
* <ol>
Expand Down
50 changes: 49 additions & 1 deletion src/main/java/gregtech/api/recipes/RecipeBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import gregtech.api.recipes.ingredients.nbtmatch.NBTCondition;
import gregtech.api.recipes.ingredients.nbtmatch.NBTMatcher;
import gregtech.api.recipes.recipeproperties.CleanroomProperty;
import gregtech.api.recipes.recipeproperties.DimensionProperty;
import gregtech.api.recipes.recipeproperties.IRecipePropertyStorage;
import gregtech.api.recipes.recipeproperties.RecipeProperty;
import gregtech.api.recipes.recipeproperties.RecipePropertyStorage;
Expand All @@ -39,6 +40,8 @@
import com.cleanroommc.groovyscript.api.IIngredient;
import com.cleanroommc.groovyscript.helper.ingredient.OreDictIngredient;
import crafttweaker.CraftTweakerAPI;
import it.unimi.dsi.fastutil.ints.IntList;
import it.unimi.dsi.fastutil.ints.IntLists;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
Expand Down Expand Up @@ -137,8 +140,51 @@ public R cleanroom(@Nullable CleanroomType cleanroom) {
return (R) this;
}

public R dimension(int dimensionID) {
return dimension(dimensionID, false);
}

public R dimension(int dimensionID, boolean toBlackList) {
DimensionProperty.DimensionPropertyList dimensionIDs = getCompleteDimensionIDs();
if (dimensionIDs == DimensionProperty.DimensionPropertyList.EMPTY_LIST) {
dimensionIDs = new DimensionProperty.DimensionPropertyList();
this.applyProperty(DimensionProperty.getInstance(), dimensionIDs);
}
dimensionIDs.add(dimensionID, toBlackList);
return (R) this;
}

public DimensionProperty.DimensionPropertyList getCompleteDimensionIDs() {
return this.recipePropertyStorage == null ? DimensionProperty.DimensionPropertyList.EMPTY_LIST :
this.recipePropertyStorage.getRecipePropertyValue(DimensionProperty.getInstance(),
DimensionProperty.DimensionPropertyList.EMPTY_LIST);
}

public IntList getDimensionIDs() {
return this.recipePropertyStorage == null ? IntLists.EMPTY_LIST :
this.recipePropertyStorage.getRecipePropertyValue(DimensionProperty.getInstance(),
DimensionProperty.DimensionPropertyList.EMPTY_LIST).whiteListDimensions;
}

public IntList getBlockedDimensionIDs() {
return this.recipePropertyStorage == null ? IntLists.EMPTY_LIST :
this.recipePropertyStorage.getRecipePropertyValue(DimensionProperty.getInstance(),
DimensionProperty.DimensionPropertyList.EMPTY_LIST).whiteListDimensions;
}

public boolean applyProperty(@NotNull String key, @Nullable Object value) {
if (key.equals(CleanroomProperty.KEY)) {
if (key.equals(DimensionProperty.KEY)) {
if (value instanceof DimensionProperty.DimensionPropertyList list) {
DimensionProperty.DimensionPropertyList dimensionIDs = getCompleteDimensionIDs();
if (dimensionIDs == DimensionProperty.DimensionPropertyList.EMPTY_LIST) {
dimensionIDs = new DimensionProperty.DimensionPropertyList();
this.applyProperty(DimensionProperty.getInstance(), dimensionIDs);
}
dimensionIDs.merge(list);
return true;
}
return false;
} else if (key.equals(CleanroomProperty.KEY)) {
if (value instanceof CleanroomType) {
this.cleanroom((CleanroomType) value);
} else if (value instanceof String) {
Expand Down Expand Up @@ -976,6 +1022,8 @@ public String toString() {
.append("EUt", EUt)
.append("hidden", hidden)
.append("cleanroom", getCleanroom())
.append("dimensions", getDimensionIDs().toString())
.append("dimensions_blocked", getBlockedDimensionIDs().toString())
.append("recipeStatus", recipeStatus)
.toString();
}
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/gregtech/api/recipes/RecipeMaps.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import gregtech.api.recipes.builders.ComputationRecipeBuilder;
import gregtech.api.recipes.builders.FuelRecipeBuilder;
import gregtech.api.recipes.builders.FusionRecipeBuilder;
import gregtech.api.recipes.builders.GasCollectorRecipeBuilder;
import gregtech.api.recipes.builders.ImplosionRecipeBuilder;
import gregtech.api.recipes.builders.PrimitiveRecipeBuilder;
import gregtech.api.recipes.builders.SimpleRecipeBuilder;
Expand Down Expand Up @@ -982,8 +981,8 @@ public final class RecipeMaps {
.build();

@ZenProperty
public static final RecipeMap<GasCollectorRecipeBuilder> GAS_COLLECTOR_RECIPES = new RecipeMapBuilder<>(
"gas_collector", new GasCollectorRecipeBuilder())
public static final RecipeMap<SimpleRecipeBuilder> GAS_COLLECTOR_RECIPES = new RecipeMapBuilder<>(
"gas_collector", new SimpleRecipeBuilder())
.itemInputs(1)
.fluidOutputs(1)
.itemSlotOverlay(GuiTextures.INT_CIRCUIT_OVERLAY, false, true)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package gregtech.api.recipes.recipeproperties;

import gregtech.api.worldgen.config.WorldGenRegistry;

import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.I18n;

import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.ints.IntList;

public class DimensionProperty extends RecipeProperty<DimensionProperty.DimensionPropertyList> {

public static final String KEY = "dimension";

private static DimensionProperty INSTANCE;

private DimensionProperty() {
super(KEY, DimensionPropertyList.class);
}

public static DimensionProperty getInstance() {
if (INSTANCE == null)
INSTANCE = new DimensionProperty();
return INSTANCE;
}

@Override
public void drawInfo(Minecraft minecraft, int x, int y, int color, Object value) {
DimensionPropertyList list = castValue(value);

if (list.whiteListDimensions.size() > 0)
minecraft.fontRenderer.drawString(I18n.format("gregtech.recipe.dimensions",
getDimensionsForRecipe(castValue(value).whiteListDimensions)), x, y, color);
if (list.blackListDimensions.size() > 0)
minecraft.fontRenderer.drawString(I18n.format("gregtech.recipe.dimensions_blocked",
getDimensionsForRecipe(castValue(value).blackListDimensions)), x, y, color);
}

private static String getDimensionsForRecipe(IntList value) {
Int2ObjectMap<String> dimNames = WorldGenRegistry.getNamedDimensions();
StringBuilder builder = new StringBuilder();
for (int i = 0; i < value.size(); i++) {
builder.append(dimNames.getOrDefault(value.getInt(i), String.valueOf(value.getInt(i))));
if (i != value.size() - 1)
builder.append(", ");
}
String str = builder.toString();

if (str.length() >= 13) {
str = str.substring(0, 10) + "..";
}
return str;
}

// It would've been better to have one list and swap between blacklist and whitelist, but that would've been
// a bit awkward to apply to the property in practice.
public static class DimensionPropertyList {

public static DimensionPropertyList EMPTY_LIST = new DimensionPropertyList();

public IntList whiteListDimensions = new IntArrayList();
public IntList blackListDimensions = new IntArrayList();

public void add(int key, boolean toBlacklist) {
if (toBlacklist) {
blackListDimensions.add(key);
whiteListDimensions.rem(key);
} else {
whiteListDimensions.add(key);
blackListDimensions.rem(key);
}
}

public void merge(DimensionPropertyList list) {
this.whiteListDimensions.addAll(list.whiteListDimensions);
this.blackListDimensions.addAll(list.blackListDimensions);
}

public boolean checkDimension(int dim) {
boolean valid = true;
if (this.blackListDimensions.size() > 0) valid = !this.blackListDimensions.contains(dim);
if (this.whiteListDimensions.size() > 0) valid = this.whiteListDimensions.contains(dim);
return valid;
}
}
}

This file was deleted.

Loading

0 comments on commit bf9ce4d

Please sign in to comment.