Skip to content
Tad Hardesty edited this page Jun 17, 2014 · 3 revisions

Importing

To import Java classes, such as Bukkit classes, the following is possible:

# use 'World' to refer to the World interface
from org.bukkit import World
# use 'Player', 'Creeper', and 'Zombie' to refer to the entity classes
from org.bukkit.entity import Player, Creeper, Zombie

# access Java's System class
from java.lang import System

Note that most of the time you don't need to import classes unless you plan to construct instances or use static methods or variables.

Accessing properties

Many objects have getters and setters - methods with signatures like int getThing() and void setThing(int thing). In Python, it's still possible to access these methods directly, but they also act as properties:

server = Bukkit.getServer()
maxPlayers = server.getMaxPlayers()  # works as normal
maxPlayers = server.maxPlayers       # also works
server.setWhitelist(True)            # works as normal
server.whitelist = True              # also works

Functional interfaces

In Java, functional interfaces are interfaces or abstract classes with exactly one abstract method. In Java 8, lambdas can be used to implement interfaces of this type. In Jython, Python functions can be used as arguments to any method that expects such an interface as an argument. The most important functional interfaces for plugin development are:

Class Method
CommandExecutor boolean onCommand(CommandSender sender, Command command, String label, String[] args)
TabCompleter List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args)
Runnable void run() (for scheduler programming)

For example, a function can be scheduled to run later like so:

def timer():
    print "Timer finished"

# in plugin class
    def run_timer(self):
        # 'timer' is passed where a Runnable is expected
        self.server.scheduler.runTaskLater(self, timer, 20)

Clone this wiki locally