diff --git a/src/main/java/cat/nyaa/HamsterEcoHelper/Events.java b/src/main/java/cat/nyaa/HamsterEcoHelper/Events.java index f16f4dd..22f9096 100644 --- a/src/main/java/cat/nyaa/HamsterEcoHelper/Events.java +++ b/src/main/java/cat/nyaa/HamsterEcoHelper/Events.java @@ -52,20 +52,20 @@ public void onInventoryClickEvent(InventoryClickEvent event) { MarketManager.buy(player, itemId, 1); } } - MarketManager.view(player, MarketManager.viewPage.get(player), seller); + MarketManager.openGUI(player, MarketManager.viewPage.get(player), seller); return; } - MarketManager.viewItem.remove(player); + MarketManager.closeGUI(player); return; } if (event.getRawSlot() == 45 && event.getCurrentItem().getType() != Material.AIR) { - MarketManager.view(player, MarketManager.viewPage.get(player) - 1, seller); + MarketManager.openGUI(player, MarketManager.viewPage.get(player) - 1, seller); } else if (event.getRawSlot() == 47 && event.getCurrentItem().getType() != Material.AIR) { - MarketManager.view(player, 1, player.getUniqueId()); + MarketManager.openGUI(player, 1, player.getUniqueId()); } else if (event.getRawSlot() == 48 && event.getCurrentItem().getType() != Material.AIR) { MarketManager.openMailbox(player); } else if (event.getRawSlot() == 53 && event.getCurrentItem().getType() != Material.AIR) { - MarketManager.view(player, MarketManager.viewPage.get(player) + 1, seller); + MarketManager.openGUI(player, MarketManager.viewPage.get(player) + 1, seller); } event.setCancelled(true); } diff --git a/src/main/java/cat/nyaa/HamsterEcoHelper/market/MarketCommands.java b/src/main/java/cat/nyaa/HamsterEcoHelper/market/MarketCommands.java index 5286c54..deb6cca 100644 --- a/src/main/java/cat/nyaa/HamsterEcoHelper/market/MarketCommands.java +++ b/src/main/java/cat/nyaa/HamsterEcoHelper/market/MarketCommands.java @@ -1,6 +1,8 @@ package cat.nyaa.HamsterEcoHelper.market; import cat.nyaa.HamsterEcoHelper.HamsterEcoHelper; +import cat.nyaa.HamsterEcoHelper.I18n; +import cat.nyaa.HamsterEcoHelper.utils.Database; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.OfflinePlayer; @@ -52,10 +54,24 @@ public static void view(CommandSender sender, Arguments args, HamsterEcoHelper p if (args.length() == 2) { OfflinePlayer seller = Bukkit.getOfflinePlayer(args.next()); if (seller != null) { - MarketManager.view(player, 1, seller.getUniqueId()); + MarketManager.openGUI(player, 1, seller.getUniqueId()); } } else { - MarketManager.view(player, 1, null); + MarketManager.openGUI(player, 1, null); + } + } + + @SubCommand(value = "givemarketitem", permission = "heh.givemarketitem") + public static void give(CommandSender sender, Arguments args, HamsterEcoHelper plugin) { + Player player = asPlayer(sender); + Database.MarketItem item = MarketManager.getItem(args.nextInt()); + if (item != null) { + int slot = player.getInventory().firstEmpty(); + if (slot >= 0 && player.getInventory().getItem(slot) == null) { + msg(player, "user.market.offered", item.getPlayerName()); + msg(player, "user.market.unit_price", item.getUnitPrice()); + player.getInventory().setItem(slot, item.getItemStack(1)); + } } } } diff --git a/src/main/java/cat/nyaa/HamsterEcoHelper/market/MarketManager.java b/src/main/java/cat/nyaa/HamsterEcoHelper/market/MarketManager.java index 5698423..1d02fc9 100644 --- a/src/main/java/cat/nyaa/HamsterEcoHelper/market/MarketManager.java +++ b/src/main/java/cat/nyaa/HamsterEcoHelper/market/MarketManager.java @@ -16,10 +16,7 @@ import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.plugin.RegisteredServiceProvider; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.UUID; +import java.util.*; import static cat.nyaa.HamsterEcoHelper.CommandHandler.msg; import static org.bukkit.Bukkit.getServer; @@ -48,11 +45,13 @@ public static boolean offer(Player player, ItemStack item, double unit_price) { if (getPlayerSlot(player) <= db.getMarketPlayerItemCount(player)) { return false; } - db.marketOffer(player, item, unit_price); + int id=db.marketOffer(player, item, unit_price); + plugin.logger.info(I18n.get("internal.info.market_offer",id,getItemName(item),item.getAmount(),unit_price,player.getName())); if (plugin.config.marketBroadcast && (System.currentTimeMillis() - lastBroadcast) > (plugin.config.marketBroadcastCooldown * 1000)) { lastBroadcast = System.currentTimeMillis(); - Bukkit.broadcastMessage(I18n.get("user.market.broadcast")); + new Message("").append(item,I18n.get("user.market.broadcast")).broadcast(); } + updateAllGUI(); return true; } @@ -66,17 +65,22 @@ public static boolean buy(Player player, int itemId, int amount) { playSound(player, Sound.BLOCK_FENCE_GATE_OPEN); return false; } + plugin.logger.info(I18n.get("internal.info.market_bought",itemId,getItemName(item.getItemStack()),amount,price,player.getName(),item.getPlayerName())); if (!player.getUniqueId().equals(item.getPlayerId())) { if(item.getPlayer().isOnline()){ new Message("") .append(item.getItemStack(amount),I18n.get("user.market.someone_bought",player.getName(),price)) .send((Player) item.getPlayer()); } + new Message("") + .append(item.getItemStack(amount),I18n.get("user.market.buy_success",item.getPlayerName(),price)) + .send(player); plugin.eco.withdraw(player, price); plugin.eco.deposit(item.getPlayer(), price); } db.marketBuy(player, itemId, amount); playSound(player, Sound.ENTITY_EXPERIENCE_ORB_TOUCH); + updateAllGUI(); return true; } else { msg(player, "user.warn.no_enough_money"); @@ -106,7 +110,7 @@ public static Database.MarketItem getItem(int itemId) { return db.getMarketItem(itemId); } - public static void view(Player player, int page, UUID seller) { + public static void openGUI(Player player, int page, UUID seller) { HashMap list = new HashMap<>(); Inventory inventory = Bukkit.createInventory(player, 54, ChatColor.DARK_GREEN + I18n.get("user.market.title")); int pageCount; @@ -174,7 +178,7 @@ public static void view(Player player, int page, UUID seller) { meta.setDisplayName(ChatColor.AQUA + I18n.get("user.market.my_items") + (String.format(" (%s/%s)", db.getMarketPlayerItemCount(player), getPlayerSlot(player)))); lore = new ArrayList<>(); - lore.add(ChatColor.GREEN + I18n.get("user.info.balance", ChatColor.WHITE + "" + plugin.eco.balance(player))); + lore.add(ChatColor.GREEN + I18n.get("user.info.balance", plugin.eco.balance(player))); meta.setLore(lore); myItem.setItemMeta(meta); inventory.setItem(47, myItem); @@ -182,6 +186,15 @@ public static void view(Player player, int page, UUID seller) { player.openInventory(inventory); } + public static void closeGUI(Player player){ + if(player.isOnline() && player.getOpenInventory().getTitle().contains(I18n.get("user.market.title"))) { + player.getOpenInventory().close(); + } + viewPage.remove(player); + viewItem.remove(player); + viewSeller.remove(player); + } + public static void openMailbox(Player player) { Inventory inventory = Bukkit.createInventory(player, 54, I18n.get("user.market.mailbox")); ItemStack[] mailbox = getMailbox(player); @@ -206,4 +219,25 @@ public static void playSound(Player player, Sound sound) { } return; } + + private static String getItemName(ItemStack item){ + String itemName=""; + if(item.hasItemMeta()&&item.getItemMeta().hasDisplayName()){ + itemName=item.getItemMeta().getDisplayName(); + } + if(itemName.length()==0){ + itemName=item.getType().name()+":"+item.getDurability(); + }else { + itemName+="("+item.getType().name()+":"+item.getDurability()+")"; + } + return itemName; + } + + public static void updateAllGUI() { + for(Player player:viewPage.keySet()){ + if(player.isOnline() && player.getOpenInventory()!=null && player.getOpenInventory().getTitle().contains(I18n.get("user.market.title"))){ + openGUI(player,viewPage.get(player),viewSeller.get(player)); + } + } + } } diff --git a/src/main/java/cat/nyaa/HamsterEcoHelper/utils/Database.java b/src/main/java/cat/nyaa/HamsterEcoHelper/utils/Database.java index fd7945f..cd8223b 100644 --- a/src/main/java/cat/nyaa/HamsterEcoHelper/utils/Database.java +++ b/src/main/java/cat/nyaa/HamsterEcoHelper/utils/Database.java @@ -171,39 +171,35 @@ public boolean addItemToMailbox(Player player, ItemStack item) { public List getMarketItems(int offset, int limit, UUID seller) { Query list; if (seller == null) { - list = db.find(MarketItem.class).order().desc("id").setFirstRow(offset).setMaxRows(limit); + list = db.find(MarketItem.class).where().ge("amount", 1).order().desc("id").setFirstRow(offset).setMaxRows(limit); return list.findList(); } else { - list = db.find(MarketItem.class).where().eq("playerId", seller).order().desc("id").setFirstRow(offset).setMaxRows(limit); + list = db.find(MarketItem.class).where().ge("amount", 1).eq("playerId", seller).order().desc("id").setFirstRow(offset).setMaxRows(limit); return list.findList(); } } - public void marketOffer(Player player, ItemStack itemStack, double unit_price) { + public int marketOffer(Player player, ItemStack itemStack, double unit_price) { MarketItem item = new MarketItem(); item.setItemStack(itemStack); item.setAmount(itemStack.getAmount()); item.setPlayerId(player.getUniqueId()); item.setUnitPrice(unit_price); db.save(item); - return; + return item.getId(); } public void marketBuy(Player player, int itemId, int amount) { MarketItem mItem = db.find(MarketItem.class, itemId); if (mItem != null) { - if (mItem.getAmount() == amount) { - db.delete(MarketItem.class, itemId); - } else { - mItem.setAmount(mItem.getAmount() - amount); - db.update(mItem); - } + mItem.setAmount(mItem.getAmount() - amount); + db.update(mItem); } return; } public int getMarketPlayerItemCount(OfflinePlayer player) { - int count = db.find(MarketItem.class).where().eq("playerId", player.getUniqueId()).findRowCount(); + int count = db.find(MarketItem.class).where().ge("amount", 1).eq("playerId", player.getUniqueId()).findRowCount(); if (count > 0) { return count; } @@ -213,7 +209,7 @@ public int getMarketPlayerItemCount(OfflinePlayer player) { public int getMarketPageCount() { int count = db.find(MarketItem.class).findRowCount(); if (count > 0) { - return db.find(MarketItem.class).findPagingList(MarketManager.pageSize).getTotalPageCount(); + return db.find(MarketItem.class).where().ge("amount", 1).findPagingList(MarketManager.pageSize).getTotalPageCount(); } return 0; } diff --git a/src/main/resources/lang/en_US.yml b/src/main/resources/lang/en_US.yml index f8d6617..d91a60b 100644 --- a/src/main/resources/lang/en_US.yml +++ b/src/main/resources/lang/en_US.yml @@ -15,6 +15,8 @@ internal: req_start: "Requisition started: {item: \"%s\", amount: %d, price: %d, timeout: %d, id: \"%s\"}" req_sell: "Player sold item: {id: \"%s\", amount: %d, remain: %d, player: \"%s\"}" req_finish: "Requisition finished: {id: \"%s\", total_sold: %d, reason: \"%s\"}" + market_offer: "offer: {id: %d, item: \"%s\", amount: %d, unit_price: %.2f, player: \"%s\"}" + market_bought: "bought: {id: %d, item: \"%s\", amount: %d, price: %.2f, player: \"%s\", from: \"%s\"}" error: missing_sqlite_driver: "SQLite Driver not found. Plugin disabled." enable_fail: "Plugin failed to enable" @@ -35,7 +37,7 @@ user: not_item_hand: "No item in hand" no_current_auction: "No ongoing auction" no_current_requisition: "No ongoing requisition" - balance: "Balance: %s" + balance: "Balance: %.2f" back: "Back" next_page: "Next page" command_complete: "Command Executed!" @@ -90,8 +92,9 @@ user: unit_price: "Unit Price: %s" offered: "offered by: %s" my_items: "My items" - broadcast: "New market offer! use '/heh view' view the items." + broadcast: "New market offer: {itemName} *{amount} use '/heh view' view the items." someone_bought: "%s bought {itemName} for $%.2f from you." + buy_success: "you bought {itemName} from %s for $%.2f" retrieve: need_confirm: "The items will drop from your current position. Please ensure the safety. Use '/heh retrieve confirm'" no_item: "You have no item to retrieve." diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 60898d4..9d75a55 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -42,3 +42,4 @@ permissions: heh.addauction: true heh.addrequisition: true heh.debug: true + heh.givemarketitem: true \ No newline at end of file