-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
178 additions
and
19 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
src/main/java/com/github/zly2006/enclosure/access/ClientAccess.kt
This file contains 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,10 @@ | ||
package com.github.zly2006.enclosure.access | ||
|
||
import net.minecraft.sound.MusicSound | ||
|
||
@Suppress("INAPPLICABLE_JVM_NAME") | ||
interface ClientAccess { | ||
@get:JvmName("enclosure\$getBgm") | ||
@set:JvmName("enclosure\$setBgm") | ||
var bgm: MusicSound? | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/java/com/github/zly2006/enclosure/client/mixin/MixinClient.java
This file contains 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,38 @@ | ||
package com.github.zly2006.enclosure.client.mixin; | ||
|
||
import com.github.zly2006.enclosure.access.ClientAccess; | ||
import net.minecraft.client.MinecraftClient; | ||
import net.minecraft.sound.MusicSound; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.Unique; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
import org.spongepowered.asm.mixin.injection.Inject; | ||
import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; | ||
|
||
@Mixin(MinecraftClient.class) | ||
public class MixinClient implements ClientAccess { | ||
@Unique @Nullable | ||
MusicSound musicSound; | ||
@Inject( | ||
method = "getMusicType", | ||
at = @At(value = "FIELD", target = "Lnet/minecraft/client/MinecraftClient;player:Lnet/minecraft/client/network/ClientPlayerEntity;", ordinal = 0), | ||
cancellable = true | ||
) | ||
private void modifyBgm(CallbackInfoReturnable<MusicSound> cir) { | ||
if (musicSound != null) { | ||
cir.setReturnValue(musicSound); | ||
} | ||
} | ||
|
||
|
||
@Override | ||
public void enclosure$setBgm(MusicSound musicSound) { | ||
this.musicSound = musicSound; | ||
} | ||
|
||
@Override | ||
public MusicSound enclosure$getBgm() { | ||
return musicSound; | ||
} | ||
} |
This file contains 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 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 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 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 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
29 changes: 29 additions & 0 deletions
29
src/main/kotlin/com/github/zly2006/enclosure/command/MusicSubcommand.kt
This file contains 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,29 @@ | ||
package com.github.zly2006.enclosure.command | ||
|
||
import com.github.zly2006.enclosure.utils.TrT | ||
import net.minecraft.command.argument.IdentifierArgumentType | ||
import net.minecraft.command.suggestion.SuggestionProviders | ||
import net.minecraft.registry.Registries | ||
import net.minecraft.server.command.CommandManager | ||
|
||
fun BuilderScope<*>.registerMusic() { | ||
literal("music") { | ||
literal("set") { | ||
argument( | ||
CommandManager.argument("music", IdentifierArgumentType.identifier()) | ||
.suggests(SuggestionProviders.AVAILABLE_SOUNDS) | ||
) { | ||
optionalEnclosure { area -> | ||
val music = IdentifierArgumentType.getIdentifier(this, "music") | ||
if (!music.path.startsWith("music.") && !music.path.startsWith("music_disc.")) { | ||
error(TrT.of("Invalid music."), this) | ||
} | ||
val soundEvent = Registries.SOUND_EVENT.get(music) | ||
?: error(TrT.of("Invalid music."), this) | ||
area.music = music | ||
source.sendMessage(TrT.of("Set music to $music in ${area.fullName}.")) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains 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 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
55 changes: 55 additions & 0 deletions
55
src/main/kotlin/com/github/zly2006/enclosure/network/play/BackgroundMusicPayload.kt
This file contains 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,55 @@ | ||
package com.github.zly2006.enclosure.network.play | ||
|
||
import com.github.zly2006.enclosure.access.ClientAccess | ||
import net.fabricmc.api.EnvType | ||
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking | ||
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry | ||
import net.fabricmc.loader.api.FabricLoader | ||
import net.minecraft.client.MinecraftClient | ||
import net.minecraft.network.PacketByteBuf | ||
import net.minecraft.network.codec.PacketCodec | ||
import net.minecraft.network.packet.CustomPayload | ||
import net.minecraft.registry.RegistryKeys | ||
import net.minecraft.sound.MusicSound | ||
import net.minecraft.util.Identifier | ||
|
||
class BackgroundMusicPayload( | ||
val enclosure: String?, | ||
val music: Identifier? | ||
): CustomPayload { | ||
companion object { | ||
val ID = CustomPayload.Id<BackgroundMusicPayload>(Identifier.of("enclosure", "background_music")) | ||
private val CODEC = PacketCodec.of<PacketByteBuf, BackgroundMusicPayload>( | ||
{ value, buf -> | ||
buf.writeNullable(value!!.enclosure, PacketByteBuf::writeString) | ||
buf.writeNullable(value.music, PacketByteBuf::writeIdentifier) | ||
}, | ||
{ buf -> | ||
BackgroundMusicPayload(buf.readNullable(PacketByteBuf::readString), buf.readNullable(PacketByteBuf::readIdentifier)) | ||
} | ||
) | ||
|
||
fun register() { | ||
PayloadTypeRegistry.playS2C().register(ID, CODEC) | ||
PayloadTypeRegistry.playC2S().register(ID, CODEC) | ||
|
||
if (FabricLoader.getInstance().environmentType == EnvType.CLIENT) { | ||
ClientPlayNetworking.registerGlobalReceiver(ID) { p, _ -> | ||
val mc = MinecraftClient.getInstance() | ||
if (p.music == null) { | ||
(mc as ClientAccess).bgm = null | ||
} else { | ||
(mc as ClientAccess).bgm = MusicSound( | ||
mc.player!!.registryManager.get(RegistryKeys.SOUND_EVENT).getEntry(p.music).orElseThrow(), | ||
12000, | ||
24000, | ||
true | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
override fun getId() = ID | ||
} |
This file contains 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