Skip to content

Commit

Permalink
Sound customization & per category permission
Browse files Browse the repository at this point in the history
  • Loading branch information
UnlimitedNinjas committed Jan 11, 2022
1 parent 9adafdc commit 1bd19ef
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>tsp.headdb</groupId>
<artifactId>HeadDB</artifactId>
<version>3.0.2</version>
<version>3.1.0</version>
<packaging>jar</packaging>

<name>HeadDB</name>
Expand Down
32 changes: 29 additions & 3 deletions src/main/java/tsp/headdb/inventory/InventoryUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ public static void openLocalMenu(Player player) {
return;
}
if (e.getClick() == ClickType.RIGHT) {
player.closeInventory();
Utils.sendMessage(player, "&cLocal heads can not be added to favorites!");
}
}));
Expand All @@ -119,6 +118,7 @@ public static void openFavoritesMenu(Player player) {
HeadAPI.removeFavoriteHead(player.getUniqueId(), head.getValue());
openFavoritesMenu(player);
Utils.sendMessage(player, "&7Removed &e" + head.getName() + " &7from favorites.");
Utils.playSound(player, "removeFavorite");
}
}));
}
Expand All @@ -140,8 +140,15 @@ public static PagedPane openSearchDatabase(Player player, String search) {
purchaseHead(player, head, 1, head.getCategory().getName(), head.getName());
}
if (e.getClick() == ClickType.RIGHT) {
if (!player.hasPermission("headdb.favorites")) {
Utils.sendMessage(player, "&cYou do not have permission for favorites!");
Utils.playSound(player, "noPermission");
return;
}

HeadAPI.addFavoriteHead(player.getUniqueId(), head.getValue());
Utils.sendMessage(player, "&7Added &e" + head.getName() + " &7to favorites.");
Utils.playSound(player, "addFavorite");
}
}));
}
Expand All @@ -164,8 +171,15 @@ public static void openTagSearchDatabase(Player player, String tag) {
purchaseHead(player, head, 1, head.getCategory().getName(), head.getName());
}
if (e.getClick() == ClickType.RIGHT) {
if (!player.hasPermission("headdb.favorites")) {
Utils.sendMessage(player, "&cYou do not have permission for favorites!");
Utils.playSound(player, "noPermission");
return;
}

HeadAPI.addFavoriteHead(player.getUniqueId(), head.getValue());
Utils.sendMessage(player, "&7Added &e" + head.getName() + " &7to favorites.");
Utils.playSound(player, "addFavorite");
}
}));
}
Expand All @@ -187,8 +201,15 @@ public static void openCategoryDatabase(Player player, Category category) {
purchaseHead(player, head, 1, head.getCategory().getName(), head.getName());
}
if (e.getClick() == ClickType.RIGHT) {
if (!player.hasPermission("headdb.favorites")) {
Utils.sendMessage(player, "&cYou do not have permission for favorites!");
Utils.playSound(player, "noPermission");
return;
}

HeadAPI.addFavoriteHead(player.getUniqueId(), head.getValue());
Utils.sendMessage(player, "&7Added &e" + head.getName() + " &7to favorites.");
Utils.playSound(player, "addFavorite");
}
}));
}
Expand Down Expand Up @@ -287,6 +308,7 @@ public static boolean processPayment(Player player, int amount, String category,
// Don't mention receiving it for free in this case, since it is always free.
if (economy == null) {
Utils.sendMessage(player, String.format("&7You received &e%d &7x &e%s&7!", amount, description));
Utils.playSound(player, "noEconomy");
return true;
}

Expand All @@ -296,20 +318,24 @@ public static boolean processPayment(Player player, int amount, String category,
if (cost > 0) {
if (economy.has(player, cost)) {
economy.withdrawPlayer(player, cost);
Utils.sendMessage(player, String.format("&7You purchased &e%d &7x &e%s &7for &e%.2f %s&7!", amount, description, cost, economy.currencyNamePlural()));
Utils.sendMessage(player, String.format("&7You purchased &e%d &7x &e%s &7for &e%.2f&7!", amount, description, cost));
Utils.playSound(player, "paid");
return true;
}
Utils.sendMessage(player, String.format("&7You do not have enough &e%s &cto purchase &e%d &cx &e%s&7.", economy.currencyNamePlural(), amount, description));
Utils.sendMessage(player, String.format("&cYou do not have enough to purchase &e%d &cx &e%s&7.", amount, description));
Utils.playSound(player, "unavailable");
return false;
}

// Otherwise, the item is free.
Utils.sendMessage(player, String.format("&7You received &e%d &7x &e%s &7for &efree&7!", amount, description));
Utils.playSound(player, "free");
return true;
}

public static void purchaseHead(Player player, Head head, int amount, String category, String description) {
if (!processPayment(player, amount, category, description)) return;

PlayerHeadPurchaseEvent event = new PlayerHeadPurchaseEvent(player, head, getCategoryCost(player, category));
Bukkit.getPluginManager().callEvent(event);
if (!event.isCancelled()) {
Expand Down
23 changes: 21 additions & 2 deletions src/main/java/tsp/headdb/listener/MenuListener.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,23 +40,34 @@ public void click(InventoryClickEvent e) {
String name = ChatColor.stripColor(item.getItemMeta().getDisplayName().toLowerCase());
if (name.equalsIgnoreCase("favorites")) {
if (!player.hasPermission("headdb.favorites")) {
Utils.sendMessage(player, "&cYou do not have permission for favorites!");
player.closeInventory();
Utils.sendMessage(player, "&cYou do not have permission for favorites!");
Utils.playSound(player, "noPermission");
return;
}
InventoryUtils.openFavoritesMenu(player);
Utils.playSound(player, "open");
return;
}
if (name.equalsIgnoreCase("local")) {
if (!player.hasPermission("headdb.local")) {
Utils.sendMessage(player, "&cYou do not have permission to view local heads!");
player.closeInventory();
Utils.sendMessage(player, "&cYou do not have permission to view local heads!");
Utils.playSound(player, "noPermission");
return;
}
InventoryUtils.openLocalMenu(player);
Utils.playSound(player, "open");
return;
}
if (name.equalsIgnoreCase("search")) {
if (!player.hasPermission("headdb.search")) {
player.closeInventory();
Utils.sendMessage(player, "&cYou do not have permission for the search function!");
Utils.playSound(player, "noPermission");
return;
}

new AnvilGUI.Builder()
.onComplete((p, text) -> {
InventoryUtils.openSearchDatabase(p, text);
Expand All @@ -66,12 +77,20 @@ public void click(InventoryClickEvent e) {
.text("Name...")
.plugin(HeadDB.getInstance())
.open(player);
Utils.playSound(player, "open");
return;
}

Category category = Category.getByName(name);

if (category != null) {
if (HeadDB.getInstance().getConfig().getBoolean("requireCategoryPermission") && !player.hasPermission("headdb.category." + category)) {
Utils.sendMessage(player, "&cYou do not have permission for this category! (&e" + category.getName() + "&c)");
Utils.playSound(player, "noPermission");
return;
}

Utils.playSound(player, "open");
HeadAPI.openCategoryDatabase(player, category);
}
}
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/tsp/headdb/util/Utils.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
package tsp.headdb.util;

import org.bukkit.ChatColor;
import org.bukkit.Sound;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import tsp.headdb.HeadDB;

import java.util.regex.Pattern;

public class Utils {

private static final FileConfiguration config = HeadDB.getInstance().getConfig();
public static final Pattern UUID_PATTERN = Pattern.compile("[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-4[0-9a-fA-F]{3}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}");

/**
Expand All @@ -19,6 +24,18 @@ public static boolean validateUniqueId(String uuid) {
return UUID_PATTERN.matcher(uuid).matches();
}

public static void playSound(Player player, String key) {
// Check if sound is enabled
if (!config.getBoolean("ui.sound.enabled")) {
return;
}

player.playSound(player.getLocation(),
Sound.valueOf(config.getString("ui.sound." + key + ".name")),
(float) config.getDouble("ui.sound." + key + ".volume"),
(float) config.getDouble("ui.sound." + key + ".pitch"));
}

public static void sendMessage(CommandSender sender, String message) {
sender.sendMessage(colorize(message));
}
Expand Down
52 changes: 51 additions & 1 deletion src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ refresh: 3600
# If local heads should be enabled. Only starts keeping track of joined players when enabled!
localHeads: true

# If enabled categories will require a permission to be used
# Permission: headdb.category.<category>
requireCategoryPermission: false

# Economy options
economy:
enable: false
Expand Down Expand Up @@ -67,6 +71,52 @@ ui:
# Item used to fill unused slots in the categories menu. AIR is supported. You can use head: instead of item: here.
fill:
item: BLACK_STAINED_GLASS_PANE
sound:
# Whether the sounds should be played
enabled: true

# Played when a head is taken with no economy plugin
noEconomy:
# The name of the sound.
# Must be one from: https://hub.spigotmc.org/javadocs/bukkit/org/bukkit/Sound.html
name: ENTITY_EXPERIENCE_ORB_PICKUP
volume: 1
pitch: 1
# Played when the player doesn't have permission for the menu
noPermission:
name: BLOCK_ANVIL_BREAK
volume: 1
pitch: 1
# Played when they purchase a head
paid:
name: ENTITY_EXPERIENCE_ORB_PICKUP
volume: 1
pitch: 1
# Played when the head is free
free:
name: ENTITY_EXPERIENCE_ORB_PICKUP
volume: 1
pitch: 1
# Played when the player does not have enough funds for the head
unavailable:
name: BLOCK_ANVIL_BREAK
volume: 1
pitch: 1
# Played when a category/menu is opened
open:
name: ENTITY_EXPERIENCE_ORB_PICKUP
volume: 1
pitch: 1
# Played when a head is added to the favorites list
addFavorite:
name: ENTITY_EXPERIENCE_ORB_PICKUP
volume: 1
pitch: 1
# Played when a head is removed to from favorites list
removeFavorite:
name: BLOCK_ANVIL_BREAK
volume: 1
pitch: 1

# Debug Mode
debug: false
debug: false

0 comments on commit 1bd19ef

Please sign in to comment.