Using Minecraft to teach programming in Python

Discussion in 'Bukkit Discussion' started by Passiday, Sep 26, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Passiday

    I'd like to teach kids programming in Python, using such dynamic, interactive environment as Minecraft.
    So, I have created a Bukkit plugin that basically does two things: captures selection of events (ie, user clicked on a block, pushed a button) and logs them in the database; and polls the Tasks table on regular time intervals and executes any incoming tasks (ie, put block in certain location, spawn entity, teleport player).
    On the Python side, I have a class that connects the database, queries the Events table for any new events and triggers associated event handlers (Python methods). And, it has series of methods that add entries to the Tasks table. Now it is easy to write a Python code that causes some change in the Minecraft world, reacting on a player action. Since there is this data exchange through the database, there is some latency, but too damaging for most of the programming ideas.
    But I think I need some mentoring now, to know if I do the things "the right way" - since I have to write some bit of code (not much but anyway) for any kind of event and any type of task, my solution is doomed to be limited by this bottleneck interpretation layer - what and how the events/tasks are registered in the database. Should there be some alternative way how to achieve what I need, I better switch now, before I move on developing my plugin further.
    These are the requirements:
    • A user should be able to write Python code that can react to the Minecraft events and modify the world, run the code in both normal mode and in the interpreter shell.
    • This should be server-based solution, where several users can work/play in one environment.
    One obvious solution would be letting people develop their own Bukkit plugins (I see there is Python plugin loader http://dev.bukkit.org/bukkit-plugins/python-plugin-loader), but I am worried that having to take care for full-scale plugin coding is well beyond my students competence level. Besides, the compile-copy-reload routine needed for any plugin update steals the joy of writing world.setTime(0) in the shell and immediately seeing the result.
    For now, I see only event/task exchange via database as a solution for this task. But perhaps I am blindfolded by my tunnel vision. If so, I need your help to learn the alternatives.
     
  2. Offline

    hatstand

    Only other way I can see is using Java's scripting capabilities (Jython, Rhino, etc), set up a comprehensive context for them to run in, then execute console input using that.
     
  3. Offline

    mkremins

    I would advise doing something like this rather than what's described in the OP.

    To elaborate: rather than having a separate Python process running somewhere and pulling instructions from a database, this solution would have you simply embedding a Python interpreter in a Bukkit plugin and using the embedded interpreter to eval Python code on the fly.

    Ideally, you'll be able to initialize the scripting context with a Python object or set of objects that are basically just a wrapper over the Bukkit API itself; then, when someone enters Python code, you'll just evaluate it within that context, giving users the ability to mess with various parts of the world by making calls to that object's methods in their Python code.

    Jython is a Python interpreter written in Java that can be embedded in Java apps (including Bukkit plugins) and presumably used to implement something like the above. I have no experience with Jython myself, but I do have some prior experience in the area of developing Bukkit plugins that permit scripting – specifically, scripting in JavaScript by way of the Rhino interpreter.

    I'll be keeping an eye on this thread – always intrigued whenever someone starts talking about letting people run scripts by way of a Bukkit plugin, and definitely happy to help if you have any other questions :)
     
    Axe2760 likes this.
  4. Offline

    Hoolean

    Passiday

    Hey there, sounds like a great idea, Minecraft is definitely an awesome way to get people involved in programming. I know of a web-dev place nearby to me that runs a club for kids that uses python with Bukkit to teach them it, although I know little more; I can ask more of them if you wish!

    I believe the best solution is something along the lines of what mkremins and hatstand have said, an interpreter built into the server itself. I recommend directly interfacing the Bukkit API - perhaps with Jython or Rhino - maybe with perhaps a few classes that simplify it yet always retaining access to the base classes themselves, so you will never face self-imposed limitations on the API you are making.

    I will watch this thread too, if you need to know any more just ask; I may make a small plugin to show you how it could work :)
     
  5. Offline

    Passiday

    Ok, although the word invested in the DB-mediated solution feels like a big deal, I could force myself to treat it as a deadweight loss, given all the benefits you guys praise :)

    Perhaps it would be appropriate to burden no other than myself by developing the necessary plugin, but I indeed have some questions about the architecture of such solution where I could really use some advice from you. I really appreciate your readiness to help. I find that surprising today's kids with the awesomeness of programming is pretty challenging. Seeing "Hello World" outputted on the computer's screen is no more such a big deal as it was in my childhood. But making them influence the Minecraft world with their own created code could indeed make the deal.

    As far as I understand, Jython can be used to set up a fully functional Python context that has access to the Bukkit objects. And then I could inject Python code to be either successfully executed or fail with some error. I am worried if this way of running code is not with limitations: ie, can I use IDE of my choice (currently we use Geany), could I do line-by-line execution, or add breakpoints? Does that call for making custom API, where the client sends code to the Bukkit plugin and received back whatever is printed in the Jython virtual console's stdout? I still hope for a solution that would let several users from different workstations to run each their own code, ie, the classroom environment. Of course, there should be implemented some kind of user isolation to avoid unintended damage, but that's another issue that would be relatively easy, given all this setup works.

    Another issue I fail to grasp is, whether this scripting context is persistent (ie, values of variables not lost whatever goes on in the world), how to re-initialize it, and how to capture events. Perhaps those are quite naive issues that are obvious for someone who has experience with embedding Jython in Java app, but I have no such thing unfortunately.

    I'll be happy to be enlightened.
     
  6. Offline

    xTrollxDudex

    Passiday
    Teaching KIDS python? Forget about it. Jkay I learned java and I'm 12
     
  7. Offline

    mkremins

    It looks like your best bet is to use a PythonInterpreter object to evaluate code. The PythonInterpreter interface exposes methods that allow you to evaluate a whole file at once, as well as to evaluate a single string of Python code at a time; between these two methods, you should be able to do all of the above in one way or another.

    This should be pretty easy to implement: once you've read a string of Python code from a command or chat message, just pass it to the "evaluate single string" method of your PythonInterpreter object and send the user a string representation of the PyObject that's returned.

    You should be able to let multiple users collaborate by running all of their code within a single PythonInterpreter instance. Likewise, you could probably implement a very simple form of "sandboxing" by assigning a separate PythonInterpreter instance to each user.

    As long as you're holding onto a single instance of PythonInterpreter, it would appear that variables set previously on that same instance will retain their values.

    The Bukkit plugin responsible for running the interpreter will most likely have to register an EventListener for each type of event you want to capture, then "handle" the event by passing it to one or more users' Python code as desired. This is probably the trickiest part of the whole setup, as you'll have to figure out how a Bukkit event object should be represented in Python, as well as determine what Python code needs to be invoked when a particular type of event is fired.
     
  8. Offline

    Axe2760

    xTrollxDudex Learned python when I was 10, java when I was 13 :p

    @OP: Following this thread. Would love to see how this turns out as well! :p
     
  9. Offline

    Passiday

    Ok, I'm not sure if and when I will be able to pull this through, but in my vision I could see a web app that has user accounts and has Ace editor (http://ace.c9.io/#nav=about) embedded in it for the imports, functions and classes, and a separate textbox for entering immediately evaluated code. I still will need some method how to pass the code to the Bukkit plugin, and perhaps that would be a database (for the web app to operate, the code has to be preserved in the database anyway). Slight delay at submitting the code for execution is tolerable, as the code, including any events, afterwards will be run at the plugin scope.

    Line-by-line execution, breakpoints and watches would be something that could be added later, as they would require some serious tinkering with the web app interface and plugin coding.

    How does that sound?
     
  10. Offline

    Hoolean

    Passiday

    That sure doesn't sound bad!

    I'd just like to say, if you need any help with simplifying Bukkit classes then I'd be more than happy to help make a few for you :)
     
  11. Offline

    Passiday

    Ok, I just have to get into this so that I can formulate where exactly I'd need your help. When you say "simplifying Bukkit classes", do you talk about the Python wrappers for the most common world manipulation needs?
     
  12. Offline

    Hoolean

    Passiday

    Yeah, that was my aim. Whilst python could easily use Bukkit classses directly, Bukkit classes are made for Java and made for people who program. Making a few wrapper classes could mean it is a lot easier to use. :)

    E.g.

    player.giveItem(Material.FISH);

    instead of

    player.getInventory().addItem(new ItemStack(Material.FISH, 1));
     
  13. Offline

    xTrollxDudex

  14. Offline

    Passiday

    Well, here's my first hurdle.

    I can now execute arbitrary Python code, accessing the Bukkit classes. I capture the output in StringWriter and when the code is happily executed, I output the result to the plugin logger.

    But I don't see how the PythonInterpreter should be set up so that the python code could run continuously in the background without suspending (or possibly crashing) the server. I seem to need two things:
    • Where to keep the PythonInterpreter instance so that it doesn't die between the requests, and also in case it runs long-executing code. Running it in a separate thread is a possibility buy I wonder if the link to the Bukkit server is preserved and reliable.
    • A way how to capture the output asynchronously, ie, output content whenever something arrives rather than buffering all of it and output when the script code execution is finished.
    Perhaps I am now focusing on somewhat unimportant aspect, ie, code with longer-than-immediate execution time, but I just want to hedge my risks for future.

    Hoolean
    Regarding the simplification of Bukkit classes, I assume you meant that the wrapping would done by developing a set of Java classes, rather than Python class wrappers processed in Jython, right? There are pros and cons for both, but having that code nicely compiled and running on the JVM perhaps has pretty significant performance benefits.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  15. Offline

    Hoolean

    Passiday

    Coding the classes in Java would definitely be the way to go there. The performance boost would be great plus you'd be working with the same language as the Bukkit API, which would be good if you want to keep all the classes you'd interact with in Jython the same language. :)
     
  16. Offline

    Passiday

    Hoolean Axe2760 mkremins hatstand
    Here's a little update (see the attached screenshot):
    • It's a web application (using Python micro-framework Flask)
    • Keeping the data in MySQL database
    • Bukkit plugin reading the code from the database and executing in persistent Jython interpreter
    • Jython output is captured and written in MySQL database, thus letting the web app read&display it.
    • Bukkit events are passed to event manager at the Jython side that triggers the handlers.
    Works pretty smooth, turns out actually a great tool for quick idea implementing and understanding the Bukkit classes.
    What's left is some basic user management with ability to manage several such sessions, switch between them, activate/deactivate them. The major issue I see is that running Jython code at the Bukkit server side is a pretty serious security issue. It's fine while your students are not malicious, but that might not always be the case (ie, imagine a class of smartpants). I don't know how to limit the things what the Jython script can do, ie, file access, http requests, etc.
    [​IMG]
     
    Axe2760 likes this.
  17. Offline

    Zarkopafilis

    I know some python. I know very well Java . I dont think that should be a good idea. Python is way less simple in my opinion. And finally... gah , I cant remember...someone made a server wrapper in python...for bukkit.
     
  18. Offline

    Hoolean

    Zarkopafilis

    Python is a lot simpler to deploy, quicker to write and quicker to get going. Additionally, due to the simplicity of its syntax, it makes an ideal language for learning to program. :)
     
  19. Offline

    Zarkopafilis

    Hoolean I personally would find it harder to lean python and them try to learn um.... Java/C/C++. I say , better learn a strong language. It will take more time , but then everything will be easier. e.g. If you know Java , PHP and HTML (and C?, I am not into the C stuff) Ruby Perl Python etc would be easy to learn! and Python to java? no....I mean .... WHY!?
     
  20. Offline

    Hoolean

    Zarkopafilis

    True but in this case we are teaching people who have most likely never coded before in their life to program. Throwing them into the deep end with public/static/void modifiers and OOP (Object Orientated Programming) would be depremental.

    What people who are starting to code - and need to be drawn into it to persevere - are instant results. The compile time, deployment, other such features of a language that has to be compiled do not allow this, hence the reasons that one of the scripting languages you mentioned would be more applicable. (e.g. Python, JavaScript)

    This is meant to draw them in and let them have fun quickly without having to make much code; once they've decided they enjoy programming and know the basics will it be more beneficial for them to check out one of the more powerful languages; for now, there is no need for them. :)
     
    Axe2760 and Passiday like this.
  21. Offline

    Zarkopafilis

    You know , im 15 years old. I started learning java with no programming background. So , you decide.
     
  22. Offline

    Hoolean

    Zarkopafilis

    I started Java programming two years younger than you after doing work with scripting languages for half a year.
     
    Axe2760 likes this.
  23. Offline

    Passiday

    I am reaching 8 year olds here.
     
    Hoolean likes this.
  24. Offline

    Hoolean

    Not relevant; you obviously began because you had the commitmenet to learn an OOP, most likely to code Bukkit plugins.

    As I have mentioned, they will want to see instant results and easy deployment as - at this stage - this is what will appeal to them.

    Scripting languages are definitely the way to go with the age of the kids that are being taught; we don't want to throw them in the deep end with no lifejacket!

    Anyways, enough of this. We don't want to derail the thread :)
     
  25. Offline

    Zarkopafilis

    Why everybody thinks that?!?! No , I did not started to learn Java to code Bukkit plugins. I started it for game developing , and I am NOT ONLY creating plugins!(I started learning java 5 years ago , but only the last 2 that good), I have also started making my firsrt 2D game and made some other apps for friends. I like the lifejacket thing. It would be a good idea to do something like that , but like most (good) ideas , they require too much work for a little payback. If you want to do something like it , make a wrapper , or rewrite Bukkit?xD Hoolean how old are you now?
     
  26. Offline

    anton8604

    Any updates? I was hoping for something kind of like ScriptCraft where you can make your own plugins too(they use javascript, but Python variation should be feasible)
     
  27. Offline

    Passiday

    Well, the development of this tool has somewhat slowed down (ie, the real work interferes with this :) , I will share it with the world, but I have to reach somewhat stable state in order to do it. I'm not there yet. I hope to test this tool with class of about 7 users and see how well that goes. After some testing and adjustment iterations the first version will be published and I'll post update in this thread.
     
  28. Offline

    Brixishuge

    Passiday If you want, I can help you. I'm coding in python, using PyPluginLoader for almost 8+ months... I coded really complex plugins with it, such as economy, minigames, events and others. Also take a look of plugins I uploaded to devbukkit, they are all written in python, plus, they are opensourced! ^^

    Great idea! :) Python is great coding language and opens huge amount of possibilities. Only bad thing that I discovered while coding in python is that you need to solve problems by yourself. Others can help you with bukkit api and others, but when you have string formatting or any other python error, you have to deal it by yourself :D

    Keep it up! Cheers!
     
  29. Offline

    Passiday

    Yeah, I find Python as a great first language for teaching programming. And, with infinite depth, when one wants to get advanced.
    Of course, this forum will not tell you how to do something in Python, but there are other great forums (ie, http://stackoverflow.com) with helpful people who will give incredibly elaborate answers.
    Regarding my plugin/tool, I really have to do my homework before I share the work with someone else. I will be happy then to have both your feedback and contributions.
     
Thread Status:
Not open for further replies.

Share This Page