From 2c40453ff5ba045d0de59dcd002c701a968d81be Mon Sep 17 00:00:00 2001 From: Tsubasa6848 <116721335+Tsubasa6848@users.noreply.github.com> Date: Sat, 17 Feb 2024 21:26:52 +0800 Subject: [PATCH 1/2] feat: add update FloatingText --- include/GMLIB/Server/FloatingTextAPI.h | 12 ++++-- manifest.json | 2 +- src/Server/ActorAPI.cc | 4 +- src/Server/FloatingTextAPI.cc | 58 +++++++++++++++++++++++--- src/Version.h | 2 +- tooth.json | 4 +- 6 files changed, 66 insertions(+), 16 deletions(-) diff --git a/include/GMLIB/Server/FloatingTextAPI.h b/include/GMLIB/Server/FloatingTextAPI.h index fa18ad7..702864f 100644 --- a/include/GMLIB/Server/FloatingTextAPI.h +++ b/include/GMLIB/Server/FloatingTextAPI.h @@ -25,8 +25,6 @@ class FloatingText { GMLIB_API virtual ~FloatingText(); public: - GMLIB_API int64_t getFloatingTextRuntimeId(); - GMLIB_API void sendToClient(Player* pl); GMLIB_API void sendToAllClients(); @@ -35,9 +33,15 @@ class FloatingText { GMLIB_API void removeFromAllClients(); + GMLIB_API void updateAllClients(); + + GMLIB_API void updateClient(Player* pl); + GMLIB_API void setText(std::string newText); - GMLIB_API int64 getRuntimeID(); + GMLIB_API void setPosition(Vec3& pos, DimensionType dimid); + + GMLIB_API int64_t getRuntimeID(); GMLIB_API std::string getText(); @@ -46,4 +50,4 @@ class FloatingText { GMLIB_API DimensionType getDimensionId(); }; -} \ No newline at end of file +} // namespace GMLIB::Server \ No newline at end of file diff --git a/manifest.json b/manifest.json index dfb59b3..e6a031b 100644 --- a/manifest.json +++ b/manifest.json @@ -4,5 +4,5 @@ "type": "native", "author": "GroupMountain", "description": "Group Mountain Library", - "version": "0.8.2" + "version": "0.8.3" } \ No newline at end of file diff --git a/src/Server/ActorAPI.cc b/src/Server/ActorAPI.cc index d43c08d..1945287 100644 --- a/src/Server/ActorAPI.cc +++ b/src/Server/ActorAPI.cc @@ -142,9 +142,9 @@ GMLIB_Actor* GMLIB_Actor::shootProjectile(std::string typeName, float speed, flo } int64_t GMLIB_Actor::getNextActorUniqueID() { - auto res = ll::service::getLevel()->getRandom().nextLong(); + auto res = Random::getThreadLocal().nextLong(); while (ll::service::getLevel()->fetchEntity(ActorUniqueID(res)) != nullptr) { - res = ll::service::getLevel()->getRandom().nextLong(); + res = Random::getThreadLocal().nextLong(); } return res; } diff --git a/src/Server/FloatingTextAPI.cc b/src/Server/FloatingTextAPI.cc index 9471499..b165380 100644 --- a/src/Server/FloatingTextAPI.cc +++ b/src/Server/FloatingTextAPI.cc @@ -1,21 +1,31 @@ #include "Global.h" #include "mc/world/item/NetworkItemStackDescriptor.h" -#include #include #include +#include #include #include #include +#include +#include namespace GMLIB::Server { std::unordered_map mRuntimeFloatingTextList; +int getNextFloatingTextId() { + auto id = Random::getThreadLocal().nextInt(0, 2147473647); + while (ll::service::getLevel()->fetchEntity(ActorUniqueID(id))) { + id = Random::getThreadLocal().nextInt(0, 2147473647); + } + return id; +} + FloatingText::FloatingText(std::string text, Vec3 position, DimensionType dimensionId) : mText(text), mPosition(position), mDimensionId(dimensionId) { - mRuntimeId = GMLIB_Actor::getNextActorUniqueID(); + mRuntimeId = getNextFloatingTextId(); mRuntimeFloatingTextList.insert({mRuntimeId, this}); } @@ -24,9 +34,12 @@ FloatingText::~FloatingText() { mRuntimeFloatingTextList.erase(mRuntimeId); } -int64_t FloatingText::getFloatingTextRuntimeId() { return mRuntimeId; } +void FloatingText::setPosition(Vec3& pos, DimensionType dimid) { + mPosition = pos; + mDimensionId = dimid; +} -GMLIB::Server::NetworkPacket<15> createFloatingTextPacket(FloatingText* ft) { +GMLIB::Server::NetworkPacket<15> createAddFloatingTextPacket(FloatingText* ft) { auto item = std::make_unique(ItemStack{"minecraft:air"}); auto nisd = NetworkItemStackDescriptor(*item); GMLIB_BinaryStream bs; @@ -50,13 +63,13 @@ GMLIB::Server::NetworkPacket<15> createFloatingTextPacket(FloatingText* ft) { void FloatingText::sendToClient(Player* pl) { if (!pl->isSimulatedPlayer() && pl->getDimensionId() == mDimensionId) { - auto pkt = createFloatingTextPacket(this); + auto pkt = createAddFloatingTextPacket(this); pkt.sendTo(*pl); } } void FloatingText::sendToAllClients() { - auto pkt = createFloatingTextPacket(this); + auto pkt = createAddFloatingTextPacket(this); ll::service::getLevel()->forEachPlayer([&](Player& pl) -> bool { if (!pl.isSimulatedPlayer() && pl.getDimensionId() == mDimensionId) { pkt.sendTo(pl); @@ -91,6 +104,39 @@ bool FloatingText::deleteFloatingText(int64 runtimeId) { return false; } +GMLIB::Server::NetworkPacket<39> createUpdateFloatingTextPacket(FloatingText* ft) { + GMLIB_BinaryStream bs; + bs.writeUnsignedVarInt64(ft->getRuntimeID()); + bs.writeUnsignedVarInt(2); + bs.writeUnsignedVarInt((uint)0x4); + bs.writeUnsignedVarInt((uint)0x4); + bs.writeString(ft->getText()); + bs.writeUnsignedVarInt((uint)0x51); + bs.writeUnsignedVarInt((uint)0x0); + bs.writeUnsignedVarInt(0); + bs.writeUnsignedVarInt(0); + bs.writeUnsignedVarInt64(0); + GMLIB::Server::NetworkPacket<(int)MinecraftPacketIds::SetActorData> pkt(bs.getAndReleaseData()); + return pkt; +} + +void FloatingText::updateAllClients() { + auto pkt = createUpdateFloatingTextPacket(this); + ll::service::getLevel()->forEachPlayer([&](Player& pl) -> bool { + if (!pl.isSimulatedPlayer() && pl.getDimensionId() == mDimensionId) { + pkt.sendTo(pl); + } + return true; + }); +} + +void FloatingText::updateClient(Player* pl) { + if (!pl->isSimulatedPlayer() && pl->getDimensionId() == mDimensionId) { + auto pkt = createUpdateFloatingTextPacket(this); + pkt.sendTo(*pl); + } +} + int64 FloatingText::getRuntimeID() { return mRuntimeId; } std::string FloatingText::getText() { return mText; } diff --git a/src/Version.h b/src/Version.h index c6fe56e..b942456 100644 --- a/src/Version.h +++ b/src/Version.h @@ -4,7 +4,7 @@ #define LIB_VERSION_MAJOR 0 #define LIB_VERSION_MINOR 8 -#define LIB_VERSION_REVISION 2 +#define LIB_VERSION_REVISION 3 #define LIB_VERSION_PRERELEASE "" #define LIB_VERSION_BUILD_META "" diff --git a/tooth.json b/tooth.json index 23d4b46..822bc81 100644 --- a/tooth.json +++ b/tooth.json @@ -1,7 +1,7 @@ { "format_version": 2, "tooth": "github.com/GroupMountain/GMLIB", - "version": "0.8.2", + "version": "0.8.3", "info": { "name": "GMLIB", "description": "Group Mountain Library", @@ -12,7 +12,7 @@ "Library" ] }, - "asset_url": "https://github.com/GroupMountain/GMLIB/releases/download/v0.8.2/GMLIB-windows-x64.zip", + "asset_url": "https://github.com/GroupMountain/GMLIB/releases/download/v0.8.3/GMLIB-windows-x64.zip", "files": { "place": [ { From 77842b0b7f03a28f952f9aefa5f1c2bcb0bd67a9 Mon Sep 17 00:00:00 2001 From: Tsubasa6848 <116721335+Tsubasa6848@users.noreply.github.com> Date: Sat, 17 Feb 2024 22:02:38 +0800 Subject: [PATCH 2/2] feat: add broadcast --- include/GMLIB/Server/FloatingTextAPI.h | 4 ---- include/GMLIB/Server/LevelAPI.h | 4 ++++ include/GMLIB/Server/PlayerAPI.h | 2 ++ src/Server/FloatingTextAPI.cc | 33 -------------------------- src/Server/LevelAPI.cc | 9 +++++++ src/Server/PlayerAPI.cc | 6 +++++ 6 files changed, 21 insertions(+), 37 deletions(-) diff --git a/include/GMLIB/Server/FloatingTextAPI.h b/include/GMLIB/Server/FloatingTextAPI.h index 702864f..36a8c46 100644 --- a/include/GMLIB/Server/FloatingTextAPI.h +++ b/include/GMLIB/Server/FloatingTextAPI.h @@ -33,10 +33,6 @@ class FloatingText { GMLIB_API void removeFromAllClients(); - GMLIB_API void updateAllClients(); - - GMLIB_API void updateClient(Player* pl); - GMLIB_API void setText(std::string newText); GMLIB_API void setPosition(Vec3& pos, DimensionType dimid); diff --git a/include/GMLIB/Server/LevelAPI.h b/include/GMLIB/Server/LevelAPI.h index 12ff2ae..e464239 100644 --- a/include/GMLIB/Server/LevelAPI.h +++ b/include/GMLIB/Server/LevelAPI.h @@ -46,6 +46,10 @@ class GMLIB_Level : public Level { GMLIB_API static std::map getAllExperimentsTranslateKeys(); + GMLIB_API static void broadcast(std::string_view message); + + GMLIB_API static void broadcastToast(std::string_view title, std::string_view message); + public: GMLIB_API BlockSource* getBlockSource(DimensionType dimid); diff --git a/include/GMLIB/Server/PlayerAPI.h b/include/GMLIB/Server/PlayerAPI.h index 8bfdfb6..e1f226b 100644 --- a/include/GMLIB/Server/PlayerAPI.h +++ b/include/GMLIB/Server/PlayerAPI.h @@ -145,6 +145,8 @@ class GMLIB_Player : public Player { GMLIB_API void setClientWeather(WeatherType weather); + GMLIB_API void sendToast(std::string_view title, std::string_view message); + GMLIB_API void addEffect( MobEffect::EffectType effectType, int duration = 600, diff --git a/src/Server/FloatingTextAPI.cc b/src/Server/FloatingTextAPI.cc index b165380..109457d 100644 --- a/src/Server/FloatingTextAPI.cc +++ b/src/Server/FloatingTextAPI.cc @@ -104,39 +104,6 @@ bool FloatingText::deleteFloatingText(int64 runtimeId) { return false; } -GMLIB::Server::NetworkPacket<39> createUpdateFloatingTextPacket(FloatingText* ft) { - GMLIB_BinaryStream bs; - bs.writeUnsignedVarInt64(ft->getRuntimeID()); - bs.writeUnsignedVarInt(2); - bs.writeUnsignedVarInt((uint)0x4); - bs.writeUnsignedVarInt((uint)0x4); - bs.writeString(ft->getText()); - bs.writeUnsignedVarInt((uint)0x51); - bs.writeUnsignedVarInt((uint)0x0); - bs.writeUnsignedVarInt(0); - bs.writeUnsignedVarInt(0); - bs.writeUnsignedVarInt64(0); - GMLIB::Server::NetworkPacket<(int)MinecraftPacketIds::SetActorData> pkt(bs.getAndReleaseData()); - return pkt; -} - -void FloatingText::updateAllClients() { - auto pkt = createUpdateFloatingTextPacket(this); - ll::service::getLevel()->forEachPlayer([&](Player& pl) -> bool { - if (!pl.isSimulatedPlayer() && pl.getDimensionId() == mDimensionId) { - pkt.sendTo(pl); - } - return true; - }); -} - -void FloatingText::updateClient(Player* pl) { - if (!pl->isSimulatedPlayer() && pl->getDimensionId() == mDimensionId) { - auto pkt = createUpdateFloatingTextPacket(this); - pkt.sendTo(*pl); - } -} - int64 FloatingText::getRuntimeID() { return mRuntimeId; } std::string FloatingText::getText() { return mText; } diff --git a/src/Server/LevelAPI.cc b/src/Server/LevelAPI.cc index d8c197d..b9a8100 100644 --- a/src/Server/LevelAPI.cc +++ b/src/Server/LevelAPI.cc @@ -6,6 +6,8 @@ #include #include #include +#include +#include #include #include #include @@ -348,6 +350,13 @@ void GMLIB_Level::setClientWeather(WeatherType weather) { } } +void GMLIB_Level::broadcast(std::string_view message) { TextPacket::createRawMessage(message).sendToClients(); } + +void GMLIB_Level::broadcastToast(std::string_view title, std::string_view message) { + auto pkt = ToastRequestPacket(std::string(title), std::string(message)); + pkt.sendToClients(); +} + std::optional GMLIB_Level::getGameruleBool(GameRuleId id) { auto rule = ll::service::bedrock::getLevel()->getGameRules().getRule(id); if (rule) { diff --git a/src/Server/PlayerAPI.cc b/src/Server/PlayerAPI.cc index 809dcf3..4e0bd4e 100644 --- a/src/Server/PlayerAPI.cc +++ b/src/Server/PlayerAPI.cc @@ -12,6 +12,7 @@ #include #include #include +#include #include #include #include @@ -330,6 +331,11 @@ void GMLIB_Player::updateClientBossbar( void GMLIB_Player::setClientWeather(WeatherType weather) { return GMLIB_Level::setClientWeather(weather, this); } +void GMLIB_Player::sendToast(std::string_view title, std::string_view message) { + auto pkt = ToastRequestPacket(std::string(title), std::string(message)); + pkt.sendTo(*this); +} + void GMLIB_Player::addEffect( MobEffect::EffectType effectType, int duration,