Skip to content

Commit

Permalink
feat: add python bindings for ban lists
Browse files Browse the repository at this point in the history
  • Loading branch information
wu-vincent committed Dec 23, 2024
1 parent a3a86c3 commit 1e984d4
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 2 deletions.
52 changes: 51 additions & 1 deletion python/src/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', 'ActorKnockbackEvent', 'ActorRemoveEvent', 'ActorSpawnEvent', 'ActorTeleportEvent', 'BanEntry', 'BarColor', 'BarFlag', 'BarStyle', 'Block', 'BlockBreakEvent', 'BlockData', 'BlockEvent', 'BlockFace', 'BlockPlaceEvent', 'BlockState', 'BossBar', 'BroadcastMessageEvent', 'ColorFormat', 'Command', 'CommandExecutor', 'CommandSender', 'CommandSenderWrapper', 'ConsoleCommandSender', 'Criteria', 'Dimension', 'DisplaySlot', 'Dropdown', 'Event', 'EventPriority', 'GameMode', 'Inventory', 'IpBanEntry', 'ItemStack', 'Label', 'Language', 'Level', 'Location', 'Logger', 'MessageForm', 'Mob', 'ModalForm', 'Objective', 'ObjectiveSortOrder', 'Packet', 'PacketType', 'Permissible', 'Permission', 'PermissionAttachment', 'PermissionAttachmentInfo', 'PermissionDefault', 'Player', 'PlayerBanEntry', 'PlayerChatEvent', 'PlayerCommandEvent', 'PlayerDeathEvent', 'PlayerEvent', 'PlayerInteractActorEvent', 'PlayerInteractEvent', 'PlayerInventory', 'PlayerJoinEvent', 'PlayerKickEvent', 'PlayerLoginEvent', 'PlayerQuitEvent', 'PlayerTeleportEvent', 'Plugin', 'PluginCommand', 'PluginDescription', 'PluginDisableEvent', 'PluginEnableEvent', 'PluginLoadOrder', 'PluginLoader', 'PluginManager', 'Position', 'RenderType', 'Scheduler', 'Score', 'Scoreboard', 'ScriptMessageEvent', 'Server', 'ServerCommandEvent', 'ServerListPingEvent', 'ServerLoadEvent', 'Skin', 'Slider', 'SocketAddress', 'SpawnParticleEffectPacket', 'StepSlider', 'Task', 'TextInput', 'ThunderChangeEvent', 'Toggle', 'Translatable', 'Vector', 'WeatherChangeEvent']
__all__ = ['ActionForm', 'Actor', 'ActorDeathEvent', 'ActorEvent', 'ActorKnockbackEvent', 'ActorRemoveEvent', 'ActorSpawnEvent', 'ActorTeleportEvent', 'BanEntry', 'BarColor', 'BarFlag', 'BarStyle', 'Block', 'BlockBreakEvent', 'BlockData', 'BlockEvent', 'BlockFace', 'BlockPlaceEvent', 'BlockState', 'BossBar', 'BroadcastMessageEvent', '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', 'Packet', 'PacketType', 'Permissible', 'Permission', 'PermissionAttachment', 'PermissionAttachmentInfo', 'PermissionDefault', 'Player', 'PlayerBanEntry', 'PlayerBanList', 'PlayerChatEvent', 'PlayerCommandEvent', 'PlayerDeathEvent', 'PlayerEvent', 'PlayerInteractActorEvent', 'PlayerInteractEvent', 'PlayerInventory', 'PlayerJoinEvent', 'PlayerKickEvent', 'PlayerLoginEvent', 'PlayerQuitEvent', 'PlayerTeleportEvent', 'Plugin', 'PluginCommand', 'PluginDescription', 'PluginDisableEvent', 'PluginEnableEvent', 'PluginLoadOrder', 'PluginLoader', 'PluginManager', 'Position', 'RenderType', 'Scheduler', 'Score', 'Scoreboard', 'ScriptMessageEvent', 'Server', 'ServerCommandEvent', 'ServerListPingEvent', 'ServerLoadEvent', 'Skin', 'Slider', 'SocketAddress', 'SpawnParticleEffectPacket', 'StepSlider', 'Task', 'TextInput', 'ThunderChangeEvent', 'Toggle', 'Translatable', 'Vector', 'WeatherChangeEvent']
class ActionForm:
"""
Represents a form with buttons that let the player take action.
Expand Down Expand Up @@ -1209,6 +1209,31 @@ class IpBanEntry(BanEntry):
"""
Gets the banned IP address.
"""
class IpBanList:
"""
Represents a ban list containing banned IP addresses.
"""
def add_ban(self, address: str, reason: str | None = None, expires: datetime.datetime | None = None, source: str | None = None) -> IpBanEntry:
"""
Adds a ban to this list, or updates an existing one.
"""
def get_ban_entry(self, address: str) -> IpBanEntry:
"""
Gets a BanEntry by IP address.
"""
def is_banned(self, address: str) -> bool:
"""
Checks if a BanEntry exists for the target by IP address.
"""
def remove_ban(self, address: str) -> None:
"""
Removes an IP address from the ban list.
"""
@property
def entries(self) -> list[IpBanEntry]:
"""
Gets a vector of pointers to entries in the ban list.
"""
class ItemStack:
"""
Represents a stack of items.
Expand Down Expand Up @@ -2103,6 +2128,31 @@ class PlayerBanEntry(BanEntry):
"""
Gets the banned player's Xbox user ID (XUID), or None if not available.
"""
class PlayerBanList:
"""
Represents a ban list containing banned players.
"""
def add_ban(self, name: str, uuid: uuid.UUID | None = None, xuid: str | None = None, reason: str | None = None, expires: datetime.datetime | None = None, source: str | None = None) -> PlayerBanEntry:
"""
Adds a ban to this list, or updates an existing one.
"""
def get_ban_entry(self, name: str, uuid: uuid.UUID | None = None, xuid: str | None = None) -> PlayerBanEntry:
"""
Gets a BanEntry by player name, UUID, or XUID.
"""
def is_banned(self, name: str, uuid: uuid.UUID | None = None, xuid: str | None = None) -> bool:
"""
Checks if a BanEntry exists for the target by name, UUID, or XUID.
"""
def remove_ban(self, name: str, uuid: uuid.UUID | None = None, xuid: str | None = None) -> None:
"""
Removes a player from the ban list by name, UUID, or XUID.
"""
@property
def entries(self) -> list[PlayerBanEntry]:
"""
Gets a vector of pointers to entries in the ban list.
"""
class PlayerChatEvent(PlayerEvent):
"""
Called when a player sends a chat message.
Expand Down
43 changes: 43 additions & 0 deletions src/endstone_python/ban.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@

#include "endstone/ban/ban_entry.h"
#include "endstone/ban/ip_ban_entry.h"
#include "endstone/ban/ip_ban_list.h"
#include "endstone/ban/player_ban_entry.h"
#include "endstone/ban/player_ban_list.h"
#include "endstone/detail/pybind_type_caster.h"

namespace py = pybind11;
Expand All @@ -39,6 +41,22 @@ void init_ban(py::module_ &m)
.def(py::init<std::string>(), py::arg("address"))
.def_property_readonly("address", &IpBanEntry::getAddress, "Gets the banned IP address.");

py::class_<IpBanList>(m, "IpBanList", "Represents a ban list containing banned IP addresses.")
.def("get_ban_entry", py::overload_cast<std::string>(&IpBanList::getBanEntry),
py::return_value_policy::reference, py::arg("address"), "Gets a BanEntry by IP address.")
.def("add_ban",
py::overload_cast<std::string, std::optional<std::string>, std::optional<BanEntry::Date>,
std::optional<std::string>>(&IpBanList::addBan),
py::return_value_policy::reference, py::arg("address"), py::arg("reason") = std::nullopt,
py::arg("expires") = std::nullopt, py::arg("source") = std::nullopt,
"Adds a ban to this list, or updates an existing one.")
.def("is_banned", py::overload_cast<std::string>(&IpBanList::isBanned, py::const_), py::arg("address"),
"Checks if a BanEntry exists for the target by IP address.")
.def("remove_ban", &IpBanList::removeBan, py::arg("address"), "Removes an IP address from the ban list.")
.def_property_readonly("entries", py::overload_cast<>(&IpBanList::getEntries, py::const_),
py::return_value_policy::reference_internal,
"Gets a vector of pointers to entries in the ban list.");

py::class_<PlayerBanEntry, BanEntry>(m, "PlayerBanEntry", "Represents a ban entry for a player.")
.def(py::init<std::string, std::optional<UUID>, std::optional<std::string>>(), py::arg("name"),
py::arg("uuid") = std::nullopt, py::arg("xuid") = std::nullopt)
Expand All @@ -47,6 +65,31 @@ void init_ban(py::module_ &m)
"Gets the banned player's unique ID, or None if not available.")
.def_property_readonly("xuid", &PlayerBanEntry::getXuid,
"Gets the banned player's Xbox user ID (XUID), or None if not available.");

py::class_<PlayerBanList>(m, "PlayerBanList", "Represents a ban list containing banned players.")
.def("get_ban_entry",
py::overload_cast<std::string, std::optional<UUID>, std::optional<std::string>>(
&PlayerBanList::getBanEntry),
py::return_value_policy::reference, py::arg("name"), py::arg("uuid") = std::nullopt,
py::arg("xuid") = std::nullopt, "Gets a BanEntry by player name, UUID, or XUID.")
.def("add_ban",
py::overload_cast<std::string, std::optional<UUID>, std::optional<std::string>, std::optional<std::string>,
std::optional<BanEntry::Date>, std::optional<std::string>>(&PlayerBanList::addBan),
py::return_value_policy::reference, py::arg("name"), py::arg("uuid") = std::nullopt,
py::arg("xuid") = std::nullopt, py::arg("reason") = std::nullopt, py::arg("expires") = std::nullopt,
py::arg("source") = std::nullopt, "Adds a ban to this list, or updates an existing one.")
.def("is_banned",
py::overload_cast<std::string, std::optional<UUID>, std::optional<std::string>>(&PlayerBanList::isBanned,
py::const_),
py::arg("name"), py::arg("uuid") = std::nullopt, py::arg("xuid") = std::nullopt,
"Checks if a BanEntry exists for the target by name, UUID, or XUID.")
.def("remove_ban",
py::overload_cast<std::string, std::optional<UUID>, std::optional<std::string>>(&PlayerBanList::removeBan),
py::arg("name"), py::arg("uuid") = std::nullopt, py::arg("xuid") = std::nullopt,
"Removes a player from the ban list by name, UUID, or XUID.")
.def_property_readonly("entries", py::overload_cast<>(&PlayerBanList::getEntries, py::const_),
py::return_value_policy::reference_internal,
"Gets a vector of pointers to entries in the ban list.");
}

} // namespace endstone::detail
6 changes: 5 additions & 1 deletion src/endstone_python/endstone_python.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,11 @@ void init_server(py::class_<Server> &server)
},
py::arg("type"), py::arg("block_states") = std::nullopt,
"Creates a new BlockData instance for the specified block type, with all properties initialized to "
"defaults, except for those provided.");
"defaults, except for those provided.")
.def_property_readonly("ban_list", &Server::getBanList, "Gets the player ban list.",
py::return_value_policy::reference)
.def_property_readonly("ip_ban_list", &Server::getIpBanList, "Gets the IP ban list.",
py::return_value_policy::reference);
}

void init_player(py::module_ &m, py::class_<Player, Mob> &player)
Expand Down

0 comments on commit 1e984d4

Please sign in to comment.