Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ publishing {
maven(MavenPublication) {
groupId = project.mod_group
artifactId = project.mod_name
version = project.mod_version + "-" + project.bta_version
version = project.mod_version
from components.java
}
}
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ mod_menu_version=3.0.0
halplibe_version=5.2.4

# Mod
mod_version=1.1.2
mod_version=2.0.0
mod_group=turing
mod_name=tmb
7 changes: 6 additions & 1 deletion src/main/java/turing/tmb/InfoRecipeCategory.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,18 @@ public IDrawable getBackground() {

@Override
public void drawRecipe(ITMBRuntime runtime, InfoRecipeTranslator recipe, IRecipeLayout layout, List<IIngredientList> ingredients, ILookupContext context) {
ingredients.add(0, new IngredientList(recipe.getOriginal().getIngredient()));
getIngredients(recipe, layout, context, ingredients);

String text = recipe.getOriginal().getInfoTranslated();
runtime.getGuiHelper().getMinecraft().font.drawStringIntoConstrainedBlock(text, 3, 21, background.getWidth() - 5, 0xFFFFFF, true);
runtime.getGuiHelper().getMinecraft().font.drawStringIntoConstrainedBlock(text, 2, 20, background.getWidth() - 4, 0xFFFFFF);
}

@Override
public void getIngredients(InfoRecipeTranslator recipe, IRecipeLayout layout, ILookupContext context, List<IIngredientList> ingredients) {
ingredients.add(0, new IngredientList(recipe.getOriginal().getIngredient()));
}

@Override
public IRecipeLayout getRecipeLayout() {
return new RecipeLayoutBuilder()
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/turing/tmb/RecipeIndex.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ public List<IRecipeCategory<?>> getAllCategories() {
return Collections.unmodifiableList(categories);
}

@Override
public Map<IRecipeCategory<?>, List<IRecipeTranslator<?>>> getRecipeLists() {
return recipeLists;
}

@Override
public void registerCatalyst(IRecipeCategory<?> category, ITypedIngredient<?> catalyst) {
catalysts.get(category).add(catalyst);
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/turing/tmb/RecipeIngredient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package turing.tmb;

import turing.tmb.api.ingredient.ITypedIngredient;
import turing.tmb.api.recipe.IRecipeCategory;
import turing.tmb.api.recipe.IRecipeTranslator;
import turing.tmb.api.recipe.RecipeIngredientRole;

public class RecipeIngredient {
public final ITypedIngredient<?> ingredient;
public final IRecipeTranslator<?> recipe;
public final IRecipeCategory<? super IRecipeTranslator<?>> category;
public final RecipeIngredientRole role;

public RecipeIngredient(ITypedIngredient<?> ingredient, IRecipeTranslator<?> recipe, IRecipeCategory<?> category, RecipeIngredientRole role) {
this.ingredient = ingredient;
this.recipe = recipe;
this.category = (IRecipeCategory<? super IRecipeTranslator<?>>) category;
this.role = role;
}
}
21 changes: 21 additions & 0 deletions src/main/java/turing/tmb/RecipeTreeIngredient.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package turing.tmb;

public class RecipeTreeIngredient {
public final RecipeIngredient ingredient;
public int x;
public int y;

public RecipeTreeIngredient(RecipeIngredient ingredient, int x, int y) {
this.ingredient = ingredient;
this.x = x;
this.y = y;
}

public int getX() {
return x;
}

public int getY() {
return y;
}
}
122 changes: 120 additions & 2 deletions src/main/java/turing/tmb/TMB.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package turing.tmb;

import com.mojang.nbt.NbtIo;
import com.mojang.nbt.tags.CompoundTag;
import com.mojang.nbt.tags.Tag;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.loader.api.FabricLoader;
import net.minecraft.client.Minecraft;
Expand All @@ -8,19 +11,29 @@
import net.minecraft.client.gui.options.components.OptionsCategory;
import net.minecraft.client.gui.options.data.OptionsPages;
import net.minecraft.client.option.GameSettings;
import net.minecraft.core.item.ItemStack;
import net.minecraft.core.net.command.CommandManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import turing.tmb.api.ITMBPlugin;
import turing.tmb.api.TMBEntrypoint;
import turing.tmb.api.ingredient.ITypedIngredient;
import turing.tmb.api.recipe.IRecipeCategory;
import turing.tmb.api.recipe.IRecipeTranslator;
import turing.tmb.api.recipe.RecipeIngredientRole;
import turing.tmb.api.runtime.ITMBRuntime;
import turing.tmb.plugin.BTATweaker;
import turing.tmb.util.IKeybinds;
import turing.tmb.vanilla.VanillaPlugin;
import turniplabs.halplibe.util.ClientStartEntrypoint;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

public class TMB implements ModInitializer, ClientStartEntrypoint, TMBEntrypoint {
public static final String MOD_ID = "tmb";
Expand Down Expand Up @@ -49,6 +62,9 @@ public static void onClientStart() {
GameSettings settings = Minecraft.getMinecraft().gameSettings;
OptionsCategory category = new OptionsCategory("gui.options.page.controls.category.tmb");
category.withComponent(new KeyBindingComponent(((IKeybinds) settings).toomanyblocks$getKeyHideTMB()));
category.withComponent(new KeyBindingComponent(((IKeybinds) settings).toomanyblocks$getKeyAddFavourite()));
category.withComponent(new KeyBindingComponent(((IKeybinds) settings).toomanyblocks$getKeySetDefaultRecipe()));
category.withComponent(new KeyBindingComponent(((IKeybinds) settings).toomanyblocks$getKeyShowRecipeTree()));
OptionsPages.CONTROLS.withComponent(category);
OptionsCategory category1 = new OptionsCategory("gui.options.page.general.category.tmb");
category1.withComponent(new BooleanOptionComponent(((IKeybinds) settings).toomanyblocks$getIsRecipeViewEnabled()));
Expand All @@ -67,7 +83,7 @@ public void beforeClientStart() {

@Override
public void afterClientStart() {

loadData();
}

private static void loadTMB() {
Expand All @@ -84,9 +100,111 @@ private static void loadTMB() {
plugin.registerRecipeCatalysts(runtime);
plugin.registerRecipes(runtime);
}
runtime.isReady = true;
long timeTook = System.currentTimeMillis() - time;
LOGGER.info("TMB loaded in {}ms!", timeTook);
runtime.isReady = true;
}

public static void saveData() {
File dataFolder = new File(Minecraft.getMinecraft().getMinecraftDir()+"/config/tmb/");
dataFolder.mkdirs();

File dataFile = new File(Minecraft.getMinecraft().getMinecraftDir()+"/config/tmb/", "data.dat");

CompoundTag dataTag = new CompoundTag();

CompoundTag favourites = new CompoundTag();
List<ITypedIngredient<?>> iTypedIngredients = TMB.getRuntime().getFavourites();
for (int j = 0; j < iTypedIngredients.size(); j++) {
ITypedIngredient<?> favourite = iTypedIngredients.get(j);
CompoundTag favouriteTag = new CompoundTag();
favouriteTag.putString("namespace", favourite.getNamespace());
favouriteTag.putString("uid", favourite.getUid());
favourites.put(String.valueOf(j), favouriteTag);
}
dataTag.putCompound("Favourites", favourites);

CompoundTag defaultRecipes = new CompoundTag();
int i = 0;
for (Map.Entry<RecipeIngredient, IRecipeTranslator<?>> entry : TMB.getRuntime().getDefaultRecipes().entrySet()) {
RecipeIngredient key = entry.getKey();
IRecipeTranslator<?> value = entry.getValue();
CompoundTag defaultRecipeTag = new CompoundTag();
CompoundTag ingredientTag = new CompoundTag();
CompoundTag categoryTag = new CompoundTag();

defaultRecipeTag.putString("recipe", value.getOriginal().toString());
ingredientTag.putString("namespace", key.ingredient.getNamespace());
ingredientTag.putString("uid", key.ingredient.getUid());
defaultRecipeTag.put("ingredient", ingredientTag);
categoryTag.putString("namespace", key.category.getNamespace());
categoryTag.putString("name", key.category.getName());
defaultRecipeTag.put("category", categoryTag);
defaultRecipes.put(String.valueOf(i), defaultRecipeTag);
i++;
}
dataTag.putCompound("DefaultRecipes", defaultRecipes);

try {
if(!dataFile.exists()) {
if (!dataFile.createNewFile()) {
return;
}
}
NbtIo.writeCompressed(dataTag, Files.newOutputStream(dataFile.toPath()));
} catch (IOException e) {
throw new RuntimeException(e);
}
}

public static void loadData() {

File dataFolder = new File(Minecraft.getMinecraft().getMinecraftDir()+"/config/tmb/");
dataFolder.mkdirs();

File dataFile = new File(Minecraft.getMinecraft().getMinecraftDir()+"/config/tmb/", "data.dat");
try {
if(!dataFile.exists()) {
return;
}
CompoundTag tag = NbtIo.readCompressed(Files.newInputStream(dataFile.toPath()));

TMB.getRuntime().getFavourites().clear();
for (Tag<?> compoundTag : tag.getCompound("Favourites").getValues()) {
CompoundTag favouriteTag = (CompoundTag) compoundTag;
TMB.getRuntime().getIngredientIndex()
.getIngredient(favouriteTag.getString("namespace"), favouriteTag.getString("uid"))
.ifPresent(TMB.getRuntime().getFavourites()::add);
}

for (Tag<?> compoundTag : tag.getCompound("DefaultRecipes").getValues()) {
CompoundTag defaultRecipeTag = (CompoundTag) compoundTag;
CompoundTag ingredientTag = defaultRecipeTag.getCompound("ingredient");
CompoundTag categoryTag = defaultRecipeTag.getCompound("category");
String recipeId = defaultRecipeTag.getString("recipe");

Optional<IRecipeCategory<?>> category = TMB.getRuntime().getRecipeIndex().getAllCategories().stream()
.filter(it ->
it.getName().equals(categoryTag.getString("name"))
&& it.getNamespace().equals(categoryTag.getString("namespace"))).findFirst();

Optional<IRecipeTranslator<?>> recipe = category
.flatMap(it -> TMB.getRuntime().getRecipeIndex().getRecipeLists().get(it).stream()
.filter(it2 -> it2.getOriginal().toString().equals(recipeId)).findFirst());

Optional<ITypedIngredient<Object>> ingredient = recipe
.flatMap(it -> TMB.getRuntime().getIngredientIndex()
.getIngredient(ingredientTag.getString("namespace"), ingredientTag.getString("uid")));

ingredient
.ifPresent(it -> {
RecipeIngredient recipeIngredient = new RecipeIngredient(it, recipe.get(), category.get(), RecipeIngredientRole.OUTPUT);
TMB.getRuntime().getDefaultRecipes().put(recipeIngredient, recipe.get());
});
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}

private static void clear() {
Expand Down
17 changes: 16 additions & 1 deletion src/main/java/turing/tmb/TMBRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import net.minecraft.client.gui.TooltipElement;
import net.minecraft.core.item.ItemStack;
import net.minecraft.core.util.collection.Pair;
import org.jetbrains.annotations.Nullable;
import turing.tmb.api.ingredient.IIngredientRegistry;
import turing.tmb.api.ingredient.IIngredientType;
import turing.tmb.api.ingredient.IIngredientTypeRegistry;
Expand All @@ -31,6 +30,8 @@ public class TMBRuntime implements ITMBRuntime {
protected final RecipeIndex recipeIndex = new RecipeIndex(this);
protected static final TooltipElement tooltipElement = new TooltipElement(Minecraft.getMinecraft());
protected final GuiHelper guiHelper = new GuiHelper(this);
public final List<ITypedIngredient<?>> favourites = new ArrayList<>();
protected final Map<RecipeIngredient, IRecipeTranslator<?>> defaultRecipes = new HashMap<>();
protected boolean isReady;

protected TMBRuntime() {
Expand All @@ -56,6 +57,10 @@ public void showRecipe(ILookupContext lookup) {
}
}
}
if(results.size() == 1 && lookup.getRole() == RecipeIngredientRole.OUTPUT){
Pair<IRecipeCategory<?>, IRecipeTranslator<?>> pair = results.get(0);
defaultRecipes.put(new RecipeIngredient(lookup.getIngredient(),pair.getRight(),pair.getLeft(),RecipeIngredientRole.OUTPUT),pair.getRight());
}
if (!results.isEmpty()) {
ScreenTMBRecipe.show(results, lookup, null);
}
Expand Down Expand Up @@ -107,6 +112,16 @@ public RecipeIndex getRecipeIndex() {
return recipeIndex;
}

@Override
public Map<RecipeIngredient, IRecipeTranslator<?>> getDefaultRecipes() {
return defaultRecipes;
}

@Override
public List<ITypedIngredient<?>> getFavourites() {
return favourites;
}

public static String getTooltipText(ItemStack stack, boolean showDescription) {
return tooltipElement.getTooltipText(stack, showDescription);
}
Expand Down
33 changes: 32 additions & 1 deletion src/main/java/turing/tmb/TypedIngredient.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.minecraft.core.item.ItemStack;
import turing.tmb.api.VanillaTypes;
import turing.tmb.api.ingredient.IIngredientType;
import turing.tmb.api.ingredient.IIngredientTypeWithSubtypes;
import turing.tmb.api.ingredient.ITypedIngredient;
import turing.tmb.util.ModIDHelper;

Expand All @@ -20,7 +21,7 @@ public TypedIngredient(String namespace, String name, IIngredientType<T> type, T
}

public static TypedIngredient<ItemStack> itemStackIngredient(ItemStack stack) {
return new TypedIngredient<>(ModIDHelper.getModIDForItem(stack), stack.getDisplayName(), VanillaTypes.ITEM_STACK, stack);
return new TypedIngredient<>(ModIDHelper.getModIDForItem(stack), stack.getDisplayName(), VanillaTypes.ITEM_STACK, stack.copy());
}

@Override
Expand All @@ -43,6 +44,36 @@ public String getUid() {
return name;
}

@Override
public String getName() {
if(getType() instanceof IIngredientTypeWithSubtypes){
return ((IIngredientTypeWithSubtypes<?, T>) getType()).getName(ingredient);
}
return "";
}

@Override
public void addAmount(int amount) {
if(getType() instanceof IIngredientTypeWithSubtypes){
((IIngredientTypeWithSubtypes<?, T>) getType()).add(ingredient, amount);
}
}

@Override
public int getAmount() {
if(getType() instanceof IIngredientTypeWithSubtypes){
return ((IIngredientTypeWithSubtypes<?, T>) getType()).getAmount(ingredient);
}
return 1;
}

public boolean matches(Object ingredient){
if(getType() instanceof IIngredientTypeWithSubtypes){
return ((IIngredientTypeWithSubtypes<?, T>) getType()).matches(this.ingredient, ingredient);
}
return false;
}

@Override
public int hashCode() {
return getUid().hashCode() + getNamespace().hashCode();
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/turing/tmb/api/VanillaTypes.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.minecraft.core.WeightedRandomLootObject;
import net.minecraft.core.item.Item;
import net.minecraft.core.item.ItemStack;
import turing.tmb.TypedIngredient;
import turing.tmb.api.ingredient.IIngredientType;
import turing.tmb.api.ingredient.IIngredientTypeWithSubtypes;

Expand Down Expand Up @@ -32,6 +33,28 @@ public Item getBase(ItemStack ingredient) {
public ItemStack getDefaultIngredient(Item base) {
return base.getDefaultStack();
}

@Override
public String getName(ItemStack ingredient) {
return ingredient.getDisplayName();
}

@Override
public void add(ItemStack ingredient, int amount) {
ingredient.stackSize += amount;
}

@Override
public int getAmount(ItemStack ingredient) {
return ingredient.stackSize;
}

@Override
public boolean matches(ItemStack ingredient, Object otherIngredient) {
if(otherIngredient instanceof TypedIngredient<?>) throw new IllegalArgumentException("Received TypedIngredient instead of actual ingredient class, use .getIngredient() when calling this method.");
if(!(otherIngredient instanceof ItemStack)) return false;
return ingredient.isItemEqual((ItemStack) otherIngredient);
}
};

public static final IIngredientType<WeightedRandomLootObject> LOOT_OBJECT = new IIngredientType<WeightedRandomLootObject>() {
Expand Down
Loading
Loading