Implemented Bossbar & Sounds Partially

This commit is contained in:
LOOHP
2022-12-08 02:35:37 +00:00
parent 8dd92345d4
commit 9193d907d1
18 changed files with 919 additions and 227 deletions
@@ -48,7 +48,7 @@ public class Channel implements AutoCloseable {
private void ensureOpen() {
if (!valid.get()) {
throw new IllegalStateException("Channel already closed!");
close();
}
}
@@ -106,10 +106,13 @@ public class Channel implements AutoCloseable {
}
@Override
public synchronized void close() throws Exception {
public synchronized void close() {
if (valid.compareAndSet(true, false)) {
input.close();
output.close();
try {
input.close();
output.close();
} catch (Exception ignore) {
}
}
}
@@ -491,7 +491,7 @@ public class ClientConnection extends Thread {
player = new Player(this, username, uuid, Limbo.getInstance().getNextEntityId(), Limbo.getInstance().getServerProperties().getWorldSpawn(), new PlayerInteractManager());
player.setSkinLayers((byte) (0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40));
Limbo.getInstance().addPlayer(player);
Limbo.getInstance().getUnsafe().addPlayer(player);
break;
} else if (packetIn instanceof PacketLoginInPluginMessaging) {
PacketLoginInPluginMessaging response = (PacketLoginInPluginMessaging) packetIn;
@@ -519,7 +519,7 @@ public class ClientConnection extends Thread {
player = new Player(this, data.getUsername(), data.getUuid(), Limbo.getInstance().getNextEntityId(), Limbo.getInstance().getServerProperties().getWorldSpawn(), new PlayerInteractManager());
player.setSkinLayers((byte) (0x01 | 0x02 | 0x04 | 0x08 | 0x10 | 0x20 | 0x40));
Limbo.getInstance().addPlayer(player);
Limbo.getInstance().getUnsafe().addPlayer(player);
break;
}
@@ -757,7 +757,7 @@ public class ClientConnection extends Thread {
state = ClientState.DISCONNECTED;
if (player != null) {
Limbo.getInstance().removePlayer(player);
Limbo.getInstance().getUnsafe().removePlayer(player);
}
Limbo.getInstance().getServerConnection().getClients().remove(this);
running = false;
@@ -0,0 +1,53 @@
/*
* This file is part of Limbo.
*
* Copyright (C) 2022. LoohpJames <jamesloohp@gmail.com>
* Copyright (C) 2022. Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.loohp.limbo.network.protocol.packets;
import com.loohp.limbo.utils.DataTypeIO;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
public class ClientboundSetActionBarTextPacket extends PacketOut {
private Component actionBar;
public ClientboundSetActionBarTextPacket(Component actionBar) {
this.actionBar = actionBar;
}
public Component getActionBar() {
return actionBar;
}
@Override
public byte[] serializePacket() throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
DataOutputStream output = new DataOutputStream(buffer);
output.writeByte(Packet.getPlayOut().get(getClass()));
DataTypeIO.writeString(output, GsonComponentSerializer.gson().serialize(actionBar), StandardCharsets.UTF_8);
return buffer.toByteArray();
}
}
@@ -0,0 +1,117 @@
/*
* This file is part of Limbo.
*
* Copyright (C) 2022. LoohpJames <jamesloohp@gmail.com>
* Copyright (C) 2022. Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.loohp.limbo.network.protocol.packets;
import com.loohp.limbo.bossbar.KeyedBossBar;
import com.loohp.limbo.utils.DataTypeIO;
import net.kyori.adventure.bossbar.BossBar;
import net.kyori.adventure.text.serializer.gson.GsonComponentSerializer;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
public class PacketPlayOutBoss extends PacketOut {
public enum BossBarAction {
ADD,
REMOVE,
UPDATE_PROGRESS,
UPDATE_NAME,
UPDATE_STYLE,
UPDATE_PROPERTIES;
}
private static int encodeProperties(boolean darkenScreen, boolean playMusic, boolean createWorldFog) {
int i = 0;
if (darkenScreen) {
i |= 1;
}
if (playMusic) {
i |= 2;
}
if (createWorldFog) {
i |= 4;
}
return i;
}
private KeyedBossBar bossBar;
private BossBarAction action;
public PacketPlayOutBoss(KeyedBossBar bossBar, BossBarAction action) {
this.bossBar = bossBar;
this.action = action;
}
public KeyedBossBar getBossBar() {
return bossBar;
}
public BossBarAction getAction() {
return action;
}
@Override
public byte[] serializePacket() throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
DataOutputStream output = new DataOutputStream(buffer);
output.writeByte(Packet.getPlayOut().get(getClass()));
DataTypeIO.writeUUID(output, bossBar.getUniqueId());
DataTypeIO.writeVarInt(output, action.ordinal());
BossBar properties = bossBar.getProperties();
switch (action) {
case ADD: {
DataTypeIO.writeString(output, GsonComponentSerializer.gson().serialize(properties.name()), StandardCharsets.UTF_8);
output.writeFloat(properties.progress());
DataTypeIO.writeVarInt(output, properties.color().ordinal());
DataTypeIO.writeVarInt(output, properties.overlay().ordinal());
output.writeByte(encodeProperties(properties.hasFlag(BossBar.Flag.DARKEN_SCREEN), properties.hasFlag(BossBar.Flag.PLAY_BOSS_MUSIC), properties.hasFlag(BossBar.Flag.CREATE_WORLD_FOG)));
break;
}
case REMOVE: {
break;
}
case UPDATE_PROGRESS: {
output.writeFloat(properties.progress());
break;
}
case UPDATE_NAME: {
DataTypeIO.writeString(output, GsonComponentSerializer.gson().serialize(properties.name()), StandardCharsets.UTF_8);
break;
}
case UPDATE_STYLE: {
DataTypeIO.writeVarInt(output, properties.color().ordinal());
DataTypeIO.writeVarInt(output, properties.overlay().ordinal());
break;
}
case UPDATE_PROPERTIES: {
output.writeByte(encodeProperties(properties.hasFlag(BossBar.Flag.DARKEN_SCREEN), properties.hasFlag(BossBar.Flag.PLAY_BOSS_MUSIC), properties.hasFlag(BossBar.Flag.CREATE_WORLD_FOG)));
break;
}
}
return buffer.toByteArray();
}
}
@@ -0,0 +1,112 @@
/*
* This file is part of Limbo.
*
* Copyright (C) 2022. LoohpJames <jamesloohp@gmail.com>
* Copyright (C) 2022. Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.loohp.limbo.network.protocol.packets;
import com.loohp.limbo.sounds.SoundEffect;
import com.loohp.limbo.utils.DataTypeIO;
import net.kyori.adventure.sound.Sound;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Optional;
public class PacketPlayOutNamedSoundEffect extends PacketOut {
private SoundEffect sound;
private Sound.Source source;
private int x;
private int y;
private int z;
private float volume;
private float pitch;
private long seed;
public PacketPlayOutNamedSoundEffect(SoundEffect sound, Sound.Source source, double x, double y, double z, float volume, float pitch, long seed) {
this.sound = sound;
this.source = source;
this.x = (int) (x * 8.0);
this.y = (int) (y * 8.0);
this.z = (int) (z * 8.0);
this.volume = volume;
this.pitch = pitch;
this.seed = seed;
}
public SoundEffect getSound() {
return sound;
}
public Sound.Source getSource() {
return source;
}
public double getX() {
return (float) this.x / 8.0F;
}
public double getY() {
return (float) this.y / 8.0F;
}
public double getZ() {
return (float) this.z / 8.0F;
}
public float getVolume() {
return volume;
}
public float getPitch() {
return pitch;
}
public long getSeed() {
return seed;
}
@Override
public byte[] serializePacket() throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
DataOutputStream output = new DataOutputStream(buffer);
output.writeByte(Packet.getPlayOut().get(getClass()));
DataTypeIO.writeVarInt(output, 0);
DataTypeIO.writeString(output, sound.getSound().toString(), StandardCharsets.UTF_8);
Optional<Float> fixedRange = sound.fixedRange();
if (fixedRange.isPresent()) {
output.writeBoolean(true);
output.writeFloat(fixedRange.get());
} else {
output.writeBoolean(false);
}
DataTypeIO.writeVarInt(output, source.ordinal());
output.writeInt(x);
output.writeInt(y);
output.writeInt(z);
output.writeFloat(volume);
output.writeFloat(pitch);
output.writeLong(seed);
return buffer.toByteArray();
}
}
@@ -0,0 +1,74 @@
/*
* This file is part of Limbo.
*
* Copyright (C) 2022. LoohpJames <jamesloohp@gmail.com>
* Copyright (C) 2022. Contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.loohp.limbo.network.protocol.packets;
import com.loohp.limbo.utils.DataTypeIO;
import com.loohp.limbo.utils.NamespacedKey;
import net.kyori.adventure.sound.Sound;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
public class PacketPlayOutStopSound extends PacketOut {
private NamespacedKey sound;
private Sound.Source source;
public PacketPlayOutStopSound(NamespacedKey sound, Sound.Source source) {
this.sound = sound;
this.source = source;
}
public NamespacedKey getSound() {
return sound;
}
public Sound.Source getSource() {
return source;
}
@Override
public byte[] serializePacket() throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
DataOutputStream output = new DataOutputStream(buffer);
output.writeByte(Packet.getPlayOut().get(getClass()));
if (source != null) {
if (sound != null) {
output.writeByte(3);
DataTypeIO.writeVarInt(output, source.ordinal());
DataTypeIO.writeString(output, sound.toString(), StandardCharsets.UTF_8);
} else {
output.writeByte(1);
DataTypeIO.writeVarInt(output, source.ordinal());
}
} else if (sound != null) {
output.writeByte(2);
DataTypeIO.writeString(output, sound.toString(), StandardCharsets.UTF_8);
} else {
output.writeByte(0);
}
return buffer.toByteArray();
}
}