Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* JEI.addDescription accepts IIngredient and bug fix * import * tab -> 4 spaces
- Loading branch information
1 parent
9ea8d01
commit 94b4d8c
Showing
2 changed files
with
77 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 49 additions & 50 deletions
99
CraftTweaker2-MC1120-Mod-JEI/src/main/java/crafttweaker/mods/jei/actions/DescribeAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,60 @@ | ||
package crafttweaker.mods.jei.actions; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import crafttweaker.CraftTweakerAPI; | ||
import crafttweaker.IAction; | ||
import crafttweaker.api.item.IIngredient; | ||
import crafttweaker.api.item.IItemStack; | ||
import crafttweaker.api.liquid.ILiquidStack; | ||
import crafttweaker.api.minecraft.CraftTweakerMC; | ||
import crafttweaker.mods.jei.JEIAddonPlugin; | ||
import mezz.jei.api.ingredients.VanillaTypes; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
public class DescribeAction implements IAction { | ||
private final List<?> stack; | ||
private final String[] description; | ||
private final String name; | ||
|
||
@Deprecated | ||
public DescribeAction(IItemStack stack, String[] description) { | ||
this(Collections.singletonList(stack), description, stack.toString()); | ||
} | ||
/** | ||
* Used to add a JEI Description page to the given items/fluids/... | ||
* Objects need to implement {@code getInternal()} and the result of this method needs to be | ||
* <ol type = "A"> | ||
* <li> of a Minecraft ingredient type (e.g. ItemStack, FluidStack,...)</li> | ||
* <li> all items in the list need to be of the same class</li> | ||
* </ol> | ||
* @param stack The objects to be given descriptions | ||
* @param description The Description: one or more lines per item, auto wrap if lines are too long | ||
* @param name Solely for the User output, should be a representation of stack | ||
*/ | ||
public DescribeAction(List<? extends IIngredient> stack, String[] description, String name) { | ||
this.stack = stack.stream().map(IIngredient::getInternal).collect(Collectors.toList()); | ||
this.description = description; | ||
this.name = name; | ||
} | ||
private final List<IItemStack> itemStacks = new ArrayList<>(); | ||
private final List<ILiquidStack> fluidStacks = new ArrayList<>(); | ||
private final String[] description; | ||
private final String name; | ||
|
||
@Deprecated | ||
public DescribeAction(IItemStack stack, String[] description) { | ||
this(Collections.singletonList(stack), description, stack.toString()); | ||
} | ||
/** | ||
* Used to add a JEI Description page to the given ingredients | ||
* @param ingredients The ingredients to be given descriptions | ||
* @param description The Description: one or more lines per item, auto wrap if lines are too long | ||
* @param name Solely for the User output, should be a representation of stack | ||
*/ | ||
public DescribeAction(List<? extends IIngredient> ingredients, String[] description, String name) { | ||
ingredients.forEach(it -> { | ||
List<ILiquidStack> liquids = it.getLiquids(); | ||
if (liquids.isEmpty()) { | ||
itemStacks.addAll(it.getItems()); | ||
} else { | ||
fluidStacks.addAll(liquids); | ||
} | ||
}); | ||
this.description = description; | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
public void apply() { | ||
if (!itemStacks.isEmpty()) { | ||
JEIAddonPlugin.modRegistry.addIngredientInfo(itemStacks.stream().map(CraftTweakerMC::getItemStack).collect(Collectors.toList()), VanillaTypes.ITEM, description); | ||
} | ||
|
||
@Override | ||
public void apply() { | ||
if(stack.isEmpty()) { | ||
CraftTweakerAPI.logError(name + " is empty!"); | ||
return; | ||
} else if (!checkClasses()) { | ||
CraftTweakerAPI.logError(name + " needs to consist only of items of the same type!"); | ||
return; | ||
} | ||
|
||
JEIAddonPlugin.modRegistry.addIngredientInfo(stack, JEIAddonPlugin.itemRegistry.getIngredientType(stack.get(0).getClass()), description); | ||
} | ||
if (!fluidStacks.isEmpty()) { | ||
JEIAddonPlugin.modRegistry.addIngredientInfo(fluidStacks.stream().map(CraftTweakerMC::getLiquidStack).collect(Collectors.toList()), VanillaTypes.FLUID, description); | ||
} | ||
} | ||
|
||
@Override | ||
public String describe() { | ||
return "Adding description in JEI for: " + name; | ||
} | ||
|
||
public boolean checkClasses() { | ||
Class<?> clazz = stack.get(0).getClass(); | ||
return stack.stream().map(Object::getClass).allMatch(i -> i == clazz); | ||
} | ||
@Override | ||
public String describe() { | ||
return "Adding description in JEI for: " + name; | ||
} | ||
} |