End lily recipes

This commit is contained in:
paulevsGitch 2020-10-11 13:44:48 +03:00
parent 2d1fe18781
commit 3f9e524b54
33 changed files with 221 additions and 44 deletions

View file

@ -0,0 +1,253 @@
package ru.betterend.recipe.builders;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemConvertible;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.recipe.Ingredient;
import net.minecraft.recipe.Recipe;
import net.minecraft.recipe.RecipeSerializer;
import net.minecraft.recipe.RecipeType;
import net.minecraft.tag.Tag;
import net.minecraft.util.Identifier;
import net.minecraft.util.JsonHelper;
import net.minecraft.util.collection.DefaultedList;
import net.minecraft.util.registry.Registry;
import net.minecraft.world.World;
import ru.betterend.BetterEnd;
import ru.betterend.recipe.EndRecipeManager;
import ru.betterend.registry.BlockRegistry;
public class AlloyingRecipe implements Recipe<Inventory> {
public final static String GROUP = "alloying";
public final static RecipeType<AlloyingRecipe> TYPE = EndRecipeManager.registerType(GROUP);
public final static Serializer SERIALIZER = EndRecipeManager.registerSerializer(GROUP, new Serializer());
public final static Identifier ID = BetterEnd.makeID(GROUP);
protected final RecipeType<?> type;
protected final Identifier id;
protected final Ingredient primaryInput;
protected final Ingredient secondaryInput;
protected final ItemStack output;
protected final String group;
protected final float experience;
protected final int smeltTime;
public AlloyingRecipe(Identifier id, String group, Ingredient primaryInput, Ingredient secondaryInput, ItemStack output, float experience, int smeltTime) {
this.group = group;
this.id = id;
this.primaryInput = primaryInput;
this.secondaryInput = secondaryInput;
this.output = output;
this.experience = experience;
this.smeltTime = smeltTime;
this.type = TYPE;
}
public float getExperience() {
return this.experience;
}
public int getSmeltTime() {
return this.smeltTime;
}
@Override
public DefaultedList<Ingredient> getPreviewInputs() {
DefaultedList<Ingredient> defaultedList = DefaultedList.of();
defaultedList.add(primaryInput);
defaultedList.add(secondaryInput);
return defaultedList;
}
@Override
public boolean matches(Inventory inv, World world) {
return this.primaryInput.test(inv.getStack(0)) && this.secondaryInput.test(inv.getStack(1)) ||
this.primaryInput.test(inv.getStack(1)) && this.secondaryInput.test(inv.getStack(0));
}
@Override
public ItemStack craft(Inventory inv) {
return this.output.copy();
}
@Override
public boolean fits(int width, int height) {
return true;
}
@Override
public ItemStack getOutput() {
return this.output;
}
@Override
public Identifier getId() {
return this.id;
}
@Override
public RecipeSerializer<?> getSerializer() {
return SERIALIZER;
}
@Override
public RecipeType<?> getType() {
return this.type;
}
@Override
@Environment(EnvType.CLIENT)
public String getGroup() {
return this.group;
}
@Environment(EnvType.CLIENT)
public ItemStack getRecipeKindIcon() {
return new ItemStack(BlockRegistry.END_STONE_SMELTER);
}
public static class Builder {
private final static Builder INSTANCE = new Builder();
public static Builder create(String id) {
INSTANCE.id = BetterEnd.makeID(id);
INSTANCE.group = String.format("%s_%s", GROUP, id);
INSTANCE.primaryInput = null;
INSTANCE.secondaryInput = null;
INSTANCE.output = null;
INSTANCE.experience = 0.0F;
INSTANCE.smeltTime = 350;
return INSTANCE;
}
private Identifier id;
private Ingredient primaryInput;
private Ingredient secondaryInput;
private ItemStack output;
private String group;
private float experience;
private int smeltTime;
private Builder() {}
public Builder setGroup(String group) {
this.group = group;
return this;
}
public Builder setPrimaryInput(ItemConvertible... inputs) {
this.primaryInput = Ingredient.ofItems(inputs);
return this;
}
public Builder setSecondaryInput(ItemConvertible... inputs) {
this.secondaryInput = Ingredient.ofItems(inputs);
return this;
}
public Builder setPrimaryInput(Tag<Item> input) {
this.primaryInput = Ingredient.fromTag(input);
return this;
}
public Builder setSecondaryInput(Tag<Item> input) {
this.secondaryInput = Ingredient.fromTag(input);
return this;
}
public Builder setInput(ItemConvertible primaryInput, ItemConvertible secondaryInput) {
this.setPrimaryInput(primaryInput);
this.setSecondaryInput(secondaryInput);
return this;
}
public Builder setInput(Tag<Item> primaryInput, Tag<Item> secondaryInput) {
this.setPrimaryInput(primaryInput);
this.setSecondaryInput(secondaryInput);
return this;
}
public Builder setInput(Ingredient primaryInput, Ingredient secondaryInput) {
this.primaryInput = primaryInput;
this.secondaryInput = secondaryInput;
return this;
}
public Builder setOutput(ItemConvertible output, int amount) {
this.output = new ItemStack(output, amount);
return this;
}
public Builder setExpiriense(float amount) {
this.experience = amount;
return this;
}
public Builder setSmeltTime(int time) {
this.smeltTime = time;
return this;
}
public void build() {
if (primaryInput == null) {
throw new IllegalArgumentException("Primary input can't be null!");
} else if(secondaryInput == null) {
throw new IllegalArgumentException("Secondary input can't be null!");
} else if(output == null) {
throw new IllegalArgumentException("Output can't be null!");
}
EndRecipeManager.addRecipe(AlloyingRecipe.TYPE, new AlloyingRecipe(id, group, primaryInput, secondaryInput, output, experience, smeltTime));
}
}
public static class Serializer implements RecipeSerializer<AlloyingRecipe> {
@Override
public AlloyingRecipe read(Identifier id, JsonObject json) {
JsonArray ingredients = JsonHelper.getArray(json, "ingredients");
Ingredient primaryInput = Ingredient.fromJson(ingredients.get(0));
Ingredient secondaryInput = Ingredient.fromJson(ingredients.get(1));
String resultStr = JsonHelper.getString(json, "result");
String group = JsonHelper.getString(json, "group", "");
Identifier resultId = new Identifier(resultStr);
ItemStack output = new ItemStack(Registry.ITEM.getOrEmpty(resultId).orElseThrow(() -> {
return new IllegalStateException("Item: " + resultStr + " does not exists!");
}));
float experience = JsonHelper.getFloat(json, "experience", 0.0F);
int smeltTime = JsonHelper.getInt(json, "smelttime", 350);
return new AlloyingRecipe(id, group, primaryInput, secondaryInput, output, experience, smeltTime);
}
@Override
public AlloyingRecipe read(Identifier id, PacketByteBuf packetBuffer) {
String group = packetBuffer.readString(32767);
Ingredient primary = Ingredient.fromPacket(packetBuffer);
Ingredient secondary = Ingredient.fromPacket(packetBuffer);
ItemStack output = packetBuffer.readItemStack();
float experience = packetBuffer.readFloat();
int smeltTime = packetBuffer.readVarInt();
return new AlloyingRecipe(id, group, primary, secondary, output, experience, smeltTime);
}
@Override
public void write(PacketByteBuf packetBuffer, AlloyingRecipe recipe) {
packetBuffer.writeString(recipe.group);
recipe.primaryInput.write(packetBuffer);
recipe.secondaryInput.write(packetBuffer);
packetBuffer.writeItemStack(recipe.output);
packetBuffer.writeFloat(recipe.experience);
packetBuffer.writeVarInt(recipe.smeltTime);
}
}
}

