11package mod .acgaming .extrasounds .sound ;
22
3- import java .util .Map ;
3+ import java .util .* ;
44
55import net .minecraft .client .Minecraft ;
66import net .minecraft .client .audio .ISound ;
99import net .minecraft .init .Items ;
1010import net .minecraft .init .SoundEvents ;
1111import net .minecraft .item .*;
12+ import net .minecraft .item .crafting .CraftingManager ;
13+ import net .minecraft .item .crafting .IRecipe ;
14+ import net .minecraft .item .crafting .Ingredient ;
1215import net .minecraft .util .ResourceLocation ;
1316import net .minecraft .util .SoundCategory ;
1417import net .minecraft .util .SoundEvent ;
@@ -53,17 +56,18 @@ public static void initSoundCategoryMap()
5356 {
5457 soundCategoryMap .clear ();
5558
56- soundCategoryMap .put ("wood" , ESSoundEvents .pick_place_wood );
5759 soundCategoryMap .put ("dirt" , ESSoundEvents .pick_place_dirt );
58- soundCategoryMap .put ("gravel " , ESSoundEvents .pick_place_gravel );
59- soundCategoryMap .put ("sand " , ESSoundEvents .pick_place_sand );
60+ soundCategoryMap .put ("dust " , ESSoundEvents .pick_place_dust );
61+ soundCategoryMap .put ("gem " , ESSoundEvents .pick_place_gem );
6062 soundCategoryMap .put ("grass" , ESSoundEvents .pick_place_grass );
61- soundCategoryMap .put ("wool" , ESSoundEvents .pick_place_wool );
62- soundCategoryMap .put ("snow" , ESSoundEvents .pick_place_snow );
63+ soundCategoryMap .put ("gravel" , ESSoundEvents .pick_place_gravel );
6364 soundCategoryMap .put ("ingot" , ESSoundEvents .pick_place_ingot );
6465 soundCategoryMap .put ("nugget" , ESSoundEvents .pick_place_nugget );
65- soundCategoryMap .put ("gem" , ESSoundEvents .pick_place_gem );
66- soundCategoryMap .put ("dust" , ESSoundEvents .pick_place_dust );
66+ soundCategoryMap .put ("sand" , ESSoundEvents .pick_place_sand );
67+ soundCategoryMap .put ("snow" , ESSoundEvents .pick_place_snow );
68+ soundCategoryMap .put ("stone" , ESSoundEvents .pick_place_stone );
69+ soundCategoryMap .put ("wood" , ESSoundEvents .pick_place_wood );
70+ soundCategoryMap .put ("wool" , ESSoundEvents .pick_place_wool );
6771 }
6872
6973 @ SuppressWarnings ("ConstantConditions" )
@@ -134,13 +138,34 @@ public static void initSoundFinalMap()
134138 if (item instanceof ItemBlock ) soundEvent = ((ItemBlock ) item ).getBlock ().getSoundType ().getPlaceSound ();
135139 else if (item instanceof ItemFood ) soundEvent = ESSoundEvents .pick_place_food ;
136140 else if (item instanceof ItemArmor ) soundEvent = ((ItemArmor ) item ).getArmorMaterial ().getSoundEvent ();
137- else if (item == Items .WATER_BUCKET ) soundEvent = SoundEvents .ITEM_BUCKET_FILL ;
141+ else if (item == Items .WATER_BUCKET || item == Items . MILK_BUCKET ) soundEvent = SoundEvents .ITEM_BUCKET_FILL ;
138142 else if (item == Items .LAVA_BUCKET ) soundEvent = SoundEvents .ITEM_BUCKET_FILL_LAVA ;
139143 else if (item instanceof UniversalBucket )
140144 {
141145 FluidStack fluidStack = ((UniversalBucket ) item ).getFluid (new ItemStack (item ));
142146 if (fluidStack != null ) soundEvent = fluidStack .getFluid ().getFillSound ();
143147 }
148+ // CHECK ITEM'S RECIPE INGREDIENTS FOR KNOWN SOUND EVENTS
149+ else if (item .getRegistryName () != null && CraftingManager .REGISTRY .containsKey (item .getRegistryName ()))
150+ {
151+ IRecipe recipe = CraftingManager .REGISTRY .getObject (item .getRegistryName ());
152+ if (recipe != null )
153+ {
154+ List <Ingredient > ingredientList = new ArrayList <>(recipe .getIngredients ());
155+ List <ItemStack > ingredientStackList = new ArrayList <>();
156+ // GET ALL INGREDIENT ITEM STACKS
157+ for (Ingredient ingredient : ingredientList ) ingredientStackList .addAll (Arrays .asList (ingredient .getMatchingStacks ()));
158+ if (ingredientStackList .size () > 0 )
159+ {
160+ // SORT LIST BY FREQUENCY
161+ ingredientStackList .sort (Comparator .comparing (i -> Collections .frequency (ingredientStackList , i )).reversed ());
162+ // GET MOST FREQUENT ITEM (BEST EFFORT)
163+ Item ingredientItem = ingredientStackList .get (0 ).getItem ();
164+ if (soundItemMap .containsKey (ingredientItem )) soundEvent = soundCategoryMap .get (soundItemMap .get (ingredientItem ));
165+ else if (ingredientItem instanceof ItemBlock ) soundEvent = ((ItemBlock ) ingredientItem ).getBlock ().getSoundType ().getPlaceSound ();
166+ }
167+ }
168+ }
144169 if (soundEvent != null ) soundFinalMap .put (item , soundEvent );
145170 }
146171 }
@@ -157,8 +182,8 @@ public static void playSoundPlayer(SoundEvent soundEvent, float pitch, float vol
157182 {
158183 Minecraft mc = Minecraft .getMinecraft ();
159184 EntityPlayerSP player = mc .player ;
160- if (player != null ) mc .getSoundHandler ().playSound (new PositionedSoundRecord (soundEvent .getSoundName (), SoundCategory .PLAYERS , volume , pitch , false , 0 , ISound .AttenuationType .NONE , (float ) player .posX , (float ) player .posY + 32 , (float ) player .posZ ));
161- else mc .getSoundHandler ().playSound (new PositionedSoundRecord (soundEvent .getSoundName (), SoundCategory .MASTER , volume , pitch , false , 0 , ISound .AttenuationType .NONE , 0F , 0F , 0F ));
185+ if (player != null ) mc .getSoundHandler ().playSound (new PositionedSoundRecord (soundEvent .getSoundName (), SoundCategory .PLAYERS , volume , pitch - 0.1F + randomOffset () , false , 0 , ISound .AttenuationType .NONE , (float ) player .posX , (float ) player .posY + 32 , (float ) player .posZ ));
186+ else mc .getSoundHandler ().playSound (new PositionedSoundRecord (soundEvent .getSoundName (), SoundCategory .MASTER , volume , pitch - 0.1F + randomOffset () , false , 0 , ISound .AttenuationType .NONE , 0F , 0F , 0F ));
162187 lastPlayed = now ;
163188 }
164189 }
@@ -170,8 +195,8 @@ public static void playSoundPlayer(SoundEvent soundEvent)
170195 {
171196 Minecraft mc = Minecraft .getMinecraft ();
172197 EntityPlayerSP player = mc .player ;
173- if (player != null ) mc .getSoundHandler ().playSound (new PositionedSoundRecord (soundEvent .getSoundName (), SoundCategory .PLAYERS , 1.0F , 1.0F , false , 0 , ISound .AttenuationType .NONE , (float ) player .posX , (float ) player .posY + 32 , (float ) player .posZ ));
174- else mc .getSoundHandler ().playSound (new PositionedSoundRecord (soundEvent .getSoundName (), SoundCategory .MASTER , 1.0F , 1.0F , false , 0 , ISound .AttenuationType .NONE , 0F , 0F , 0F ));
198+ if (player != null ) mc .getSoundHandler ().playSound (new PositionedSoundRecord (soundEvent .getSoundName (), SoundCategory .PLAYERS , 1.0F , 0.9F + randomOffset () , false , 0 , ISound .AttenuationType .NONE , (float ) player .posX , (float ) player .posY + 32 , (float ) player .posZ ));
199+ else mc .getSoundHandler ().playSound (new PositionedSoundRecord (soundEvent .getSoundName (), SoundCategory .MASTER , 1.0F , 0.9F + randomOffset () , false , 0 , ISound .AttenuationType .NONE , 0F , 0F , 0F ));
175200 lastPlayed = now ;
176201 }
177202 }
@@ -206,7 +231,6 @@ public static void playClickSound(ItemStack stackIn)
206231 if (stackIn .isEmpty ()) return ;
207232
208233 Item item = stackIn .getItem ();
209-
210234 if (soundItemMap .containsKey (item )) playSoundPlayer (soundCategoryMap .get (soundItemMap .get (item )));
211235 else if (soundFinalMap .containsKey (item )) playSoundPlayer (soundFinalMap .get (item ), 2.0F , 0.4F );
212236 else playSoundPlayer (ESSoundEvents .pick_place_generic );
0 commit comments