Skip to content

Commit

Permalink
声音系统的部分修正
Browse files Browse the repository at this point in the history
  • Loading branch information
TartaricAcid committed Oct 19, 2023
1 parent fea28f1 commit 2b2829c
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@
import static com.github.tartaricacid.touhoulittlemaid.TouhouLittleMaid.LOGGER;

public class CustomSoundLoader {
public static final Map<String, SoundCache> CACHE = Maps.newHashMap();
private static final Pattern FILENAME_REG = Pattern.compile("^\\d+\\.ogg$");
private static final Marker MARKER = MarkerManager.getMarker("CustomSoundLoader");
private static final String JSON_FILE_NAME = "maid_sound.json";
private static final Map<String, SoundCache> CACHE = Maps.newHashMap();

public static void clear() {
CACHE.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,13 @@ public SoundPackInfo getInfo() {

public SoundBuffer getBuffer(ResourceLocation soundEvent) {
List<SoundBuffer> soundBuffers = buffers.get(soundEvent);
if (soundBuffers != null && soundBuffers.size() > 0) {
if (soundBuffers != null && !soundBuffers.isEmpty()) {
return soundBuffers.get(RANDOM.nextInt(soundBuffers.size()));
}
return null;
}

public Map<ResourceLocation, List<SoundBuffer>> getBuffers() {
return buffers;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.google.gson.annotations.SerializedName;
import net.minecraft.resources.ResourceLocation;

import javax.annotation.Nullable;
import java.util.Collections;
import java.util.List;

Expand All @@ -29,6 +30,7 @@ public class SoundPackInfo {
@SerializedName("url")
private String url;

@Nullable
public String getDate() {
return date;
}
Expand All @@ -41,18 +43,22 @@ public List<String> getAuthor() {
return author;
}

@Nullable
public String getDescription() {
return description;
}

@Nullable
public String getVersion() {
return version;
}

@Nullable
public ResourceLocation getIcon() {
return icon;
}

@Nullable
public String getUrl() {
return url;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public static void init() {
Optional.of(NetworkDirection.PLAY_TO_CLIENT));
CHANNEL.registerMessage(21, PlayMaidSoundMessage.class, PlayMaidSoundMessage::encode, PlayMaidSoundMessage::decode, PlayMaidSoundMessage::handle,
Optional.of(NetworkDirection.PLAY_TO_CLIENT));
CHANNEL.registerMessage(22, SetMaidSoundIdMessage.class, SetMaidSoundIdMessage::encode, SetMaidSoundIdMessage::decode, SetMaidSoundIdMessage::handle,
Optional.of(NetworkDirection.PLAY_TO_SERVER));
}

public static void sendToClientPlayer(Object message, Player player) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.github.tartaricacid.touhoulittlemaid.network.message;

import com.github.tartaricacid.touhoulittlemaid.entity.passive.EntityMaid;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.Entity;
import net.minecraftforge.network.NetworkEvent;

import java.util.function.Supplier;

public class SetMaidSoundIdMessage {
private final int entityId;
private final String soundId;

public SetMaidSoundIdMessage(int entityId, String soundId) {
this.entityId = entityId;
this.soundId = soundId;
}

public static void encode(SetMaidSoundIdMessage message, FriendlyByteBuf buf) {
buf.writeInt(message.entityId);
buf.writeUtf(message.soundId);
}

public static SetMaidSoundIdMessage decode(FriendlyByteBuf buf) {
return new SetMaidSoundIdMessage(buf.readInt(), buf.readUtf());
}

public static void handle(SetMaidSoundIdMessage message, Supplier<NetworkEvent.Context> contextSupplier) {
NetworkEvent.Context context = contextSupplier.get();
if (context.getDirection().getReceptionSide().isServer()) {
context.enqueueWork(() -> {
ServerPlayer sender = context.getSender();
if (sender == null) {
return;
}
Entity entity = sender.level.getEntity(message.entityId);
if (entity instanceof EntityMaid maid && maid.isOwnedBy(sender)) {
maid.setSoundPackId(message.soundId);
}
});
}
context.setPacketHandled(true);
}
}

0 comments on commit 2b2829c

Please sign in to comment.