View file

@ -0,0 +1,221 @@
package ru.betterend.recipe.builders;
import com.google.gson.JsonObject;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemConvertible;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ToolItem;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.recipe.Ingredient;
import net.minecraft.recipe.Recipe;
import net.minecraft.recipe.RecipeSerializer;
import net.minecraft.recipe.RecipeType;
import net.minecraft.tag.Tag;
import net.minecraft.util.Identifier;
import net.minecraft.util.JsonHelper;
import net.minecraft.util.collection.DefaultedList;
import net.minecraft.util.registry.Registry;
import net.minecraft.world.World;
import ru.betterend.BetterEnd;
import ru.betterend.recipe.EndRecipeManager;
import ru.betterend.registry.ItemTagRegistry;
public class AnvilSmithingRecipe implements Recipe<Inventory> {
public final static String GROUP = "smithing";
public final static RecipeType<AnvilSmithingRecipe> TYPE = EndRecipeManager.registerType(GROUP);
public final static Serializer SERIALIZER = EndRecipeManager.registerSerializer(GROUP, new Serializer());
public final static Identifier ID = BetterEnd.makeID(GROUP);
private final Identifier id;
private final Ingredient input;
private final ItemStack output;
private final int damage;
private final int level;
public AnvilSmithingRecipe(Identifier identifier, Ingredient input, ItemStack output, int level, int damage) {
this.id = identifier;
this.input = input;
this.output = output;
this.level = level;
this.damage = damage;
}
@Override
public RecipeSerializer<?> getSerializer() {
return SERIALIZER;
}
@Override
public ItemStack getOutput() {
return this.output;
}
@Override
public boolean matches(Inventory craftingInventory, World world) {
return this.matches(craftingInventory);
}
@Override
public ItemStack craft(Inventory craftingInventory) {
return this.output.copy();
}
public ItemStack craft(Inventory craftingInventory, PlayerEntity player) {
if (!player.isCreative()) {
ItemStack hammer = craftingInventory.getStack(0);
int damage = hammer.getDamage() + this.damage;
if (damage >= hammer.getMaxDamage()) return ItemStack.EMPTY;
hammer.damage(this.damage, player, entity -> {
entity.sendEquipmentBreakStatus(null);
});
}
return this.craft(craftingInventory);
}
public boolean matches(Inventory craftingInventory) {
ItemStack hammer = craftingInventory.getStack(0);
if (hammer.isEmpty() || !ItemTagRegistry.HAMMERS.contains(hammer.getItem())) {
return false;
}
int level = ((ToolItem) hammer.getItem()).getMaterial().getMiningLevel();
return level >= this.level && this.input.test(craftingInventory.getStack(1));
}
public int getDamage() {
return this.damage;
}
@Override
public DefaultedList<Ingredient> getPreviewInputs() {
DefaultedList<Ingredient> defaultedList = DefaultedList.of();
defaultedList.add(Ingredient.ofStacks(ItemTagRegistry.HAMMERS.values().stream().filter(hammer -> {
return ((ToolItem) hammer).getMaterial().getMiningLevel() >= level;
}).map(ItemStack::new)));
defaultedList.add(input);
return defaultedList;
}
@Override
@Environment(EnvType.CLIENT)
public boolean fits(int width, int height) {
return true;
}
@Override
public Identifier getId() {
return this.id;
}
@Override
public RecipeType<?> getType() {
return TYPE;
}
@Override
public boolean isIgnoredInRecipeBook() {
return true;
}
public static class Builder {
private final static Builder INSTANCE = new Builder();
public static Builder create(String id) {
INSTANCE.id = BetterEnd.makeID(id);
INSTANCE.input = null;
INSTANCE.output = null;
INSTANCE.level = 1;
INSTANCE.damage = 1;
return INSTANCE;
}
private Identifier id;
private Ingredient input;
private ItemStack output;
private int level = 1;
private int damage = 1;
private Builder() {}
public Builder setInput(ItemConvertible... inputItem) {
this.setInput(Ingredient.ofItems(inputItem));
return this;
}
public Builder setInput(Tag<Item> inputTag) {
this.setInput(Ingredient.fromTag(inputTag));
return this;
}
public Builder setInput(Ingredient ingredient) {
this.input = ingredient;
return this;
}
public Builder setOutput(ItemConvertible output, int amount) {
this.output = new ItemStack(output, amount);
return this;
}
public Builder setLevel(int level) {
this.level = level;
return this;
}
public Builder setDamage(int damage) {
this.damage = damage;
return this;
}
public void build() {
if (input == null) {
throw new IllegalArgumentException("Input can't be null!");
} else if(output == null) {
throw new IllegalArgumentException("Output can't be null!");
}
EndRecipeManager.addRecipe(TYPE, new AnvilSmithingRecipe(id, input, output, level, damage));
}
}
public static class Serializer implements RecipeSerializer<AnvilSmithingRecipe> {
@Override
public AnvilSmithingRecipe read(Identifier id, JsonObject json) {
Ingredient input = Ingredient.fromJson(JsonHelper.getObject(json, "input"));
String resultStr = JsonHelper.getString(json, "result");
Identifier resultId = new Identifier(resultStr);
ItemStack output = new ItemStack(Registry.ITEM.getOrEmpty(resultId).orElseThrow(() -> {
return new IllegalStateException("Item: " + resultStr + " does not exists!");
}));
int level = JsonHelper.getInt(json, "level", 1);
int damage = JsonHelper.getInt(json, "damage", 1);
return new AnvilSmithingRecipe(id, input, output, level, damage);
}
@Override
public AnvilSmithingRecipe read(Identifier id, PacketByteBuf packetBuffer) {
Ingredient input = Ingredient.fromPacket(packetBuffer);
ItemStack output = packetBuffer.readItemStack();
int level = packetBuffer.readVarInt();
int damage = packetBuffer.readVarInt();
return new AnvilSmithingRecipe(id, input, output, level, damage);
}
@Override
public void write(PacketByteBuf packetBuffer, AnvilSmithingRecipe recipe) {
recipe.input.write(packetBuffer);
packetBuffer.writeItemStack(recipe.output);
packetBuffer.writeVarInt(recipe.level);
packetBuffer.writeVarInt(recipe.damage);
}
}
}

