Skip to content

Commit

Permalink
clean up the code playing sounds
Browse files Browse the repository at this point in the history
  • Loading branch information
ryderbelserion committed Feb 8, 2024
1 parent 9b445fb commit 683f9ba
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 41 deletions.
Expand Up @@ -4,10 +4,12 @@
import com.badbones69.crazycrates.api.objects.Crate;
import com.badbones69.crazycrates.api.objects.ItemBuilder;
import com.badbones69.crazycrates.api.objects.Prize;
import com.badbones69.crazycrates.tasks.crates.effects.SoundEffect;
import com.google.common.base.Preconditions;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.SoundCategory;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.entity.Player;
import org.bukkit.inventory.Inventory;
Expand Down Expand Up @@ -369,13 +371,24 @@ public Prize getPrize(int slot) {
}

/**
* Plays a sound at different volume levels with fallbacks
* Plays a sound at different volume levels with fallbacks.
*
* @param type i.e. stop, cycle or click sound
* @param category sound category to respect client settings
* @param defaultSound fallback sound
* @param type i.e. stop, cycle or click sound.
* @param category sound category to respect client settings.
* @param fallback fallback sound in case no sound is found.
*/
public void playSound(String type, SoundCategory category, String defaultSound) {
crate.playSound(getPlayer(), getPlayer().getLocation(), type, category, defaultSound);
public void playSound(String type, SoundCategory category, String fallback) {
ConfigurationSection section = getFile().getConfigurationSection("Crate.sound");

if (section != null) {
SoundEffect sound = new SoundEffect(
section,
type,
fallback,
category
);

sound.play(getPlayer().getLocation());
}
}
}
@@ -1,7 +1,9 @@
package com.badbones69.crazycrates.api.objects;

import com.badbones69.crazycrates.tasks.crates.effects.SoundEffect;
import org.bukkit.Sound;
import org.bukkit.SoundCategory;
import org.bukkit.configuration.ConfigurationSection;
import us.crazycrew.crazycrates.api.enums.types.CrateType;
import com.badbones69.crazycrates.CrazyCrates;
import com.badbones69.crazycrates.api.FileManager;
Expand All @@ -21,7 +23,6 @@
import com.badbones69.crazycrates.api.builders.types.CratePreviewMenu;
import com.badbones69.crazycrates.api.utils.MiscUtils;
import com.badbones69.crazycrates.api.utils.MsgUtils;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
Expand Down Expand Up @@ -568,23 +569,24 @@ public List<ItemStack> getPreviewItems() {
}

