107 lines
3.6 KiB
Java
107 lines
3.6 KiB
Java
package com.thebrokenrail.reliccraft.recipe;
|
|
|
|
import com.thebrokenrail.reliccraft.RelicCraft;
|
|
import com.thebrokenrail.reliccraft.item.RelicItem;
|
|
import net.fabricmc.api.EnvType;
|
|
import net.fabricmc.api.Environment;
|
|
import net.minecraft.inventory.CraftingInventory;
|
|
import net.minecraft.item.Item;
|
|
import net.minecraft.item.ItemStack;
|
|
import net.minecraft.item.Items;
|
|
import net.minecraft.nbt.CompoundTag;
|
|
import net.minecraft.recipe.RecipeSerializer;
|
|
import net.minecraft.recipe.SpecialCraftingRecipe;
|
|
import net.minecraft.util.Identifier;
|
|
import net.minecraft.util.collection.DefaultedList;
|
|
import net.minecraft.world.World;
|
|
|
|
import java.util.Random;
|
|
|
|
public class RevealRelicRecipe extends SpecialCraftingRecipe {
|
|
public RevealRelicRecipe(Identifier id) {
|
|
super(id);
|
|
}
|
|
|
|
@Override
|
|
public boolean matches(CraftingInventory craftingInventory, World world) {
|
|
ItemStack itemStack = ItemStack.EMPTY;
|
|
int netherStar = 0;
|
|
|
|
for (int i = 0; i < craftingInventory.size(); ++i) {
|
|
ItemStack itemStack2 = craftingInventory.getStack(i);
|
|
if (!itemStack2.isEmpty()) {
|
|
if (itemStack2.getItem() instanceof RelicItem) {
|
|
if (!itemStack.isEmpty()) {
|
|
return false;
|
|
}
|
|
|
|
itemStack = itemStack2;
|
|
} else if (itemStack2.getItem() == Items.NETHER_STAR) {
|
|
netherStar++;
|
|
} else {
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
|
|
return !itemStack.isEmpty() && netherStar == 1;
|
|
}
|
|
|
|
@Override
|
|
public ItemStack craft(CraftingInventory craftingInventory) {
|
|
ItemStack itemStack = ItemStack.EMPTY;
|
|
|
|
for (int i = 0; i < craftingInventory.size(); i++) {
|
|
ItemStack itemStack2 = craftingInventory.getStack(i);
|
|
if (!itemStack2.isEmpty()) {
|
|
Item item = itemStack2.getItem();
|
|
if (item instanceof RelicItem) {
|
|
if (!itemStack.isEmpty() || (itemStack2.getTag() != null && itemStack2.getTag().getByte(RelicItem.RELIC_REVEALED_KEY) != 0)) {
|
|
return ItemStack.EMPTY;
|
|
}
|
|
|
|
itemStack = itemStack2.copy();
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!itemStack.isEmpty()) {
|
|
CompoundTag tag = itemStack.getTag();
|
|
if (tag == null) {
|
|
tag = new CompoundTag();
|
|
}
|
|
tag.putByte(RelicItem.RELIC_REVEALED_KEY, (byte) 1);
|
|
itemStack.setTag(tag);
|
|
return itemStack;
|
|
} else {
|
|
return ItemStack.EMPTY;
|
|
}
|
|
}
|
|
|
|
@Override
|
|
@Environment(EnvType.CLIENT)
|
|
public boolean fits(int width, int height) {
|
|
return width * height >= 2;
|
|
}
|
|
|
|
@Override
|
|
public RecipeSerializer<?> getSerializer() {
|
|
return RelicCraft.REVEAL_RELIC_RECIPE;
|
|
}
|
|
|
|
@Override
|
|
public DefaultedList<ItemStack> getRemainingStacks(CraftingInventory inventory) {
|
|
DefaultedList<ItemStack> defaultedList = DefaultedList.ofSize(inventory.size(), ItemStack.EMPTY);
|
|
for (int i = 0; i < defaultedList.size(); i++) {
|
|
ItemStack stack = inventory.getStack(i);
|
|
if (stack.getItem().hasRecipeRemainder()) {
|
|
defaultedList.set(i, new ItemStack(stack.getItem().getRecipeRemainder()));
|
|
} else if (stack.getItem() == Items.NETHER_STAR && !stack.damage(1, new Random(), null)) {
|
|
defaultedList.set(i, stack.copy());
|
|
}
|
|
}
|
|
return defaultedList;
|
|
}
|
|
}
|