View file

@ -0,0 +1,69 @@
package ru.betterend.recipe.builders;
import net.minecraft.item.ItemConvertible;
import net.minecraft.item.ItemStack;
import net.minecraft.recipe.Ingredient;
import net.minecraft.recipe.RecipeType;
import net.minecraft.recipe.SmeltingRecipe;
import net.minecraft.util.Identifier;
import ru.betterend.BetterEnd;
import ru.betterend.recipe.EndRecipeManager;
import ru.betterend.util.RecipeHelper;
public class FurnaceRecipe {
private static final FurnaceRecipe INSTANCE = new FurnaceRecipe();
private ItemConvertible input;
private ItemConvertible output;
private boolean exist = true;
private String group;
private String name;
private int count;
private int time;
private float xp;
private FurnaceRecipe() {}
public static FurnaceRecipe make(String name, ItemConvertible input, ItemConvertible output) {
INSTANCE.name = name;
INSTANCE.group = "";
INSTANCE.input = input;
INSTANCE.output = output;
INSTANCE.count = 1;
INSTANCE.time = 200;
INSTANCE.xp = 0;
INSTANCE.exist = RecipeHelper.exists(output) && RecipeHelper.exists(input);
return INSTANCE;
}
public FurnaceRecipe setGroup(String group) {
this.group = group;
return this;
}
public FurnaceRecipe setOutputCount(int count) {
this.count = count;
return this;
}
public FurnaceRecipe setXP(float xp) {
this.xp = xp;
return this;
}
public FurnaceRecipe setCookTime(int time) {
this.time = time;
return this;
}
public void build() {
if (exist) {
Identifier id = BetterEnd.makeID(name);
SmeltingRecipe recipe = new SmeltingRecipe(id, group, Ingredient.ofItems(input), new ItemStack(output, count), xp, time);
EndRecipeManager.addRecipe(RecipeType.SMELTING, recipe);
}
else {
BetterEnd.LOGGER.debug("Smelting recipe {} couldn't be added", name);
}
}
}

