mirror of
https://gitlab.com/phoenix-dvpmt/mmoitems.git
synced 2025-02-02 11:21:20 +01:00
Added crafting previews to the crafting stations
This commit is contained in:
parent
19cb91cba6
commit
7d38ce3381
@ -27,8 +27,10 @@ public class ConfigItem {
|
||||
// generated
|
||||
private ItemStack item;
|
||||
|
||||
public static final ConfigItem CONFIRM = new ConfigItem("CONFIRM", VersionMaterial.GREEN_STAINED_GLASS_PANE.toMaterial(), "&aConfirm");
|
||||
public static final ConfigItem NO_ITEM = new ConfigItem("NO_ITEM", VersionMaterial.GRAY_STAINED_GLASS_PANE.toMaterial(), "&c- No Item -");
|
||||
public static final ConfigItem NO_TYPE = new ConfigItem("NO_TYPE", VersionMaterial.GRAY_STAINED_GLASS_PANE.toMaterial(), "&c- No Type -");
|
||||
public static final ConfigItem FILL = new ConfigItem("FILL", VersionMaterial.GRAY_STAINED_GLASS_PANE.toMaterial(), "&8");
|
||||
public static final ConfigItem TYPE_DISPLAY = new ConfigItem("TYPE_DISPLAY", Material.BARRIER, "&a#type# &8(Click to browse)", "&7There are &6#recipes#&7 available recipes.");
|
||||
public static final CustomSkull PREVIOUS_PAGE = new CustomSkull("PREVIOUS_PAGE", "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvYmQ2OWUwNmU1ZGFkZmQ4NGU1ZjNkMWMyMTA2M2YyNTUzYjJmYTk0NWVlMWQ0ZDcxNTJmZGM1NDI1YmMxMmE5In19fQ==", "&aPrevious Page");
|
||||
public static final CustomSkull NEXT_PAGE = new CustomSkull("NEXT_PAGE", "eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvMTliZjMyOTJlMTI2YTEwNWI1NGViYTcxM2FhMWIxNTJkNTQxYTFkODkzODgyOWM1NjM2NGQxNzhlZDIyYmYifX19", "&aNext Page");
|
||||
@ -39,8 +41,8 @@ public class ConfigItem {
|
||||
public static final CraftingRecipeDisplay CRAFTING_RECIPE_DISPLAY = new CraftingRecipeDisplay();
|
||||
public static final UpgradingRecipeDisplay UPGRADING_RECIPE_DISPLAY = new UpgradingRecipeDisplay();
|
||||
public static final QueueItemDisplay QUEUE_ITEM_DISPLAY = new QueueItemDisplay();
|
||||
|
||||
public static final ConfigItem[] values = { NO_ITEM, NO_TYPE, TYPE_DISPLAY, PREVIOUS_PAGE, NEXT_PAGE, PREVIOUS_IN_QUEUE, NEXT_IN_QUEUE, BACK, RECIPE_LIST, CRAFTING_RECIPE_DISPLAY, UPGRADING_RECIPE_DISPLAY, QUEUE_ITEM_DISPLAY };
|
||||
|
||||
public static final ConfigItem[] values = { CONFIRM, FILL, NO_ITEM, NO_TYPE, TYPE_DISPLAY, PREVIOUS_PAGE, NEXT_PAGE, PREVIOUS_IN_QUEUE, NEXT_IN_QUEUE, BACK, RECIPE_LIST, CRAFTING_RECIPE_DISPLAY, UPGRADING_RECIPE_DISPLAY, QUEUE_ITEM_DISPLAY };
|
||||
|
||||
public ConfigItem(String id, Material material) {
|
||||
this(id, material, null);
|
||||
|
@ -25,7 +25,7 @@ public class CraftingRecipeDisplay extends ConfigItem {
|
||||
private static final DecimalFormat craftingTimeFormat = new DecimalFormat("0.#");
|
||||
|
||||
public CraftingRecipeDisplay() {
|
||||
super("CRAFTING_RECIPE_DISPLAY", Material.BARRIER, "&a&lCraft&f #name#", "{conditions}", "#condition_level#", "{crafting_time}", "{crafting_time}&7Crafting Time: &c#crafting-time#&7s", "", "&8Ingredients:", "#ingredients#", "", "&eClick to craft!");
|
||||
super("CRAFTING_RECIPE_DISPLAY", Material.BARRIER, "&a&lCraft&f #name#", "{conditions}", "#condition_level#", "{crafting_time}", "{crafting_time}&7Crafting Time: &c#crafting-time#&7s", "", "&8Ingredients:", "#ingredients#", "", "&eLeft-Click to craft!", "&eRight-Click to preview!");
|
||||
}
|
||||
|
||||
public ItemBuilder newBuilder(RecipeInfo recipe) {
|
||||
|
@ -22,7 +22,7 @@ import net.Indyuce.mmoitems.version.nms.ItemTag;
|
||||
|
||||
public class UpgradingRecipeDisplay extends ConfigItem {
|
||||
public UpgradingRecipeDisplay() {
|
||||
super("UPGRADING_RECIPE_DISPLAY", Material.BARRIER, "&e&lUpgrade&f #name#", "{conditions}", "#condition_level#", "", "&8Ingredients:", "#ingredients#", "", "&eClick to craft!");
|
||||
super("UPGRADING_RECIPE_DISPLAY", Material.BARRIER, "&e&lUpgrade&f #name#", "{conditions}", "#condition_level#", "", "&8Ingredients:", "#ingredients#", "", "&eLeft-Click to craft!", "&eRight-Click to preview!");
|
||||
}
|
||||
|
||||
public ItemBuilder newBuilder(RecipeInfo recipe) {
|
||||
|
@ -375,7 +375,7 @@ public class MMOItemsCommand implements CommandExecutor {
|
||||
return true;
|
||||
}
|
||||
|
||||
new CraftingStationView(target, MMOItems.plugin.getCrafting().getStation(args[2])).open();
|
||||
new CraftingStationView(target, MMOItems.plugin.getCrafting().getStation(args[2]), 1).open();
|
||||
}
|
||||
}
|
||||
// ==================================================================================================================================
|
||||
|
@ -0,0 +1,124 @@
|
||||
package net.Indyuce.mmoitems.gui;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.event.inventory.InventoryClickEvent;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
|
||||
import net.Indyuce.mmoitems.MMOUtils;
|
||||
import net.Indyuce.mmoitems.api.crafting.CraftingStation;
|
||||
import net.Indyuce.mmoitems.api.crafting.ingredient.Ingredient.IngredientInfo;
|
||||
import net.Indyuce.mmoitems.api.crafting.recipe.CraftingRecipe;
|
||||
import net.Indyuce.mmoitems.api.crafting.recipe.RecipeInfo;
|
||||
import net.Indyuce.mmoitems.api.crafting.recipe.UpgradingRecipe;
|
||||
import net.Indyuce.mmoitems.api.item.plugin.ConfigItem;
|
||||
import net.md_5.bungee.api.ChatColor;
|
||||
|
||||
public class CraftingStationPreview extends PluginInventory {
|
||||
private final int previousPage;
|
||||
private final CraftingStation station;
|
||||
private final RecipeInfo recipe;
|
||||
|
||||
private List<IngredientInfo> ingredients = new ArrayList<>();
|
||||
|
||||
private static final int[]
|
||||
slots = { 12, 13, 14, 21, 22, 23, 30, 31, 32 },
|
||||
fill = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 15, 17, 18, 19, 25, 26, 27, 29, 33, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44 };
|
||||
|
||||
public CraftingStationPreview(Player player, CraftingStation station, RecipeInfo recipe, int previousPage) {
|
||||
super(player);
|
||||
|
||||
this.previousPage = previousPage;
|
||||
this.station = station;
|
||||
this.recipe = recipe;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Inventory getInventory() {
|
||||
Inventory inv = Bukkit.createInventory(this, 45, "Recipe Preview");
|
||||
ingredients.clear();
|
||||
ingredients.addAll(recipe.getIngredients());
|
||||
|
||||
int min = (page - 1) * slots.length,
|
||||
max = page * slots.length;
|
||||
for (int j = min; j < max; j++) {
|
||||
if (j >= ingredients.size()) {
|
||||
if (station.getItemOptions().hasNoRecipe())
|
||||
inv.setItem(slots[j - min], null);
|
||||
continue;
|
||||
}
|
||||
|
||||
inv.setItem(slots[j - min], ingredients.get(j).getIngredient().generateItemStack());
|
||||
}
|
||||
|
||||
for (int slot : fill)
|
||||
inv.setItem(slot, ConfigItem.FILL.getItem());
|
||||
|
||||
if(recipe.getRecipe() instanceof CraftingRecipe)
|
||||
inv.setItem(16, ((CraftingRecipe) recipe.getRecipe()).getOutput().getPreview());
|
||||
if(recipe.getRecipe() instanceof UpgradingRecipe) {
|
||||
ItemStack stack = ((UpgradingRecipe) recipe.getRecipe()).getItem().getPreview();
|
||||
ItemMeta meta = stack.getItemMeta();
|
||||
meta.setDisplayName(meta.getDisplayName() + ChatColor.translateAlternateColorCodes('&', " &a+1!"));
|
||||
stack.setItemMeta(meta);
|
||||
inv.setItem(16, stack);
|
||||
}
|
||||
|
||||
inv.setItem(10, ConfigItem.BACK.getItem());
|
||||
inv.setItem(34, ConfigItem.CONFIRM.getItem());
|
||||
ItemStack book = recipe.display();
|
||||
book.setType(Material.KNOWLEDGE_BOOK);
|
||||
ItemMeta meta = book.getItemMeta();
|
||||
List<String> newLore = meta.getLore().subList(0, meta.getLore().size() - 3);
|
||||
meta.setLore(newLore);
|
||||
for(Enchantment ench : meta.getEnchants().keySet())
|
||||
meta.removeEnchant(ench);
|
||||
book.setItemMeta(meta);
|
||||
inv.setItem(28, book);
|
||||
|
||||
|
||||
inv.setItem(20, page > 1 ? ConfigItem.PREVIOUS_PAGE.getItem() : ConfigItem.FILL.getItem());
|
||||
inv.setItem(24, max < ingredients.size() ? ConfigItem.NEXT_PAGE.getItem() : ConfigItem.FILL.getItem());
|
||||
|
||||
return inv;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void whenClicked(InventoryClickEvent event) {
|
||||
event.setCancelled(true);
|
||||
|
||||
if (!MMOUtils.isPluginItem(event.getCurrentItem(), false))
|
||||
return;
|
||||
|
||||
if (event.getCurrentItem().isSimilar(ConfigItem.CONFIRM.getItem())) {
|
||||
CraftingStationView csv = new CraftingStationView(player, station, previousPage);
|
||||
csv.processRecipe(recipe);
|
||||
csv.open();
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.getCurrentItem().isSimilar(ConfigItem.PREVIOUS_PAGE.getItem())) {
|
||||
page--;
|
||||
open();
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.getCurrentItem().isSimilar(ConfigItem.NEXT_PAGE.getItem())) {
|
||||
page++;
|
||||
open();
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.getCurrentItem().isSimilar(ConfigItem.BACK.getItem())) {
|
||||
new CraftingStationView(player, station, previousPage).open();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
@ -40,11 +40,12 @@ public class CraftingStationView extends PluginInventory {
|
||||
private static final int[] slots = { 10, 11, 12, 13, 14, 15, 16, 19, 20, 21, 22, 23, 24, 25 }, queueSlots = { 38, 39, 40, 41, 42 };
|
||||
private static final int[] fill = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 17, 18, 26, 27, 35, 36, 37, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 28, 29, 30, 31, 32, 33, 34 };
|
||||
|
||||
public CraftingStationView(Player player, CraftingStation station) {
|
||||
public CraftingStationView(Player player, CraftingStation station, int page) {
|
||||
super(player);
|
||||
|
||||
this.data = PlayerData.get(player);
|
||||
this.station = station;
|
||||
this.page = page;
|
||||
|
||||
updateData();
|
||||
}
|
||||
@ -152,29 +153,12 @@ public class CraftingStationView extends PluginInventory {
|
||||
String tag = item.getString("recipeId");
|
||||
if (!tag.equals("")) {
|
||||
RecipeInfo recipe = getRecipe(tag);
|
||||
if (!recipe.areConditionsMet()) {
|
||||
Message.CONDITIONS_NOT_MET.format(ChatColor.RED).send(player);
|
||||
player.playSound(player.getLocation(), Sound.ENTITY_VILLAGER_NO, 1, 1);
|
||||
if(event.isRightClick()) {
|
||||
new CraftingStationPreview(player, station, recipe, page).open();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!recipe.allIngredientsHad()) {
|
||||
Message.NOT_ENOUGH_MATERIALS.format(ChatColor.RED).send(player);
|
||||
player.playSound(player.getLocation(), Sound.ENTITY_VILLAGER_NO, 1, 1);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!recipe.getRecipe().canUse(data, ingredients, recipe, station))
|
||||
return;
|
||||
|
||||
PlayerUseRecipeEvent called = new PlayerUseRecipeEvent(data, station, recipe);
|
||||
Bukkit.getPluginManager().callEvent(called);
|
||||
if (called.isCancelled())
|
||||
return;
|
||||
|
||||
recipe.getRecipe().whenUsed(data, ingredients, recipe, station);
|
||||
recipe.getIngredients().forEach(ingredient -> ingredient.getPlayerIngredient().reduceItem(ingredient.getIngredient().getAmount()));
|
||||
recipe.getConditions().forEach(condition -> condition.getCondition().whenCrafting(data));
|
||||
|
||||
processRecipe(recipe);
|
||||
|
||||
updateData();
|
||||
open();
|
||||
@ -202,6 +186,32 @@ public class CraftingStationView extends PluginInventory {
|
||||
}
|
||||
}
|
||||
|
||||
public void processRecipe(RecipeInfo recipe) {
|
||||
if (!recipe.areConditionsMet()) {
|
||||
Message.CONDITIONS_NOT_MET.format(ChatColor.RED).send(player);
|
||||
player.playSound(player.getLocation(), Sound.ENTITY_VILLAGER_NO, 1, 1);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!recipe.allIngredientsHad()) {
|
||||
Message.NOT_ENOUGH_MATERIALS.format(ChatColor.RED).send(player);
|
||||
player.playSound(player.getLocation(), Sound.ENTITY_VILLAGER_NO, 1, 1);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!recipe.getRecipe().canUse(data, ingredients, recipe, station))
|
||||
return;
|
||||
|
||||
PlayerUseRecipeEvent called = new PlayerUseRecipeEvent(data, station, recipe);
|
||||
Bukkit.getPluginManager().callEvent(called);
|
||||
if (called.isCancelled())
|
||||
return;
|
||||
|
||||
recipe.getRecipe().whenUsed(data, ingredients, recipe, station);
|
||||
recipe.getIngredients().forEach(ingredient -> ingredient.getPlayerIngredient().reduceItem(ingredient.getIngredient().getAmount()));
|
||||
recipe.getConditions().forEach(condition -> condition.getCondition().whenCrafting(data));
|
||||
}
|
||||
|
||||
private RecipeInfo getRecipe(String id) {
|
||||
for (RecipeInfo info : recipes)
|
||||
if (info.getRecipe().getId().equals(id))
|
||||
|
Loading…
Reference in New Issue
Block a user