mirror of
https://e.coding.net/circlecloud/QuickShop.git
synced 2024-11-22 01:58:54 +00:00
refactor: 清理部分无用方法
This commit is contained in:
parent
c2ac919c40
commit
918c329b2c
@ -214,282 +214,261 @@ public class ShopManager {
|
|||||||
return inWorld.get(shopChunk);
|
return inWorld.get(shopChunk);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void handleChat(final Player p, final String msg) {
|
public void handleChat(final Player p, final String msgs) {
|
||||||
final String message = ChatColor.stripColor(msg);
|
final String message = ChatColor.stripColor(msgs);
|
||||||
// Use from the main thread, because Bukkit hates life
|
final HashMap<String, Info> actions = getActions();
|
||||||
Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
|
// They wanted to do something.
|
||||||
@SuppressWarnings("deprecation")
|
final Info info = actions.remove(p.getName());
|
||||||
@Override
|
if (info == null) {
|
||||||
public void run() {
|
return; // multithreaded means this can happen
|
||||||
final HashMap<String, Info> actions = getActions();
|
}
|
||||||
// They wanted to do something.
|
/* Creation handling */
|
||||||
final Info info = actions.remove(p.getName());
|
if (info.getAction() == ShopAction.CREATE) {
|
||||||
if (info == null) {
|
try {
|
||||||
return; // multithreaded means this can happen
|
// Checking the shop can be created
|
||||||
}
|
if (plugin.getShopManager().getShop(info.getLocation()) != null) {
|
||||||
if (info.getLocation().getWorld() != p.getLocation().getWorld()) {
|
p.sendMessage(MsgUtil.p("shop-already-owned"));
|
||||||
p.sendMessage(MsgUtil.p("shop-creation-cancelled"));
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (info.getLocation().distanceSquared(p.getLocation()) > 25) {
|
if (Util.getSecondHalf(info.getLocation().getBlock()) != null && !p.hasPermission("quickshop.create.double")) {
|
||||||
p.sendMessage(MsgUtil.p("shop-creation-cancelled"));
|
p.sendMessage(MsgUtil.p("no-double-chests"));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
/* Creation handling */
|
if (Util.canBeShop(info.getLocation().getBlock()) == false) {
|
||||||
if (info.getAction() == ShopAction.CREATE) {
|
p.sendMessage(MsgUtil.p("chest-was-removed"));
|
||||||
try {
|
return;
|
||||||
// Checking the shop can be created
|
}
|
||||||
if (plugin.getShopManager().getShop(info.getLocation()) != null) {
|
// Price per item
|
||||||
p.sendMessage(MsgUtil.p("shop-already-owned"));
|
double price;
|
||||||
return;
|
if (plugin.getConfig().getBoolean("whole-number-prices-only")) {
|
||||||
}
|
price = Integer.parseInt(message);
|
||||||
if (Util.getSecondHalf(info.getLocation().getBlock()) != null && !p.hasPermission("quickshop.create.double")) {
|
} else {
|
||||||
p.sendMessage(MsgUtil.p("no-double-chests"));
|
price = Double.parseDouble(message);
|
||||||
return;
|
}
|
||||||
}
|
if (price < 0.01) {
|
||||||
if (Util.canBeShop(info.getLocation().getBlock()) == false) {
|
p.sendMessage(MsgUtil.p("price-too-cheap"));
|
||||||
p.sendMessage(MsgUtil.p("chest-was-removed"));
|
return;
|
||||||
return;
|
}
|
||||||
}
|
final double tax = plugin.getConfig().getDouble("shop.cost");
|
||||||
// Price per item
|
// Tax refers to the cost to create a shop. Not actual
|
||||||
double price;
|
// tax, that would be silly
|
||||||
if (plugin.getConfig().getBoolean("whole-number-prices-only")) {
|
if (tax != 0 && plugin.getEcon().getBalance(p.getName()) < tax) {
|
||||||
price = Integer.parseInt(message);
|
p.sendMessage(MsgUtil.p("you-cant-afford-a-new-shop", format(tax)));
|
||||||
} else {
|
return;
|
||||||
price = Double.parseDouble(message);
|
}
|
||||||
}
|
// Create the sample shop.
|
||||||
if (price < 0.01) {
|
final Shop shop = new ContainerShop(info.getLocation(), price, info.getItem(), p.getName());
|
||||||
p.sendMessage(MsgUtil.p("price-too-cheap"));
|
// This must be called after the event has been called.
|
||||||
return;
|
// Else, if the event is cancelled, they won't get their
|
||||||
}
|
// money back.
|
||||||
final double tax = plugin.getConfig().getDouble("shop.cost");
|
if (tax != 0) {
|
||||||
// Tax refers to the cost to create a shop. Not actual
|
if (!plugin.getEcon().withdraw(p.getName(), tax)) {
|
||||||
// tax, that would be silly
|
p.sendMessage(MsgUtil.p("you-cant-afford-a-new-shop", format(tax)));
|
||||||
if (tax != 0 && plugin.getEcon().getBalance(p.getName()) < tax) {
|
|
||||||
p.sendMessage(MsgUtil.p("you-cant-afford-a-new-shop", format(tax)));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// Create the sample shop.
|
|
||||||
final Shop shop = new ContainerShop(info.getLocation(), price, info.getItem(), p.getName());
|
|
||||||
// This must be called after the event has been called.
|
|
||||||
// Else, if the event is cancelled, they won't get their
|
|
||||||
// money back.
|
|
||||||
if (tax != 0) {
|
|
||||||
if (!plugin.getEcon().withdraw(p.getName(), tax)) {
|
|
||||||
p.sendMessage(MsgUtil.p("you-cant-afford-a-new-shop", format(tax)));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
plugin.getEcon().deposit(plugin.getConfig().getString("tax-account"), tax);
|
|
||||||
}
|
|
||||||
final ShopCreateEvent e = new ShopCreateEvent(shop, p);
|
|
||||||
Bukkit.getPluginManager().callEvent(e);
|
|
||||||
if (e.isCancelled()) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
shop.onLoad();
|
|
||||||
/* The shop has hereforth been successfully created */
|
|
||||||
createShop(shop);
|
|
||||||
p.sendMessage(MsgUtil.p("success-created-shop"));
|
|
||||||
final Location loc = shop.getLocation();
|
|
||||||
plugin.log(String.format("玩家: %s 创建了一个 %s 商店 在 (%s - %s, %s, %s)", p.getName(), shop.getDataName(), loc.getWorld().getName(), loc.getX(), loc.getY(), loc.getZ()));
|
|
||||||
if (!plugin.getConfig().getBoolean("shop.lock")) {
|
|
||||||
// Warn them if they haven't been warned since
|
|
||||||
// reboot
|
|
||||||
final HashSet<String> warings = plugin.getConfigManager().getWarnings();
|
|
||||||
if (!warings.contains(p.getName())) {
|
|
||||||
p.sendMessage(MsgUtil.p("shops-arent-locked"));
|
|
||||||
warings.add(p.getName());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Figures out which way we should put the sign on and
|
|
||||||
// sets its text.
|
|
||||||
if (info.getSignBlock() != null && info.getSignBlock().getType() == Material.AIR && plugin.getConfig().getBoolean("shop.auto-sign")) {
|
|
||||||
final BlockState bs = info.getSignBlock().getState();
|
|
||||||
final BlockFace bf = info.getLocation().getBlock().getFace(info.getSignBlock());
|
|
||||||
bs.setType(Material.WALL_SIGN);
|
|
||||||
final Sign sign = (Sign) bs.getData();
|
|
||||||
sign.setFacingDirection(bf);
|
|
||||||
bs.update(true);
|
|
||||||
shop.setSignText();
|
|
||||||
}
|
|
||||||
if (shop instanceof ContainerShop) {
|
|
||||||
final ContainerShop cs = (ContainerShop) shop;
|
|
||||||
if (cs.isDoubleShop()) {
|
|
||||||
final Shop nextTo = cs.getAttachedShop();
|
|
||||||
if (nextTo.getPrice() > shop.getPrice()) {
|
|
||||||
// The one next to it must always be a
|
|
||||||
// buying shop.
|
|
||||||
p.sendMessage(MsgUtil.p("buying-more-than-selling"));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
/* They didn't enter a number. */
|
|
||||||
catch (final NumberFormatException ex) {
|
|
||||||
p.sendMessage(MsgUtil.p("shop-creation-cancelled"));
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
plugin.getEcon().deposit(plugin.getConfig().getString("tax-account"), tax);
|
||||||
|
}
|
||||||
|
final ShopCreateEvent e = new ShopCreateEvent(shop, p);
|
||||||
|
Bukkit.getPluginManager().callEvent(e);
|
||||||
|
if (e.isCancelled()) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
shop.onLoad();
|
||||||
|
/* The shop has hereforth been successfully created */
|
||||||
|
createShop(shop);
|
||||||
|
p.sendMessage(MsgUtil.p("success-created-shop"));
|
||||||
|
final Location loc = shop.getLocation();
|
||||||
|
plugin.log(String.format("玩家: %s 创建了一个 %s 商店 在 (%s - %s, %s, %s)", p.getName(), shop.getDataName(), loc.getWorld().getName(), loc.getX(), loc.getY(), loc.getZ()));
|
||||||
|
if (!plugin.getConfig().getBoolean("shop.lock")) {
|
||||||
|
// Warn them if they haven't been warned since
|
||||||
|
// reboot
|
||||||
|
final HashSet<String> warings = plugin.getConfigManager().getWarnings();
|
||||||
|
if (!warings.contains(p.getName())) {
|
||||||
|
p.sendMessage(MsgUtil.p("shops-arent-locked"));
|
||||||
|
warings.add(p.getName());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
/* Purchase Handling */
|
// Figures out which way we should put the sign on and
|
||||||
else if (info.getAction() == ShopAction.BUY) {
|
// sets its text.
|
||||||
int amount = 0;
|
if (info.getSignBlock() != null && info.getSignBlock().getType() == Material.AIR && plugin.getConfig().getBoolean("shop.auto-sign")) {
|
||||||
try {
|
final BlockState bs = info.getSignBlock().getState();
|
||||||
amount = Integer.parseInt(message);
|
final BlockFace bf = info.getLocation().getBlock().getFace(info.getSignBlock());
|
||||||
} catch (final NumberFormatException e) {
|
bs.setType(Material.WALL_SIGN);
|
||||||
p.sendMessage(MsgUtil.p("shop-purchase-cancelled"));
|
final Sign sign = (Sign) bs.getData();
|
||||||
return;
|
sign.setFacingDirection(bf);
|
||||||
}
|
bs.update(true);
|
||||||
// Get the shop they interacted with
|
shop.setSignText();
|
||||||
final Shop shop = plugin.getShopManager().getShop(info.getLocation());
|
|
||||||
// It's not valid anymore
|
|
||||||
if (shop == null || Util.canBeShop(info.getLocation().getBlock()) == false) {
|
|
||||||
p.sendMessage(MsgUtil.p("chest-was-removed"));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (info.hasChanged(shop)) {
|
|
||||||
p.sendMessage(MsgUtil.p("shop-has-changed"));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (shop.isSelling()) {
|
|
||||||
final int stock = shop.getRemainingStock();
|
|
||||||
if (stock < amount) {
|
|
||||||
p.sendMessage(MsgUtil.p("shop-stock-too-low", "" + shop.getRemainingStock(), shop.getDataName()));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (amount == 0) {
|
|
||||||
// Dumb.
|
|
||||||
MsgUtil.sendPurchaseSuccess(p, shop, amount);
|
|
||||||
return;
|
|
||||||
} else if (amount < 0) {
|
|
||||||
// & Dumber
|
|
||||||
p.sendMessage(MsgUtil.p("negative-amount"));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
final int pSpace = Util.countSpace(p.getInventory(), shop.getItem());
|
|
||||||
if (amount > pSpace) {
|
|
||||||
p.sendMessage(MsgUtil.p("not-enough-space", "" + pSpace));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
final ShopPurchaseEvent e = new ShopPurchaseEvent(shop, p, amount);
|
|
||||||
Bukkit.getPluginManager().callEvent(e);
|
|
||||||
if (e.isCancelled()) {
|
|
||||||
return; // Cancelled
|
|
||||||
}
|
|
||||||
// Money handling
|
|
||||||
if (!p.getName().equals(shop.getOwner())) {
|
|
||||||
// Check their balance. Works with *most* economy
|
|
||||||
// plugins*
|
|
||||||
if (plugin.getEcon().getBalance(p.getName()) < amount * shop.getPrice()) {
|
|
||||||
p.sendMessage(MsgUtil.p("you-cant-afford-to-buy", format(amount * shop.getPrice()), format(plugin.getEcon().getBalance(p.getName()))));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// Don't tax them if they're purchasing from
|
|
||||||
// themselves.
|
|
||||||
// Do charge an amount of tax though.
|
|
||||||
final double tax = plugin.getConfigManager().getTax();
|
|
||||||
final double total = amount * shop.getPrice();
|
|
||||||
if (!plugin.getEcon().withdraw(p.getName(), total)) {
|
|
||||||
p.sendMessage(MsgUtil.p("you-cant-afford-to-buy", format(amount * shop.getPrice()), format(plugin.getEcon().getBalance(p.getName()))));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (!shop.isUnlimited() || plugin.getConfig().getBoolean("shop.pay-unlimited-shop-owners")) {
|
|
||||||
plugin.getEcon().deposit(shop.getOwner(), total * (1 - tax));
|
|
||||||
if (tax != 0) {
|
|
||||||
plugin.getEcon().deposit(plugin.getConfigManager().getTaxAccount(), total * tax);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Notify the shop owner
|
|
||||||
if (plugin.getConfigManager().isShowTax()) {
|
|
||||||
String msg = MsgUtil.p("player-bought-from-your-store-tax", p.getName(), "" + amount, shop.getDataName(), Util.format((tax * total)));
|
|
||||||
if (stock == amount) {
|
|
||||||
msg += "\n" + MsgUtil.p("shop-out-of-stock",
|
|
||||||
"" + shop.getLocation().getBlockX(),
|
|
||||||
"" + shop.getLocation().getBlockY(),
|
|
||||||
"" + shop.getLocation().getBlockZ(),
|
|
||||||
shop.getDataName());
|
|
||||||
}
|
|
||||||
MsgUtil.send(shop.getOwner(), msg);
|
|
||||||
} else {
|
|
||||||
String msg = MsgUtil.p("player-bought-from-your-store", p.getName(), "" + amount, shop.getDataName());
|
|
||||||
if (stock == amount) {
|
|
||||||
msg += "\n" + MsgUtil.p("shop-out-of-stock",
|
|
||||||
"" + shop.getLocation().getBlockX(),
|
|
||||||
"" + shop.getLocation().getBlockY(),
|
|
||||||
"" + shop.getLocation().getBlockZ(),
|
|
||||||
shop.getDataName());
|
|
||||||
}
|
|
||||||
MsgUtil.send(shop.getOwner(), msg);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Transfers the item from A to B
|
|
||||||
shop.sell(p, amount);
|
|
||||||
MsgUtil.sendPurchaseSuccess(p, shop, amount);
|
|
||||||
plugin.log(String.format("玩家: %s 从 %s 购买了 %s 件商品 花费 %s", p.getName(), shop.toString(), amount, shop.getPrice() * amount));
|
|
||||||
} else if (shop.isBuying()) {
|
|
||||||
final int space = shop.getRemainingSpace();
|
|
||||||
if (space < amount) {
|
|
||||||
p.sendMessage(MsgUtil.p("shop-has-no-space", "" + space, shop.getDataName()));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
final int count = Util.countItems(p.getInventory(), shop.getItem());
|
|
||||||
// Not enough items
|
|
||||||
if (amount > count) {
|
|
||||||
p.sendMessage(MsgUtil.p("you-dont-have-that-many-items", "" + count, shop.getDataName()));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (amount == 0) {
|
|
||||||
// Dumb.
|
|
||||||
MsgUtil.sendPurchaseSuccess(p, shop, amount);
|
|
||||||
return;
|
|
||||||
} else if (amount < 0) {
|
|
||||||
// & Dumber
|
|
||||||
p.sendMessage(MsgUtil.p("negative-amount"));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// Money handling
|
|
||||||
if (!p.getName().equals(shop.getOwner())) {
|
|
||||||
// Don't tax them if they're purchasing from
|
|
||||||
// themselves.
|
|
||||||
// Do charge an amount of tax though.
|
|
||||||
final double tax = plugin.getConfigManager().getTax();
|
|
||||||
final double total = amount * shop.getPrice();
|
|
||||||
if (!shop.isUnlimited() || plugin.getConfig().getBoolean("shop.pay-unlimited-shop-owners")) {
|
|
||||||
// Tries to check their balance nicely to see if
|
|
||||||
// they can afford it.
|
|
||||||
if (plugin.getEcon().getBalance(shop.getOwner()) < amount * shop.getPrice()) {
|
|
||||||
p.sendMessage(MsgUtil.p("the-owner-cant-afford-to-buy-from-you", format(amount * shop.getPrice()), format(plugin.getEcon().getBalance(shop.getOwner()))));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
// Check for plugins faking econ.has(amount)
|
|
||||||
if (!plugin.getEcon().withdraw(shop.getOwner(), total)) {
|
|
||||||
p.sendMessage(MsgUtil.p("the-owner-cant-afford-to-buy-from-you", format(amount * shop.getPrice()), format(plugin.getEcon().getBalance(shop.getOwner()))));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (tax != 0) {
|
|
||||||
plugin.getEcon().deposit(plugin.getConfigManager().getTaxAccount(), total * tax);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// Give them the money after we know we succeeded
|
|
||||||
plugin.getEcon().deposit(p.getName(), total * (1 - tax));
|
|
||||||
// Notify the owner of the purchase.
|
|
||||||
String msg = MsgUtil.p("player-sold-to-your-store", p.getName(), "" + amount, shop.getDataName());
|
|
||||||
if (space == amount) {
|
|
||||||
msg += "\n" + MsgUtil.p("shop-out-of-space", "" + shop.getLocation().getBlockX(), "" + shop.getLocation().getBlockY(), "" + shop.getLocation().getBlockZ());
|
|
||||||
}
|
|
||||||
MsgUtil.send(shop.getOwner(), msg);
|
|
||||||
}
|
|
||||||
shop.buy(p, amount);
|
|
||||||
MsgUtil.sendSellSuccess(p, shop, amount);
|
|
||||||
plugin.log(String.format("玩家: %s 出售了 %s 件商品 到 %s 获得 %s", p.getName(), amount, shop.toString(), shop.getPrice() * amount));
|
|
||||||
}
|
|
||||||
shop.setSignText(); // Update the signs count
|
|
||||||
}
|
}
|
||||||
/* If it was already cancelled (from destroyed) */
|
if (shop instanceof ContainerShop) {
|
||||||
else {
|
final ContainerShop cs = (ContainerShop) shop;
|
||||||
return; // It was cancelled, go away.
|
if (cs.isDoubleShop()) {
|
||||||
|
final Shop nextTo = cs.getAttachedShop();
|
||||||
|
if (nextTo.getPrice() > shop.getPrice()) {
|
||||||
|
// The one next to it must always be a
|
||||||
|
// buying shop.
|
||||||
|
p.sendMessage(MsgUtil.p("buying-more-than-selling"));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
/* They didn't enter a number. */
|
||||||
|
catch (final NumberFormatException ex) {
|
||||||
|
p.sendMessage(MsgUtil.p("shop-creation-cancelled"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* Purchase Handling */
|
||||||
|
else if (info.getAction() == ShopAction.BUY) {
|
||||||
|
int amount = 0;
|
||||||
|
try {
|
||||||
|
amount = Integer.parseInt(message);
|
||||||
|
} catch (final NumberFormatException e) {
|
||||||
|
p.sendMessage(MsgUtil.p("shop-purchase-cancelled"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// Get the shop they interacted with
|
||||||
|
final Shop shop = plugin.getShopManager().getShop(info.getLocation());
|
||||||
|
// It's not valid anymore
|
||||||
|
if (shop == null || Util.canBeShop(info.getLocation().getBlock()) == false) {
|
||||||
|
p.sendMessage(MsgUtil.p("chest-was-removed"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (info.hasChanged(shop)) {
|
||||||
|
p.sendMessage(MsgUtil.p("shop-has-changed"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (shop.isSelling()) {
|
||||||
|
final int stock = shop.getRemainingStock();
|
||||||
|
if (stock < amount) {
|
||||||
|
p.sendMessage(MsgUtil.p("shop-stock-too-low", "" + shop.getRemainingStock(), shop.getDataName()));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (amount == 0) {
|
||||||
|
// Dumb.
|
||||||
|
MsgUtil.sendPurchaseSuccess(p, shop, amount);
|
||||||
|
return;
|
||||||
|
} else if (amount < 0) {
|
||||||
|
// & Dumber
|
||||||
|
p.sendMessage(MsgUtil.p("negative-amount"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
final int pSpace = Util.countSpace(p.getInventory(), shop.getItem());
|
||||||
|
if (amount > pSpace) {
|
||||||
|
p.sendMessage(MsgUtil.p("not-enough-space", "" + pSpace));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
final ShopPurchaseEvent e = new ShopPurchaseEvent(shop, p, amount);
|
||||||
|
Bukkit.getPluginManager().callEvent(e);
|
||||||
|
if (e.isCancelled()) {
|
||||||
|
return; // Cancelled
|
||||||
|
}
|
||||||
|
// Money handling
|
||||||
|
if (!p.getName().equals(shop.getOwner())) {
|
||||||
|
// Check their balance. Works with *most* economy
|
||||||
|
// plugins*
|
||||||
|
if (plugin.getEcon().getBalance(p.getName()) < amount * shop.getPrice()) {
|
||||||
|
p.sendMessage(MsgUtil.p("you-cant-afford-to-buy", format(amount * shop.getPrice()), format(plugin.getEcon().getBalance(p.getName()))));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// Don't tax them if they're purchasing from
|
||||||
|
// themselves.
|
||||||
|
// Do charge an amount of tax though.
|
||||||
|
final double tax = plugin.getConfigManager().getTax();
|
||||||
|
final double total = amount * shop.getPrice();
|
||||||
|
if (!plugin.getEcon().withdraw(p.getName(), total)) {
|
||||||
|
p.sendMessage(MsgUtil.p("you-cant-afford-to-buy", format(amount * shop.getPrice()), format(plugin.getEcon().getBalance(p.getName()))));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!shop.isUnlimited() || plugin.getConfig().getBoolean("shop.pay-unlimited-shop-owners")) {
|
||||||
|
plugin.getEcon().deposit(shop.getOwner(), total * (1 - tax));
|
||||||
|
if (tax != 0) {
|
||||||
|
plugin.getEcon().deposit(plugin.getConfigManager().getTaxAccount(), total * tax);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Notify the shop owner
|
||||||
|
if (plugin.getConfigManager().isShowTax()) {
|
||||||
|
String msg = MsgUtil.p("player-bought-from-your-store-tax", p.getName(), "" + amount, shop.getDataName(), Util.format((tax * total)));
|
||||||
|
if (stock == amount) {
|
||||||
|
msg += "\n"
|
||||||
|
+ MsgUtil.p("shop-out-of-stock", "" + shop.getLocation().getBlockX(), "" + shop.getLocation().getBlockY(), "" + shop.getLocation().getBlockZ(), shop.getDataName());
|
||||||
|
}
|
||||||
|
MsgUtil.send(shop.getOwner(), msg);
|
||||||
|
} else {
|
||||||
|
String msg = MsgUtil.p("player-bought-from-your-store", p.getName(), "" + amount, shop.getDataName());
|
||||||
|
if (stock == amount) {
|
||||||
|
msg += "\n"
|
||||||
|
+ MsgUtil.p("shop-out-of-stock", "" + shop.getLocation().getBlockX(), "" + shop.getLocation().getBlockY(), "" + shop.getLocation().getBlockZ(), shop.getDataName());
|
||||||
|
}
|
||||||
|
MsgUtil.send(shop.getOwner(), msg);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Transfers the item from A to B
|
||||||
|
shop.sell(p, amount);
|
||||||
|
MsgUtil.sendPurchaseSuccess(p, shop, amount);
|
||||||
|
plugin.log(String.format("玩家: %s 从 %s 购买了 %s 件商品 花费 %s", p.getName(), shop.toString(), amount, shop.getPrice() * amount));
|
||||||
|
} else if (shop.isBuying()) {
|
||||||
|
final int space = shop.getRemainingSpace();
|
||||||
|
if (space < amount) {
|
||||||
|
p.sendMessage(MsgUtil.p("shop-has-no-space", "" + space, shop.getDataName()));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
final int count = Util.countItems(p.getInventory(), shop.getItem());
|
||||||
|
// Not enough items
|
||||||
|
if (amount > count) {
|
||||||
|
p.sendMessage(MsgUtil.p("you-dont-have-that-many-items", "" + count, shop.getDataName()));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (amount == 0) {
|
||||||
|
// Dumb.
|
||||||
|
MsgUtil.sendPurchaseSuccess(p, shop, amount);
|
||||||
|
return;
|
||||||
|
} else if (amount < 0) {
|
||||||
|
// & Dumber
|
||||||
|
p.sendMessage(MsgUtil.p("negative-amount"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// Money handling
|
||||||
|
if (!p.getName().equals(shop.getOwner())) {
|
||||||
|
// Don't tax them if they're purchasing from
|
||||||
|
// themselves.
|
||||||
|
// Do charge an amount of tax though.
|
||||||
|
final double tax = plugin.getConfigManager().getTax();
|
||||||
|
final double total = amount * shop.getPrice();
|
||||||
|
if (!shop.isUnlimited() || plugin.getConfig().getBoolean("shop.pay-unlimited-shop-owners")) {
|
||||||
|
// Tries to check their balance nicely to see if
|
||||||
|
// they can afford it.
|
||||||
|
if (plugin.getEcon().getBalance(shop.getOwner()) < amount * shop.getPrice()) {
|
||||||
|
p.sendMessage(MsgUtil.p("the-owner-cant-afford-to-buy-from-you", format(amount * shop.getPrice()), format(plugin.getEcon().getBalance(shop.getOwner()))));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
// Check for plugins faking econ.has(amount)
|
||||||
|
if (!plugin.getEcon().withdraw(shop.getOwner(), total)) {
|
||||||
|
p.sendMessage(MsgUtil.p("the-owner-cant-afford-to-buy-from-you", format(amount * shop.getPrice()), format(plugin.getEcon().getBalance(shop.getOwner()))));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (tax != 0) {
|
||||||
|
plugin.getEcon().deposit(plugin.getConfigManager().getTaxAccount(), total * tax);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Give them the money after we know we succeeded
|
||||||
|
plugin.getEcon().deposit(p.getName(), total * (1 - tax));
|
||||||
|
// Notify the owner of the purchase.
|
||||||
|
String msg = MsgUtil.p("player-sold-to-your-store", p.getName(), "" + amount, shop.getDataName());
|
||||||
|
if (space == amount) {
|
||||||
|
msg += "\n" + MsgUtil.p("shop-out-of-space", "" + shop.getLocation().getBlockX(), "" + shop.getLocation().getBlockY(), "" + shop.getLocation().getBlockZ());
|
||||||
|
}
|
||||||
|
MsgUtil.send(shop.getOwner(), msg);
|
||||||
|
}
|
||||||
|
shop.buy(p, amount);
|
||||||
|
MsgUtil.sendSellSuccess(p, shop, amount);
|
||||||
|
plugin.log(String.format("玩家: %s 出售了 %s 件商品 到 %s 获得 %s", p.getName(), amount, shop.toString(), shop.getPrice() * amount));
|
||||||
|
}
|
||||||
|
shop.setSignText(); // Update the signs count
|
||||||
|
}
|
||||||
|
/* If it was already cancelled (from destroyed) */
|
||||||
|
else {
|
||||||
|
return; // It was cancelled, go away.
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Loading…
Reference in New Issue
Block a user