Skip to content

Commit bc6ffd3

Browse files
committed
Add system for wrapping registry entries.
1 parent 2baaa2e commit bc6ffd3

File tree

4 files changed

+132
-0
lines changed

4 files changed

+132
-0
lines changed
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package net.darkhax.bookshelf.api.data.recipes;
2+
3+
import com.google.gson.JsonObject;
4+
import net.minecraft.network.FriendlyByteBuf;
5+
import net.minecraft.resources.ResourceLocation;
6+
import net.minecraft.world.item.crafting.Recipe;
7+
import net.minecraft.world.item.crafting.RecipeSerializer;
8+
9+
/**
10+
* This interface is a rough copy of {@link net.minecraft.world.item.crafting.RecipeSerializer}. Some platforms like
11+
* Forge modify the class hierarchy of the vanilla interface which creates compile time problems. This interface gets
12+
* around these issues by decoupling the neutral code. Platform specific wrappers are provided to bring this back to the
13+
* vanilla type.
14+
*
15+
* @param <T> The type of recipe serialized by the serializer.
16+
*/
17+
public abstract class IRecipeSerializer<T extends Recipe<?>> {
18+
19+
private RecipeSerializer<?> wrapper;
20+
21+
public void setWrapper(RecipeSerializer<?> wrapper) {
22+
23+
this.wrapper = wrapper;
24+
}
25+
26+
public RecipeSerializer<?> getWrapper() {
27+
28+
return this.wrapper;
29+
}
30+
31+
/**
32+
* Reads a recipe from JSON data.
33+
*
34+
* @param recipeId The ID to assign the recipe.
35+
* @param json The JSON data.
36+
* @return The recipe that was deserialized.
37+
*/
38+
public abstract T fromJson(ResourceLocation recipeId, JsonObject json);
39+
40+
/**
41+
* Reads a recipe from a network buffer.
42+
*
43+
* @param recipeId The ID to assign the recipe.
44+
* @param buffer The network buffer.
45+
* @return The recipe that was deserialized.
46+
*/
47+
public abstract T fromNetwork(ResourceLocation recipeId, FriendlyByteBuf buffer);
48+
49+
/**
50+
* Writes a recipe to a network buffer.
51+
*
52+
* @param buffer The network buffer to write into.
53+
* @param toWrite The recipe to write.
54+
*/
55+
public abstract void toNetwork(FriendlyByteBuf buffer, T toWrite);
56+
}

Common/src/main/java/net/darkhax/bookshelf/api/registry/IRegistryEntries.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import javax.annotation.Nullable;
66
import java.util.Map;
77
import java.util.function.BiConsumer;
8+
import java.util.function.BiFunction;
89
import java.util.function.Consumer;
910
import java.util.function.Supplier;
1011

@@ -18,6 +19,8 @@ public interface IRegistryEntries<V> extends Iterable<V> {
1819

1920
void addRegistryListener(BiConsumer<ResourceLocation, V> listener);
2021

22+
void addRegistryWrapper(BiFunction<ResourceLocation, V, V> wrapperFunc);
23+
2124
void build(BiConsumer<ResourceLocation, V> registerFunc);
2225

2326
@Nullable
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package net.darkhax.bookshelf.impl.data.recipes;
2+
3+
import com.google.gson.JsonObject;
4+
import net.darkhax.bookshelf.api.data.recipes.IRecipeSerializer;
5+
import net.minecraft.network.FriendlyByteBuf;
6+
import net.minecraft.resources.ResourceLocation;
7+
import net.minecraft.world.item.crafting.Recipe;
8+
import net.minecraft.world.item.crafting.RecipeSerializer;
9+
10+
public class WrappedRecipeSerializer<T extends Recipe<?>> implements RecipeSerializer<T> {
11+
12+
private final IRecipeSerializer<T> delegate;
13+
14+
public WrappedRecipeSerializer(IRecipeSerializer<T> delegate) {
15+
16+
this.delegate = delegate;
17+
}
18+
19+
@Override
20+
public T fromJson(ResourceLocation recipeId, JsonObject json) {
21+
22+
return this.delegate.fromJson(recipeId, json);
23+
}
24+
25+
@Override
26+
public T fromNetwork(ResourceLocation recipeId, FriendlyByteBuf buffer) {
27+
28+
return this.delegate.fromNetwork(recipeId, buffer);
29+
}
30+
31+
@Override
32+
public void toNetwork(FriendlyByteBuf buffer, T toWrite) {
33+
34+
this.delegate.toNetwork(buffer, toWrite);
35+
}
36+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package net.darkhax.bookshelf.impl.data.recipes;
2+
3+
import com.google.gson.JsonObject;
4+
import net.darkhax.bookshelf.api.data.recipes.IRecipeSerializer;
5+
import net.minecraft.network.FriendlyByteBuf;
6+
import net.minecraft.resources.ResourceLocation;
7+
import net.minecraft.world.item.crafting.Recipe;
8+
import net.minecraft.world.item.crafting.RecipeSerializer;
9+
import net.minecraftforge.registries.ForgeRegistryEntry;
10+
11+
public class WrappedRecipeSerializer<T extends Recipe<?>> extends ForgeRegistryEntry<RecipeSerializer<?>> implements RecipeSerializer<T> {
12+
13+
private final IRecipeSerializer<T> delegate;
14+
15+
public WrappedRecipeSerializer(IRecipeSerializer<T> delegate) {
16+
17+
this.delegate = delegate;
18+
}
19+
20+
@Override
21+
public T fromJson(ResourceLocation recipeId, JsonObject json) {
22+
23+
return delegate.fromJson(recipeId, json);
24+
}
25+
26+
@Override
27+
public T fromNetwork(ResourceLocation recipeId, FriendlyByteBuf byteBuf) {
28+
29+
return delegate.fromNetwork(recipeId, byteBuf);
30+
}
31+
32+
@Override
33+
public void toNetwork(FriendlyByteBuf byteBuf, T toWrite) {
34+
35+
delegate.toNetwork(byteBuf, toWrite);
36+
}
37+
}

0 commit comments

Comments
 (0)