Skip to content

Commit

Permalink
Merge pull request #6 from skuzow/develop
Browse files Browse the repository at this point in the history
v0.1.2
  • Loading branch information
skuzow authored Jul 1, 2022
2 parents 0f9f920 + 2c3d87b commit 191a233
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 52 deletions.
6 changes: 1 addition & 5 deletions .github/workflows/package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,7 @@ jobs:
build:
strategy:
matrix:
# Use these Python versions
python: [
3.6
]
# and run on both Linux and Windows
python: [ 3.6 ]
os: [ ubuntu-20.04, windows-2022 ]
runs-on: ${{ matrix.os }}
steps:
Expand Down
2 changes: 1 addition & 1 deletion mcdreforged.plugin.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"id": "offline_whitelist",
"version": "0.0.1",
"version": "0.1.2",
"name": "OfflineWhitelist",
"description": "Offline whitelist helper",
"author": "skuzow",
Expand Down
26 changes: 17 additions & 9 deletions offline_whitelist/__init__.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import offline_whitelist.commands as commands
import offline_whitelist.utils as utils
from mcdreforged.api.all import *
from offline_whitelist.commands import whitelist_add
from offline_whitelist.utils import load_config

PLUGIN_METADATA = ServerInterface.get_instance().as_plugin_server_interface().get_self_metadata()
plugin_metadata = utils.get_plugin_metadata()


