Skip to content

Commit

Permalink
feat
Browse files Browse the repository at this point in the history
  • Loading branch information
zly2006 committed Jul 16, 2024
1 parent a1df8ba commit 879ffc2
Show file tree
Hide file tree
Showing 12 changed files with 178 additions and 19 deletions.
10 changes: 10 additions & 0 deletions src/main/java/com/github/zly2006/enclosure/access/ClientAccess.kt
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?
}
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@
import com.github.zly2006.enclosure.EnclosureArea;
import com.github.zly2006.enclosure.ServerMain;
import com.github.zly2006.enclosure.access.PlayerAccess;
import com.github.zly2006.enclosure.network.config.EnclosureInstalledC2SPacket;
import com.github.zly2006.enclosure.network.play.BackgroundMusicPayload;
import com.github.zly2006.enclosure.utils.Permission;
import com.github.zly2006.enclosure.utils.TrT;
import com.github.zly2006.enclosure.utils.Utils;
import com.github.zly2006.enclosure.utils.UtilsKt;
import com.google.common.collect.Sets;
import com.mojang.authlib.GameProfile;
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.entity.ItemEntity;
import net.minecraft.entity.damage.DamageSource;
import net.minecraft.entity.effect.StatusEffectInstance;
Expand Down Expand Up @@ -217,6 +220,9 @@ private void onTick(CallbackInfo ci) {
EnclosureArea area = ServerMain.INSTANCE.getSmallestEnclosure(getServerWorld(), getBlockPos());
if (lastArea != null) {
if (area != lastArea) {
if (EnclosureInstalledC2SPacket.experimentalFunctionality(player)) {
ServerPlayNetworking.send(player, new BackgroundMusicPayload(null, null));
}
sendFormattedMessage(player, lastArea, false);
}
}
Expand All @@ -233,6 +239,9 @@ private void onTick(CallbackInfo ci) {
}
}
if (area != lastArea) {
if (area.getMusic() != null && EnclosureInstalledC2SPacket.experimentalFunctionality(player)) {
ServerPlayNetworking.send(player, new BackgroundMusicPayload(area.getFullName(), area.getMusic()));
}
sendFormattedMessage(player, area, true);
}
// glowing effect
Expand Down
11 changes: 11 additions & 0 deletions src/main/kotlin/com/github/zly2006/enclosure/EnclosureArea.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import net.fabricmc.fabric.api.networking.v1.PacketByteBufs
import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking
import net.minecraft.nbt.NbtCompound
import net.minecraft.nbt.NbtDouble
import net.minecraft.nbt.NbtElement
import net.minecraft.nbt.NbtList
import net.minecraft.registry.RegistryWrapper
import net.minecraft.server.command.ServerCommandSource
Expand All @@ -20,6 +21,7 @@ import net.minecraft.text.MutableText
import net.minecraft.text.Style
import net.minecraft.text.Text
import net.minecraft.util.Formatting
import net.minecraft.util.Identifier
import net.minecraft.util.math.*
import net.minecraft.world.Heightmap
import net.minecraft.world.PersistentState
Expand Down Expand Up @@ -47,6 +49,7 @@ open class EnclosureArea : PersistentState, EnclosureView {
}
}

var music: Identifier? = null
private var locked = false
final override var minX by lockChecker(0)
final override var minY by lockChecker(0)
Expand Down Expand Up @@ -116,6 +119,11 @@ open class EnclosureArea : PersistentState, EnclosureView {
} else {
UUID.randomUUID()
}
music = if (compound.contains("music", NbtElement.STRING_TYPE.toInt())) {
Identifier.of(compound.getString("music"))
} else {
null
}
}

