Skip to content

Commit

Permalink
Fix clay tool being consumed while crafting bricks - splits recipes i…
Browse files Browse the repository at this point in the history
…nto two delegates for shaped and shapeless due to Forge. Fixes #219. Add cauldron interactions for ceramic buckets, Fixes #221.
  • Loading branch information
alcatrazEscapee committed Jul 17, 2022
1 parent c4795c0 commit 43c323c
Show file tree
Hide file tree
Showing 81 changed files with 610 additions and 525 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
package com.alcatrazescapee.notreepunching;

import net.minecraft.core.BlockPos;
import net.minecraft.core.cauldron.CauldronInteraction;
import net.minecraft.sounds.SoundEvents;
import net.minecraft.sounds.SoundSource;
import net.minecraft.stats.Stats;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.ItemUtils;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.DispenserBlock;
import net.minecraft.world.level.block.LayeredCauldronBlock;
import net.minecraft.world.level.gameevent.GameEvent;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

Expand Down Expand Up @@ -60,5 +70,35 @@ public static void lateSetup()
BlockPos offsetPos = context.getPos().relative(context.getBlockState().getValue(DispenserBlock.FACING));
return ClayToolItem.interactWithBlock(context.getLevel(), offsetPos, context.getLevel().getBlockState(offsetPos), null, null, stack);
});

CauldronInteraction.WATER.put(ModItems.CERAMIC_BUCKET.get(), (state, level, pos, player, hand, stack) -> {
if (state.getValue(LayeredCauldronBlock.LEVEL) == 3)
{
if (!level.isClientSide)
{
player.setItemInHand(hand, ItemUtils.createFilledResult(stack, player, new ItemStack(ModItems.CERAMIC_WATER_BUCKET.get())));
player.awardStat(Stats.USE_CAULDRON);
player.awardStat(Stats.ITEM_USED.get(stack.getItem()));
level.setBlockAndUpdate(pos, Blocks.CAULDRON.defaultBlockState());
level.playSound(null, pos, SoundEvents.BUCKET_FILL, SoundSource.BLOCKS, 1.0F, 1.0F);
level.gameEvent(null, GameEvent.FLUID_PICKUP, pos);
}
return InteractionResult.sidedSuccess(level.isClientSide);
}
return InteractionResult.PASS;
});

