From 8627f07163e02318da757a8d515390d1d6f6f7c0 Mon Sep 17 00:00:00 2001 From: AppleDash Date: Sun, 15 Jan 2017 00:47:49 -0500 Subject: [PATCH] Sign shop: Random stuff! --- .../SaneEconomySignShop.java | 3 +++ .../listeners/SignChangeListener.java | 2 +- .../util/DefaultHashMap.java | 16 ++++++++------ .../util/LimitManager.java | 21 +++++++++++++++++++ 4 files changed, 35 insertions(+), 7 deletions(-) diff --git a/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/SaneEconomySignShop.java b/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/SaneEconomySignShop.java index 04d0d4c..c873696 100644 --- a/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/SaneEconomySignShop.java +++ b/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/SaneEconomySignShop.java @@ -40,6 +40,9 @@ public class SaneEconomySignShop extends JavaPlugin { saveDefaultConfig(); signShopManager.loadSignShops(); + + getServer().getScheduler().scheduleSyncRepeatingTask(this, limitManager::incrementLimitsHourly, 0, 20 * 60 * 60); + getServer().getPluginManager().registerEvents(new SignChangeListener(this), this); getServer().getPluginManager().registerEvents(new InteractListener(this), this); getServer().getPluginManager().registerEvents(new BreakListener(this), this); diff --git a/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/listeners/SignChangeListener.java b/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/listeners/SignChangeListener.java index 13eab1a..5fddac6 100644 --- a/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/listeners/SignChangeListener.java +++ b/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/listeners/SignChangeListener.java @@ -53,7 +53,7 @@ public class SignChangeListener implements Listener { MessageUtils.sendMessage(evt.getPlayer(), "Item: {1} x {2}", signShop.getQuantity(), signShop.getItemStack()); if (signShop.canBuy()) { // The player be buying from the shop, not the other way around. - MessageUtils.sendMessage(evt.getPlayer(), "Will sell to players for {!}.", + MessageUtils.sendMessage(evt.getPlayer(), "Will sell to players for {1}.", plugin.getSaneEconomy().getEconomyManager().getCurrency().formatAmount(signShop.getBuyPrice()) ); } diff --git a/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/util/DefaultHashMap.java b/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/util/DefaultHashMap.java index 1ca08d8..a792331 100644 --- a/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/util/DefaultHashMap.java +++ b/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/util/DefaultHashMap.java @@ -8,14 +8,14 @@ import java.util.function.Supplier; * Blackjack is still best pony. */ public class DefaultHashMap extends HashMap { - private final Supplier defaultSupplier; + private final KeyBasedSupplier defaultSupplier; public DefaultHashMap(Supplier defaultSupplier) { - if (defaultSupplier == null) { - throw new NullPointerException("defaultSupplier is null"); - } + this((k) -> defaultSupplier.get()); + } - this.defaultSupplier = defaultSupplier; + public DefaultHashMap(KeyBasedSupplier supplier) { + this.defaultSupplier = supplier; } @Override @@ -23,10 +23,14 @@ public class DefaultHashMap extends HashMap { V v = super.get(k); if (v == null) { - v = defaultSupplier.get(); + v = defaultSupplier.get((K)k); this.put((K) k, v); } return v; } + + public interface KeyBasedSupplier { + V get(K k); + } } diff --git a/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/util/LimitManager.java b/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/util/LimitManager.java index c8c517d..701ef05 100644 --- a/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/util/LimitManager.java +++ b/SaneEconomySignShop/src/main/java/org/appledash/saneeconomysignshop/util/LimitManager.java @@ -2,10 +2,13 @@ package org.appledash.saneeconomysignshop.util; import org.appledash.saneeconomysignshop.signshop.ShopTransaction; import org.appledash.saneeconomysignshop.signshop.ShopTransaction.TransactionDirection; +import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; import java.util.HashMap; import java.util.Map; +import java.util.Optional; import java.util.UUID; /** @@ -58,4 +61,22 @@ public class LimitManager { }); } + public void loadLimits(ConfigurationSection config) { + for (Map map : config.getMapList("")) { + String itemName = String.valueOf(map.get("item")); + int sellLimit = Integer.valueOf(String.valueOf(map.get("buyLimit"))); + int hourlyGain = Integer.valueOf(String.valueOf(map.get("gain"))); + + Optional> pair = ItemDatabase.getIDAndDamageForName(itemName); + + if (!pair.isPresent()) { + continue; + } + + ItemInfo itemInfo = new ItemInfo(new ItemStack(pair.get().getLeft(), pair.get().getRight())); + + itemLimits.get(TransactionDirection.SELL).put(itemInfo, new ItemLimits(sellLimit, hourlyGain)); + } + } + }