Skip to content

Commit

Permalink
feat: add PlayerRespawnEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Jan 22, 2025
1 parent 3a54aab commit f4fd821
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 14 deletions.
9 changes: 8 additions & 1 deletion endstone/_internal/endstone_python.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import numpy
import os
import typing
import uuid
__all__ = ['ActionForm', 'Actor', 'ActorDeathEvent', 'ActorEvent', 'ActorExplodeEvent', 'ActorKnockbackEvent', 'ActorRemoveEvent', 'ActorSpawnEvent', 'ActorTeleportEvent', 'BanEntry', 'BarColor', 'BarFlag', 'BarStyle', 'Block', 'BlockBreakEvent', 'BlockData', 'BlockEvent', 'BlockFace', 'BlockPlaceEvent', 'BlockState', 'BossBar', 'BroadcastMessageEvent', 'Cancellable', 'ColorFormat', 'Command', 'CommandExecutor', 'CommandSender', 'CommandSenderWrapper', 'ConsoleCommandSender', 'Criteria', 'Dimension', 'DisplaySlot', 'Dropdown', 'Event', 'EventPriority', 'GameMode', 'Inventory', 'IpBanEntry', 'IpBanList', 'ItemStack', 'Label', 'Language', 'Level', 'Location', 'Logger', 'MessageForm', 'Mob', 'ModalForm', 'Objective', 'ObjectiveSortOrder', 'OfflinePlayer', 'Packet', 'PacketType', 'Permissible', 'Permission', 'PermissionAttachment', 'PermissionAttachmentInfo', 'PermissionDefault', 'Player', 'PlayerBanEntry', 'PlayerBanList', 'PlayerChatEvent', 'PlayerCommandEvent', 'PlayerDeathEvent', 'PlayerEmoteEvent', 'PlayerEvent', 'PlayerGameModeChangeEvent', 'PlayerInteractActorEvent', 'PlayerInteractEvent', 'PlayerInventory', 'PlayerJoinEvent', 'PlayerKickEvent', 'PlayerLoginEvent', 'PlayerQuitEvent', 'PlayerTeleportEvent', 'Plugin', 'PluginCommand', 'PluginDescription', 'PluginDisableEvent', 'PluginEnableEvent', 'PluginLoadOrder', 'PluginLoader', 'PluginManager', 'Position', 'RenderType', 'Scheduler', 'Score', 'Scoreboard', 'ScriptMessageEvent', 'Server', 'ServerCommandEvent', 'ServerEvent', 'ServerListPingEvent', 'ServerLoadEvent', 'Skin', 'Slider', 'SocketAddress', 'SpawnParticleEffectPacket', 'StepSlider', 'Task', 'TextInput', 'ThunderChangeEvent', 'Toggle', 'Translatable', 'Vector', 'WeatherChangeEvent', 'WeatherEvent']
__all__ = ['ActionForm', 'Actor', 'ActorDeathEvent', 'ActorEvent', 'ActorExplodeEvent', 'ActorKnockbackEvent', 'ActorRemoveEvent', 'ActorSpawnEvent', 'ActorTeleportEvent', 'BanEntry', 'BarColor', 'BarFlag', 'BarStyle', 'Block', 'BlockBreakEvent', 'BlockData', 'BlockEvent', 'BlockFace', 'BlockPlaceEvent', 'BlockState', 'BossBar', 'BroadcastMessageEvent', 'Cancellable', 'ColorFormat', 'Command', 'CommandExecutor', 'CommandSender', 'CommandSenderWrapper', 'ConsoleCommandSender', 'Criteria', 'Dimension', 'DisplaySlot', 'Dropdown', 'Event', 'EventPriority', 'GameMode', 'Inventory', 'IpBanEntry', 'IpBanList', 'ItemStack', 'Label', 'Language', 'Level', 'Location', 'Logger', 'MessageForm', 'Mob', 'ModalForm', 'Objective', 'ObjectiveSortOrder', 'OfflinePlayer', 'Packet', 'PacketType', 'Permissible', 'Permission', 'PermissionAttachment', 'PermissionAttachmentInfo', 'PermissionDefault', 'Player', 'PlayerBanEntry', 'PlayerBanList', 'PlayerChatEvent', 'PlayerCommandEvent', 'PlayerDeathEvent', 'PlayerEmoteEvent', 'PlayerEvent', 'PlayerGameModeChangeEvent', 'PlayerInteractActorEvent', 'PlayerInteractEvent', 'PlayerInventory', 'PlayerJoinEvent', 'PlayerKickEvent', 'PlayerLoginEvent', 'PlayerQuitEvent', 'PlayerRespawnEvent', 'PlayerTeleportEvent', 'Plugin', 'PluginCommand', 'PluginDescription', 'PluginDisableEvent', 'PluginEnableEvent', 'PluginLoadOrder', 'PluginLoader', 'PluginManager', 'Position', 'RenderType', 'Scheduler', 'Score', 'Scoreboard', 'ScriptMessageEvent', 'Server', 'ServerCommandEvent', 'ServerEvent', 'ServerListPingEvent', 'ServerLoadEvent', 'Skin', 'Slider', 'SocketAddress', 'SpawnParticleEffectPacket', 'StepSlider', 'Task', 'TextInput', 'ThunderChangeEvent', 'Toggle', 'Translatable', 'Vector', 'WeatherChangeEvent', 'WeatherEvent']
class ActionForm:
"""
Represents a form with buttons that let the player take action.
Expand Down Expand Up @@ -2615,6 +2615,13 @@ class PlayerQuitEvent(PlayerEvent):
@quit_message.setter
def quit_message(self, arg1: str) -> None:
...
class PlayerRespawnEvent(PlayerEvent):
"""
Called when a player respawns.
"""
@staticmethod
def _pybind11_conduit_v1_(*args, **kwargs):
...
class PlayerTeleportEvent(PlayerEvent, Cancellable):
"""
Called when a player is teleported from one location to another.
Expand Down
4 changes: 3 additions & 1 deletion endstone/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
PlayerKickEvent,
PlayerLoginEvent,
PlayerQuitEvent,
PlayerRespawnEvent,
PlayerTeleportEvent,
PluginDisableEvent,
PluginEnableEvent,
Expand All @@ -45,7 +46,6 @@
"ActorEvent",
"ActorExplodeEvent",
"ActorDeathEvent",
"PlayerEmoteEvent",
"ActorKnockbackEvent",
"ActorRemoveEvent",
"ActorSpawnEvent",
Expand All @@ -58,13 +58,15 @@
"PlayerChatEvent",
"PlayerCommandEvent",
"PlayerDeathEvent",
"PlayerEmoteEvent",
"PlayerGameModeChangeEvent",
"PlayerInteractEvent",
"PlayerInteractActorEvent",
"PlayerJoinEvent",
"PlayerKickEvent",
"PlayerLoginEvent",
"PlayerQuitEvent",
"PlayerRespawnEvent",
"PlayerTeleportEvent",
"BroadcastMessageEvent",
"PluginEnableEvent",
Expand Down
1 change: 1 addition & 0 deletions include/endstone/endstone.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ static_assert(_ITERATOR_DEBUG_LEVEL == 0,
#include "event/player/player_kick_event.h"
#include "event/player/player_login_event.h"
#include "event/player/player_quit_event.h"
#include "event/player/player_respawn_event.h"
#include "event/player/player_teleport_event.h"
#include "event/server/broadcast_message_event.h"
#include "event/server/plugin_disable_event.h"
Expand Down
1 change: 0 additions & 1 deletion include/endstone/event/player/player_emote_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

#pragma once

#include "endstone/event/cancellable.h"
#include "endstone/event/player/player_event.h"

namespace endstone {
Expand Down
36 changes: 36 additions & 0 deletions include/endstone/event/player/player_respawn_event.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright (c) 2024, The Endstone Project. (https://endstone.dev) All Rights Reserved.
//
// 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.

#pragma once

#include "endstone/event/player/player_event.h"

namespace endstone {

/**
* @brief Called when a player respawns.
*/
class PlayerRespawnEvent : public PlayerEvent {
public:
explicit PlayerRespawnEvent(Player &player) : PlayerEvent(player) {}
~PlayerRespawnEvent() override = default;

inline static const std::string NAME = "PlayerRespawnEvent";
[[nodiscard]] std::string getEventName() const override
{
return NAME;
}
};

} // namespace endstone
34 changes: 23 additions & 11 deletions src/endstone/core/event/handlers/player_gameplay_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "endstone/event/player/player_game_mode_change_event.h"
#include "endstone/event/player/player_interact_actor_event.h"
#include "endstone/event/player/player_interact_event.h"
#include "endstone/event/player/player_respawn_event.h"

namespace endstone::core {

Expand Down Expand Up @@ -57,7 +58,8 @@ GameplayHandlerResult<CoordinatorResult> EndstonePlayerGameplayHandler::handleEv
using T = std::decay_t<decltype(arg)>;
if constexpr (std::is_same_v<T, Details::ValueOrRef<const PlayerInteractWithBlockBeforeEvent>> ||
std::is_same_v<T, Details::ValueOrRef<const PlayerInteractWithEntityBeforeEvent>> ||
std::is_same_v<T, Details::ValueOrRef<const PlayerEmoteEvent>>) {
std::is_same_v<T, Details::ValueOrRef<const ::PlayerRespawnEvent>> ||
std::is_same_v<T, Details::ValueOrRef<const ::PlayerEmoteEvent>>) {
if (!handleEvent(arg.value())) {
return {HandlerResult::BypassListeners, CoordinatorResult::Cancel};
}
Expand Down Expand Up @@ -98,6 +100,26 @@ bool EndstonePlayerGameplayHandler::handleEvent(const PlayerFormCloseEvent &even
return true;
}

bool EndstonePlayerGameplayHandler::handleEvent(const ::PlayerRespawnEvent &event)
{
if (const auto *player = WeakEntityRef(event.player).tryUnwrap<::Player>(); player) {
const auto &server = entt::locator<EndstoneServer>::value();
PlayerRespawnEvent e{player->getEndstoneActor<EndstonePlayer>()};
server.getPluginManager().callEvent(e);
}
return true;
}

bool EndstonePlayerGameplayHandler::handleEvent(const ::PlayerEmoteEvent &event)
{
if (const auto *player = WeakEntityRef(event.player).tryUnwrap<::Player>(); player) {
const auto &server = entt::locator<EndstoneServer>::value();
PlayerEmoteEvent e{player->getEndstoneActor<EndstonePlayer>(), event.emote_piece_id};
server.getPluginManager().callEvent(e);
}
return true;
}

bool EndstonePlayerGameplayHandler::handleEvent(const PlayerInteractWithBlockBeforeEvent &event)
{
if (auto *player = WeakEntityRef(event.player).tryUnwrap<::Player>(); player) {
Expand Down Expand Up @@ -142,16 +164,6 @@ bool EndstonePlayerGameplayHandler::handleEvent(const PlayerInteractWithEntityBe
return true;
}

bool EndstonePlayerGameplayHandler::handleEvent(const ::PlayerEmoteEvent &event)
{
if (const auto *player = WeakEntityRef(event.player).tryUnwrap<::Player>(); player) {
const auto &server = entt::locator<EndstoneServer>::value();
PlayerEmoteEvent e{player->getEndstoneActor<EndstonePlayer>(), event.emote_piece_id};
server.getPluginManager().callEvent(e);
}
return true;
}

bool EndstonePlayerGameplayHandler::handleEvent(::PlayerGameModeChangeEvent &event)
{
if (auto *player = event.player.tryUnwrap<::Player>(); player) {
Expand Down
1 change: 1 addition & 0 deletions src/endstone/core/event/handlers/player_gameplay_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class EndstonePlayerGameplayHandler final : public PlayerGameplayHandler {
private:
bool handleEvent(const PlayerFormResponseEvent &event);
bool handleEvent(const PlayerFormCloseEvent &event);
bool handleEvent(const PlayerRespawnEvent &event);
bool handleEvent(const PlayerEmoteEvent &event);
bool handleEvent(const PlayerInteractWithBlockBeforeEvent &event);
bool handleEvent(const PlayerInteractWithEntityBeforeEvent &event);
Expand Down
1 change: 1 addition & 0 deletions src/endstone/python/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ void init_event(py::module_ &m, py::class_<Event> &event, py::enum_<EventPriorit
py::class_<PlayerQuitEvent, PlayerEvent>(m, "PlayerQuitEvent", "Called when a player leaves a server.")
.def_property("quit_message", &PlayerQuitEvent::getQuitMessage, &PlayerQuitEvent::setQuitMessage,
"Gets or sets the quit message to send to all online players.");
py::class_<PlayerRespawnEvent, PlayerEvent>(m, "PlayerRespawnEvent", "Called when a player respawns.");
py::class_<PlayerTeleportEvent, PlayerEvent, ICancellable>(
m, "PlayerTeleportEvent", "Called when a player is teleported from one location to another.")
.def_property("from_location", &PlayerTeleportEvent::getFrom, &PlayerTeleportEvent::setFrom,
Expand Down

0 comments on commit f4fd821

Please sign in to comment.