Use optionals

This commit is contained in:
Eric 2020-03-18 16:00:51 +01:00
parent 5e479d65c2
commit b55e9f2c4f
13 changed files with 106 additions and 94 deletions

View File

@ -27,9 +27,7 @@ public abstract class TimedFlag implements Flag {
@Override @Override
public void onAssign(ShopPlayer player) { public void onAssign(ShopPlayer player) {
task = Bukkit.getScheduler().runTaskLater(plugin, () -> { task = Bukkit.getScheduler().runTaskLater(plugin, () -> {
if (this.equals(player.getFlag())) { player.getFlag().filter(this::equals).ifPresent(f -> player.removeFlag());
player.removeFlag();
}
}, seconds * 20); }, seconds * 20);
} }

View File

@ -2,6 +2,7 @@ package de.epiceric.shopchest.api.player;
import java.text.MessageFormat; import java.text.MessageFormat;
import java.util.Collection; import java.util.Collection;
import java.util.Optional;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -50,10 +51,10 @@ public interface ShopPlayer {
/** /**
* Gets this player's flag * Gets this player's flag
* *
* @return the flag or {@code null} if the player does not have one * @return the flag or an empty optional if the player does not have one
* @since 1.13 * @since 1.13
*/ */
Flag getFlag(); Optional<Flag> getFlag();
/** /**
* Sets this player's flag * Sets this player's flag
@ -70,7 +71,7 @@ public interface ShopPlayer {
* @since 1.13 * @since 1.13
*/ */
default boolean hasFlag() { default boolean hasFlag() {
return getFlag() != null; return getFlag().isPresent();
} }
/** /**
@ -109,6 +110,6 @@ public interface ShopPlayer {
*/ */
default boolean ownsShop(Shop shop) { default boolean ownsShop(Shop shop) {
return shop != null && !shop.isAdminShop() return shop != null && !shop.isAdminShop()
&& shop.getVendor().getUniqueId().equals(getBukkitPlayer().getUniqueId()); && shop.getVendor().get().getUniqueId().equals(getBukkitPlayer().getUniqueId());
} }
} }

View File

