Files
Botania/src/main/java/vazkii/botania/api/lexicon/LexiconEntry.java
T

377 lines
14 KiB
Java

/**
* This class was created by <Vazkii>. It's distributed as
* part of the Botania Mod. Get the Source Code in github:
* https://github.com/Vazkii/Botania
*
* Botania is Open Source and distributed under the
* Botania License: http://botaniamod.net/license.php
*
* File Created @ [Jan 14, 2014, 6:17:06 PM (GMT)]
*/
package vazkii.botania.api.lexicon;
import com.google.common.base.CaseFormat;
import com.google.common.collect.ImmutableMap;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.crafting.IRecipe;
import net.minecraft.item.crafting.Ingredient;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.text.TextFormatting;
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn;
import vazkii.botania.api.BotaniaAPI;
import vazkii.botania.api.recipe.IModRecipe;
import vazkii.botania.api.recipe.RecipeManaInfusion;
import vazkii.botania.common.item.ModItems;
import vazkii.botania.common.lexicon.AlfheimLexiconEntry;
import vazkii.botania.common.lexicon.CompatLexiconEntry;
import vazkii.botania.common.lexicon.RelicLexiconEntry;
import vazkii.botania.common.lexicon.WelcomeLexiconEntry;
import vazkii.botania.common.lexicon.page.PageBrew;
import vazkii.botania.common.lexicon.page.PageCraftingRecipe;
import vazkii.botania.common.lexicon.page.PageElvenRecipe;
import vazkii.botania.common.lexicon.page.PageImage;
import vazkii.botania.common.lexicon.page.PageLoreText;
import vazkii.botania.common.lexicon.page.PageManaInfusionRecipe;
import vazkii.botania.common.lexicon.page.PageModRecipe;
import vazkii.botania.common.lexicon.page.PageMultiblock;
import vazkii.botania.common.lexicon.page.PagePetalRecipe;
import vazkii.botania.common.lexicon.page.PageTerrasteel;
import vazkii.botania.common.lexicon.page.PageText;
import vazkii.botania.common.lib.LibLexicon;
import vazkii.botania.common.lib.ModTags;
import javax.annotation.Nonnull;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static vazkii.botania.common.lib.ResourceLocationHelper.prefix;
public class LexiconEntry implements Comparable<LexiconEntry> {
public final String unlocalizedName;
public final LexiconCategory category;
private KnowledgeType type = BotaniaAPI.basicKnowledge;
public final List<LexiconPage> pages = new ArrayList<>();
private boolean priority = false;
private ItemStack icon = ItemStack.EMPTY;
private final List<ItemStack> extraDisplayedRecipes = new ArrayList<>();
/**
* @param unlocalizedName The unlocalized name of this entry. This will be localized by the client display.
*/
public LexiconEntry(String unlocalizedName, LexiconCategory category) {
this.unlocalizedName = unlocalizedName;
this.category = category;
}
/**
* Sets this page as prioritized, as in, will appear before others in the lexicon.
*/
public LexiconEntry setPriority() {
priority = true;
return this;
}
/**
* Sets the Knowledge type of this entry.
*/
public LexiconEntry setKnowledgeType(KnowledgeType type) {
this.type = type;
return this;
}
public KnowledgeType getKnowledgeType() {
return type;
}
/**
* Sets the display icon for this entry. Overriding the one already there. When adding recipe pages to the
* entry, this will be called once for the result of the first found recipe.
*/
public void setIcon(ItemStack stack) {
icon = stack;
}
public ItemStack getIcon() {
return icon;
}
public boolean isPriority() {
return priority;
}
public String getUnlocalizedName() {
return unlocalizedName;
}
public String getTagline() {
return ""; // Override this if you want a tagline. You probably do
}
@OnlyIn(Dist.CLIENT)
public boolean isVisible() {
return true;
}
private static Map<String, String> multiblocks = ImmutableMap.<String, String>builder()
.put("botania.page.terrasteel4", "botania:terra_plate")
.put("botania.page.manaEnchanting2", "botania:enchanter")
.put("botania.page.aIntro4", "botania:alfheim_portal")
.put("botania.page.gaiaRitual2", "botania:gaia_ritual").build();
// Basic mapping of input -> advancement, requires manually checking first
private static Map<Predicate<Item>, ResourceLocation> inputsToAdvancements = ImmutableMap.<Predicate<Item>, ResourceLocation>builder()
.put(or(ModTags.Items.INGOTS_TERRASTEEL::contains, ModTags.Items.NUGGETS_TERRASTEEL::contains),
prefix("main/terrasteel_pickup"))
.put(ModTags.Items.RUNES::contains, prefix("main/runic_altar_pickup"))
.put(oneOf(ModItems.manaDiamond, ModItems.manaPearl, ModItems.manaSteel, ModItems.manaPowder, ModItems.manaString),
prefix("main/mana_pool_pickup"))
.put(or(ModTags.Items.LIVINGWOOD::contains, ModTags.Items.LIVINGWOOD::contains), prefix("main/pure_daisy_pickup"))
.put(ModTags.Items.PETALS::contains, prefix("main/flower_pickup"))
.build();
private static <T> Predicate<T> or(Predicate<T> first, Predicate<T> second) {
return first.or(second);
}
private static Predicate<Item> oneOf(Item... items) {
return item -> {
for(Item i : items)
if(i == item) return true;
return false;
};
}
public void dump() {
File dir = Paths.get(".", "entries", fixCamelCase(category.getUnlocalizedName().substring(LibLexicon.CATEGORY_PREFIX.length()))).toFile();
if(!dir.exists())
dir.mkdirs();
File f = new File(dir, fixCamelCase(this.unlocalizedName) + ".json");
if (f.exists())
throw new RuntimeException("preexisting file " + f.getAbsolutePath());
Gson gson = new GsonBuilder().setPrettyPrinting().create();
try (OutputStreamWriter os = new OutputStreamWriter(new BufferedOutputStream(new FileOutputStream(f)))) {
Map<String, Object> entry = new LinkedHashMap<>();
entry.put("name", this.getUnlocalizedName());
entry.put("category", fixCamelCase(category.getUnlocalizedName().substring("botania.category.".length())));
entry.put("icon", getIcon().getItem().getRegistryName().toString());
if(this.getKnowledgeType().color != TextFormatting.RESET) {
entry.put("entry_color", Integer.toString(this.getKnowledgeType().color.getColor(), 16));
}
if (isPriority()) {
entry.put("priority", true);
}
if (this instanceof WelcomeLexiconEntry)
entry.put("sortnum", -10);
if (this instanceof RelicLexiconEntry) {
ResourceLocation advancement = ((RelicLexiconEntry) this).advancement;
if(advancement != null)
entry.put("advancement", advancement.toString());
} else if(this instanceof AlfheimLexiconEntry) {
entry.put("advancement", "botania:main/elf_lexicon_pickup");
} else if(this instanceof CompatLexiconEntry) {
entry.put("flag", "|debug,mod:" + ((CompatLexiconEntry) this).mod.toLowerCase(Locale.ROOT));
}
if(!entry.containsKey("advancement")) {
for(Map.Entry<Predicate<Item>, ResourceLocation> thing : inputsToAdvancements.entrySet()) {
if(pages.stream().flatMap(this::getRecipeInputs).map(ItemStack::getItem).anyMatch(thing.getKey())) {
entry.put("advancement", thing.getValue().toString());
break;
}
}
}
List<Object> pages = new ArrayList<>();
for (int i = 0; i < this.pages.size(); i++) {
LexiconPage page = this.pages.get(i);
Map<String, Object> json = new LinkedHashMap<>();
String unlocalizedName = page.getUnlocalizedName();
if (page instanceof PageLoreText) {
json.put("type", "lore_page");
json.put("text", unlocalizedName);
json.put(i % 2 == 0 ? "left_side" : "right_side", "true");
} else if (page instanceof PageText) {
json.put("type", "text");
json.put("text", unlocalizedName);
} else if (page instanceof PageImage) {
json.put("type", "image");
json.put("text", unlocalizedName);
json.put("images", Collections.singletonList(((PageImage) page).resource.toString()));
} else if (page instanceof PageCraftingRecipe) {
List<IRecipe<?>> recipes = ((PageCraftingRecipe) page).getRecipes();
json.put("type", recipes.size() <= 2 ? "crafting" : "crafting_multi");
json.put("text", unlocalizedName);
if (recipes.size() <= 2) {
json.put("recipe", recipes.get(0).getId().toString());
if(recipes.size() == 2)
json.put("recipe2", recipes.get(1).getId().toString());
} else
json.put("recipes", recipes.stream().map(r -> r.getId().toString()).collect(Collectors.joining(";")));
} else if (page instanceof PageManaInfusionRecipe) {
json.put("type", "mana_infusion");
json.put("text", unlocalizedName);
json.put("recipes", ((PageManaInfusionRecipe) page).getRecipes().stream().map(r -> r.getId().toString()).collect(Collectors.joining(";")));
} else if (page instanceof PageModRecipe) {
json.put("type", page instanceof PagePetalRecipe ? "petal_apothecary" : "runic_altar");
json.put("text", unlocalizedName);
List<String> strings = ((PageModRecipe<? extends IModRecipe>) page).getRecipes().stream().map(r -> r.getId().toString()).collect(Collectors.toList());
if(strings.size() == 1) {
json.put("recipe", strings.get(0));
} else {
json.put("recipes", strings);
}
} else if (page instanceof PageElvenRecipe) {
json.put("type", "elven_trade");
json.put("text", unlocalizedName);
json.put("recipes", ((PageElvenRecipe) page).getRecipes().stream().map(r -> r.getId().toString()).collect(Collectors.joining(";")));
} else if (page instanceof PageBrew) {
json.put("type", "brew");
json.put("text", ((PageBrew) page).text);
json.put("flavor", unlocalizedName);
json.put("recipe", ((PageBrew) page).recipe.getId().toString());
} else if (page instanceof PageMultiblock) {
json.put("type", "multiblock");
json.put("multiblock_id", multiblocks.get(unlocalizedName));
json.put("text", unlocalizedName);
if (unlocalizedName.contains("terrasteel") || unlocalizedName.contains("aIntro")) {
json.put("symmetrical", true);
}
} else if(page instanceof PageTerrasteel) {
json.put("type", "terrasteel");
json.put("text", unlocalizedName);
} else {
json.put("type", "_unknown_" + page.getClass().getSimpleName().toLowerCase(Locale.ROOT));
json.put("text", unlocalizedName);
}
pages.add(json);
}
entry.put("pages", pages);
os.write(gson.toJson(entry));
} catch (IOException e) {
e.printStackTrace();
}
}
public Stream<ItemStack> getRecipeInputs(LexiconPage page) {
Stream<Ingredient> stream;
if(page instanceof PageModRecipe) {
stream = ((PageModRecipe<? extends IModRecipe>) page).getRecipes().stream().flatMap(o -> o.getInputs().stream());
} else if(page instanceof PageCraftingRecipe) {
stream = ((PageCraftingRecipe) page).getRecipes().stream().flatMap(o -> o.getIngredients().stream());
} else if(page instanceof PageManaInfusionRecipe) {
stream = ((PageManaInfusionRecipe) page).getRecipes().stream().map(RecipeManaInfusion::getInput);
} else if(page instanceof PageElvenRecipe) {
stream = ((PageElvenRecipe) page).getRecipes().stream().flatMap(o -> o.getInputs().stream());
} else if(page instanceof PageBrew) {
stream = ((PageBrew) page).recipe.getInputs().stream();
} else {
stream = Stream.empty();
}
return stream.flatMap(ingr -> Stream.of(ingr.getMatchingStacks()));
}
private static String fixCamelCase(String s) {
if(LibLexicon.CATEGORY_FUNCTIONAL_FLOWERS.equals(s)) {
return "functional_flowers";
} else if(LibLexicon.CATEGORY_GENERATION_FLOWERS.equals(s)) {
return "generating_flowers";
} else if(s.length() == 6 && s.endsWith("Intro")) {
return "intro";
}
return CaseFormat.LOWER_CAMEL.to(CaseFormat.LOWER_UNDERSCORE, s);
}
/**
* Sets what pages you want this entry to have.
*/
public LexiconEntry setLexiconPages(LexiconPage... pages) {
this.pages.addAll(Arrays.asList(pages));
for(int i = 0; i < this.pages.size(); i++) {
LexiconPage page = this.pages.get(i);
if(!page.skipRegistry)
page.onPageAdded(this, i);
}
return this;
}
/**
* Returns the web link for this entry. If this isn't null, looking at this entry will
* show a "View Online" button in the book. The String returned should be the URL to
* open when the button is clicked.
*/
public String getWebLink() {
return null;
}
/**
* Adds a page to the list of pages.
*/
public void addPage(LexiconPage page) {
pages.add(page);
}
public final String getNameForSorting() {
// todo used to be localized
return (priority ? 0 : 1) + getUnlocalizedName();
}
public List<ItemStack> getDisplayedRecipes() {
ArrayList<ItemStack> list = new ArrayList<>();
for(LexiconPage page : pages) {
ArrayList<ItemStack> itemsAddedThisPage = new ArrayList<>();
for(ItemStack s : page.getDisplayedRecipes()) {
addItem: {
for(ItemStack s1 : itemsAddedThisPage)
if(s1.getItem() == s.getItem())
break addItem;
for(ItemStack s1 : list)
if(s1.isItemEqual(s) && ItemStack.areItemStackTagsEqual(s1, s))
break addItem;
itemsAddedThisPage.add(s);
list.add(s);
}
}
}
list.addAll(extraDisplayedRecipes);
return list;
}
public void addExtraDisplayedRecipe(ItemStack stack) {
extraDisplayedRecipes.add(stack);
}
@Override
public int compareTo(@Nonnull LexiconEntry o) {
return getNameForSorting().compareTo(o.getNameForSorting());
}
}