diff --git a/.idea/libraries/Maven__com_github_cryptomorin_XSeries_7_2_1.xml b/.idea/libraries/Maven__com_github_cryptomorin_XSeries_7_2_1.xml
deleted file mode 100644
index 4ad4b7b..0000000
--- a/.idea/libraries/Maven__com_github_cryptomorin_XSeries_7_2_1.xml
+++ /dev/null
@@ -1,13 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/libraries/Maven__com_google_code_gson_gson_2_8_6.xml b/.idea/libraries/Maven__com_google_code_gson_gson_2_8_6.xml
deleted file mode 100644
index 82a9f20..0000000
--- a/.idea/libraries/Maven__com_google_code_gson_gson_2_8_6.xml
+++ /dev/null
@@ -1,13 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/libraries/Maven__com_google_guava_guava_17_0.xml b/.idea/libraries/Maven__com_google_guava_guava_17_0.xml
deleted file mode 100644
index 2a9069c..0000000
--- a/.idea/libraries/Maven__com_google_guava_guava_17_0.xml
+++ /dev/null
@@ -1,13 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/libraries/Maven__javax_persistence_persistence_api_1_0.xml b/.idea/libraries/Maven__javax_persistence_persistence_api_1_0.xml
deleted file mode 100644
index e60370e..0000000
--- a/.idea/libraries/Maven__javax_persistence_persistence_api_1_0.xml
+++ /dev/null
@@ -1,13 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/libraries/Maven__net_md_5_bungeecord_chat_1_8_SNAPSHOT.xml b/.idea/libraries/Maven__net_md_5_bungeecord_chat_1_8_SNAPSHOT.xml
deleted file mode 100644
index 2e39f35..0000000
--- a/.idea/libraries/Maven__net_md_5_bungeecord_chat_1_8_SNAPSHOT.xml
+++ /dev/null
@@ -1,13 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/libraries/Maven__org_avaje_ebean_2_8_1.xml b/.idea/libraries/Maven__org_avaje_ebean_2_8_1.xml
deleted file mode 100644
index 91f161a..0000000
--- a/.idea/libraries/Maven__org_avaje_ebean_2_8_1.xml
+++ /dev/null
@@ -1,13 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/libraries/Maven__org_jetbrains_annotations_20_1_0.xml b/.idea/libraries/Maven__org_jetbrains_annotations_20_1_0.xml
deleted file mode 100644
index de125e3..0000000
--- a/.idea/libraries/Maven__org_jetbrains_annotations_20_1_0.xml
+++ /dev/null
@@ -1,13 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/libraries/Maven__org_spigotmc_spigot_api_1_8_8_R0_1_SNAPSHOT.xml b/.idea/libraries/Maven__org_spigotmc_spigot_api_1_8_8_R0_1_SNAPSHOT.xml
deleted file mode 100644
index 15cce87..0000000
--- a/.idea/libraries/Maven__org_spigotmc_spigot_api_1_8_8_R0_1_SNAPSHOT.xml
+++ /dev/null
@@ -1,13 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/.idea/libraries/Maven__org_yaml_snakeyaml_1_15.xml b/.idea/libraries/Maven__org_yaml_snakeyaml_1_15.xml
deleted file mode 100644
index 02d9152..0000000
--- a/.idea/libraries/Maven__org_yaml_snakeyaml_1_15.xml
+++ /dev/null
@@ -1,13 +0,0 @@
-
-
-
-
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/src/main/java/net/epconsortium/cryptomarket/ui/Calendar.java b/src/main/java/net/epconsortium/cryptomarket/ui/Calendar.java
deleted file mode 100644
index c331cbf..0000000
--- a/src/main/java/net/epconsortium/cryptomarket/ui/Calendar.java
+++ /dev/null
@@ -1,282 +0,0 @@
-package net.epconsortium.cryptomarket.ui;
-
-import java.math.BigDecimal;
-import java.text.MessageFormat;
-import java.time.LocalDate;
-import java.time.Month;
-import java.time.YearMonth;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-import java.util.Objects;
-import java.util.UUID;
-
-import com.cryptomorin.xseries.XMaterial;
-import org.bukkit.Bukkit;
-import org.bukkit.Material;
-import org.bukkit.entity.Player;
-import org.bukkit.inventory.Inventory;
-import org.bukkit.inventory.ItemStack;
-import org.bukkit.inventory.meta.ItemMeta;
-
-import net.epconsortium.cryptomarket.CryptoMarket;
-import net.epconsortium.cryptomarket.finances.ExchangeRate;
-import net.epconsortium.cryptomarket.finances.ExchangeRates;
-import net.epconsortium.cryptomarket.util.Configuration;
-import net.epconsortium.cryptomarket.util.Formatter;
-
-import static net.epconsortium.cryptomarket.CryptoMarket.debug;
-
-/**
- * Represents the Calendar menu
- *
- * @author roinujnosde
- */
-public class Calendar {
-
- private final CryptoMarket plugin;
-
- private final Player player;
- private final Configuration config;
- private final Inventory inventory;
- private YearMonth period;
-
- private static final Map DATA = new HashMap<>();
-
- public Calendar(CryptoMarket plugin, Player player) {
- this.player = Objects.requireNonNull(player);
- this.plugin = Objects.requireNonNull(plugin);
- config = new Configuration(plugin);
-
- //if contains, the inventory is open
- if (!DATA.containsKey(player.getUniqueId())) {
- DATA.put(player.getUniqueId(), YearMonth.now());
- inventory = Bukkit.createInventory(null, 54,
- config.getCalendarMenuName());
- } else {
- inventory = player.getOpenInventory().getTopInventory();
- }
- period = DATA.get(player.getUniqueId());
- }
-
- /**
- * Removes the Player from the Calendar data, so that when he reopens the
- * Calendar the period will be the current one
- *
- * @param player player
- */
- public static void remove(Player player) {
- DATA.remove(player.getUniqueId());
- }
-
- /**
- * Goes to the next month
- */
- public void nextMonth() {
- debug("Current: " + period);
- if (period.plusMonths(1).isAfter(YearMonth.now())) {
- return;
- }
- period = period.plusMonths(1);
- debug("Changed: " + period);
- DATA.put(player.getUniqueId(), period);
-
- loadValues();
- }
-
- /**
- * Goes to the previous month
- */
- public void previousMonth() {
- debug("Current: " + period);
- period = period.minusMonths(1);
- debug("Changed: " + period);
- DATA.put(player.getUniqueId(), period);
- loadValues();
- }
-
- /**
- * Opens the Calendar
- */
- public void open() {
- configureInventory();
- loadValues();
- player.openInventory(inventory);
- }
-
- /**
- * Cleans the inventory so that it can be reused
- */
- private void cleanInventory() {
- int index = 18;
- ItemStack air = new ItemStack(Objects.requireNonNull(XMaterial.AIR.parseMaterial()), 1);
- for (int i = index; i < (index + 31); i++) {
- inventory.setItem(i, air);
- }
- }
-
- /**
- * Load the cryptocoins values and display them on inventory
- */
- private void loadValues() {
- debug("Loading values for period: " + period);
- cleanInventory();
- ItemStack day = XMaterial.GRAY_STAINED_GLASS_PANE.parseItem();
-
- ItemStack periodItem = configurePeriodItem(period.toString());
- inventory.setItem(6, periodItem);
-
- Month month = period.getMonth();
- int index = 18;
- int days = month.length(period.isLeapYear());
-
- for (int x = 1; x <= days; x++) {
- ItemMeta dayMeta = day.getItemMeta();
- dayMeta.setDisplayName(MessageFormat.format(
- config.getCalendarMenuDayItemName(), x));
- LocalDate date = LocalDate.of(period.getYear(), month, x);
- LocalDate today = LocalDate.now();
-
- if (date.isAfter(today)) {
- ArrayList< String> lore = new ArrayList<>();
- lore.add(config.getCalendarMenuNoExchangeRate());
- dayMeta.setLore(lore);
- } else {
- ExchangeRate er = plugin.getExchangeRates().getExchangeRate(date);
-
- if (er == null) {
- er = new ExchangeRate();
- }
- configureItemDayLore(er, dayMeta, day);
- }
-
- day.setAmount(x);
- day.setItemMeta(dayMeta);
- inventory.setItem(index, day);
- index++;
- }
- debug("Finished loading for period " + period);
- }
-
- /**
- * Configures the lore of item Day
- *
- * @param meta
- * @param itemStack
- */
- private void configureItemDayLore(ExchangeRate er, ItemMeta meta,
- ItemStack itemStack) {
- List< String> lore = config.getLoreOfTheDayItem();
- //Configurando a linha de valores das moedas
- List< String> coinsLine = new ArrayList<>();
- List< String> coins = config.getCoins();
-
- for (String coin : coins) {
- BigDecimal coinValue = er.getCoinValue(coin);
- String format;
- if (coinValue.equals(new BigDecimal(-1))) {
- format = MessageFormat.format(config.getCalendarMenuCoinLine(),
- coin, config.getItemDayError());
- } else {
- format = MessageFormat.format(config.getCalendarMenuCoinLine(),
- coin, Formatter.formatServerCurrency(coinValue));
- }
- coinsLine.add(format);
- }
-
- //Adicionando a linha de valores à lore
- int indexB = 0;
- String remove = null;
- for (int i = 0; i < lore.size(); i++) {
- String s = lore.get(i);
- if (s.contains("{0}")) {
- indexB = i;
- remove = s;
- }
- }
-
- lore.addAll(indexB, coinsLine);
- if (remove != null) {
- lore.remove(remove);
- }
-
- meta.setLore(lore);
- itemStack.setItemMeta(meta);
- }
-
- /**
- * Configures the inventory
- */
- private void configureInventory() {
- ItemStack back = configureGenericItem(XMaterial.ARROW,
- config.getCalendarMenuBackButtonName());
- ItemStack nextMonth = configureGenericItem(XMaterial.OAK_BUTTON,
- config.getCalendarMenuNextMonthButtonName());
- ItemStack previousMonth = configureGenericItem(XMaterial.OAK_BUTTON,
- config.getCalendarMenuPreviousMonthButtonName());
- ItemStack periodItem = configurePeriodItem(period.toString());
- ItemStack blackGlass = configureGenericItem(
- XMaterial.BLACK_STAINED_GLASS_PANE, " ");
- ItemStack greyGlass = configureGenericItem(
- XMaterial.GRAY_STAINED_GLASS_PANE, " ");
- setButtonsPositions(back, nextMonth, previousMonth, periodItem,
- blackGlass, greyGlass);
- }
-
- /**
- * Configures the Period item
- *
- * @param date date
- * @return Period item
- */
- private ItemStack configurePeriodItem(String date) {
- ItemStack item = new ItemStack(Objects.requireNonNull(XMaterial.FILLED_MAP.parseMaterial(true)));
- ItemMeta meta = item.getItemMeta();
- meta.setDisplayName(date);
- item.setItemMeta(meta);
- return item;
- }
-
- /**
- * Configures a generic item
- *
- * @param material material
- * @param name name
- * @return the item
- */
- private ItemStack configureGenericItem(XMaterial material, String name) {
- ItemStack stack = material.parseItem();
- if (stack == null) {
- stack = new ItemStack(Material.STONE);
- }
- ItemMeta meta = stack.getItemMeta();
- meta.setDisplayName(name);
- stack.setItemMeta(meta);
- return stack;
- }
-
- /**
- * Sets the buttons' positions
- *
- * @param back
- * @param nextMonth
- * @param previousMonth
- * @param periodItem
- * @param blackGlass
- * @param greyGlass
- */
- private void setButtonsPositions(ItemStack back, ItemStack nextMonth,
- ItemStack previousMonth, ItemStack periodItem, ItemStack blackGlass,
- ItemStack greyGlass) {
- inventory.setItem(0, blackGlass);
- inventory.setItem(1, back);
- inventory.setItem(2, blackGlass);
- inventory.setItem(3, greyGlass);
- inventory.setItem(4, blackGlass);
- inventory.setItem(5, previousMonth);
- inventory.setItem(6, periodItem);
- inventory.setItem(7, nextMonth);
- inventory.setItem(8, blackGlass);
- }
-}
diff --git a/src/main/java/net/epconsortium/cryptomarket/ui/CalendarListener.java b/src/main/java/net/epconsortium/cryptomarket/ui/CalendarListener.java
deleted file mode 100644
index d963506..0000000
--- a/src/main/java/net/epconsortium/cryptomarket/ui/CalendarListener.java
+++ /dev/null
@@ -1,88 +0,0 @@
-package net.epconsortium.cryptomarket.ui;
-
-import java.util.Objects;
-import org.bukkit.entity.Player;
-import org.bukkit.event.EventHandler;
-import org.bukkit.event.Listener;
-import org.bukkit.event.inventory.InventoryClickEvent;
-
-import net.epconsortium.cryptomarket.CryptoMarket;
-import net.epconsortium.cryptomarket.util.Configuration;
-import org.bukkit.event.inventory.InventoryCloseEvent;
-/**
- * Class used to listen to clicks on the Calendar menu and process them
- *
- * @author roinujnosde
- */
-public class CalendarListener implements Listener {
-
- private final CryptoMarket plugin;
- private final Configuration config;
-
- public CalendarListener(CryptoMarket plugin) {
- this.plugin = Objects.requireNonNull(plugin);
- config = new Configuration(plugin);
- }
-
- @EventHandler
- public void onInventoryClickEvent(InventoryClickEvent event) {
- Player player = (Player) event.getWhoClicked();
-
- if (event.getView().getTitle().equals(
- config.getCalendarMenuName())) {
- event.setCancelled(true);
-
- if (processBackButton(event, player)) {
- return;
- }
- if (processPreviousMonthButton(event, player)) {
- return;
- }
- processNextMonthButton(event, player);
- }
-
- }
-
- @EventHandler
- public void onInventoryClose(InventoryCloseEvent event) {
- String name = event.getView().getTitle();
- if (name == null || !name.equals(config.getCalendarMenuName())) {
- return;
- }
-
- Player player = (Player) event.getPlayer();
- Calendar.remove(player);
- }
-
- private boolean processBackButton(InventoryClickEvent event, Player player) {
- if (event.getCurrentItem().getItemMeta().getDisplayName().equals(
- config.getCalendarMenuBackButtonName())) {
- player.closeInventory();
- Menu menu = new Menu(plugin, player);
- menu.open();
- return true;
- }
-
- return false;
- }
-
- private boolean processNextMonthButton(InventoryClickEvent event, Player player) {
- if (event.getCurrentItem().getItemMeta().getDisplayName().equals(
- config.getCalendarMenuNextMonthButtonName())) {
- Calendar c = new Calendar(plugin, player);
- c.nextMonth();
- return true;
- }
- return false;
- }
-
- private boolean processPreviousMonthButton(InventoryClickEvent event, Player player) {
- if (event.getCurrentItem().getItemMeta().getDisplayName().equals(
- config.getCalendarMenuPreviousMonthButtonName())) {
- Calendar c = new Calendar(plugin, player);
- c.previousMonth();
- return true;
- }
- return false;
- }
-}
diff --git a/src/main/java/net/epconsortium/cryptomarket/ui/ComponentImpl.java b/src/main/java/net/epconsortium/cryptomarket/ui/ComponentImpl.java
index 661fef8..d9f783b 100644
--- a/src/main/java/net/epconsortium/cryptomarket/ui/ComponentImpl.java
+++ b/src/main/java/net/epconsortium/cryptomarket/ui/ComponentImpl.java
@@ -104,6 +104,11 @@ public Builder withSlot(int slot) {
return this;
}
+ public Builder withAmount(int amount) {
+ component.item.setAmount(amount);
+ return this;
+ }
+
public Component build() {
ItemMeta itemMeta = component.getItemMeta();
if (itemMeta != null) {
diff --git a/src/main/java/net/epconsortium/cryptomarket/ui/MenuListener.java b/src/main/java/net/epconsortium/cryptomarket/ui/MenuListener.java
deleted file mode 100644
index bbfb955..0000000
--- a/src/main/java/net/epconsortium/cryptomarket/ui/MenuListener.java
+++ /dev/null
@@ -1,139 +0,0 @@
-package net.epconsortium.cryptomarket.ui;
-
-import net.epconsortium.cryptomarket.CryptoMarket;
-import net.epconsortium.cryptomarket.conversation.NegotiationConversation;
-import net.epconsortium.cryptomarket.finances.ExchangeRates;
-import net.epconsortium.cryptomarket.finances.Negotiation;
-import net.epconsortium.cryptomarket.ui.frames.RankingFrame;
-import net.epconsortium.cryptomarket.util.Configuration;
-import org.bukkit.entity.Player;
-import org.bukkit.event.EventHandler;
-import org.bukkit.event.Listener;
-import org.bukkit.event.inventory.InventoryClickEvent;
-
-import java.text.MessageFormat;
-import java.util.Objects;
-import java.util.concurrent.ExecutionException;
-
-/**
- * Class used to listen to clicks on the Menu menu and process them
- *
- * @author roinujnosde
- */
-public class MenuListener implements Listener {
-
- private final CryptoMarket plugin;
- private final Configuration config;
-
- public MenuListener(CryptoMarket plugin) {
- this.plugin = Objects.requireNonNull(plugin);
- config = new Configuration(plugin);
- }
-
- @EventHandler
- public void onInventoryClick(InventoryClickEvent event) {
- Player player = (Player) event.getWhoClicked();
-
- if (event.getView().getTitle().equals(config.getMenuName())) {
- event.setCancelled(true);
-
- if (processCoinsButton(event, player)) {
- return;
- }
- if (processUpdateButton(event, player)) {
- return;
- }
- if (processRankingButton(event, player)) {
- return;
- }
- processCalendarButton(event, player);
- }
- }
-
- private boolean processUpdateButton(InventoryClickEvent event, Player player) {
- if (event.getCurrentItem().getItemMeta().getDisplayName().equals(config.getButtonUpdateName())) {
- if (!player.hasPermission("cryptomarket.update")) {
- player.sendMessage(config.getMessageErrorNoPermission());
- player.closeInventory();
- return true;
- }
- if (ExchangeRates.errorOccurred()) {
- ExchangeRates er = plugin.getExchangeRates();
- er.updateAll();
- player.closeInventory();
-
- String msg = config.getMessageUpdatingContent();
- msg = MessageFormat.format(msg, er.getMinutesToUpdate());
- player.sendMessage(msg);
- return true;
- } else {
- player.sendMessage(config.getMessageContentAlreadyUptodate());
- player.closeInventory();
- return true;
- }
- }
- return false;
- }
-
- private boolean processCoinsButton(InventoryClickEvent event, Player player) {
- if (event.getCurrentItem().getItemMeta().getDisplayName()
- .equals(config.getButtonCoinsName())) {
- if (!player.hasPermission("cryptomarket.negotiate")) {
- player.sendMessage(config.getMessageErrorNoPermission());
- player.closeInventory();
- return true;
- }
- if (event.getClick().isLeftClick()) {
- NegotiationConversation conv = new NegotiationConversation(
- plugin, Negotiation.SELL, player);
- conv.start();
- player.closeInventory();
- return true;
- }
-
- if (event.getClick().isRightClick()) {
- NegotiationConversation conv = new NegotiationConversation(
- plugin, Negotiation.PURCHASE, player);
- conv.start();
- player.closeInventory();
- return true;
- }
- }
- return false;
- }
-
- private boolean processRankingButton(InventoryClickEvent event, Player player) {
- if (event.getCurrentItem().getItemMeta().getDisplayName()
- .equals(config.getButtonRankingName())) {
- if (!player.hasPermission("cryptomarket.ranking")) {
- player.sendMessage(config.getMessageErrorNoPermission());
- player.closeInventory();
- return true;
- }
- //todo trocar parent para o MenuFrame
- try {
- InventoryDrawer.getInstance().open(new RankingFrame(null, player));
- } catch (ExecutionException | InterruptedException e) {
- player.sendMessage(config.getMessageErrorAccessingRankingData());
- e.printStackTrace();
- }
- return true;
- }
- return false;
- }
-
- private boolean processCalendarButton(InventoryClickEvent event, Player player) {
- if (event.getCurrentItem().getItemMeta().getDisplayName()
- .equals(config.getButtonCalendarName())) {
- if (!player.hasPermission("cryptomarket.calendar")) {
- player.sendMessage(config.getMessageErrorNoPermission());
- player.closeInventory();
- return true;
- }
- Calendar calendar = new Calendar(plugin, player);
- calendar.open();
- return true;
- }
- return false;
- }
-}
diff --git a/src/main/java/net/epconsortium/cryptomarket/ui/frames/CalendarFrame.java b/src/main/java/net/epconsortium/cryptomarket/ui/frames/CalendarFrame.java
new file mode 100644
index 0000000..fb40901
--- /dev/null
+++ b/src/main/java/net/epconsortium/cryptomarket/ui/frames/CalendarFrame.java
@@ -0,0 +1,169 @@
+package net.epconsortium.cryptomarket.ui.frames;
+
+import com.cryptomorin.xseries.XMaterial;
+import net.epconsortium.cryptomarket.finances.ExchangeRate;
+import net.epconsortium.cryptomarket.ui.*;
+import net.epconsortium.cryptomarket.ui.ComponentImpl.Builder;
+import net.epconsortium.cryptomarket.util.Formatter;
+import org.bukkit.entity.Player;
+import org.bukkit.event.inventory.ClickType;
+import org.jetbrains.annotations.NotNull;
+import org.jetbrains.annotations.Nullable;
+
+import java.math.BigDecimal;
+import java.text.MessageFormat;
+import java.time.LocalDate;
+import java.time.Month;
+import java.time.YearMonth;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+public class CalendarFrame extends Frame {
+
+ private @NotNull YearMonth period = YearMonth.now();
+
+ public CalendarFrame(@Nullable Frame parent, @NotNull Player viewer) {
+ super(parent, viewer);
+ }
+
+ @Override
+ public @NotNull String getTitle() {
+ return configuration.getCalendarMenuName();
+ }
+
+ @Override
+ public int getSize() {
+ return 54;
+ }
+
+ @Override
+ public void createComponents() {
+ addDays();
+ add(back());
+ add(previousMonthItem());
+ add(nextMonthItem());
+ add(period());
+ addPanels();
+ }
+
+ /**
+ * Shows data from the next month
+ * It does nothing if the {@link Player} is viewing the current month
+ */
+ public void nextMonth() {
+ if (period.plusMonths(1).isAfter(YearMonth.now())) {
+ return;
+ }
+ period = period.plusMonths(1);
+ InventoryDrawer.getInstance().open(this);
+ }
+
+ /**
+ * Shows data from the previous month
+ */
+ public void previousMonth() {
+ period = period.minusMonths(1);
+ }
+
+ private void addPanels() {
+ int[] blackSlots = {0, 2, 4, 8};
+ int[] graySlots = {3};
+ Components.addPanels(this, XMaterial.BLACK_STAINED_GLASS_PANE, blackSlots);
+ Components.addPanels(this, XMaterial.GRAY_STAINED_GLASS_PANE, graySlots);
+ }
+
+ private Component period() {
+ return new ComponentImpl(period.toString(), null, XMaterial.FILLED_MAP, 6);
+ }
+
+ private Component nextMonthItem() {
+ Component component = new Builder(XMaterial.OAK_BUTTON)
+ .withDisplayName(configuration.getCalendarMenuNextMonthButtonName()).withSlot(7).build();
+ component.setListener(ClickType.LEFT, this::nextMonth);
+ return component;
+ }
+
+ private Component previousMonthItem() {
+ Component component = new Builder(XMaterial.OAK_BUTTON)
+ .withDisplayName(configuration.getCalendarMenuPreviousMonthButtonName()).withSlot(5).build();
+ component.setListener(ClickType.LEFT, this::previousMonth);
+ return component;
+ }
+
+ private Component back() {
+ Component component = new Builder(XMaterial.ARROW).withSlot(1)
+ .withDisplayName(configuration.getCalendarMenuBackButtonName()).build();
+ component.setListener(ClickType.LEFT, () -> InventoryDrawer.getInstance().open(getParent()));
+ return component;
+ }
+
+ private void addDays() {
+ Month month = period.getMonth();
+ int slot = 18;
+ int days = month.length(period.isLeapYear());
+
+ for (int day = 1; day <= days; day++) {
+ LocalDate date = LocalDate.of(period.getYear(), month, day);
+
+ List lore;
+ if (date.isAfter(LocalDate.now())) {
+ lore = Collections.singletonList(configuration.getCalendarMenuNoExchangeRate());
+ } else {
+ ExchangeRate er = plugin.getExchangeRates().getExchangeRate(date);
+ if (er == null) {
+ er = new ExchangeRate();
+ }
+ lore = getDayItemLore(er);
+ }
+
+ Component component = new Builder(XMaterial.GRAY_STAINED_GLASS_PANE).withAmount(day).withSlot(slot)
+ .withLore(lore).withDisplayName(getDayItemName(day)).build();
+ add(component);
+ slot++;
+ }
+ }
+
+ @NotNull
+ private String getDayItemName(int day) {
+ return MessageFormat.format(configuration.getCalendarMenuDayItemName(), day);
+ }
+
+ private List getDayItemLore(ExchangeRate rate) {
+ List lore = configuration.getLoreOfTheDayItem();
+ //Configurando a linha de valores das moedas
+ List coinsLine = new ArrayList<>();
+ List coins = configuration.getCoins();
+
+ for (String coin : coins) {
+ BigDecimal coinValue = rate.getCoinValue(coin);
+ String format;
+ if (coinValue.equals(new BigDecimal(-1))) {
+ format = MessageFormat.format(configuration.getCalendarMenuCoinLine(), coin,
+ configuration.getItemDayError());
+ } else {
+ format = MessageFormat.format(configuration.getCalendarMenuCoinLine(), coin,
+ Formatter.formatServerCurrency(coinValue));
+ }
+ coinsLine.add(format);
+ }
+
+ //Adicionando a linha de valores à lore
+ int indexB = 0;
+ String remove = null;
+ for (int i = 0; i < lore.size(); i++) {
+ String s = lore.get(i);
+ if (s.contains("{0}")) {
+ indexB = i;
+ remove = s;
+ }
+ }
+
+ lore.addAll(indexB, coinsLine);
+ if (remove != null) {
+ lore.remove(remove);
+ }
+
+ return lore;
+ }
+}
diff --git a/src/main/java/net/epconsortium/cryptomarket/ui/frames/MenuFrame.java b/src/main/java/net/epconsortium/cryptomarket/ui/frames/MenuFrame.java
index 8fe6631..be2a8a2 100644
--- a/src/main/java/net/epconsortium/cryptomarket/ui/frames/MenuFrame.java
+++ b/src/main/java/net/epconsortium/cryptomarket/ui/frames/MenuFrame.java
@@ -1,19 +1,21 @@
package net.epconsortium.cryptomarket.ui.frames;
import com.cryptomorin.xseries.XMaterial;
+import net.epconsortium.cryptomarket.conversation.NegotiationConversation;
import net.epconsortium.cryptomarket.database.dao.Investor;
import net.epconsortium.cryptomarket.finances.ExchangeRate;
import net.epconsortium.cryptomarket.finances.ExchangeRates;
+import net.epconsortium.cryptomarket.finances.Negotiation;
import net.epconsortium.cryptomarket.ui.Component;
import net.epconsortium.cryptomarket.ui.ComponentImpl;
import net.epconsortium.cryptomarket.ui.ComponentImpl.Builder;
import net.epconsortium.cryptomarket.ui.Frame;
+import net.epconsortium.cryptomarket.ui.InventoryDrawer;
import net.epconsortium.cryptomarket.util.Formatter;
-import org.bukkit.Material;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
+import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemFlag;
-import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -23,11 +25,10 @@
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
+import java.util.Objects;
-import static com.cryptomorin.xseries.XMaterial.BLACK_STAINED_GLASS_PANE;
-import static com.cryptomorin.xseries.XMaterial.GRAY_STAINED_GLASS_PANE;
+import static com.cryptomorin.xseries.XMaterial.*;
import static net.epconsortium.cryptomarket.ui.Components.addPanels;
-import static net.epconsortium.cryptomarket.ui.Components.generic;
public class MenuFrame extends Frame {
private final Investor investor;
@@ -55,11 +56,41 @@ public void createComponents() {
add(coins());
add(wallet());
add(ranking());
- add(generic(XMaterial.FILLED_MAP, configuration.getButtonCalendarName(), 25));
- add(generic(XMaterial.STRUCTURE_VOID, configuration.getButtonUpdateName(), 43));
+ add(calendar());
+ add(update());
addGlasses();
}
+ @NotNull
+ private Component update() {
+ ComponentImpl update = new ComponentImpl(configuration.getButtonUpdateName(), null, STRUCTURE_VOID, 43);
+ update.setPermission(ClickType.LEFT, "cryptomarket.update");
+ update.setListener(ClickType.LEFT, () -> {
+ if (ExchangeRates.errorOccurred()) {
+ ExchangeRates er = plugin.getExchangeRates();
+ er.updateAll();
+ getViewer().closeInventory();
+
+ String msg = configuration.getMessageUpdatingContent();
+ msg = MessageFormat.format(msg, er.getMinutesToUpdate());
+ getViewer().sendMessage(msg);
+ } else {
+ getViewer().sendMessage(configuration.getMessageContentAlreadyUptodate());
+ getViewer().closeInventory();
+ }
+ });
+ return update;
+ }
+
+ @NotNull
+ private Component calendar() {
+ ComponentImpl calendar = new ComponentImpl(configuration.getCalendarMenuBackButtonName(), null, FILLED_MAP, 25);
+ calendar.setPermission(ClickType.LEFT, "cryptomarket.calendar");
+ calendar.setListener(ClickType.LEFT, () ->
+ InventoryDrawer.getInstance().open(new CalendarFrame(this, getViewer())));
+ return calendar;
+ }
+
private void addGlasses() {
int[] blackSlots = {28, 0, 3, 5, 8, 10, 16, 21, 23, 34, 45, 46, 49, 52, 52, 31};
addPanels(this, BLACK_STAINED_GLASS_PANE, blackSlots);
@@ -115,18 +146,24 @@ private Component wallet() {
}
private Component coins() {
- ItemStack coins = XMaterial.SUNFLOWER.parseItem(true);
- if (coins == null) {
- coins = new ItemStack(Material.STONE);
- }
+ Component coins = new ComponentImpl(configuration.getButtonCoinsName(), getCoinsLore(), SUNFLOWER, 12);
- ItemMeta meta = coins.getItemMeta();
+ ItemMeta meta = Objects.requireNonNull(coins.getItemMeta());
meta.addEnchant(Enchantment.ARROW_DAMAGE, 1, false);
meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
- meta.setDisplayName(configuration.getButtonCoinsName());
-
coins.setItemMeta(meta);
- return new ComponentImpl(configuration.getButtonCoinsName(), getCoinsLore(), coins, 12);
+
+ getViewer().closeInventory();
+ coins.setPermission(ClickType.RIGHT, "cryptomarket.negotiate");
+ coins.setListener(ClickType.RIGHT, () -> {
+ new NegotiationConversation(plugin, Negotiation.PURCHASE, getViewer()).start();
+ });
+ coins.setPermission(ClickType.LEFT, "cryptomarket.negotiate");
+ coins.setListener(ClickType.LEFT, () -> {
+ new NegotiationConversation(plugin, Negotiation.SELL, getViewer()).start();
+ });
+
+ return coins;
}
private List getWalletLore() {
@@ -206,7 +243,19 @@ private List getCoinsLore() {
}
private Component ranking() {
- return new Builder(XMaterial.PLAYER_HEAD).withDisplayName(configuration.getButtonRankingName()).withSlot(37)
- .build();
+ Component component = new Builder(PLAYER_HEAD).withDisplayName(configuration.getButtonRankingName())
+ .withSlot(37).build();
+ component.setPermission(ClickType.LEFT, "cryptomarket.ranking");
+ component.setListener(ClickType.LEFT, () -> {
+ ExchangeRate exchangeRate = plugin.getExchangeRates().getExchangeRate(LocalDate.now());
+ if (exchangeRate == null) {
+ getViewer().sendMessage(configuration.getMessageErrorAccessingRankingData());
+ getViewer().closeInventory();
+ return;
+ }
+ InventoryDrawer.getInstance().open(new RankingFrame(this, getViewer(), exchangeRate));
+ });
+ return component;
}
+
}
diff --git a/src/main/java/net/epconsortium/cryptomarket/ui/frames/RankingFrame.java b/src/main/java/net/epconsortium/cryptomarket/ui/frames/RankingFrame.java
index 6eb4360..6e9ebed 100644
--- a/src/main/java/net/epconsortium/cryptomarket/ui/frames/RankingFrame.java
+++ b/src/main/java/net/epconsortium/cryptomarket/ui/frames/RankingFrame.java
@@ -1,16 +1,16 @@
package net.epconsortium.cryptomarket.ui.frames;
import com.cryptomorin.xseries.XMaterial;
-import net.epconsortium.cryptomarket.CryptoMarket;
import net.epconsortium.cryptomarket.database.dao.Investor;
import net.epconsortium.cryptomarket.finances.Economy;
import net.epconsortium.cryptomarket.finances.ExchangeRate;
import net.epconsortium.cryptomarket.ui.Component;
import net.epconsortium.cryptomarket.ui.ComponentImpl;
import net.epconsortium.cryptomarket.ui.Frame;
-import net.epconsortium.cryptomarket.util.Configuration;
+import net.epconsortium.cryptomarket.ui.InventoryDrawer;
import net.epconsortium.cryptomarket.util.Formatter;
import org.bukkit.entity.Player;
+import org.bukkit.event.inventory.ClickType;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.SkullMeta;
@@ -19,28 +19,25 @@
import java.math.BigDecimal;
import java.text.MessageFormat;
-import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.FormatStyle;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
-import java.util.concurrent.ExecutionException;
import static com.cryptomorin.xseries.XMaterial.*;
import static net.epconsortium.cryptomarket.ui.Components.addPanels;
public class RankingFrame extends Frame {
- private final CryptoMarket plugin = CryptoMarket.getInstance();
private final Economy econ;
private final List richersList;
private final double totalInvestments;
- private final Configuration config;
+ private final ExchangeRate exchangeRate;
- public RankingFrame(@Nullable Frame parent, @NotNull Player viewer) throws ExecutionException, InterruptedException {
+ public RankingFrame(@Nullable Frame parent, @NotNull Player viewer, @NotNull ExchangeRate exchangeRate) {
super(parent, viewer);
- config = new Configuration(plugin);
+ this.exchangeRate = exchangeRate;
econ = plugin.getEconomy();
richersList = econ.getTopInvestors(5);
totalInvestments = econ.getTotalInvestments();
@@ -67,12 +64,6 @@ public void createComponents() {
}
}
- /**
- * Configures the Richer on the index
- *
- * @param index index
- * @return the Richer item
- */
private Component richer(int index, int slot) {
ItemStack head = XMaterial.PLAYER_HEAD.parseItem(true);
ItemMeta meta = Objects.requireNonNull(head).getItemMeta();
@@ -81,16 +72,15 @@ private Component richer(int index, int slot) {
String displayName;
ArrayList lore = new ArrayList<>();
if (index >= richersList.size()) {
- displayName = MessageFormat.format(config.getRankingMenuNoRicherLore(), rank);
+ displayName = MessageFormat.format(configuration.getRankingMenuNoRicherLore(), rank);
head.setItemMeta(meta);
} else {
Investor richer = richersList.get(index);
SkullMeta skullMeta = (SkullMeta) head.getItemMeta();
skullMeta.setOwner(richer.getPlayer().getName());
- displayName = MessageFormat.format(config.getRankingMenuRicherItemName(), rank, richer.getPlayer().getName());
+ displayName = MessageFormat.format(configuration.getRankingMenuRicherItemName(), rank, richer.getPlayer().getName());
- List configLore = config.getRankingMenuRicherItemLore();
- ExchangeRate exchangeRate = plugin.getExchangeRates().getExchangeRate(LocalDate.now());
+ List configLore = configuration.getRankingMenuRicherItemLore();
BigDecimal patrimony = richer.getConvertedPatrimony(exchangeRate);
double percentage = 0;
if (totalInvestments > 0) {
@@ -108,29 +98,26 @@ private Component richer(int index, int slot) {
private Component lastUpdated() {
List lastUpdateLore = new ArrayList<>();
- for (String s : config.getRankingMenuLastUpdatedItemLore()) {
+ for (String s : configuration.getRankingMenuLastUpdatedItemLore()) {
lastUpdateLore.add(MessageFormat.format(s,
econ.getRichersLastUpdate().format(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT))));
}
- return new ComponentImpl(config.getRankingMenuLastUpdatedItemName(), lastUpdateLore, CLOCK, 38);
+ return new ComponentImpl(configuration.getRankingMenuLastUpdatedItemName(), lastUpdateLore, CLOCK, 38);
}
private Component totalInvestments() {
List totalLore = new ArrayList<>();
- for (String s : config.getRankingMenuTotalInvestmentsItemLore()) {
+ for (String s : configuration.getRankingMenuTotalInvestmentsItemLore()) {
totalLore.add(MessageFormat.format(s, Formatter.formatServerCurrency(totalInvestments)));
}
- return new ComponentImpl(config.getRankingMenuTotalInvestmentsItemName(), totalLore, XMaterial.SUNFLOWER, 36);
+ return new ComponentImpl(configuration.getRankingMenuTotalInvestmentsItemName(), totalLore, XMaterial.SUNFLOWER, 36);
}
- /**
- * Configures the Back button
- *
- * @return the back button
- */
private Component backButton() {
- return new ComponentImpl(config.getRankingMenuBackButton(), null, XMaterial.ARROW, 4);
+ ComponentImpl component = new ComponentImpl(configuration.getRankingMenuBackButton(), null, ARROW, 4);
+ component.setListener(ClickType.LEFT, () -> InventoryDrawer.getInstance().open(getParent()));
+ return component;
}
private void addGlasses() {