CauldronInteraction.EMPTY.put(ModItems.CERAMIC_WATER_BUCKET.get(), (state, level, pos, player, hand, stack) -> {
if (!level.isClientSide)
{
player.setItemInHand(hand, ItemUtils.createFilledResult(stack, player, new ItemStack(ModItems.CERAMIC_BUCKET.get())));
player.awardStat(Stats.FILL_CAULDRON);
player.awardStat(Stats.ITEM_USED.get(stack.getItem()));
level.setBlockAndUpdate(pos, Blocks.WATER_CAULDRON.defaultBlockState());
level.playSound(null, pos, SoundEvents.BUCKET_EMPTY, SoundSource.BLOCKS, 1.0F, 1.0F);
level.gameEvent(null, GameEvent.FLUID_PLACE, pos);
}
return InteractionResult.sidedSuccess(level.isClientSide);
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.level.Level;

public interface IShapedDelegateRecipe<C extends Container> extends Recipe<C>
public interface DelegateRecipe<C extends Container> extends Recipe<C>
{
Recipe<C> delegate();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.alcatrazescapee.notreepunching.common.recipes;

import net.minecraft.core.Registry;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.item.crafting.RecipeSerializer;
import net.minecraft.world.item.crafting.RecipeType;

Expand All @@ -14,7 +13,10 @@ public class ModRecipes
public static final RegistryInterface<RecipeSerializer<?>> RECIPE_SERIALIZERS = XPlatform.INSTANCE.registryInterface(Registry.RECIPE_SERIALIZER);
public static final RegistryInterface<RecipeType<?>> RECIPE_TYPES = XPlatform.INSTANCE.registryInterface(Registry.RECIPE_TYPE);

public static final RegistryHolder<RecipeSerializer<?>> TOOL_DAMAGING = RECIPE_SERIALIZERS.register("tool_damaging", () -> XPlatform.INSTANCE.recipeSerializer(ShapedToolDamagingRecipe.Serializer.INSTANCE));
// Forge requires us to implement IShapedRecipe<> on one of these
// Otherwise we could get away with only one delegate
public static final RegistryHolder<RecipeSerializer<?>> SHAPED_TOOL_DAMAGING = RECIPE_SERIALIZERS.register("tool_damaging_shaped", () -> XPlatform.INSTANCE.recipeSerializer(new ToolDamagingRecipe.Serializer<>(XPlatform.INSTANCE::shapedToolDamagingRecipe)));
public static final RegistryHolder<RecipeSerializer<?>> SHAPELESS_TOOL_DAMAGING = RECIPE_SERIALIZERS.register("tool_damaging_shapeless", () -> XPlatform.INSTANCE.recipeSerializer(new ToolDamagingRecipe.Serializer<>(XPlatform.INSTANCE::shapelessToolDamagingRecipe)));

public static final RegistryHolder<RecipeSerializer<?>> EMPTY_SERIALIZER = RECIPE_SERIALIZERS.register("empty", () -> XPlatform.INSTANCE.recipeSerializer(EmptyRecipe.Serializer.INSTANCE));
public static final RegistryHolder<RecipeType<?>> EMPTY_TYPE = RECIPE_TYPES.register("empty", () -> new RecipeType<>() {});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.alcatrazescapee.notreepunching.common.recipes;

import java.util.function.BiFunction;
import com.google.gson.JsonObject;
import net.minecraft.core.NonNullList;
import net.minecraft.network.FriendlyByteBuf;
Expand All @@ -16,12 +17,12 @@
import com.alcatrazescapee.notreepunching.platform.XPlatform;
import com.alcatrazescapee.notreepunching.util.Helpers;

public class ShapedToolDamagingRecipe implements IShapedDelegateRecipe<CraftingContainer>, CraftingRecipe
public abstract class ToolDamagingRecipe implements DelegateRecipe<CraftingContainer>, CraftingRecipe
{
private final ResourceLocation id;
private final Recipe<?> recipe;

public ShapedToolDamagingRecipe(ResourceLocation id, Recipe<?> recipe)
protected ToolDamagingRecipe(ResourceLocation id, Recipe<?> recipe)
{
this.id = id;
this.recipe = recipe;
Expand Down Expand Up @@ -53,37 +54,57 @@ public ResourceLocation getId()
return id;
}

@Override
public RecipeSerializer<?> getSerializer()
{
return ModRecipes.TOOL_DAMAGING.get();
}

@Override
@SuppressWarnings("unchecked")
public Recipe<CraftingContainer> delegate()
{
return (Recipe<CraftingContainer>) recipe;
}

public enum Serializer implements RecipeSerializerImpl<ShapedToolDamagingRecipe>
public static class Shaped extends ToolDamagingRecipe
{
INSTANCE;
public Shaped(ResourceLocation id, Recipe<?> recipe)
{
super(id, recipe);
}

@Override
public RecipeSerializer<?> getSerializer()
{
return ModRecipes.SHAPED_TOOL_DAMAGING.get();
}
}

public static class Shapeless extends ToolDamagingRecipe
{
public Shapeless(ResourceLocation id, Recipe<?> recipe)
{
super(id, recipe);
}

@Override
public RecipeSerializer<?> getSerializer()
{
return ModRecipes.SHAPELESS_TOOL_DAMAGING.get();
}
}

public record Serializer<T extends ToolDamagingRecipe>(BiFunction<ResourceLocation, Recipe<?>, T> factory) implements RecipeSerializerImpl<T>
{
@Override
public ShapedToolDamagingRecipe fromJson(ResourceLocation recipeId, JsonObject json, RecipeSerializerImpl.Context context)
public T fromJson(ResourceLocation recipeId, JsonObject json, RecipeSerializerImpl.Context context)
{
return XPlatform.INSTANCE.shapedToolDamagingRecipe(recipeId, RecipeManager.fromJson(recipeId, GsonHelper.getAsJsonObject(json, "recipe")));
return factory.apply(recipeId, context.fromJson(recipeId, GsonHelper.getAsJsonObject(json, "recipe")));
}

@Override
public ShapedToolDamagingRecipe fromNetwork(ResourceLocation recipeId, FriendlyByteBuf buffer)
public T fromNetwork(ResourceLocation recipeId, FriendlyByteBuf buffer)
{
return XPlatform.INSTANCE.shapedToolDamagingRecipe(recipeId, ClientboundUpdateRecipesPacket.fromNetwork(buffer));
return factory.apply(recipeId, ClientboundUpdateRecipesPacket.fromNetwork(buffer));
}

@Override
public void toNetwork(FriendlyByteBuf buffer, ShapedToolDamagingRecipe recipe)
public void toNetwork(FriendlyByteBuf buffer, ToolDamagingRecipe recipe)
{
ClientboundUpdateRecipesPacket.toNetwork(buffer, recipe.delegate());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.alcatrazescapee.notreepunching.platform;

import java.util.ServiceLoader;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Supplier;
import net.minecraft.core.BlockPos;
Expand All @@ -13,7 +12,6 @@
import net.minecraft.world.MenuProvider;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.item.BucketItem;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
Expand All @@ -33,7 +31,7 @@
import com.alcatrazescapee.notreepunching.common.items.CeramicBucketItem;
import com.alcatrazescapee.notreepunching.common.items.MattockItem;
import com.alcatrazescapee.notreepunching.common.recipes.RecipeSerializerImpl;
import com.alcatrazescapee.notreepunching.common.recipes.ShapedToolDamagingRecipe;
import com.alcatrazescapee.notreepunching.common.recipes.ToolDamagingRecipe;
import com.alcatrazescapee.notreepunching.platform.event.BlockEntityFactory;
import com.alcatrazescapee.notreepunching.platform.event.ContainerFactory;

Expand Down Expand Up @@ -76,9 +74,14 @@ default MattockItem mattockItem(Tier tier, float attackDamage, float attackSpeed

<T extends AbstractContainerMenu> MenuType<T> containerType(ContainerFactory<T> factory);

default ShapedToolDamagingRecipe shapedToolDamagingRecipe(ResourceLocation id, Recipe<?> recipe)
default ToolDamagingRecipe shapedToolDamagingRecipe(ResourceLocation id, Recipe<?> recipe)
{
return new ShapedToolDamagingRecipe(id, recipe);
return new ToolDamagingRecipe.Shaped(id, recipe);
}

default ToolDamagingRecipe shapelessToolDamagingRecipe(ResourceLocation id, Recipe<?> recipe)
{
return new ToolDamagingRecipe.Shapeless(id, recipe);
}

// APIs
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"__comment__": "This file was automatically created by mcresources",
"type": "notreepunching:tool_damaging",
"type": "notreepunching:tool_damaging_shaped",
"recipe": {
"type": "minecraft:crafting_shaped",
"pattern": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"__comment__": "This file was automatically created by mcresources",
"type": "notreepunching:tool_damaging",
"type": "notreepunching:tool_damaging_shaped",
"recipe": {
"type": "minecraft:crafting_shaped",
"pattern": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"__comment__": "This file was automatically created by mcresources",
"type": "notreepunching:tool_damaging",
"type": "notreepunching:tool_damaging_shaped",
"recipe": {
"type": "minecraft:crafting_shaped",
"pattern": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"__comment__": "This file was automatically created by mcresources",
"type": "notreepunching:tool_damaging",
"type": "notreepunching:tool_damaging_shaped",
"recipe": {
"type": "minecraft:crafting_shaped",
"pattern": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
{
"__comment__": "This file was automatically created by mcresources",
"type": "minecraft:crafting_shapeless",
"ingredients": [
{
"item": "notreepunching:clay_tool"
},
{
"item": "minecraft:clay_ball"
"type": "notreepunching:tool_damaging_shapeless",
"recipe": {
"type": "minecraft:crafting_shapeless",
"ingredients": [
{
"item": "notreepunching:clay_tool"
},
{
"item": "minecraft:clay_ball"
}
],
"result": {
"item": "notreepunching:clay_brick"
}
],
"result": {
"item": "notreepunching:clay_brick"
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
{
"__comment__": "This file was automatically created by mcresources",
"type": "minecraft:crafting_shapeless",
"ingredients": [
{
"item": "notreepunching:clay_tool"
},
{
"item": "minecraft:clay"
"type": "notreepunching:tool_damaging_shapeless",
"recipe": {
"type": "minecraft:crafting_shapeless",
"ingredients": [
{
"item": "notreepunching:clay_tool"
},
{
"item": "minecraft:clay"
}
],
"result": {
"item": "notreepunching:clay_brick",
"count": 4
}
],
"result": {
"item": "notreepunching:clay_brick",
"count": 4
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"__comment__": "This file was automatically created by mcresources",
"type": "notreepunching:tool_damaging",
"type": "notreepunching:tool_damaging_shaped",
"recipe": {
"type": "minecraft:crafting_shaped",
"pattern": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"__comment__": "This file was automatically created by mcresources",
"type": "notreepunching:tool_damaging",
"type": "notreepunching:tool_damaging_shaped",
"recipe": {
"type": "minecraft:crafting_shaped",
"pattern": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"__comment__": "This file was automatically created by mcresources",
"type": "notreepunching:tool_damaging",
"type": "notreepunching:tool_damaging_shaped",
"recipe": {
"type": "minecraft:crafting_shaped",
"pattern": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"__comment__": "This file was automatically created by mcresources",
"type": "notreepunching:tool_damaging",
"type": "notreepunching:tool_damaging_shaped",
"recipe": {
"type": "minecraft:crafting_shaped",
"pattern": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"__comment__": "This file was automatically created by mcresources",
"type": "notreepunching:tool_damaging",
"type": "notreepunching:tool_damaging_shaped",
"recipe": {
"type": "minecraft:crafting_shaped",
"pattern": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"__comment__": "This file was automatically created by mcresources",
"type": "notreepunching:tool_damaging",
"type": "notreepunching:tool_damaging_shaped",
"recipe": {
"type": "minecraft:crafting_shaped",
"pattern": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"__comment__": "This file was automatically created by mcresources",
"type": "notreepunching:tool_damaging",
"type": "notreepunching:tool_damaging_shaped",
"recipe": {
"type": "minecraft:crafting_shaped",
"pattern": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"__comment__": "This file was automatically created by mcresources",
"type": "notreepunching:tool_damaging",
"type": "notreepunching:tool_damaging_shaped",
"recipe": {
"type": "minecraft:crafting_shaped",
"pattern": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"__comment__": "This file was automatically created by mcresources",
"type": "notreepunching:tool_damaging",
"type": "notreepunching:tool_damaging_shaped",
"recipe": {
"type": "minecraft:crafting_shaped",
"pattern": [
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"__comment__": "This file was automatically created by mcresources",
"type": "notreepunching:tool_damaging",
"type": "notreepunching:tool_damaging_shaped",
"recipe": {
"type": "minecraft:crafting_shaped",
"pattern": [
Expand Down
Loading

0 comments on commit 43c323c

Please sign in to comment.