diff --git a/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/SaneEconomySignShop.java b/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/SaneEconomySignShop.java index 51c7b91..45481cc 100644 --- a/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/SaneEconomySignShop.java +++ b/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/SaneEconomySignShop.java @@ -4,16 +4,19 @@ import org.appledash.saneeconomy.ISaneEconomy; import org.appledash.saneeconomysignshop.listeners.InteractListener; import org.appledash.saneeconomysignshop.listeners.SignChangeListener; import org.appledash.saneeconomysignshop.signshop.SignShopManager; +import org.appledash.saneeconomysignshop.signshop.storage.SignShopStorageFlatfile; import org.bukkit.plugin.Plugin; import org.bukkit.plugin.java.JavaPlugin; +import java.io.File; + /** * Created by appledash on 10/2/16. * Blackjack is still best pony. */ public class SaneEconomySignShop extends JavaPlugin { private ISaneEconomy saneEconomy; - private final SignShopManager signShopManager = new SignShopManager(); + private final SignShopManager signShopManager = new SignShopManager(new SignShopStorageFlatfile(new File(getDataFolder(), "shops.db"))); @Override public void onEnable() { @@ -33,6 +36,7 @@ public class SaneEconomySignShop extends JavaPlugin { signShopManager.loadSignShops(); getServer().getPluginManager().registerEvents(new SignChangeListener(this), this); getServer().getPluginManager().registerEvents(new InteractListener(this), this); + getServer().getPluginManager().registerEvents(new SignChangeListener(this), this); } public SignShopManager getSignShopManager() { diff --git a/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/listeners/BreakListener.java b/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/listeners/BreakListener.java new file mode 100644 index 0000000..2bf38a2 --- /dev/null +++ b/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/listeners/BreakListener.java @@ -0,0 +1,33 @@ +package org.appledash.saneeconomysignshop.listeners; + +import org.appledash.saneeconomy.utils.MessageUtils; +import org.appledash.saneeconomysignshop.SaneEconomySignShop; +import org.bukkit.event.EventHandler; +import org.bukkit.event.Listener; +import org.bukkit.event.block.BlockBreakEvent; + +/** + * Created by appledash on 10/16/16. + * Blackjack is best pony. + */ +public class BreakListener implements Listener { + private SaneEconomySignShop plugin; + + public BreakListener(SaneEconomySignShop plugin) { + this.plugin = plugin; + } + + @EventHandler + public void onBlockBreak(BlockBreakEvent evt) { + plugin.getSignShopManager().getSignShop(evt.getBlock().getLocation()).ifPresent((shop) -> { + if (!evt.getPlayer().hasPermission("saneeconomy.signshop.destroy.admin")) { + MessageUtils.sendMessage(evt.getPlayer(), "You may not destroy that!"); + evt.setCancelled(true); + return; + } + + plugin.getSignShopManager().removeSignShop(shop); + MessageUtils.sendMessage(evt.getPlayer(), "Sign shop destroyed!"); + }); + } +} diff --git a/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/listeners/InteractListener.java b/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/listeners/InteractListener.java index 3c2d6bc..ad99c4e 100644 --- a/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/listeners/InteractListener.java +++ b/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/listeners/InteractListener.java @@ -13,6 +13,7 @@ import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.block.Action; import org.bukkit.event.player.PlayerInteractEvent; +import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.ItemStack; import java.util.Optional; @@ -34,6 +35,10 @@ public class InteractListener implements Listener { return; } + if (evt.getHand() != EquipmentSlot.HAND) { + return; + } + Optional shopOptional = plugin.getSignShopManager().getSignShop(evt.getClickedBlock().getLocation()); if (!shopOptional.isPresent()) { @@ -44,6 +49,7 @@ public class InteractListener implements Listener { // Buy if (evt.getAction() == Action.RIGHT_CLICK_BLOCK) { + evt.setCancelled(true); if (!shop.canBuy()) { MessageUtils.sendMessage(evt.getPlayer(), "This shop does not permit buying."); return; @@ -54,6 +60,7 @@ public class InteractListener implements Listener { // Sell if (evt.getAction() == Action.LEFT_CLICK_BLOCK) { + evt.setCancelled(true); if (!shop.canSell()) { MessageUtils.sendMessage(evt.getPlayer(), "This shop does not permit selling."); return; @@ -89,15 +96,13 @@ public class InteractListener implements Listener { int quantity = player.isSneaking() ? 1 : shop.getQuantity(); double price = shop.getSellPrice(quantity); - ItemStack requiredItem = new ItemStack(shop.getItem(), quantity); - - if (!player.getInventory().contains(requiredItem)) { + if (!player.getInventory().containsAtLeast(new ItemStack(shop.getItem()), quantity)) { MessageUtils.sendMessage(player, String.format("You do not have %d %s!", quantity, shop.getItem())); return; } - player.getInventory().remove(requiredItem); + player.getInventory().removeItem(new ItemStack(shop.getItem(), quantity)); // FIXME: This does not remove items with damage values that were detected by contains() ecoMan.transact(new Transaction(Economable.PLUGIN, Economable.wrap(player), price, TransactionReason.PLUGIN_GIVE)); - MessageUtils.sendMessage(player, String.format("You have sold %d %s for %s.", shop.getQuantity(), shop.getItem(), ecoMan.getCurrency().formatAmount(price))); + MessageUtils.sendMessage(player, String.format("You have sold %d %s for %s.", quantity, shop.getItem(), ecoMan.getCurrency().formatAmount(price))); } } diff --git a/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/signshop/SignShop.java b/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/signshop/SignShop.java index 74b61f0..ed8b3b0 100644 --- a/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/signshop/SignShop.java +++ b/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/signshop/SignShop.java @@ -73,7 +73,7 @@ public class SignShop { * @return Price to buy that number of items at this shop */ public double getBuyPrice(int quantity) { - return Math.ceil(this.buyPrice * (this.quantity / quantity)); // TODO: Is this okay? + return Math.ceil(this.buyPrice * (quantity / this.quantity)); // TODO: Is this okay? } /** @@ -83,7 +83,7 @@ public class SignShop { * @return Price to sell that number of items at this shop */ public double getSellPrice(int quantity) { - return Math.floor(this.sellPrice * (this.quantity / quantity)); // TODO: Is this okay? + return Math.floor(this.sellPrice * (quantity / this.quantity)); // TODO: Is this okay? } /** diff --git a/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/signshop/SignShopManager.java b/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/signshop/SignShopManager.java index a85e0a9..d48e5ef 100644 --- a/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/signshop/SignShopManager.java +++ b/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/signshop/SignShopManager.java @@ -1,9 +1,8 @@ package org.appledash.saneeconomysignshop.signshop; +import org.appledash.saneeconomysignshop.signshop.storage.SignShopStorage; import org.bukkit.Location; -import java.util.HashMap; -import java.util.Map; import java.util.Optional; /** @@ -11,17 +10,25 @@ import java.util.Optional; * Blackjack is still best pony. */ public class SignShopManager { - private Map signShops = new HashMap(); + private SignShopStorage storage; + + public SignShopManager(SignShopStorage storage) { + this.storage = storage; + } public void loadSignShops() { - + storage.loadSignShops(); } public void addSignShop(SignShop signShop) { - signShops.put(signShop.getLocation(), signShop); + storage.putSignShop(signShop); + } + + public void removeSignShop(SignShop signShop) { + storage.removeSignShop(signShop); } public Optional getSignShop(Location location) { - return Optional.ofNullable(signShops.get(location)); + return Optional.ofNullable(storage.getSignShops().get(location)); } } diff --git a/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/signshop/storage/SignShopStorage.java b/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/signshop/storage/SignShopStorage.java new file mode 100644 index 0000000..7ab924d --- /dev/null +++ b/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/signshop/storage/SignShopStorage.java @@ -0,0 +1,17 @@ +package org.appledash.saneeconomysignshop.signshop.storage; + +import org.appledash.saneeconomysignshop.signshop.SignShop; +import org.bukkit.Location; + +import java.util.Map; + +/** + * Created by appledash on 10/6/16. + * Blackjack is best pony. + */ +public interface SignShopStorage { + void loadSignShops(); + void putSignShop(SignShop signShop); + void removeSignShop(SignShop signShop); + Map getSignShops(); +} diff --git a/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/signshop/storage/SignShopStorageFlatfile.java b/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/signshop/storage/SignShopStorageFlatfile.java new file mode 100644 index 0000000..276b4f8 --- /dev/null +++ b/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/signshop/storage/SignShopStorageFlatfile.java @@ -0,0 +1,83 @@ +package org.appledash.saneeconomysignshop.signshop.storage; + +import com.google.common.collect.ImmutableMap; +import org.appledash.saneeconomysignshop.signshop.SignShop; +import org.appledash.saneeconomysignshop.util.SerializableLocation; +import org.bukkit.Location; + +import java.io.*; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * Created by appledash on 10/6/16. + * Blackjack is best pony. + */ +public class SignShopStorageFlatfile implements SignShopStorage { + private Map cachedSignShops; + private File file; + + public SignShopStorageFlatfile(File file) { + this.file = file; + } + + @Override + public void loadSignShops() { + readSignShops(); + } + + @Override + public void putSignShop(SignShop signShop) { + cachedSignShops.put(signShop.getLocation(), signShop); + writeSignShops(); + } + + @Override + public void removeSignShop(SignShop signShop) { + cachedSignShops.remove(signShop.getLocation()); + writeSignShops(); + } + + @Override + public Map getSignShops() { + return ImmutableMap.copyOf(cachedSignShops); + } + + private void readSignShops() { + cachedSignShops = new ConcurrentHashMap<>(); + + if (!file.exists()) { + return; + } + + try { + ObjectInput ois = new ObjectInputStream(new FileInputStream(file)); + Map tempMap = ((Map) ois.readObject()); + tempMap.forEach((sLoc, shop) -> { + cachedSignShops.put(sLoc.getBukkitLocation(), shop); + }); + ois.close(); + } catch (Exception e) { + throw new RuntimeException("Failed to load sign shop date!", e); + } + } + + private void writeSignShops() { + if (file.exists()) { + file.delete(); + } + + try { + ObjectOutput oos = new ObjectOutputStream(new FileOutputStream(file)); + Map tempMap = new HashMap<>(); + cachedSignShops.forEach((loc, shop) -> { + tempMap.put(new SerializableLocation(loc), shop); + }); + oos.writeObject(tempMap); + oos.close(); + } catch (Exception e) { + throw new RuntimeException("Failed to save sign shop date!", e); + } + } +} diff --git a/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/util/SerializableLocation.java b/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/util/SerializableLocation.java new file mode 100644 index 0000000..a97f6b1 --- /dev/null +++ b/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/util/SerializableLocation.java @@ -0,0 +1,29 @@ +package org.appledash.saneeconomysignshop.util; + +import org.bukkit.Bukkit; +import org.bukkit.Location; + +import java.util.UUID; + +/** + * Created by appledash on 10/17/16. + * Blackjack is best pony. + */ +public class SerializableLocation { + private double x, y, z; + private float yaw, pitch; + private UUID worldUuid; + + public SerializableLocation(Location location) { + this.x = location.getX(); + this.y = location.getY(); + this.z = location.getZ(); + this.yaw = location.getYaw(); + this.pitch = location.getPitch(); + this.worldUuid = location.getWorld().getUID(); + } + + public Location getBukkitLocation() { + return new Location(Bukkit.getServer().getWorld(worldUuid), x, y, z, yaw, pitch); + } +} diff --git a/SaneEconomySignShop/src/main/resources/config.yml b/SaneEconomySignShop/src/main/resources/config.yml index 173da0d..a78f5e9 100644 --- a/SaneEconomySignShop/src/main/resources/config.yml +++ b/SaneEconomySignShop/src/main/resources/config.yml @@ -1 +1,2 @@ +admin-shop-trigger: '[Shop]' admin-shop-title: '&8[&6Shop&8]&r'