Fix SignShop limits

This commit is contained in:
AppleDash 2017-01-17 20:32:47 -05:00
parent 96ee933d19
commit 2058b9bd64
6 changed files with 41 additions and 24 deletions

View File

@ -8,6 +8,7 @@ import org.appledash.saneeconomysignshop.signshop.SignShopManager;
import org.appledash.saneeconomysignshop.signshop.storage.SignShopStorageFlatfile;
import org.appledash.saneeconomysignshop.util.ItemDatabase;
import org.appledash.saneeconomysignshop.util.LimitManager;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
@ -39,6 +40,7 @@ public class SaneEconomySignShop extends JavaPlugin {
saveDefaultConfig();
limitManager.loadLimits(YamlConfiguration.loadConfiguration(getClass().getResourceAsStream("/limits.yml"))); // Always load from JAR
signShopManager.loadSignShops();
getServer().getScheduler().scheduleSyncRepeatingTask(this, limitManager::incrementLimitsHourly, 0, 20 * 60 * 60);
@ -46,6 +48,7 @@ public class SaneEconomySignShop extends JavaPlugin {
getServer().getPluginManager().registerEvents(new SignChangeListener(this), this);
getServer().getPluginManager().registerEvents(new InteractListener(this), this);
getServer().getPluginManager().registerEvents(new BreakListener(this), this);
}
public SignShopManager getSignShopManager() {

View File

@ -86,12 +86,14 @@ public class InteractListener implements Listener {
ShopTransaction shopTransaction = shop.makeTransaction(player, ShopTransaction.TransactionDirection.BUY, quantity);
/* No buy limits for now!
if (!plugin.getLimitManager().shouldAllowTransaction(shopTransaction)) {
MessageUtils.sendMessage(player, "You have reached your buying limit for the time being. Try back in an hour or so.");
return;
}
plugin.getLimitManager().setRemainingLimit(player, ShopTransaction.TransactionDirection.BUY, shop.getItem(), plugin.getLimitManager().getRemainingLimit(player, ShopTransaction.TransactionDirection.BUY, shop.getItem()) - quantity);
*/
Transaction ecoTransaction = shopTransaction.makeEconomyTransaction();
TransactionResult result = ecoMan.transact(ecoTransaction);
@ -126,7 +128,7 @@ public class InteractListener implements Listener {
return;
}
plugin.getLimitManager().setRemainingLimit(player, ShopTransaction.TransactionDirection.SELL, shop.getItem(), plugin.getLimitManager().getRemainingLimit(player, ShopTransaction.TransactionDirection.BUY, shop.getItem()) - quantity);
plugin.getLimitManager().setRemainingLimit(player, ShopTransaction.TransactionDirection.SELL, shop.getItem(), plugin.getLimitManager().getRemainingLimit(player, ShopTransaction.TransactionDirection.SELL, shop.getItem()) - quantity);
ItemStack stack = shop.getItemStack().clone();

View File

@ -28,13 +28,13 @@ public class SignShopStorageFlatfile implements SignShopStorage {
}
@Override
public void putSignShop(SignShop signShop) {
public synchronized void putSignShop(SignShop signShop) {
cachedSignShops.put(signShop.getLocation(), signShop);
writeSignShops();
}
@Override
public void removeSignShop(SignShop signShop) {
public synchronized void removeSignShop(SignShop signShop) {
cachedSignShops.remove(signShop.getLocation());
writeSignShops();
}

View File

@ -60,7 +60,7 @@ public class ItemDatabase {
}
public static <K, V> Pair of(K k, V v) {
return new Pair(k, v);
return new Pair<>(k, v);
}
}
}

View File

@ -16,29 +16,43 @@ import java.util.UUID;
* Blackjack is still best pony.
*/
public class LimitManager {
private final Map<TransactionDirection, Map<ItemInfo, ItemLimits>> itemLimits = new DefaultHashMap<>(() -> new DefaultHashMap<>(() -> ItemLimits.DEFAULT));
// This is a slightly complex data structure. It works like this:
// It's a map of (limit types to (maps of players to (maps of materials to the remaning limit))).
// All the TransactionDirections defaults to an empty map, which defaults to an empty map, which defaults to 0.
// private final Map<ItemInfo, ItemLimits> buyItemLimits = new DefaultHashMap<ItemInfo, ItemLimits>(() -> ItemLimits.DEFAULT);
private final Map<ItemInfo, ItemLimits> sellItemLimits = new DefaultHashMap<>(() -> ItemLimits.DEFAULT);
@SuppressWarnings("MismatchedQueryAndUpdateOfCollection")
private final Map<TransactionDirection, Map<UUID, Map<ItemInfo, Integer>>> playerLimits = new DefaultHashMap<>(() -> new DefaultHashMap<>(() -> new DefaultHashMap<>(() -> 0)));
private final Map<UUID, Map<ItemInfo, Integer>> sellPlayerLimits = new DefaultHashMap<>(() -> new DefaultHashMap<>((info) -> sellItemLimits.get(info).getLimit()));
// private final Map<TransactionDirection, Map<ItemInfo, ItemLimits>> itemLimits = new DefaultHashMap<>(() -> new DefaultHashMap<>(() -> ItemLimits.DEFAULT));
// This is a slightly complex data structure. It works like this:
// It's a map of (limit types to (maps of players to (maps of materials to the remaining limit))).
// All the TransactionDirections defaults to an empty map, which defaults to an empty map, which defaults to 0.
// @SuppressWarnings("MismatchedQueryAndUpdateOfCollection")
// private final Map<TransactionDirection, Map<UUID, Map<ItemInfo, Integer>>> playerLimits = new DefaultHashMap<>(() -> new DefaultHashMap<>(() -> new DefaultHashMap<>(() -> 0)));
public int getRemainingLimit(Player player, TransactionDirection type, ItemInfo stack) {
return playerLimits.get(type).get(player.getUniqueId()).get(stack);
if (type == TransactionDirection.SELL) {
return sellPlayerLimits.get(player.getUniqueId()).get(stack);
}
throw new IllegalArgumentException("Don't know how to get limits for that TransactionDirection!");
}
public void setRemainingLimit(Player player, TransactionDirection type, ItemInfo stack, int limit) {
if (playerLimits.get(type).get(player.getUniqueId()).get(stack) == -1) {
if (type == TransactionDirection.SELL) {
if (sellPlayerLimits.get(player.getUniqueId()).get(stack) == -1) {
return;
}
limit = Math.min(limit, sellItemLimits.get(stack).getLimit());
limit = Math.max(0, limit);
sellPlayerLimits.get(player.getUniqueId()).put(stack, limit);
return;
}
limit = Math.min(limit, itemLimits.get(type).get(stack).getLimit());
limit = Math.max(0, limit);
playerLimits.get(type).get(player.getUniqueId()).put(stack, limit);
throw new IllegalArgumentException("Don't know how to set limits for that TransactionDorection!");
}
public boolean shouldAllowTransaction(ShopTransaction transaction) {
// System.out.printf("Limit: %d, quantity: %d\n", limit, transaction.getQuantity());
return getRemainingLimit(transaction.getPlayer(), transaction.getDirection(), transaction.getItem()) >= transaction.getQuantity();
}
@ -48,16 +62,14 @@ public class LimitManager {
// For every limit
// Increment limit by the limit for the specific direction and item.
playerLimits.forEach((transactionDirection, playerToLimit) -> {
playerToLimit.forEach((playerUuid, itemToLimit) -> {
Map<ItemInfo, Integer> newLimits = new HashMap<>();
sellPlayerLimits.forEach((playerUuid, itemToLimit) -> {
Map<ItemInfo, Integer> newLimits = new HashMap<>();
itemToLimit.forEach((itemInfo, currentLimit) -> {
newLimits.put(itemInfo, currentLimit + (itemLimits.get(transactionDirection).get(itemInfo).getHourlyGain()));
});
itemToLimit.putAll(newLimits);
itemToLimit.forEach((itemInfo, currentLimit) -> {
newLimits.put(itemInfo, currentLimit + (sellItemLimits.get(itemInfo).getHourlyGain()));
});
itemToLimit.putAll(newLimits);
});
}
@ -75,7 +87,7 @@ public class LimitManager {
ItemInfo itemInfo = new ItemInfo(new ItemStack(pair.get().getLeft(), pair.get().getRight()));
itemLimits.get(TransactionDirection.SELL).put(itemInfo, new ItemLimits(sellLimit, hourlyGain));
sellItemLimits.put(itemInfo, new ItemLimits(sellLimit, hourlyGain));
}
}