Skip to content

Commit

Permalink
Use new ForMatching methods (#1423)
Browse files Browse the repository at this point in the history
  • Loading branch information
democat3457 committed Jan 1, 2022
1 parent 6bd6f22 commit 15c7880
Show file tree
Hide file tree
Showing 16 changed files with 84 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,6 @@ public static boolean isSuppressingWarnings() {
*
* @param annotatedClass class that is annotated
*/
@SuppressWarnings("deprecation") // Java 11+ for RedHat
public static void registerClass(Class<?> annotatedClass) {
boolean registered = false;
for(Annotation annotation : annotatedClass.getAnnotations()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import crafttweaker.annotations.ZenRegister;
import stanhebben.zenscript.annotations.ZenClass;
import stanhebben.zenscript.annotations.ZenGetter;
import stanhebben.zenscript.annotations.ZenSetter;

/**
* @author Stan
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import crafttweaker.mc1120.entity.expand.ExpandEntityEquipmentSlot;
import crafttweaker.mc1120.game.MCTeam;
import crafttweaker.mc1120.item.MCItemStack;
import crafttweaker.mc1120.item.MCMutableItemStack;
import crafttweaker.mc1120.item.VanillaIngredient;
import crafttweaker.mc1120.liquid.MCLiquidDefinition;
import crafttweaker.mc1120.liquid.MCLiquidStack;
Expand Down Expand Up @@ -222,11 +223,61 @@ public static IItemStack getIItemStack(ItemStack item) {
* @return crafttweaker stack
*/
public static IItemStack getIItemStackWildcardSize(ItemStack item) {
if(item.isEmpty())
if(item == null || item.isEmpty())
return null;

return new MCItemStack(item, true);
}

/**
* Returns the CraftTweaker mutable item stack for this item.
*
* @param item minecraft item stack
*
* @return crafttweaker mutable item stack
*/
public static IItemStack getIItemStackMutable(ItemStack item) {
if (item == null || item.isEmpty())
return null;

return new MCMutableItemStack(item);
}

/**
* Constructs a mutable item stack with wildcard size.
*
* @param item minecraft item stack
*
* @return crafttweaker mutable item stack
*/
public static IItemStack getIItemStackMutableWildcardSize(ItemStack item) {
if (item == null || item.isEmpty())
return null;

return new MCMutableItemStack(item, true);
}

/**
* Constructs an item stack for matching. Less performance-heavy than normal getIItemStack.
*
* @param item minecraft item stack
*
* @return crafttweaker item stack for matching
*/
public static IItemStack getIItemStackForMatching(ItemStack item) {
return getIItemStackForMatching(item, false);
}

/**
* Constructs an item stack for matching with wildcard size. Less performance-heavy than normal getIItemStack.
*
* @param item minecraft item stack
*
* @return crafttweaker item stack for matching
*/
public static IItemStack getIItemStackForMatching(ItemStack item, boolean wildcardSize) {
return wildcardSize ? getIItemStackMutableWildcardSize(item) : getIItemStackMutable(item);
}

/**
* Constructs an item stack with wildcard size.
Expand All @@ -239,6 +290,7 @@ public static IItemStack getIItemStackWildcardSize(ItemStack item) {
public static IItemStack getIItemStackWildcardSize(Item item, int meta) {
if(item == null)
return null;

return new MCItemStack(new ItemStack(item, 1, meta), true);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@
import net.minecraft.item.crafting.FurnaceRecipes;

import java.util.*;
import java.util.stream.Collectors;

import static crafttweaker.api.minecraft.CraftTweakerMC.getIItemStack;
import static crafttweaker.api.minecraft.CraftTweakerMC.getIItemStackForMatching;

public class ActionFurnaceRemoveRecipe implements IActionFurnaceRemoval {

Expand All @@ -26,20 +25,20 @@ public void apply() {
Map<ItemStack, ItemStack> smeltingList = FurnaceRecipes.instance().getSmeltingList();
if(input == null) {
for(Map.Entry<ItemStack, ItemStack> entry : smeltingList.entrySet()) {
if(output.matches(getIItemStack(entry.getValue()))) {
if(output.matches(getIItemStackForMatching(entry.getValue()))) {
smeltingMap.put(entry.getKey(), entry.getValue());
}
}
} else {
for(Map.Entry<ItemStack, ItemStack> entry : smeltingList.entrySet()) {
if(output.matches(getIItemStack(entry.getValue())) && input.matches(getIItemStack(entry.getKey()))) {
if(output.matches(getIItemStackForMatching(entry.getValue())) && input.matches(getIItemStackForMatching(entry.getKey()))) {
smeltingMap.put(entry.getKey(), entry.getValue());
}
}
}
for(Map.Entry<ItemStack, ItemStack> entry : smeltingMap.entrySet()) {
FurnaceRecipes.instance().getSmeltingList().remove(entry.getKey(), entry.getValue());
FurnaceRecipes.instance().experienceList.keySet().removeIf(itemStack -> output.matches(getIItemStack(itemStack)));
FurnaceRecipes.instance().experienceList.keySet().removeIf(itemStack -> output.matches(getIItemStackForMatching(itemStack)));
}
CraftTweakerAPI.logInfo(smeltingMap.size() + " recipes removed for: " + output);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public void apply() {

for(Object entry : SEEDS) {
ItemStack itemStack = CraftTweakerHacks.getSeedEntrySeed(entry);
if(pattern.matches(CraftTweakerMC.getIItemStack(itemStack))) {
if(pattern.matches(CraftTweakerMC.getIItemStackForMatching(itemStack))) {
removed.add(entry);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ public ItemStack getOutput(ItemStack input, ItemStack ingredient) {

@Override
public boolean isIngredient(ItemStack tester) {
return ingredient.matches(CraftTweakerMC.getIItemStack(tester));
return ingredient.matches(CraftTweakerMC.getIItemStackForMatching(tester));
}

@Override
public boolean isInput(ItemStack tester) {
return input.matches(CraftTweakerMC.getIItemStack(tester));
return input.matches(CraftTweakerMC.getIItemStackForMatching(tester));
}

public ItemStack getOutput() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ public VanillaBrewingPlus(List<Tuple<IIngredient, IIngredient>> removedRecipes)

@Override
public ItemStack getOutput(ItemStack input, ItemStack ingredient) {
IItemStack _input = CraftTweakerMC.getIItemStack(input);
IItemStack _ingredient = CraftTweakerMC.getIItemStack(ingredient);
IItemStack _input = CraftTweakerMC.getIItemStackForMatching(input);
IItemStack _ingredient = CraftTweakerMC.getIItemStackForMatching(ingredient);

if (removedRecipes.stream().anyMatch(t -> t.getFirst().matches(_input) && t.getSecond().matches(_ingredient))) {
return ItemStack.EMPTY;
Expand All @@ -33,7 +33,7 @@ public ItemStack getOutput(ItemStack input, ItemStack ingredient) {

@Override
public boolean isIngredient(@Nonnull ItemStack stack) {
IItemStack _ingredient = CraftTweakerMC.getIItemStack(stack);
IItemStack _ingredient = CraftTweakerMC.getIItemStackForMatching(stack);

return super.isIngredient(stack) && removedRecipes.stream().noneMatch(t -> t.getFirst() == IngredientAny.INSTANCE && t.getSecond().matches(_ingredient));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ public void onOreDictEvent(OreDictionary.OreRegisterEvent ev) {
List<IItemStack> ingredients = MCOreDictEntry.REMOVED_CONTENTS.get(ev.getName());
if (ingredients != null)
for (IItemStack ingredient : ingredients)
if (ingredient.matches(CraftTweakerMC.getIItemStack(ev.getOre())))
if (ingredient.matches(CraftTweakerMC.getIItemStackForMatching(ev.getOre())))
OreDictionary.getOres(ev.getName()).remove(ev.getOre());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import crafttweaker.api.event.PlayerWakeUpEvent;
import crafttweaker.api.minecraft.CraftTweakerMC;
import crafttweaker.api.player.IPlayer;
import crafttweaker.api.world.IBlockPos;
import net.minecraft.entity.player.EntityPlayer;

public class MCPlayerWakeUpEvent implements PlayerWakeUpEvent {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ protected MCItemStack(ItemStack itemStack, IData tag) {
this.tag = tag;
}

private MCItemStack(ItemStack itemStack, IData tag, boolean wildcardSize) {
protected MCItemStack(ItemStack itemStack, IData tag, boolean wildcardSize) {
if(itemStack.isEmpty())
throw new IllegalArgumentException("stack cannot be null");
stack = itemStack;
Expand All @@ -111,7 +111,7 @@ private MCItemStack(ItemStack itemStack, IData tag, boolean wildcardSize) {

/**
* This is a constructor which creates a itemstack that doesn't copy the internal stack
* Only use this is you are sure it will never be changed
* Only use this if you are sure it will never be changed
*
* @param unused to prevent constructor conflicts, has no purpose
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public MCMutableItemStack(ItemStack itemStack) {
super(itemStack, NBTConverter.from(itemStack.getTagCompound(), false));
}

public MCMutableItemStack(ItemStack itemStack, boolean wildcardSize) {
super(itemStack, NBTConverter.from(itemStack.getTagCompound(), false), wildcardSize);
}

@Override
public void shrink(int quality) {
origin.shrink(quality);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public ItemStack[] getMatchingStacks() {

@Override
public boolean apply(@Nullable ItemStack itemStack) {
return ingredient.matches(CraftTweakerMC.getIItemStack(itemStack));
return ingredient.matches(CraftTweakerMC.getIItemStackForMatching(itemStack));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public void remove(IItemStack... items) {
REMOVED_CONTENTS.get(id).add(item);
ItemStack result = ItemStack.EMPTY;
for(ItemStack itemStack : OreDictionary.getOres(id)) {
if(item.matches(getIItemStackWildcardSize(itemStack))) {
if(item.matches(getIItemStackForMatching(itemStack, true))) {
result = itemStack;
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import crafttweaker.api.item.IIngredient;
import crafttweaker.api.item.IItemStack;
import crafttweaker.api.item.IngredientOr;
import crafttweaker.api.minecraft.CraftTweakerMC;
import crafttweaker.api.recipes.*;
import crafttweaker.mc1120.CraftTweaker;
import crafttweaker.mc1120.item.MCItemStack;
Expand All @@ -31,6 +30,7 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static crafttweaker.api.minecraft.CraftTweakerMC.getIItemStackForMatching;
import static crafttweaker.api.minecraft.CraftTweakerMC.getIItemStack;
import static crafttweaker.api.minecraft.CraftTweakerMC.getItemStack;
import static crafttweaker.api.minecraft.CraftTweakerMC.getOreDict;
Expand All @@ -54,7 +54,8 @@ public MCRecipeManager() {
}

private static boolean matchesItem(ItemStack input, IIngredient ingredient) {
return ingredient == null ? input.isEmpty() : !input.isEmpty() && ingredient.matches(getIItemStack(input));
return ingredient == null ? input.isEmpty() :
!input.isEmpty() && ingredient.matches(getIItemStackForMatching(input));
}

private static boolean matches(Object input, IIngredient ingredient) {
Expand Down Expand Up @@ -124,7 +125,7 @@ public List<ICraftingRecipe> getRecipesFor(IIngredient ingredient) {
for(Map.Entry<ResourceLocation, IRecipe> ent : recipes) {
ItemStack stack = ent.getValue().getRecipeOutput();
if(!stack.isEmpty()) {
if(ingredient.matches(CraftTweakerMC.getIItemStack(stack))) {
if(ingredient.matches(getIItemStackForMatching(stack))) {
if(ent.getValue() instanceof MCRecipeBase) {
results.add((MCRecipeBase) ent.getValue());
} else
Expand Down Expand Up @@ -341,7 +342,7 @@ public void apply() {
for(Map.Entry<ResourceLocation, IRecipe> recipeEntry : recipes) {
final IRecipe recipe = recipeEntry.getValue();
final ItemStack output = recipe.getRecipeOutput();
if(output.isEmpty() || !this.output.matches(MCItemStack.createNonCopy(output))) {
if(output.isEmpty() || !this.output.matches(getIItemStackForMatching(output))) {
continue;
}

Expand All @@ -367,7 +368,7 @@ public void apply() {
} else {
input = ing.getMatchingStacks()[0];
}
if(!matches(input, ingredient)) {
if(!matchesItem(input, ingredient)) {
continue outer;
}
}
Expand Down Expand Up @@ -414,7 +415,7 @@ public void apply() {
outer:
for(Map.Entry<ResourceLocation, IRecipe> entry : recipes) {
IRecipe recipe = entry.getValue();
if(entry.getValue().getRecipeOutput().isEmpty() || !output.matches(MCItemStack.createNonCopy(entry.getValue().getRecipeOutput()))) {
if(entry.getValue().getRecipeOutput().isEmpty() || !output.matches(getIItemStackForMatching(entry.getValue().getRecipeOutput()))) {
continue;
}
if(recipe instanceof IShapedRecipe) {
Expand Down Expand Up @@ -499,7 +500,7 @@ public void apply() {

for(Map.Entry<ResourceLocation, IRecipe> recipe : recipes) {
ItemStack recipeOutput = recipe.getValue().getRecipeOutput();
IItemStack stack = getIItemStack(recipeOutput);
IItemStack stack = getIItemStackForMatching(recipeOutput);
if(stack != null && matches(stack)) {
toRemove.add(recipe.getKey());
}
Expand Down Expand Up @@ -543,7 +544,7 @@ public void apply() {
for(Map.Entry<ResourceLocation, IRecipe> recipe : recipes) {
if(recipe.getKey().toString().equals(recipeName)) {
if(filter != null) {
if(filter.matches(getIItemStack(recipe.getValue().getRecipeOutput())))
if(filter.matches(getIItemStackForMatching(recipe.getValue().getRecipeOutput())))
toRemove.add(recipe.getKey());
} else {
toRemove.add(recipe.getKey());
Expand Down Expand Up @@ -613,7 +614,7 @@ public void apply() {
Matcher m = p.matcher(resourceLocation.toString());
if(m.matches()) {
if(filter != null) {
if(filter.matches(getIItemStack(recipe.getValue().getRecipeOutput())))
if(filter.matches(getIItemStackForMatching(recipe.getValue().getRecipeOutput())))
toRemove.add(recipe.getKey());
} else {
toRemove.add(resourceLocation);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ private Pair<Integer, Integer> checkRecipe(IIngredient[][] ingredients, Inventor
continue;
else
return offsetInvalid;
if(itemStack.isEmpty() || !ingredients[row][column].matches(CraftTweakerMC.getIItemStack(itemStack)))
if(itemStack.isEmpty() || !ingredients[row][column].matches(CraftTweakerMC.getIItemStackForMatching(itemStack)))
continue outer;
visited[(column + columnOffset) + (row + rowOffset) * inv.getWidth()] = true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public boolean matches(InventoryCrafting inv, World worldIn) {
ItemStack stackInSlot = inv.getStackInSlot(slot);
if(visited[slot] || stackInSlot.isEmpty())
continue;
if(ingredient.matches(CraftTweakerMC.getIItemStack(stackInSlot))) {
if(ingredient.matches(CraftTweakerMC.getIItemStackForMatching(stackInSlot))) {
//make sure no slot is matched twice
visited[slot] = true;
continue outer;
Expand Down

0 comments on commit 15c7880

Please sign in to comment.