diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4d267a2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,169 @@ +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg +MANIFEST + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/#use-with-ide +.pdm.toml + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +.idea/ + +# Wheels +wheelhouse/ + +# Other +.venv +start_server.bat +bedrock_server/ +*.whl \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..5210f2e --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2023 Endstone + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..3a48209 --- /dev/null +++ b/README.md @@ -0,0 +1,26 @@ +# PlayerTP + +Simple player teleportation with confirmation for survival Endstone servers. + +### Warning +This was developed using 4.7 endstone dev build. +Consider using this on [development wheel of endstone](https://github.com/EndstoneMC/endstone/actions/workflows/wheel.yml) + +## Commands +`/tpa ` +Request a teleport to specified player. +`/tpaccept` +Accept a teleport request. +`/tpdeny` +Deny a teleport request. +`/tpcancel` +Cancel your current teleport request. + +## Features +- [x] Player teleport confirmation +- [x] Player request teleport timeout +- [ ] Player teleport delay after confirmation agreed +- [ ] Teleport and confirmation sounds? + +## Requirements +- [Endstone 4.7 dev build or higher](https://github.com/EndstoneMC/endstone) \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..01364fe --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,21 @@ +[build-system] +requires = ["hatchling"] +build-backend = "hatchling.build" + +[project] +name = "endstone-player-tp" +version = "0.1.0" +dependencies = [] +authors = [ + { name = "ArkanDash", email = "suckgamer0@gmail.com" }, +] +description = "A simple player teleport plugin for Endstone servers" +readme = "README.md" +license = { file = "LICENSE" } +keywords = ["endstone", "plugin", "discord"] + +[project.urls] +Homepage = "https://github.com/ArkanDash/endstone-player-tp" + +[project.entry-points."endstone"] +player_tp = "endstone_player_tp:PlayerTP" \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..280c6c4 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +endstone +pipx \ No newline at end of file diff --git a/src/endstone_player_tp/__init__.py b/src/endstone_player_tp/__init__.py new file mode 100644 index 0000000..6db9045 --- /dev/null +++ b/src/endstone_player_tp/__init__.py @@ -0,0 +1,3 @@ +from endstone_player_tp.teleport import PlayerTP + +__all__ = ["PlayerTP"] diff --git a/src/endstone_player_tp/config.toml b/src/endstone_player_tp/config.toml new file mode 100644 index 0000000..1ddf3e4 --- /dev/null +++ b/src/endstone_player_tp/config.toml @@ -0,0 +1,3 @@ +# Teleport Settings + +timeout_timer = 90 # Teleport request timeout if player hasn't responded the request. (Seconds) diff --git a/src/endstone_player_tp/teleport.py b/src/endstone_player_tp/teleport.py new file mode 100644 index 0000000..b41dba6 --- /dev/null +++ b/src/endstone_player_tp/teleport.py @@ -0,0 +1,140 @@ +from endstone.event import ( + event_handler +) +from endstone.plugin import Plugin +from endstone.command import Command, CommandSender +from endstone import ColorFormat +from functools import partial +from time import sleep + +class PlayerTP(Plugin): + api_version = "0.4" + + commands = { + "tpa": { + "description": "Request a teleport to specified player.", + "usages": ["/tpa [target: player]"], + "permissions": ["playertp.command.tpa"], + }, + "tpaccept": { + "description": "Accept the requested player to teleport to you.", + "usages": ["/tpaccept"], + "aliases": ["tpacc"], + "permissions": ["playertp.command.tpaccept"] + }, + "tpdeny": { + "description": "Deny the requested player to teleport to you.", + "usages": ["/tpdeny"], + "aliases": ["tpd"], + "permissions": ["playertp.command.tpdeny"] + }, + "tpcancel": { + "description": "Cancel your current teleport request.", + "usages": ["/tpcancel"], + "aliases": ["tpc"], + "permissions": ["playertp.command.tpcancel"] + } + } + + permissions = { + "playertp.command": { + "description": "Allow users to use all commands provided by this plugin.", + "default": True, + "children": { + "playertp.command.tpa": True, + "playertp.command.tpaccept": True, + "playertp.command.tpdeny": True, + "playertp.command.tpcancel": True, + }, + }, + "playertp.command.tpa": { + "description": "Allow users to use the /tpa command.", + "default": True, + }, + "playertp.command.tpaccept": { + "description": "Allow users to use the /tpaccept command.", + "default": True, + }, + "playertp.command.tpdeny": { + "description": "Allow users to use the /tpdeny command.", + "default": True, + }, + "playertp.command.tpcancel": { + "description": "Allow users to use the /tpcancel command.", + "default": True, + }, + } + def __init__(self, *args, **kwargs): + self.player_tp_queue = [] + super().__init__(*args, **kwargs) + + def on_enable(self): + self.save_default_config() + self.logger.info("PlayerTP has been enabled!") + self.register_events(self) + + def on_disable(self): + self.logger.info("PlayerTP has been disabled!") + + def on_command(self, sender: CommandSender, command: Command, args: list[str]) -> bool: + match command.name: + case "tpa": + if len(args) > 0: + req_player = sender.as_player() + if sender.name == args[0]: + sender.send_error_message("Teleporting to yourself is not allowed!") + return False + if any(queue['sender'] == req_player.name for queue in self.player_tp_queue): + sender.send_error_message("You already have a pending teleport request!\nUse /tpcancel to cancel your current teleport request.") + return False + for tp_player in self.server.online_players: + if tp_player.name == args[0]: + req_player.send_message(f"{ColorFormat.YELLOW}Requesting teleport to {args[0]}!") + tp_player.send_message(f"{ColorFormat.RED}You have {self.config['timeout_timer']} seconds to accept the teleport request from {ColorFormat.WHITE}{req_player.name}") + tp_player.send_message(f"{ColorFormat.YELLOW}Use /tpaccept to accept or /tpdeny to deny the teleport request.") + self.player_tp_queue.append({"sender": f"{req_player.name}", "to_sender": f"{tp_player.name}"}) + p = partial(self.tp_timeout, req_player, tp_player) + self.server.scheduler.run_task_later(self, p, 20 * self.config["timeout_timer"]) + else: + sender.send_error_message("No player input found.\nUsage: /tpa ") + case "tpaccept": + tp_player = sender.as_player() + for player_queue in self.player_tp_queue: + if player_queue["to_sender"] == tp_player.name: + p = partial(self.tp_accepted, player_queue["sender"], tp_player, player_queue) + self.server.scheduler.run_task(self, p) + case "tpdeny": + tp_player = sender.as_player() + for player_queue in self.player_tp_queue: + if player_queue["to_sender"] == tp_player.name: + p = partial(self.tp_denied, player_queue["sender"], tp_player, player_queue) + self.server.scheduler.run_task(self, p) + case "tpcancel": + req_player = sender.as_player() + for player_queue in self.player_tp_queue: + if player_queue["sender"] == req_player.name: + self.player_tp_queue.remove(player_queue) + req_player.send_message(f"{ColorFormat.YELLOW}You have canceled your teleport request.") + return True + + def tp_accepted(self, req_player, tp_player, player_queue) -> None: + for player_sender in self.server.online_players: + if player_sender.name == req_player: + player_sender.send_message(f"{ColorFormat.WHITE}{tp_player.name} {ColorFormat.GREEN}has accepted your request.") + self.player_tp_queue.remove(player_queue) + self.server.dispatch_command(self.server.command_sender, f"tp {player_sender.name} {tp_player.name}") + # player_sender.send_message(f"{ColorFormat.YELLOW}Teleporting in {self.config['tp_request_delay'] - i} seconds...") + + def tp_denied(self, req_player, tp_player, player_queue) -> None: + for player_sender in self.server.online_players: + if player_sender.name == req_player: + player_sender.send_message(f"{ColorFormat.WHITE}{tp_player.name} {ColorFormat.GREEN} has denied your request.") + self.player_tp_queue.remove(player_queue) + + def tp_timeout(self, req_player, tp_player) -> None: + for player_queue in self.player_tp_queue: + if player_queue["to_sender"] == tp_player.name: + req_player.send_message(f"{ColorFormat.WHITE}{tp_player.name} {ColorFormat.GREEN} has ignored your request.") + self.player_tp_queue.remove(player_queue) + + \ No newline at end of file