Skip to content

Commit

Permalink
feat: Update to Minecraft 1.21.2
Browse files Browse the repository at this point in the history
  • Loading branch information
BlayTheNinth committed Oct 11, 2024
1 parent f90b44e commit 75ff48e
Show file tree
Hide file tree
Showing 21 changed files with 155 additions and 89 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
- Fixed refilling stack only refilling a single craft
- Updated to Minecraft 1.21.2
- The Compress key now only works if the mod is installed on the server side
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import net.blay09.mods.craftingtweaks.network.HelloMessage;
import net.blay09.mods.craftingtweaks.network.ModNetworking;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.inventory.CraftingContainer;
import net.minecraft.world.item.crafting.RecipeType;
import net.minecraft.world.item.crafting.ShapedRecipe;
Expand All @@ -34,18 +36,20 @@ public static void initialize() {
Balm.addServerReloadListener(ResourceLocation.fromNamespaceAndPath(MOD_ID, "json_registry"), new JsonCompatLoader());

CraftingTweaksAPI.registerCraftingGridProvider(new VanillaCraftingGridProvider());
CraftingTweaksAPI.registerRecipeMatrixMapper(ShapedRecipe.class, new ShapedRecipeMatrixMapper());
CraftingTweaksAPI.registerRecipeMatrixMapper(ShapelessRecipe.class, new ShapelessRecipeMatrixMapper());
CraftingTweaksAPI.registerRecipeMapper(ShapedRecipe.class, new ShapedRecipeMatrixMapper());
CraftingTweaksAPI.registerRecipeMapper(ShapelessRecipe.class, new ShapelessRecipeMatrixMapper());

Balm.getEvents().onEvent(PlayerLoginEvent.class, event -> Balm.getNetworking().sendTo(event.getPlayer(), new HelloMessage()));
Balm.getEvents().onEvent(ItemCraftedEvent.class, event -> {
final var player = event.getPlayer();
final var level = player.level();
final var craftMatrix = event.getCraftMatrix();
final var recipeManager = level.getRecipeManager();
if (craftMatrix instanceof CraftingContainer craftingContainer) {
final var optionalRecipeHolder = recipeManager.getRecipeFor(RecipeType.CRAFTING, craftingContainer.asCraftInput(), level);
optionalRecipeHolder.ifPresent(recipeHolder -> CraftingTweaksAPI.setLastCraftedRecipe(player, recipeHolder));
if (player instanceof ServerPlayer serverPlayer && level instanceof ServerLevel serverLevel) {
final var craftMatrix = event.getCraftMatrix();
final var recipeManager = serverLevel.getServer().getRecipeManager();
if (craftMatrix instanceof CraftingContainer craftingContainer) {
final var optionalRecipeHolder = recipeManager.getRecipeFor(RecipeType.CRAFTING, craftingContainer.asCraftInput(), level);
optionalRecipeHolder.ifPresent(recipeHolder -> CraftingTweaksAPI.setLastCraftedRecipe(serverPlayer, recipeHolder));
}
}
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package net.blay09.mods.craftingtweaks.api;

import net.minecraft.world.Container;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.item.crafting.RecipeHolder;
Expand Down Expand Up @@ -30,19 +30,19 @@ public static void unregisterCraftingGridProvider(CraftingGridProvider provider)
internalMethods.unregisterCraftingGridProvider(provider);
}

public static <C extends RecipeInput, T extends Recipe<C>> void registerRecipeMatrixMapper(Class<T> recipeClass, RecipeMatrixMapper<T> recipeMatrixMapper) {
internalMethods.registerRecipeMatrixMapper(recipeClass, recipeMatrixMapper);
public static <C extends RecipeInput, T extends Recipe<C>> void registerRecipeMapper(Class<T> recipeClass, RecipeMapper<T> recipeMapper) {
internalMethods.registerRecipeMapper(recipeClass, recipeMapper);
}

public static <C extends RecipeInput, T extends Recipe<C>> RecipeMatrixMapper<T> getRecipeMatrixMapper(Class<T> recipe) {
return internalMethods.getRecipeMatrixMapper(recipe);
public static <C extends RecipeInput, T extends Recipe<C>> RecipeMapper<T> getRecipeMapper(Class<T> recipe) {
return internalMethods.getRecipeMapper(recipe);
}

public static Optional<RecipeHolder<?>> getLastCraftedRecipe(Player player) {
public static Optional<RecipeHolder<?>> getLastCraftedRecipe(ServerPlayer player) {
return internalMethods.getLastCraftedRecipe(player);
}

public static <T extends Recipe<? extends RecipeInput>> void setLastCraftedRecipe(Player player, RecipeHolder<T> recipe) {
public static <T extends Recipe<? extends RecipeInput>> void setLastCraftedRecipe(ServerPlayer player, RecipeHolder<T> recipe) {
internalMethods.setLastCraftedRecipe(player, recipe);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.blay09.mods.craftingtweaks.api;

import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.item.crafting.RecipeHolder;
Expand All @@ -14,7 +15,7 @@ public interface GridRefillHandler<TMenu extends AbstractContainerMenu> {
* @param player the player this is in regard to
* @param menu the menu the grid is part of
*/
default Optional<RecipeHolder<?>> getLastCrafted(CraftingGrid grid, Player player, TMenu menu) {
default Optional<RecipeHolder<?>> getLastCrafted(CraftingGrid grid, ServerPlayer player, TMenu menu) {
return CraftingTweaksAPI.getLastCraftedRecipe(player);
}

Expand All @@ -25,7 +26,7 @@ default Optional<RecipeHolder<?>> getLastCrafted(CraftingGrid grid, Player playe
* @param menu the menu the grid is part of
* @param stack if true, the grid will be filled to produce a stack of the last crafted recipe
*/
default void refillLastCrafted(CraftingGrid grid, Player player, TMenu menu, boolean stack) {
default void refillLastCrafted(CraftingGrid grid, ServerPlayer player, TMenu menu, boolean stack) {
final var lastCrafted = getLastCrafted(grid, player, menu);
lastCrafted.ifPresent(recipeHolder -> refillRecipe(grid, player, menu, recipeHolder, stack));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package net.blay09.mods.craftingtweaks.api;

import net.minecraft.world.Container;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.item.crafting.Recipe;
Expand All @@ -27,11 +27,11 @@ public interface InternalMethods {

GridRefillHandler<AbstractContainerMenu> defaultRefillHandler();

Optional<RecipeHolder<?>> getLastCraftedRecipe(Player player);
Optional<RecipeHolder<?>> getLastCraftedRecipe(ServerPlayer player);

<T extends Recipe<? extends RecipeInput>> void setLastCraftedRecipe(Player player, RecipeHolder<T> recipe);
<T extends Recipe<? extends RecipeInput>> void setLastCraftedRecipe(ServerPlayer player, RecipeHolder<T> recipe);

<C extends RecipeInput, T extends Recipe<C>> void registerRecipeMatrixMapper(Class<T> recipeClass, RecipeMatrixMapper<T> recipeMatrixMapper);
<C extends RecipeInput, T extends Recipe<C>> void registerRecipeMapper(Class<T> recipeClass, RecipeMapper<T> recipeMapper);

<T extends Recipe<? extends RecipeInput>> RecipeMatrixMapper<T> getRecipeMatrixMapper(Class<T> recipeClass);
<T extends Recipe<? extends RecipeInput>> RecipeMapper<T> getRecipeMapper(Class<T> recipeClass);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package net.blay09.mods.craftingtweaks.api;

import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.crafting.Recipe;

import java.util.List;
import java.util.Optional;

public interface RecipeMapper<T extends Recipe<?>> {
int mapToMatrixSlot(T recipe, int ingredientIndex);
List<Optional<Ingredient>> getIngredients(T recipe);
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public void refillRecipe(CraftingGrid grid, Player player, AbstractContainerMenu
outer:
do {
final var ingredientTokens = operation.getIngredientTokens();
final var matrixMapper = CraftingTweaksAPI.getRecipeMatrixMapper(recipe.getClass());
final var matrixMapper = CraftingTweaksAPI.getRecipeMapper(recipe.getClass());

final var matrixDiff = new HashMap<Integer, IngredientToken>();
for (int i = 0; i < ingredientTokens.size(); i++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
import net.blay09.mods.balm.api.Balm;
import net.blay09.mods.craftingtweaks.CraftingTweaksProviderManager;
import net.blay09.mods.craftingtweaks.api.*;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.Container;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.item.crafting.Recipe;
Expand All @@ -17,7 +19,7 @@

public class InternalMethodsImpl implements InternalMethods {

private static final Map<Class<? extends Recipe<?>>, RecipeMatrixMapper<? extends Recipe<?>>> recipeMatrixMappers = new HashMap<>();
private static final Map<Class<? extends Recipe<?>>, RecipeMapper<? extends Recipe<?>>> recipeMatrixMappers = new HashMap<>();

@Override
public void registerCraftingGridProvider(CraftingGridProvider provider) {
Expand Down Expand Up @@ -65,38 +67,38 @@ public GridRefillHandler<AbstractContainerMenu> defaultRefillHandler() {
}

@Override
public Optional<RecipeHolder<?>> getLastCraftedRecipe(Player player) {
final var level = player.level();
final var recipeManager = level.getRecipeManager();
public Optional<RecipeHolder<?>> getLastCraftedRecipe(ServerPlayer player) {
final var level = player.serverLevel();
final var recipeManager = level.getServer().getRecipeManager();
final var persistentData = Balm.getHooks().getPersistentData(player);
final var lastCraftedRecipeId = ResourceLocation.tryParse(persistentData.getString("LastCraftedRecipe"));
if (lastCraftedRecipeId != null) {
return recipeManager.byKey(lastCraftedRecipeId);
return recipeManager.byKey(ResourceKey.create(Registries.RECIPE, lastCraftedRecipeId));
}

return Optional.empty();
}

@Override
public <T extends Recipe<? extends RecipeInput>> void setLastCraftedRecipe(Player player, RecipeHolder<T> recipe) {
public <T extends Recipe<? extends RecipeInput>> void setLastCraftedRecipe(ServerPlayer player, RecipeHolder<T> recipe) {
final var persistentData = Balm.getHooks().getPersistentData(player);
persistentData.putString("LastCraftedRecipe", recipe.id().toString());
persistentData.putString("LastCraftedRecipe", recipe.id().location().toString());
}

@Override
public <C extends RecipeInput, T extends Recipe<C>> void registerRecipeMatrixMapper(Class<T> recipeClass, RecipeMatrixMapper<T> recipeMatrixMapper) {
recipeMatrixMappers.put(recipeClass, recipeMatrixMapper);
public <C extends RecipeInput, T extends Recipe<C>> void registerRecipeMapper(Class<T> recipeClass, RecipeMapper<T> recipeMapper) {
recipeMatrixMappers.put(recipeClass, recipeMapper);
}

@Override
@SuppressWarnings("unchecked")
public <T extends Recipe<? extends RecipeInput>> RecipeMatrixMapper<T> getRecipeMatrixMapper(Class<T> recipeClass) {
public <T extends Recipe<? extends RecipeInput>> RecipeMapper<T> getRecipeMapper(Class<T> recipeClass) {
for (Class<? extends Recipe<?>> handlerClass : recipeMatrixMappers.keySet()) {
if (handlerClass.isAssignableFrom(recipeClass)) {
return (RecipeMatrixMapper<T>) recipeMatrixMappers.get(handlerClass);
return (RecipeMapper<T>) recipeMatrixMappers.get(handlerClass);
}
}

return (RecipeMatrixMapper<T>) recipeMatrixMappers.get(recipeClass);
return (RecipeMapper<T>) recipeMatrixMappers.get(recipeClass);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -433,14 +433,15 @@ public void compress(LocalPlayer player, AbstractContainerMenu menu, CraftingGri

@SuppressWarnings("unchecked")
private static <T extends RecipeInput> ItemStack findMatchingResult(T craftingInventory, LocalPlayer player) {
for (RecipeCollection recipeList : player.getRecipeBook().getCollections()) {
for (RecipeHolder<?> recipe : recipeList.getRecipes()) {
if (recipe.value().getType() == RecipeType.CRAFTING) {
final var craftingRecipe = (Recipe<RecipeInput>) recipe.value();
if (craftingRecipe.matches(craftingInventory, player.level())) {
return craftingRecipe.assemble(craftingInventory, player.level().registryAccess());
}
}
for (final var recipeList : player.getRecipeBook().getCollections()) {
for (final var recipe : recipeList.getRecipes()) {
// TODO Recipes are no longer available on the client
// if (recipe.value().getType() == RecipeType.CRAFTING) {
// final var craftingRecipe = (Recipe<RecipeInput>) recipe.value();
// if (craftingRecipe.matches(craftingInventory, player.level())) {
// return craftingRecipe.assemble(craftingInventory, player.level().registryAccess());
// }
// }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ public static void onScreenDrawn(ContainerScreenDrawEvent.Background event) {
}
for (int x = startX; x <= endX; x++) {
for (int y = startY; y <= endY; y++) {
graphics.setColor(1f, 1f, 1f, 0.1f);
graphics.fillGradient(x, y, x + 16, y + 16, 0x1000FF00, 0x1000FF00);
graphics.setColor(1f, 1f, 1f, 1f);
graphics.fillGradient(x, y, x + 16, y + 16, 0x1900FF00, 0x1900FF00);
}
}
graphics.pose().popPose();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import net.blay09.mods.craftingtweaks.api.ButtonState;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.components.Button;
import net.minecraft.client.renderer.RenderType;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;

Expand All @@ -23,13 +24,12 @@ public GuiImageButton(int x, int y, ButtonProperties properties) {
@Override
public void renderWidget(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTicks) {
isHovered = active && visible && mouseX >= getX() && mouseY >= getY() && mouseX < getX() + width && mouseY < getY() + height;
guiGraphics.setColor(1f, 1f, 1f, 1f);
var state = isHovered ? ButtonState.HOVER : ButtonState.NORMAL;
if(!active) {
state = ButtonState.DISABLED;
}
var stateProperties = properties.getState(state);
guiGraphics.blit(texture, getX(), getY(), stateProperties.getTextureX(), stateProperties.getTextureY(), width, height);
guiGraphics.blit(RenderType::guiTextured, texture, getX(), getY(), stateProperties.getTextureX(), stateProperties.getTextureY(), width, height, 256, 256);
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.blay09.mods.craftingtweaks.crafting;

import it.unimi.dsi.fastutil.ints.IntList;
import net.minecraft.core.Holder;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.item.crafting.RecipeHolder;
Expand All @@ -11,7 +12,7 @@

public class CraftingContext {

private final Map<IntList, Integer> cachedProviderIndexByIngredient = new HashMap<>();
private final Map<List<Holder<Item>>, Integer> cachedProviderIndexByIngredient = new HashMap<>();
private final Map<CraftingOperation.IngredientTokenKey, IngredientCacheHint> cacheHintsByIngredient = new HashMap<>();

private final List<IngredientProvider> ingredientProviders;
Expand All @@ -29,7 +30,7 @@ public List<IngredientProvider> getIngredientProviders() {
}

public int getCachedIngredientProviderIndexFor(Ingredient ingredient) {
return cachedProviderIndexByIngredient.getOrDefault(ingredient.getStackingIds(), -1);
return cachedProviderIndexByIngredient.getOrDefault(ingredient.items(), -1);
}

public IngredientCacheHint getCacheHintFor(CraftingOperation.IngredientTokenKey ingredientTokenKey) {
Expand All @@ -38,6 +39,6 @@ public IngredientCacheHint getCacheHintFor(CraftingOperation.IngredientTokenKey

public void cache(CraftingOperation.IngredientTokenKey ingredientTokenKey, int itemProviderIndex, IngredientCacheHint cacheHint) {
cacheHintsByIngredient.put(ingredientTokenKey, cacheHint);
cachedProviderIndexByIngredient.put(ingredientTokenKey.stackingIds(), itemProviderIndex);
cachedProviderIndexByIngredient.put(ingredientTokenKey.items(), itemProviderIndex);
}
}
Loading

0 comments on commit 75ff48e

Please sign in to comment.