Skip to content

Commit

Permalink
[1.8.0-beta3]配方编辑页面新增删除配方功能
Browse files Browse the repository at this point in the history
  • Loading branch information
YufiriaMazenta committed Dec 29, 2023
1 parent 25f9360 commit fa18c39
Show file tree
Hide file tree
Showing 13 changed files with 111 additions and 66 deletions.
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import java.text.SimpleDateFormat
version = "1.8.0-beta2"
version = "1.8.0-beta3"

plugins {
`java-library`
Expand Down Expand Up @@ -40,7 +40,7 @@ dependencies {
compileOnly("io.lumine:Mythic-Dist:5.3.5")
compileOnly("io.lumine:MythicLib-dist:1.6.2-SNAPSHOT")
compileOnly("net.Indyuce:MMOItems-API:6.9.5-SNAPSHOT")
implementation("com.crypticlib:CrypticLib:0.10.22")
implementation("com.crypticlib:CrypticLib:0.10.23")
}

group = "com.github.yufiriamazenta"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ public class Languages {
public static final StringLangConfigEntry MENU_RECIPE_EDITOR_ICON_COOKING_ELEMENT_TYPE_NAME_CAMPFIRE = new StringLangConfigEntry("menu.recipe_editor.icon.cooking_element.type_name.campfire");
public static final StringListLangConfigEntry MENU_RECIPE_EDITOR_ICON_ANVIL_ELEMENT_LORE = new StringListLangConfigEntry("menu.recipe_editor.icon.anvil_element.lore");
public static final StringLangConfigEntry MENU_RECIPE_EDITOR_ICON_ANVIL_ELEMENT_INPUT_HINT = new StringLangConfigEntry("menu.recipe_editor.icon.anvil_element.input_hint");
public static final StringLangConfigEntry MENU_RECIPE_EDITOR_ICON_REMOVE_NAME = new StringLangConfigEntry("menu.recipe_editor.icon.remove.name");
public static final StringLangConfigEntry MENU_RECIPE_EDITOR_ICON_REMOVE_MESSAGE = new StringLangConfigEntry("menu.recipe_editor.icon.remove.message");
public static final StringLangConfigEntry ARCENCIEL_NOT_ENOUGH_PARAM = new StringLangConfigEntry("arcenciel.not_enough_param");
public static final StringLangConfigEntry ARCENCIEL_UNKNOWN_TOKEN = new StringLangConfigEntry("arcenciel.unknown_token");
public static final StringLangConfigEntry LOAD_FINISH = new StringLangConfigEntry("load.finish");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,41 +23,20 @@
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.function.BiFunction;

public class RecipeGroupListMenu extends Menu {

private int page;
private final int maxPage;
private final List<Map.Entry<String, ItemStack>> recipeGroupResultList;
private final Map<RecipeType, TernaryFunction<Player, RecipeGroup, Menu, RecipeGroupEditor>> recipeGroupEditorMap;
private int maxPage;
private List<Map.Entry<String, ItemStack>> recipeGroupResultList;
private final Map<RecipeType, TernaryFunction<Player, RecipeGroup, RecipeGroupListMenu, RecipeGroupEditor>> recipeGroupEditorMap;

public RecipeGroupListMenu(Player player) {
super(player);
Map<String, ItemStack> recipeResultMap = new HashMap<>();
RecipeManager.INSTANCE.recipeMap().forEach((recipeType, recipeGroupMap) ->
recipeGroupMap.forEach((groupName, recipeGroup) -> {
if (recipeGroup == null || recipeGroup.isEmpty())
return;
Recipe firstRecipe = RecipeManager.INSTANCE.getRecipe(recipeGroup.groupRecipeKeys().get(0));
if (firstRecipe == null)
return;
recipeResultMap.put(groupName, firstRecipe.getResult());
})
);
recipeGroupResultList = new ArrayList<>(recipeResultMap.entrySet());
page = 0;
int recipeGroupNum = recipeResultMap.size();
if (recipeGroupNum % 45 == 0) {
maxPage = recipeGroupNum / 45;
} else {
maxPage = recipeGroupNum / 45 + 1;
}
recipeGroupResultList.sort((o1, o2) -> {
int sortId = RecipeManager.INSTANCE.getRecipeGroupSortId(o1.getKey());
int sortId2 = RecipeManager.INSTANCE.getRecipeGroupSortId(o2.getKey());
return Integer.compare(sortId, sortId2);
});
recipeGroupResultList = new CopyOnWriteArrayList<>();
refreshRecipes();

recipeGroupEditorMap = new ConcurrentHashMap<>();
recipeGroupEditorMap.put(RecipeType.SHAPED, CraftingRecipeGroupEditor::new);
Expand All @@ -74,34 +53,57 @@ public RecipeGroupListMenu(Player player) {
@Override
public Inventory getInventory() {
resetIcons();
Inventory inventory = Bukkit.createInventory(
this,
54,
TextProcessor.color(Languages.MENU_NEW_RECIPE_LIST_TITLE.value(player))
);
for (Integer slot : super.slotMap.keySet()) {
inventory.setItem(slot, slotMap.get(slot).display());
if (openedInventory == null) {
openedInventory = Bukkit.createInventory(
this,
54,
TextProcessor.color(Languages.MENU_NEW_RECIPE_LIST_TITLE.value(player))
);
}
return inventory;
refreshInventory();

return openedInventory;
}

public void nextPage() {
setPage(Math.min(page + 1, maxPage - 1)).resetIcons();
openedInventory.clear();
for (Integer slot : slotMap.keySet()) {
openedInventory.setItem(slot, slotMap.get(slot).display());
}
refreshInventory();
}

public void previousPage() {
setPage(Math.max(page - 1, 0)).resetIcons();
openedInventory.clear();
for (Integer slot : slotMap.keySet()) {
openedInventory.setItem(slot, slotMap.get(slot).display());
refreshInventory();
}

public RecipeGroupListMenu refreshRecipes() {
Map<String, ItemStack> recipeResultMap = new HashMap<>();
RecipeManager.INSTANCE.recipeMap().forEach((recipeType, recipeGroupMap) ->
recipeGroupMap.forEach((groupName, recipeGroup) -> {
if (recipeGroup == null || recipeGroup.isEmpty())
return;
Recipe firstRecipe = RecipeManager.INSTANCE.getRecipe(recipeGroup.groupRecipeKeys().get(0));
if (firstRecipe == null)
return;
recipeResultMap.put(groupName, firstRecipe.getResult());
})
);
recipeGroupResultList = new CopyOnWriteArrayList<>(recipeResultMap.entrySet());
page = 0;
int recipeGroupNum = recipeResultMap.size();
if (recipeGroupNum % 45 == 0) {
maxPage = recipeGroupNum / 45;
} else {
maxPage = recipeGroupNum / 45 + 1;
}
recipeGroupResultList.sort((o1, o2) -> {
int sortId = RecipeManager.INSTANCE.getRecipeGroupSortId(o1.getKey());
int sortId2 = RecipeManager.INSTANCE.getRecipeGroupSortId(o2.getKey());
return Integer.compare(sortId, sortId2);
});
return this;
}

private void resetIcons() {
public RecipeGroupListMenu resetIcons() {
slotMap.clear();
int []frameSlots = {45, 47, 48, 49, 50, 51, 53};
Icon frameIcon = new Icon(
Expand Down Expand Up @@ -130,6 +132,15 @@ private void resetIcons() {
continue;
slotMap.put(i, new Icon(new ItemStack(Material.AIR)));
}
return this;
}

public RecipeGroupListMenu refreshInventory() {
openedInventory.clear();
for (Integer slot : slotMap.keySet()) {
openedInventory.setItem(slot, slotMap.get(slot).display());
}
return this;
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.github.yufiriamazenta.craftorithm.Craftorithm;
import com.github.yufiriamazenta.craftorithm.config.Languages;
import com.github.yufiriamazenta.craftorithm.item.ItemManager;
import com.github.yufiriamazenta.craftorithm.menu.display.RecipeGroupListMenu;
import com.github.yufiriamazenta.craftorithm.recipe.RecipeGroup;
import crypticlib.chat.TextProcessor;
import crypticlib.config.ConfigWrapper;
Expand All @@ -12,7 +13,6 @@
import crypticlib.ui.display.Icon;
import crypticlib.ui.display.MenuDisplay;
import crypticlib.ui.display.MenuLayout;
import crypticlib.ui.menu.Menu;
import net.md_5.bungee.api.chat.BaseComponent;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
Expand All @@ -27,14 +27,14 @@ public class AnvilRecipeGroupEditor extends RecipeGroupEditor {

private final List<AnvilRecipeSource> anvilRecipeSourceList = new ArrayList<>();

public AnvilRecipeGroupEditor(@NotNull Player player, @NotNull RecipeGroup recipeGroup, Menu parent) {
public AnvilRecipeGroupEditor(@NotNull Player player, @NotNull RecipeGroup recipeGroup, RecipeGroupListMenu parent) {
super(player, recipeGroup, parent);
setDisplay(
new MenuDisplay(
title,
new MenuLayout(
Arrays.asList(
"####A####",
"####A###Z",
"X%%%%%%%Y",
"#########"
),
Expand All @@ -44,6 +44,7 @@ public AnvilRecipeGroupEditor(@NotNull Player player, @NotNull RecipeGroup recip
iconMap.put('A', getSortIdEditIcon(4));
iconMap.put('X', getPreviousIcon());
iconMap.put('Y', getNextIcon());
iconMap.put('Z', getRemoveIcon());
//TODO condition和action
return iconMap;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.github.yufiriamazenta.craftorithm.Craftorithm;
import com.github.yufiriamazenta.craftorithm.config.Languages;
import com.github.yufiriamazenta.craftorithm.item.ItemManager;
import com.github.yufiriamazenta.craftorithm.menu.display.RecipeGroupListMenu;
import com.github.yufiriamazenta.craftorithm.recipe.RecipeGroup;
import com.github.yufiriamazenta.craftorithm.recipe.registry.impl.CookingRecipeRegistry;
import com.github.yufiriamazenta.craftorithm.util.ItemUtils;
Expand All @@ -13,7 +14,6 @@
import crypticlib.ui.display.Icon;
import crypticlib.ui.display.MenuDisplay;
import crypticlib.ui.display.MenuLayout;
import crypticlib.ui.menu.Menu;
import crypticlib.util.ItemUtil;
import net.md_5.bungee.api.chat.BaseComponent;
import org.bukkit.configuration.file.YamlConfiguration;
Expand All @@ -28,14 +28,14 @@ public class CookingRecipeGroupEditor extends UnlockableRecipeGroupEditor {

private final List<CookingRecipeSource> cookingRecipeSourceList = new ArrayList<>();

public CookingRecipeGroupEditor(@NotNull Player player, @NotNull RecipeGroup recipeGroup, Menu parent) {
public CookingRecipeGroupEditor(@NotNull Player player, @NotNull RecipeGroup recipeGroup, RecipeGroupListMenu parent) {
super(player, recipeGroup, parent);
setDisplay(
new MenuDisplay(
title,
new MenuLayout(
Arrays.asList(
"###A#B###",
"###A#B##Z",
"X%%%%%%%Y",
"#########"
),
Expand All @@ -46,6 +46,7 @@ public CookingRecipeGroupEditor(@NotNull Player player, @NotNull RecipeGroup rec
layoutMap.put('#', getFrameIcon());
layoutMap.put('X', getPreviousIcon());
layoutMap.put('Y', getNextIcon());
layoutMap.put('Z', getRemoveIcon());
return layoutMap;
}
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package com.github.yufiriamazenta.craftorithm.menu.editor;

import com.github.yufiriamazenta.craftorithm.menu.display.RecipeGroupListMenu;
import com.github.yufiriamazenta.craftorithm.recipe.RecipeGroup;
import crypticlib.ui.display.Icon;
import crypticlib.ui.display.MenuDisplay;
import crypticlib.ui.display.MenuLayout;
import crypticlib.ui.menu.Menu;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

public class CraftingRecipeGroupEditor extends UnlockableRecipeGroupEditor {

public CraftingRecipeGroupEditor(@NotNull Player player, @NotNull RecipeGroup recipeGroup, Menu parent) {
public CraftingRecipeGroupEditor(@NotNull Player player, @NotNull RecipeGroup recipeGroup, RecipeGroupListMenu parent) {
super(player, recipeGroup, parent);
setDisplay(
new MenuDisplay(
Expand All @@ -28,6 +27,7 @@ public CraftingRecipeGroupEditor(@NotNull Player player, @NotNull RecipeGroup re
Map<Character, Icon> iconMap = new HashMap<>();
iconMap.put('A', getSortIdEditIcon(0));
iconMap.put('B', getUnlockIcon());
iconMap.put('I', getRemoveIcon());
//TODO condition和action
return iconMap;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.github.yufiriamazenta.craftorithm.menu.editor;

import com.github.yufiriamazenta.craftorithm.menu.display.RecipeGroupListMenu;
import com.github.yufiriamazenta.craftorithm.recipe.RecipeGroup;
import crypticlib.ui.display.Icon;
import crypticlib.ui.display.MenuDisplay;
import crypticlib.ui.display.MenuLayout;
import crypticlib.ui.menu.Menu;
import org.bukkit.entity.Player;
import org.jetbrains.annotations.NotNull;

Expand All @@ -14,7 +14,7 @@

public class PotionMixGroupEditor extends RecipeGroupEditor {

public PotionMixGroupEditor(@NotNull Player player, @NotNull RecipeGroup recipeGroup, Menu parent) {
public PotionMixGroupEditor(@NotNull Player player, @NotNull RecipeGroup recipeGroup, RecipeGroupListMenu parent) {
super(player, recipeGroup, parent);
setDisplay(
new MenuDisplay(
Expand All @@ -26,6 +26,7 @@ public PotionMixGroupEditor(@NotNull Player player, @NotNull RecipeGroup recipeG
() -> {
Map<Character, Icon> iconMap = new HashMap<>();
iconMap.put('A', getSortIdEditIcon(0));
iconMap.put('I', getRemoveIcon());
return iconMap;
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

import com.github.yufiriamazenta.craftorithm.Craftorithm;
import com.github.yufiriamazenta.craftorithm.config.Languages;
import com.github.yufiriamazenta.craftorithm.menu.display.RecipeGroupListMenu;
import com.github.yufiriamazenta.craftorithm.recipe.RecipeGroup;
import com.github.yufiriamazenta.craftorithm.recipe.RecipeManager;
import com.github.yufiriamazenta.craftorithm.util.CollectionsUtil;
import com.github.yufiriamazenta.craftorithm.util.LangUtil;
import crypticlib.CrypticLib;
import crypticlib.chat.MessageSender;
import crypticlib.chat.TextProcessor;
import crypticlib.config.ConfigWrapper;
import crypticlib.conversation.Conversation;
Expand All @@ -25,19 +29,20 @@
import org.jetbrains.annotations.Nullable;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

public abstract class RecipeGroupEditor extends MultipageMenu {

protected RecipeGroup recipeGroup;
protected Menu parent;
protected RecipeGroupListMenu parent;
protected String title;
protected int sortId;
protected final Character ELEMENT_KEY = '%';
protected boolean inConversation = false;

protected RecipeGroupEditor(@NotNull Player player, @NotNull RecipeGroup recipeGroup, Menu parent) {
protected RecipeGroupEditor(@NotNull Player player, @NotNull RecipeGroup recipeGroup, RecipeGroupListMenu parent) {
super(player);
setElementKey(ELEMENT_KEY);
Validate.notNull(recipeGroup);
Expand Down Expand Up @@ -92,6 +97,22 @@ protected Icon getSortIdEditIcon(int slot) {
);
}

protected Icon getRemoveIcon() {
return new Icon(
Material.BARRIER,
Languages.MENU_RECIPE_EDITOR_ICON_REMOVE_NAME.value(player),
event -> {
RecipeManager.INSTANCE.removeCraftorithmRecipe(recipeGroup().groupName(), true);
player.closeInventory();
LangUtil.sendLang(
player,
Languages.MENU_RECIPE_EDITOR_ICON_REMOVE_MESSAGE,
CollectionsUtil.newStringHashMap("<recipe_name>", recipeGroup.groupName())
);
}
);
}

protected void updateSortIdEditIcon(ItemStack sortIdEditIconDisplay) {
ItemUtil.setDisplayName(
sortIdEditIconDisplay,
Expand All @@ -113,7 +134,7 @@ public void onClose(InventoryCloseEvent event) {
List<InventoryType> typeWhenNotOpenInv = Arrays.asList(InventoryType.CRAFTING, InventoryType.CREATIVE);
if (!typeWhenNotOpenInv.contains(type))
return;
parent.openMenu();
parent.refreshRecipes().resetIcons().refreshInventory().openMenu();
}
);
}
Expand All @@ -123,7 +144,7 @@ public Menu parent() {
return parent;
}

public RecipeGroupEditor setParent(Menu parent) {
public RecipeGroupEditor setParent(RecipeGroupListMenu parent) {
this.parent = parent;
return this;
}
Expand Down
Loading

0 comments on commit fa18c39

Please sign in to comment.