mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-01-28 18:04:47 +00:00
Use reflection to get the record's sound
getSound is client side only - I could have sworn it became shared at some point, but that may have been reverted again (or I imagined it). Fixes #263
This commit is contained in:
parent
6ea8ca991b
commit
18068effec
@ -6,11 +6,16 @@
|
||||
|
||||
package dan200.computercraft.shared.media.items;
|
||||
|
||||
import dan200.computercraft.ComputerCraft;
|
||||
import dan200.computercraft.api.media.IMedia;
|
||||
import dan200.computercraft.shared.util.RecordUtil;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.item.MusicDiscItem;
|
||||
import net.minecraft.util.SoundEvent;
|
||||
import net.minecraft.util.text.TranslationTextComponent;
|
||||
import net.minecraftforge.fml.common.ObfuscationReflectionHelper;
|
||||
import net.minecraftforge.fml.common.ObfuscationReflectionHelper.UnableToAccessFieldException;
|
||||
import net.minecraftforge.fml.common.ObfuscationReflectionHelper.UnableToFindFieldException;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
@ -34,12 +39,26 @@ public final class RecordMedia implements IMedia
|
||||
@Override
|
||||
public String getAudioTitle( @Nonnull ItemStack stack )
|
||||
{
|
||||
return RecordUtil.getRecordInfo( stack );
|
||||
Item item = stack.getItem();
|
||||
if( !(item instanceof MusicDiscItem) ) return null;
|
||||
|
||||
return new TranslationTextComponent( item.getTranslationKey() + ".desc" ).getString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public SoundEvent getAudio( @Nonnull ItemStack stack )
|
||||
{
|
||||
return ((MusicDiscItem) stack.getItem()).getSound();
|
||||
Item item = stack.getItem();
|
||||
if( !(item instanceof MusicDiscItem) ) return null;
|
||||
|
||||
try
|
||||
{
|
||||
return ObfuscationReflectionHelper.getPrivateValue( MusicDiscItem.class, (MusicDiscItem) item, "field_185076_b" );
|
||||
}
|
||||
catch( UnableToAccessFieldException | UnableToFindFieldException e )
|
||||
{
|
||||
ComputerCraft.log.error( "Cannot get disk sound", e );
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -9,17 +9,11 @@ package dan200.computercraft.shared.util;
|
||||
import dan200.computercraft.shared.network.NetworkHandler;
|
||||
import dan200.computercraft.shared.network.NetworkMessage;
|
||||
import dan200.computercraft.shared.network.client.PlayRecordClientMessage;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.item.MusicDiscItem;
|
||||
import net.minecraft.util.SoundEvent;
|
||||
import net.minecraft.util.math.BlockPos;
|
||||
import net.minecraft.util.math.Vec3d;
|
||||
import net.minecraft.util.text.TranslationTextComponent;
|
||||
import net.minecraft.world.World;
|
||||
|
||||
import javax.annotation.Nonnull;
|
||||
|
||||
public final class RecordUtil
|
||||
{
|
||||
private RecordUtil() {}
|
||||
@ -29,12 +23,4 @@ public final class RecordUtil
|
||||
NetworkMessage packet = record != null ? new PlayRecordClientMessage( pos, record, recordInfo ) : new PlayRecordClientMessage( pos );
|
||||
NetworkHandler.sendToAllAround( packet, world, new Vec3d( pos ), 64 );
|
||||
}
|
||||
|
||||
public static String getRecordInfo( @Nonnull ItemStack recordStack )
|
||||
{
|
||||
Item item = recordStack.getItem();
|
||||
if( !(item instanceof MusicDiscItem) ) return null;
|
||||
|
||||
return new TranslationTextComponent( item.getTranslationKey() + ".desc" ).getString();
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user