generated from CleanroomMC/TemplateDevEnv
-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cleanup ItemStackMixin & add transformer helpers
- Loading branch information
Showing
7 changed files
with
286 additions
and
180 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
34 changes: 34 additions & 0 deletions
34
src/main/java/com/cleanroommc/groovyscript/compat/vanilla/ItemStackExpansion.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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.cleanroommc.groovyscript.compat.vanilla; | ||
|
||
import com.cleanroommc.groovyscript.api.IIngredient; | ||
import com.cleanroommc.groovyscript.helper.ingredient.OreDictIngredient; | ||
import net.minecraft.item.ItemStack; | ||
import net.minecraft.util.text.TextFormatting; | ||
|
||
public class ItemStackExpansion { | ||
|
||
public static ItemStack setRarity(ItemStack self, TextFormatting color) { | ||
VanillaModule.rarity.set(color, self); | ||
return self; | ||
} | ||
|
||
public static void addOreDict(ItemStack self, OreDictIngredient ingredient) { | ||
VanillaModule.oreDict.add(ingredient.getOreDict(), self); | ||
} | ||
|
||
public static void removeOreDict(ItemStack self, OreDictIngredient ingredient) { | ||
VanillaModule.oreDict.remove(ingredient.getOreDict(), self); | ||
} | ||
|
||
public static boolean leftShift(ItemStack self, IIngredient ingredient) { | ||
return ingredient.isCase(self) && ingredient.getAmount() >= self.getCount(); | ||
} | ||
|
||
public static boolean isSameExact(ItemStack self, ItemStack itemStack) { | ||
return ItemStack.areItemStacksEqual(self, itemStack); | ||
} | ||
|
||
public static boolean isSame(ItemStack self, ItemStack itemStack, boolean ignoreNbt) { | ||
return ItemStack.areItemsEqual(self, itemStack) && (ignoreNbt || ItemStack.areItemStackTagsEqual(self, itemStack)); | ||
} | ||
} |
212 changes: 212 additions & 0 deletions
212
src/main/java/com/cleanroommc/groovyscript/compat/vanilla/ItemStackMixinExpansion.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 |
---|---|---|
@@ -0,0 +1,212 @@ | ||
package com.cleanroommc.groovyscript.compat.vanilla; | ||
|
||
import com.cleanroommc.groovyscript.api.IIngredient; | ||
import com.cleanroommc.groovyscript.api.IMarkable; | ||
import com.cleanroommc.groovyscript.api.INBTResourceStack; | ||
import com.cleanroommc.groovyscript.api.INbtIngredient; | ||
import com.cleanroommc.groovyscript.helper.ingredient.IngredientHelper; | ||
import com.cleanroommc.groovyscript.helper.ingredient.NbtHelper; | ||
import net.minecraft.init.Items; | ||
import net.minecraft.item.ItemStack; | ||
import net.minecraft.item.crafting.Ingredient; | ||
import net.minecraft.nbt.NBTTagCompound; | ||
import net.minecraftforge.common.ForgeHooks; | ||
import net.minecraftforge.oredict.OreDictionary; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.function.Predicate; | ||
import java.util.function.UnaryOperator; | ||
|
||
public interface ItemStackMixinExpansion extends IIngredient, INbtIngredient, IMarkable { | ||
|
||
static ItemStackMixinExpansion of(ItemStack stack) { | ||
return (ItemStackMixinExpansion) (Object) stack; | ||
} | ||
|
||
ItemStack grs$getItemStack(); | ||
|
||
@Nullable | ||
ItemStackTransformer grs$getTransformer(); | ||
|
||
@Nullable | ||
Predicate<NBTTagCompound> grs$getNbtMatcher(); | ||
|
||
@Nullable | ||
Predicate<ItemStack> grs$getMatcher(); | ||
|
||
void grs$setTransformer(ItemStackTransformer transformer); | ||
|
||
void grs$setNbtMatcher(Predicate<NBTTagCompound> matcher); | ||
|
||
void grs$setMatcher(Predicate<ItemStack> matcher); | ||
|
||
default boolean grs$isEmpty() { | ||
return grs$getItemStack().isEmpty(); | ||
} | ||
|
||
@Override | ||
default ItemStackMixinExpansion exactCopy() { | ||
if (grs$isEmpty()) { | ||
return (ItemStackMixinExpansion) (Object) ItemStack.EMPTY; | ||
} | ||
ItemStackMixinExpansion copy = of(grs$getItemStack().copy()); | ||
copy.setMark(getMark()); | ||
copy.grs$setTransformer(grs$getTransformer()); | ||
copy.grs$setMatcher(grs$getMatcher()); | ||
copy.grs$setNbtMatcher(grs$getNbtMatcher()); | ||
return copy; | ||
} | ||
|
||
@Override | ||
default boolean test(ItemStack stack) { | ||
if (!OreDictionary.itemMatches(grs$getItemStack(), stack, false) && | ||
(grs$getMatcher() == null || !grs$getMatcher().test(stack))) { | ||
return false; | ||
} | ||
if (grs$getNbtMatcher() != null) { | ||
NBTTagCompound nbt = stack.getTagCompound(); | ||
return nbt != null && grs$getNbtMatcher().test(nbt); | ||
} | ||
return true; | ||
} | ||
|
||
default ItemStack when(Predicate<ItemStack> matchCondition) { | ||
ItemStackMixinExpansion fresh = exactCopy(); | ||
fresh.grs$setMatcher(matchCondition); | ||
return fresh.grs$getItemStack(); | ||
} | ||
|
||
default ItemStack whenAnyDamage() { | ||
return when(stack -> stack.getItem() == grs$getItemStack().getItem()); | ||
} | ||
|
||
default ItemStack whenAnyMeta() { | ||
return whenAnyDamage(); | ||
} | ||
|
||
default ItemStack transform(ItemStackTransformer transformer) { | ||
ItemStackMixinExpansion fresh = exactCopy(); | ||
fresh.grs$setTransformer(transformer); | ||
return fresh.grs$getItemStack(); | ||
} | ||
|
||
default ItemStack reuse() { | ||
return transform(self -> self); | ||
} | ||
|
||
default ItemStack noreturn() { | ||
return transform(self -> ItemStack.EMPTY); | ||
} | ||
|
||
default ItemStack transform(ItemStack stack) { | ||
return transform(self -> stack); | ||
} | ||
|
||
default ItemStack transformDamage(int amount) { | ||
return transform(self -> of(self).withDamage(Math.min(32767, Items.DIAMOND.getDamage(self) + amount))); | ||
} | ||
|
||
default ItemStack transformDamage() { | ||
return transformDamage(1); | ||
} | ||
|
||
default ItemStack transformNbt(UnaryOperator<NBTTagCompound> transformer) { | ||
return transform(self -> { | ||
of(self).exactCopy().grs$getItemStack().setTagCompound(transformer.apply(self.getTagCompound())); | ||
return self; | ||
}); | ||
} | ||
|
||
@Override | ||
default ItemStack applyTransform(ItemStack matchedInput) { | ||
if (grs$getTransformer() != null) { | ||
ItemStack result = grs$getTransformer().transform(matchedInput); | ||
if (result == null) return ItemStack.EMPTY; | ||
return result.copy(); | ||
} | ||
return ForgeHooks.getContainerItem(matchedInput); | ||
} | ||
|
||
@Override | ||
default Ingredient toMcIngredient() { | ||
return Ingredient.fromStacks(grs$getItemStack()); | ||
} | ||
|
||
@Override | ||
default ItemStack[] getMatchingStacks() { | ||
return new ItemStack[]{IngredientHelper.toItemStack(exactCopy())}; | ||
} | ||
|
||
@Override | ||
default int getAmount() { | ||
return grs$getItemStack().getCount(); | ||
} | ||
|
||
@Override | ||
default void setAmount(int amount) { | ||
grs$getItemStack().setCount(amount); | ||
} | ||
|
||
|
||
@Override | ||
default @Nullable NBTTagCompound getNbt() { | ||
return grs$getItemStack().getTagCompound(); | ||
} | ||
|
||
@Override | ||
default void setNbt(NBTTagCompound nbt) { | ||
grs$getItemStack().setTagCompound(nbt); | ||
} | ||
|
||
@Override | ||
default INBTResourceStack withNbt(NBTTagCompound nbt) { | ||
ItemStackMixinExpansion itemStackMixin = (ItemStackMixinExpansion) INbtIngredient.super.withNbt(nbt); | ||
itemStackMixin.grs$setNbtMatcher(nbt1 -> nbt.isEmpty() || NbtHelper.containsNbt(nbt1, nbt)); | ||
return itemStackMixin; | ||
} | ||
|
||
@Override | ||
default INbtIngredient withNbtExact(NBTTagCompound nbt) { | ||
ItemStackMixinExpansion itemStackMixin = (ItemStackMixinExpansion) INbtIngredient.super.withNbt(nbt); | ||
if (nbt == null) { | ||
itemStackMixin.grs$setNbtMatcher(null); | ||
} else { | ||
itemStackMixin.grs$setNbtMatcher(nbt1 -> nbt.isEmpty() || nbt1.equals(nbt)); | ||
} | ||
return itemStackMixin; | ||
} | ||
|
||
default INbtIngredient withNbtFilter(Predicate<NBTTagCompound> nbtFilter) { | ||
this.grs$setNbtMatcher(nbtFilter == null ? nbt -> true : nbtFilter); | ||
return this; | ||
} | ||
|
||
default INbtIngredient whenNoNbt() { | ||
setNbt(null); | ||
grs$setMatcher(self -> { | ||
NBTTagCompound nbt = self.getTagCompound(); | ||
return nbt == null || nbt.isEmpty(); | ||
}); | ||
return this; | ||
} | ||
|
||
default INbtIngredient whenAnyNbt() { | ||
setNbt(new NBTTagCompound()); | ||
grs$setMatcher(self -> { | ||
NBTTagCompound nbt = self.getTagCompound(); | ||
return nbt != null && !nbt.isEmpty(); | ||
}); | ||
return this; | ||
} | ||
|
||
default ItemStack withMeta(int meta) { | ||
ItemStack itemStack = exactCopy().grs$getItemStack(); | ||
// reliably set itemDamage field of item stack | ||
Items.DIAMOND.setDamage(itemStack, meta); | ||
return itemStack; | ||
} | ||
|
||
default ItemStack withDamage(int meta) { | ||
return withMeta(meta); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/com/cleanroommc/groovyscript/compat/vanilla/ItemStackTransformer.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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package com.cleanroommc.groovyscript.compat.vanilla; | ||
|
||
import net.minecraft.item.ItemStack; | ||
|
||
public interface ItemStackTransformer { | ||
|
||
ItemStack transform(ItemStack self); | ||
} |
12 changes: 0 additions & 12 deletions
12
src/main/java/com/cleanroommc/groovyscript/compat/vanilla/RarityItemStackExpansion.java
This file was deleted.
Oops, something went wrong.
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
Oops, something went wrong.