Copy+Paste Me (example plugin, copy + paste as needd)

Discussion in 'Resources' started by captainawesome7, Jul 12, 2011.

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

    captainawesome7

    Copy+Paste Me
    In my opinion setting up the base code for a plugin is just annoying, so you can copy + paste whatever you need from these classes.

    Features:
    • Example main class with:
      • How to set up permissions support
      • How to set up Bukkit's Standard Configuration
      • How to set up listeners
      • How to set up command executors
    • Example Command Executor class with:
      • How to modify config values with a command
      • How to reload a config
    • Example Player Listener class with:
      • Example Event Registered (PlayerInteract)
    • Example Block Listener class with:
      • Example Event Registered (BlockPlace)
    • Example Entity Listener class with:
      • Example Event Registered (EntityDamage)
    Downloads:

    Changelog:

    Version 1.1
    • Changed onEnable message (so you don't have to change that when you copy+paste)
    • *Not in the downloadable .jar, only in the source spoilers*
    Version 1.0

    • Release
    Source:

    CPM.class (open)


    Code:
    package me.ic3d.cpm;
    //Java Imports
    
    import java.io.File;
    import java.util.logging.Logger;
    
    import org.bukkit.entity.Player;
    import org.bukkit.event.Event;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.util.config.Configuration;
    
    import com.nijiko.permissions.PermissionHandler;
    import com.nijikokun.bukkit.Permissions.Permissions;
    
    public class CPM extends JavaPlugin {
    
        //PERMISSIONS
        private boolean UsePermissions;
        public static PermissionHandler Permissions;
        private void setupPermissions() {
            Plugin test = this.getServer().getPluginManager().getPlugin("Permissions");
            if (this.Permissions == null) {
                if (test != null) {
                    UsePermissions = true;
                    this.Permissions = ((Permissions) test).getHandler();
                    System.out.println("[" + this.getDescription().getName() + "] Permissions system detected!");
                } else {
                    log.info("[" + this.getDescription().getName() + "] Permissions system not detected, defaulting to OP");
                    UsePermissions = false;
                }
            }
        }
        public boolean hasPerm(Player p, String string) {
            if (UsePermissions) {
                return this.Permissions.has(p, string);
            }
            return p.isOp();
        }
    
        //BlockListener
        private final BL blocklistener = new BL(this);
        //EntityListener
        private final EL entitylistener = new EL(this);
        //PlayerListener
        private final PL playerlistener = new PL(this);
    
        //CONFIG
        public Configuration config;
        public Boolean configBoolean;
        public String configString;
        public Integer configInt;
    
        //LOG
        private static final Logger log = Logger.getLogger("Minecraft");
        //ENABLE
        public void onEnable() {
            //PERMISSIONS
            setupPermissions();
            //LOG
    
            log.info("[" + this.getDescription().getName() + "] Version " + this.getDescription().getVersion() + " by " + this.getDescription().getAuthors().toString() + " enabled");
    
            //PM For Listeners
            PluginManager pm = getServer().getPluginManager();
            //Block Register
            pm.registerEvent(Event.Type.BLOCK_PLACE, blocklistener, Event.Priority.Normal, this);
            //Player Register
            pm.registerEvent(Event.Type.PLAYER_INTERACT, playerlistener, Event.Priority.Normal, this);
            //EntityListener
            pm.registerEvent(Event.Type.ENTITY_DAMAGE, entitylistener, Event.Priority.Normal, this);
            //CONFIG LOAD
            loadConfig();
            //GET COMMAND
            getCommand("cpm").setExecutor(new CMD(this));
        }
        //DISABLE
        public void onDisable() {
            log.info("[" + this.getDescription().getName() + "] Version " + this.getDescription().getVersion() + " by " + this.getDescription().getAuthors().toString() + " disabled");
        }
        //CONFIG LOAD
        public void loadConfig() {
            config = getConfiguration();
            configBoolean = config.getBoolean("Boolean", false);
            configString = config.getString("String", "string here");
            configInt = config.getInt("Integer", 0);
            config.save();
        }
        //CONFIG RELOAD
        public void reloadConfig() {
            File configFile = new File(this.getDataFolder() + "config.yml");
            config = new Configuration(configFile);
            configBoolean = config.getBoolean("Boolean", false);
            configString = config.getString("String", "string here");
            configInt = config.getInt("Integer", 0);
        }
        //CONFIG SAVE
        public void saveConfig() {
            config = getConfiguration();
            config.setProperty("String", configString);
            config.setProperty("Boolean", configBoolean);
            config.setProperty("Integer", configInt);
            config.save();
        }
    }


    CMD.class (open)

    Code:
    package me.ic3d.cpm;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class CMD implements CommandExecutor {
        private final CPM plugin;
    
          public CMD(CPM instance) {
          plugin = instance;
          }
          //The main part of a CommandExecutor class
          public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
              //I just threw stuff in here as an example.
              if(!(sender instanceof Player)) {
                  return false;
              }
              if(args.length == 0) {
                  sender.sendMessage(ChatColor.GOLD + "== CPM ==");
                  sender.sendMessage("/cpm reload");
                  sender.sendMessage("/cpm string <string>");
                  sender.sendMessage("/cpm boolean <boolean>");
                  sender.sendMessage("/cpm int <integer>");
                  return true;
              }
              if(args.length == 1) {
                  String arg1 = args[0];
                  if(arg1.equals("reload")) {
                      plugin.reloadConfig();
                  }
                  if(arg1.equals("string")) {
                        sender.sendMessage(plugin.configString);
                  }
                  if(arg1.equals("boolean")) {
                        sender.sendMessage(plugin.configBoolean.toString());
                  }
                  if(arg1.equals("int")) {
                        sender.sendMessage(plugin.configInt.toString());
                  }
              }
              if(args.length == 2) {
                  String arg1 = args[0];
                  if(arg1.equals("string")) {
                      String newString = args[1];
                      plugin.configString = newString;
                      plugin.saveConfig();
                      sender.sendMessage("String set to: " + plugin.configString);
                  }
                  if(arg1.equals("boolean")) {
                      Boolean newBoolean = Boolean.parseBoolean(args[1]);
                      plugin.configBoolean = newBoolean;
                      plugin.saveConfig();
                      sender.sendMessage("Boolean set to: " + plugin.configBoolean.toString());
                  }
                  if(arg1.equals("int")) {
                      Integer newInt = Integer.parseInt(args[1]);
                      plugin.configInt = newInt;
                      plugin.saveConfig();
                      sender.sendMessage("Int set to: " + plugin.configInt.toString());
                  }
              }
              return false;
           }
    }

    PL.class (open)

    Code:
    package me.ic3d.cpm;
    
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.event.player.PlayerListener;
    
    public class PL extends PlayerListener {
    
        public static CPM plugin;
    
        public PL(CPM instance) {
            plugin = instance;
        }
    
        public void onPlayerInteract(PlayerInteractEvent event) {
            //Do stuff
        }
    }

    BL.class (open)

    Code:
    package me.ic3d.cpm;
    
    import org.bukkit.event.block.BlockListener;
    import org.bukkit.event.block.BlockPlaceEvent;
    
    public class BL extends BlockListener {
    
        public static CPM plugin;
    
        public BL(CPM instance) {
            plugin = instance;
        }
    
        public void onBlockPlace(BlockPlaceEvent event) {
            //Do stuff
        }
    }

    EL.class (open)

    Code:
    package me.ic3d.cpm;
    
    import org.bukkit.event.entity.EntityDamageEvent;
    import org.bukkit.event.entity.EntityListener;
    
    public class EL extends EntityListener {
    
        private CPM plugin;
    
        public EL(CPM plugin) {
            this.plugin = plugin;
        }
    
        public void onEntityDamage(EntityDamageEvent event) {
            //Do stuff
        }
    }


    God I hate XenForo's text editor :mad:

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 17, 2016
  2. Offline

    Chris32

    This may be helpful to me! Many thanks for your hard work on this!
     
  3. Offline

    wouter0100

  4. Offline

    jeffadkins51

    VERY useful. Thanks for this mate.
     
  5. Offline

    amunro

    Thanks, so useful for me, however can you also show an example of using the permission nodes for your commands correctly? I seem to be having a few errors setting it up.
     
Thread Status:
Not open for further replies.

Share This Page