/**
* Plays a sound at different volume levels with fallbacks
* Plays a sound at different volume levels with fallbacks.
*
* @param type i.e. stop, cycle or click sound
* @param category sound category to respect client settings
* @param defaultSound fallback sound
*/
public void playSound(Player player, Location location, String type, SoundCategory category, String defaultSound) {
String path = "Crate." + type;

boolean isEnabled = getFile().getBoolean(path + ".toggle", true);

if (isEnabled) {
String sound = getFile().getString(path + ".value", defaultSound);
double volume = getFile().getDouble(path + ".volume", 1.0);
double pitch = getFile().getDouble(path + ".pitch", 1.0);

player.playSound(location, Sound.valueOf(sound), category, (float) volume, (float) pitch);
* @param type i.e. stop, cycle or click sound.
* @param category sound category to respect client settings.
* @param fallback fallback sound in case no sound is found.
*/
public void playSound(Location location, String type, String fallback, SoundCategory category) {
ConfigurationSection section = getFile().getConfigurationSection("Crate.sound");

if (section != null) {
SoundEffect sound = new SoundEffect(
section,
type,
fallback,
category
);

sound.play(location);
}
}
}
Expand Up @@ -87,7 +87,7 @@ public void onInventoryClose(InventoryCloseEvent event) {
}

// Play sound.
if (playSound) cosmic.getCrate().playSound(player, player.getLocation(), "click-sound", SoundCategory.PLAYERS, "UI_BUTTON_CLICK");
if (playSound) cosmic.getCrate().playSound(player.getLocation(), "click-sound", "UI_BUTTON_CLICK", SoundCategory.PLAYERS);

// Remove opening stuff.
this.crateManager.removePlayerFromOpeningList(player);
Expand Down Expand Up @@ -162,7 +162,7 @@ public void onInventoryClickPrize(InventoryClickEvent event) {

event.setCurrentItem(prize.getDisplayItem());

cosmic.getCrate().playSound(player, player.getLocation(), "click-sound", SoundCategory.PLAYERS, "UI_BUTTON_CLICK");
cosmic.getCrate().playSound(player.getLocation(), "click-sound","UI_BUTTON_CLICK", SoundCategory.PLAYERS);

if (prize.useFireworks()) MiscUtils.spawnFirework(player.getLocation().add(0, 1, 0), null);
}
Expand Down Expand Up @@ -241,7 +241,7 @@ public void onInventoryClick(InventoryClickEvent event) {
cosmicCrateManager.addPickedPrize(player, slot);

// Play a sound to indicate they clicked a chest.
cosmic.getCrate().playSound(player, player.getLocation(), "click-sound", SoundCategory.PLAYERS, "UI_BUTTON_CLICK");
cosmic.getCrate().playSound(player.getLocation(), "click-sound","UI_BUTTON_CLICK", SoundCategory.PLAYERS);
}
} else if (container.has(PersistentKeys.cosmic_picked_crate.getNamespacedKey(this.plugin))) {
// Get item builder.
Expand All @@ -256,7 +256,7 @@ public void onInventoryClick(InventoryClickEvent event) {
cosmicCrateManager.removePickedPrize(player, slot);

// Play a sound to indicate they clicked a chest.
cosmic.getCrate().playSound(player, player.getLocation(), "click-sound", SoundCategory.PLAYERS, "UI_BUTTON_CLICK");
cosmic.getCrate().playSound(player.getLocation(), "click-sound","UI_BUTTON_CLICK", SoundCategory.PLAYERS);
}

// Get the crate name.
Expand Down Expand Up @@ -361,7 +361,7 @@ public void run() {
plugin.getServer().getLogger().log(Level.SEVERE, "An issue occurred when the user " + player.getName() + " was using the " + crate.getName() + " crate and so they were issued a key refund.", exception);

// Play a sound
crate.playSound(player, player.getLocation(), "stop-sound", SoundCategory.PLAYERS, "BLOCK_ANVIL_PLACE");
crate.playSound(player.getLocation(), "stop-sound", "BLOCK_ANVIL_PLACE", SoundCategory.PLAYERS);
}

// Wrap it all up.
Expand All @@ -379,7 +379,7 @@ public void run() {
showRewards(player, view, cosmic, cosmicCrateManager);

// Play a sound
crate.playSound(player, player.getLocation(), "stop-sound", SoundCategory.PLAYERS, "BLOCK_ANVIL_PLACE");
crate.playSound(player.getLocation(), "stop-sound", "BLOCK_ANVIL_PLACE", SoundCategory.PLAYERS);
}
}
}, 0L, 80L);
Expand All @@ -393,7 +393,7 @@ private void startRollingAnimation(Player player, InventoryView view, CratePrize
if (tier != null) view.getTopInventory().setItem(slot, tier.getTierPane());
}

cosmic.getCrate().playSound(player, player.getLocation(), "cycle-sound", SoundCategory.PLAYERS, "BLOCK_NOTE_BLOCK_XYLOPHONE");
cosmic.getCrate().playSound(player.getLocation(), "cycle-sound", "BLOCK_NOTE_BLOCK_XYLOPHONE", SoundCategory.PLAYERS);
player.updateInventory();
}

