Skip to content

Commit

Permalink
cleanup ItemStackMixin & add transformer helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
brachy84 committed Jul 6, 2024
1 parent 11ddbdd commit 6d63e46
Show file tree
Hide file tree
Showing 7 changed files with 286 additions and 180 deletions.
4 changes: 2 additions & 2 deletions examples/postInit/vanilla.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ crafting.shapedBuilder()
crafting.shapedBuilder()
.name('gold_v_to_clay')
.output(item('minecraft:clay'))
.shape([[item('minecraft:gold_ingot'),null,item('minecraft:gold_ingot')],[null,item('minecraft:gold_ingot'),null]])
.shape([[item('minecraft:gold_ingot'),null,item('minecraft:gold_ingot')],[null,item('minecraft:stone_pickaxe').transformDamage(2).whenAnyDamage(),null]])
.register()

//crafting.addShaped(resource('example:resource_location'), item('minecraft:clay'), [[item('minecraft:cobblestone')],[item('minecraft:nether_star')],[item('minecraft:cobblestone')]])
Expand Down Expand Up @@ -131,7 +131,7 @@ def presetKeys = [
T: item('minecraft:tnt'),
D: item('minecraft:diamond'),
S: ore('netherStar').reuse(),
'!': item('minecraft:tnt').transform({ _ -> item('minecraft:diamond') }),
'!': item('minecraft:tnt').transform(item('minecraft:diamond')),
G: ore('ingotGold'),
W: fluid('water') * 1000, // Any tank that contains >= 1000 mb and can be reduced by 1000.
'0': item('minecraft:diamond_sword').withNbt([display:[Name:'Sword with Specific NBT data']])
Expand Down
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));
}
}
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);
}
}
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);
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static void initializeBinding() {
GroovyScript.getSandbox().registerBinding(rarity);
GroovyScript.getSandbox().registerBinding(inWorldCrafting);

ExpansionHelper.mixinClass(ItemStack.class, RarityItemStackExpansion.class);
ExpansionHelper.mixinClass(ItemStack.class, ItemStackExpansion.class);
}

@Override
Expand Down
Loading

0 comments on commit 6d63e46

Please sign in to comment.