Skip to content

Commit

Permalink
fix: Backport warp plate changes
Browse files Browse the repository at this point in the history
  • Loading branch information
edralzar authored and BlayTheNinth committed Dec 9, 2023
1 parent bcdf920 commit 4f8c955
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 31 deletions.
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ minecraft_version_range=[1.20,1.21)
java_version = 17

# Balm
balm_version = 7.1.0-SNAPSHOT
balm_version_range = [7.0.0,)
balm_version = 7.3.0-SNAPSHOT
balm_version_range = [7.2.0,)

# Forge
forge_version = 46.0.10
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ public static Optional<IWaystone> getBoundWaystone(ItemStack itemStack) {
return __internalMethods.getBoundWaystone(itemStack);
}

public static Optional<IWaystone> setBoundWaystone(ItemStack itemStack, IWaystone waystone) {
return __internalMethods.setBoundWaystone(itemStack, waystone);
public static void setBoundWaystone(ItemStack itemStack, IWaystone waystone) {
__internalMethods.setBoundWaystone(itemStack, waystone);
}

public static ItemStack createAttunedShard(IWaystone warpPlate) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import net.minecraft.world.inventory.ContainerData;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.crafting.RecipeHolder;
import net.minecraft.world.level.ServerLevelAccessor;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.AABB;
Expand Down Expand Up @@ -141,8 +140,7 @@ public void initializeWaystone(ServerLevelAccessor world, @Nullable LivingEntity
private void initializeInventory(ServerLevelAccessor levelAccessor) {
WarpPlateRecipe initializingRecipe = levelAccessor.getLevel().getRecipeManager().getAllRecipesFor(ModRecipes.warpPlateRecipeType)
.stream()
.filter(holder -> holder.id().getNamespace().equals(Waystones.MOD_ID) && holder.id().getPath().equals("attuned_shard"))
.map(RecipeHolder::value)
.filter(recipe -> recipe.getId().getNamespace().equals(Waystones.MOD_ID) && recipe.getId().getPath().equals("attuned_shard"))
.findFirst()
.orElse(null);
if (initializingRecipe == null) {
Expand Down Expand Up @@ -387,8 +385,7 @@ private WarpPlateRecipe trySelectRecipe() {
return null; //prevents crafting when more than 1 ingredient is present
}

return level.getRecipeManager().getRecipeFor(ModRecipes.warpPlateRecipeType, this, level)
.map(RecipeHolder::value).orElse(null);
return level.getRecipeManager().getRecipeFor(ModRecipes.warpPlateRecipeType, this, level).orElse(null);
}

public ItemStack getTargetAttunementStack() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,35 @@
package net.blay09.mods.waystones.recipe;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.blay09.mods.waystones.block.ModBlocks;
import net.minecraft.core.NonNullList;
import net.minecraft.core.RegistryAccess;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.GsonHelper;
import net.minecraft.world.Container;
import net.minecraft.world.entity.player.StackedContents;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.*;
import net.minecraft.world.level.Level;

import java.util.Map;

public class WarpPlateRecipe implements Recipe<Container> {

private final ResourceLocation id;
private final ItemStack resultItem;
private final Ingredient primaryIngredient;
private final NonNullList<Ingredient> secondaryIngredients;
private final NonNullList<Ingredient> combinedIngredients;

public WarpPlateRecipe(ItemStack resultItem, Ingredient primaryIngredient, NonNullList<Ingredient> secondaryIngredients) {
public WarpPlateRecipe(ResourceLocation id, ItemStack resultItem, Ingredient primaryIngredient, NonNullList<Ingredient> secondaryIngredients) {
this.id = id;
this.resultItem = resultItem;

this.primaryIngredient = primaryIngredient;
Expand Down Expand Up @@ -96,39 +105,49 @@ public RecipeType<?> getType() {
return ModRecipes.warpPlateRecipeType;
}

static class Serializer implements RecipeSerializer<WarpPlateRecipe> {
@Override
public ResourceLocation getId() {
return id;
}

private static final Codec<WarpPlateRecipe> CODEC = RecordCodecBuilder.create(instance -> instance.group(
CraftingRecipeCodecs.ITEMSTACK_OBJECT_CODEC.fieldOf("result").forGetter(recipe -> recipe.resultItem),
Ingredient.CODEC.fieldOf("primary").forGetter(recipe -> recipe.primaryIngredient),
Ingredient.CODEC.listOf().fieldOf("secondary")
.flatXmap(secondary -> {
Ingredient[] ingredients = secondary.stream().filter((ingredient) -> !ingredient.isEmpty()).toArray(Ingredient[]::new);
if (ingredients.length == 0) {
return DataResult.error(() -> "No secondary ingredients for warp plate recipe");
} else {
return ingredients.length > 4 ? DataResult.error(() -> "Too many secondary ingredients for warp plate recipe") : DataResult.success(
NonNullList.of(Ingredient.EMPTY, ingredients));
}
}, DataResult::success)
.forGetter(recipe -> recipe.secondaryIngredients)
)
.apply(instance, WarpPlateRecipe::new));
static class Serializer implements RecipeSerializer<WarpPlateRecipe> {

@Override
public Codec<WarpPlateRecipe> codec() {
return CODEC;
public WarpPlateRecipe fromJson(ResourceLocation id, JsonObject jsonObject) {
Ingredient primaryIngredient = Ingredient.fromJson(GsonHelper.getAsJsonObject(jsonObject, "primary"));
NonNullList<Ingredient> secondaryIngredients = itemsFromJson(GsonHelper.getAsJsonArray(jsonObject, "secondary"));
if (secondaryIngredients.isEmpty()) {
throw new JsonParseException("No secondary ingredients for warp plate recipe");
} else if (secondaryIngredients.size() > 4) {
throw new JsonParseException("Too many secondary ingredients for shapeless recipe");
} else {
ItemStack resultItem = ShapedRecipe.itemStackFromJson(GsonHelper.getAsJsonObject(jsonObject, "result"));
return new WarpPlateRecipe(id, resultItem, primaryIngredient, secondaryIngredients);
}
}

private static NonNullList<Ingredient> itemsFromJson(JsonArray jsonArray) {
NonNullList<Ingredient> ingredients = NonNullList.create();

for(int i = 0; i < jsonArray.size(); ++i) {
Ingredient ingredient = Ingredient.fromJson(jsonArray.get(i), false);
if (!ingredient.isEmpty()) {
ingredients.add(ingredient);
}
}

return ingredients;
}

@Override
public WarpPlateRecipe fromNetwork(FriendlyByteBuf buf) {
public WarpPlateRecipe fromNetwork(ResourceLocation id, FriendlyByteBuf buf) {
final var resultItem = buf.readItem();
final var primaryIngredient = Ingredient.fromNetwork(buf);
final NonNullList<Ingredient> secondaryIngredients = NonNullList.createWithCapacity(4);
for (int i = 0; i < secondaryIngredients.size(); i++) {
secondaryIngredients.add(Ingredient.fromNetwork(buf));
}
return new WarpPlateRecipe(resultItem, primaryIngredient, secondaryIngredients);
return new WarpPlateRecipe(id, resultItem, primaryIngredient, secondaryIngredients);
}

@Override
Expand Down

0 comments on commit 4f8c955

Please sign in to comment.