mirror of
https://github.com/AppleDash/SaneEconomy.git
synced 2024-11-23 02:25:26 +01:00
Make signshop storage work, maybe.
This commit is contained in:
parent
290d98d26a
commit
7dc66d143e
@ -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() {
|
||||
|
@ -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!");
|
||||
});
|
||||
}
|
||||
}
|
@ -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<SignShop> 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)));
|
||||
}
|
||||
}
|
||||
|
@ -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?
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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<Location, SignShop> signShops = new HashMap<Location, SignShop>();
|
||||
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<SignShop> getSignShop(Location location) {
|
||||
return Optional.ofNullable(signShops.get(location));
|
||||
return Optional.ofNullable(storage.getSignShops().get(location));
|
||||
}
|
||||
}
|
||||
|
@ -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<Location, SignShop> getSignShops();
|
||||
}
|
@ -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<Location, SignShop> 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<Location, SignShop> getSignShops() {
|
||||
return ImmutableMap.copyOf(cachedSignShops);
|
||||
}
|
||||
|
||||
private void readSignShops() {
|
||||
cachedSignShops = new ConcurrentHashMap<>();
|
||||
|
||||
if (!file.exists()) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
ObjectInput ois = new ObjectInputStream(new FileInputStream(file));
|
||||
Map<SerializableLocation, SignShop> tempMap = ((Map<SerializableLocation, SignShop>) 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<SerializableLocation, SignShop> 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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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);
|
||||
}
|
||||
}
|
@ -1 +1,2 @@
|
||||
admin-shop-trigger: '[Shop]'
|
||||
admin-shop-title: '&8[&6Shop&8]&r'
|
||||
|
Loading…
Reference in New Issue
Block a user