@ -1,5 +1,6 @@
package de.epiceric.shopchest.api.shop; package de.epiceric.shopchest.api.shop;
import java.util.Optional;
import java.util.function.Consumer; import java.util.function.Consumer;
import org.bukkit.Location; import org.bukkit.Location;
@ -31,10 +32,10 @@ public interface Shop {
/** /**
* Gets the player who owns this shop * Gets the player who owns this shop
* *
* @return the vendor or {@code null} if this shop is an admin shop * @return the vendor or an empty optional if this shop is an admin shop
* @since 1.13 * @since 1.13
*/ */
OfflinePlayer getVendor(); Optional<OfflinePlayer> getVendor();
/** /**
* Gets a copy of the product this shop is buying or selling * Gets a copy of the product this shop is buying or selling
@ -88,7 +89,7 @@ public interface Shop {
* @since 1.13 * @since 1.13
*/ */
default boolean isAdminShop() { default boolean isAdminShop() {
return getVendor() == null; return !getVendor().isPresent();
} }
/** /**

View File

@ -85,10 +85,8 @@ public class ShopManagerImpl implements ShopManager {
} }
shopsInWorld.get(worldName).put(toBlockLocation(shop.getLocation()), shop); shopsInWorld.get(worldName).put(toBlockLocation(shop.getLocation()), shop);
Location otherLoc = ((ShopImpl) shop).getOtherLocation(); ((ShopImpl) shop).getOtherLocation().ifPresent(otherLoc ->
if (otherLoc != null) { shopsInWorld.get(worldName).put(toBlockLocation(otherLoc), shop));;
shopsInWorld.get(worldName).put(toBlockLocation(otherLoc), shop);
}
}); });
callback.accept(shops); callback.accept(shops);
}, },
@ -125,7 +123,7 @@ public class ShopManagerImpl implements ShopManager {
@Override @Override
public Collection<Shop> getShops(OfflinePlayer vendor) { public Collection<Shop> getShops(OfflinePlayer vendor) {
return getShops().stream().filter(shop -> !shop.isAdminShop()) return getShops().stream().filter(shop -> !shop.isAdminShop())
.filter(shop -> shop.getVendor().getUniqueId().equals(vendor.getUniqueId())) .filter(shop -> shop.getVendor().get().getUniqueId().equals(vendor.getUniqueId()))
.collect(Collectors.toList()); .collect(Collectors.toList());
} }
@ -151,10 +149,8 @@ public class ShopManagerImpl implements ShopManager {
} }
shopsInWorld.get(worldName).put(toBlockLocation(location), shop); shopsInWorld.get(worldName).put(toBlockLocation(location), shop);
Location otherLoc = ((ShopImpl) shop).getOtherLocation(); ((ShopImpl) shop).getOtherLocation().ifPresent(otherLoc ->
if (otherLoc != null) { shopsInWorld.get(worldName).put(toBlockLocation(otherLoc), shop));
shopsInWorld.get(worldName).put(toBlockLocation(otherLoc), shop);
}
callback.accept(shop); callback.accept(shop);
}, },

View File

@ -460,7 +460,7 @@ public abstract class Database {
ps.setInt(1, shop.getId()); ps.setInt(1, shop.getId());
} }
ps.setString(i+1, shop.isAdminShop() ? "admin" : shop.getVendor().getUniqueId().toString()); ps.setString(i+1, shop.getVendor().map(vendor -> vendor.getUniqueId().toString()).orElse("admin"));
ps.setString(i+2, encodeItemStack(shop.getProduct().getItemStack())); ps.setString(i+2, encodeItemStack(shop.getProduct().getItemStack()));
ps.setInt(i+3, shop.getProduct().getAmount()); ps.setInt(i+3, shop.getProduct().getAmount());
ps.setString(i+4, shop.getLocation().getWorld().getName()); ps.setString(i+4, shop.getLocation().getWorld().getName());
@ -523,8 +523,8 @@ public abstract class Database {
ps.setString(6, product.getLocalizedName()); ps.setString(6, product.getLocalizedName());
ps.setString(7, encodeItemStack(product.getItemStack())); ps.setString(7, encodeItemStack(product.getItemStack()));
ps.setInt(8, product.getAmount()); ps.setInt(8, product.getAmount());
ps.setString(9, shop.getVendor().getName()); ps.setString(9, shop.getVendor().map(OfflinePlayer::getName).orElse(""));
ps.setString(10, shop.getVendor().getUniqueId().toString()); ps.setString(10, shop.getVendor().map(vendor -> vendor.getUniqueId().toString()).orElse(""));
ps.setBoolean(11, shop.isAdminShop()); ps.setBoolean(11, shop.isAdminShop());
ps.setString(12, shop.getLocation().getWorld().getName()); ps.setString(12, shop.getLocation().getWorld().getName());
ps.setInt(13, shop.getLocation().getBlockX()); ps.setInt(13, shop.getLocation().getBlockX());

View File

@ -22,7 +22,6 @@ import de.epiceric.shopchest.api.event.ShopOpenEvent;
import de.epiceric.shopchest.api.event.ShopRemoveEvent; import de.epiceric.shopchest.api.event.ShopRemoveEvent;
import de.epiceric.shopchest.api.event.ShopBuySellEvent.Type; import de.epiceric.shopchest.api.event.ShopBuySellEvent.Type;
import de.epiceric.shopchest.api.flag.CreateFlag; import de.epiceric.shopchest.api.flag.CreateFlag;
import de.epiceric.shopchest.api.flag.Flag;
import de.epiceric.shopchest.api.flag.InfoFlag; import de.epiceric.shopchest.api.flag.InfoFlag;
import de.epiceric.shopchest.api.flag.OpenFlag; import de.epiceric.shopchest.api.flag.OpenFlag;
import de.epiceric.shopchest.api.flag.RemoveFlag; import de.epiceric.shopchest.api.flag.RemoveFlag;
@ -57,8 +56,8 @@ public class ChestInteractListener implements Listener {
if (shopOpt.isPresent()) { if (shopOpt.isPresent()) {
Shop shop = shopOpt.get(); Shop shop = shopOpt.get();
if (player.hasFlag() && e.getAction() == Action.RIGHT_CLICK_BLOCK) { if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
Flag flag = player.getFlag(); player.getFlag().ifPresent(flag -> {
if (flag instanceof InfoFlag) { if (flag instanceof InfoFlag) {
plugin.getServer().getPluginManager().callEvent(new ShopInfoEvent(player, shop)); plugin.getServer().getPluginManager().callEvent(new ShopInfoEvent(player, shop));
e.setCancelled(true); e.setCancelled(true);
@ -74,6 +73,7 @@ public class ChestInteractListener implements Listener {
player.sendMessage("§cThis chest already is a shop."); // TODO: i18n player.sendMessage("§cThis chest already is a shop."); // TODO: i18n
} }
player.removeFlag(); player.removeFlag();
});
} else if (e.hasItem() && e.getItem().getType() == Config.CORE_SHOP_INFO_ITEM.get()) { } else if (e.hasItem() && e.getItem().getType() == Config.CORE_SHOP_INFO_ITEM.get()) {
plugin.getServer().getPluginManager().callEvent(new ShopInfoEvent(player, shopOpt.get())); plugin.getServer().getPluginManager().callEvent(new ShopInfoEvent(player, shopOpt.get()));
e.setCancelled(true); e.setCancelled(true);
@ -100,14 +100,17 @@ public class ChestInteractListener implements Listener {
.callEvent(new ShopBuySellEvent(player, shop, type, shop.getProduct().getAmount(), price)); .callEvent(new ShopBuySellEvent(player, shop, type, shop.getProduct().getAmount(), price));
} }
} else { } else {
if (player.getFlag() instanceof CreateFlag && e.getAction() == Action.RIGHT_CLICK_BLOCK) { if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
player.getFlag().filter(flag -> flag instanceof CreateFlag).ifPresent(f -> {
e.setCancelled(true); e.setCancelled(true);
CreateFlag flag = (CreateFlag) player.getFlag(); CreateFlag flag = (CreateFlag) f;
player.removeFlag(); player.removeFlag();
OfflinePlayer vendor = flag.isAdminShop() ? null : player.getBukkitPlayer(); OfflinePlayer vendor = flag.isAdminShop() ? null : player.getBukkitPlayer();
plugin.getServer().getPluginManager().callEvent(new ShopCreateEvent(player, plugin.getServer().getPluginManager().callEvent(new ShopCreateEvent(player,
new ShopImpl(vendor, flag.getProduct(), location, flag.getBuyPrice(), flag.getSellPrice()), new ShopImpl(vendor, flag.getProduct(), location, flag.getBuyPrice(), flag.getSellPrice()),
Config.SHOP_CREATION_PRICE.get())); Config.SHOP_CREATION_PRICE.get()));
});
} }
} }
} }

View File

@ -38,21 +38,20 @@ public class CreativeSelectListener implements Listener {
} }
ShopPlayer player = plugin.wrapPlayer((Player) e.getWhoClicked()); ShopPlayer player = plugin.wrapPlayer((Player) e.getWhoClicked());
if (player.getFlag() instanceof SelectFlag) { player.getFlag().filter(flag -> flag instanceof SelectFlag).ifPresent(f -> {
e.setCancelled(true); e.setCancelled(true);
if (e.getCursor() == null || e.getCursor().getType() == Material.AIR) { if (e.getCursor() == null || e.getCursor().getType() == Material.AIR) {
return; return;
} }
SelectFlag flag = (SelectFlag) player.getFlag(); SelectFlag flag = (SelectFlag) f;
player.removeFlag(); player.removeFlag();
plugin.getServer().getScheduler().runTask(plugin, () -> player.getBukkitPlayer().closeInventory()); plugin.getServer().getScheduler().runTask(plugin, () -> player.getBukkitPlayer().closeInventory());
plugin.getServer().getPluginManager().callEvent(new ShopSelectItemEvent(player, e.getCursor(), plugin.getServer().getPluginManager().callEvent(new ShopSelectItemEvent(player, e.getCursor(),
flag.getAmount(), flag.getBuyPrice(), flag.getSellPrice(), flag.isAdminShop())); flag.getAmount(), flag.getBuyPrice(), flag.getSellPrice(), flag.isAdminShop()));
});
}
} }
@EventHandler @EventHandler
@ -62,7 +61,7 @@ public class CreativeSelectListener implements Listener {
} }
ShopPlayer player = plugin.wrapPlayer((Player) e.getPlayer()); ShopPlayer player = plugin.wrapPlayer((Player) e.getPlayer());
if (player.getFlag() instanceof SelectFlag) { if (hasSelectFlag(player)) {
player.removeFlag(); player.removeFlag();
player.sendMessage("§cShop creation has been cancelled."); player.sendMessage("§cShop creation has been cancelled.");
} }
@ -82,7 +81,7 @@ public class CreativeSelectListener implements Listener {
} }
ShopPlayer player = plugin.wrapPlayer((Player) e.getWhoClicked()); ShopPlayer player = plugin.wrapPlayer((Player) e.getWhoClicked());
if (player.getFlag() instanceof SelectFlag) { if (hasSelectFlag(player)) {
e.setCancelled(true); e.setCancelled(true);
} }
} }
@ -95,7 +94,7 @@ public class CreativeSelectListener implements Listener {
} }
ShopPlayer player = plugin.wrapPlayer((Player) e.getSource().getHolder()); ShopPlayer player = plugin.wrapPlayer((Player) e.getSource().getHolder());
if (player.getFlag() instanceof SelectFlag) { if (hasSelectFlag(player)) {
e.setCancelled(true); e.setCancelled(true);
} }
} }
@ -108,7 +107,7 @@ public class CreativeSelectListener implements Listener {
} }
ShopPlayer player = plugin.wrapPlayer((Player) e.getEntity()); ShopPlayer player = plugin.wrapPlayer((Player) e.getEntity());
if (player.getFlag() instanceof SelectFlag) { if (hasSelectFlag(player)) {
e.setCancelled(true); e.setCancelled(true);
} }
} }
@ -117,7 +116,7 @@ public class CreativeSelectListener implements Listener {
public void onBlockBreak(BlockBreakEvent e) { public void onBlockBreak(BlockBreakEvent e) {
// Cancel any block breaks if SelectFlag is assigned // Cancel any block breaks if SelectFlag is assigned
ShopPlayer player = plugin.wrapPlayer(e.getPlayer()); ShopPlayer player = plugin.wrapPlayer(e.getPlayer());
if (player.getFlag() instanceof SelectFlag) { if (hasSelectFlag(player)) {
e.setCancelled(true); e.setCancelled(true);
} }
} }
@ -126,7 +125,7 @@ public class CreativeSelectListener implements Listener {
public void onBlockPlace(BlockPlaceEvent e) { public void onBlockPlace(BlockPlaceEvent e) {
// Cancel any block places if SelectFlag is assigned // Cancel any block places if SelectFlag is assigned
ShopPlayer player = plugin.wrapPlayer(e.getPlayer()); ShopPlayer player = plugin.wrapPlayer(e.getPlayer());
if (player.getFlag() instanceof SelectFlag) { if (hasSelectFlag(player)) {
e.setCancelled(true); e.setCancelled(true);
} }
} }
@ -135,7 +134,7 @@ public class CreativeSelectListener implements Listener {
public void onBlockMultiPlace(BlockMultiPlaceEvent e) { public void onBlockMultiPlace(BlockMultiPlaceEvent e) {
// Cancel any block places if SelectFlag is assigned // Cancel any block places if SelectFlag is assigned
ShopPlayer player = plugin.wrapPlayer(e.getPlayer()); ShopPlayer player = plugin.wrapPlayer(e.getPlayer());
if (player.getFlag() instanceof SelectFlag) { if (hasSelectFlag(player)) {
e.setCancelled(true); e.setCancelled(true);
} }
} }
@ -144,7 +143,7 @@ public class CreativeSelectListener implements Listener {
public void onPlayerInteract(PlayerInteractEvent e) { public void onPlayerInteract(PlayerInteractEvent e) {
// Cancel any interactions if SelectFlag is assigned // Cancel any interactions if SelectFlag is assigned
ShopPlayer player = plugin.wrapPlayer(e.getPlayer()); ShopPlayer player = plugin.wrapPlayer(e.getPlayer());
if (player.getFlag() instanceof SelectFlag) { if (hasSelectFlag(player)) {
e.setCancelled(true); e.setCancelled(true);
} }
} }
@ -153,7 +152,7 @@ public class CreativeSelectListener implements Listener {
public void onPlayerInteractAtEntity(PlayerInteractAtEntityEvent e) { public void onPlayerInteractAtEntity(PlayerInteractAtEntityEvent e) {
// Cancel any entity interactions if SelectFlag is assigned // Cancel any entity interactions if SelectFlag is assigned
ShopPlayer player = plugin.wrapPlayer(e.getPlayer()); ShopPlayer player = plugin.wrapPlayer(e.getPlayer());
if (player.getFlag() instanceof SelectFlag) { if (hasSelectFlag(player)) {
e.setCancelled(true); e.setCancelled(true);
} }
} }
@ -166,7 +165,7 @@ public class CreativeSelectListener implements Listener {
} }
ShopPlayer player = plugin.wrapPlayer((Player) e.getDamager()); ShopPlayer player = plugin.wrapPlayer((Player) e.getDamager());
if (player.getFlag() instanceof SelectFlag) { if (hasSelectFlag(player)) {
e.setCancelled(true); e.setCancelled(true);
} }
} }
@ -175,9 +174,13 @@ public class CreativeSelectListener implements Listener {
public void onPlayerMove(PlayerMoveEvent e) { public void onPlayerMove(PlayerMoveEvent e) {
// Cancel any player movement if SelectFlag is assigned // Cancel any player movement if SelectFlag is assigned
ShopPlayer player = plugin.wrapPlayer(e.getPlayer()); ShopPlayer player = plugin.wrapPlayer(e.getPlayer());
if (player.getFlag() instanceof SelectFlag) { if (hasSelectFlag(player)) {
e.setCancelled(true); e.setCancelled(true);
} }
} }
private boolean hasSelectFlag(ShopPlayer player) {
return player.getFlag().filter(flag -> flag instanceof SelectFlag).isPresent();
}
} }

View File

@ -1,6 +1,9 @@
package de.epiceric.shopchest.listener.internal; package de.epiceric.shopchest.listener.internal;
import java.util.Optional;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.block.BlockFace; import org.bukkit.block.BlockFace;
import org.bukkit.block.data.type.Chest; import org.bukkit.block.data.type.Chest;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -132,7 +135,7 @@ public class ShopInteractListener implements Listener {
e.setCancelled(true); e.setCancelled(true);
return; return;
} }
if (!shop.isAdminShop() && !economy.has(shop.getVendor(), shop.getWorld().getName(), e.getPrice())) { if (!shop.isAdminShop() && !economy.has(shop.getVendor().get(), shop.getWorld().getName(), e.getPrice())) {
player.sendMessage("§cThe vendor of this shop doesn't have enough money."); player.sendMessage("§cThe vendor of this shop doesn't have enough money.");
e.setCancelled(true); e.setCancelled(true);
return; return;
@ -158,12 +161,14 @@ public class ShopInteractListener implements Listener {
} }
boolean vendorMessages = Config.FEATURES_VENDOR_MESSAGES.get(); boolean vendorMessages = Config.FEATURES_VENDOR_MESSAGES.get();
ShopPlayer vendor = shop.getVendor().isOnline() ? plugin.wrapPlayer(shop.getVendor().getPlayer()) : null; Optional<ShopPlayer> vendor = shop.getVendor().filter(OfflinePlayer::isOnline)
.map(offlinePlayer -> plugin.wrapPlayer(offlinePlayer.getPlayer()));
if (e.getType() == Type.BUY) { if (e.getType() == Type.BUY) {
if (shopAmount < e.getAmount()) { if (shopAmount < e.getAmount()) {
player.sendMessage("§cThis shop is out of items to sell."); player.sendMessage("§cThis shop is out of items to sell.");
if (vendorMessages && vendor != null) { if (vendorMessages && vendor.isPresent()) {
vendor.sendMessage("§cYour shop selling §e{0} x {1} §cis out of stock.", product.getAmount(), vendor.get().sendMessage("§cYour shop selling §e{0} x {1} §cis out of stock.", product.getAmount(),
product.getLocalizedName()); product.getLocalizedName());
} }
e.setCancelled(true); e.setCancelled(true);
@ -172,8 +177,8 @@ public class ShopInteractListener implements Listener {
} else if (e.getType() == Type.SELL) { } else if (e.getType() == Type.SELL) {
if (shopSpace < e.getAmount()) { if (shopSpace < e.getAmount()) {
player.sendMessage("§cThis shop doesn't have enough space for your items."); player.sendMessage("§cThis shop doesn't have enough space for your items.");
if (vendorMessages && vendor != null) { if (vendorMessages && vendor.isPresent()) {
vendor.sendMessage("§cYour shop buying §e{0} x {1} §cis full.", product.getAmount(), vendor.get().sendMessage("§cYour shop buying §e{0} x {1} §cis full.", product.getAmount(),
product.getLocalizedName()); product.getLocalizedName());
} }
e.setCancelled(true); e.setCancelled(true);

View File

@ -33,7 +33,7 @@ public class ShopCommandMonitorListener implements Listener {
ShopPlayer player = e.getPlayer(); ShopPlayer player = e.getPlayer();
if (!e.isItemSelected()) { if (!e.isItemSelected()) {
if (!(player.getFlag() instanceof SelectFlag)) { if (!(player.getFlag().orElse(null) instanceof SelectFlag)) {
// Set flag only if player doesn't already have SelectFlag // Set flag only if player doesn't already have SelectFlag
Flag flag = new SelectFlag(e.getAmount(), e.getBuyPrice(), e.getSellPrice(), e.isAdminShop(), Flag flag = new SelectFlag(e.getAmount(), e.getBuyPrice(), e.getSellPrice(), e.isAdminShop(),
player.getBukkitPlayer().getGameMode()); player.getBukkitPlayer().getGameMode());

View File

@ -5,6 +5,7 @@ import java.text.MessageFormat;
import com.google.gson.JsonPrimitive; import com.google.gson.JsonPrimitive;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
@ -47,7 +48,7 @@ public class ShopInteractMonitorListener implements Listener {
error -> e.getPlayer().sendMessage("§cFailed to add admin shop: {0}", error.getMessage()) error -> e.getPlayer().sendMessage("§cFailed to add admin shop: {0}", error.getMessage())
); );
} else { } else {
plugin.getShopManager().addShop(shop.getVendor(), shop.getProduct(), shop.getLocation(), shop.getBuyPrice(), shop.getSellPrice(), plugin.getShopManager().addShop(shop.getVendor().get(), shop.getProduct(), shop.getLocation(), shop.getBuyPrice(), shop.getSellPrice(),
newShop -> e.getPlayer().sendMessage("§aShop has been added with ID {0}.", newShop.getId()), // TODO: i18n newShop -> e.getPlayer().sendMessage("§aShop has been added with ID {0}.", newShop.getId()), // TODO: i18n
error -> e.getPlayer().sendMessage("§cFailed to add shop: {0}", error.getMessage()) error -> e.getPlayer().sendMessage("§cFailed to add shop: {0}", error.getMessage())
); );
@ -104,7 +105,7 @@ public class ShopInteractMonitorListener implements Listener {
// TODO: i18n // TODO: i18n
player.sendMessage("§e--------- §fShop Info §e-----------------------------"); player.sendMessage("§e--------- §fShop Info §e-----------------------------");
player.sendMessage("§7Hover over the underlined product for more details"); player.sendMessage("§7Hover over the underlined product for more details");
player.sendMessage("§6Vendor: §f{0}", shop.isAdminShop() ? "Admin" : shop.getVendor().getName()); player.sendMessage("§6Vendor: §f{0}", shop.getVendor().map(OfflinePlayer::getName).orElse("Admin"));
if (productJson.startsWith("[{")) { if (productJson.startsWith("[{")) {
NmsUtil.sendJsonMessage(player.getBukkitPlayer(), getProductJson(product)); NmsUtil.sendJsonMessage(player.getBukkitPlayer(), getProductJson(product));
@ -161,7 +162,6 @@ public class ShopInteractMonitorListener implements Listener {
Economy economy = ((ShopChestImpl) plugin).getEconomy(); Economy economy = ((ShopChestImpl) plugin).getEconomy();
Player bukkitPlayer = e.getPlayer().getBukkitPlayer(); Player bukkitPlayer = e.getPlayer().getBukkitPlayer();
String worldName = e.getShop().getWorld().getName(); String worldName = e.getShop().getWorld().getName();
boolean isAdmin = e.getShop().isAdminShop();
if (e.getType() == Type.BUY) { if (e.getType() == Type.BUY) {
EconomyResponse r = economy.withdrawPlayer(bukkitPlayer, worldName, e.getPrice()); EconomyResponse r = economy.withdrawPlayer(bukkitPlayer, worldName, e.getPrice());
@ -171,8 +171,8 @@ public class ShopInteractMonitorListener implements Listener {
return; return;
} }
if (!isAdmin) { e.getShop().getVendor().ifPresent(vendor -> {
EconomyResponse rVendor = economy.depositPlayer(e.getShop().getVendor(), worldName, e.getPrice()); EconomyResponse rVendor = economy.depositPlayer(vendor, worldName, e.getPrice());
if (!rVendor.transactionSuccess()) { if (!rVendor.transactionSuccess()) {
e.setCancelled(true); e.setCancelled(true);
e.getPlayer().sendMessage("§cFailed to deposit money to vendor: {0}", r.errorMessage); // TODO: i18n e.getPlayer().sendMessage("§cFailed to deposit money to vendor: {0}", r.errorMessage); // TODO: i18n
@ -183,7 +183,7 @@ public class ShopInteractMonitorListener implements Listener {
} }
return; return;
} }
} });
} else { } else {
EconomyResponse r = economy.depositPlayer(bukkitPlayer, worldName, e.getPrice()); EconomyResponse r = economy.depositPlayer(bukkitPlayer, worldName, e.getPrice());
if (!r.transactionSuccess()) { if (!r.transactionSuccess()) {
@ -192,8 +192,8 @@ public class ShopInteractMonitorListener implements Listener {
return; return;
} }
if (!isAdmin) { e.getShop().getVendor().ifPresent(vendor -> {
EconomyResponse rVendor = economy.withdrawPlayer(e.getShop().getVendor(), worldName, e.getPrice()); EconomyResponse rVendor = economy.withdrawPlayer(vendor, worldName, e.getPrice());
if (!rVendor.transactionSuccess()) { if (!rVendor.transactionSuccess()) {
e.setCancelled(true); e.setCancelled(true);
e.getPlayer().sendMessage("§cFailed to withdraw money from vendor: {0}", r.errorMessage); // TODO: i18n e.getPlayer().sendMessage("§cFailed to withdraw money from vendor: {0}", r.errorMessage); // TODO: i18n
@ -204,7 +204,8 @@ public class ShopInteractMonitorListener implements Listener {
} }
return; return;
} }
} });
} }
} }
@ -233,7 +234,7 @@ public class ShopInteractMonitorListener implements Listener {
player.sendMessage("§aYou bought §e{0} x {1} §afor §e{2}§a.", e.getAmount(), itemName, price); player.sendMessage("§aYou bought §e{0} x {1} §afor §e{2}§a.", e.getAmount(), itemName, price);
} else { } else {
player.sendMessage("§aYou bought §e{0} x {1} §afor §e{2} §afrom §e{3}§a.", e.getAmount(), itemName, player.sendMessage("§aYou bought §e{0} x {1} §afor §e{2} §afrom §e{3}§a.", e.getAmount(), itemName,
price, shop.getVendor().getName()); price, shop.getVendor().get().getName());
} }
} else { } else {
for (int i = 0; i < e.getAmount(); i++) { for (int i = 0; i < e.getAmount(); i++) {
@ -248,7 +249,7 @@ public class ShopInteractMonitorListener implements Listener {
player.sendMessage("§aYou sold §e{0} x {1} §afor §e{2}§a.", e.getAmount(), itemName, price); player.sendMessage("§aYou sold §e{0} x {1} §afor §e{2}§a.", e.getAmount(), itemName, price);
} else { } else {
player.sendMessage("§aYou sold §e{0} x {1} §afor §e{2} §ato §e{3}§a.", e.getAmount(), itemName, player.sendMessage("§aYou sold §e{0} x {1} §afor §e{2} §ato §e{3}§a.", e.getAmount(), itemName,
price, shop.getVendor().getName()); price, shop.getVendor().get().getName());
} }
} }
} catch (ChestNotFoundException ignored) { } catch (ChestNotFoundException ignored) {

View File

@ -3,6 +3,7 @@ package de.epiceric.shopchest.player;
import java.util.Collection; import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Optional;
import java.util.UUID; import java.util.UUID;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -55,8 +56,8 @@ public class ShopPlayerImpl implements ShopPlayer {
} }
@Override @Override
public Flag getFlag() { public Optional<Flag> getFlag() {
return flag; return Optional.ofNullable(flag);
} }
@Override @Override

View File

@ -1,5 +1,7 @@
package de.epiceric.shopchest.shop; package de.epiceric.shopchest.shop;
import java.util.Optional;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
@ -58,9 +60,9 @@ public class ShopImpl implements Shop {
/** /**
* Gets the location of the other chest block if this shop is on a double chest * Gets the location of the other chest block if this shop is on a double chest
* *
* @return the other location or {@code null} if there is no other chest * @return the other location or an empty optional if there is no other chest
*/ */
public Location getOtherLocation() { public Optional<Location> getOtherLocation() {
try { try {
Inventory inv = getInventory(); Inventory inv = getInventory();
if (inv instanceof DoubleChestInventory) { if (inv instanceof DoubleChestInventory) {
@ -71,15 +73,15 @@ public class ShopImpl implements Shop {
location.getBlockX() == left.getBlockX() && location.getBlockX() == left.getBlockX() &&
location.getBlockY() == left.getBlockY() && location.getBlockY() == left.getBlockY() &&
location.getBlockZ() == left.getBlockZ()) { location.getBlockZ() == left.getBlockZ()) {
return right; return Optional.of(right);
} else { } else {
return left; return Optional.of(left);
} }
} }
} catch (ChestNotFoundException e) { } catch (ChestNotFoundException e) {
Logger.severe(e.getMessage()); Logger.severe(e.getMessage());
} }
return null; return Optional.empty();
} }
/** /**
@ -127,8 +129,8 @@ public class ShopImpl implements Shop {
} }
@Override @Override
public OfflinePlayer getVendor() { public Optional<OfflinePlayer> getVendor() {
return vendor; return Optional.ofNullable(vendor);
} }
@Override @Override

View File

@ -4,6 +4,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.OfflinePlayer;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import de.epiceric.shopchest.api.ShopChest; import de.epiceric.shopchest.api.ShopChest;
@ -20,7 +21,7 @@ public class Hologram {
int topLine = shop.canPlayerBuy() && shop.canPlayerSell() ? 3 : 2; int topLine = shop.canPlayerBuy() && shop.canPlayerSell() ? 3 : 2;
// TODO: Configurable // TODO: Configurable
lines.add(new HologramLine(getLocation(topLine), shop.isAdminShop() ? "§cAdmin Shop" : shop.getVendor().getName())); lines.add(new HologramLine(getLocation(topLine), shop.getVendor().map(OfflinePlayer::getName).orElse("§cAdmin Shop")));
lines.add(new HologramLine(getLocation(topLine - 1), shop.getProduct().getAmount() + " §7x §f" + shop.getProduct().getLocalizedName())); lines.add(new HologramLine(getLocation(topLine - 1), shop.getProduct().getAmount() + " §7x §f" + shop.getProduct().getLocalizedName()));
if (shop.canPlayerBuy()) lines.add(new HologramLine(getLocation(topLine - 2), "§eBuy for " + plugin.formatEconomy(shop.getBuyPrice()))); if (shop.canPlayerBuy()) lines.add(new HologramLine(getLocation(topLine - 2), "§eBuy for " + plugin.formatEconomy(shop.getBuyPrice())));
if (shop.canPlayerSell()) lines.add(new HologramLine(getLocation(0), "§eSell for " + plugin.formatEconomy(shop.getSellPrice()))); if (shop.canPlayerSell()) lines.add(new HologramLine(getLocation(0), "§eSell for " + plugin.formatEconomy(shop.getSellPrice())));
@ -29,7 +30,7 @@ public class Hologram {
private Location getLocation(int lineFromBottom) { private Location getLocation(int lineFromBottom) {
double lineHeight = 0.25; double lineHeight = 0.25;
Location loc = shop.getLocation().subtract(0, 0.75, 0); Location loc = shop.getLocation().subtract(0, 0.75, 0);
Location otherLoc = ((ShopImpl) shop).getOtherLocation(); Location otherLoc = ((ShopImpl) shop).getOtherLocation().orElse(null);
if (otherLoc == null) { if (otherLoc == null) {
return loc.add(0.5, lineFromBottom * lineHeight, 0.5); return loc.add(0.5, lineFromBottom * lineHeight, 0.5);