Small tweaks and fixes

This commit is contained in:
oitsjustjose 2023-12-03 16:03:50 -06:00
commit 8b7171378c
6 changed files with 57 additions and 11 deletions

View file

@ -2,7 +2,9 @@ package com.oitsjustjose.itemgator;
import com.oitsjustjose.itemgator.common.data.RecipeTypeRegistry;
import net.minecraft.world.item.ItemStack;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.AddReloadListenerEvent;
import net.minecraftforge.event.TickEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
@ -28,6 +30,11 @@ public class ItemGator {
CustomRecipeRegistry.SERIALIZERS.register(FMLJavaModLoadingContext.get().getModEventBus());
}
@SubscribeEvent
public void onReload(AddReloadListenerEvent event) {
CustomRecipeRegistry.reset();
}
@SubscribeEvent
public void onTick(TickEvent.PlayerTickEvent event) {
if (event.player.isCreative()) return;
@ -45,7 +52,8 @@ public class ItemGator {
var shouldReplace = allRecipes.parallelStream().filter(x -> x.matchesOriginal(stack) && x.appliesTo(event.player)).findFirst();
if (shouldReplace.isPresent()) {
var sub = shouldReplace.get().getSubstitute(stack);
inventory.setItem(i, sub);
inventory.setItem(i, ItemStack.EMPTY);
inventory.add(sub);
continue;
}
@ -53,7 +61,8 @@ public class ItemGator {
var shouldRevert = allRecipes.parallelStream().filter(x -> x.matchesSubstitute(stack) && !x.appliesTo(event.player)).findFirst();
if (shouldRevert.isPresent()) {
var orig = shouldRevert.get().getOriginal(stack);
inventory.setItem(i, orig);
inventory.setItem(i, ItemStack.EMPTY);
inventory.add(orig);
}
}
});

View file

@ -46,6 +46,10 @@ public class RecipeTypeRegistry {
return this.allRecipes;
}
public void reset() {
this.allRecipes.clear();
}
public void registerRecipe(BaseRecipe recipe) {
this.allRecipes.add(recipe);
}

View file

@ -13,6 +13,7 @@ import net.minecraft.world.item.crafting.RecipeType;
import net.minecraft.world.level.Level;
import net.minecraftforge.items.wrapper.RecipeWrapper;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
@ -21,12 +22,14 @@ public class IngredientRecipe extends BaseRecipe implements Recipe<RecipeWrapper
private final Ingredient ingredient;
private final ItemStack substitute;
private final List<String> tags;
private final @Nullable Ingredient exceptions;
public IngredientRecipe(ResourceLocation id, Ingredient ingredient, ItemStack substitute, List<String> tags) {
public IngredientRecipe(ResourceLocation id, Ingredient ingredient, ItemStack substitute, List<String> tags, @Nullable Ingredient exceptions) {
this.id = id;
this.ingredient = ingredient;
this.substitute = substitute;
this.tags = tags;
this.exceptions = exceptions;
ItemGator.getInstance().CustomRecipeRegistry.registerRecipe(this);
}
@ -68,6 +71,7 @@ public class IngredientRecipe extends BaseRecipe implements Recipe<RecipeWrapper
@Override
public boolean matchesOriginal(ItemStack stackIn) {
if (exceptions != null && exceptions.test(stackIn)) return false;
return this.ingredient.test(stackIn);
}
@ -127,4 +131,6 @@ public class IngredientRecipe extends BaseRecipe implements Recipe<RecipeWrapper
public ItemStack getPlainSubstitute() {
return this.substitute.copy();
}
public @Nullable Ingredient getExceptions() { return this.exceptions; }
}

View file

@ -28,7 +28,8 @@ public class IngredientRecipeSerializer implements RecipeSerializer<IngredientRe
tags.add(obj.get("tag").getAsString());
}
return new IngredientRecipe(id, input, substitute, tags);
var exceptions = obj.has("exceptions") ? Ingredient.fromJson(obj.get("exceptions"), false) : null;
return new IngredientRecipe(id, input, substitute, tags, exceptions);
}
@Override
@ -36,6 +37,12 @@ public class IngredientRecipeSerializer implements RecipeSerializer<IngredientRe
CraftingHelper.write(buf, recipe.getIngredient());
buf.writeItemStack(recipe.getPlainSubstitute(), false);
buf.writeUtf(String.join(",", recipe.getTags()));
var exceptions = recipe.getExceptions();
buf.writeBoolean(exceptions != null);
if (exceptions != null) {
CraftingHelper.write(buf, exceptions);
}
}
@Override
@ -43,6 +50,9 @@ public class IngredientRecipeSerializer implements RecipeSerializer<IngredientRe
var ingredient = Ingredient.fromNetwork(buf);
var substitute = buf.readItem();
var tags = Arrays.stream(buf.readUtf().split(",")).toList();
return new IngredientRecipe(id, ingredient, substitute, tags);
var hasExceptions = buf.readBoolean();
var exceptions = hasExceptions ? Ingredient.fromNetwork(buf) : null;
return new IngredientRecipe(id, ingredient, substitute, tags, exceptions);
}
}

