Skip to content

Commit

Permalink
wip: Continue port to Minecraft 1.21.3
Browse files Browse the repository at this point in the history
  • Loading branch information
BlayTheNinth committed Oct 24, 2024
1 parent 4f269e2 commit b5eefd4
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public Component getTooltip() {

@Override
public Comparator<RecipeWithStatus> getComparator(Player player) {
return new ComparatorName();
return new ComparatorName(player);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

import java.util.*;

public class CraftingOperation {
public class CraftingOperation<C extends RecipeInput, T extends Recipe<C>> {

public record IngredientTokenKey(int providerIndex, List<Holder<Item>> items) {
@Override
Expand All @@ -39,7 +39,7 @@ public int hashCode() {
}

private final CraftingContext context;
private final Recipe<?> recipe;
private final T recipe;

private final Multimap<IngredientTokenKey, IngredientToken> tokensByIngredient = ArrayListMultimap.create();
private final List<IngredientToken> ingredientTokens = new ArrayList<>();
Expand All @@ -48,17 +48,17 @@ public int hashCode() {
private NonNullList<ItemStack> lockedInputs;
private int missingIngredientsMask;

public CraftingOperation(final CraftingContext context, RecipeHolder<Recipe<?>> recipe) {
public CraftingOperation(final CraftingContext context, RecipeHolder<T> recipe) {
this.context = context;
this.recipe = recipe.value();
}

public CraftingOperation withLockedInputs(@Nullable NonNullList<ItemStack> lockedInputs) {
public CraftingOperation<C, T> withLockedInputs(@Nullable NonNullList<ItemStack> lockedInputs) {
this.lockedInputs = lockedInputs;
return this;
}

public CraftingOperation prepare() {
public CraftingOperation<C, T> prepare() {
tokensByIngredient.clear();
ingredientTokens.clear();
missingIngredients.clear();
Expand Down Expand Up @@ -148,7 +148,7 @@ public ItemStack craft(AbstractContainerMenu menu, RegistryAccess registryAccess
return craft(menu, registryAccess, recipe);
}

private <C extends RecipeInput, T extends Recipe<C>> ItemStack craft(AbstractContainerMenu menu, RegistryAccess registryAccess, T recipe) {
private ItemStack craft(AbstractContainerMenu menu, RegistryAccess registryAccess, T recipe) {
final var recipeTypeHandler = CookingForBlockheadsRegistry.getKitchenRecipeHandler(recipe);
if (recipeTypeHandler == null) {
return ItemStack.EMPTY;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@

public class KitchenMenu extends AbstractContainerMenu {

private final Player player;
public final Player player;
private final KitchenImpl kitchen;

private final List<CraftableListingFakeSlot> recipeListingSlots = new ArrayList<>();
Expand All @@ -46,7 +46,7 @@ public class KitchenMenu extends AbstractContainerMenu {
private final List<RecipeWithStatus> filteredCraftables = new ArrayList<>();

private String currentSearch;
private Comparator<RecipeWithStatus> currentSorting = new ComparatorName();
private Comparator<RecipeWithStatus> currentSorting;

private List<RecipeWithStatus> craftables = new ArrayList<>();

Expand All @@ -65,11 +65,13 @@ public KitchenMenu(MenuType<KitchenMenu> containerType, int windowId, Player pla
this.player = player;
this.kitchen = kitchen;

currentSorting = new ComparatorName(player);

final var fakeInventory = new DefaultContainer(4 * 3 + 3 * 3);

for (int i = 0; i < 4; i++) {
for (int j = 0; j < 3; j++) {
final var slot = new CraftableListingFakeSlot(fakeInventory, j + i * 3, 102 + j * 18, 11 + i * 18);
final var slot = new CraftableListingFakeSlot(this, fakeInventory, j + i * 3, 102 + j * 18, 11 + i * 18);
recipeListingSlots.add(slot);
addSlot(slot);
}
Expand Down Expand Up @@ -528,8 +530,7 @@ public RecipeWithStatus getSelectedRecipe() {
public boolean isSelectedSlot(CraftableListingFakeSlot slot) {
final var selectedRecipe = getSelectedRecipe();
final var craftable = selectedRecipe != null ? selectedRecipe : selectedCraftable;
return craftable != null && slot.getCraftable() != null && ItemStack.isSameItemSameComponents(slot.getCraftable().resultItem(),
craftable.resultItem());
return craftable != null && slot.getCraftable() != null && slot.getCraftable().recipeDisplayEntry().id().equals(craftable.recipeDisplayEntry().id());
}

public boolean isScrollOffsetDirty() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,27 @@
import net.blay09.mods.cookingforblockheads.crafting.RecipeWithStatus;
import net.minecraft.core.component.DataComponents;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.crafting.display.SlotDisplayContext;

import java.util.Comparator;

public class ComparatorHunger implements Comparator<RecipeWithStatus> {

private final ComparatorName fallback = new ComparatorName();
private final ComparatorName fallback;
private final Player player;

public ComparatorHunger(Player player) {
this.fallback = new ComparatorName(player);
this.player = player;
}

@Override
public int compare(RecipeWithStatus o1, RecipeWithStatus o2) {
boolean isFirstFood = o1.resultItem().has(DataComponents.FOOD);
boolean isSecondFood = o2.resultItem().has(DataComponents.FOOD);
final var contextMap = SlotDisplayContext.fromLevel(player.level());
final var firstItem = o1.recipeDisplayEntry().display().result().resolveForFirstStack(contextMap);
final var secondItem = o2.recipeDisplayEntry().display().result().resolveForFirstStack(contextMap);
boolean isFirstFood = firstItem.has(DataComponents.FOOD);
boolean isSecondFood = secondItem.has(DataComponents.FOOD);
if (!isFirstFood && !isSecondFood) {
return fallback.compare(o1, o2);
} else if (!isFirstFood) {
Expand All @@ -28,7 +33,8 @@ public int compare(RecipeWithStatus o1, RecipeWithStatus o2) {
return -1;
}

int result = CookingForBlockheadsAPI.getFoodStatsProvider().getNutrition(o2.resultItem(), player) - CookingForBlockheadsAPI.getFoodStatsProvider().getNutrition(o1.resultItem(), player);
int result = CookingForBlockheadsAPI.getFoodStatsProvider().getNutrition(secondItem, player)
- CookingForBlockheadsAPI.getFoodStatsProvider().getNutrition(firstItem, player);
if (result == 0) {
return fallback.compare(o1, o2);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
package net.blay09.mods.cookingforblockheads.menu.comparator;

import net.blay09.mods.cookingforblockheads.crafting.RecipeWithStatus;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.crafting.display.SlotDisplayContext;

import java.util.Comparator;

public class ComparatorName implements Comparator<RecipeWithStatus> {

private final Player player;

public ComparatorName(Player player) {
this.player = player;
}

@Override
public int compare(RecipeWithStatus o1, RecipeWithStatus o2) {
String s1 = o1.resultItem().getDisplayName().getString();
String s2 = o2.resultItem().getDisplayName().getString();
final var contextMap = SlotDisplayContext.fromLevel(player.level());
final var firstItem = o1.recipeDisplayEntry().display().result().resolveForFirstStack(contextMap);
final var secondItem = o2.recipeDisplayEntry().display().result().resolveForFirstStack(contextMap);
String s1 = firstItem.getDisplayName().getString();
String s2 = secondItem.getDisplayName().getString();
return s1.compareToIgnoreCase(s2);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,27 @@
import net.blay09.mods.cookingforblockheads.crafting.RecipeWithStatus;
import net.minecraft.core.component.DataComponents;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.crafting.display.SlotDisplayContext;

import java.util.Comparator;

public class ComparatorSaturation implements Comparator<RecipeWithStatus> {

private final ComparatorName fallback = new ComparatorName();
private final Player entityPlayer;
private final ComparatorName fallback;
private final Player player;

public ComparatorSaturation(Player entityPlayer) {
this.entityPlayer = entityPlayer;
public ComparatorSaturation(Player player) {
this.fallback = new ComparatorName(player);
this.player = player;
}

@Override
public int compare(RecipeWithStatus o1, RecipeWithStatus o2) {
boolean isFirstFood = o1.resultItem().has(DataComponents.FOOD);
boolean isSecondFood = o2.resultItem().has(DataComponents.FOOD);
final var contextMap = SlotDisplayContext.fromLevel(player.level());
final var firstItem = o1.recipeDisplayEntry().display().result().resolveForFirstStack(contextMap);
final var secondItem = o2.recipeDisplayEntry().display().result().resolveForFirstStack(contextMap);
boolean isFirstFood = firstItem.has(DataComponents.FOOD);
boolean isSecondFood = secondItem.has(DataComponents.FOOD);
if (!isFirstFood && !isSecondFood) {
return fallback.compare(o1, o2);
} else if (!isFirstFood) {
Expand All @@ -29,7 +34,8 @@ public int compare(RecipeWithStatus o1, RecipeWithStatus o2) {
}

final var foodStatsProvider = CookingForBlockheadsAPI.getFoodStatsProvider();
int result = (int) (foodStatsProvider.getSaturationModifier(o2.resultItem(), entityPlayer) * 100 - foodStatsProvider.getSaturationModifier(o1.resultItem(), entityPlayer) * 100);
int result = (int) (foodStatsProvider.getSaturationModifier(secondItem, player) * 100
- foodStatsProvider.getSaturationModifier(firstItem, player) * 100);
if (result == 0) {
return fallback.compare(o1, o2);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
package net.blay09.mods.cookingforblockheads.menu.slot;

import net.blay09.mods.cookingforblockheads.crafting.RecipeWithStatus;
import net.blay09.mods.cookingforblockheads.menu.KitchenMenu;
import net.minecraft.world.Container;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.display.SlotDisplayContext;
import org.jetbrains.annotations.Nullable;

public class CraftableListingFakeSlot extends AbstractFakeSlot {

private final KitchenMenu menu;
private RecipeWithStatus craftable;

public CraftableListingFakeSlot(Container container, int slotId, int x, int y) {
public CraftableListingFakeSlot(KitchenMenu menu, Container container, int slotId, int x, int y) {
super(container, slotId, x, y);
this.menu = menu;
}

@Override
public ItemStack getItem() {
return craftable != null ? craftable.resultItem() : ItemStack.EMPTY;
if (craftable != null) {
final var contextMap = SlotDisplayContext.fromLevel(menu.player.level());
return craftable.recipeDisplayEntry().display().result().resolveForFirstStack(contextMap);
}
return ItemStack.EMPTY;
}

@Override
Expand All @@ -37,5 +46,9 @@ public RecipeWithStatus getCraftable() {
return craftable;
}

@Override
public boolean isFake() {
return true;
}
}

0 comments on commit b5eefd4

Please sign in to comment.