MCPluginCheatSheet
by rynstwrt
About
1. Naming Conventions
- Package Name: websiteending/me.name/websiteurl.projectname
- Class Name: ProjectNameCamelCase.java
2. Initial Functions
All must be below a @Overide metadata tag
public void onEnable() {}
public void onDisable() {}
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {return false;}
3. plugin.yml Configuration
```yaml
name: ProjectName
main: packagename.MainClass
version: 0.1
author: YourName
commands:
ping:
aliases: [png, pingerino]
description: simulates a ping-pong game
usage: /
permissions: myplugin.*: description: Full access to all of my plugin's commands children: myplugin.reload: true myplugin.anybodycanuse: true default: op myplugin.reload: description: Reloads my plugin. default: op myplugin.anybodycanuse: description: Just a test that any guest can use. default: true ```
4. Event Class
- Start Listener (in first class)
onEnable() {new <listenerclassname>(this);}
- Implement the listener class:
public class <listenerclassname> implements Listener {}
- Start with constructor:
public <listenerclassname>(<firsclass> plugin) {plugin.getServer().getPluginManager().registerEvents(this, plugin)}
- Events are then implemented under a @EventHandler metadata tag as
public void <functionName>(<eventHere> e) {}
- Custom Events
- Create new class for the event called "somethingSomethingEvent" that extends Event
- Create constuctor that (can) pass this.var = var.
- Create other functions needed for your event such as
getWarpLocation()
orgetPlayer()
- Write
private static final HandlerList handlers = new HandlerList();
- Create "getHandlers()" function that returns the handlerslist
- Create "getHanderList()" function that returns the handlerslist
- Call it in your code when wanted with
Bukkit.getServer().getPluginManager().callEvent(new <yourEventName>())
- Handle the event in your listener class like any other event.
5. Command Arguments
- Check if arguments
if (args.length == 1) {
-
Check if argument was a player (for this example) ```Java
boolean plrFound = false;
for (Player p : Bukkit.getServer().getOnlinePlayers()) { if (p.getName().equalsIgnoreCase(args[0])) { plrFound = true; p.setHealth(p.getMaxHealth()); break; } }
if (!plrFound) { plr.sendMessage("No player found!"); } ```
6. Permissions
- Create the permission in the main class
Permission canBuild = new Permission("myplugin.canbuild");
- Check if player has a permission
if (plr.hasPermission("myplugin.canbuild")) {}
7. Configuration Options (config.yml)
- Generate (if not already present) (put in onEnable() function)
this.getConfig().addDefault("playervisits", 0)
- Keep config as default
this.getConfig().options().copyDefaults(true);
- Save the config (put in onDisable() too)
saveConfig();
- How to get data
firstclass.getConfig().getInt("playervisits");
- How to set data
firstclass.getConfig().set("playervisits", configGetter.getConfig().getInt("playervisits") + 1);
- Check if something exists in the config
if (firstclass.getConfig().contains("playervisits.player1") {}
- Sections
- Referenced as
this.getConfig().getInt("playervisits.player1");
- Set as
plugin.getConfig().set("playervisits.player2", 200);
- Referenced as
- Multiple Configs
- Use
FileConfig x = YamlConfiguration.loadConfiguration(new File(plugin.getDataFolder(), "example.yml"));
yaml playervisits: player1: 10 player2: 100 player3: 1337
- Use
8. Scheduler
- Set up
Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable(<run function>) {}, startupTimeInTicksL, repeatingTimeInTicksL
- Times are in ticks (20 ticks = 1 second)
- Use
scheduleSyncRepeatingTask
for loops, andscheduleSyncDelayedTask
for waits (like cooldowns) - In total, a countdown will look like this:
Java if (cmd.getName().equalsIgnoreCase("count3")) { num = 3; Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable() { @Override public void run() { if (num > 0) { Bukkit.broadcastMessage("" + num); } else if (num == 0) { Bukkit.broadcastMessage("Go!"); } num--; } }, 0L, 1*20L); return true; }
9. Misc
- Keep players in place by teleporting the player to themselves on the
PlayerMoveEvent
- Use the TagAPI to change nametags above player's colors and text
Recommend MCP