close option and commands on purchase

Dieser Commit ist enthalten in:
Silent 2022-04-22 23:46:44 +02:00
Ursprung aaef1d4056
Commit a90023a26a
5 geänderte Dateien mit 35 neuen und 2 gelöschten Zeilen

Datei anzeigen

@ -6,7 +6,7 @@
<groupId>tsp.headdb</groupId>
<artifactId>HeadDB</artifactId>
<version>4.3.1</version>
<version>4.4.0</version>
<packaging>jar</packaging>
<properties>

Datei anzeigen

@ -23,6 +23,8 @@ import java.util.UUID;
*
* @author TheSilentPro
*/
// TODO: Optional instead of null.
// TODO: Remove stream, use loop.
public final class HeadAPI {
private HeadAPI() {}

Datei anzeigen

@ -33,6 +33,7 @@ import javax.annotation.Nonnull;
*
* @author TheSilentPro
*/
// TODO: Optionals instead of null.
public class HeadDatabase {
private final JavaPlugin plugin;
@ -120,7 +121,8 @@ public class HeadDatabase {
@Nonnull
public List<Head> getHeads(Category category) {
return heads.get(category) != null ? Collections.unmodifiableList(heads.get(category)) : new ArrayList<>();
List<Head> result = heads.get(category);
return result != null ? Collections.unmodifiableList(result) : Collections.emptyList();
}
/**
@ -186,6 +188,7 @@ public class HeadDatabase {
protected List<Head> gather(String url, Category category) throws IOException, ParseException {
long start = System.currentTimeMillis();
List<Head> headList = new ArrayList<>();
// TODO: gson
JSONParser parser = new JSONParser();
JSONArray array = (JSONArray) parser.parse(fetch(url));
for (Object o : array) {

Datei anzeigen

@ -31,6 +31,7 @@ import java.util.stream.Collectors;
* Class for handling the "dirty" work
* such as inventories and economy.
*/
// TODO: Rewrite
public class InventoryUtils {
private InventoryUtils() {}
@ -356,6 +357,10 @@ public class InventoryUtils {
}
public static void purchaseHead(Player player, Head head, int amount, String category, String description) {
if (config.getBoolean("closeOnPurchase", false)) {
player.closeInventory();
}
Utils.sendMessage(player, String.format(localization.getMessage("processPayment"), amount, head.getName()));
processPayment(player, amount, category, description, result -> {
if (Boolean.TRUE.equals(result)) {
@ -365,11 +370,25 @@ public class InventoryUtils {
ItemStack item = head.getMenuItem();
item.setAmount(amount);
player.getInventory().addItem(item);
// Commands can only be ran on main thread
Bukkit.getScheduler().runTask(HeadDB.getInstance(), InventoryUtils::runPurchaseCommands);
}
}
});
}
private static void runPurchaseCommands() {
// Backwards compatability
if (!config.contains("commands.purchase")) {
return;
}
for (String cmd : config.getStringList("commands.purchase")) {
Bukkit.dispatchCommand(Bukkit.getConsoleSender(), cmd);
}
}
private static String replace(String message, int size, String category, String search, Player player) {
return message
.replace("%size%", String.valueOf(size))

Datei anzeigen

@ -8,6 +8,9 @@ localHeads: true
# Permission: headdb.category.<category>
requireCategoryPermission: false
# If enabled, the menu will close after purchasing a head (even if the purchase fails)
closeOnPurchase: false
# Hidden heads from the menu
hidden:
enabled: false
@ -136,6 +139,12 @@ ui:
volume: 1
pitch: 1
# Command Configuration
commands:
# Commands to run ONLY if the purchase is successful.
purchase:
- ""
# If the original fetching fails and this is enabled,
# the plugin will attempt to fetch the heads from an archive.
fallback: true