Un-specialcase elven lexicon recipe, improve elven trade api (#3062)

This commit is contained in:
Hubry authored and Vincent Lee committed 2019-12-24 15:11:00 -06:00
1 parent 566c29a809
commit 554a34e8be
4 files changed
+72 -39

No files matched your search

@@ -8,6 +8,7 @@ import net.minecraft.util.ResourceLocation;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
public class RecipeElvenTrade {
private final ResourceLocation id;
@@ -24,7 +25,12 @@ public class RecipeElvenTrade {
this.inputs = ImmutableList.copyOf(inputs);
}
public boolean matches(List<ItemStack> stacks, boolean remove) {
/**
* Attempts to match the recipe
* @param stacks Entire contents of the portal's buffer
* @return {@link Optional#empty()} if recipe doesn't match, Optional with a set of items used by recipe otherwise
*/
public Optional<List<ItemStack>> match(List<ItemStack> stacks) {
List<Ingredient> inputsMissing = new ArrayList<>(inputs);
List<ItemStack> stacksToRemove = new ArrayList<>();
@@ -51,11 +57,19 @@ public class RecipeElvenTrade {
inputsMissing.remove(stackIndex);
}
if(remove)
for(ItemStack r : stacksToRemove)
stacks.remove(r);
return inputsMissing.isEmpty() ? Optional.of(stacksToRemove) : Optional.empty();
}
return inputsMissing.isEmpty();
/**
* If the recipe does not contain the item, it will be destroyed upon entering the portal.
*/
public boolean containsItem(ItemStack stack) {
for(Ingredient input : inputs) {
if(input.test(stack)) {
return true;
}
}
return false;
}
public ResourceLocation getId() {
@@ -70,6 +84,10 @@ public class RecipeElvenTrade {
return outputs;
}
public List<ItemStack> getOutputs(List<ItemStack> inputs) {
return getOutputs();
}
public void write(PacketBuffer buf) {
buf.writeResourceLocation(id);
buf.writeVarInt(inputs.size());
@@ -76,7 +76,7 @@ public class JEIBotaniaPlugin implements IModPlugin {
registry.registerSubtypeInterpreter(ModItems.twigWand, stack -> ItemTwigWand.getColor1(stack) + "_" + ItemTwigWand.getColor2(stack));
registry.registerSubtypeInterpreter(ModItems.flightTiara, stack -> String.valueOf(ItemFlightTiara.getVariant(stack)));
registry.registerSubtypeInterpreter(ModItems.lexicon, stack -> String.valueOf(ItemNBTHelper.getBoolean(stack, ItemLexicon.TAG_ELVEN_UNLOCK, true)));
registry.registerSubtypeInterpreter(ModItems.lexicon, stack -> String.valueOf(ItemNBTHelper.getBoolean(stack, ItemLexicon.TAG_ELVEN_UNLOCK, false)));
registry.registerSubtypeInterpreter(ModItems.laputaShard, stack -> String.valueOf(ItemLaputaShard.getShardLevel(stack)));
for(Item item : new Item[]{ModItems.manaTablet, ModItems.manaRing, ModItems.manaRingGreater, ModItems.terraPick}) {
@@ -181,7 +181,7 @@ public class JEIBotaniaPlugin implements IModPlugin {
for(RecipeElvenTrade recipe : BotaniaAPI.elvenTradeRecipes.values()) {
List<Ingredient> inputs = recipe.getInputs();
List<ItemStack> outputs = recipe.getOutputs();
if(inputs.size() == 1 && outputs.size() == 1 && inputs.get(0).test(outputs.get(0))) {
if(inputs.size() == 1 && outputs.size() == 1 && recipe.containsItem(outputs.get(0))) {
recipeRegistry.hideRecipe(recipe, ElvenTradeRecipeCategory.UID);
}
}
@@ -10,13 +10,10 @@
*/
package vazkii.botania.common.block.tile;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.entity.item.ItemEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.item.crafting.Ingredient;
import net.minecraft.nbt.CompoundNBT;
import net.minecraft.tileentity.ITickableTileEntity;
import net.minecraft.tileentity.TileEntity;
@@ -40,17 +37,15 @@ import vazkii.botania.common.block.mana.BlockPool;
import vazkii.botania.common.block.tile.mana.TilePool;
import vazkii.botania.common.core.handler.ConfigHandler;
import vazkii.botania.common.item.ItemLexicon;
import vazkii.botania.common.item.ModItems;
import vazkii.botania.common.lib.LibBlockNames;
import vazkii.botania.common.lib.LibMisc;
import vazkii.patchouli.api.IMultiblock;
import vazkii.patchouli.api.PatchouliAPI;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.Optional;
public class TileAlfPortal extends TileMod implements ITickableTileEntity {
@@ -135,9 +130,8 @@ public class TileAlfPortal extends TileMod implements ITickableTileEntity {
}
}
if(ticksSinceLastItem >= 4) {
if(!world.isRemote)
resolveRecipes();
if(!world.isRemote && !stacksIn.isEmpty() && ticksSinceLastItem >= 4) {
resolveRecipes();
}
}
@@ -159,14 +153,9 @@ public class TileAlfPortal extends TileMod implements ITickableTileEntity {
}
private boolean validateItemUsage(ItemStack inputStack) {
if(inputStack.getItem() == ModItems.lexicon)
return true;
for(RecipeElvenTrade recipe : BotaniaAPI.elvenTradeRecipes.values()) {
for(Ingredient o : recipe.getInputs()) {
if(o.test(inputStack)) {
return true;
}
if(recipe.containsItem(inputStack)) {
return true;
}
}
if(inputStack.getItem() == Items.BREAD) //Don't teleport bread. (See also: #2403)
@@ -228,25 +217,16 @@ public class TileAlfPortal extends TileMod implements ITickableTileEntity {
}
private void resolveRecipes() {
int i = 0;
for(ItemStack stack : stacksIn) {
if(!stack.isEmpty() && stack.getItem() == ModItems.lexicon) {
if (!stack.getOrCreateTag().getBoolean(ItemLexicon.TAG_ELVEN_UNLOCK)) {
stack.getTag().putBoolean(ItemLexicon.TAG_ELVEN_UNLOCK, true);
spawnItem(stack);
stacksIn.remove(i);
return;
}
}
i++;
}
List<BlockPos> pylons = locatePylons();
for(RecipeElvenTrade recipe : BotaniaAPI.elvenTradeRecipes.values()) {
if(recipe.matches(stacksIn, false)) {
Optional<List<ItemStack>> match = recipe.match(stacksIn);
if(match.isPresent()) {
if(consumeMana(pylons, 500, false)) {
recipe.matches(stacksIn, true);
for(ItemStack output : recipe.getOutputs())
List<ItemStack> inputs = match.get();
for(ItemStack stack : inputs) {
stacksIn.remove(stack);
}
for(ItemStack output : recipe.getOutputs(inputs))
spawnItem(output.copy());
}
break;
@@ -4,15 +4,22 @@ import net.minecraft.block.Blocks;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.item.crafting.Ingredient;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import vazkii.botania.api.recipe.RecipeElvenTrade;
import vazkii.botania.api.recipe.RegisterRecipesEvent;
import vazkii.botania.common.block.ModBlocks;
import vazkii.botania.common.core.helper.ItemNBTHelper;
import vazkii.botania.common.item.ItemLexicon;
import vazkii.botania.common.item.ModItems;
import vazkii.botania.common.lib.LibMisc;
import vazkii.botania.common.lib.ModTags;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import static vazkii.botania.common.lib.ResourceLocationHelper.prefix;
@Mod.EventBusSubscriber(modid = LibMisc.MOD_ID)
@@ -40,6 +47,34 @@ public class ModElvenTradeRecipes {
evt.elvenTrade().accept(new RecipeElvenTrade(prefix("ender_pearl_return"), new ItemStack(Items.ENDER_PEARL), Ingredient.fromItems(Items.ENDER_PEARL)));
evt.elvenTrade().accept(new RecipeElvenTrade(prefix("diamond_return"), new ItemStack(Items.DIAMOND), Ingredient.fromItems(Items.DIAMOND)));
evt.elvenTrade().accept(new RecipeElvenTrade(prefix("diamond_block_return"), new ItemStack(Blocks.DIAMOND_BLOCK), Ingredient.fromItems(Blocks.DIAMOND_BLOCK)));
ItemStack elvenLexicon = new ItemStack(ModItems.lexicon);
elvenLexicon.getOrCreateTag().putBoolean(ItemLexicon.TAG_ELVEN_UNLOCK, true);
evt.elvenTrade().accept(new RecipeLexicon(prefix("elven_lexicon"), elvenLexicon, Ingredient.fromStacks(new ItemStack(ModItems.lexicon))));
}
private static class RecipeLexicon extends RecipeElvenTrade {
RecipeLexicon(ResourceLocation id, ItemStack output, Ingredient input) {
super(id, output, input);
}
@Override
public boolean containsItem(ItemStack stack) {
return super.containsItem(stack) && !ItemNBTHelper.getBoolean(stack, ItemLexicon.TAG_ELVEN_UNLOCK, false);
}
@Override
public Optional<List<ItemStack>> match(List<ItemStack> stacks) {
if(stacks.size() == 1 && !ItemNBTHelper.getBoolean(stacks.get(0), ItemLexicon.TAG_ELVEN_UNLOCK, false))
return super.match(stacks);
return Optional.empty();
}
@Override
public List<ItemStack> getOutputs(List<ItemStack> inputs) {
ItemStack stack = inputs.get(0).copy();
stack.getOrCreateTag().putBoolean(ItemLexicon.TAG_ELVEN_UNLOCK, true);
return Collections.singletonList(stack);
}
}
}