Expand Down
Expand Up @@ -117,7 +117,7 @@ public void onChestClick(PlayerInteractEvent event) {
@Override
public void run() {
session.endCrate();
crate.playSound(player, block.getLocation(), "stop-sound", SoundCategory.BLOCKS, "BLOCK_ANVIL_LAND");
crate.playSound(block.getLocation(), "stop-sound", "BLOCK_ANVIL_LAND", SoundCategory.BLOCKS);
}
}.runTaskLater(this.plugin, 60);
}
Expand Down
Expand Up @@ -61,8 +61,7 @@ public void onInventoryClick(InventoryClickEvent event) {
this.plugin.getServer().getPluginManager().callEvent(new PlayerPrizeEvent(player, crate, crate.getName(), prize));
this.crateManager.removePlayerFromOpeningList(player);

//TODO() Adopt the new sound system including custom sounds.
crate.playSound(player, player.getLocation(), "cycle-sound", SoundCategory.PLAYERS, "BLOCK_ANVIL_LAND");
crate.playSound(player.getLocation(), "cycle-sound", "BLOCK_ANVIL_LAND", SoundCategory.PLAYERS);

this.crateManager.addCrateTask(player, new BukkitRunnable() {
@Override
Expand Down
Expand Up @@ -73,7 +73,7 @@ public void onInventoryClick(InventoryClickEvent event) {

if (event.getAction() == InventoryAction.PICKUP_HALF) { // Right-clicked the item
if (crate.isPreviewEnabled()) {
crate.playSound(player, player.getLocation(), "click-sound", SoundCategory.PLAYERS, "UI_BUTTON_CLICK");
crate.playSound(player.getLocation(), "click-sound", "UI_BUTTON_CLICK", SoundCategory.PLAYERS);

player.closeInventory();
this.inventoryManager.addViewer(player);
Expand Down Expand Up @@ -123,8 +123,6 @@ public void onInventoryClick(InventoryClickEvent event) {
return;
}

crate.playSound(player, player.getLocation(), "click-sound", SoundCategory.PLAYERS, "UI_BUTTON_CLICK");

this.crateManager.openCrate(player, crate, keyType, player.getLocation(), true, false);
}

Expand Down
Expand Up @@ -58,7 +58,7 @@ public void onInventoryClick(InventoryClickEvent event) {

if (event.getRawSlot() == crate.getAbsoluteItemPosition(4) && this.crazyHandler.getConfigManager().getConfig().getProperty(ConfigKeys.enable_crate_menu)) { // Clicked the menu button.
if (this.inventoryManager.inCratePreview(player)) {
crate.playSound(player, player.getLocation(), "click-sound", SoundCategory.PLAYERS, "UI_BUTTON_CLICK");
crate.playSound(player.getLocation(), "click-sound","UI_BUTTON_CLICK", SoundCategory.PLAYERS);

this.inventoryManager.removeViewer(player);
this.inventoryManager.closeCratePreview(player);
Expand All @@ -69,15 +69,15 @@ public void onInventoryClick(InventoryClickEvent event) {
}
} else if (event.getRawSlot() == crate.getAbsoluteItemPosition(5)) { // Clicked the next button.
if (this.inventoryManager.getPage(player) < crate.getMaxPage()) {
crate.playSound(player, player.getLocation(), "click-sound", SoundCategory.PLAYERS, "UI_BUTTON_CLICK");
crate.playSound(player.getLocation(), "click-sound","UI_BUTTON_CLICK", SoundCategory.PLAYERS);

this.inventoryManager.nextPage(player);

this.inventoryManager.openCratePreview(player, crate);
}
} else if (event.getRawSlot() == crate.getAbsoluteItemPosition(3)) { // Clicked the back button.
if (this.inventoryManager.getPage(player) > 1 && this.inventoryManager.getPage(player) <= crate.getMaxPage()) {
crate.playSound(player, player.getLocation(), "click-sound", SoundCategory.PLAYERS, "UI_BUTTON_CLICK");
crate.playSound(player.getLocation(), "click-sound","UI_BUTTON_CLICK", SoundCategory.PLAYERS);

this.inventoryManager.backPage(player);

Expand Down
@@ -0,0 +1,39 @@
package com.badbones69.crazycrates.tasks.crates.effects;

import org.bukkit.Location;
import org.bukkit.Sound;
import org.bukkit.SoundCategory;
import org.bukkit.configuration.ConfigurationSection;

public class SoundEffect {

private final SoundCategory category;
private final boolean isEnabled;
private final Sound sound;
private final float volume;
private final float pitch;

/**
* Builds a sound to play.
*
* @param section in the crate config.
* @param type of sound i.e. cycle-sound or click-sound.
* @param fallback the fallback sound if no sound is found.
* @param category of sound as to respect the client side sound settings.
*/
public SoundEffect(ConfigurationSection section, String type, String fallback, SoundCategory category) {
this.isEnabled = section.getBoolean(type + ".toggle", false);

this.sound = Sound.valueOf(section.getString(type + ".value", fallback));
this.volume = (float) section.getDouble(type + ".volume", 1.0);
this.pitch = (float) section.getDouble(type + ".pitch", 1.0);

this.category = category;
}

public void play(Location location) {
if (!this.isEnabled) return;

location.getWorld().playSound(location, this.sound, this.category, this.volume, this.pitch);
}
}
Expand Up @@ -230,7 +230,7 @@ public void run() {
spawnParticles(particle, particleColor, this.spiralLocationsClockwise.get(this.tickTillSpawn), this.spiralLocationsCounterClockwise.get(this.tickTillSpawn));
this.tickTillSpawn++;
} else {
crate.playSound(player, player.getLocation(), "cycle-sound", SoundCategory.PLAYERS, "BLOCK_STONE_STEP");
crate.playSound(player.getLocation(), "cycle-sound", "BLOCK_STONE_STEP", SoundCategory.PLAYERS);

Block chest = crateLocations.get(crateNumber).getBlock();

Expand All @@ -257,7 +257,7 @@ public void run() {
// End the crate by force.
endCrateForce(true);
player.sendMessage(Messages.out_of_time.getString());
crate.playSound(player, player.getLocation(), "stop-sound", SoundCategory.PLAYERS, "ENTITY_PLAYER_LEVELUP");
crate.playSound(player.getLocation(), "stop-sound", "ENTITY_PLAYER_LEVELUP", SoundCategory.PLAYERS);
}
}.runTaskLater(this.plugin, this.plugin.getConfigManager().getConfig().getProperty(ConfigKeys.quad_crate_timer) * 20));
}
Expand Down

0 comments on commit 683f9ba

Please sign in to comment.