An example of a python plugin
Off topic → Programming → An example of a python plugin
###Why am I writing this There seems to be a large amount of interest in how our python plugin system works so in an effort to dispel the queries I will try to give an idea of how we do what we do. If you have any other questions after this post please ask below, so lets begin shall we?
###How python plugins are loaded Python plugins are loaded using Python Plugin Loader. The Python Loader then uses some clever java stuffs and Jython when it loads the custom plugins. I will not go into to much detail about how the loader works as it is complicated and i don’t think I actually understand it myself.
###How do we write a plugin in python The python plugins are themselves written directly in ordinary python namely as a .py file.
###Can you just show me some code already? No
Only kiddin. Here we go…
#Import necessary bukkit packages
import org.bukkit as bukkit
import org.bukkit.Player as Player
#Listen for someone joining the noreply+3@redstoner.com("player.PlayerJoinEvent", "monitor")
def on_join(event):
player = event.getPlayer()
#Lets get chatty
if not player.hasPlayedBefore():
broadcast("")
broadcast("OMFG, look " + player.getDisplayName() + " is &lNEW&r!!!!!")
broadcast("")
elif: player.getDisplayName() == "Pigglypops":
broadcast("")
broadcast("You wont believe it but...")
broadcast("A real life Pigglypops just joined")
broadcast("")
#Hmm should definately be noreply+4@redstoner.com("block.blockBreakEvent", "monitor"):
def russian_roulette(event):
player = event.getPlayer()
location = player.getLocation()
explosion_power = "40F" #Hmmmm TNT is 4F sooooo 40F should be ok
#Generate a random number between 0 and 100
import random
fire_gun = random.randint(0, 100)
#Lets do something fun shall we?
if fire_gun == 69 or player.getDisplayName() == Dico200:
msg("its nothing personal but you are about to die!! Bye Bye")
player.getWorld().createExplosion(location, explosion_power)
###Sooooo what does it actually do? The code above adds two functions to the game:
- When a player joins the server it checks if they are new and broadcasts a message, or if it is me (Pigglypops) gives a custom broadcast message
- Every time a block is broken there is a 1/100 chance that they will get killed in a ridiculously large explosion.
The sample is nothing serious and is just to give a brief example of how not what we code, for those that are interested.
###Just to finish off So that is my example which should be simple enough to follow along with however if you do have any questions please feel free to ask and I or any other staff will be more than happy to provide you with an answer.
Bye your friendly neighbourhood Pig
Thanks! But isn’t there something wrong:
Every time a block is broken there is a 1/100 chance that they will get killed in a ridiculously large explosion.
It uses PlayerMoveEvent
?
Otherwise, you’ll have to add that to your redstoner-utils ;).
randint
in clojure such that (rand-int n)
where it is between 0 (inclusive) and n (exclusive).Okay. I’m veery slowly getting the grisp of python plugins. A few questions:
You are writing this with the decorator api?
And how do I get my plugin running? I know that to install python plugin loader you have to put the PPL-Jar into the /plugins-folder, and the jython.jar in the /lib folder. What now? Where do I put my plugins? And the plugin.yml?
Could you explain that to me? Thanks!
Ok, it’s clear After reading your readme - it’s different from the One I read.
Thanks!
###To prove to you that python is easier than (my favourite language) java:
–Some java code that does the same
#Import stuff
import org.bukkit.*;
public void onEnable()
{
this.getServer().getPluginManager().registerEvents(this,this);
}
public void on_join(PlayerJoinEvent event)
{
Player player = event.getPlayer();
if (!(player.hasPlayedBefore()))
{
Bukkit.broadcast("");
Bukkit.broadcast("OMFG, look " + player.getName() + " is "+ ChatColor.BOLD + "NEW" + ChatColor.RESET + "!!!!!");
Bukkit.broadcast("");
}
else if (player.getName().equals("Pigglypops")
{
Bukkit.broadcast("");
Bukkit.broadcast("You won’t believe it but…");
Bukkit.broadcast("A real life Pigglypops just joined");
Bukkit.broadcast("");
}
}
public void russian_roulette(block.BlockBreakEvent event)
{
Player player = event.getPlayer();
Location location = event.getLocation();
float explosion_power = 40;
fire_gun = Random.nextInt(100);
if (fire_gun == 69 || player.getName().equals("Dico200"))
{
player.sendMessage("its nothing personal but you are about to die!! Bye Bye");
player.getWorld().createExplosion(location, explosion_power);
}
}
Wow either something is wrong with my code or higlight.js…
nvm was my code
Made the code Panda-Compatible. :3 (@PanFritz)