Skip to content

Commit

Permalink
customRecipeType
Browse files Browse the repository at this point in the history
  • Loading branch information
BeiShanair committed Feb 19, 2024
1 parent 5ae7eb9 commit 82723f5
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 6 deletions.
3 changes: 3 additions & 0 deletions src/main/java/com/besson/tutorialmod/TutorialMod.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.besson.tutorialmod.block.entity.ModBlockEntities;
import com.besson.tutorialmod.item.ModItemGroup;
import com.besson.tutorialmod.item.ModItems;
import com.besson.tutorialmod.recipe.ModRecipes;
import com.besson.tutorialmod.screen.ModScreenHandlers;
import com.besson.tutorialmod.sounds.ModSounds;
import com.besson.tutorialmod.util.ModLootTableModifiers;
Expand Down Expand Up @@ -34,6 +35,8 @@ public void onInitialize() {
ModBlockEntities.registerBlockEntities();
ModScreenHandlers.registerScreenHandlers();

ModRecipes.registerRecipes();

FuelRegistry.INSTANCE.add(ModItems.ANTHRACITE,2000);
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.besson.tutorialmod.block.entity;

import com.besson.tutorialmod.item.ModItems;
import com.besson.tutorialmod.recipe.PolishingMachineRecipe;
import com.besson.tutorialmod.screen.PolishingMachineScreenHandler;
import net.fabricmc.fabric.api.screenhandler.v1.ExtendedScreenHandlerFactory;
import net.minecraft.block.BlockState;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.Inventories;
import net.minecraft.inventory.SimpleInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NbtCompound;
Expand All @@ -21,6 +22,8 @@
import net.minecraft.world.World;
import org.jetbrains.annotations.Nullable;

import java.util.Optional;

public class PolishingMachineBlockEntity extends BlockEntity implements ExtendedScreenHandlerFactory,ImplementedInventory {
private final DefaultedList<ItemStack> inventory = DefaultedList.ofSize(2,ItemStack.EMPTY);

Expand Down Expand Up @@ -120,9 +123,10 @@ private void resetProgress() {

private void craftItem() {
this.removeStack(INPUT_SLOT,1);
ItemStack result = new ItemStack(ModItems.ICE_ETHER);
Optional<PolishingMachineRecipe> recipe = getCurrentRecipe();

this.setStack(OUTPUT_SLOT,new ItemStack(result.getItem(),getStack(OUTPUT_SLOT).getCount() + result.getCount()));
this.setStack(OUTPUT_SLOT,new ItemStack(recipe.get().getOutput(null).getItem(),
getStack(OUTPUT_SLOT).getCount() + recipe.get().getOutput(null).getCount()));
}

private boolean hasCraftingFinished() {
Expand All @@ -134,10 +138,18 @@ private void increaseCraftProgress() {
}

private boolean hsaRecipe() {
ItemStack result = new ItemStack(ModItems.ICE_ETHER);
boolean hasInput = getStack(INPUT_SLOT).getItem() == ModItems.RAW_ICE_ETHER;
Optional<PolishingMachineRecipe> recipe = getCurrentRecipe();

return recipe.isPresent() && canInsertAmountIntoOutputSlot(recipe.get().getOutput(null)) &&
canInsertItemIntoOutputSlot(recipe.get().getOutput(null).getItem());
}

return hasInput && canInsertAmountIntoOutputSlot(result) && canInsertItemIntoOutputSlot(result.getItem());
private Optional<PolishingMachineRecipe> getCurrentRecipe() {
SimpleInventory inv = new SimpleInventory(this.size());
for (int i = 0; i< this.size(); i++){
inv.setStack(i,this.getStack(i));
}
return getWorld().getRecipeManager().getFirstMatch(PolishingMachineRecipe.Type.INSTANCE,inv,getWorld());
}

private boolean canInsertAmountIntoOutputSlot(ItemStack result) {
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/besson/tutorialmod/recipe/ModRecipes.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.besson.tutorialmod.recipe;

import com.besson.tutorialmod.TutorialMod;
import net.minecraft.registry.Registries;
import net.minecraft.registry.Registry;
import net.minecraft.util.Identifier;

public class ModRecipes {
public static void registerRecipes(){
Registry.register(Registries.RECIPE_SERIALIZER,new Identifier(TutorialMod.MOD_ID,PolishingMachineRecipe.Serializer.ID),
PolishingMachineRecipe.Serializer.INSTANCE);
Registry.register(Registries.RECIPE_TYPE,new Identifier(TutorialMod.MOD_ID,PolishingMachineRecipe.Type.ID),
PolishingMachineRecipe.Type.INSTANCE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package com.besson.tutorialmod.recipe;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import net.minecraft.inventory.SimpleInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.recipe.*;
import net.minecraft.registry.DynamicRegistryManager;
import net.minecraft.util.Identifier;
import net.minecraft.util.JsonHelper;
import net.minecraft.util.collection.DefaultedList;
import net.minecraft.world.World;

import java.util.List;

public class PolishingMachineRecipe implements Recipe<SimpleInventory> {
private final Identifier id;
private final ItemStack output;
private final List<Ingredient> recipeItems;

public PolishingMachineRecipe(Identifier id, ItemStack output, List<Ingredient> recipeItems) {
this.id = id;
this.output = output;
this.recipeItems = recipeItems;
}

@Override
public boolean matches(SimpleInventory inventory, World world) {
if (world.isClient()){
return false;
}
return recipeItems.get(0).test(inventory.getStack(0));
}

@Override
public ItemStack craft(SimpleInventory inventory, DynamicRegistryManager registryManager) {
return output;
}

@Override
public boolean fits(int width, int height) {
return true;
}

@Override
public ItemStack getOutput(DynamicRegistryManager registryManager) {
return output;
}

@Override
public Identifier getId() {
return id;
}

@Override
public RecipeSerializer<?> getSerializer() {
return Serializer.INSTANCE;
}

@Override
public RecipeType<?> getType() {
return Type.INSTANCE;
}
public static class Type implements RecipeType<PolishingMachineRecipe>{
public static final Type INSTANCE = new Type();
public static final String ID = "polishing_machine";
}
public static class Serializer implements RecipeSerializer<PolishingMachineRecipe>{
public static final Serializer INSTANCE = new Serializer();
public static final String ID = "polishing_machine";
@Override
public PolishingMachineRecipe read(Identifier id, JsonObject json) {
ItemStack output = ShapedRecipe.outputFromJson(JsonHelper.getObject(json,"output"));

JsonArray ingredients = JsonHelper.getArray(json,"ingredients");
DefaultedList<Ingredient> inputs = DefaultedList.ofSize(1,Ingredient.EMPTY);
for (int i = 0; i < inputs.size(); i++){
inputs.set(i,Ingredient.fromJson(ingredients.get(i)));
}
return new PolishingMachineRecipe(id,output,inputs);
}

@Override
public PolishingMachineRecipe read(Identifier id, PacketByteBuf buf) {
DefaultedList<Ingredient> inputs = DefaultedList.ofSize(1,Ingredient.EMPTY);

for (int i = 0; i < inputs.size(); i++){
inputs.set(i,Ingredient.fromPacket(buf));
}
ItemStack output = buf.readItemStack();
return new PolishingMachineRecipe(id,output,inputs);
}

@Override
public void write(PacketByteBuf buf, PolishingMachineRecipe recipe) {
buf.writeInt(recipe.getIngredients().size());
for (Ingredient ingredient: recipe.getIngredients()){
ingredient.write(buf);
}
buf.writeItemStack(recipe.getOutput(null));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"type": "tutorialmod:polishing_machine",
"ingredients": [
{
"item": "minecraft:coal"
}
],
"output": {
"item": "minecraft:diamond"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"type": "tutorialmod:polishing_machine",
"ingredients": [
{
"item": "tutorialmod:raw_ice_ether"
}
],
"output": {
"item": "tutorialmod:ice_ether",
"count": 3
}
}

0 comments on commit 82723f5

Please sign in to comment.