Skip to content

Commit

Permalink
refactor: change ActorEvent to support templated actor types
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Jan 23, 2025
1 parent 05f0a51 commit a52312f
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 37 deletions.
18 changes: 15 additions & 3 deletions 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', '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']
__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', 'MobEvent', '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 @@ -225,7 +225,7 @@ class Actor(CommandSender):
"""
Gets this actor's current velocity.
"""
class ActorDeathEvent(ActorEvent):
class ActorDeathEvent(MobEvent):
"""
Called when an Actor dies.
"""
Expand Down Expand Up @@ -264,7 +264,7 @@ class ActorExplodeEvent(ActorEvent, Cancellable):
"""
Returns the location where the explosion happened.
"""
class ActorKnockbackEvent(ActorEvent, Cancellable):
class ActorKnockbackEvent(MobEvent, Cancellable):
"""
Called when a living entity receives knockback.
"""
Expand Down Expand Up @@ -1668,6 +1668,18 @@ class Mob(Actor):
"""
Checks to see if an actor is gliding, such as using an Elytra.
"""
class MobEvent(Event):
"""
Represents an Mob-related event.
"""
@staticmethod
def _pybind11_conduit_v1_(*args, **kwargs):
...
@property
def actor(self) -> Mob:
"""
Returns the Mob involved in this event
"""
class ModalForm:
"""
Represents a modal form with controls.
Expand Down
5 changes: 2 additions & 3 deletions include/endstone/event/actor/actor_death_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@ namespace endstone {
/**
* @brief Called when an Actor dies
*/
class ActorDeathEvent : public ActorEvent {
class ActorDeathEvent : public ActorEvent<Mob> {
public:
explicit ActorDeathEvent(Actor &actor) : ActorEvent(actor) {}
~ActorDeathEvent() override = default;
using ActorEvent::ActorEvent;

inline static const std::string NAME = "ActorDeathEvent";
[[nodiscard]] std::string getEventName() const override
Expand Down
7 changes: 4 additions & 3 deletions include/endstone/event/actor/actor_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,26 @@ namespace endstone {
/**
* @brief Represents an Actor-related event
*/
template <typename ActorType>
class ActorEvent : public Event {
public:
explicit ActorEvent(Actor &actor) : actor_(actor){};
explicit ActorEvent(ActorType &actor) : actor_(actor) {};
~ActorEvent() override = default;

/**
* @brief Returns the Actor involved in this event
*
* @return Actor which is involved in this event
*/
[[nodiscard]] Actor &getActor() const
[[nodiscard]] ActorType &getActor() const
{
return actor_;
}

// TODO(event): add getActorType method

private:
Actor &actor_;
ActorType &actor_;
};

} // namespace endstone
2 changes: 1 addition & 1 deletion include/endstone/event/actor/actor_explode_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace endstone {
/**
* @brief Called when an actor explodes
*/
class ActorExplodeEvent : public Cancellable<ActorEvent> {
class ActorExplodeEvent : public Cancellable<ActorEvent<Actor>> {
using BlockList = std::vector<std::shared_ptr<Block>>;

public:
Expand Down
12 changes: 1 addition & 11 deletions include/endstone/event/actor/actor_knockback_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ namespace endstone {
/**
* @brief Called when a living entity receives knockback.
*/
class ActorKnockbackEvent : public Cancellable<ActorEvent> {
class ActorKnockbackEvent : public Cancellable<ActorEvent<Mob>> {
public:
explicit ActorKnockbackEvent(Mob &mob, Actor *source, Vector<float> knockback)
: Cancellable(mob), mob_(mob), source_(source), knockback_(knockback)
Expand All @@ -37,16 +37,6 @@ class ActorKnockbackEvent : public Cancellable<ActorEvent> {
return NAME;
}

/**
* @brief Returns the Mob involved in this event
*
* @return Mob which is involved in this event
*/
[[nodiscard]] Mob &getActor() const
{
return mob_;
}

/**
* @brief Get the source actor that has caused knockback to the defender, if exists.
*
Expand Down
5 changes: 2 additions & 3 deletions include/endstone/event/actor/actor_remove_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ namespace endstone {
* This event should only be used for monitoring. Modifying the actor during or after this event leads to undefined
* behaviours. This event will not be called for Players.
*/
class ActorRemoveEvent : public ActorEvent {
class ActorRemoveEvent : public ActorEvent<Actor> {
public:
explicit ActorRemoveEvent(Actor &actor) : ActorEvent(actor) {}
~ActorRemoveEvent() override = default;
using ActorEvent::ActorEvent;

inline static const std::string NAME = "ActorRemoveEvent";
[[nodiscard]] std::string getEventName() const override
Expand Down
2 changes: 1 addition & 1 deletion include/endstone/event/actor/actor_spawn_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ namespace endstone {
*
* If an Actor Spawn event is cancelled, the actor will not spawn.
*/
class ActorSpawnEvent : public Cancellable<ActorEvent> {
class ActorSpawnEvent : public Cancellable<ActorEvent<Actor>> {
public:
explicit ActorSpawnEvent(Actor &actor) : Cancellable(actor) {}
~ActorSpawnEvent() override = default;
Expand Down
7 changes: 5 additions & 2 deletions include/endstone/event/actor/actor_teleport_event.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ namespace endstone {
*
* This may be as a result of natural causes (Enderman, Shulker), pathfinding (Wolf), or commands (/teleport).
*/
class ActorTeleportEvent : public Cancellable<ActorEvent> {
class ActorTeleportEvent : public Cancellable<ActorEvent<Actor>> {
public:
explicit ActorTeleportEvent(Actor &actor, Location from, Location to) : Cancellable(actor), from_(from), to_(to) {}
explicit ActorTeleportEvent(Actor &actor, Location from, Location to)
: Cancellable(actor), from_(std::move(from)), to_(std::move(to))
{
}
~ActorTeleportEvent() override = default;

inline static const std::string NAME = "ActorTeleportEvent";
Expand Down
24 changes: 14 additions & 10 deletions src/endstone/python/event.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,15 @@ void init_event(py::module_ &m, py::class_<Event> &event, py::enum_<EventPriorit
"Cancel this event. A cancelled event will not be executed in the server, but will still pass to other "
"plugins.");

py::class_<ActorEvent, Event>(m, "ActorEvent", "Represents an Actor-related event.")
.def_property_readonly("actor", &ActorEvent::getActor, py::return_value_policy::reference,
py::class_<ActorEvent<Actor>, Event>(m, "ActorEvent", "Represents an Actor-related event.")
.def_property_readonly("actor", &ActorEvent<Actor>::getActor, py::return_value_policy::reference,
"Returns the Actor involved in this event");
py::class_<ActorDeathEvent, ActorEvent>(m, "ActorDeathEvent", "Called when an Actor dies.");
py::class_<ActorExplodeEvent, ActorEvent, ICancellable>(m, "ActorExplodeEvent", "Called when an Actor explodes.")
py::class_<ActorEvent<Mob>, Event>(m, "MobEvent", "Represents an Mob-related event.")
.def_property_readonly("actor", &ActorEvent<Mob>::getActor, py::return_value_policy::reference,
"Returns the Mob involved in this event");
py::class_<ActorDeathEvent, ActorEvent<Mob>>(m, "ActorDeathEvent", "Called when an Actor dies.");
py::class_<ActorExplodeEvent, ActorEvent<Actor>, ICancellable>(m, "ActorExplodeEvent",
"Called when an Actor explodes.")
.def_property_readonly("location", &ActorExplodeEvent::getLocation,
"Returns the location where the explosion happened.")
.def_property(
Expand All @@ -77,16 +81,16 @@ void init_event(py::module_ &m, py::class_<Event> &event, py::enum_<EventPriorit
},
py::return_value_policy::reference_internal,
"Gets or sets the list of blocks that would have been removed or were removed from the explosion event.");
py::class_<ActorKnockbackEvent, ActorEvent, ICancellable>(m, "ActorKnockbackEvent",
"Called when a living entity receives knockback.")
py::class_<ActorKnockbackEvent, ActorEvent<Mob>, ICancellable>(m, "ActorKnockbackEvent",
"Called when a living entity receives knockback.")
.def_property_readonly("source", &ActorKnockbackEvent::getSource, py::return_value_policy::reference,
"Get the source actor that has caused knockback to the defender, if exists.")
.def_property("knockback", &ActorKnockbackEvent::getKnockback, &ActorKnockbackEvent::setKnockback,
"Gets or sets the knockback that will be applied to the entity.");
py::class_<ActorRemoveEvent, ActorEvent>(m, "ActorRemoveEvent", "Called when an Actor is removed.");
py::class_<ActorSpawnEvent, ActorEvent, ICancellable>(m, "ActorSpawnEvent",
"Called when an Actor is spawned into a world.");
py::class_<ActorTeleportEvent, ActorEvent, ICancellable>(
py::class_<ActorRemoveEvent, ActorEvent<Actor>>(m, "ActorRemoveEvent", "Called when an Actor is removed.");
py::class_<ActorSpawnEvent, ActorEvent<Actor>, ICancellable>(m, "ActorSpawnEvent",
"Called when an Actor is spawned into a world.");
py::class_<ActorTeleportEvent, ActorEvent<Actor>, ICancellable>(
m, "ActorTeleportEvent", "Called when a non-player entity is teleported from one location to another.")
.def_property("from_location", &ActorTeleportEvent::getFrom, &ActorTeleportEvent::setFrom,
"Gets or sets the location that this actor moved from.")
Expand Down

0 comments on commit a52312f

Please sign in to comment.