View file

@ -6,6 +6,7 @@ import net.minecraft.core.RegistryAccess;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.item.crafting.RecipeSerializer;
import net.minecraft.world.item.crafting.RecipeType;
@ -13,6 +14,7 @@ import net.minecraft.world.level.Level;
import net.minecraftforge.items.wrapper.RecipeWrapper;
import net.minecraftforge.registries.ForgeRegistries;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
@ -21,12 +23,14 @@ public class ModRecipe extends BaseRecipe implements Recipe<RecipeWrapper> {
private final String mod;
private final ItemStack substitute;
private final List<String> tags;
private final @Nullable Ingredient exceptions;
public ModRecipe(ResourceLocation id, String mod, ItemStack substitute, List<String> tags) {
public ModRecipe(ResourceLocation id, String mod, ItemStack substitute, List<String> tags, @Nullable Ingredient exceptions) {
this.id = id;
this.mod = mod;
this.substitute = substitute;
this.tags = tags;
this.exceptions = exceptions;
ItemGator.getInstance().CustomRecipeRegistry.registerRecipe(this);
}
@ -68,6 +72,7 @@ public class ModRecipe extends BaseRecipe implements Recipe<RecipeWrapper> {
@Override
public boolean matchesOriginal(ItemStack stackIn) {
if (exceptions != null && exceptions.test(stackIn)) return false;
var rl = ForgeRegistries.ITEMS.getKey(stackIn.getItem());
return rl != null && rl.getNamespace().equals(this.mod);
}
@ -125,7 +130,7 @@ public class ModRecipe extends BaseRecipe implements Recipe<RecipeWrapper> {
return mod;
}
public ItemStack getPlainSubstitute() {
return this.substitute.copy();
}
public ItemStack getPlainSubstitute() { return this.substitute.copy(); }
public @Nullable Ingredient getExceptions() { return this.exceptions; }
}

View file

@ -3,8 +3,10 @@ package com.oitsjustjose.itemgator.common.data.mod;
import com.google.gson.JsonObject;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.crafting.RecipeSerializer;
import net.minecraft.world.item.crafting.ShapedRecipe;
import net.minecraftforge.common.crafting.CraftingHelper;
import org.apache.commons.compress.utils.Lists;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@ -26,7 +28,8 @@ public class ModRecipeSerializer implements RecipeSerializer<ModRecipe> {
tags.add(obj.get("tag").getAsString());
}
return new ModRecipe(id, mod, substitute, tags);
var exceptions = obj.has("exceptions") ? Ingredient.fromJson(obj.get("exceptions"), false) : null;
return new ModRecipe(id, mod, substitute, tags, exceptions);
}
@Override
@ -34,6 +37,12 @@ public class ModRecipeSerializer implements RecipeSerializer<ModRecipe> {
buf.writeUtf(recipe.getMod());
buf.writeItemStack(recipe.getPlainSubstitute(), false);
buf.writeUtf(String.join(",", recipe.getTags()));
var exceptions = recipe.getExceptions();
buf.writeBoolean(exceptions != null);
if (exceptions != null) {
CraftingHelper.write(buf, exceptions);
}
}
@Override
@ -41,6 +50,9 @@ public class ModRecipeSerializer implements RecipeSerializer<ModRecipe> {
var mod = buf.readUtf();
var substitute = buf.readItem();
var tags = Arrays.stream(buf.readUtf().split(",")).toList();
return new ModRecipe(id, mod, substitute, tags);
var hasExceptions = buf.readBoolean();
var exceptions = hasExceptions ? Ingredient.fromNetwork(buf) : null;
return new ModRecipe(id, mod, substitute, tags, exceptions);
}
}