-
-
Notifications
You must be signed in to change notification settings - Fork 921
(Improvement) Implement playing sound by string ID #5201
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
05162fb
(improvement) implement playing sound by string ID
JeBobs 22f9a0f
Merge branch 'master' into sound_lookup
JeBobs 2c202cb
Fix getIsEnabled using wrong key
nossr50 e89d44e
always use registry, simplify custom sound enabling logic, optimize r…
nossr50 e51d00b
forgot we need this for legacy versions
nossr50 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
src/main/java/com/gmail/nossr50/util/sounds/SoundRegistryUtils.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| package com.gmail.nossr50.util.sounds; | ||
|
|
||
| import static java.lang.String.format; | ||
|
|
||
| import com.gmail.nossr50.mcMMO; | ||
| import com.gmail.nossr50.util.AttributeMapper; | ||
| import com.gmail.nossr50.util.LogUtils; | ||
| import java.lang.reflect.InvocationTargetException; | ||
| import java.lang.reflect.Method; | ||
| import java.util.Locale; | ||
| import org.bukkit.NamespacedKey; | ||
| import org.bukkit.Sound; | ||
| import org.jetbrains.annotations.Nullable; | ||
|
|
||
| public final class SoundRegistryUtils { | ||
|
|
||
| private static Method registryLookup; | ||
| private static Object soundReg; | ||
|
|
||
| public static final String PAPER_SOUND_REGISTRY_FIELD = "SOUND_EVENT"; | ||
| public static final String SPIGOT_SOUND_REGISTRY_FIELD = "SOUNDS"; | ||
| public static final String METHOD_GET_OR_THROW_NAME = "getOrThrow"; | ||
| public static final String METHOD_GET_NAME = "get"; | ||
|
|
||
| static { | ||
| boolean foundRegistry = false; | ||
| Class<?> registry; | ||
| try { | ||
| registry = Class.forName(AttributeMapper.ORG_BUKKIT_REGISTRY); | ||
| try { | ||
| // First check for Paper's sound registry, held by field SOUND_EVENT | ||
| soundReg = registry.getField(PAPER_SOUND_REGISTRY_FIELD).get(null); | ||
| foundRegistry = true; | ||
| } catch (NoSuchFieldException | IllegalAccessException e) { | ||
| try { | ||
| soundReg = registry.getField(SPIGOT_SOUND_REGISTRY_FIELD); | ||
| foundRegistry = true; | ||
| } catch (NoSuchFieldException ex) { | ||
| // ignored | ||
| } | ||
| } | ||
| } catch (ClassNotFoundException e) { | ||
| // ignored | ||
| } | ||
|
|
||
| if (foundRegistry) { | ||
| try { | ||
| // getOrThrow isn't in all API versions, but we use it if it exists | ||
| registryLookup = soundReg.getClass().getMethod(METHOD_GET_OR_THROW_NAME, | ||
| NamespacedKey.class); | ||
| } catch (NoSuchMethodException e) { | ||
| try { | ||
| registryLookup = soundReg.getClass().getMethod(METHOD_GET_NAME, | ||
| NamespacedKey.class); | ||
| } catch (NoSuchMethodException ex) { | ||
| // ignored exception | ||
| registryLookup = null; | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public static boolean useLegacyLookup() { | ||
| return registryLookup == null; | ||
| } | ||
|
|
||
| public static @Nullable Sound getSound(String id, String fallBackId) { | ||
| if (registryLookup != null) { | ||
| try { | ||
| return (Sound) registryLookup.invoke(soundReg, NamespacedKey.fromString(id)); | ||
| } catch(InvocationTargetException | IllegalAccessException | ||
| | IllegalArgumentException e) { | ||
| if (fallBackId != null) { | ||
| LogUtils.debug(mcMMO.p.getLogger(), | ||
| format("Could not find sound with ID '%s', trying fallback ID '%s'", id, | ||
| fallBackId)); | ||
| try { | ||
| return (Sound) registryLookup.invoke(soundReg, | ||
| NamespacedKey.fromString(fallBackId)); | ||
| } catch (IllegalAccessException | InvocationTargetException ex) { | ||
| mcMMO.p.getLogger().severe(format("Could not find sound with ID %s," | ||
| + " fallback ID of %s also failed.", id, fallBackId)); | ||
| } | ||
| } else { | ||
| mcMMO.p.getLogger().severe(format("Could not find sound with ID %s.", id)); | ||
| } | ||
| throw new RuntimeException(e); | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
| } |
60 changes: 34 additions & 26 deletions
60
src/main/java/com/gmail/nossr50/util/sounds/SoundType.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,31 +1,39 @@ | ||||||
| package com.gmail.nossr50.util.sounds; | ||||||
|
|
||||||
| public enum SoundType { | ||||||
| ANVIL, | ||||||
| LEVEL_UP, | ||||||
| FIZZ, | ||||||
| ITEM_BREAK, | ||||||
| POP, | ||||||
| CHIMAERA_WING, | ||||||
| ROLL_ACTIVATED, | ||||||
| SKILL_UNLOCKED, | ||||||
| DEFLECT_ARROWS, | ||||||
| TOOL_READY, | ||||||
| ABILITY_ACTIVATED_GENERIC, | ||||||
| ABILITY_ACTIVATED_BERSERK, | ||||||
| BLEED, | ||||||
| GLASS, | ||||||
| ITEM_CONSUMED, | ||||||
| CRIPPLE, | ||||||
| TIRED; | ||||||
| ANVIL("minecraft:block.anvil.place"), | ||||||
| ITEM_BREAK("minecraft:entity.item.break"), | ||||||
| POP("minecraft:entity.item.pickup"), | ||||||
| CHIMAERA_WING("minecraft:entity.bat.takeoff"), | ||||||
| LEVEL_UP("minecraft:entity.player.levelup"), | ||||||
| FIZZ("minecraft:block.fire.extinguish"), | ||||||
| TOOL_READY("minecraft:item.armor.equip_gold"), | ||||||
| ROLL_ACTIVATED("minecraft:entity.llama.swag"), | ||||||
| SKILL_UNLOCKED("minecraft:ui.toast.challenge_complete"), | ||||||
| ABILITY_ACTIVATED_BERSERK("minecraft:block.conduit.ambient"), | ||||||
| TIRED("minecraft:block.conduit.ambient"), | ||||||
| ABILITY_ACTIVATED_GENERIC("minecraft:item.trident.riptide_3"), | ||||||
| DEFLECT_ARROWS("minecraft:entity.ender_eye.death"), | ||||||
| BLEED("minecraft:entity.ender_eye.death"), | ||||||
| GLASS("minecraft:block.glass.break"), | ||||||
| ITEM_CONSUMED("minecraft:item.bottle.empty"), | ||||||
| CRIPPLE("minecraft:block.anvil.place"); | ||||||
|
||||||
| CRIPPLE("minecraft:block.anvil.place"); | |
| CRIPPLE("minecraft:item.mace.smash_ground"); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The default value for EnableCustomSounds should be 'false' to match the configuration file default, not 'true'. This could cause unexpected behavior when the configuration is not explicitly set.