View file

@ -0,0 +1,119 @@
package ru.betterend.recipe.builders;
import java.util.Map;
import com.google.common.collect.Maps;
import net.minecraft.item.Item;
import net.minecraft.item.ItemConvertible;
import net.minecraft.item.ItemStack;
import net.minecraft.recipe.CraftingRecipe;
import net.minecraft.recipe.Ingredient;
import net.minecraft.recipe.RecipeType;
import net.minecraft.recipe.ShapedRecipe;
import net.minecraft.recipe.ShapelessRecipe;
import net.minecraft.tag.Tag;
import net.minecraft.util.Identifier;
import net.minecraft.util.collection.DefaultedList;
import ru.betterend.BetterEnd;
import ru.betterend.recipe.EndRecipeManager;
import ru.betterend.util.RecipeHelper;
public class RecipeBuilder {
private static final RecipeBuilder INSTANCE = new RecipeBuilder();
private String name;
private ItemConvertible output;
private String group;
private RecipeType<?> type;
private boolean shaped;
private String[] shape;
private Map<Character, Ingredient> materialKeys = Maps.newHashMap();
private int count;
private boolean exist = true;
private RecipeBuilder() {}
public static RecipeBuilder make(String name, ItemConvertible output) {
INSTANCE.name = name;
INSTANCE.output = output;
INSTANCE.group = "";
INSTANCE.type = RecipeType.CRAFTING;
INSTANCE.shaped = true;
INSTANCE.shape = new String[] {"#"};
INSTANCE.materialKeys.clear();
INSTANCE.count = 1;
INSTANCE.exist = RecipeHelper.exists(output);
return INSTANCE;
}
public RecipeBuilder setGroup(String group) {
this.group = group;
return this;
}
public RecipeBuilder setShape(String... shape) {
this.shape = shape;
return this;
}
public RecipeBuilder setList(String shape) {
this.shape = new String[] {shape};
this.shaped = false;
return this;
}
public RecipeBuilder addMaterial(char key, Tag<Item> value) {
return addMaterial(key, Ingredient.fromTag(value));
}
public RecipeBuilder addMaterial(char key, ItemConvertible... values) {
for (ItemConvertible item: values) {
exist &= RecipeHelper.exists(item);
}
return addMaterial(key, Ingredient.ofItems(values));
}
private RecipeBuilder addMaterial(char key, Ingredient value) {
materialKeys.put(key, value);
return this;
}
public RecipeBuilder setOutputCount(int count) {
this.count = count;
return this;
}
private DefaultedList<Ingredient> getMaterials(int width, int height) {
DefaultedList<Ingredient> materials = DefaultedList.ofSize(width * height, Ingredient.EMPTY);
int pos = 0;
for (String line: shape) {
for (int i = 0; i < width; i++) {
char c = line.charAt(i);
Ingredient material = materialKeys.get(c);
materials.set(pos ++, material == null ? Ingredient.EMPTY : material);
}
}
return materials;
}
public void build() {
if (exist) {
int height = shape.length;
int width = shape[0].length();
ItemStack result = new ItemStack(output, count);
Identifier id = BetterEnd.makeID(name);
DefaultedList<Ingredient> materials = this.getMaterials(width, height);
CraftingRecipe recipe = shaped ? new ShapedRecipe(id, group, width, height, materials, result) : new ShapelessRecipe(id, group, result, materials);
EndRecipeManager.addRecipe(type, recipe);
}
else {
BetterEnd.LOGGER.debug("recipe {} couldn't be added", name);
}
}
}