diff --git a/src/main/java/ru/betterend/blocks/InfusionPedestal.java b/src/main/java/ru/betterend/blocks/InfusionPedestal.java index f6c0a1c3..fa510931 100644 --- a/src/main/java/ru/betterend/blocks/InfusionPedestal.java +++ b/src/main/java/ru/betterend/blocks/InfusionPedestal.java @@ -5,10 +5,15 @@ import net.minecraft.block.BlockState; import net.minecraft.block.Blocks; import net.minecraft.block.ShapeContext; import net.minecraft.block.entity.BlockEntity; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraft.util.ActionResult; +import net.minecraft.util.Hand; +import net.minecraft.util.hit.BlockHitResult; import net.minecraft.util.math.BlockPos; import net.minecraft.util.shape.VoxelShape; import net.minecraft.util.shape.VoxelShapes; import net.minecraft.world.BlockView; +import net.minecraft.world.World; import ru.betterend.blocks.basis.BlockPedestal; import ru.betterend.blocks.entities.InfusionPedestalEntity; @@ -21,6 +26,12 @@ public class InfusionPedestal extends BlockPedestal { this.height = 1.08F; } + @Override + public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) { + ActionResult result = super.onUse(state, world, pos, player, hand, hit); + return result; + } + @Override public BlockEntity createBlockEntity(BlockView world) { return new InfusionPedestalEntity(); diff --git a/src/main/java/ru/betterend/blocks/entities/InfusionPedestalEntity.java b/src/main/java/ru/betterend/blocks/entities/InfusionPedestalEntity.java index 315f5b71..8f022ddb 100644 --- a/src/main/java/ru/betterend/blocks/entities/InfusionPedestalEntity.java +++ b/src/main/java/ru/betterend/blocks/entities/InfusionPedestalEntity.java @@ -1,5 +1,8 @@ package ru.betterend.blocks.entities; +import net.minecraft.block.BlockState; +import net.minecraft.nbt.CompoundTag; + import ru.betterend.rituals.InfusionRitual; public class InfusionPedestalEntity extends PedestalBlockEntity { @@ -9,5 +12,14 @@ public class InfusionPedestalEntity extends PedestalBlockEntity { public boolean hasRitual() { return this.activeRitual != null; } + + @Override + public void fromTag(BlockState state, CompoundTag tag) { + super.fromTag(state, tag); + } + @Override + public CompoundTag toTag(CompoundTag tag) { + return super.toTag(tag); + } } diff --git a/src/main/java/ru/betterend/recipe/builders/InfusionRecipe.java b/src/main/java/ru/betterend/recipe/builders/InfusionRecipe.java index f6af7230..675e6841 100644 --- a/src/main/java/ru/betterend/recipe/builders/InfusionRecipe.java +++ b/src/main/java/ru/betterend/recipe/builders/InfusionRecipe.java @@ -1,55 +1,232 @@ package ru.betterend.recipe.builders; +import java.util.Arrays; + +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; + +import net.minecraft.item.ItemConvertible; import net.minecraft.item.ItemStack; +import net.minecraft.nbt.CompoundTag; +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.util.Identifier; +import net.minecraft.util.JsonHelper; +import net.minecraft.util.registry.Registry; import net.minecraft.world.World; + +import ru.betterend.BetterEnd; +import ru.betterend.interfaces.CompoundSerializer; +import ru.betterend.recipe.EndRecipeManager; import ru.betterend.rituals.InfusionRitual; public class InfusionRecipe implements Recipe { - - @Override - public boolean matches(InfusionRitual inv, World world) { - // TODO Auto-generated method stub - return false; + + public final static String GROUP = "infusion"; + public final static RecipeType 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 Ingredient input; + private ItemStack output; + private int time = 1; + private Ingredient[] catalysts = new Ingredient[8]; + + private InfusionRecipe(Identifier id) { + this(id, null, null); + } + + private InfusionRecipe(Identifier id, Ingredient input, ItemStack output) { + this.id = id; + this.input = input; + this.output = output; + Arrays.fill(catalysts, Ingredient.EMPTY); + } + + public int getInfusionTime() { + return this.time; } @Override - public ItemStack craft(InfusionRitual inv) { - // TODO Auto-generated method stub - return null; + public boolean matches(InfusionRitual inv, World world) { + boolean valid = this.input.test(inv.getStack(0)); + if (!valid) return false; + for (int i = 1; i < 9; i++) { + valid &= this.catalysts[i].test(inv.getStack(i)); + } + return valid; + } + + @Override + public ItemStack craft(InfusionRitual ritual) { + return this.output.copy(); } @Override public boolean fits(int width, int height) { - // TODO Auto-generated method stub - return false; + return true; } @Override public ItemStack getOutput() { - // TODO Auto-generated method stub - return null; + return this.output; } @Override public Identifier getId() { - // TODO Auto-generated method stub - return null; + return this.id; } @Override public RecipeSerializer getSerializer() { - // TODO Auto-generated method stub - return null; + return SERIALIZER; } @Override public RecipeType getType() { - // TODO Auto-generated method stub - return null; + return TYPE; + } + + public InfusionRecipe fromTag(CompoundTag tag) { + return SERIALIZER.fromTag(tag); } + public CompoundTag toTag(CompoundTag tag) { + return SERIALIZER.toTag(this, tag); + } + + public static class Builder { + private final static Builder INSTANCE = new Builder(); + + public static Builder create(String id) { + return create(BetterEnd.makeID(id)); + } + + public static Builder create(Identifier id) { + INSTANCE.id = id; + INSTANCE.input = null; + INSTANCE.output = null; + INSTANCE.time = 1; + + Arrays.fill(INSTANCE.catalysts, Ingredient.EMPTY); + + return INSTANCE; + } + + private Identifier id; + private Ingredient input; + private ItemStack output; + private int time = 1; + private Ingredient[] catalysts = new Ingredient[8]; + + private Builder() { + Arrays.fill(catalysts, Ingredient.EMPTY); + } + + public Builder setInput(ItemConvertible input) { + this.input = Ingredient.ofItems(input); + return this; + } + + public Builder setOutput(ItemStack output) { + this.output = output; + this.output.setCount(1); + return this; + } + + public Builder setTime(int time) { + this.time = time; + return this; + } + + public Builder addCatalyst(int slot, ItemConvertible item) { + if (slot > 7) return this; + this.catalysts[slot] = Ingredient.ofItems(item); + return this; + } + + public void build() { + if (input == null) { + BetterEnd.LOGGER.warning("Input for Infusion recipe can't be 'null', recipe {} will be ignored!", id); + return; + } + if (output == null) { + BetterEnd.LOGGER.warning("Output for Infusion recipe can't be 'null', recipe {} will be ignored!", id); + return; + } + InfusionRecipe recipe = new InfusionRecipe(id, input, output); + recipe.time = time; + int empty = 0; + for (int i = 0; i < catalysts.length; i++) { + if (catalysts[i].isEmpty()) empty++; + else recipe.catalysts[i] = catalysts[i]; + } + if (empty == catalysts.length) { + BetterEnd.LOGGER.warning("At least one catalyst must be non empty, recipe {} will be ignored!", id); + return; + } + EndRecipeManager.addRecipe(TYPE, recipe); + } + } + + public static class Serializer implements RecipeSerializer { + @Override + public InfusionRecipe read(Identifier id, JsonObject json) { + InfusionRecipe recipe = new InfusionRecipe(id); + recipe.input = Ingredient.fromJson(json.get("input")); + Identifier outId = new Identifier(JsonHelper.getString(json, "output")); + recipe.output = new ItemStack(Registry.ITEM.getOrEmpty(outId).orElseThrow(() -> { + return new IllegalStateException("Item: " + outId + " does not exists!"); + })); + recipe.time = JsonHelper.getInt(json, "time", 1); + JsonArray catalysts = JsonHelper.asArray(json, "catalysts"); + for (int i = 0; i < catalysts.size(); i++) { + ItemStack stack = new ItemStack(Registry.ITEM.getOrEmpty(outId).orElse(null)); + recipe.catalysts[i] = Ingredient.ofStacks( + Arrays.stream(new ItemStack[] { stack })); + } + return recipe; + } + + @Override + public InfusionRecipe read(Identifier id, PacketByteBuf buffer) { + InfusionRecipe recipe = new InfusionRecipe(id); + recipe.input = Ingredient.fromPacket(buffer); + recipe.output = buffer.readItemStack(); + recipe.time = buffer.readVarInt(); + for (int i = 0; i < 9; i++) { + recipe.catalysts[i] = Ingredient.fromPacket(buffer); + } + return recipe; + } + + @Override + public void write(PacketByteBuf buffer, InfusionRecipe recipe) { + recipe.input.write(buffer); + buffer.writeItemStack(recipe.output); + buffer.writeVarInt(recipe.time); + for (int i = 0; i < 9; i++) { + recipe.catalysts[i].write(buffer); + } + } + + public InfusionRecipe fromTag(CompoundTag tag) { + Identifier id = new Identifier(tag.getString("id")); + InfusionRecipe recipe = new InfusionRecipe(id); + + return recipe; + } + + public CompoundTag toTag(InfusionRecipe recipe, CompoundTag tag) { + CompoundSerializer inputSerializer = CompoundSerializer.class.cast(recipe.input); + tag.put("input", inputSerializer.toTag(new CompoundTag())); + tag.put("output", recipe.output.toTag(new CompoundTag())); + + return tag; + } + } } diff --git a/src/main/java/ru/betterend/rituals/InfusionRitual.java b/src/main/java/ru/betterend/rituals/InfusionRitual.java index 2887283e..03864391 100644 --- a/src/main/java/ru/betterend/rituals/InfusionRitual.java +++ b/src/main/java/ru/betterend/rituals/InfusionRitual.java @@ -1,19 +1,42 @@ package ru.betterend.rituals; +import net.minecraft.block.BlockState; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.inventory.Inventory; import net.minecraft.item.ItemStack; +import net.minecraft.nbt.CompoundTag; +import net.minecraft.util.math.BlockPos; +import net.minecraft.world.World; +import ru.betterend.recipe.builders.InfusionRecipe; public class InfusionRitual implements Inventory { + private final World world; + private final BlockPos worldPos; + private InfusionRecipe activeRecipe; + private int progress = 0; + private int time = 0; + + public InfusionRitual(World world, BlockPos pos) { + this.world = world; + this.worldPos = pos; + } + public void tick() { - // TODO + if (!hasRecipe()) return; + this.progress++; + if (progress == time) { + + } + } + + public boolean hasRecipe() { + return this.activeRecipe != null; } @Override public void clear() { - // TODO Auto-generated method stub - + // TODO } @Override @@ -53,6 +76,13 @@ public class InfusionRitual implements Inventory { @Override public boolean canPlayerUse(PlayerEntity player) { - return false; + return true; + } + + public void fromTag(CompoundTag tag) { + } + + public CompoundTag toTag(CompoundTag tag) { + return tag; } }