Skip to content

Commit

Permalink
[1.9.0-dev3]新配方格式的逻辑初步完善
Browse files Browse the repository at this point in the history
  • Loading branch information
YufiriaMazenta committed Jan 8, 2024
1 parent a033a36 commit fc326a0
Show file tree
Hide file tree
Showing 27 changed files with 338 additions and 294 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,42 +54,38 @@ public boolean onCommand(CommandSender sender, List<String> args) {
LangUtil.sendLang(sender, Languages.COMMAND_CREATE_UNSUPPORTED_RECIPE_TYPE);
return true;
}
String recipeName;
if (args.size() < 2)
recipeName = UUID.randomUUID().toString();
else
recipeName = args.get(1);

Matcher matcher = recipeNamePattern.matcher(recipeName);
String groupName = args.size() < 2 ? "global" : args.get(1);
String recipeName = args.size() < 3 ? UUID.randomUUID().toString() : args.get(2);
Matcher matcher = recipeNamePattern.matcher(groupName);
if (!matcher.matches()) {
LangUtil.sendLang(sender, Languages.COMMAND_CREATE_UNSUPPORTED_RECIPE_NAME);
return true;
}
if (RecipeManager.INSTANCE.hasCraftorithmRecipe(recipeName)) {
LangUtil.sendLang(sender, Languages.COMMAND_CREATE_NAME_USED);
return true;
}
// if (RecipeManager.INSTANCE.hasRecipeGroup(groupName)) {
// LangUtil.sendLang(sender, Languages.COMMAND_CREATE_NAME_USED);
// return true;
// }
RecipeType recipeType = RecipeType.valueOf(recipeTypeStr.toUpperCase(Locale.ROOT));
Player player = (Player) sender;
switch (recipeType) {
case SHAPED:
case SHAPELESS:
new CraftingRecipeCreator(player, recipeType, recipeName).openMenu();
new CraftingRecipeCreator(player, recipeType, groupName, recipeName).openMenu();
break;
case COOKING:
new CookingRecipeCreator(player, recipeName).openMenu();
new CookingRecipeCreator(player, groupName, recipeName).openMenu();
break;
case SMITHING:
new SmithingRecipeCreator(player, recipeName).openMenu();
new SmithingRecipeCreator(player, groupName, recipeName).openMenu();
break;
case STONE_CUTTING:
new StoneCuttingRecipeCreator(player, recipeName).openMenu();
new StoneCuttingRecipeCreator(player, groupName, recipeName).openMenu();
break;
case POTION:
new PotionMixCreator(player, recipeName).openMenu();
new PotionMixCreator(player, groupName, recipeName).openMenu();
break;
case ANVIL:
new AnvilRecipeCreator(player, recipeName).openMenu();
new AnvilRecipeCreator(player, groupName, recipeName).openMenu();
break;
default:
LangUtil.sendLang(sender, Languages.COMMAND_CREATE_UNSUPPORTED_RECIPE_TYPE);
Expand All @@ -104,6 +100,10 @@ public List<String> onTabComplete(CommandSender sender, List<String> args) {
List<String> tabList = new ArrayList<>(recipeTypeList);
filterTabList(tabList, args.get(0));
return tabList;
} else if (args.size() == 2) {
List<String> tabList = new ArrayList<>(RecipeManager.INSTANCE.getRecipeGroups());
filterTabList(tabList, args.get(1));
return tabList;
}
return Collections.singletonList("<recipe_name>");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public boolean onCommand(CommandSender sender, List<String> args) {
return true;
}

if (RecipeManager.INSTANCE.removeCraftorithmRecipe(args.get(0), true)) {
if (RecipeManager.INSTANCE.removeRecipeGroup(args.get(0), true)) {
LangUtil.sendLang(sender, Languages.COMMAND_REMOVE_SUCCESS);
}
else
Expand All @@ -40,10 +40,7 @@ public boolean onCommand(CommandSender sender, List<String> args) {
@Override
public List<String> onTabComplete(CommandSender sender, List<String> args) {
if (args.size() <= 1) {
List<String> tabList = new ArrayList<>();
for (Map<String, RecipeGroup> recipeGroupMap : RecipeManager.INSTANCE.recipeMap().values()) {
tabList.addAll(recipeGroupMap.keySet());
}
List<String> tabList = new ArrayList<>(RecipeManager.INSTANCE.getRecipeGroups());
filterTabList(tabList, args.get(0));
return tabList;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
package com.github.yufiriamazenta.craftorithm.listener;

import com.github.yufiriamazenta.craftorithm.item.ItemManager;
import com.github.yufiriamazenta.craftorithm.recipe.RecipeGroup;
import com.github.yufiriamazenta.craftorithm.recipe.RecipeManager;
import com.github.yufiriamazenta.craftorithm.recipe.RecipeType;
import com.github.yufiriamazenta.craftorithm.recipe.registry.RandomCookingRecipeRegistry;
import com.github.yufiriamazenta.craftorithm.recipe.registry.RecipeRegistry;
import crypticlib.listener.BukkitListener;
import org.bukkit.NamespacedKey;
import org.bukkit.block.Block;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.FurnaceSmeltEvent;
import org.bukkit.event.inventory.FurnaceStartSmeltEvent;
import org.bukkit.inventory.ItemStack;

import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

@BukkitListener
public enum RandomSmeltHandler implements Listener {

INSTANCE;

private final Map<Block, YamlConfiguration> randomFurnaceBlockMap;
private final Map<Block, List<RandomCookingRecipeRegistry.RandomCookingResult>> randomFurnaceBlockMap;
private final Random random;

RandomSmeltHandler() {
Expand All @@ -33,53 +35,33 @@ public enum RandomSmeltHandler implements Listener {
@EventHandler(priority = EventPriority.LOWEST)
public void onFurnaceStartSmelt(FurnaceStartSmeltEvent event) {
NamespacedKey recipeKey = event.getRecipe().getKey();
Map<String, RecipeGroup> randomCookingRecipeGroups = RecipeManager.INSTANCE.recipeMap().getOrDefault(RecipeType.RANDOM_COOKING, new HashMap<>());
boolean isRandomCooking = false;
for (RecipeGroup group : randomCookingRecipeGroups.values()) {
if (group.contains(recipeKey)) {
isRandomCooking = true;
break;
}
}
if (!isRandomCooking)
RecipeGroup recipeGroup = RecipeManager.INSTANCE.getRecipeGroup(recipeKey);
if (recipeGroup == null)
return;
YamlConfiguration config = RecipeManager.INSTANCE.getRecipeConfig(event.getRecipe().getKey());
if (config == null)

RecipeRegistry recipeRegistry = recipeGroup.getRecipeRegistry(recipeKey);
if (recipeRegistry == null)
return;
if (config.getString("type", "shaped").equals("random_cooking")) {
randomFurnaceBlockMap.put(event.getBlock(), config);
}

if (!recipeRegistry.recipeType().equals(RecipeType.RANDOM_COOKING))
return;

RandomCookingRecipeRegistry randomCookingRecipeRegistry = (RandomCookingRecipeRegistry) recipeRegistry;
randomFurnaceBlockMap.put(event.getBlock(), randomCookingRecipeRegistry.results());
}

@EventHandler(priority = EventPriority.LOWEST)
public void onFurnaceSmelt(FurnaceSmeltEvent event) {
if (randomFurnaceBlockMap.containsKey(event.getBlock())) {
List<String> resultList = randomFurnaceBlockMap.get(event.getBlock()).getStringList("result");
List<Map.Entry<ItemStack, Integer>> weightList = getWeight(resultList);
int randomNum = random.nextInt(weightList.get(weightList.size() - 1).getValue());
for (Map.Entry<ItemStack, Integer> entry : weightList) {
if (randomNum < entry.getValue()) {
event.setResult(entry.getKey());
List<RandomCookingRecipeRegistry.RandomCookingResult> results = randomFurnaceBlockMap.get(event.getBlock());
int randomNum = random.nextInt(results.get(results.size() - 1).weight());
for (RandomCookingRecipeRegistry.RandomCookingResult result : results) {
if (randomNum < result.weight()) {
event.setResult(result.result());
break;
}
}
randomFurnaceBlockMap.remove(event.getBlock());
}
}

private List<Map.Entry<ItemStack, Integer>> getWeight(List<String> resultStr) {
Map<ItemStack, Integer> weightMap = new HashMap<>();
int sum = 0;
for (String result : resultStr) {
String item = result.substring(0, result.lastIndexOf(" "));
int weight = Integer.parseInt(result.substring(result.lastIndexOf(" ") + 1));
ItemStack itemStack = ItemManager.INSTANCE.matchItem(item);
sum += weight;
weightMap.put(itemStack, sum);
}
List<Map.Entry<ItemStack, Integer>> weightList = new ArrayList<>(weightMap.entrySet());
weightList.sort(Map.Entry.comparingByValue());
return weightList;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public class AnvilRecipeCreator extends RecipeCreator {
private int costLevel;
private boolean inConversation;

public AnvilRecipeCreator(@NotNull Player player, @NotNull String recipeName) {
super(player, RecipeType.ANVIL, recipeName);
public AnvilRecipeCreator(@NotNull Player player, @NotNull String groupName, @NotNull String recipeName) {
super(player, RecipeType.ANVIL, groupName, recipeName);
this.copyNbt = true;
this.inConversation = false;
this.costLevel = 0;
Expand Down Expand Up @@ -82,7 +82,8 @@ public AnvilRecipeCreator(@NotNull Player player, @NotNull String recipeName) {
recipeConfig.set("result", resultName);
recipeConfig.saveConfig();
recipeConfig.reloadConfig();
regRecipeGroup(recipeConfig);
//TODO 修改创建配方
// getRecipeGroup(recipeConfig);
event.getWhoClicked().closeInventory();
sendSuccessMsg(player, recipeName);
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public class CookingRecipeCreator extends UnlockableRecipeCreator {
private float exp;
private boolean inConversation = false;

public CookingRecipeCreator(@NotNull Player player, @NotNull String recipeName) {
super(player, RecipeType.COOKING, recipeName);
public CookingRecipeCreator(@NotNull Player player, @NotNull String groupName, @NotNull String recipeName) {
super(player, RecipeType.COOKING, groupName, recipeName);
this.cookingTime = 200;
this.exp = 0;
this.cookingToggleMap = new HashMap<>();
Expand Down Expand Up @@ -104,7 +104,8 @@ public CookingRecipeCreator(@NotNull Player player, @NotNull String recipeName)
recipeConfig.set("source", sourceList);
recipeConfig.saveConfig();
recipeConfig.reloadConfig();
regRecipeGroup(recipeConfig);
//TODO 修改创建配方
// getRecipeGroup(recipeConfig);
event.getWhoClicked().closeInventory();
sendSuccessMsg(event.getWhoClicked(), recipeName);
})
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.yufiriamazenta.craftorithm.menu.creator;

import com.github.yufiriamazenta.craftorithm.config.Languages;
import com.github.yufiriamazenta.craftorithm.recipe.RecipeGroup;
import com.github.yufiriamazenta.craftorithm.recipe.RecipeType;
import com.github.yufiriamazenta.craftorithm.util.ItemUtils;
import com.github.yufiriamazenta.craftorithm.util.LangUtil;
Expand All @@ -12,6 +13,7 @@
import crypticlib.ui.menu.StoredMenu;
import crypticlib.util.ItemUtil;
import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;
Expand All @@ -20,8 +22,8 @@

public class CraftingRecipeCreator extends UnlockableRecipeCreator {

public CraftingRecipeCreator(@NotNull Player player, RecipeType recipeType, @NotNull String recipeName) {
super(player, recipeType, recipeName);
public CraftingRecipeCreator(@NotNull Player player, RecipeType recipeType, @NotNull String groupName, @NotNull String recipeName) {
super(player, recipeType, groupName, recipeName);
Preconditions.checkArgument(
recipeType.equals(RecipeType.SHAPED) || recipeType.equals(RecipeType.SHAPELESS),
"Crafting recipe only allow shaped and shapeless type"
Expand Down Expand Up @@ -73,7 +75,9 @@ public CraftingRecipeCreator(@NotNull Player player, RecipeType recipeType, @Not
LangUtil.sendLang(event.getWhoClicked(), Languages.COMMAND_CREATE_NULL_SOURCE);
return;
}
ConfigWrapper recipeConfig = createRecipeConfig(recipeName);
RecipeGroup recipeGroup = getRecipeGroup(groupName);
ConfigWrapper recipeConfig = recipeGroup.recipeGroupConfig();
ConfigurationSection recipeCfgSection = recipeConfig.config().createSection(recipeName);
switch (recipeType()) {
case SHAPED:
List<String> shape = new ArrayList<>(Arrays.asList("abc", "def", "ghi"));
Expand All @@ -96,21 +100,21 @@ public CraftingRecipeCreator(@NotNull Player player, RecipeType recipeType, @Not
shape.set(i, s);
}
shape.removeIf(s -> s.trim().isEmpty());
recipeConfig.set("type", "shaped");
recipeConfig.set("shape", shape);
recipeConfig.set("source", itemNameMap);
recipeCfgSection.set("type", "shaped");
recipeCfgSection.set("source.shape", shape);
recipeCfgSection.set("source.ingredients", itemNameMap);
break;
case SHAPELESS:
sourceList.removeIf(String::isEmpty);
recipeConfig.set("type", "shapeless");
recipeConfig.set("source", sourceList);
recipeCfgSection.set("type", "shapeless");
recipeCfgSection.set("source.ingredients", sourceList);
break;
}
recipeConfig.set("unlock", unlock());
recipeConfig.set("result", resultName);
recipeCfgSection.set("unlock", unlock());
recipeCfgSection.set("result", resultName);
recipeConfig.saveConfig();
recipeConfig.reloadConfig();
regRecipeGroup(recipeConfig);
recipeGroup.updateRecipeGroup();
event.getWhoClicked().closeInventory();
sendSuccessMsg(event.getWhoClicked(), recipeName);
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@

public class PotionMixCreator extends RecipeCreator {

public PotionMixCreator(@NotNull Player player, @NotNull String recipeName) {
super(player, RecipeType.POTION, recipeName);
public PotionMixCreator(@NotNull Player player, @NotNull String groupName, @NotNull String recipeName) {
super(player, RecipeType.POTION, groupName, recipeName);
setDisplay(
new MenuDisplay(
title(),
Expand Down Expand Up @@ -67,7 +67,8 @@ public PotionMixCreator(@NotNull Player player, @NotNull String recipeName) {
recipeConfig.set("result", resultName);
recipeConfig.saveConfig();
recipeConfig.reloadConfig();
regRecipeGroup(recipeConfig);
//TODO 修改创建配方
// getRecipeGroup(recipeConfig);
event.getWhoClicked().closeInventory();
sendSuccessMsg(player, recipeName);
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import com.github.yufiriamazenta.craftorithm.recipe.RecipeGroup;
import com.github.yufiriamazenta.craftorithm.recipe.RecipeManager;
import com.github.yufiriamazenta.craftorithm.recipe.RecipeType;
import com.github.yufiriamazenta.craftorithm.recipe.registry.RecipeRegistry;
import com.github.yufiriamazenta.craftorithm.util.CollectionsUtil;
import com.github.yufiriamazenta.craftorithm.util.ItemUtils;
import com.github.yufiriamazenta.craftorithm.util.LangUtil;
Expand All @@ -24,21 +23,19 @@

public abstract class RecipeCreator extends StoredMenu {

private RecipeType recipeType;
private String recipeName;
private String title;
protected RecipeType recipeType;
protected String groupName;
protected String recipeName;
protected String title;

public RecipeCreator(
@NotNull Player player,
@NotNull RecipeType recipeType,
@NotNull String recipeName
) {
public RecipeCreator(@NotNull Player player, @NotNull RecipeType recipeType, @NotNull String groupName, @NotNull String recipeName) {
super(player);
this.recipeName = recipeName;
this.recipeType = recipeType;
this.title = Languages.MENU_RECIPE_CREATOR_TITLE.value(player)
.replace("<recipe_type>", RecipeManager.INSTANCE.getRecipeTypeName(recipeType).value(player))
.replace("<recipe_name>", recipeName);
this.groupName = groupName;
}

protected void toggleIconGlowing(int slot, InventoryClickEvent event) {
Expand Down Expand Up @@ -66,8 +63,8 @@ protected void sendSuccessMsg(HumanEntity receiver, String recipeName) {
);
}

protected ConfigWrapper createRecipeConfig(String recipeName) {
File recipeFile = new File(RecipeManager.INSTANCE.RECIPE_FILE_FOLDER, recipeName + ".yml");
protected ConfigWrapper createRecipeConfig(String groupName) {
File recipeFile = new File(RecipeManager.INSTANCE.RECIPE_FILE_FOLDER, groupName + ".yml");
if (!recipeFile.exists()) {
FileUtil.createNewFile(recipeFile);
}
Expand Down Expand Up @@ -105,12 +102,11 @@ public RecipeCreator setTitle(String title) {
return this;
}

public void regRecipeGroup(ConfigWrapper recipeConfig) {
RecipeGroup recipeGroup = new RecipeGroup(recipeName, recipeType(), recipeConfig);
RecipeManager.INSTANCE.addRecipeGroup(recipeGroup);
for (RecipeRegistry recipeRegistry : RecipeFactory.newRecipeRegistry(recipeConfig.config(), recipeName)) {
recipeRegistry.register();
}
public RecipeGroup getRecipeGroup(String groupName) {
if (RecipeManager.INSTANCE.hasRecipeGroup(groupName))
return RecipeManager.INSTANCE.getRecipeGroup(groupName);

return new RecipeGroup(recipeName, createRecipeConfig(groupName), 0);
}

}
Loading

0 comments on commit fc326a0

Please sign in to comment.