diff --git a/api/src/main/java/de/epiceric/shopchest/api/flag/TimedFlag.java b/api/src/main/java/de/epiceric/shopchest/api/flag/TimedFlag.java index baed417..d62c8dd 100644 --- a/api/src/main/java/de/epiceric/shopchest/api/flag/TimedFlag.java +++ b/api/src/main/java/de/epiceric/shopchest/api/flag/TimedFlag.java @@ -27,9 +27,7 @@ public abstract class TimedFlag implements Flag { @Override public void onAssign(ShopPlayer player) { task = Bukkit.getScheduler().runTaskLater(plugin, () -> { - if (this.equals(player.getFlag())) { - player.removeFlag(); - } + player.getFlag().filter(this::equals).ifPresent(f -> player.removeFlag()); }, seconds * 20); } diff --git a/api/src/main/java/de/epiceric/shopchest/api/player/ShopPlayer.java b/api/src/main/java/de/epiceric/shopchest/api/player/ShopPlayer.java index 0640c19..6b28d59 100644 --- a/api/src/main/java/de/epiceric/shopchest/api/player/ShopPlayer.java +++ b/api/src/main/java/de/epiceric/shopchest/api/player/ShopPlayer.java @@ -2,6 +2,7 @@ package de.epiceric.shopchest.api.player; import java.text.MessageFormat; import java.util.Collection; +import java.util.Optional; import org.bukkit.entity.Player; @@ -50,10 +51,10 @@ public interface ShopPlayer { /** * 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 */ - Flag getFlag(); + Optional getFlag(); /** * Sets this player's flag @@ -70,7 +71,7 @@ public interface ShopPlayer { * @since 1.13 */ default boolean hasFlag() { - return getFlag() != null; + return getFlag().isPresent(); } /** @@ -109,6 +110,6 @@ public interface ShopPlayer { */ default boolean ownsShop(Shop shop) { return shop != null && !shop.isAdminShop() - && shop.getVendor().getUniqueId().equals(getBukkitPlayer().getUniqueId()); + && shop.getVendor().get().getUniqueId().equals(getBukkitPlayer().getUniqueId()); } } \ No newline at end of file diff --git a/api/src/main/java/de/epiceric/shopchest/api/shop/Shop.java b/api/src/main/java/de/epiceric/shopchest/api/shop/Shop.java index 138dee9..428c8d0 100644 --- a/api/src/main/java/de/epiceric/shopchest/api/shop/Shop.java +++ b/api/src/main/java/de/epiceric/shopchest/api/shop/Shop.java @@ -1,5 +1,6 @@ package de.epiceric.shopchest.api.shop; +import java.util.Optional; import java.util.function.Consumer; import org.bukkit.Location; @@ -31,10 +32,10 @@ public interface 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 */ - OfflinePlayer getVendor(); + Optional getVendor(); /** * Gets a copy of the product this shop is buying or selling @@ -88,7 +89,7 @@ public interface Shop { * @since 1.13 */ default boolean isAdminShop() { - return getVendor() == null; + return !getVendor().isPresent(); } /** diff --git a/new-implementation/src/main/java/de/epiceric/shopchest/ShopManagerImpl.java b/new-implementation/src/main/java/de/epiceric/shopchest/ShopManagerImpl.java index 3bd6df2..8a31d06 100644 --- a/new-implementation/src/main/java/de/epiceric/shopchest/ShopManagerImpl.java +++ b/new-implementation/src/main/java/de/epiceric/shopchest/ShopManagerImpl.java @@ -85,10 +85,8 @@ public class ShopManagerImpl implements ShopManager { } shopsInWorld.get(worldName).put(toBlockLocation(shop.getLocation()), shop); - Location otherLoc = ((ShopImpl) shop).getOtherLocation(); - if (otherLoc != null) { - shopsInWorld.get(worldName).put(toBlockLocation(otherLoc), shop); - } + ((ShopImpl) shop).getOtherLocation().ifPresent(otherLoc -> + shopsInWorld.get(worldName).put(toBlockLocation(otherLoc), shop));; }); callback.accept(shops); }, @@ -125,7 +123,7 @@ public class ShopManagerImpl implements ShopManager { @Override public Collection getShops(OfflinePlayer vendor) { 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()); } @@ -151,10 +149,8 @@ public class ShopManagerImpl implements ShopManager { } shopsInWorld.get(worldName).put(toBlockLocation(location), shop); - Location otherLoc = ((ShopImpl) shop).getOtherLocation(); - if (otherLoc != null) { - shopsInWorld.get(worldName).put(toBlockLocation(otherLoc), shop); - } + ((ShopImpl) shop).getOtherLocation().ifPresent(otherLoc -> + shopsInWorld.get(worldName).put(toBlockLocation(otherLoc), shop)); callback.accept(shop); }, diff --git a/new-implementation/src/main/java/de/epiceric/shopchest/database/Database.java b/new-implementation/src/main/java/de/epiceric/shopchest/database/Database.java index bd2aa80..cfd99ef 100644 --- a/new-implementation/src/main/java/de/epiceric/shopchest/database/Database.java +++ b/new-implementation/src/main/java/de/epiceric/shopchest/database/Database.java @@ -460,7 +460,7 @@ public abstract class Database { 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.setInt(i+3, shop.getProduct().getAmount()); ps.setString(i+4, shop.getLocation().getWorld().getName()); @@ -523,8 +523,8 @@ public abstract class Database { ps.setString(6, product.getLocalizedName()); ps.setString(7, encodeItemStack(product.getItemStack())); ps.setInt(8, product.getAmount()); - ps.setString(9, shop.getVendor().getName()); - ps.setString(10, shop.getVendor().getUniqueId().toString()); + ps.setString(9, shop.getVendor().map(OfflinePlayer::getName).orElse("")); + ps.setString(10, shop.getVendor().map(vendor -> vendor.getUniqueId().toString()).orElse("")); ps.setBoolean(11, shop.isAdminShop()); ps.setString(12, shop.getLocation().getWorld().getName()); ps.setInt(13, shop.getLocation().getBlockX()); diff --git a/new-implementation/src/main/java/de/epiceric/shopchest/listener/ChestInteractListener.java b/new-implementation/src/main/java/de/epiceric/shopchest/listener/ChestInteractListener.java index eb930b5..616378d 100644 --- a/new-implementation/src/main/java/de/epiceric/shopchest/listener/ChestInteractListener.java +++ b/new-implementation/src/main/java/de/epiceric/shopchest/listener/ChestInteractListener.java @@ -22,7 +22,6 @@ import de.epiceric.shopchest.api.event.ShopOpenEvent; import de.epiceric.shopchest.api.event.ShopRemoveEvent; import de.epiceric.shopchest.api.event.ShopBuySellEvent.Type; 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.OpenFlag; import de.epiceric.shopchest.api.flag.RemoveFlag; @@ -57,23 +56,24 @@ public class ChestInteractListener implements Listener { if (shopOpt.isPresent()) { Shop shop = shopOpt.get(); - if (player.hasFlag() && e.getAction() == Action.RIGHT_CLICK_BLOCK) { - Flag flag = player.getFlag(); - if (flag instanceof InfoFlag) { - plugin.getServer().getPluginManager().callEvent(new ShopInfoEvent(player, shop)); - e.setCancelled(true); - } else if (flag instanceof RemoveFlag) { - plugin.getServer().getPluginManager().callEvent(new ShopRemoveEvent(player, shop)); - e.setCancelled(true); - } else if (flag instanceof OpenFlag) { - ShopOpenEvent event = new ShopOpenEvent(player, shop); - plugin.getServer().getPluginManager().callEvent(event); - e.setCancelled(event.isCancelled()); - } else if (flag instanceof CreateFlag) { - e.setCancelled(true); - player.sendMessage("§cThis chest already is a shop."); // TODO: i18n - } - player.removeFlag(); + if (e.getAction() == Action.RIGHT_CLICK_BLOCK) { + player.getFlag().ifPresent(flag -> { + if (flag instanceof InfoFlag) { + plugin.getServer().getPluginManager().callEvent(new ShopInfoEvent(player, shop)); + e.setCancelled(true); + } else if (flag instanceof RemoveFlag) { + plugin.getServer().getPluginManager().callEvent(new ShopRemoveEvent(player, shop)); + e.setCancelled(true); + } else if (flag instanceof OpenFlag) { + ShopOpenEvent event = new ShopOpenEvent(player, shop); + plugin.getServer().getPluginManager().callEvent(event); + e.setCancelled(event.isCancelled()); + } else if (flag instanceof CreateFlag) { + e.setCancelled(true); + player.sendMessage("§cThis chest already is a shop."); // TODO: i18n + } + player.removeFlag(); + }); } else if (e.hasItem() && e.getItem().getType() == Config.CORE_SHOP_INFO_ITEM.get()) { plugin.getServer().getPluginManager().callEvent(new ShopInfoEvent(player, shopOpt.get())); e.setCancelled(true); @@ -100,14 +100,17 @@ public class ChestInteractListener implements Listener { .callEvent(new ShopBuySellEvent(player, shop, type, shop.getProduct().getAmount(), price)); } } else { - if (player.getFlag() instanceof CreateFlag && e.getAction() == Action.RIGHT_CLICK_BLOCK) { - e.setCancelled(true); - CreateFlag flag = (CreateFlag) player.getFlag(); - player.removeFlag(); - OfflinePlayer vendor = flag.isAdminShop() ? null : player.getBukkitPlayer(); - plugin.getServer().getPluginManager().callEvent(new ShopCreateEvent(player, - new ShopImpl(vendor, flag.getProduct(), location, flag.getBuyPrice(), flag.getSellPrice()), - Config.SHOP_CREATION_PRICE.get())); + if (e.getAction() == Action.RIGHT_CLICK_BLOCK) { + player.getFlag().filter(flag -> flag instanceof CreateFlag).ifPresent(f -> { + e.setCancelled(true); + CreateFlag flag = (CreateFlag) f; + player.removeFlag(); + OfflinePlayer vendor = flag.isAdminShop() ? null : player.getBukkitPlayer(); + plugin.getServer().getPluginManager().callEvent(new ShopCreateEvent(player, + new ShopImpl(vendor, flag.getProduct(), location, flag.getBuyPrice(), flag.getSellPrice()), + Config.SHOP_CREATION_PRICE.get())); + }); + } } } diff --git a/new-implementation/src/main/java/de/epiceric/shopchest/listener/CreativeSelectListener.java b/new-implementation/src/main/java/de/epiceric/shopchest/listener/CreativeSelectListener.java index f1b0ce2..23d6f40 100644 --- a/new-implementation/src/main/java/de/epiceric/shopchest/listener/CreativeSelectListener.java +++ b/new-implementation/src/main/java/de/epiceric/shopchest/listener/CreativeSelectListener.java @@ -38,21 +38,20 @@ public class CreativeSelectListener implements Listener { } ShopPlayer player = plugin.wrapPlayer((Player) e.getWhoClicked()); - if (player.getFlag() instanceof SelectFlag) { + player.getFlag().filter(flag -> flag instanceof SelectFlag).ifPresent(f -> { e.setCancelled(true); if (e.getCursor() == null || e.getCursor().getType() == Material.AIR) { return; } - SelectFlag flag = (SelectFlag) player.getFlag(); + SelectFlag flag = (SelectFlag) f; player.removeFlag(); plugin.getServer().getScheduler().runTask(plugin, () -> player.getBukkitPlayer().closeInventory()); plugin.getServer().getPluginManager().callEvent(new ShopSelectItemEvent(player, e.getCursor(), flag.getAmount(), flag.getBuyPrice(), flag.getSellPrice(), flag.isAdminShop())); - - } + }); } @EventHandler @@ -62,7 +61,7 @@ public class CreativeSelectListener implements Listener { } ShopPlayer player = plugin.wrapPlayer((Player) e.getPlayer()); - if (player.getFlag() instanceof SelectFlag) { + if (hasSelectFlag(player)) { player.removeFlag(); player.sendMessage("§cShop creation has been cancelled."); } @@ -82,7 +81,7 @@ public class CreativeSelectListener implements Listener { } ShopPlayer player = plugin.wrapPlayer((Player) e.getWhoClicked()); - if (player.getFlag() instanceof SelectFlag) { + if (hasSelectFlag(player)) { e.setCancelled(true); } } @@ -95,7 +94,7 @@ public class CreativeSelectListener implements Listener { } ShopPlayer player = plugin.wrapPlayer((Player) e.getSource().getHolder()); - if (player.getFlag() instanceof SelectFlag) { + if (hasSelectFlag(player)) { e.setCancelled(true); } } @@ -108,7 +107,7 @@ public class CreativeSelectListener implements Listener { } ShopPlayer player = plugin.wrapPlayer((Player) e.getEntity()); - if (player.getFlag() instanceof SelectFlag) { + if (hasSelectFlag(player)) { e.setCancelled(true); } } @@ -117,7 +116,7 @@ public class CreativeSelectListener implements Listener { public void onBlockBreak(BlockBreakEvent e) { // Cancel any block breaks if SelectFlag is assigned ShopPlayer player = plugin.wrapPlayer(e.getPlayer()); - if (player.getFlag() instanceof SelectFlag) { + if (hasSelectFlag(player)) { e.setCancelled(true); } } @@ -126,7 +125,7 @@ public class CreativeSelectListener implements Listener { public void onBlockPlace(BlockPlaceEvent e) { // Cancel any block places if SelectFlag is assigned ShopPlayer player = plugin.wrapPlayer(e.getPlayer()); - if (player.getFlag() instanceof SelectFlag) { + if (hasSelectFlag(player)) { e.setCancelled(true); } } @@ -135,7 +134,7 @@ public class CreativeSelectListener implements Listener { public void onBlockMultiPlace(BlockMultiPlaceEvent e) { // Cancel any block places if SelectFlag is assigned ShopPlayer player = plugin.wrapPlayer(e.getPlayer()); - if (player.getFlag() instanceof SelectFlag) { + if (hasSelectFlag(player)) { e.setCancelled(true); } } @@ -144,7 +143,7 @@ public class CreativeSelectListener implements Listener { public void onPlayerInteract(PlayerInteractEvent e) { // Cancel any interactions if SelectFlag is assigned ShopPlayer player = plugin.wrapPlayer(e.getPlayer()); - if (player.getFlag() instanceof SelectFlag) { + if (hasSelectFlag(player)) { e.setCancelled(true); } } @@ -153,7 +152,7 @@ public class CreativeSelectListener implements Listener { public void onPlayerInteractAtEntity(PlayerInteractAtEntityEvent e) { // Cancel any entity interactions if SelectFlag is assigned ShopPlayer player = plugin.wrapPlayer(e.getPlayer()); - if (player.getFlag() instanceof SelectFlag) { + if (hasSelectFlag(player)) { e.setCancelled(true); } } @@ -166,7 +165,7 @@ public class CreativeSelectListener implements Listener { } ShopPlayer player = plugin.wrapPlayer((Player) e.getDamager()); - if (player.getFlag() instanceof SelectFlag) { + if (hasSelectFlag(player)) { e.setCancelled(true); } } @@ -175,9 +174,13 @@ public class CreativeSelectListener implements Listener { public void onPlayerMove(PlayerMoveEvent e) { // Cancel any player movement if SelectFlag is assigned ShopPlayer player = plugin.wrapPlayer(e.getPlayer()); - if (player.getFlag() instanceof SelectFlag) { + if (hasSelectFlag(player)) { e.setCancelled(true); } } + private boolean hasSelectFlag(ShopPlayer player) { + return player.getFlag().filter(flag -> flag instanceof SelectFlag).isPresent(); + } + } \ No newline at end of file diff --git a/new-implementation/src/main/java/de/epiceric/shopchest/listener/internal/ShopInteractListener.java b/new-implementation/src/main/java/de/epiceric/shopchest/listener/internal/ShopInteractListener.java index 5a2147e..b3d9868 100644 --- a/new-implementation/src/main/java/de/epiceric/shopchest/listener/internal/ShopInteractListener.java +++ b/new-implementation/src/main/java/de/epiceric/shopchest/listener/internal/ShopInteractListener.java @@ -1,6 +1,9 @@ package de.epiceric.shopchest.listener.internal; +import java.util.Optional; + import org.bukkit.Material; +import org.bukkit.OfflinePlayer; import org.bukkit.block.BlockFace; import org.bukkit.block.data.type.Chest; import org.bukkit.entity.Player; @@ -132,7 +135,7 @@ public class ShopInteractListener implements Listener { e.setCancelled(true); 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."); e.setCancelled(true); return; @@ -157,13 +160,15 @@ public class ShopInteractListener implements Listener { // for this event to be fired. } - boolean vendorMessages = Config.FEATURES_VENDOR_MESSAGES.get(); - ShopPlayer vendor = shop.getVendor().isOnline() ? plugin.wrapPlayer(shop.getVendor().getPlayer()) : null; + boolean vendorMessages = Config.FEATURES_VENDOR_MESSAGES.get(); + Optional vendor = shop.getVendor().filter(OfflinePlayer::isOnline) + .map(offlinePlayer -> plugin.wrapPlayer(offlinePlayer.getPlayer())); + if (e.getType() == Type.BUY) { if (shopAmount < e.getAmount()) { player.sendMessage("§cThis shop is out of items to sell."); - if (vendorMessages && vendor != null) { - vendor.sendMessage("§cYour shop selling §e{0} x {1} §cis out of stock.", product.getAmount(), + if (vendorMessages && vendor.isPresent()) { + vendor.get().sendMessage("§cYour shop selling §e{0} x {1} §cis out of stock.", product.getAmount(), product.getLocalizedName()); } e.setCancelled(true); @@ -172,8 +177,8 @@ public class ShopInteractListener implements Listener { } else if (e.getType() == Type.SELL) { if (shopSpace < e.getAmount()) { player.sendMessage("§cThis shop doesn't have enough space for your items."); - if (vendorMessages && vendor != null) { - vendor.sendMessage("§cYour shop buying §e{0} x {1} §cis full.", product.getAmount(), + if (vendorMessages && vendor.isPresent()) { + vendor.get().sendMessage("§cYour shop buying §e{0} x {1} §cis full.", product.getAmount(), product.getLocalizedName()); } e.setCancelled(true); diff --git a/new-implementation/src/main/java/de/epiceric/shopchest/listener/internal/monitor/ShopCommandMonitorListener.java b/new-implementation/src/main/java/de/epiceric/shopchest/listener/internal/monitor/ShopCommandMonitorListener.java index 5210627..bd276b3 100644 --- a/new-implementation/src/main/java/de/epiceric/shopchest/listener/internal/monitor/ShopCommandMonitorListener.java +++ b/new-implementation/src/main/java/de/epiceric/shopchest/listener/internal/monitor/ShopCommandMonitorListener.java @@ -33,7 +33,7 @@ public class ShopCommandMonitorListener implements Listener { ShopPlayer player = e.getPlayer(); 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 Flag flag = new SelectFlag(e.getAmount(), e.getBuyPrice(), e.getSellPrice(), e.isAdminShop(), player.getBukkitPlayer().getGameMode()); diff --git a/new-implementation/src/main/java/de/epiceric/shopchest/listener/internal/monitor/ShopInteractMonitorListener.java b/new-implementation/src/main/java/de/epiceric/shopchest/listener/internal/monitor/ShopInteractMonitorListener.java index a233869..6429a0c 100644 --- a/new-implementation/src/main/java/de/epiceric/shopchest/listener/internal/monitor/ShopInteractMonitorListener.java +++ b/new-implementation/src/main/java/de/epiceric/shopchest/listener/internal/monitor/ShopInteractMonitorListener.java @@ -5,6 +5,7 @@ import java.text.MessageFormat; import com.google.gson.JsonPrimitive; import org.bukkit.Material; +import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; 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()) ); } 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 error -> e.getPlayer().sendMessage("§cFailed to add shop: {0}", error.getMessage()) ); @@ -104,7 +105,7 @@ public class ShopInteractMonitorListener implements Listener { // TODO: i18n player.sendMessage("§e--------- §fShop Info §e-----------------------------"); 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("[{")) { NmsUtil.sendJsonMessage(player.getBukkitPlayer(), getProductJson(product)); @@ -161,7 +162,6 @@ public class ShopInteractMonitorListener implements Listener { Economy economy = ((ShopChestImpl) plugin).getEconomy(); Player bukkitPlayer = e.getPlayer().getBukkitPlayer(); String worldName = e.getShop().getWorld().getName(); - boolean isAdmin = e.getShop().isAdminShop(); if (e.getType() == Type.BUY) { EconomyResponse r = economy.withdrawPlayer(bukkitPlayer, worldName, e.getPrice()); @@ -171,8 +171,8 @@ public class ShopInteractMonitorListener implements Listener { return; } - if (!isAdmin) { - EconomyResponse rVendor = economy.depositPlayer(e.getShop().getVendor(), worldName, e.getPrice()); + e.getShop().getVendor().ifPresent(vendor -> { + EconomyResponse rVendor = economy.depositPlayer(vendor, worldName, e.getPrice()); if (!rVendor.transactionSuccess()) { e.setCancelled(true); e.getPlayer().sendMessage("§cFailed to deposit money to vendor: {0}", r.errorMessage); // TODO: i18n @@ -183,7 +183,7 @@ public class ShopInteractMonitorListener implements Listener { } return; } - } + }); } else { EconomyResponse r = economy.depositPlayer(bukkitPlayer, worldName, e.getPrice()); if (!r.transactionSuccess()) { @@ -192,8 +192,8 @@ public class ShopInteractMonitorListener implements Listener { return; } - if (!isAdmin) { - EconomyResponse rVendor = economy.withdrawPlayer(e.getShop().getVendor(), worldName, e.getPrice()); + e.getShop().getVendor().ifPresent(vendor -> { + EconomyResponse rVendor = economy.withdrawPlayer(vendor, worldName, e.getPrice()); if (!rVendor.transactionSuccess()) { e.setCancelled(true); e.getPlayer().sendMessage("§cFailed to withdraw money from vendor: {0}", r.errorMessage); // TODO: i18n @@ -204,7 +204,8 @@ public class ShopInteractMonitorListener implements Listener { } 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); } else { 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 { 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); } else { 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) { diff --git a/new-implementation/src/main/java/de/epiceric/shopchest/player/ShopPlayerImpl.java b/new-implementation/src/main/java/de/epiceric/shopchest/player/ShopPlayerImpl.java index 7e0eb1e..24360ee 100644 --- a/new-implementation/src/main/java/de/epiceric/shopchest/player/ShopPlayerImpl.java +++ b/new-implementation/src/main/java/de/epiceric/shopchest/player/ShopPlayerImpl.java @@ -3,6 +3,7 @@ package de.epiceric.shopchest.player; import java.util.Collection; import java.util.HashMap; import java.util.Map; +import java.util.Optional; import java.util.UUID; import org.bukkit.entity.Player; @@ -55,8 +56,8 @@ public class ShopPlayerImpl implements ShopPlayer { } @Override - public Flag getFlag() { - return flag; + public Optional getFlag() { + return Optional.ofNullable(flag); } @Override diff --git a/new-implementation/src/main/java/de/epiceric/shopchest/shop/ShopImpl.java b/new-implementation/src/main/java/de/epiceric/shopchest/shop/ShopImpl.java index 7a37660..3b9a5a4 100644 --- a/new-implementation/src/main/java/de/epiceric/shopchest/shop/ShopImpl.java +++ b/new-implementation/src/main/java/de/epiceric/shopchest/shop/ShopImpl.java @@ -1,5 +1,7 @@ package de.epiceric.shopchest.shop; +import java.util.Optional; + import org.bukkit.Bukkit; import org.bukkit.Location; 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 * - * @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 getOtherLocation() { try { Inventory inv = getInventory(); if (inv instanceof DoubleChestInventory) { @@ -71,15 +73,15 @@ public class ShopImpl implements Shop { location.getBlockX() == left.getBlockX() && location.getBlockY() == left.getBlockY() && location.getBlockZ() == left.getBlockZ()) { - return right; + return Optional.of(right); } else { - return left; + return Optional.of(left); } } } catch (ChestNotFoundException e) { Logger.severe(e.getMessage()); } - return null; + return Optional.empty(); } /** @@ -127,8 +129,8 @@ public class ShopImpl implements Shop { } @Override - public OfflinePlayer getVendor() { - return vendor; + public Optional getVendor() { + return Optional.ofNullable(vendor); } @Override diff --git a/new-implementation/src/main/java/de/epiceric/shopchest/shop/hologram/Hologram.java b/new-implementation/src/main/java/de/epiceric/shopchest/shop/hologram/Hologram.java index 32c4a05..1f52508 100644 --- a/new-implementation/src/main/java/de/epiceric/shopchest/shop/hologram/Hologram.java +++ b/new-implementation/src/main/java/de/epiceric/shopchest/shop/hologram/Hologram.java @@ -4,6 +4,7 @@ import java.util.ArrayList; import java.util.List; import org.bukkit.Location; +import org.bukkit.OfflinePlayer; import org.bukkit.entity.Player; import de.epiceric.shopchest.api.ShopChest; @@ -20,7 +21,7 @@ public class Hologram { int topLine = shop.canPlayerBuy() && shop.canPlayerSell() ? 3 : 2; // 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())); 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()))); @@ -29,12 +30,12 @@ public class Hologram { private Location getLocation(int lineFromBottom) { double lineHeight = 0.25; Location loc = shop.getLocation().subtract(0, 0.75, 0); - Location otherLoc = ((ShopImpl) shop).getOtherLocation(); + Location otherLoc = ((ShopImpl) shop).getOtherLocation().orElse(null); if (otherLoc == null) { return loc.add(0.5, lineFromBottom * lineHeight, 0.5); } - + if (loc.getX() == otherLoc.getX()) { double zDiff = otherLoc.subtract(loc).getZ(); return loc.add(0.5, lineFromBottom * lineHeight, zDiff);