Skip to content

Commit

Permalink
Bugfixes & Improvements (#116)
Browse files Browse the repository at this point in the history
* Typos

* Added option to toggle web-server

* Added help & adminHelp command

* Fix #115

* Disabled item not found warning on /sell.
* Now item is given back to the player instead of disappearing.

* Update Config.java

* Added EnableCollection setting

* Applied #85 to #115
  • Loading branch information
LcyDev committed Jan 13, 2023
1 parent 875d61f commit 09380c2
Show file tree
Hide file tree
Showing 20 changed files with 95 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,35 @@ public abstract class AutoTuneShopFormat {
private static OutlinePane background;

protected boolean interpret(@NotNull CommandSender sender, @NotNull String[] args) {
Config config = Config.get();
ChestGui gui = new ChestGui(6, "Shop");
gui.setOnGlobalClick(event -> event.setCancelled(true));
getBackground(gui);

if (args.length == 0) {
gui.addPane(loadSectionsPane((Player) sender, gui));
} else if (args.length == 1) {
if (args[0].equalsIgnoreCase("reload")) {
if (args[0].equalsIgnoreCase("help")) {
if (!sender.hasPermission("autotune.help") && !sender.isOp()) {
Format.sendMessage((Player) sender,
"<red>You do not have permission to use help.");
return true;
}
for (String message : config.getHelp()) {
Format.sendMessage((Player) sender, message);
}
return true;
} else if (args[0].equalsIgnoreCase("adminhelp")) {
if (!sender.hasPermission("autotune.admin") && !sender.isOp()) {
Format.sendMessage((Player) sender,
"<red>You do not have permission to see admin help.");
return true;
}
for (String message : config.getAdminHelp()) {
Format.sendMessage((Player) sender, message);
}
return true;
} else if (args[0].equalsIgnoreCase("reload")) {
if (!sender.hasPermission("autotune.admin") && !sender.isOp()) {
Format.sendMessage((Player) sender,
"<red>You do not have permission to reload shops.");
Expand Down Expand Up @@ -114,7 +135,7 @@ protected boolean interpret(@NotNull CommandSender sender, @NotNull String[] arg
return true;
}

Shop shop = ShopUtil.getShop(args[1]);
Shop shop = ShopUtil.getShop(args[1], true);

if (shop == null) {
Format.sendMessage((Player) sender, Config.get().getNotInShop());
Expand Down Expand Up @@ -336,7 +357,7 @@ protected TagResolver getGdpTagResolver() {

protected List<Component> getLore(@NotNull Player player, @NotNull String name,
@NotNull List<String> lore, int amount) {
Shop shop = ShopUtil.getShop(name);
Shop shop = ShopUtil.getShop(name, true);
boolean autosellSetting = false;

if (Config.get().getAutosell().get(player.getUniqueId() + "." + name) != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
@Override
protected void doShop(@NotNull HumanEntity player, @NotNull ChestGui gui,
@NotNull String shopName) {
Shop shop = ShopUtil.getShop(shopName);
Shop shop = ShopUtil.getShop(shopName, true);

if (shop.isEnchantment()) {
Format.sendMessage(player, "<red>You cannot autosell enchanted items at the moment.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command
Player player = (Player) sender;
if (args.length == 0) {
getTotalLoans(player);
Format.sendMessage(player, "<gold>Usage: /loan <amount>/pay");
Format.sendMessage(player, "<gold>Usage: /loan <amount/pay>");
} else if (args[0].equalsIgnoreCase("pay") || args[0].equalsIgnoreCase("payback")) {
Database database = Database.get();
for (Map.Entry<Long, Loan> entry : Database.get().getLoans().entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ private boolean interpret(@NotNull Player player) {
}

String itemName = item.getType().toString().toLowerCase();
Shop shop = ShopUtil.getShop(itemName);
Shop shop = ShopUtil.getShop(itemName, false);

if (shop == null) {
PurchaseUtil.returnItem(player, item);
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ protected void doShop(@NotNull HumanEntity player, @NotNull ChestGui gui,
gui.getPanes().clear();
getBackground(gui);
gui.addPane(getGdpPane((Player) player, gui));
gui.addPane(getBackToShop((Player) player, gui, ShopUtil.getShop(shopName).getSection()));
gui.addPane(getBackToShop((Player) player, gui, ShopUtil.getShop(shopName, true).getSection()));
gui.addPane(getPurchasePane((Player) player, gui, shopName));
gui.update();
}
Expand All @@ -69,7 +69,7 @@ protected static void update(@NotNull Player player) {

private OutlinePane getPurchasePane(@NotNull Player player,
@NotNull ChestGui gui, @NotNull String shopName) {
Shop shop = ShopUtil.getShop(shopName);
Shop shop = ShopUtil.getShop(shopName, true);
int length = shop.isEnchantment() ? ENCHANTMENT_AMOUNTS.length : AMOUNTS.length;
OutlinePane pane = new OutlinePane(1, 2, length, 2, Priority.HIGHEST);
Config config = Config.get();
Expand Down Expand Up @@ -122,7 +122,7 @@ private OutlinePane getPurchasePane(@NotNull Player player,
getBackground(gui);
gui.addPane(getGdpPane(player, gui));
gui.addPane(getBackToShop(player, gui,
ShopUtil.getShop(shopName).getSection()));
ShopUtil.getShop(shopName, true).getSection()));
gui.addPane(getPurchasePane(player, gui, shopName));
gui.update();
});
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/unprotesting/com/github/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ public class Config {
private final Integer minimumPlayers;
private final double interest;
private final double tutorialUpdate;
private final boolean webServer;
private final Integer port;
private final String background;
private final String logLevel;
private final String locale;
private final boolean enableSellLimits;
private final boolean enableCollection;
private final boolean enableLoans;

private final String notInShop;
Expand All @@ -63,6 +65,8 @@ public class Config {
private final List<String> purchaseBuyLore;
private final List<String> purchaseSellLore;
private final List<String> autosellLore;
private final List<String> help;
private final List<String> adminHelp;
private final List<String> tutorial;

private final ConfigurationSection shops;
Expand Down Expand Up @@ -134,6 +138,8 @@ private Config() {
logger.finer("Interest: " + interest);
this.tutorialUpdate = configs[0].getDouble("tutorial-update", 300);
logger.finer("Tutorial update: " + tutorialUpdate);
this.webServer = configs[0].getBoolean("web-server", true);
logger.finer("Web-Server: " + webServer);
this.port = configs[0].getInt("port", 8989);
logger.finer("Port: " + port);
this.background = configs[0].getString("background", "BLACK_STAINED_GLASS_PANE");
Expand All @@ -143,6 +149,8 @@ private Config() {
logger.finer("Locale: " + locale);
this.enableSellLimits = configs[0].getBoolean("enable-sell-limits", false);
logger.finer("Skip Max Limits: " + enableSellLimits);
this.enableCollection = configs[0].getBoolean("enable-collection", true);
logger.finer("Collection Enabled: " + enableCollection);
this.enableLoans = configs[0].getBoolean("enable-loans", false);
logger.finer("Loans Enabled: " + enableLoans);

Expand Down Expand Up @@ -185,6 +193,10 @@ private Config() {
logger.finest("Purchase sell lore: " + Arrays.toString(purchaseSellLore.toArray()));
this.autosellLore = configs[3].getStringList("autosell-lore");
logger.finest("Autosell lore: " + Arrays.toString(autosellLore.toArray()));
this.help = configs[3].getStringList("help");
logger.finest("Help: " + Arrays.toString(help.toArray()));
this.adminHelp = configs[3].getStringList("admin-help");
logger.finest("AdminHelp: " + Arrays.toString(adminHelp.toArray()));
this.tutorial = configs[3].getStringList("tutorial");
logger.finest("Tutorial: " + Arrays.toString(tutorial.toArray()));

Expand Down
3 changes: 2 additions & 1 deletion src/main/java/unprotesting/com/github/config/CsvHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class CsvHandler {
* Write the price data for all items to a CSV file.
*/
public static void writePriceData() {
if (!Config.get().isWebServer()) return;
Bukkit.getScheduler().runTaskAsynchronously(AutoTune.getInstance(), () -> {
AutoTuneLogger logger = Format.getLog();
try {
Expand Down Expand Up @@ -53,7 +54,7 @@ private static void writeCsv() throws IOException {
int size = shopNames.length;
Shop[] shops = new Shop[size];
for (int i = 0; i < size; i++) {
shops[i] = ShopUtil.getShop(shopNames[i]);
shops[i] = ShopUtil.getShop(shopNames[i], true);
}
for (int i = 0; i < size; i++) {
if (i < size - 1) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/unprotesting/com/github/config/TxtHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private static void exportPriceData() throws IOException {
BufferedWriter writer = new BufferedWriter(new java.io.FileWriter(file));
String[] shopNames = ShopUtil.getShopNames();
for (String shopName : shopNames) {
Shop shop = ShopUtil.getShop(shopName);
Shop shop = ShopUtil.getShop(shopName, true);
writer.write(shopName + ": " + shop.getPrice());
writer.newLine();
}
Expand All @@ -68,7 +68,7 @@ private static void parseLine(String line) {
String shopName = split[0];
try {
double price = Double.parseDouble(split[1]);
Shop shop = ShopUtil.getShop(shopName);
Shop shop = ShopUtil.getShop(shopName, true);
shop.setPrice(price);
ShopUtil.putShop(shopName, shop);
} catch (NumberFormatException e) {
Expand Down
14 changes: 7 additions & 7 deletions src/main/java/unprotesting/com/github/data/Database.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ public void reload() {
public void updateChanges() {
AutoTuneLogger logger = Format.getLog();
for (String name : shops.keySet()) {
Shop shop = getShop(name);
Shop shop = getShop(name, true);
shop.updateChange();
putShop(name, shop);
logger.finest(name + "'s change is now "
+ Format.percent(getShop(name).getChange()));
+ Format.percent(getShop(name, true).getChange()));
}
}

Expand All @@ -126,7 +126,7 @@ public void updateRelations() {
}

Pair<String, String> pair = Tuples.pair(name, name2);
Relation relation = new Relation(getShop(name), getShop(name2));
Relation relation = new Relation(getShop(name, true), getShop(name2, true));
relations.put(pair, relation);
}
}
Expand All @@ -146,11 +146,11 @@ public void updateLoan(Long key, Loan loan) {
}
}

protected Shop getShop(String s) {
protected Shop getShop(String s, boolean warn) {
String item = s.toLowerCase();

if (shops.get(item) == null) {
Format.getLog().severe("Could not find shop for " + item);
if (warn) Format.getLog().severe("Could not find shop for " + item);
return null;
}

Expand All @@ -169,7 +169,7 @@ protected String[] getShopNames() {
}

protected int getPurchasesLeft(String item, UUID player, boolean isBuy) {
Shop shop = getShop(item);
Shop shop = getShop(item, true);
int max = isBuy ? shop.getMaxBuys() : shop.getMaxSells();

if (isBuy) {
Expand Down Expand Up @@ -234,7 +234,7 @@ private void loadShopDefaults() {
}

if (shops.containsKey(key)) {
Shop shop = getShop(key);
Shop shop = getShop(key, true);
shop.loadConfiguration(section, sectionName);
shops.put(key, shop);
logger.finer("Shop " + key + " loaded.");
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/unprotesting/com/github/data/PurchaseUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public void sellItemStack(ItemStack item, Player player) {
}

String itemName = item.getType().toString().toLowerCase();
Shop itemShop = ShopUtil.getShop(itemName);
Shop itemShop = ShopUtil.getShop(itemName, true);
if (itemShop == null) {
Format.sendMessage(player, config.getNotInShop(), r);
returnItem(player, item);
Expand Down Expand Up @@ -180,7 +180,7 @@ public void sellItemStack(ItemStack item, Player player) {

}

private void returnItem(Player player, ItemStack item) {
public void returnItem(Player player, ItemStack item) {
HashMap<Integer, ItemStack> failed = player.getInventory().addItem(item);
if (!failed.isEmpty()) {
player.getWorld().dropItem(player.getLocation(), failed.get(0));
Expand Down Expand Up @@ -217,7 +217,7 @@ public TagResolver getTagResolver(Component display, double price,

private Shop getAssociatedShop(Player player, String itemName) {
String name = itemName.toLowerCase();
Shop shop = ShopUtil.getShop(name);
Shop shop = ShopUtil.getShop(name, true);
if (shop == null) {
Format.sendMessage(player, Config.get().getNotInShop());
return null;
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/unprotesting/com/github/data/Section.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected Section(String name, ConfigurationSection section) {
protected Map<String, Shop> loadShops(String sectionName) {
Map<String, Shop> shops = new HashMap<String, Shop>();
for (String shopName : ShopUtil.getShopNames()) {
Shop shop = ShopUtil.getShop(shopName);
Shop shop = ShopUtil.getShop(shopName, true);

if (shop.getSection() == null) {
Format.getLog().warning("Shop " + shopName + " has no section!");
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/unprotesting/com/github/data/Shop.java
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,9 @@ public void addAutosell(UUID uuid, int count) {
* @return Whether the player has unlocked this item.
*/
public boolean isUnlocked(UUID player) {
if (setting.getSetting().equals(CollectFirstSetting.SERVER)) {
if (Config.get().isEnableCollection()) {
return true;
} else if (setting.getSetting().equals(CollectFirstSetting.SERVER)) {
return setting.isFoundInServer();
} else if (setting.getSetting().equals(CollectFirstSetting.PLAYER)) {
return setting.playerFound(player);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/unprotesting/com/github/data/ShopUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ public class ShopUtil {
private static String[] sectionNameCache;
private static String[] shopNameCache;

public Shop getShop(String item) {
return Database.get().getShop(item);
public Shop getShop(String item, boolean warn) {
return Database.get().getShop(item, true);
}

public void putShop(String key, Shop shop) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ private Shop getShop(String shopName) {
return null;
}

Shop shop = ShopUtil.getShop(shopName);
Shop shop = ShopUtil.getShop(shopName, true);

if (shop == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public AutosellProfitEvent(boolean isAsync) {

private void deposit() {
for (String s : ShopUtil.getShopNames()) {
Shop shop = ShopUtil.getShop(s);
Shop shop = ShopUtil.getShop(s, true);
Map<UUID, Integer> autosell = shop.getAutosell();

if (autosell.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public TimePeriodEvent(boolean isAsync) {
private void updatePrices() {
AutoTuneLogger logger = Format.getLog();
for (String s : ShopUtil.getShopNames()) {
Shop shop = ShopUtil.getShop(s);
Shop shop = ShopUtil.getShop(s, true);
double initialPrice = shop.getPrice();
double strength = shop.strength();
double newPrice = initialPrice + initialPrice * strength * shop.getVolatility() * 0.01;
Expand All @@ -65,7 +65,7 @@ private void updatePrices() {

private void resetRecentPurchases() {
for (String s : ShopUtil.getShopNames()) {
Shop shop = ShopUtil.getShop(s);
Shop shop = ShopUtil.getShop(s, true);
shop.clearRecentPurchases();
ShopUtil.putShop(s, shop);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class LocalServer {

public static void initialize() {
instance = new LocalServer();
instance.start();
if (Config.get().isWebServer()) instance.start();
}

/**
Expand Down
10 changes: 8 additions & 2 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,24 @@ minimum-players: 2
# If you wish to have sell & buy limits enable this.
enable-sell-limits: true

# If you wish to have collection on your server, enable this.
enable-collection: false

# If you wish to have loans on your server, enable this.
enable-loans: false

# The interest applied on loans each minute.
# If inflation is to high you can increase this.
# If inflation is to low you can decrease this.
# If inflation is too high you can increase this.
# If inflation is too low you can decrease this.
interest: 0.05

# The rate that the tutorial messages are sent in seconds.
# If they are getting annoying you can increase this value.
tutorial-update: 300

# If you wish to use the web-server, enable this.
web-server: true

# The port of the web-server.
port: 8989

Expand Down
Loading

0 comments on commit 09380c2

Please sign in to comment.