Skip to content

Commit

Permalink
refactor(craft): refactor IngredientHolder and CurrentRecipe
Browse files Browse the repository at this point in the history
  • Loading branch information
Siroshun09 committed Apr 15, 2024
1 parent eab24f8 commit f06e0fa
Show file tree
Hide file tree
Showing 8 changed files with 164 additions and 203 deletions.
Original file line number Diff line number Diff line change
@@ -1,39 +1,35 @@
package net.okocraft.box.feature.craft.gui;

import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import net.okocraft.box.api.model.item.BoxItem;
import net.okocraft.box.feature.craft.model.BoxIngredientItem;
import net.okocraft.box.feature.craft.model.BoxItemRecipe;
import net.okocraft.box.feature.craft.model.IngredientHolder;
import net.okocraft.box.feature.craft.model.SelectedRecipe;
import net.okocraft.box.feature.gui.api.session.TypedKey;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Map;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class CurrentRecipe {

public static final TypedKey<CurrentRecipe> DATA_KEY = TypedKey.of(CurrentRecipe.class, "current_recipe");
public static final TypedKey<Boolean> CHANGE_PER_INGREDIENT = TypedKey.of(Boolean.class, "change_per_ingredient");

private final BoxItemRecipe source;
private final Map<Integer, IngredientHolder.SelectableIngredients> ingredientsMap;
private final Int2ObjectMap<SelectableIngredients> ingredientsMap = new Int2ObjectOpenHashMap<>();

private SelectedRecipe selectedRecipe;
private Comparator<BoxIngredientItem> ingredientSorter;

public CurrentRecipe(@NotNull BoxItemRecipe source) {
this.source = source;
this.ingredientsMap =
source.ingredients().stream()
.filter(Predicate.not(ingredients -> ingredients.getPatterns().isEmpty()))
.collect(Collectors.toUnmodifiableMap(
IngredientHolder::getSlot,
IngredientHolder::toSelectableIngredients
));

updateSelectedRecipe();
this.resetIngredientMap();
this.updateSelectedRecipe();
}

public @NotNull BoxItem getResult() {
Expand Down Expand Up @@ -64,14 +60,14 @@ public void nextRecipe(int pos, boolean sameIngredient) {
updateSelectedRecipe();
}

public @Nullable IngredientHolder.SelectableIngredients getIngredients(int pos) {
public @Nullable SelectableIngredients getIngredients(int pos) {
return ingredientsMap.get(pos);
}

public void updateSelectedRecipe() {
this.selectedRecipe =
new SelectedRecipe(
ingredientsMap.values().stream().map(IngredientHolder.SelectableIngredients::getSelected).toList(),
ingredientsMap.values().stream().map(SelectableIngredients::getSelected).toList(),
source.result(),
source.amount()
);
Expand All @@ -80,4 +76,29 @@ public void updateSelectedRecipe() {
public @NotNull SelectedRecipe getSelectedRecipe() {
return selectedRecipe;
}

public void changeIngredientSorter(Comparator<BoxIngredientItem> ingredientSorter) {
this.ingredientSorter = ingredientSorter;
this.resetIngredientMap();
this.updateSelectedRecipe();
}

private void resetIngredientMap() {
for (var holder : this.source.ingredients()) {
if (holder.patterns().isEmpty()) {
continue;
}

List<BoxIngredientItem> patterns;

if (this.ingredientSorter != null) {
patterns = new ArrayList<>(holder.patterns());
patterns.sort(this.ingredientSorter);
} else {
patterns = holder.patterns();
}

this.ingredientsMap.put(holder.slot(), new SelectableIngredients(holder, patterns));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package net.okocraft.box.feature.craft.gui;

import net.okocraft.box.feature.craft.model.BoxIngredientItem;
import net.okocraft.box.feature.craft.model.IngredientHolder;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Unmodifiable;

import java.util.List;

/**
* A class for handling multiple {@link BoxIngredientItem}s that can be selected
*/
public class SelectableIngredients {

private final IngredientHolder ingredientHolder;
private final List<BoxIngredientItem> patterns;

private int selected;

public SelectableIngredients(@NotNull IngredientHolder ingredientHolder, @NotNull List<BoxIngredientItem> patterns) {
this.ingredientHolder = ingredientHolder;
this.patterns = patterns;
}

/**
* Gets the currently selected {@link BoxIngredientItem}.
*
* @return the currently selected {@link BoxIngredientItem}
*/
public @NotNull BoxIngredientItem getSelected() {
return patterns.get(selected);
}

/**
* Selects the next {@link BoxIngredientItem}.
*
* @return the position of the selected {@link BoxIngredientItem}
*/
public int next() {
selected++;

if (patterns.size() <= selected) {
selected = 0;
}

return selected;
}

/**
* Selects the {@link BoxIngredientItem} at the specified position.
*
* @param num the position
*/
public void select(int num) {
selected = num;

if (patterns.size() <= selected) {
selected = 0;
}
}

/**
* Returns the {@link IngredientHolder#patterns()}.
*
* @return the {@link IngredientHolder#patterns()}
*/
public @NotNull @Unmodifiable List<BoxIngredientItem> get() {
return patterns;
}

/**
* Returns the number of {@link BoxIngredientItem}s.
*
* @return the number of {@link BoxIngredientItem}s
*/
public int size() {
return patterns.size();
}

/**
* Checks if this and the specified {@link SelectableIngredients} are same.
*
* @param other {@link SelectableIngredients} to check
* @return {@code true} if two {@link SelectableIngredients} are same, otherwise {@code false}
*/
public boolean isSameIngredient(@NotNull SelectableIngredients other) {
return this.ingredientHolder.equals(other.ingredientHolder);
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof SelectableIngredients that)) return false;
return this.isSameIngredient(that);
}

@Override
public int hashCode() {
return this.ingredientHolder.hashCode();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import net.okocraft.box.feature.gui.api.lang.Styles;
import net.okocraft.box.feature.gui.api.menu.Menu;
import net.okocraft.box.feature.gui.api.session.PlayerSession;
import net.okocraft.box.feature.gui.api.session.TypedKey;
import net.okocraft.box.feature.gui.api.util.ItemEditor;
import org.bukkit.Material;
import org.bukkit.event.inventory.ClickType;
Expand All @@ -20,6 +21,7 @@

public record IngredientButton(int slot, int ingredientPos) implements Button {

public static final TypedKey<Boolean> CHANGE_PER_INGREDIENT = TypedKey.of(Boolean.class, "change_per_ingredient");
private static final MiniMessageBase CLICK_TO_SHOW_RECIPES = MiniMessageBase.messageKey(DisplayKeys.INGREDIENT_BUTTON_CLICK_TO_SHOW_RECIPES);

@Override
Expand Down Expand Up @@ -89,7 +91,7 @@ public int getSlot() {

return ClickResult.changeMenu(menu);
} else {
currentRecipe.nextRecipe(ingredientPos, session.getData(CurrentRecipe.CHANGE_PER_INGREDIENT) == null);
currentRecipe.nextRecipe(ingredientPos, session.getData(CHANGE_PER_INGREDIENT) == null);
return ClickResult.UPDATE_ICONS;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package net.okocraft.box.feature.craft.gui.button;

import com.github.siroshun09.messages.minimessage.base.MiniMessageBase;
import net.okocraft.box.feature.craft.gui.CurrentRecipe;
import net.okocraft.box.feature.craft.lang.DisplayKeys;
import net.okocraft.box.feature.gui.api.button.Button;
import net.okocraft.box.feature.gui.api.button.ClickResult;
Expand Down Expand Up @@ -31,16 +30,16 @@ public int getSlot() {

@Override
public @NotNull ItemStack createIcon(@NotNull PlayerSession session) {
boolean changePerIngredientMode = session.getData(CurrentRecipe.CHANGE_PER_INGREDIENT) != null;
boolean changePerIngredientMode = session.getData(IngredientButton.CHANGE_PER_INGREDIENT) != null;
return ItemEditor.create()
.displayName((changePerIngredientMode ? EACH_INGREDIENT_MODE : ALL_INGREDIENT_MODE).create(session.getMessageSource()))
.createItem(changePerIngredientMode ? Material.FIREWORK_STAR : Material.FIRE_CHARGE);
}

@Override
public @NotNull ClickResult onClick(@NotNull PlayerSession session, @NotNull ClickType clickType) {
if (session.removeData(CurrentRecipe.CHANGE_PER_INGREDIENT) == null) {
session.putData(CurrentRecipe.CHANGE_PER_INGREDIENT, Boolean.TRUE);
if (session.removeData(IngredientButton.CHANGE_PER_INGREDIENT) == null) {
session.putData(IngredientButton.CHANGE_PER_INGREDIENT, Boolean.TRUE);
}

SoundBase.CLICK.play(session.getViewer());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static void render(@NotNull ItemEditor<? extends ItemMeta> editor, @NotNu
var ingredients =
recipe.ingredients()
.stream()
.map(holder -> holder.getPatterns().getFirst())
.map(holder -> holder.patterns().getFirst())
.toList();

render(editor, session, new SelectedRecipe(ingredients, recipe.result(), recipe.amount()), times);
Expand All @@ -88,7 +88,7 @@ public static void render(@NotNull ItemEditor<? extends ItemMeta> editor, @NotNu
int need = ingredient.getValue() * times;

var itemNames =
holder.getPatterns().stream()
holder.patterns().stream()
.map(BoxIngredientItem::item)
.map(BoxItem::getOriginal)
.map(Component::translatable)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ private void processShapedRecipe(@NotNull ShapedRecipe recipe, @NotNull BoxItem
} else if (choice instanceof RecipeChoice.ExactChoice exactChoice) {
var ingredientItem = IngredientHolder.fromExactChoice(slot, exactChoice);

if (ingredientItem.getPatterns().isEmpty()) {
if (ingredientItem.patterns().isEmpty()) {
return;
} else {
ingredients.add(IngredientHolder.fromExactChoice(slot, exactChoice));
Expand All @@ -161,7 +161,7 @@ private void processShapelessRecipe(@NotNull ShapelessRecipe recipe, @NotNull Bo
} else if (choice instanceof RecipeChoice.ExactChoice exactChoice) {
var ingredientItem = IngredientHolder.fromExactChoice(slot, exactChoice);

if (ingredientItem.getPatterns().isEmpty()) {
if (ingredientItem.patterns().isEmpty()) {
return;
} else {
ingredients.add(IngredientHolder.fromExactChoice(slot, exactChoice));
Expand Down
Loading

0 comments on commit f06e0fa

Please sign in to comment.