Just enough wizardry!

- Adds JEI support for arcane workbench 'recipes' (charging, upgrading, and scroll enchantment)
- Adds an item transfer handler for said recipes, including bookshelf virtual slot support
- Prevents conjured items from showing up in JEI
This commit is contained in:
Electroblob77
2020-04-15 23:01:45 +01:00
parent 945c789482
commit 8301a24afe
12 changed files with 641 additions and 78 deletions
@@ -0,0 +1,119 @@
package electroblob.wizardry.integration.jei;
import electroblob.wizardry.client.DrawingUtils;
import electroblob.wizardry.constants.Constants;
import electroblob.wizardry.constants.Element;
import electroblob.wizardry.inventory.ContainerArcaneWorkbench;
import electroblob.wizardry.item.IWorkbenchItem;
import electroblob.wizardry.registry.WizardryItems;
import mezz.jei.api.ingredients.IIngredients;
import mezz.jei.api.ingredients.VanillaTypes;
import mezz.jei.api.recipe.IRecipeWrapper;
import net.minecraft.client.Minecraft;
import net.minecraft.item.ItemStack;
import net.minecraft.util.math.MathHelper;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
/**
* Represents a 'recipe' for the arcane workbench.
*
* @author Electroblob
* @since Wizardry 4.3
*/
public class ArcaneWorkbenchRecipe implements IRecipeWrapper {
private final ItemStack centreStack;
private final List<ItemStack> books;
private final List<ItemStack> crystals;
private final List<ItemStack> upgrades;
private final ItemStack result;
private final int bookSlots;
private final List<List<ItemStack>> inputs;
public ArcaneWorkbenchRecipe(ItemStack centreStack, List<ItemStack> books, List<ItemStack> crystals, List<ItemStack> upgrades, ItemStack result){
this.centreStack = centreStack;
this.books = books; // CAUTION! This list is an OUTER LIST!
this.crystals = crystals;
this.upgrades = upgrades;
this.result = result;
this.inputs = new ArrayList<>();
for(ItemStack book : books) this.inputs.add(Collections.singletonList(book));
this.inputs.add(crystals);
this.inputs.add(Collections.singletonList(centreStack));
this.inputs.add(upgrades);
if(centreStack.getItem() instanceof IWorkbenchItem){
bookSlots = ((IWorkbenchItem)centreStack.getItem()).getSpellSlotCount(centreStack);
}else{
bookSlots = 0;
}
}
public ArcaneWorkbenchRecipe(ItemStack centreStack, List<ItemStack> books, int mana, List<ItemStack> upgrades, ItemStack result){
this(centreStack, books, generateCrystalStacks(mana), upgrades, result);
}
@Override
public void getIngredients(IIngredients ingredients){
ingredients.setInputLists(VanillaTypes.ITEM, inputs);
ingredients.setOutput(VanillaTypes.ITEM, result);
}
@Override
public void drawInfo(Minecraft minecraft, int recipeWidth, int recipeHeight, int mouseX, int mouseY){
// Can't do this in IRecipeCategory#drawExtras because we have no access to the recipe there!
// ArcaneWorkbenchRecipeCategory.TEXTURE is already bound at this point
for(int i = 0; i < bookSlots; i++){
int x = ArcaneWorkbenchRecipeCategory.CENTRE_SLOT_X + ContainerArcaneWorkbench.getBookSlotXOffset(i, bookSlots) - 9;
int y = ArcaneWorkbenchRecipeCategory.CENTRE_SLOT_Y + ContainerArcaneWorkbench.getBookSlotYOffset(i, bookSlots) - 9;
DrawingUtils.drawTexturedRect(x, y, 0, ArcaneWorkbenchRecipeCategory.HEIGHT, 36, 36, 256, 256);
}
}
/**
* Returns a list of item stacks, one for each type of crystal (regular, elemental, grand and shards), each with
* the minimum quantity needed to supply the given amount of mana. Types of crystal for which more than the max.
* stack size would be needed are ignored.
*/
public static List<ItemStack> generateCrystalStacks(int mana){
if(mana < 0) throw new IllegalArgumentException("Cannot create an arcane workbench recipe with negative mana!");
if(mana == 0) return Collections.emptyList();
List<ItemStack> crystalStacks = new ArrayList<>();
int count = MathHelper.ceil((float)mana / Constants.MANA_PER_CRYSTAL);
// A stack of crystals will almost certainly be enough mana, but you never know!
// Using ItemStack.EMPTY to avoid deprecated method; crystals' stack size is not stack-sensitive so it doesn't matter
if(count <= WizardryItems.magic_crystal.getItemStackLimit(ItemStack.EMPTY)){
for(int meta = 0; meta < Element.values().length; meta++){
crystalStacks.add(new ItemStack(WizardryItems.magic_crystal, count, meta));
}
}
count = MathHelper.ceil((float)mana / Constants.MANA_PER_SHARD);
if(count <= WizardryItems.crystal_shard.getItemStackLimit(ItemStack.EMPTY)){
crystalStacks.add(new ItemStack(WizardryItems.crystal_shard, count));
}
count = MathHelper.ceil((float)mana / Constants.GRAND_CRYSTAL_MANA);
if(count <= WizardryItems.grand_crystal.getItemStackLimit(ItemStack.EMPTY)){
crystalStacks.add(new ItemStack(WizardryItems.grand_crystal, count));
}
return crystalStacks;
}
}
@@ -0,0 +1,269 @@
package electroblob.wizardry.integration.jei;
import com.google.common.collect.Streams;
import electroblob.wizardry.Wizardry;
import electroblob.wizardry.constants.Constants;
import electroblob.wizardry.constants.Element;
import electroblob.wizardry.inventory.ContainerArcaneWorkbench;
import electroblob.wizardry.item.*;
import electroblob.wizardry.registry.WizardryItems;
import electroblob.wizardry.registry.WizardryRecipes;
import electroblob.wizardry.spell.Spell;
import electroblob.wizardry.util.WandHelper;
import mezz.jei.api.IGuiHelper;
import mezz.jei.api.gui.IDrawable;
import mezz.jei.api.gui.IGuiItemStackGroup;
import mezz.jei.api.gui.IRecipeLayout;
import mezz.jei.api.ingredients.IIngredients;
import mezz.jei.api.ingredients.VanillaTypes;
import mezz.jei.api.recipe.IRecipeCategory;
import mezz.jei.api.recipe.IRecipeCategoryRegistration;
import net.minecraft.client.resources.I18n;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.NonNullList;
import net.minecraft.util.ResourceLocation;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
/**
* JEI recipe category implementation for all 'recipes' in the arcane workbench (of course, the arcane workbench
* doesn't have 'recipes' in the normal sense, but that's how they're displayed in JEI).
*
* @author Electroblob
* @since Wizardry 4.3
*/
public class ArcaneWorkbenchRecipeCategory implements IRecipeCategory<ArcaneWorkbenchRecipe> {
static final String UID = "ebwizardry:arcane_workbench";
static final ResourceLocation TEXTURE = new ResourceLocation(Wizardry.MODID, "textures/gui/container/arcane_workbench_jei_background.png");
static final int WIDTH = 166;
static final int HEIGHT = 126;
// Annoyingly, these seem to be for 18x18 slots rather than the actual highlighted area, unlike container slots
static final int CENTRE_SLOT_X = 74;
static final int CENTRE_SLOT_Y = 54;
static final int CRYSTAL_SLOT_X = 7;
static final int CRYSTAL_SLOT_Y = 91;
static final int UPGRADE_SLOT_X = 141;
static final int UPGRADE_SLOT_Y = 7;
static final int OUTPUT_SLOT_X = 141;
static final int OUTPUT_SLOT_Y = 101;
private final IDrawable background;
public ArcaneWorkbenchRecipeCategory(IRecipeCategoryRegistration registry){
IGuiHelper helper = registry.getJeiHelpers().getGuiHelper();
background = helper.createDrawable(TEXTURE, 0, 0, WIDTH, HEIGHT);
}
@Override
public String getUid(){
return UID;
}
@Override
public String getTitle(){
// JEI is client-side, so client classes can safely be used here
return I18n.format("integration.jei.category." + UID);
}
@Override
public String getModName(){
return Wizardry.NAME;
}
@Override
public IDrawable getBackground(){
return background;
}
@Override
public void setRecipe(IRecipeLayout recipeLayout, ArcaneWorkbenchRecipe recipeWrapper, IIngredients ingredients){
// Okay, they're not technically *slots* but to all intents and purposes, that's how they behave
IGuiItemStackGroup slots = recipeLayout.getItemStacks();
List<List<ItemStack>> inputs = ingredients.getInputs(VanillaTypes.ITEM);
List<List<ItemStack>> outputs = ingredients.getOutputs(VanillaTypes.ITEM);
ItemStack centreStack = inputs.get(inputs.size() - 2).get(0);
int bookSlots = 0;
if(centreStack.getItem() instanceof IWorkbenchItem){
bookSlots = ((IWorkbenchItem)centreStack.getItem()).getSpellSlotCount(centreStack);
}
// Slot initialisation
int i = 0;
while(i < bookSlots){
int x = CENTRE_SLOT_X + ContainerArcaneWorkbench.getBookSlotXOffset(i, bookSlots);
int y = CENTRE_SLOT_Y + ContainerArcaneWorkbench.getBookSlotYOffset(i, bookSlots);
slots.init(i++, true, x, y);
}
// Add dummy slots for the hidden book slots so the transfer handler works correctly
// Sure, we COULD use an IRecipeTransferHandler, but this is far less effort!
while(i < ContainerArcaneWorkbench.CRYSTAL_SLOT){
slots.init(i++, true, 0, 0);
}
slots.init(i++, true, CRYSTAL_SLOT_X, CRYSTAL_SLOT_Y);
slots.init(i++, true, CENTRE_SLOT_X, CENTRE_SLOT_Y);
slots.init(i++, true, UPGRADE_SLOT_X, UPGRADE_SLOT_Y);
slots.init(i++, false, OUTPUT_SLOT_X, OUTPUT_SLOT_Y);
// Assign ingredients to slots
// The number of books we actually have is inputs.size() - 3, probably less than the number of book slots
for(int j = 0; j < Math.min(bookSlots, inputs.size() - 3); j++){
slots.set(j, inputs.get(j));
}
slots.set(ContainerArcaneWorkbench.CRYSTAL_SLOT, inputs.get(inputs.size() - 3));
slots.set(ContainerArcaneWorkbench.CENTRE_SLOT, inputs.get(inputs.size() - 2));
slots.set(ContainerArcaneWorkbench.UPGRADE_SLOT, inputs.get(inputs.size() - 1));
for(int k = 0; k < outputs.size(); k++) slots.set(11 + k, outputs.get(k));
}
/** Called to generate all of wizardry's arcane workbench 'recipes' for JEI. */
public static Collection<ArcaneWorkbenchRecipe> generateRecipes(){
List<ArcaneWorkbenchRecipe> recipes = new ArrayList<>();
recipes.addAll(generateUpgradeRecipes()); // Probably nicest to have these first, they're the most useful
recipes.addAll(generateChargingRecipes());
recipes.addAll(generateScrollRecipes());
return recipes;
}
private static Collection<ArcaneWorkbenchRecipe> generateUpgradeRecipes(){
List<ArcaneWorkbenchRecipe> recipes = new ArrayList<>();
List<ItemStack> upgrades = new ArrayList<>();
for(Item item : Item.REGISTRY){
if(item instanceof ItemArcaneTome || item instanceof ItemArmourUpgrade){
NonNullList<ItemStack> variants = NonNullList.create();
item.getSubItems(item.getCreativeTab(), variants);
upgrades.addAll(variants);
}
}
// Condense all special upgrades into one ingredient in an effort to reduce the number of separate recipes
List<ItemStack> specialUpgrades = new ArrayList<>();
for(Item item : WandHelper.getSpecialUpgrades()){
NonNullList<ItemStack> variants = NonNullList.create();
item.getSubItems(item.getCreativeTab(), variants);
specialUpgrades.addAll(variants);
}
for(Item item : Item.REGISTRY){
if(item instanceof IWorkbenchItem){
ItemStack original = new ItemStack(item);
for(ItemStack upgrade : upgrades){
// Copy both input stacks to ignore any modifications to them during the upgrading process
ItemStack result = ((IWorkbenchItem)item).applyUpgrade(null, original.copy(), upgrade.copy());
// It's only a valid 'recipe' if something actually changed
if(!ItemStack.areItemStacksEqual(original, result)){
recipes.add(new ArcaneWorkbenchRecipe(original, Collections.emptyList(), Collections.emptyList(),
Collections.singletonList(upgrade), result));
}
}
List<ItemStack> applicableSpecialUpgrades = new ArrayList<>();
for(ItemStack upgrade : specialUpgrades){
// Copy both input stacks to ignore any modifications to them during the upgrading process
ItemStack result = ((IWorkbenchItem)item).applyUpgrade(null, original.copy(), upgrade.copy());
// It's only a valid 'recipe' if something actually changed
if(!ItemStack.areItemStacksEqual(original, result)){
applicableSpecialUpgrades.add(upgrade);
}
}
recipes.add(new ArcaneWorkbenchRecipe(original, Collections.emptyList(), Collections.emptyList(),
applicableSpecialUpgrades, original)); // Wands with special upgrades look no different anyway
}
}
return recipes;
}
private static Collection<ArcaneWorkbenchRecipe> generateChargingRecipes(){
List<ArcaneWorkbenchRecipe> recipes = new ArrayList<>();
List<ItemStack> crystals = new ArrayList<>();
for(int meta = 0; meta < Element.values().length; meta++) crystals.add(new ItemStack(WizardryItems.magic_crystal, 1, meta));
List<ItemStack> shard = Collections.singletonList(new ItemStack(WizardryItems.crystal_shard));
List<ItemStack> grandCrystal = Collections.singletonList(new ItemStack(WizardryItems.grand_crystal));
for(Item chargeable : WizardryRecipes.getChargeableItems()){
if(!(chargeable instanceof IManaStoringItem)) throw new IllegalArgumentException("Item to be charged must be an instance of IManaStoringItem");
ItemStack input = new ItemStack(chargeable);
((IManaStoringItem)chargeable).setMana(input, 0);
ItemStack result = new ItemStack(chargeable);
((IManaStoringItem)chargeable).setMana(result, Constants.MANA_PER_CRYSTAL);
recipes.add(new ArcaneWorkbenchRecipe(input, Collections.emptyList(), crystals, Collections.emptyList(), result));
result = new ItemStack(chargeable);
((IManaStoringItem)chargeable).setMana(result, Constants.MANA_PER_SHARD);
recipes.add(new ArcaneWorkbenchRecipe(input, Collections.emptyList(), shard, Collections.emptyList(), result));
result = new ItemStack(chargeable);
((IManaStoringItem)chargeable).setMana(result, Constants.GRAND_CRYSTAL_MANA);
recipes.add(new ArcaneWorkbenchRecipe(input, Collections.emptyList(), grandCrystal, Collections.emptyList(), result));
}
return recipes;
}
private static Collection<ArcaneWorkbenchRecipe> generateScrollRecipes(){
List<ArcaneWorkbenchRecipe> recipes = new ArrayList<>();
ItemStack blankScroll = new ItemStack(WizardryItems.blank_scroll);
// We need not make people register these manually since spells already have control over what they can be put on
List<Item> spellBooks = Streams.stream(Item.REGISTRY).filter(i -> i instanceof ItemSpellBook).collect(Collectors.toList());
List<Item> scrolls = Streams.stream(Item.REGISTRY).filter(i -> i instanceof ItemScroll).collect(Collectors.toList());
for(Spell spell : Spell.getAllSpells()){
for(Item spellBook : spellBooks){
for(Item scroll : scrolls){
if(spell.applicableForItem(spellBook) && spell.applicableForItem(scroll)){
List<ItemStack> books = Collections.singletonList(new ItemStack(WizardryItems.spell_book, 1, spell.metadata()));
ItemStack result = new ItemStack(WizardryItems.scroll, 1, spell.metadata());
recipes.add(new ArcaneWorkbenchRecipe(blankScroll, books, spell.getCost(), Collections.emptyList(), result));
}
}
}
}
return recipes;
}
}
@@ -0,0 +1,52 @@
package electroblob.wizardry.integration.jei;
import electroblob.wizardry.inventory.ContainerArcaneWorkbench;
import mezz.jei.api.recipe.transfer.IRecipeTransferInfo;
import net.minecraft.inventory.Slot;
import java.util.ArrayList;
import java.util.List;
/**
* JEI recipe transfer handler for the arcane workbench. This differs from a standard recipe handler in that it returns
* the active bookshelf (virtual) slots from the workbench as part of the inventory slots, and does not require complete
* sets of ingredients.
*
* @author Electroblob
* @since Wizardry 4.3
*/
public class ArcaneWorkbenchTransferHandler implements IRecipeTransferInfo<ContainerArcaneWorkbench> {
@Override
public Class<ContainerArcaneWorkbench> getContainerClass(){
return ContainerArcaneWorkbench.class;
}
@Override
public String getRecipeCategoryUid(){
return ArcaneWorkbenchRecipeCategory.UID;
}
@Override
public boolean canHandle(ContainerArcaneWorkbench container){
return true;
}
@Override
public boolean requireCompleteSets(){
return false; // Arcane workbench maths doesn't work like crafting maths!
}
@Override
public List<Slot> getRecipeSlots(ContainerArcaneWorkbench container){
return container.inventorySlots.subList(0, ContainerArcaneWorkbench.UPGRADE_SLOT + 1);
}
@Override
public List<Slot> getInventorySlots(ContainerArcaneWorkbench container){
List<Slot> slots = new ArrayList<>(container.inventorySlots.subList(ContainerArcaneWorkbench.UPGRADE_SLOT + 1, ContainerArcaneWorkbench.UPGRADE_SLOT + 37));
slots.addAll(container.getBookshelfSlots());
return slots;
}
}
@@ -0,0 +1,56 @@
package electroblob.wizardry.integration.jei;
import electroblob.wizardry.Wizardry;
import electroblob.wizardry.item.IConjuredItem;
import electroblob.wizardry.registry.WizardryBlocks;
import mezz.jei.api.IModPlugin;
import mezz.jei.api.IModRegistry;
import mezz.jei.api.ISubtypeRegistry;
import mezz.jei.api.JEIPlugin;
import mezz.jei.api.ingredients.IIngredientBlacklist;
import mezz.jei.api.recipe.IRecipeCategoryRegistration;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
@JEIPlugin
public class WizardryJEIPlugin implements IModPlugin {
@Override
public void registerCategories(IRecipeCategoryRegistration registry){
if(!Wizardry.settings.jeiIntegration) return;
registry.addRecipeCategories(new ArcaneWorkbenchRecipeCategory(registry));
}
@Override
public void register(IModRegistry registry){
if(!Wizardry.settings.jeiIntegration) return;
// Add arcane workbench as the item required to use arcane workbench recipes
registry.addRecipeCatalyst(new ItemStack(WizardryBlocks.arcane_workbench), ArcaneWorkbenchRecipeCategory.UID);
registry.addRecipes(ArcaneWorkbenchRecipeCategory.generateRecipes(), ArcaneWorkbenchRecipeCategory.UID);
registry.getRecipeTransferRegistry().addRecipeTransferHandler(new ArcaneWorkbenchTransferHandler());
// Hide conjured items from JEI
IIngredientBlacklist blacklist = registry.getJeiHelpers().getIngredientBlacklist();
for(Item item : Item.REGISTRY){
if(item instanceof IConjuredItem) blacklist.addIngredientToBlacklist(new ItemStack(item));
}
}
@Override
public void registerItemSubtypes(ISubtypeRegistry subtypeRegistry){
if(!Wizardry.settings.jeiIntegration) return;
// TODO: Probably need to distinguish between normal/legendary armour, at the very least
// ... that being said, we may well be changing that mechanic anyway
}
}