Sound & header/footer

This commit is contained in:
Felix Cravic 2020-04-10 13:39:22 +02:00
parent 66572db705
commit c8ebe32116
8 changed files with 202 additions and 1 deletions

View File

@ -96,6 +96,8 @@ public class PlayerInit {
player.setGameMode(GameMode.CREATIVE);
player.teleport(new Position(0, 66, 0));
player.sendHeaderFooter("Its the header", "Its the footer", '&');
/*Random random = new Random();
for (int i = 0; i < 50; i++) {
ChickenCreature chickenCreature = new ChickenCreature();

View File

@ -20,6 +20,8 @@ import fr.themode.minestom.net.packet.server.play.*;
import fr.themode.minestom.net.player.PlayerConnection;
import fr.themode.minestom.scoreboard.BelowNameScoreboard;
import fr.themode.minestom.scoreboard.Team;
import fr.themode.minestom.sound.Sound;
import fr.themode.minestom.sound.SoundCategory;
import fr.themode.minestom.utils.ArrayUtils;
import fr.themode.minestom.utils.BlockPosition;
import fr.themode.minestom.utils.ChunkUtils;
@ -365,10 +367,39 @@ public class Player extends LivingEntity {
sendMessage(textObject.toJson());
}
public void playSound(Sound sound, SoundCategory soundCategory, int x, int y, int z, float volume, float pitch) {
SoundEffectPacket soundEffectPacket = new SoundEffectPacket();
soundEffectPacket.soundId = sound.getId();
soundEffectPacket.soundCategory = soundCategory;
soundEffectPacket.x = x;
soundEffectPacket.y = y;
soundEffectPacket.z = z;
soundEffectPacket.volume = volume;
soundEffectPacket.pitch = pitch;
playerConnection.sendPacket(soundEffectPacket);
}
public void stopSound() {
StopSoundPacket stopSoundPacket = new StopSoundPacket();
stopSoundPacket.flags = 0x00;
playerConnection.sendPacket(stopSoundPacket);
}
public void sendHeaderFooter(String header, String footer, char colorChar) {
PlayerListHeaderAndFooterPacket playerListHeaderAndFooterPacket = new PlayerListHeaderAndFooterPacket();
playerListHeaderAndFooterPacket.emptyHeader = header == null;
playerListHeaderAndFooterPacket.emptyFooter = footer == null;
playerListHeaderAndFooterPacket.header = Chat.legacyText(header, colorChar).toJson().toString();
playerListHeaderAndFooterPacket.footer = Chat.legacyText(footer, colorChar).toJson().toString();
playerConnection.sendPacket(playerListHeaderAndFooterPacket);
}
public void sendActionBarMessage(String message, char colorChar) {
TitlePacket titlePacket = new TitlePacket();
titlePacket.action = TitlePacket.Action.SET_ACTION_BAR;
titlePacket.actionBarText = Chat.legacyText(message, colorChar).toString();
titlePacket.actionBarText = Chat.legacyText(message, colorChar).toJson().toString();
playerConnection.sendPacket(titlePacket);
}

View File

@ -0,0 +1,29 @@
package fr.themode.minestom.net.packet.server.play;
import fr.themode.minestom.net.packet.PacketWriter;
import fr.themode.minestom.net.packet.server.ServerPacket;
import fr.themode.minestom.net.packet.server.ServerPacketIdentifier;
import fr.themode.minestom.sound.SoundCategory;
public class EntitySoundEffect implements ServerPacket {
public int soundId;
public SoundCategory soundCategory;
public int entityId;
public float volume;
public float pitch;
@Override
public void write(PacketWriter writer) {
writer.writeVarInt(soundId);
writer.writeVarInt(soundCategory.ordinal());
writer.writeVarInt(entityId);
writer.writeFloat(volume);
writer.writeFloat(pitch);
}
@Override
public int getId() {
return ServerPacketIdentifier.ENTITY_SOUND_EFFECT;
}
}

View File

@ -0,0 +1,31 @@
package fr.themode.minestom.net.packet.server.play;
import fr.themode.minestom.net.packet.PacketWriter;
import fr.themode.minestom.net.packet.server.ServerPacket;
import fr.themode.minestom.net.packet.server.ServerPacketIdentifier;
import fr.themode.minestom.sound.SoundCategory;
public class NamedSoundEffectPacket implements ServerPacket {
public String soundName;
public SoundCategory soundCategory;
public int x, y, z;
public float volume;
public float pitch;
@Override
public void write(PacketWriter writer) {
writer.writeSizedString(soundName);
writer.writeVarInt(soundCategory.ordinal());
writer.writeInt(x);
writer.writeInt(y);
writer.writeInt(z);
writer.writeFloat(volume);
writer.writeFloat(pitch);
}
@Override
public int getId() {
return ServerPacketIdentifier.NAMED_SOUND_EFFECT;
}
}

View File

@ -0,0 +1,37 @@
package fr.themode.minestom.net.packet.server.play;
import fr.themode.minestom.net.packet.PacketWriter;
import fr.themode.minestom.net.packet.server.ServerPacket;
import fr.themode.minestom.net.packet.server.ServerPacketIdentifier;
public class PlayerListHeaderAndFooterPacket implements ServerPacket {
private static final String EMPTY_COMPONENT = "{\"translate\":\"\"}";
public boolean emptyHeader;
public boolean emptyFooter;
public String header;
public String footer;
@Override
public void write(PacketWriter writer) {
if (emptyHeader) {
writer.writeSizedString(EMPTY_COMPONENT);
} else {
writer.writeSizedString(header);
}
if (emptyFooter) {
writer.writeSizedString(EMPTY_COMPONENT);
} else {
writer.writeSizedString(footer);
}
}
@Override
public int getId() {
return ServerPacketIdentifier.PLAYER_LIST_HEADER_AND_FOOTER;
}
}

View File

@ -0,0 +1,31 @@
package fr.themode.minestom.net.packet.server.play;
import fr.themode.minestom.net.packet.PacketWriter;
import fr.themode.minestom.net.packet.server.ServerPacket;
import fr.themode.minestom.net.packet.server.ServerPacketIdentifier;
import fr.themode.minestom.sound.SoundCategory;
public class SoundEffectPacket implements ServerPacket {
public int soundId;
public SoundCategory soundCategory;
public int x, y, z;
public float volume;
public float pitch;
@Override
public void write(PacketWriter writer) {
writer.writeVarInt(soundId);
writer.writeVarInt(soundCategory.ordinal());
writer.writeInt(x);
writer.writeInt(y);
writer.writeInt(z);
writer.writeFloat(volume);
writer.writeFloat(pitch);
}
@Override
public int getId() {
return ServerPacketIdentifier.SOUND_EFFECT;
}
}

View File

@ -0,0 +1,26 @@
package fr.themode.minestom.net.packet.server.play;
import fr.themode.minestom.net.packet.PacketWriter;
import fr.themode.minestom.net.packet.server.ServerPacket;
import fr.themode.minestom.net.packet.server.ServerPacketIdentifier;
public class StopSoundPacket implements ServerPacket {
public byte flags;
public int source;
public String sound;
@Override
public void write(PacketWriter writer) {
writer.writeByte(flags);
if (flags == 3 || flags == 1)
writer.writeVarInt(source);
if (flags == 2 || flags == 3)
writer.writeSizedString(sound);
}
@Override
public int getId() {
return ServerPacketIdentifier.STOP_SOUND;
}
}

View File

@ -0,0 +1,14 @@
package fr.themode.minestom.sound;
public enum SoundCategory {
MASTER,
MUSIC,
RECORDS,
WEATHER,
BLOCKS,
HOSTILE,
NEUTRAL,
PLAYERS,
AMBIENT,
VOICE
}