prefix = '!!offw'
description = PLUGIN_METADATA.description
description = plugin_metadata.description
help_message = '''
--- MCDR {1} v{2} ---
- {3} plugin
{0} add §6[username] §rAdd offline player to whitelist
'''.strip().format(prefix, PLUGIN_METADATA.name, PLUGIN_METADATA.version, description)
§7{0} add §6[username] §rAdd player to whitelist
§7{0} remove §6[username] §rRemove player from whitelist
§7{0} list §rShow players inside whitelist
§7{0} reload §rReload plugin itself
'''.strip().format(prefix, plugin_metadata.name, plugin_metadata.version, description)


def on_load(server: PluginServerInterface, old):
load_config(None, server)
utils.load_config(None, server)
server.register_help_message(prefix, description)
register_commands(server)

Expand All @@ -25,7 +28,12 @@ def get_username(callback):
return Text('username').runs(callback)
server.register_command(
Literal(prefix).
runs(lambda src: src.reply(help_message)).
requires(lambda src: src.has_permission(utils.get_config().minimum_permission_level)).
on_error(RequirementNotMet, lambda src: src.reply(RText('Insufficient permission!', color=RColor.red)), handled=True).
on_error(UnknownArgument, lambda src: src.reply(f'Parameter error! Please enter §7{prefix}§r to get plugin help'), handled=True).
then(Literal('add').then(get_username(lambda src, ctx: whitelist_add(src, ctx['username']))))
runs(lambda src: src.reply(help_message)).
then(Literal('add').then(get_username(lambda src, ctx: commands.whitelist_add(src, ctx['username'])))).
then(Literal('remove').then(get_username(lambda src, ctx: commands.whitelist_remove(src, ctx['username'])))).
then(Literal('list').runs(commands.whitelist_list)).
then(Literal('reload').runs(commands.reload_plugin))
)
128 changes: 106 additions & 22 deletions offline_whitelist/commands.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,112 @@
import time

import offline_whitelist.utils as utils
from mcdreforged.api.all import *
from offline_whitelist.utils import get_config, find_file, generate_offline, load_file, dump_file, send_info, \
send_error, check_permission


def whitelist_add(source: CommandSource, username):
config = get_config()
if find_file(source, config.whitelist_path) and check_permission(source, 3):
source.get_server().execute(f'whitelist add {username}')
time.sleep(0.5)
offline_uuid = generate_offline(source, username)
whitelist_json = load_file(source, config.whitelist_path)
# search player username inside whitelist & change uuid to offline one
for player in whitelist_json:
if player["name"] == username:


def whitelist_add(source: PlayerCommandSource, username):
config = utils.get_config()
if utils.find_file(source, config.whitelist_path):
whitelist_json = utils.load_file(source, config.whitelist_path)
whitelist_json_filter = [obj for obj in whitelist_json if obj["name"] == username]
offline_uuid = utils.generate_offline(source, username)
server = source.get_server()
# player inside whitelist
len_whitelist_json_filter = len(whitelist_json_filter)
if len_whitelist_json_filter > 0:
# online or offline whitelisted
if len_whitelist_json_filter == 1:
# online whitelisted
player = whitelist_json_filter[0]
if not player["uuid"] == offline_uuid:
# remove player with online uuid from whitelist & then change uuid to offline and add back
whitelist_json.remove(player)
player["uuid"] = offline_uuid
dump_file(source, config.whitelist_path, whitelist_json)
source.get_server().execute('whitelist reload')
return send_info(source, f'Successfully added to whitelist: {username}')
whitelist_json.append(player)
utils.dump_file(source, config.whitelist_path, whitelist_json)
utils.send_warning(source, f'Player already whitelisted online, changed online uuid to offline: {username}')
# offline whitelisted
else:
return send_error(source, f'Player already whitelisted: {username}', None)
# couldn't find nickname because bad written / only for online players
send_error(source, f'Username is misspelled: {username}', None)
source.get_server().execute(f'whitelist remove {username}')
source.get_server().execute('whitelist reload')
utils.send_error(source, f'Player already whitelisted: {username}', None)
# online & offline whitelisted
else:
# clear online whitelist & keep offline
# server.execute(f'whitelist remove {username}') not valid if player is in user-cache, removes offline
online_player = [obj for obj in whitelist_json_filter if not obj["uuid"] == offline_uuid][0]
whitelist_json.remove(online_player)
utils.dump_file(source, config.whitelist_path, whitelist_json)
utils.send_warning(source, f'Player already whitelisted online & offline, removed online: {username}')
# player not inside whitelist or username misspelled
else:
server.execute(f'whitelist add {username}')
time.sleep(0.1)
whitelist_json_add = utils.load_file(source, config.whitelist_path)
found = False
for player in whitelist_json_add:
if player["name"] == username:
found = True
if not player["uuid"] == offline_uuid:
player["uuid"] = offline_uuid
else:
# /whitelist add uses offline uuid because player already entered the server
server.logger.info(f'Player {username} already in user-cache, whitelisted like usual')
break
if found:
utils.dump_file(source, config.whitelist_path, whitelist_json_add)
utils.send_info(source, f'Successfully added to whitelist: {username}')
else:
# couldn't find nickname because bad written / only for online players
utils.dump_file(source, config.whitelist_path, whitelist_json)
utils.send_error(source, f'Username is misspelled: {username}', None)
server.execute('whitelist reload')


def whitelist_remove(source: PlayerCommandSource, username):
config = utils.get_config()
if utils.find_file(source, config.whitelist_path):
whitelist_json = utils.load_file(source, config.whitelist_path)
whitelist_json_filter = [obj for obj in whitelist_json if obj["name"] == username]
server = source.get_server()
# player inside whitelist
len_whitelist_json_filter = len(whitelist_json_filter)
if len_whitelist_json_filter > 0:
for player in whitelist_json_filter:
if player["name"] == username:
whitelist_json.remove(player)
utils.dump_file(source, config.whitelist_path, whitelist_json)
utils.send_info(source, f'Successfully removed from whitelist: {username}')
server.execute('whitelist reload')
# player not inside whitelist
else:
utils.send_error(source, f'Player not whitelisted: {username}', None)


def whitelist_list(source: PlayerCommandSource):
config = utils.get_config()
if utils.find_file(source, config.whitelist_path):
whitelist_json = utils.load_file(source, config.whitelist_path)
len_whitelist_json = len(whitelist_json)
# player_list = username : offline/online -> uuid
player_list = f'''--- Whitelist List : {config.whitelist_path} ---\n'''
for index, player in enumerate(whitelist_json):
username = player["name"]
uuid = player["uuid"]
offline_uuid = utils.generate_offline(source, username)
if uuid == offline_uuid:
player_list += f'§7{username} §r: §aoffline §r-> §r{uuid}'
else:
player_list += f'§7{username} §r: §conline §r-> §r{uuid}'
# no line jump for last line
if not index == len_whitelist_json - 1:
player_list += '\n'
source.reply(player_list)
if source.is_player:
source.get_server().logger.info(f'\n{player_list}')


def reload_plugin(source: PlayerCommandSource):
plugin_metadata = utils.get_plugin_metadata()
if source.get_server().reload_plugin(plugin_metadata.id):
utils.send_info(source, f'{plugin_metadata.name} plugin successfully reloaded!')
else:
utils.send_error(source, f'There was an error reloading {plugin_metadata.name} plugin', None)
34 changes: 19 additions & 15 deletions offline_whitelist/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@

from mcdreforged.api.all import *

PLUGIN_METADATA = ServerInterface.get_instance().as_plugin_server_interface().get_self_metadata()
plugin_metadata = ServerInterface.get_instance().as_plugin_server_interface().get_self_metadata()


class Config(Serializable):
whitelist_path: str = './server/whitelist.json'
minimum_permission_level: int = 2


config: Optional[Config] = None
Expand All @@ -18,7 +19,7 @@ class Config(Serializable):
def generate_offline(source: CommandSource, username):
# extracted from the java code:
# new GameProfile(UUID.nameUUIDFromBytes(("OfflinePlayer:" + name).getBytes(Charsets.UTF_8)), name));
string = "OfflinePlayer:" + username
string = 'OfflinePlayer:' + username
hash = hashlib.md5(string.encode('utf-8')).digest()
byte_array = [byte for byte in hash]
byte_array[6] = hash[6] & 0x0f | 0x30
Expand All @@ -32,17 +33,13 @@ def __add_stripes(uuid):
return uuid[:8] + '-' + uuid[8:12] + '-' + uuid[12:16] + '-' + uuid[16:20] + '-' + uuid[20:]


def check_permission(source: CommandSource, min_permission_level):
if source.has_permission_higher_than(min_permission_level - 1):
return True
else:
source.reply('You don\'t permission to run this command')
return False
def get_plugin_metadata():
return plugin_metadata


def load_config(source: Optional[CommandSource], server: PluginServerInterface):
global config
config_file_path = os.path.join('config', '{}.json'.format(PLUGIN_METADATA.id))
config_file_path = os.path.join('config', '{}.json'.format(plugin_metadata.id))
config = server.load_config_simple(config_file_path, in_data_folder=False, source_to_reply=source,
echo_in_console=False, target_class=Config)

Expand All @@ -52,13 +49,21 @@ def get_config():


def send_info(source: CommandSource, message):
source.reply(message)
source.get_server().logger.info(message)
source.reply(RText(message, color=RColor.green))
if source.is_player:
source.get_server().logger.info(message)


def send_warning(source: CommandSource, message):
source.reply(RText(message, color=RColor.gold))
if source.is_player:
source.get_server().logger.warning(message)


def send_error(source: CommandSource, message, error):
source.reply(message)
source.get_server().logger.error(message)
source.reply(RText(message, color=RColor.red))
if source.is_player:
source.get_server().logger.error(message)
if error is not None:
source.get_server().logger.error(error)

Expand All @@ -67,7 +72,7 @@ def find_file(source: CommandSource, file_path):
# check if file with path given exists
if os.path.isfile(file_path):
return True
send_error(source, f'Couldn\'t found file: {config.whitelist_path}', None)
send_error(source, f'Couldn\'t found file: {file_path}', None)
return False


Expand All @@ -88,6 +93,5 @@ def dump_file(source: CommandSource, file_path, file_json):
write_file = open(file_path, 'w')
# save changes into the file in the disk, then closes it
json.dump(file_json, write_file, indent=2)
source.get_server().execute('whitelist reload')
except Exception as error:
send_error(source, f'Couldn\'t dump file: {file_path}', error)

0 comments on commit 191a233

Please sign in to comment.