Skip to content
This repository has been archived by the owner on Mar 8, 2022. It is now read-only.

Commit

Permalink
improve market close #6
Browse files Browse the repository at this point in the history
  • Loading branch information
cyilin committed Jun 3, 2016
1 parent 7f3b62d commit c4f496b
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 29 deletions.
10 changes: 5 additions & 5 deletions src/main/java/cat/nyaa/HamsterEcoHelper/Events.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
20 changes: 18 additions & 2 deletions src/main/java/cat/nyaa/HamsterEcoHelper/market/MarketCommands.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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));
}
}
}
}
50 changes: 42 additions & 8 deletions src/main/java/cat/nyaa/HamsterEcoHelper/market/MarketManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}

Expand All @@ -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");
Expand Down Expand Up @@ -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<Integer, Integer> list = new HashMap<>();
Inventory inventory = Bukkit.createInventory(player, 54, ChatColor.DARK_GREEN + I18n.get("user.market.title"));
int pageCount;
Expand Down Expand Up @@ -174,14 +178,23 @@ 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);
viewItem.put(player, list);
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);
Expand All @@ -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));
}
}
}
}
20 changes: 8 additions & 12 deletions src/main/java/cat/nyaa/HamsterEcoHelper/utils/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,39 +171,35 @@ public boolean addItemToMailbox(Player player, ItemStack item) {
public List<MarketItem> getMarketItems(int offset, int limit, UUID seller) {
Query<MarketItem> 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;
}
Expand All @@ -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;
}
Expand Down
7 changes: 5 additions & 2 deletions src/main/resources/lang/en_US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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!"
Expand Down Expand Up @@ -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."
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ permissions:
heh.addauction: true
heh.addrequisition: true
heh.debug: true
heh.givemarketitem: true

0 comments on commit c4f496b

Please sign in to comment.