mcpyproject / McPy

A open source Minecraft server written 100% in Python
GNU Affero General Public License v3.0
82 stars 16 forks source link

Finish basic plugin Implementation #75

Closed walksanatora closed 3 years ago

walksanatora commented 3 years ago

begins to work on #67 currently implements a Event handler (classes.plugin.event) begins implementing a API (literally just a shortcut to sending a chat message, classes.plugin.api) loading and unloading of plugins making of plugins folder if it doesen't exist

example plugin which fires a "command" event if it cant find a command if any receiver of the command event returns true it means the command has executed and works https://pastebin.com/eR0sM2fH

walksanatora commented 3 years ago

Moved __init__ out of the folder and renamed to the former folders name Ok that broke __path__ in the event handler (classes.plugin.event)

walksanatora commented 3 years ago

fixed the __path__ problem replaced with os.path.dirname(os.path.realpath(__file__))

walksanatora commented 3 years ago

and re-squashed it

hydrostaticcog commented 3 years ago

Let's see what @Geolykt thinks

walksanatora commented 3 years ago

Don't use __init__.py for your code

i tried moving plugins.event and plugins.api into the plugins file but the implementation i tried didn't work (i made a class and copied/pasted the code into the class in plugins) plugins.api worked fine because it only imports and has no dependencies from inside the same file, as in plugins.event since i have _ functions in there that are for internal use to avoid copying code it wouldn't work, i even tried event._save_() as a example but it wouldn't find it within the events class, i tried making it a __init__ for the class but that wouldn't be a refactor as you would have to call plugins.event() in order to use it but that happens nowhere in the rest of the project

hydrostaticcog commented 3 years ago

im going to push with my __init__.py fix fyi

walksanatora commented 3 years ago

ok

hydrostaticcog commented 3 years ago

wait wth

hydrostaticcog commented 3 years ago

So apparently we reverted each others commits. I've sent you the files via Discord, to avoid further confusion.

walksanatora commented 3 years ago

everything should be fixed now

walksanatora commented 3 years ago

fixed the formatting things now it should be ready for merge

hydrostaticcog commented 3 years ago

I want to do some more extensive testing, which will probably happen on Wednesday. I know that @Geolykt will probably also want to test plugin development.

Geolykt commented 3 years ago

Not working as intended for me (no plugins - default install):

[2021-07-05 16:10:43,275 - INFO - NETWORK_THREAD] <emeric987> a
Unhandled Error
Traceback (most recent call last):
  File "/usr/lib/python3.9/site-packages/twisted/python/log.py", line 101, in callWithLogger
    return callWithContext({"system": lp}, func, *args, **kw)
  File "/usr/lib/python3.9/site-packages/twisted/python/log.py", line 85, in callWithContext
    return context.call({ILogContext: newCtx}, func, *args, **kw)
  File "/usr/lib/python3.9/site-packages/twisted/python/context.py", line 118, in callWithContext
    return self.currentContext().callWithContext(ctx, func, *args, **kw)
  File "/usr/lib/python3.9/site-packages/twisted/python/context.py", line 83, in callWithContext
    return func(*args, **kw)
--- <exception caught here> ---
  File "/usr/lib/python3.9/site-packages/twisted/internet/posixbase.py", line 687, in _doReadOrWrite
    why = selectable.doRead()
  File "/usr/lib/python3.9/site-packages/twisted/internet/tcp.py", line 246, in doRead
    return self._dataReceived(data)
  File "/usr/lib/python3.9/site-packages/twisted/internet/tcp.py", line 251, in _dataReceived
    rval = self.protocol.dataReceived(data)
  File "/home/Geolykt/.local/lib/python3.9/site-packages/quarry/net/protocol.py", line 86, in dataReceived
    return self.data_received(data)
  File "/home/Geolykt/.local/lib/python3.9/site-packages/quarry/net/protocol.py", line 247, in data_received
    self.packet_received(buff, name)
  File "/home/Geolykt/.local/lib/python3.9/site-packages/quarry/net/protocol.py", line 269, in packet_received
    dispatched = self.dispatch((name,), buff)
  File "/home/Geolykt/.local/lib/python3.9/site-packages/quarry/net/protocol.py", line 28, in dispatch
    handler(buff)
  File "/home/Geolykt/git/McPy/classes/network/Connection.py", line 97, in packet_chat_message
    out = event.fire("chat",self.display_name,p_text)
  File "/home/Geolykt/git/McPy/classes/plugins/event.py", line 50, in fire
    for func in _events_[event]:
builtins.KeyError: 'chat'

[2021-07-05 16:10:43,332 - INFO - NETWORK_THREAD] emeric987 has left.
hydrostaticcog commented 3 years ago

It's odd that it seemed to work for me, and not for you. It must be a discrepancy in WSL or something along those lines. I will double check whenever I get back to my main computer, as well as test for macOS and Windows support.

Geolykt commented 3 years ago

I mean it is quite clear that the affected line (https://github.com/mcpyproject/McPy/pull/75/files#diff-0b65522506fa3e05cb5fa70bf642c6f0216e1f2fab3ae8790950b60429dc4412R50) can only be wrong - though it is beyond me to why it is the case by default

walksanatora commented 3 years ago

i am confused as to how the chat event could be missing because when the classes.network.connection is imported it is loaded here in which it was commented

the positioning looks rather arbitrary for me i will retry in a moment when i reboot into windows and check if the error persist

Geolykt commented 3 years ago

I know - yet it somehow is missing. Perhaps the load order matters?

walksanatora commented 3 years ago

But wouldn't the code from the top if the file be loaded/ran first then when the function is called the code from above should all ready have setup the chat event

Geolykt commented 3 years ago

probably

walksanatora commented 3 years ago

ok after a quick test using python 3.8 in linux and python 3.9 in windows (python 3.9 is broken for me currently: specifically psutil since it was installed via apt) I have determined the problem to be with python 3.8 (possibly), @Geolykt what python version were you using when you were testing. i have noted on discord i notice 3 options

  1. hardcode the pre-defined events
  2. remove classes.plugin.event.registerEvent and automatically register events when called (either via event.fire or event.register)
  3. enforce python 3.9 i think number 2 is a good option but also i dont know the rest of the code base yet (also with number 2 we could just let it auto register and keep registerEvent, iirc also event.register does create events, i did this because the chat event was not being created so i did it automatically forgetting that it should be registering at the top of the file)
Geolykt commented 3 years ago

@Geolykt what python version were you using when you were testing.

python3.9-3.9.5-2.fc34

hydrostaticcog commented 3 years ago

I'll recuse myself from this PR, since this feature has baffled me since #67.

walksanatora commented 3 years ago

I dont get what that means

hydrostaticcog commented 3 years ago

It means that i won't take part in the review. i will support whatever ntoskrnl and Geolykt decide

hydrostaticcog commented 3 years ago

Personally not a huge fan of how this is laid out (especially the code in __init.py__). Also in non-compliance with PEP8 and pretty stale.