From f0ba148672896932003fbb7d87122c225c32168a Mon Sep 17 00:00:00 2001 From: Kiran Hart Date: Fri, 6 Jul 2018 23:08:30 -0400 Subject: [PATCH] Added a lot of stuff, added listing the /ah gui w/pagination & a bunch of other stuff --- .idea/artifacts/Auction_House_jar.xml | 8 + .../Maven__commons_io_commons_io_2_4.xml | 13 ++ ..._javax_persistence_persistence_api_1_0.xml | 13 ++ ...Maven__net_milkbowl_vault_VaultAPI_1_6.xml | 13 ++ .../Maven__org_avaje_ebean_2_8_1.xml | 13 ++ ...n__org_bukkit_bukkit_1_9_R0_1_SNAPSHOT.xml | 13 ++ Auction House.iml | 4 + pom.xml | 10 ++ .../java/com/shadebyte/auctionhouse/Core.java | 118 ++++++++++++++ .../com/shadebyte/auctionhouse/Settings.java | 22 +++ .../auctionhouse/api/AuctionAPI.java | 79 ++++++++++ .../auctionhouse/api/enums/Lang.java | 19 +++ .../auctionhouse/api/enums/Permissions.java | 4 +- .../auctionhouse/auction/AuctionItem.java | 144 ++++++++++++++++++ .../auctionhouse/auction/AuctionPlayer.java | 44 ++++++ .../auctionhouse/cmds/CommandManager.java | 14 +- .../cmds/subcmds/SellCommand.java | 54 ++++++- .../auctionhouse/events/AGUIListener.java | 32 ++++ .../auctionhouse/inventory/AGUI.java | 12 +- .../inventory/inventories/AuctionGUI.java | 125 ++++++++++++++- src/main/resources/config.yml | 67 ++++++++ src/main/resources/en_US.lang | 7 + src/main/resources/plugin.yml | 5 + 23 files changed, 825 insertions(+), 8 deletions(-) create mode 100644 .idea/artifacts/Auction_House_jar.xml create mode 100644 .idea/libraries/Maven__commons_io_commons_io_2_4.xml create mode 100644 .idea/libraries/Maven__javax_persistence_persistence_api_1_0.xml create mode 100644 .idea/libraries/Maven__net_milkbowl_vault_VaultAPI_1_6.xml create mode 100644 .idea/libraries/Maven__org_avaje_ebean_2_8_1.xml create mode 100644 .idea/libraries/Maven__org_bukkit_bukkit_1_9_R0_1_SNAPSHOT.xml create mode 100644 src/main/java/com/shadebyte/auctionhouse/Settings.java create mode 100644 src/main/java/com/shadebyte/auctionhouse/events/AGUIListener.java diff --git a/.idea/artifacts/Auction_House_jar.xml b/.idea/artifacts/Auction_House_jar.xml new file mode 100644 index 0000000..6da4101 --- /dev/null +++ b/.idea/artifacts/Auction_House_jar.xml @@ -0,0 +1,8 @@ + + + $USER_HOME$/Desktop/Plugin Testing/Pepper API/plugins + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__commons_io_commons_io_2_4.xml b/.idea/libraries/Maven__commons_io_commons_io_2_4.xml new file mode 100644 index 0000000..bc2aad0 --- /dev/null +++ b/.idea/libraries/Maven__commons_io_commons_io_2_4.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ 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 new file mode 100644 index 0000000..e60370e --- /dev/null +++ b/.idea/libraries/Maven__javax_persistence_persistence_api_1_0.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__net_milkbowl_vault_VaultAPI_1_6.xml b/.idea/libraries/Maven__net_milkbowl_vault_VaultAPI_1_6.xml new file mode 100644 index 0000000..fa96a55 --- /dev/null +++ b/.idea/libraries/Maven__net_milkbowl_vault_VaultAPI_1_6.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ 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 new file mode 100644 index 0000000..91f161a --- /dev/null +++ b/.idea/libraries/Maven__org_avaje_ebean_2_8_1.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/libraries/Maven__org_bukkit_bukkit_1_9_R0_1_SNAPSHOT.xml b/.idea/libraries/Maven__org_bukkit_bukkit_1_9_R0_1_SNAPSHOT.xml new file mode 100644 index 0000000..2fb4911 --- /dev/null +++ b/.idea/libraries/Maven__org_bukkit_bukkit_1_9_R0_1_SNAPSHOT.xml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/Auction House.iml b/Auction House.iml index f6807d1..db7e255 100644 --- a/Auction House.iml +++ b/Auction House.iml @@ -31,5 +31,9 @@ + + + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 133024c..a53700b 100644 --- a/pom.xml +++ b/pom.xml @@ -63,6 +63,10 @@ sonatype https://oss.sonatype.org/content/groups/public/ + + vault-repo + http://nexus.hc.to/content/repositories/pub_releases + @@ -77,5 +81,11 @@ commons-io 2.4 + + net.milkbowl.vault + VaultAPI + 1.6 + provided + diff --git a/src/main/java/com/shadebyte/auctionhouse/Core.java b/src/main/java/com/shadebyte/auctionhouse/Core.java index b7e9025..8c8cb81 100644 --- a/src/main/java/com/shadebyte/auctionhouse/Core.java +++ b/src/main/java/com/shadebyte/auctionhouse/Core.java @@ -1,15 +1,82 @@ package com.shadebyte.auctionhouse; +import com.massivestats.MassiveStats; +import com.shadebyte.auctionhouse.auction.AuctionItem; +import com.shadebyte.auctionhouse.cmds.CommandManager; +import com.shadebyte.auctionhouse.events.AGUIListener; +import com.shadebyte.auctionhouse.util.ConfigWrapper; +import com.shadebyte.auctionhouse.util.Debugger; +import com.shadebyte.auctionhouse.util.Locale; +import net.milkbowl.vault.economy.Economy; +import org.bukkit.Bukkit; +import org.bukkit.plugin.PluginManager; +import org.bukkit.plugin.RegisteredServiceProvider; import org.bukkit.plugin.java.JavaPlugin; +import java.io.File; +import java.util.ArrayList; +import java.util.List; + public final class Core extends JavaPlugin { + //Instance Variable private static Core instance; + //Instance for the command management system + private CommandManager commandManager; + + //Economy + private static Economy economy = null; + + //Settings + private Settings settings = null; + + //Data config instance from the config wrapper + private ConfigWrapper data; + + //Language system instance + private Locale locale; + + //Storage + public List auctionItems; + @Override public void onEnable() { // Plugin startup logic instance = this; + + //Locales + Locale.init(this); + Locale.saveDefaultLocale("en_US"); + this.locale = Locale.getLocale(this.getConfig().getString("Locale", "en_US")); + + settings = new Settings(); + + initDataFiles(); + + commandManager = new CommandManager(); + commandManager.initialize(); + + initEvents(); + initStorage(); + + try { + MassiveStats stats = new MassiveStats(this); + stats.setListenerDisabled(false); + } catch (Exception e) { + Debugger.report(e); + } + + try { + Bukkit.getServer().getScheduler().runTaskTimer(this, () -> { + auctionItems.forEach(auctionItem -> auctionItem.updateTime(5)); + auctionItems.forEach(auctionItem -> { + if (auctionItem.getTime() <= 0) auctionItems.remove(auctionItem); + }); + }, 0, 20 * 5); + } catch (Exception e) { + //Debugger.report(e); + } } @Override @@ -18,7 +85,58 @@ public final class Core extends JavaPlugin { instance = null; } + private boolean setupEconomy() { + if (getServer().getPluginManager().getPlugin("Vault") == null) { + return false; + } + RegisteredServiceProvider rsp = getServer().getServicesManager().getRegistration(Economy.class); + if (rsp == null) { + return false; + } + economy = rsp.getProvider(); + return economy != null; + } + + private void initDataFiles() { + getConfig().options().copyDefaults(true); + saveDefaultConfig(); + data = new ConfigWrapper(this, "", "data.yml"); + if (!new File(this.getDataFolder(), "data.yml").exists()) { + data.getConfig().createSection("data"); + } + data.saveConfig(); + } + + private void initEvents() { + PluginManager pm = Bukkit.getPluginManager(); + pm.registerEvents(new AGUIListener(), this); + } + + private void initStorage() { + auctionItems = new ArrayList<>(); + } + public static Core getInstance() { return instance; } + + public CommandManager getCommandManager() { + return commandManager; + } + + public Locale getLocale() { + return locale; + } + + public ConfigWrapper getData() { + return data; + } + + public static Economy getEconomy() { + return economy; + } + + public Settings getSettings() { + return settings; + } } diff --git a/src/main/java/com/shadebyte/auctionhouse/Settings.java b/src/main/java/com/shadebyte/auctionhouse/Settings.java new file mode 100644 index 0000000..e8ad905 --- /dev/null +++ b/src/main/java/com/shadebyte/auctionhouse/Settings.java @@ -0,0 +1,22 @@ +package com.shadebyte.auctionhouse; + +import com.shadebyte.auctionhouse.api.enums.Lang; + +/** + * The current file has been created by Kiran Hart + * Date Created: 7/6/2018 + * Time Created: 6:46 PM + * Usage of any code found within this class is prohibited unless given explicit permission otherwise. + */ +public class Settings { + + private String prefix; + + public Settings() { + prefix = Core.getInstance().getLocale().getMessage(Lang.PREFIX.getNode()) + " "; + } + + public String getPrefix() { + return prefix; + } +} diff --git a/src/main/java/com/shadebyte/auctionhouse/api/AuctionAPI.java b/src/main/java/com/shadebyte/auctionhouse/api/AuctionAPI.java index ee8c5c0..58a43cb 100644 --- a/src/main/java/com/shadebyte/auctionhouse/api/AuctionAPI.java +++ b/src/main/java/com/shadebyte/auctionhouse/api/AuctionAPI.java @@ -1,5 +1,17 @@ package com.shadebyte.auctionhouse.api; +import com.shadebyte.auctionhouse.Core; +import com.shadebyte.auctionhouse.api.enums.Version; +import org.bukkit.ChatColor; +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.PlayerInventory; +import org.bukkit.inventory.meta.ItemMeta; + +import java.util.ArrayList; +import java.util.List; + /** * The current file has been created by Kiran Hart * Date Created: 7/6/2018 @@ -7,4 +19,71 @@ package com.shadebyte.auctionhouse.api; * Usage of any code found within this class is prohibited unless given explicit permission otherwise. */ public class AuctionAPI { + + private static AuctionAPI instance; + + private AuctionAPI() { + } + + public static AuctionAPI getInstance() { + if (instance == null) { + instance = new AuctionAPI(); + } + return instance; + } + + public ItemStack createConfigItem(String node) { + String[] rawItem = Core.getInstance().getConfig().getString(node + ".item").split(":"); + ItemStack stack = new ItemStack(Material.valueOf(rawItem[0].toUpperCase()), 1, Short.parseShort(rawItem[1])); + ItemMeta meta = stack.getItemMeta(); + meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', Core.getInstance().getConfig().getString(node + ".name"))); + List lore = new ArrayList<>(); + Core.getInstance().getConfig().getStringList(node + ".lore").forEach(s -> lore.add(ChatColor.translateAlternateColorCodes('&', s))); + meta.setLore(lore); + stack.setItemMeta(meta); + return stack; + } + + public int availableSlots(PlayerInventory inventory) { + int count = 0; + for (ItemStack i : inventory) { + if (i == null) { + count++; + } + } + return count; + } + + @SuppressWarnings("deprecation") + public static ItemStack getItemInHand(Player player) { + if (Version.getCurrentVersion().getCurrentVersionInteger() >= Version.v1_9_R1.getCurrentVersionInteger()) { + return player.getInventory().getItemInMainHand(); + } else { + return player.getItemInHand(); + } + } + + @SuppressWarnings("deprecation") + public static void setItemInHand(Player player, ItemStack item) { + if (Version.getCurrentVersion().getCurrentVersionInteger() >= Version.v1_9_R1.getCurrentVersionInteger()) { + player.getInventory().setItemInMainHand(item); + } else { + player.setItemInHand(item); + } + } + + public String timeLeft(int totalSecs) { + int hours = totalSecs / 3600; + int minutes = (totalSecs % 3600) / 60; + int seconds = totalSecs % 60; + return String.format("%02d:%02d:%02d", hours, minutes, seconds); + } + + public ItemStack fill(String name) { + ItemStack stack = new ItemStack(Material.STAINED_GLASS_PANE, 1, (short) 1); + ItemMeta meta = stack.getItemMeta(); + meta.setDisplayName(ChatColor.translateAlternateColorCodes('&', "&6&l" + name)); + stack.setItemMeta(meta); + return stack; + } } diff --git a/src/main/java/com/shadebyte/auctionhouse/api/enums/Lang.java b/src/main/java/com/shadebyte/auctionhouse/api/enums/Lang.java index c9fdc8f..9e7dcf2 100644 --- a/src/main/java/com/shadebyte/auctionhouse/api/enums/Lang.java +++ b/src/main/java/com/shadebyte/auctionhouse/api/enums/Lang.java @@ -7,4 +7,23 @@ package com.shadebyte.auctionhouse.api.enums; * Usage of any code found within this class is prohibited unless given explicit permission otherwise. */ public enum Lang { + + PREFIX("prefix"), + NO_PERMISSION("nopermission"), + PLAYERS_ONLY("playersonly"), + + INVALID_SUBCOMMAND("cmd.invalid"), + CMD_SELL("cmd.sell"), + + ; + + String node; + + Lang(String node) { + this.node = node; + } + + public String getNode() { + return node; + } } diff --git a/src/main/java/com/shadebyte/auctionhouse/api/enums/Permissions.java b/src/main/java/com/shadebyte/auctionhouse/api/enums/Permissions.java index dab0dd5..4976f11 100644 --- a/src/main/java/com/shadebyte/auctionhouse/api/enums/Permissions.java +++ b/src/main/java/com/shadebyte/auctionhouse/api/enums/Permissions.java @@ -8,7 +8,9 @@ package com.shadebyte.auctionhouse.api.enums; */ public enum Permissions { - BASE("AuctionHouse"); + BASE("AuctionHouse"), + MAX_AUCTIONS(BASE.getNode() + ".maxauctions"), + ; private String node; diff --git a/src/main/java/com/shadebyte/auctionhouse/auction/AuctionItem.java b/src/main/java/com/shadebyte/auctionhouse/auction/AuctionItem.java index 2431524..27dc1e1 100644 --- a/src/main/java/com/shadebyte/auctionhouse/auction/AuctionItem.java +++ b/src/main/java/com/shadebyte/auctionhouse/auction/AuctionItem.java @@ -1,5 +1,18 @@ package com.shadebyte.auctionhouse.auction; +import com.shadebyte.auctionhouse.api.AuctionAPI; +import com.shadebyte.auctionhouse.util.NBTEditor; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.ItemMeta; + +import java.text.NumberFormat; +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +import static org.bukkit.ChatColor.translateAlternateColorCodes; + /** * The current file has been created by Kiran Hart * Date Created: 7/6/2018 @@ -7,4 +20,135 @@ package com.shadebyte.auctionhouse.auction; * Usage of any code found within this class is prohibited unless given explicit permission otherwise. */ public class AuctionItem { + + private Player owner; + private ItemStack item; + private int startPrice; + private int bidIncrement; + private int buyNowPrice; + private int currentPrice; + private int time = 0; + private String key; + + public AuctionItem(ItemStack item, int time, int startPrice, int bidIncrement, int buyNowPrice) { + this.owner = null; + this.item = item.clone(); + this.startPrice = startPrice; + this.bidIncrement = bidIncrement; + this.bidIncrement = bidIncrement; + this.buyNowPrice = buyNowPrice; + this.time = time; + key = UUID.randomUUID().toString(); + } + + public AuctionItem(Player owner, ItemStack item, int time, int startPrice, int bidIncrement, int buyNowPrice) { + this.owner = owner; + this.item = item.clone(); + this.startPrice = startPrice; + this.bidIncrement = bidIncrement; + this.bidIncrement = bidIncrement; + this.buyNowPrice = buyNowPrice; + this.time = time; + this.currentPrice = startPrice; + key = UUID.randomUUID().toString(); + } + + public Player getOwner() { + return owner; + } + + public void setOwner(Player owner) { + this.owner = owner; + } + + public ItemStack getItem() { + return item; + } + + public void setItem(ItemStack item) { + this.item = item; + } + + public int getStartPrice() { + return startPrice; + } + + public void setStartPrice(int startPrice) { + this.startPrice = startPrice; + } + + public int getBidIncrement() { + return bidIncrement; + } + + public void setBidIncrement(int bidIncrement) { + this.bidIncrement = bidIncrement; + } + + public int getBuyNowPrice() { + return buyNowPrice; + } + + public void setBuyNowPrice(int buyNowPrice) { + this.buyNowPrice = buyNowPrice; + } + + public int getCurrentPrice() { + return currentPrice; + } + + public void setCurrentPrice(int currentPrice) { + this.currentPrice = currentPrice; + } + + public int getTime() { + return time; + } + + public void setTime(int time) { + this.time = time; + } + + public String getKey() { + return key; + } + + public void updateTime(int removeAmount) { + if (time - removeAmount <= 0) { + time = 0; + } else { + time = time - removeAmount; + } + } + + public ItemStack auctionStack() { + ItemStack stack = item.clone(); + stack.setAmount((stack.getAmount() > 1) ? stack.getAmount() : 1); + ItemMeta meta = stack.getItemMeta(); + List lore = null; + if (meta.hasLore()) { + lore = meta.getLore(); + } else { + lore = new ArrayList<>(); + } + lore.add(translateAlternateColorCodes('&', "&7-------------------------")); + if (owner == null) + lore.add(translateAlternateColorCodes('&', "&eSeller&f: &bSample User")); + else + lore.add(translateAlternateColorCodes('&', "&eSeller&f: &b" + owner.getName())); + lore.add(translateAlternateColorCodes('&', "")); + lore.add(translateAlternateColorCodes('&', "&eBuy Now: &a$" + NumberFormat.getInstance().format(buyNowPrice))); + lore.add(translateAlternateColorCodes('&', "&eCurrent Price: &a$" + NumberFormat.getInstance().format(currentPrice))); + lore.add(translateAlternateColorCodes('&', "&eBid Increment: &a$" + NumberFormat.getInstance().format(bidIncrement))); + lore.add(translateAlternateColorCodes('&', "")); + lore.add(translateAlternateColorCodes('&', "&eTime Left: &b" + AuctionAPI.getInstance().timeLeft(getTime()))); + lore.add(translateAlternateColorCodes('&', "&7-------------------------")); + lore.add(translateAlternateColorCodes('&', "&eLeft-Click&f: &bBid")); + lore.add(translateAlternateColorCodes('&', "&eRight-Click&f: &bBuy Now")); + lore.add(translateAlternateColorCodes('&', "&7-------------------------")); + meta.setLore(lore); + stack.setItemMeta(meta); + stack = NBTEditor.setItemTag(stack, getKey(), "AuctionItemKey"); + return stack; + } } diff --git a/src/main/java/com/shadebyte/auctionhouse/auction/AuctionPlayer.java b/src/main/java/com/shadebyte/auctionhouse/auction/AuctionPlayer.java index 94e32e3..f9d998d 100644 --- a/src/main/java/com/shadebyte/auctionhouse/auction/AuctionPlayer.java +++ b/src/main/java/com/shadebyte/auctionhouse/auction/AuctionPlayer.java @@ -1,5 +1,12 @@ package com.shadebyte.auctionhouse.auction; +import com.shadebyte.auctionhouse.Core; +import com.shadebyte.auctionhouse.api.enums.Permissions; +import org.bukkit.entity.Player; + +import java.util.ArrayList; +import java.util.List; + /** * The current file has been created by Kiran Hart * Date Created: 7/6/2018 @@ -7,4 +14,41 @@ package com.shadebyte.auctionhouse.auction; * Usage of any code found within this class is prohibited unless given explicit permission otherwise. */ public class AuctionPlayer { + + private Player player; + + public AuctionPlayer(Player player) { + this.player = player; + } + + public int getTotalActiveAuctions() { + int total = 0; + for (AuctionItem item : Core.getInstance().auctionItems) { + if (item.getOwner().getUniqueId().equals(player.getUniqueId())) { + total++; + } + } + return total; + } + + public List getAuctionItems() { + List list = new ArrayList<>(); + for (AuctionItem item : Core.getInstance().auctionItems) { + if (item.getOwner().getUniqueId().equals(player.getUniqueId())) { + list.add(item); + } + } + return list; + } + + public boolean hasMaximumAuctionsActive() { + if (!player.hasPermission(Permissions.MAX_AUCTIONS.getNode() + "." + getTotalActiveAuctions())) { + return true; + } + return false; + } + + public Player getPlayer() { + return player; + } } diff --git a/src/main/java/com/shadebyte/auctionhouse/cmds/CommandManager.java b/src/main/java/com/shadebyte/auctionhouse/cmds/CommandManager.java index 113f167..0518e8c 100644 --- a/src/main/java/com/shadebyte/auctionhouse/cmds/CommandManager.java +++ b/src/main/java/com/shadebyte/auctionhouse/cmds/CommandManager.java @@ -1,11 +1,15 @@ package com.shadebyte.auctionhouse.cmds; import com.shadebyte.auctionhouse.Core; +import com.shadebyte.auctionhouse.api.enums.Lang; import com.shadebyte.auctionhouse.api.enums.Permissions; +import com.shadebyte.auctionhouse.cmds.subcmds.SellCommand; +import com.shadebyte.auctionhouse.inventory.inventories.AuctionGUI; import com.shadebyte.auctionhouse.util.Debugger; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; import java.util.ArrayList; import java.util.Arrays; @@ -35,20 +39,24 @@ public class CommandManager implements CommandExecutor { public void initialize() { Core.getInstance().getCommand(main).setExecutor(this); + this.commands.add(new SellCommand()); } @Override public boolean onCommand(CommandSender sender, Command command, String label, String[] args) { if (!sender.hasPermission(Permissions.BASE.getNode())) { - //sender.sendMessage(Core.getInstance().getSettings().getPrefix() + Core.getInstance().getLocale().getMessage(Lang.NO_PERMISSION.getNode())); + sender.sendMessage(Core.getInstance().getSettings().getPrefix() + Core.getInstance().getLocale().getMessage(Lang.NO_PERMISSION.getNode())); return true; } //Main command text if (command.getName().equalsIgnoreCase(main)) { if (args.length == 0) { - //OPEN + if (sender instanceof Player) { + Player p = (Player) sender; + p.openInventory(AuctionGUI.getInstance(p).getInventory()); + } return true; } @@ -56,7 +64,7 @@ public class CommandManager implements CommandExecutor { SubCommand target = this.getSubcommand(args[0]); if (target == null) { - //sender.sendMessage(Core.getInstance().getSettings().getPrefix() + Core.getInstance().getLocale().getMessage(Lang.INVALID_SUBCOMMAND.getNode())); + sender.sendMessage(Core.getInstance().getSettings().getPrefix() + Core.getInstance().getLocale().getMessage(Lang.INVALID_SUBCOMMAND.getNode())); return true; } diff --git a/src/main/java/com/shadebyte/auctionhouse/cmds/subcmds/SellCommand.java b/src/main/java/com/shadebyte/auctionhouse/cmds/subcmds/SellCommand.java index 66cea42..e8abb29 100644 --- a/src/main/java/com/shadebyte/auctionhouse/cmds/subcmds/SellCommand.java +++ b/src/main/java/com/shadebyte/auctionhouse/cmds/subcmds/SellCommand.java @@ -1,10 +1,62 @@ package com.shadebyte.auctionhouse.cmds.subcmds; +import com.shadebyte.auctionhouse.Core; +import com.shadebyte.auctionhouse.api.AuctionAPI; +import com.shadebyte.auctionhouse.api.enums.Lang; +import com.shadebyte.auctionhouse.api.enums.Permissions; +import com.shadebyte.auctionhouse.auction.AuctionItem; +import com.shadebyte.auctionhouse.cmds.SubCommand; +import org.bukkit.command.CommandSender; +import org.bukkit.entity.Player; + /** * The current file has been created by Kiran Hart * Date Created: 7/6/2018 * Time Created: 11:50 AM * Usage of any code found within this class is prohibited unless given explicit permission otherwise. */ -public class SellCommand { +public class SellCommand extends SubCommand { + + @Override + public void onCommand(CommandSender sender, String[] args) { + + if (!sender.hasPermission(Permissions.BASE.getNode())) { + sender.sendMessage(Core.getInstance().getSettings().getPrefix() + Core.getInstance().getLocale().getMessage(Lang.NO_PERMISSION.getNode())); + return; + } + + if (!(sender instanceof Player)) { + sender.sendMessage(Core.getInstance().getSettings().getPrefix() + Core.getInstance().getLocale().getMessage(Lang.PLAYERS_ONLY.getNode())); + return; + } + + Player p = (Player) sender; + + if (args.length == 1) { + p.sendMessage(Core.getInstance().getSettings().getPrefix() + Core.getInstance().getLocale().getMessage(Lang.CMD_SELL.getNode())); + return; + } + + if (args.length == 4) { + int buyNow = Integer.parseInt(args[1]); + int startPrice = Integer.parseInt(args[2]); + int increment = Integer.parseInt(args[3]); + Core.getInstance().auctionItems.add(0, new AuctionItem(p, AuctionAPI.getItemInHand(p), 3600, startPrice, increment, buyNow)); + } + } + + @Override + public String name() { + return Core.getInstance().getCommandManager().sell; + } + + @Override + public String info() { + return null; + } + + @Override + public String[] aliases() { + return new String[0]; + } } diff --git a/src/main/java/com/shadebyte/auctionhouse/events/AGUIListener.java b/src/main/java/com/shadebyte/auctionhouse/events/AGUIListener.java new file mode 100644 index 0000000..57cb69a --- /dev/null +++ b/src/main/java/com/shadebyte/auctionhouse/events/AGUIListener.java @@ -0,0 +1,32 @@ +package com.shadebyte.auctionhouse.events; + +import com.shadebyte.auctionhouse.inventory.AGUI; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.inventory.InventoryCloseEvent; + +/** + * The current file has been created by Kiran Hart + * Date Created: 7/6/2018 + * Time Created: 4:03 PM + * Usage of any code found within this class is prohibited unless given explicit permission otherwise. + */ +public class AGUIListener implements Listener { + + @EventHandler + public void onClick(InventoryClickEvent e) { + if (e.getInventory().getHolder() instanceof AGUI) { + AGUI gui = (AGUI) e.getInventory().getHolder(); + gui.click(e, e.getCurrentItem(), e.getRawSlot()); + } + } + + @EventHandler + public void onClose(InventoryCloseEvent e) { + if (e.getInventory().getHolder() instanceof AGUI) { + AGUI gui = (AGUI) e.getInventory().getHolder(); + gui.close(e); + } + } +} diff --git a/src/main/java/com/shadebyte/auctionhouse/inventory/AGUI.java b/src/main/java/com/shadebyte/auctionhouse/inventory/AGUI.java index 07112cc..7508aac 100644 --- a/src/main/java/com/shadebyte/auctionhouse/inventory/AGUI.java +++ b/src/main/java/com/shadebyte/auctionhouse/inventory/AGUI.java @@ -1,11 +1,19 @@ package com.shadebyte.auctionhouse.inventory; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.inventory.InventoryCloseEvent; +import org.bukkit.inventory.InventoryHolder; +import org.bukkit.inventory.ItemStack; + /** * The current file has been created by Kiran Hart * Date Created: 7/6/2018 * Time Created: 11:54 AM * Usage of any code found within this class is prohibited unless given explicit permission otherwise. */ -public interface AGUI { +public interface AGUI extends InventoryHolder { -} + void click(InventoryClickEvent e, ItemStack clicked, int slot); + + void close(InventoryCloseEvent e); +} \ No newline at end of file diff --git a/src/main/java/com/shadebyte/auctionhouse/inventory/inventories/AuctionGUI.java b/src/main/java/com/shadebyte/auctionhouse/inventory/inventories/AuctionGUI.java index 431aeaa..99fa706 100644 --- a/src/main/java/com/shadebyte/auctionhouse/inventory/inventories/AuctionGUI.java +++ b/src/main/java/com/shadebyte/auctionhouse/inventory/inventories/AuctionGUI.java @@ -1,10 +1,133 @@ package com.shadebyte.auctionhouse.inventory.inventories; +import com.google.common.collect.Lists; +import com.shadebyte.auctionhouse.Core; +import com.shadebyte.auctionhouse.api.AuctionAPI; +import com.shadebyte.auctionhouse.auction.AuctionItem; +import com.shadebyte.auctionhouse.inventory.AGUI; +import com.shadebyte.auctionhouse.util.NBTEditor; +import org.bukkit.Bukkit; +import org.bukkit.ChatColor; +import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.event.inventory.ClickType; +import org.bukkit.event.inventory.InventoryClickEvent; +import org.bukkit.event.inventory.InventoryCloseEvent; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.ItemStack; + +import java.util.List; + /** * The current file has been created by Kiran Hart * Date Created: 7/6/2018 * Time Created: 11:56 AM * Usage of any code found within this class is prohibited unless given explicit permission otherwise. */ -public class AuctionGUI { +public class AuctionGUI implements AGUI { + + private static AuctionGUI instance; + private Player p; + + private AuctionGUI(Player p) { + this.p = p; + } + + public static AuctionGUI getInstance(Player p) { + if (instance == null) { + instance = new AuctionGUI(p); + } + return instance; + } + + private int page = 1; + + + @Override + public void click(InventoryClickEvent e, ItemStack clicked, int slot) { + e.setCancelled(true); + Player p = (Player) e.getWhoClicked(); + + + if (page >= 1 && slot == 48) p.openInventory(this.setPage(this.getPage() - 1).getInventory()); + if (page >= 1 && slot == 50) p.openInventory(this.setPage(this.getPage() + 1).getInventory()); + + + if (slot == 49) { + p.closeInventory(); + p.openInventory(AuctionGUI.getInstance(p).getInventory()); + } + + if (slot >= 0 & slot <= 44) { + if (clicked == null || clicked.getType() == Material.AIR) { + return; + } + + AuctionItem item = null; + + if (e.getClick() == ClickType.LEFT) { + String key = (String) NBTEditor.getItemTag(clicked, "AuctionItemKey"); + for (AuctionItem auctionItem : Core.getInstance().auctionItems) { + if (auctionItem.getKey().equalsIgnoreCase(key)) item = auctionItem; + } + + item.setCurrentPrice(item.getCurrentPrice() + item.getBidIncrement()); + p.closeInventory(); + p.openInventory(AuctionGUI.getInstance(p).getInventory()); + } + + if (e.getClick() == ClickType.RIGHT) { + String key = (String) NBTEditor.getItemTag(clicked, "AuctionItemKey"); + for (AuctionItem auctionItem : Core.getInstance().auctionItems) { + if (auctionItem.getKey().equalsIgnoreCase(key)) item = auctionItem; + } + + p.getInventory().addItem(item.getItem()); + item.setTime(0); + Core.getInstance().auctionItems.remove(item); + + p.closeInventory(); + p.openInventory(AuctionGUI.getInstance(p).getInventory()); + } + } + } + + @Override + public void close(InventoryCloseEvent e) { + + } + + @Override + public Inventory getInventory() { + Inventory inventory = Bukkit.createInventory(this, 54, ChatColor.translateAlternateColorCodes('&', Core.getInstance().getConfig().getString("gui.auction.title"))); + + //Bottom Row + inventory.setItem(45, AuctionAPI.getInstance().createConfigItem("gui.auction.items.yourauctions")); + inventory.setItem(46, AuctionAPI.getInstance().createConfigItem("gui.auction.items.collectionbin")); + inventory.setItem(48, AuctionAPI.getInstance().createConfigItem("gui.auction.items.previouspage")); + inventory.setItem(49, AuctionAPI.getInstance().createConfigItem("gui.auction.items.refresh")); + inventory.setItem(50, AuctionAPI.getInstance().createConfigItem("gui.auction.items.nextpage")); + inventory.setItem(52, AuctionAPI.getInstance().createConfigItem("gui.auction.items.howtosell")); + inventory.setItem(53, AuctionAPI.getInstance().createConfigItem("gui.auction.items.guide")); + + //Pagination + List> chunks = Lists.partition(Core.getInstance().auctionItems, 45); + + if (chunks.size() != 0) + chunks.get(getPage() - 1).forEach(item -> inventory.setItem(inventory.firstEmpty(), item.auctionStack())); + + return inventory; + } + + public AuctionGUI setPage(int page) { + if (page <= 0) + this.page = 1; + else + this.page = page; + return this; + } + + public int getPage() { + return page; + } } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index e69de29..6dc7bd3 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -0,0 +1,67 @@ +gui: + auction: + title: "&7Auction House" + items: + guide: + item: "BOOK:0" + name: "&e&lGuide" + lore: + - "&7This is the auction house, here you can" + - "&7list items for sale and purchase items" + - "&7that others have listed for sale." + - "" + - "&7The auction is also a great way to make" + - "&7money by selling farmable items other" + - "&7players may be interested in buying." + - "" + - "&7All sell listings last for a max of &e60 minutes" + - "&7unsold items are sent to your collection bin." + - "" + - "&7Each item is listed as a auction, so" + - "&7players can out bid each other, or simply" + - "&7purchase it right away with a set price." + - "" + - "&7For more help, use &e/ah help!" + howtosell: + item: "GOLD_INGOT:0" + name: "&e&lHow to Sell" + lore: + - "&7To list an item on the auction house, just hold" + - "&7the item in your hand and type the following command." + - "&e/ah sell " + yourauctions: + item: "DIAMOND:0" + name: "&e&lYour Auctions" + lore: + - "&7Click here to view all of the items you" + - "&7are currently selling on the auction." + - "" + - "&e&l{active_player_auctions} Item(s)" + collectionbin: + item: "ENDER_CHEST:0" + name: "&e&lCollection Bin" + lore: + - "&7Click here to view and collect all of the" + - "&7items you have cancelled or have expired." + - "" + - "&e&l{expired_player_auctions} Item(s)" + previouspage: + item: "PAPER:0" + name: "&E&L<- Previous Page" + lore: + - "&7View the previous page of auctions." + nextpage: + item: "PAPER:0" + name: "&e&lNext Page ->" + lore: + - "&7View the next page of auctions." + refresh: + item: "CHEST:0" + name: "&6&lRefresh Auctions" + lore: + - "&7Click to refresh the currently" + - "&7available auction listings." + + + + diff --git a/src/main/resources/en_US.lang b/src/main/resources/en_US.lang index e69de29..e25fe85 100644 --- a/src/main/resources/en_US.lang +++ b/src/main/resources/en_US.lang @@ -0,0 +1,7 @@ +prefix = "&8[eAuctionHouse&8]" + +cmd.invalid = "&cThat isn't a valid subcommand!" +cmd.sell = "&e/ah sell " + +nopermission = "&cYou do not have permission to do that!" +playersonly = "&cOnly player's may use the command!" \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 2555d56..ee2d83d 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -4,3 +4,8 @@ main: com.shadebyte.auctionhouse.Core authors: [TheCrystalStar] description: A premium auction house plugin website: https://www.shadebyte.com + +commands: + auctionhouse: + description: Main command for the plugin + aliases: [ah, auchouse] \ No newline at end of file