codename_B's list of plugins under 50 lines of code AKA the under 50 challenge

Discussion in 'Resources' started by codename_B, Aug 23, 2011.

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

    oxguy3

    Nope, you can only use an asterisk for the name of a class, not of a package (I know because I tried this when I was a Java noob)
     
  2. Offline

    Perdog

    Another one for the list :)

    MoarExp:
    Code:java
    1. package me.Perdog.MoarExp;
    2. import java.util.logging.Logger;
    3. import org.bukkit.event.Event.Priority;
    4. import org.bukkit.event.Event.Type;
    5. import org.bukkit.event.entity.EntityDeathEvent;
    6. import org.bukkit.event.entity.EntityListener;
    7. import org.bukkit.plugin.PluginManager;
    8. import org.bukkit.plugin.java.JavaPlugin;
    9. public class MoarExp extends JavaPlugin {
    10. public String name;
    11. Logger log = Logger.getLogger("Minecraft");
    12. public PluginManager pm = null;
    13. public void onEnable() {
    14. name = this.getDescription().getFullName();
    15. setupConfig();
    16. registerListeners();
    17. log.info(name + " has been enabled");
    18. }
    19. public void onDisable() {
    20. log.info(name + " has been disabled");
    21. }
    22. public void setupConfig() {
    23. this.getConfig().options().copyDefaults(true);
    24. this.saveConfig();
    25. }
    26. private void registerListeners() {
    27. pm = this.getServer().getPluginManager();
    28. MEEL entityListener = new MEEL(this);
    29. pm.registerEvent(Type.ENTITY_DEATH, entityListener, Priority.Normal, this);
    30. }
    31. public class MEEL extends EntityListener {
    32. public MoarExp plugin;
    33. public MEEL (MoarExp instance) {
    34. plugin = instance;
    35. }
    36. public void onEntityDeath(EntityDeathEvent event) {
    37. if (plugin.getConfig().contains(event.getEntity().toString())) {
    38. event.setDroppedExp(plugin.getConfig().getInt(event.getEntity().toString()));
    39. }
    40. }
    41. }
    42. }
     
  3. Offline

    ShootToMaim

    My god. That shouldn't even work properly xD
    But since it does, you sir are a legend.
     
  4. Offline

    tips48

    Wrong...I import java.util.* or com.tips48.rushMe.packets.* and neither of those are classes ;)
     
  5. Offline

    Perdog

    No but what's inside are classes :p I think what he meant was that the asterisk can only substitute a class, and not a package. So doing com.tips48.* wouldn't work.
     
  6. Offline

    TechHut

    Would I be able to use some of the codes from here in a plugin I'm making?
    @codename_B
     
  7. Offline

    Perdog

    You should probably ask the individuals that posted the code, since they are the ones that posted it :p
     
  8. Offline

    LRFLEW

    How exactly is @codename_B counting the lines? Does he count comments (which is stupid to have if you're tying to limit line numbers anyways)?

    A plugin I'm making (that is under 50 lines so far) has those weird if-statements-with-no-bracket things that all go like this:
    Code:
    if (someBooleanStatement) return;
    Is that considered two lines because two actions are being performed? I realize that if it does count, then this could also count as just one line:
    Code:
    someFunction(); anotherFunction()
    Which is why I'm asking where do you draw the line. The only reason I think it should count is because this is the same as:
    Code:
    if (!someBooleanStatement) {
    which I didn't use because it looked weird once the auto-indents were included.
     
  9. Offline

    V10lator

    Just try to be fair to yourself... Try to write the code as you would write it normally (yes, cause of that I couldn't post a few codeblocks...) This is not a real challenge (correct me if I'm wrong) it's just a funny (kind of) game... :)
     
  10. Offline

    codename_B

    The general rule is one ";" per line, 150 characters max per line.

    This helps you do lots of trinary operator statements and other fun tricks to keep line length down.
     
  11. Offline

    V10lator

    While I think all my codes fall under your rules I still like the idea of "being fair to yourself" more and that's the rule I will continue to use for myself. Who would write such code if he had to maintain it? If you're fair to yourself you write it how you would write every plugin: Easy readable for yourself, even after a few years. ;)
     
  12. Offline

    Kodfod

    Code:java
    1. package com.josh.item;
    2.  
    3. import java.util.logging.Logger;
    4.  
    5. import org.bukkit.event.Event.Priority;
    6. import org.bukkit.event.Event.Type;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8. public class itemMain extends JavaPlugin {
    9. Logger log = Logger.getLogger("Minecraft");
    10. private Listener_Player listenerPlayer;
    11. @Override
    12. public void onDisable() {
    13. log.info("Bed Time Has Been Disabled!");
    14. }
    15. @Override
    16. public void onEnable() {
    17. log.info("Bed Time Has Been Enbled!");
    18. listenerPlayer = new Listener_Player(this);
    19. getServer().getPluginManager().registerEvent(Type.PLAYER_BED_ENTER, listenerPlayer, Priority.Normal, this);
    20. }
    21. }


    And the Player Listener:
    Code:java
    1. package com.josh.item;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.World;
    6. import org.bukkit.event.player.PlayerListener;
    7. import org.bukkit.event.player.PlayerBedEnterEvent;
    8. import com.josh.item.itemMain;
    9.  
    10. public class Listener_Player extends PlayerListener {
    11. private itemMain plugin;
    12. public Listener_Player(itemMain plugin){
    13. this.plugin = plugin;
    14. }
    15. @Override
    16. public void onPlayerBedEnter(PlayerBedEnterEvent event){
    17. Bukkit.getServer().broadcastMessage(ChatColor.GOLD + "A player Decided to Go to bed. Turning it to day time.");
    18. Player player = event.getPlayer();
    19. player.getWorld().setTime (0);
    20. player.setHealth(20);
    21. }
    22. }
    23. }


    Total 44 lines.
    When someone goes into a bed it heals them fully and sets time to 0 ticks (6am)


    EDIT:
    plugin.yml:
    Code:
    name: BedTime
    version: 0.1
    description: This Plugin Allows a user to change the time from night to day and heals them. (instant)
    author: kodfod
    main: com.josh.item.itemMain
    database: false
     
  13. Offline

    tips48

    You should move the setting of the players health out of the for loop :)
     
  14. Offline

    Digi

    Also, you could remove that plugin variable since along with the constructor, you're not using them anyway... but you could also make all that in one file while saving a few lines by using new PlayerListener() inside the registerEvent().

    I wrote it that way and using my style it has exacly 49 lines =) but just by placing brakets on the previous lines it had 43 lines, other empty lines can also be removed... and even more, you can use .* for imports, it's perfectly fine because AFAIK Java imports only the necesairy stuff from those folders.
     
  15. Offline

    V10lator

    @Kodfod: And you could change this:
    Code:java
    1. for (World world : Bukkit.getWorlds()) {
    2. world.setTime (0);
    3. event.getPlayer().setHealth(20);
    4. }

    to:
    Code:java
    1. Player player = event.getPlayer();
    2. player.getWorld().setTime (0);
    3. player.setHealth(20);

    It's better for multiple worlds and you save 1 line. ;)
     
  16. Offline

    Kodfod

    @tips48 Thanks for that, didn't notice that
    @Digi I wrote it like that at 1st and decided to go with this way (Easier to read.... well for me & makes more sense to me =P)
    @V10lator Thanks for the tip However, The "Player player = event.getPlayer();" the Player part needs to be imported and there goes the line you just saved. However this is easier to read and more logical. Thanks!

    Thanks for the feedback, looks like i did OKAY for a first plugin =P if i do say so myself
     
  17. Offline

    V10lator

    LastSeen, shows the time a player was last seen on the server:
    Code:java
    1. package de.V10lator.LastSeen;
    2.  
    3. import java.text.SimpleDateFormat;
    4. import java.util.Date;
    5.  
    6. import org.bukkit.OfflinePlayer;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.plugin.PluginDescriptionFile;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class LastSeen extends JavaPlugin
    13. {
    14. public void onEnable()
    15. {
    16. PluginDescriptionFile pdf = getDescription();
    17. getServer().getLogger().info("["+pdf.getName()+"] v"+pdf.getVersion()+" enabled!");
    18. }
    19. public void onDisable()
    20. {
    21. getServer().getLogger().info("["+getDescription().getName()+"] Disabled!");
    22. }
    23. public boolean onCommand(CommandSender sender, Command command,
    24. String label, String[] args)
    25. {
    26. if(args.length < 1)
    27. return false;
    28. OfflinePlayer p = getServer().getOfflinePlayer(args[0]);
    29. if(p.isOnline())
    30. sender.sendMessage(args[0] + " is online!");
    31. else if(!p.hasPlayedBefore())
    32. sender.sendMessage(args[0] + " was never seen!");
    33. else
    34. sender.sendMessage(args[0] + " was last seen at " + (new SimpleDateFormat("dd/MM/yy H:m 'o''Clock' (z)")).format(new Date(p.getLastPlayed())));
    35. return true;
    36. }
    37. }

    [​IMG]
    But I'm unsure about releasing this as it seems there are a few plugins (like essentials) which still provide such a feature... What do you think?
     
  18. Offline

    LRFLEW

  19. Offline

    md_5

    MagicWord: Make users read the rules
    @daemitus
    @Fishfish0001
    40 Lines
    Code:java
    1. package com.md_5.magicword;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.event.Event;
    6. import org.bukkit.event.Event.Priority;
    7. import org.bukkit.event.player.PlayerChatEvent;
    8. import org.bukkit.event.player.PlayerListener;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10.  
    11. public class MagicWord extends JavaPlugin {
    12.  
    13. public void onEnable() {
    14. getConfig().options().copyDefaults(true);
    15. saveConfig();
    16. getServer().getPluginManager().registerEvent(Event.Type.PLAYER_CHAT, new PlayerListener() {
    17.  
    18. @Override
    19. public void onPlayerChat(PlayerChatEvent event) {
    20. for (String w : getConfig().getStringList("words")) {
    21. if (event.getMessage().equalsIgnoreCase(w)) {
    22. getServer().dispatchCommand(Bukkit.getConsoleSender(), String.format(getConfig().getString("command"), event.getPlayer().getName()));
    23. event.getPlayer().sendMessage(getConfig().getString("message"));
    24. event.setCancelled(true);
    25. return;
    26. }
    27. }
    28. if (!event.getPlayer().hasPermission("magicword.chat")) {
    29. event.setCancelled(true);
    30. event.getPlayer().sendMessage(ChatColor.RED + getConfig().getString("blockMessage"));
    31. }
    32. }
    33. }, Priority.Normal, this);
    34. System.out.println(String.format("[MagicWord] Version %1$s by md_5 enabled", getDescription().getVersion()));
    35. }
    36.  
    37. public void onDisable() {
    38. System.out.println("[MagicWord] Disabled");
    39. }
    40. }
    41.  
    42.  
     
  20. Offline

    xGhOsTkiLLeRx

    @Avous

    NoStarvation
    Download!

    If your food-bar (food level) is 0 it will cancel the starvation event.

    Total lines: 30 (and 4 in plugin.yml) = 34
    Code:java
    1. package de.xghostkillerx.nostarvation;
    2.  
    3. import org.bukkit.entity.Player;
    4. import org.bukkit.event.EventHandler;
    5. import org.bukkit.event.Listener;
    6. import org.bukkit.event.entity.EntityDamageEvent;
    7. import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
    8. import org.bukkit.plugin.java.JavaPlugin;
    9.  
    10. public class NoStarvation extends JavaPlugin implements Listener {
    11.  
    12. public void onDisable() {
    13. getServer().getLogger().info("NoStarvation has been disabled!");
    14. }
    15.  
    16. public void onEnable() {
    17. getServer().getPluginManager().registerEvents(this, this);
    18. getServer().getLogger().info("NoStarvation is enabled!");
    19. }
    20.  
    21. @EventHandler
    22. public void onEntityDamage(final EntityDamageEvent e) {
    23. if (e.getEntity() instanceof Player) {
    24. Player player = (Player) e.getEntity();
    25. if (player.getFoodLevel() == 0 && e.getCause() == DamageCause.STARVATION) {
    26. e.setCancelled(true);
    27. }
    28. }
    29. }
    30. }

    Code:
    name: NoStarvation
    main: de.xghostkillerx.nostarvation.NoStarvation
    version: 1.0
    author: xGhOsTkiLLeRx
     
  21. Offline

    Fishrock123

    Newest version of NoEnderGrief:

    (40 Lines of code! :D)
    (2.44 kb compiled & compressed (eclipse) with plugin.yml, 1.634kb raw.)

    Code:java
    1. package Fishrock123.NoEnderGrief;
    2. import java.util.ArrayList;
    3. import java.util.Arrays;
    4. import java.util.List;
    5. import java.util.logging.Logger;
    6. import org.bukkit.event.Event;
    7. import org.bukkit.event.entity.EndermanPickupEvent;
    8. import org.bukkit.event.entity.EndermanPlaceEvent;
    9. import org.bukkit.event.entity.EntityListener;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11. public class NoEnderGrief extends JavaPlugin {
    12. protected static List<String> EWs = new ArrayList<String>();
    13. @SuppressWarnings("unchecked")
    14. public void onEnable() {
    15. if (!getConfig().contains("ExemptedWorlds")) {
    16. getConfig().addDefault("ExemptedWorlds", Arrays.asList("exempted_worldname"));
    17. getConfig().options().copyDefaults(true);
    18. saveConfig();
    19. }
    20. EWs = getConfig().getStringList("ExemptedWorlds");
    21. getServer().getPluginManager().registerEvent(Event.Type.ENDERMAN_PICKUP, new eListener(), Event.Priority.High, this);
    22. getServer().getPluginManager().registerEvent(Event.Type.ENDERMAN_PLACE, new eListener(), Event.Priority.High, this);
    23. Logger.getLogger("Minecraft").info("NoEnderGrief version 1.1.2 (1A7) enabled!");
    24. }
    25. public void onDisable() {
    26. Logger.getLogger("Minecraft").info("NoEnderGrief disabled!");
    27. }
    28. }
    29. class eListener extends EntityListener {
    30. public void onEndermanPickup(EndermanPickupEvent e) {
    31. if (NoEnderGrief.EWs == null || !NoEnderGrief.EWs.contains(e.getBlock().getLocation().getWorld().getName())) {
    32. e.setCancelled(true);
    33. }
    34. }
    35. public void onEndermanPlace(EndermanPlaceEvent e) {
    36. if (NoEnderGrief.EWs == null || !NoEnderGrief.EWs.contains(e.getLocation().getWorld().getName())) {
    37. e.setCancelled(true);
    38. }
    39. }
    40. }


    Code:
    name: NoEnderGrief
    main: Fishrock123.NoEnderGrief.NoEnderGrief
    version: 1.1.2 (1A7)
    Oh wow. I had no idea you could do this! :eek:

    Edit: Huh. That actually makes my plugin bigger in compiled filesize by .8kb ...

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 29, 2015
  22. Offline

    hammale

    Total lines: 28 (and 7 in plugin.yml) = 35



    Code:java
    1.  
    2. package me.hammale.boom;
    3.  
    4. import org.bukkit.command.Command;
    5. import org.bukkit.command.CommandSender;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.plugin.java.JavaPlugin;
    8.  
    9. public class boom extends JavaPlugin{
    10. public void onDisable(){
    11. System.out.println("[" + this + "] Disabled!");
    12. }
    13. public void onEnable(){
    14. System.out.println("[" + this + "] Enabled!");
    15. }
    16. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    17. if(cmd.getName().equalsIgnoreCase("boom")){
    18. if(args.length != 0){
    19. if(sender instanceof Player){
    20. if(getServer().getPlayer(args[0]) != null){
    21. getServer().getPlayer(args[0]).getWorld().createExplosion(getServer().getPlayer(args[0]).getLocation(), 2F);
    22. return true;
    23. }
    24. }
    25. }
    26. }
    27. return false;
    28. }
    29. }
    30.  

    Then for the plugin.yml:
    Code:
    name: Boom
    main: me.hammale.boom.boom
    version: 1.0
    commands:
      boom:
        description:
        usage: "/boom <player>"
    
    Well under 50 lines but still super fun to use! When you do '/boom <player>' it creates an explosion just powerful enough to kill <player>.
     
  23. Offline

    DDoS

    A trampoline plugin i made one day with my brother Suprem20. Compressed under 50 lines of code. Got rid of everything that wasn't necessary, like the logger, and used static for the set of bouncing players. I did this compression quickly, please don't comment on it, I know it's not perfect at all.

    BouncyBounce.java:

    Code:java
    1. package me.DDoS.bouncybounceunder50;
    2.  
    3. import java.util.HashSet;
    4. import java.util.Random;
    5. import org.bukkit.Material;
    6. import org.bukkit.block.BlockFace;
    7. import org.bukkit.entity.Player;
    8. import org.bukkit.event.Event.Priority;
    9. import org.bukkit.event.Event.Type;
    10. import org.bukkit.event.entity.EntityDamageEvent;
    11. import org.bukkit.event.entity.EntityListener;
    12. import org.bukkit.event.player.PlayerListener;
    13. import org.bukkit.event.player.PlayerMoveEvent;
    14. import org.bukkit.plugin.java.JavaPlugin;
    15. import org.bukkit.util.Vector;
    16.  
    17. public class BouncyBounce extends JavaPlugin {
    18. private static final HashSet<Player> bouncers = new HashSet<Player>();
    19. public void onEnable() {
    20. getServer().getPluginManager().registerEvent(Type.PLAYER_MOVE, new BBPlayerListener(), Priority.High, this);
    21. getServer().getPluginManager().registerEvent(Type.ENTITY_DAMAGE, new BBEntityListener(), Priority.Normal, this);
    22. }
    23. public void onDisable() {}
    24. private class BBPlayerListener extends PlayerListener {
    25. private final Random random = new Random();
    26. public void onPlayerMove(PlayerMoveEvent event) {
    27. if (event.isCancelled()) return;
    28. Player player = (Player) event.getPlayer();
    29. if (event.getTo().getBlock().getRelative(BlockFace.DOWN).getType() == Material.SPONGE) {
    30. if (!player.isSneaking()) player.setVelocity(new Vector(0, (1 + random.nextInt(2)), 0));
    31. BouncyBounce.bouncers.add(player);
    32. } else {
    33. if (BouncyBounce.bouncers.contains(player)) BouncyBounce.bouncers.remove(player);
    34. }
    35. }
    36. }
    37. private class BBEntityListener extends EntityListener {
    38. public void onEntityDamage(EntityDamageEvent event) {
    39. if (event.isCancelled()) return;
    40. if (!(event.getEntity() instanceof Player)) return;
    41. if (BouncyBounce.bouncers.contains((Player) event.getEntity())) event.setCancelled(true);
    42. }
    43. }
    44. }


    plugin.yml:

    Code:
    name: BouncyBounceUnder50
    main: me.DDoS.bouncybounceunder50.BouncyBounce
    version: 0.1
    author: DDoS
    

    Sponges make you bounce. When bouncing, players don't loose health due to fall damage (unless they land on something else than sponges). To stop bouncing, simply sneak.

    If you want to try it, use this build, that's the better, uncompressed, version.
     
  24. Offline

    Tj3

    NuclearW's godmode has a major flaw:
    Code:java
    1. event.setCancelled((event.getEntity() instanceof Player) ? true : false);

    Should be:
    Code:java
    1. event.setCancelled((event.getEntity() instanceof Player) ? true : event.isCancelled());

    Otherwise it won't play nice with other plugins. I'm so happy, I just corrected a bukkit administrator's code. I feel special. :D
     
    thehutch and zhuowei like this.
  25. Offline

    DDoS

    I don't think he was an admin when he posted this code...

    And being a Bukkit admin doesn't automatically mean your a code expert. Even more, many of these plugins haven't been thoroughly tested, and some may have mistakes.

    But you're right, it's a better way to do it.
     
  26. Offline

    Digi

    Why don't you just use FOOD_LEVEL_CHANGE event ?
     
  27. Offline

    xGhOsTkiLLeRx

    @Digi

    The request was to cancel the damage from the starvation but keep the hunger part (no more sprinting). So I decided to cancel the starvation damage when the hunger == 0.
     
  28. Offline

    Digi

    I think that event can be used for that purpose as well.
     
  29. Offline

    xGhOsTkiLLeRx

    @Digi

    Yeah I could set the food level again to 1, when it reaches 0.
    Or cancel the event when it reaches 0.
     
  30. Offline

    Evangon

    I'm going to be releasing a plugin that is only 51 lines, assuming that the file editing and loading doesn't take another thousand lines.

    ForceChatting - 41 Lines :3
    (Yes, I cheated by using direct access instead of importing)
    Code:java
    1. package fiftyftw;
    2. public class fiftylineshere extends org.bukkit.plugin.java.JavaPlugin {
    3. org.bukkit.Server server = getServer();
    4. java.util.logging.Logger log = java.util.logging.Logger.getLogger("Minecraft");
    5. public boolean onCommand (org.bukkit.command.CommandSender sender, org.bukkit.command.Command command, String label, String[] args0) {
    6. if(command.getName().equalsIgnoreCase("forcechat")){
    7. if(sender.hasPermission("forcechat.use.normal")){
    8. if(args0.length > 1){
    9. String[] temp = java.util.Arrays.copyOfRange(args0, 0, args0.length);
    10. String hax0r = "";
    11. for(int i=1; i < temp.length; i++) hax0r = hax0r + " " + temp[I];[/I]
    12. hax0r = hax0r.replaceFirst(hax0r.substring(0,1), "");
    13. if(!(args0[0].contains("/"))){
    14. log.info(sender.getName() + " has forced " + args0[0] + " to say " + hax0r + ".");
    15. server.getPlayer(args0[0]).chat(hax0r);
    16. return true;
    17. } else if(sender.hasPermission("forcechat.use.forcecommand")){
    18. log.info(sender.getName() + " has forced " + args0[0] + " to execute " + hax0r + ".");
    19. server.getPlayer(args0[0]).chat(hax0r);
    20. }
    21. } else {
    22. sender.sendMessage(org.bukkit.ChatColor.RED + "You must provide 2 varibles in this order: Player Name, Message");
    23. return true;
    24. }
    25. }
    26. }
    27. return false;
    28. }
    29.  
    30. @Override
    31. public void onDisable() {
    32. log.info("ForceChat has been disabled!");
    33. }
    34.  
    35. @Override
    36. public void onEnable() {
    37. log.info("ForceChat has been enabled!");
    38. }
    39. }
    40.  
    41. [I][I][/I][/I]


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 29, 2015
Thread Status:
Not open for further replies.

Share This Page