Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions API/src/main/java/fr/maxlego08/shop/api/event/ShopAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@ public class ShopAction {
private final ItemStack itemStack;
private final ItemButton itemButton;
private double price;
private int totalAmount;

public ShopAction(ItemStack itemStack, ItemButton itemButton, double price) {
this.itemStack = itemStack;
this.itemButton = itemButton;
this.price = price;
this.totalAmount = itemStack.getAmount();
}

public ItemStack getItemStack() {
Expand All @@ -30,4 +32,16 @@ public double getPrice() {
public void setPrice(double price) {
this.price = price;
}

public int getTotalAmount() {
return totalAmount;
}

public void setTotalAmount(int totalAmount) {
this.totalAmount = totalAmount;
}

public void addAmount(int amount) {
this.totalAmount += amount;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ public interface LimiterManager {

Collection<Limit> getLimits();

Collection<Limit> getLimits(LimitType limitType);

void create(Limit limit);

Optional<Limit> getLimit(LimitType limitType, String material);
Expand Down
50 changes: 37 additions & 13 deletions src/main/java/fr/maxlego08/shop/ZShopManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -392,21 +392,21 @@ public void sellAllContent(Player player, org.bukkit.inventory.Inventory invento
List<Pair<ItemStack, ItemButton>> buttons = this.itemButtons.stream().map(button -> {
ItemStack itemStack = button.getItemStack().build(player, false);
return new Pair<>(itemStack, button);
}).collect(Collectors.toList());
}).toList();

/* SCAN ITEMS */
for (int slot = 0; slot != inventory.getContents().length; slot++) {
ItemStack itemStack = inventory.getContents()[slot];
if (itemStack == null) continue;

Optional<ItemButton> optional = buttons.stream().filter(e -> e.first.isSimilar(itemStack)).map(e -> e.second).findFirst();
if (!optional.isPresent()) continue;
if (optional.isEmpty()) continue;

ItemButton itemButton = optional.get();
if (!itemButton.canSell()) continue;

double price = itemButton.getSellPrice(player, itemStack.getAmount());
ShopAction shopAction = new ShopAction(itemStack, itemButton, price);
ShopAction shopAction = new ShopAction(itemStack.clone(), itemButton, price);
shopActions.add(shopAction);
}

Expand All @@ -424,6 +424,8 @@ public void sellAllContent(Player player, org.bukkit.inventory.Inventory invento
ItemButton button = action.getItemButton();
ItemStack itemStack = action.getItemStack();

int requestedAmount = itemStack.getAmount();
int actualSellAmount = requestedAmount; // Amount that will actually be sold
int newServerLimitAmount = 0;
int newPlayerLimitAmount = 0;
String material = button.getItemStack().getMaterial();
Expand All @@ -435,20 +437,41 @@ public void sellAllContent(Player player, org.bukkit.inventory.Inventory invento
/* SERVER LIMIT */
if (optionalServer.isPresent()) {
Limit serverSellLimit = optionalServer.get();
newServerLimitAmount = serverSellLimit.getAmount() + itemStack.getAmount();
if (newServerLimitAmount > serverSellLimit.getLimit()) return;
int currentServerAmount = serverSellLimit.getAmount();
int maxCanSell = serverSellLimit.getLimit() - currentServerAmount;

if (maxCanSell <= 0) {
action.setTotalAmount(0);
return;
}
actualSellAmount = Math.min(actualSellAmount, maxCanSell);
newServerLimitAmount = currentServerAmount + actualSellAmount;
}
/* END SERVER LIMIT */

/* PLAYER LIMIT */
if (optionalPlayer.isPresent()) {
Limit playerSellLimit = optionalPlayer.get();
Optional<PlayerLimit> optional = limiterManager.getLimit(player);
newPlayerLimitAmount = optional.map(e -> e.getSellAmount(material)).orElse(0) + itemStack.getAmount();
if (newPlayerLimitAmount > playerSellLimit.getLimit()) return;
int currentPlayerAmount = optional.map(e -> e.getSellAmount(material)).orElse(0);
int maxCanSell = playerSellLimit.getLimit() - currentPlayerAmount;

if (maxCanSell <= 0) {
action.setTotalAmount(0);
return;
}
actualSellAmount = Math.min(actualSellAmount, maxCanSell);
newPlayerLimitAmount = currentPlayerAmount + actualSellAmount;
}
/* END PLAYER LIMIT */

if (actualSellAmount != requestedAmount) {
itemStack.setAmount(actualSellAmount);
double actualPrice = button.getSellPrice(player, actualSellAmount);
action.setPrice(actualPrice);
}
action.setTotalAmount(actualSellAmount);

/* UPDATE LIMIT VALUES */
if (optionalServer.isPresent()) optionalServer.get().setAmount(newServerLimitAmount);
if (newPlayerLimitAmount > 0)
Expand All @@ -457,8 +480,7 @@ public void sellAllContent(Player player, org.bukkit.inventory.Inventory invento

/* REMOVE ITEMS AND UPDATE MONEY */
prices.put(button.getEconomy(), prices.getOrDefault(button.getEconomy(), 0.0) + action.getPrice());
// inventory.remove(itemStack);
InventoryUtils.removeItem(inventory, itemStack, itemStack.getAmount());
InventoryUtils.removeItem(inventory, itemStack, actualSellAmount);
});

if (prices.isEmpty()) {
Expand All @@ -469,23 +491,25 @@ public void sellAllContent(Player player, org.bukkit.inventory.Inventory invento
List<ShopAction> fixedShopActions = new ArrayList<>();

shopActions.forEach(action -> {
if (action.getTotalAmount() == 0) return;

Optional<ShopAction> optional = fixedShopActions.stream().filter(e -> e.getItemStack().isSimilar(action.getItemStack())).findFirst();
if (optional.isPresent()) {
ShopAction currentAction = optional.get();
currentAction.setPrice(currentAction.getPrice() + action.getPrice());
currentAction.getItemStack().setAmount(currentAction.getItemStack().getAmount() + action.getItemStack().getAmount());
currentAction.addAmount(action.getTotalAmount());
} else fixedShopActions.add(action);
});

var translationManager = this.plugin.getTranslationManager();

String results = toList(fixedShopActions.stream().map(action -> getMessage(Message.SELL_ALL_INFO, "%amount%", action.getItemStack().getAmount(), "%item%", translationManager.translateItemStack(action.getItemStack()), "%price%", action.getItemButton().getEconomy().format(transformPrice(action.getPrice()), action.getPrice()))).collect(Collectors.toList()), Message.SELL_ALL_COLOR_SEPARATOR.msg(), Message.SELL_ALL_COLOR_INFO.msg());
String results = toList(fixedShopActions.stream().map(action -> getMessage(Message.SELL_ALL_INFO, "%amount%", action.getTotalAmount(), "%item%", translationManager.translateItemStack(action.getItemStack()), "%price%", action.getItemButton().getEconomy().format(transformPrice(action.getPrice()), action.getPrice()))).collect(Collectors.toList()), Message.SELL_ALL_COLOR_SEPARATOR.msg(), Message.SELL_ALL_COLOR_INFO.msg());
if (results == null) {
Logger.info("Error with results on sellall !");
player.sendMessage("§cError with results on sellall !");
}

String resultsReason = toList(fixedShopActions.stream().map(action -> getMessage(Config.depositAllLine, "%amount%", action.getItemStack().getAmount(), "%item%", translationManager.translateItemStack(action.getItemStack()), "%price%", action.getItemButton().getEconomy().format(transformPrice(action.getPrice()), action.getPrice()))).collect(Collectors.toList()), "", "");
String resultsReason = toList(fixedShopActions.stream().map(action -> getMessage(Config.depositAllLine, "%amount%", action.getTotalAmount(), "%item%", translationManager.translateItemStack(action.getItemStack()), "%price%", action.getItemButton().getEconomy().format(transformPrice(action.getPrice()), action.getPrice()))).collect(Collectors.toList()), "", "");
prices.forEach((economy, price) -> economy.depositMoney(player, price, Config.depositAllReason.replace("%items%", resultsReason == null ? "" : resultsReason)));

message(this.plugin, player, Message.SELL_ALL_MESSAGE, "%items%", results == null ? "ERROR" : results);
Expand All @@ -510,7 +534,7 @@ public void sellAllContent(Player player, org.bukkit.inventory.Inventory invento
public void sellHand(Player player, int amount) {

ItemStack itemInHand = player.getItemInHand(); // Use old method for 1.8 support
if (itemInHand == null || itemInHand.getType().equals(Material.AIR)) {
if (itemInHand.getType().equals(Material.AIR)) {
message(this.plugin, player, Message.SELL_HAND_AIR);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public CommandShop(ShopPlugin plugin) {
this.addSubCommand(new CommandShopReload(plugin));
this.addSubCommand(new CommandShopLogs(plugin));
this.addSubCommand(new CommandShopConvert(plugin));
this.addSubCommand(new CommandShopResetLimit(plugin));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package fr.maxlego08.shop.command.commands;

import fr.maxlego08.shop.ShopPlugin;
import fr.maxlego08.shop.api.limit.Limit;
import fr.maxlego08.shop.api.limit.LimitType;
import fr.maxlego08.shop.api.limit.LimiterManager;
import fr.maxlego08.shop.command.VCommand;
import fr.maxlego08.shop.zcore.enums.Message;
import fr.maxlego08.shop.zcore.enums.Permission;
import fr.maxlego08.shop.zcore.utils.commands.CommandType;

import java.util.Optional;

public class CommandShopResetLimit extends VCommand {

public CommandShopResetLimit(ShopPlugin plugin) {
super(plugin);
this.setPermission(Permission.ZSHOP_RESET_LIMIT);
this.addSubCommand("resetlimit");
this.setDescription(Message.DESCRIPTION_RESET_LIMIT);
this.addSubCommand(new CommandShopResetLimitAll(plugin));
this.addSubCommand(new CommandShopResetLimitPlayer(plugin));
this.addSubCommand(new CommandShopResetLimitServer(plugin));
}

@Override
protected CommandType perform(ShopPlugin plugin) {
syntaxMessage();
return CommandType.SUCCESS;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package fr.maxlego08.shop.command.commands;

import fr.maxlego08.shop.ShopPlugin;
import fr.maxlego08.shop.api.limit.Limit;
import fr.maxlego08.shop.api.limit.LimiterManager;
import fr.maxlego08.shop.command.VCommand;
import fr.maxlego08.shop.zcore.enums.Message;
import fr.maxlego08.shop.zcore.enums.Permission;
import fr.maxlego08.shop.zcore.utils.commands.CommandType;

public class CommandShopResetLimitAll extends VCommand {
public CommandShopResetLimitAll(ShopPlugin plugin) {
super(plugin);
this.setPermission(Permission.ZSHOP_RESET_LIMIT_ALL);
this.addSubCommand("all");
this.setDescription(Message.DESCRIPTION_RESET_LIMIT_ALL);
}

@Override
protected CommandType perform(ShopPlugin plugin) {
LimiterManager limiterManager = plugin.getLimiterManager();
for (Limit limit : limiterManager.getLimits()){
limiterManager.reset(limit);
}
message(plugin, sender, Message.RESET_LIMIT_ALL_SUCCESS);
return CommandType.SUCCESS;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package fr.maxlego08.shop.command.commands;

import fr.maxlego08.shop.ShopPlugin;
import fr.maxlego08.shop.api.limit.Limit;
import fr.maxlego08.shop.api.limit.LimitType;
import fr.maxlego08.shop.api.limit.LimiterManager;
import fr.maxlego08.shop.command.VCommand;
import fr.maxlego08.shop.zcore.enums.Message;
import fr.maxlego08.shop.zcore.enums.Permission;
import fr.maxlego08.shop.zcore.utils.commands.CommandType;

import java.util.Collection;
import java.util.Optional;

public class CommandShopResetLimitPlayer extends VCommand {
public CommandShopResetLimitPlayer(ShopPlugin plugin) {
super(plugin);
this.setPermission(Permission.ZSHOP_RESET_LIMIT_PLAYER);
this.addSubCommand("resetlimitplayer", "resetplayerlimit", "rpl");
this.setDescription(Message.DESCRIPTION_RESET_LIMIT_PLAYER);
this.addOptionalArg("item");
}

@Override
protected CommandType perform(ShopPlugin plugin) {
LimiterManager limiterManager = plugin.getLimiterManager();
String materialName = this.argAsString(0);
if (materialName == null) {
Collection<Limit> limitsBuy = limiterManager.getLimits(LimitType.PLAYER_BUY);
Collection<Limit> limitsSell = limiterManager.getLimits(LimitType.PLAYER_SELL);
for (Limit limit : limitsBuy) {
limiterManager.reset(limit);
}
for (Limit limit : limitsSell) {
limiterManager.reset(limit);
}
message(plugin, sender, Message.RESET_LIMIT_PLAYERS_SUCCESS);

} else {
Optional<Limit> limitsBuy = limiterManager.getLimit(LimitType.PLAYER_BUY, materialName);
Optional<Limit> limitsSell = limiterManager.getLimit(LimitType.PLAYER_SELL, materialName);
if (limitsSell.isPresent()){
limiterManager.reset(limitsSell.get());
message(plugin, sender, Message.RESET_LIMIT_PLAYER_ITEM_SUCCESS, "%item%", materialName, "%type%", "sell");
}
if (limitsBuy.isPresent()){
limiterManager.reset(limitsBuy.get());
message(plugin, sender, Message.RESET_LIMIT_PLAYER_ITEM_SUCCESS, "%item%", materialName, "%type%", "buy");
}
if (limitsBuy.isEmpty() && limitsSell.isEmpty()) {
message(plugin, sender, Message.RESET_LIMIT_NOT_FOUND, "%item%", materialName);
}
}
return CommandType.SUCCESS;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package fr.maxlego08.shop.command.commands;

import fr.maxlego08.shop.ShopPlugin;
import fr.maxlego08.shop.api.limit.Limit;
import fr.maxlego08.shop.api.limit.LimitType;
import fr.maxlego08.shop.api.limit.LimiterManager;
import fr.maxlego08.shop.command.VCommand;
import fr.maxlego08.shop.zcore.enums.Message;
import fr.maxlego08.shop.zcore.enums.Permission;
import fr.maxlego08.shop.zcore.utils.commands.CommandType;

import java.util.Collection;
import java.util.Optional;

public class CommandShopResetLimitServer extends VCommand {
public CommandShopResetLimitServer(ShopPlugin plugin) {
super(plugin);
this.setPermission(Permission.ZSHOP_RESET_LIMIT_SERVER);
this.addSubCommand("resetlimitserver", "resetserverlimit", "rsl");
this.setDescription(Message.DESCRIPTION_RESET_LIMIT_SERVER);
this.addOptionalArg("item");
}

@Override
protected CommandType perform(ShopPlugin plugin) {
LimiterManager limiterManager = plugin.getLimiterManager();
String materialName = this.argAsString(0);
if (materialName == null) {
Collection<Limit> limitsBuy = limiterManager.getLimits(LimitType.SERVER_BUY);
Collection<Limit> limitsSell = limiterManager.getLimits(LimitType.SERVER_SELL);
for (Limit limit : limitsBuy) {
limiterManager.reset(limit);
}
for (Limit limit : limitsSell) {
limiterManager.reset(limit);
}
message(plugin, sender, Message.RESET_LIMIT_SERVERS_SUCCESS);

} else {
Optional<Limit> limitsBuy = limiterManager.getLimit(LimitType.SERVER_BUY, materialName);
Optional<Limit> limitsSell = limiterManager.getLimit(LimitType.SERVER_SELL, materialName);
if (limitsSell.isPresent()){
limiterManager.reset(limitsSell.get());
message(plugin, sender, Message.RESET_LIMIT_SERVER_ITEM_SUCCESS, "%item%", materialName, "%type%", "sell");
}
if (limitsBuy.isPresent()){
limiterManager.reset(limitsBuy.get());
message(plugin, sender, Message.RESET_LIMIT_SERVER_ITEM_SUCCESS, "%item%", materialName, "%type%", "buy");
}
if (limitsBuy.isEmpty() && limitsSell.isEmpty()) {
message(plugin, sender, Message.RESET_LIMIT_NOT_FOUND, "%item%", materialName);
}
}
return CommandType.SUCCESS;
}
}
22 changes: 13 additions & 9 deletions src/main/java/fr/maxlego08/shop/limit/ZLimitManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,7 @@
import java.nio.file.Path;
import java.nio.file.Paths;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.UUID;
import java.util.*;
import java.util.stream.Stream;

public class ZLimitManager extends ZUtils implements LimiterManager, Saveable, Listener {
Expand Down Expand Up @@ -131,7 +124,18 @@ public void onQuit(PlayerQuitEvent event) {

@Override
public Collection<Limit> getLimits() {
return null;
return Collections.unmodifiableList(limits);
}

@Override
public Collection<Limit> getLimits(LimitType limitType) {
List<Limit> filteredLimits = new ArrayList<>();
for (Limit limit : limits) {
if (limit.getType() == limitType) {
filteredLimits.add(limit);
}
}
return Collections.unmodifiableList(filteredLimits);
}

@Override
Expand Down
Loading