Skip to content

Commit

Permalink
ingredient type stuff 3
Browse files Browse the repository at this point in the history
  • Loading branch information
MehVahdJukaar committed Dec 2, 2024
1 parent d8268db commit 275b0ff
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,26 @@

import dev.architectury.injectables.annotations.ExpectPlatform;
import net.mehvahdjukaar.moonlight.api.set.BlockType;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient;
import org.jetbrains.annotations.ApiStatus;

public class BlockTypeSwapIngredient {
import java.util.List;

public interface BlockTypeSwapIngredient {

@ExpectPlatform
public static <T extends BlockType> Ingredient create(Ingredient original, T from, T to) {
static <T extends BlockType> Ingredient create(Ingredient original, T from, T to) {
throw new AssertionError();
}

@ApiStatus.Internal
@ExpectPlatform
public static void init() {
static void init() {
throw new AssertionError();
}

Ingredient getInner();

List<ItemStack> convertItems(List<ItemStack> items);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.gson.JsonObject;
import net.fabricmc.fabric.api.recipe.v1.ingredient.CustomIngredient;
import net.fabricmc.fabric.api.recipe.v1.ingredient.CustomIngredientSerializer;
import net.mehvahdjukaar.moonlight.api.resources.recipe.BlockTypeSwapIngredient;
import net.mehvahdjukaar.moonlight.api.set.BlockType;
import net.mehvahdjukaar.moonlight.api.set.BlockTypeRegistry;
import net.mehvahdjukaar.moonlight.core.Moonlight;
Expand All @@ -16,7 +17,7 @@
import java.util.Arrays;
import java.util.List;

public class BlockTypeSwapIngredientImpl<T extends BlockType> implements CustomIngredient {
public class BlockTypeSwapIngredientImpl<T extends BlockType> implements CustomIngredient, BlockTypeSwapIngredient {

private static final ResourceLocation ID = Moonlight.res("block_type_swap");

Expand All @@ -36,6 +37,11 @@ public BlockTypeSwapIngredientImpl(Ingredient inner, T fromType, T toType, Block
}


@Override
public Ingredient getInner() {
return inner;
}

@Override
public boolean test(ItemStack stack) {
if (stack != null) {
Expand All @@ -48,28 +54,33 @@ public boolean test(ItemStack stack) {
return false;
}

@Override
public List<ItemStack> convertItems(List<ItemStack> toConvert) {
List<ItemStack> newItems = new ArrayList<>();
boolean success = false;
for (ItemStack it : toConvert) {
var type = this.registry.getBlockTypeOf(it.getItem());
if (type != this.fromType) {
break;
} else {
var newItem = BlockType.changeItemType(it.getItem(), this.fromType, this.toType);
if (newItem != null) {
newItems.add(new ItemStack(newItem));
success = true;
}
}
}
if (!success) {
newItems.addAll(toConvert);
}
return newItems;
}

@Override
public List<ItemStack> getMatchingStacks() {
if (this.items == null) {
var original = this.inner.getItems();
List<ItemStack> newItems = new ArrayList<>();
boolean success = false;
for (ItemStack it : this.items) {
var type = this.registry.getBlockTypeOf(it.getItem());
if (type != this.fromType) {
break;
} else {
var newItem = BlockType.changeItemType(it.getItem(), this.fromType, this.toType);
if (newItem != null) {
newItems.add(new ItemStack(newItem));
success = true;
}
}
}
if (!success) {
newItems.addAll(Arrays.stream(original).toList());
}
this.items = newItems;
this.items = convertItems(Arrays.asList(original));
}
return this.items;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import net.mehvahdjukaar.moonlight.api.resources.recipe.BlockTypeSwapIngredient;
import net.mehvahdjukaar.moonlight.api.set.BlockType;
import net.mehvahdjukaar.moonlight.api.set.BlockTypeRegistry;
import net.mehvahdjukaar.moonlight.core.Moonlight;
Expand All @@ -17,7 +18,7 @@
import java.util.Arrays;
import java.util.List;

public class BlockTypeSwapIngredientImpl<T extends BlockType> extends AbstractIngredient {
public class BlockTypeSwapIngredientImpl<T extends BlockType> extends AbstractIngredient implements BlockTypeSwapIngredient {

private final Ingredient inner;
private final T fromType;
Expand All @@ -34,33 +35,43 @@ public BlockTypeSwapIngredientImpl(Ingredient inner, T fromType, T toType, Block
this.registry = registry;
}

@Override
public Ingredient getInner() {
return inner;
}

@Override
public boolean isSimple() {
return false;
}

@Override
public List<ItemStack> convertItems(List<ItemStack> toConvert) {
List<ItemStack> newItems = new ArrayList<>();
boolean success = false;
for (ItemStack it : toConvert) {
var type = this.registry.getBlockTypeOf(it.getItem());
if (type != this.fromType) {
break;
} else {
var newItem = BlockType.changeItemType(it.getItem(), this.fromType, this.toType);
if (newItem != null) {
newItems.add(new ItemStack(newItem));
success = true;
}
}
}
if (!success) {
newItems.addAll(toConvert);
}
return newItems;
}

@Override
public ItemStack[] getItems() {
if (this.items == null) {
var original = this.inner.getItems();
List<ItemStack> newItems = new ArrayList<>();
boolean success = false;
for (ItemStack it : this.items) {
var type = this.registry.getBlockTypeOf(it.getItem());
if (type != this.fromType) {
break;
} else {
var newItem = BlockType.changeItemType(it.getItem(), this.fromType, this.toType);
if (newItem != null) {
newItems.add(new ItemStack(newItem));
success = true;
}
}
}
if (!success) {
newItems.addAll(Arrays.stream(original).toList());
}
this.items = newItems.toArray(new ItemStack[0]);
this.items = convertItems(Arrays.asList(original)).toArray(new ItemStack[0]);
}
return this.items;
}
Expand Down

0 comments on commit 275b0ff

Please sign in to comment.