Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

3 into 1 #3

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ public void initializeDefaults() {
)));
addDefault(new ConfigPart("SoundsEnabled", true, Collections.emptyList()));
addDefault(new ConfigPart("NotSellingNotification", true, Collections.emptyList()));
addDefault(new ConfigPart("PaymentFormatting", true, Collections.emptyList()));
addDefault(new ConfigPart("EcoPayRoundupDecimals", 2, Collections.emptyList()));
addDefault(new ConfigPart("SoundsVolume", 5, Collections.emptyList()));
addDefault(ConfigPart.of("Sounds", null, Arrays.asList("You can completely disable all sounds above.", "",
"Please use only the sounds that in here", "https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/Sound.html")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,19 @@
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.joml.RoundingMode;

import java.math.BigDecimal;

public class ShopMenuListener implements Listener {

private double round(double value) {
if (decimals < 0) {decimals = 2;}
BigDecimal bd = new BigDecimal(Double.toString(value));
bd = bd.setScale(decimals, RoundingMode.HALF_UP);
return bd.doubleValue();
}

@EventHandler
public void onShopMenuClick(InventoryClickEvent event) {
Inventory inventory = event.getClickedInventory();
Expand Down Expand Up @@ -49,9 +59,14 @@ public void onShopMenuClick(InventoryClickEvent event) {
economy.withdrawPlayer(player, price);
ItemStack placeItem = sellChestType.toItemStack();
player.getInventory().addItem(placeItem);
String mes = ConfigHandler.getInstance().getMessageLang("ChestBought")
String msg = ConfigHandler.getInstance().getMessageLang("ChestBought")
.replace("%money%", (balance - price) + "");
libs.getMessageHandler().sendMessage(player, mes);
if(LitSellChest.getInstance().getConfig().getBoolean("PaymentFormatting",false)) {
BigDecimal bd = new BigDecimal(Double.toString(balance));
bd = bd.setScale(LitSellChest.getInstance().getConfig().getInt("EcoPayRoundupDecimals",2), RoundingMode.HALF_UP);
msg.replace("%money%",String.valueOf(bd.doubleValue()));
}
libs.getMessageHandler().sendMessage(player, msg);
SoundManager.sendSound(player, "ChestReceive");
}
else {
Expand Down
30 changes: 26 additions & 4 deletions src/main/java/me/waterarchery/litsellchest/models/SellTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,35 @@
import org.bukkit.inventory.ItemStack;
import org.bukkit.scheduler.BukkitRunnable;
import org.bukkit.util.BoundingBox;
import org.joml.RoundingMode;

import java.math.BigDecimal;
import java.util.Collection;

public class SellTask extends BukkitRunnable {

private final int interval;
private final LitLibs libs;
private int decimals;
private final boolean RoundingEnabled;
private final boolean checkForOnline;
private final boolean NotSellingNot;

public SellTask(int interval, LitLibs libs) {
this.interval = interval;
this.libs = libs;
this.RoundingEnabled = LitSellChest.getInstance().getConfig().getBoolean("PaymentFormatting");
this.checkForOnline = LitSellChest.getInstance().getConfig().getBoolean("OnlyWorkOnlinePlayers");
this.NotSellingNot = LitSellChest.getInstance().getConfig().getBoolean("NotSellingNotification");
if(RoundingEnabled) {
this.decimals = LitSellChest.getInstance().getConfig().getInt("EcoPayRoundupDecimals");
}
}

@Override
public void run() {
ChestHandler chestHandler = ChestHandler.getInstance();
boolean checkForOnline = ConfigHandler.getInstance().getConfig().getYml().getBoolean("OnlyWorkOnlinePlayers", true);
//boolean checkForOnline = ConfigHandler.getInstance().getConfig().getYml().getBoolean("OnlyWorkOnlinePlayers", true);

for (SellChest sellChest : chestHandler.getLoadedChests()) {
if (sellChest.isLoaded() && (!checkForOnline || Bukkit.getPlayer(sellChest.getOwner()) != null)) {
Expand All @@ -67,9 +79,16 @@ public void run() {
}
}

private double round(double value) {
if (decimals < 0) {decimals = 2;}
BigDecimal bd = new BigDecimal(Double.toString(value));
bd = bd.setScale(decimals, RoundingMode.HALF_UP);
return bd.doubleValue();
}

public void handleSelling(SellChest sellChest) {
boolean isSellWithLore = LitSellChest.getInstance().getConfig().getBoolean("SellOnlyItemsWithLore");
boolean notifyOnUnsellable = ConfigHandler.getInstance().getConfig().getYml().getBoolean("NotSellingNotification", true);
//boolean isSellWithLore = LitSellChest.getInstance().getConfig().getBoolean("SellOnlyItemsWithLore");
//boolean notifyOnUnsellable = ConfigHandler.getInstance().getConfig().getYml().getBoolean("NotSellingNotification", true);
new BukkitRunnable() {
@Override
public void run() {
Expand Down Expand Up @@ -125,6 +144,9 @@ public void run() {
OfflinePlayer offlinePlayer = Bukkit.getOfflinePlayer(sellChest.getOwner());
double tax = totalPrice * (sellChest.getChestType().getTax() / 100);
totalPrice -= tax;
if(RoundingEnabled) {
totalPrice = round(totalPrice);
}
econ.depositPlayer(offlinePlayer, totalPrice);
if (offlinePlayer.isOnline()) {
Player player = offlinePlayer.getPlayer();
Expand All @@ -136,7 +158,7 @@ public void run() {
messageHandler.sendMessage(player, msg);
}
}
if(hasInvalids && notifyOnUnsellable) {
if(hasInvalids && NotSellingNot) {
String msg = configHandler.getMessageLang("InvalidPriceOrFree");
messageHandler.sendMessage(player,msg);
}
Expand Down