operator fun Map<String, Boolean>.get(perm: Permission): Boolean? {
Expand Down Expand Up @@ -194,6 +202,9 @@ open class EnclosureArea : PersistentState, EnclosureView {
nbt.put("tp_pos", nbtTpPos)
nbt.putUuid("owner", owner)
nbt.putUuid("uuid", uuid)
if (music != null) {
nbt.putString("music", music.toString())
}
return nbt
}

Expand Down
1 change: 1 addition & 0 deletions src/main/kotlin/com/github/zly2006/enclosure/ServerMain.kt
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ object ServerMain: ModInitializer {
SyncSelectionS2CPacket.register()
SyncPermissionS2CPacket.register()
EnclosureInfoPayload.register()
BackgroundMusicPayload.register()
ServerPlayConnectionEvents.JOIN.register(ServerPlayConnectionEvents.Join { handler: ServerPlayNetworkHandler, _, _ ->
// warn the server ops that this server is running in development mode and not secure.
if (minecraftServer.playerManager.isOperator(handler.player.gameProfile) && commonConfig.developMode) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,6 @@ class BuilderScope<T: argT>(var parent: T) {
}
}

fun optionalEnclosure(action: CommandContext<ServerCommandSource>.(EnclosureArea) -> Unit, builder: (argT, Command<ServerCommandSource>) -> Unit) {
optionalEnclosure(listOf(0), { _, c -> builder(parent, c) }) { a, _ -> action(a) }
}

fun <T: Any?> optionalEnclosure(list: List<T>, builder: BuilderScope<*>.(T, Command<ServerCommandSource>) -> Unit, action: CommandContext<ServerCommandSource>.(EnclosureArea, T) -> Unit) {
list.forEach { t ->
val node = landArgument()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ typealias argT = ArgumentBuilder<ServerCommandSource, *>
@JvmField
val CONSOLE = UUID(0, 0)

fun error(sst: Text, context: CommandContext<ServerCommandSource>): Nothing {
throw SimpleCommandExceptionType(sst).createWithContext(StringReader(context.input))
fun error(text: Text, context: CommandContext<ServerCommandSource>): Nothing {
throw SimpleCommandExceptionType(text).createWithContext(StringReader(context.input))
}

fun getEnclosure(context: CommandContext<ServerCommandSource>): EnclosureArea {
Expand Down Expand Up @@ -210,6 +210,10 @@ fun register(dispatcher: CommandDispatcher<ServerCommandSource>, access: Command
registerSelection()
registerPermissions()
registerMessages(access)

literal("experimental") {
registerMusic()
}
}
return dispatcher.register(node.parent)
}
Expand Down
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}."))
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -139,20 +139,20 @@ fun BuilderScope<*>.registerPermissions() {

literal("trust") {
permission("enclosure.command.trust", BuilderScope.Companion.DefaultPermission.TRUE)
optionalEnclosure({ area ->
val uuid = getOfflineUUID(this)
if (area.hasPerm(source.player!!, Permission.ADMIN)) {
area.setPermission(source, uuid, Permission.TRUSTED, true)
source.sendFeedback(
{ TrT.of("enclosure.message.added_user", Utils.getDisplayNameByUUID(uuid)) },
true
)
} else {
error(Permission.ADMIN.getNoPermissionMsg(source.player), this)
argument(offlinePlayerArgument()) {
optionalEnclosure { area ->
val uuid = getOfflineUUID(this)
if (area.hasPerm(source.player!!, Permission.ADMIN)) {
area.setPermission(source, uuid, Permission.TRUSTED, true)
source.sendFeedback(
{ TrT.of("enclosure.message.added_user", Utils.getDisplayNameByUUID(uuid)) },
true
)
} else {
error(Permission.ADMIN.getNoPermissionMsg(source.player), this)
}
}
}, { node, command ->
node.then(offlinePlayerArgument().executes(command))
})
}
}
literal("give") {
permission("enclosure.command.give", BuilderScope.Companion.DefaultPermission.TRUE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ class EnclosureInstalledC2SPacket(var version: Version?) : CustomPayload {
}
)

@JvmStatic
fun experimentalFunctionality(player: ServerPlayerEntity?): Boolean {
return player != null && installedClientMod.containsKey(player.uuid) && installedClientMod[player.uuid]!! >= MOD_VERSION
}

fun isInstalled(player: ServerPlayerEntity?): Boolean {
if (player == null) return false
return installedClientMod.containsKey(player.uuid)
Expand Down
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
}
1 change: 1 addition & 0 deletions src/main/resources/client.mixins.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"defaultRequire": 1
},
"client": [
"MixinClient",
"MixinServerEntry",
"MixinServerInfo",
"MixinServerPing",
Expand Down

0 comments on commit 879ffc2

Please sign in to comment.