Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generify DimensionProperty #2414

Merged
merged 7 commits into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
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_b", getBlockedDimensionIDs().toString())
M-W-K marked this conversation as resolved.
Show resolved Hide resolved
.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,82 @@
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_b",
M-W-K marked this conversation as resolved.
Show resolved Hide resolved
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);
else whiteListDimensions.add(key);
M-W-K marked this conversation as resolved.
Show resolved Hide resolved
}

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.

Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
package gregtech.common.metatileentities.electric;

import gregtech.api.capability.IEnergyContainer;
import gregtech.api.capability.impl.RecipeLogicEnergy;
import gregtech.api.metatileentity.MetaTileEntity;
import gregtech.api.metatileentity.SimpleMachineMetaTileEntity;
import gregtech.api.metatileentity.interfaces.IGregTechTileEntity;
import gregtech.api.recipes.Recipe;
import gregtech.api.recipes.RecipeMap;
import gregtech.api.recipes.RecipeMaps;
import gregtech.api.recipes.recipeproperties.GasCollectorDimensionProperty;
import gregtech.client.renderer.ICubeRenderer;
import gregtech.client.renderer.texture.Textures;

import net.minecraft.util.ResourceLocation;

import it.unimi.dsi.fastutil.ints.IntLists;
import org.jetbrains.annotations.NotNull;

import java.util.function.Function;
import java.util.function.Supplier;

public class MetaTileEntityGasCollector extends SimpleMachineMetaTileEntity {

Expand All @@ -36,28 +29,6 @@ public MetaTileEntity createMetaTileEntity(IGregTechTileEntity tileEntity) {

@Override
protected RecipeLogicEnergy createWorkable(RecipeMap<?> recipeMap) {
return new GasCollectorRecipeLogic(this, recipeMap, () -> energyContainer);
}

protected boolean checkRecipe(@NotNull Recipe recipe) {
for (int dimension : recipe.getProperty(GasCollectorDimensionProperty.getInstance(), IntLists.EMPTY_LIST)) {
if (dimension == this.getWorld().provider.getDimension()) {
return true;
}
}
return false;
}

private static class GasCollectorRecipeLogic extends RecipeLogicEnergy {

public GasCollectorRecipeLogic(MetaTileEntity metaTileEntity, RecipeMap<?> recipeMap,
Supplier<IEnergyContainer> energyContainer) {
super(metaTileEntity, recipeMap, energyContainer);
}

@Override
public boolean checkRecipe(@NotNull Recipe recipe) {
return ((MetaTileEntityGasCollector) metaTileEntity).checkRecipe(recipe) && super.checkRecipe(recipe);
}
return new RecipeLogicEnergy(this, recipeMap, () -> energyContainer);
}
}
Loading
Loading