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

    Deleted user

    Doesn't say that in the rules :p.
     
  2. Offline

    md_5

    RULES: No cheating!
     
  3. Offline

    Deathmarine

    Toggle sleep.
    Code:java
    1.  
    2. package com.modcrafting.nosleeptill;
    3.  
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandExecutor;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.player.PlayerBedEnterEvent;
    12. import org.bukkit.plugin.java.JavaPlugin;
    13.  
    14. public class NoSleepTill extends JavaPlugin implements Listener, CommandExecutor{
    15. boolean toggle;
    16. public void onEnable(){
    17. this.getServer().getPluginManager().registerEvents(this, this);
    18. this.getCommand("sleep").setExecutor(this);
    19. }
    20. @EventHandler
    21. public void onPlayerBed(PlayerBedEnterEvent event){
    22. if(!toggle) event.setCancelled(true);
    23. }
    24. public boolean onCommand(final CommandSender sender, Command com, String label, final String[] args){
    25. if(sender instanceof Player){
    26. if(!((Player) sender).hasPermission("nosleeptill.use")){
    27. sender.sendMessage(ChatColor.RED+"You do not have permission to use this command.");
    28. return true;
    29. }
    30. }
    31. if(toggle){
    32. sender.sendMessage(ChatColor.GREEN+"Toggled sleep off.");
    33. toggle = false;
    34. }else{
    35. sender.sendMessage(ChatColor.GREEN+"Toggled sleep on.");
    36. toggle = true;
    37. }
    38. return true;
    39. }
    40.  
    41. }


    You know this is incredibly easy even without direct access.

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

    zhuowei

    You could probably remove that line as plugin classes by default receives commands before command listeners.
     
  5. Offline

    Deathmarine

    Huh... the more you know. Thanks.
     
  6. Also you don't have to implement CommandExecutor as the JavaPlugin class (indirectly) implements it. And you don't have to cast the CommandSender to a Player to check for permissions, simply use sender.hasPermission... ;)
     
  7. Offline

    Deathmarine

    So considering this with the singular command registered in the plugin.yml there is no need to actually check the command label as well... Hmm Interesting. However I was always under the assumption that the console as the sender would return false on the permissions check and the registering of the command was bound to the executor defined by the plugin.
     
  8. true.
    Not if you didn't do something weird. IIRC the ConsoleCommandSender has all permissions per default. Also you have the if(sender instanceof Player) check, so why even care about the console here? ;)
    This is the case, but if you don't specify a executor it defaults to your plugins main class (JavaPlugin). That's why JavaPlugin extends PluginBase which implements Plugin which extends CommandExecutor. ;)
     
  9. Offline

    Deleted user

    Is this still going on :3.
     
  10. Offline

    Fishrock123

    Always is. There is an ever present fascination and coolness for smaller code that does more. :3
     
  11. Offline

    Deleted user

    I have a SimpleRagequit plugin that i'm about to release... 58 lines. I can probably make it to about 42.
     
  12. Offline

    Fishrock123

    Then make it to 42 and post it here. ^_^ (You can brag about the filesize and lines to your users aswell? :p )
     
    Deleted user likes this.
  13. Offline

    WarmakerT

    Teleport to one of 3 random locations, 30 lines.

    Code:java
    1.  
    2. package me.warmakert.randomloc;
    3. import java.util.Random;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandSender;
    7. import org.bukkit.configuration.file.FileConfiguration;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.plugin.java.JavaPlugin;
    10. public class RandomLoc extends JavaPlugin{
    11. public void onEnable(){
    12. }
    13. public void onDisable(){
    14. }
    15. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    16. Player player = (Player) sender;
    17. if(cmd.getName().equalsIgnoreCase("setrand") && sender.hasPermission("randomloc.setrand") && args.length == 1){
    18. getConfig().set(String.valueOf(args[0] + ".x"), player.getLocation().getX());
    19. getConfig().set(String.valueOf(args[0] + ".y"), player.getLocation().getY());
    20. getConfig().set(String.valueOf(args[0] + ".z"), player.getLocation().getZ());
    21. player.sendMessage(ChatColor.GOLD + "Successfully set random location for: " + args[0]);
    22. saveConfig();
    23. } if(cmd.getName().equalsIgnoreCase("start") && sender.hasPermission("randomloc.start") && args.length == 0){
    24. Random rand = new Random();
    25. player.teleport(player.getWorld().getBlockAt(getConfig().getInt(
    26. 1+rand.nextInt(3)+ ".x"), getConfig().getInt(1+rand.nextInt(3)+ ".y"), getConfig().getInt(1+rand.nextInt(3)+ ".y")).getLocation());
    27. }
    28. return true;
    29. }
    30. }
    31.  
     
  14. Offline

    Icyene

    GoldFish: 49 lines. Fishing can make you rich!

    I <marginally> cheated a bit on this plugin. It gives the player a chance to obtain loot while fishing. Color code support for catch messages.

    Code:
    package com.github.Icyene.GoldFish;
     
    import java.io.File;
    import java.util.Random;
    import org.bukkit.*;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.player.PlayerFishEvent;
    import org.bukkit.event.player.PlayerFishEvent.State;
    import org.bukkit.inventory.*;
     
    public class GoldFish extends org.bukkit.plugin.java.JavaPlugin implements
    org.bukkit.event.Listener {
    private String[] keyArray;
    private final Random rand = new Random();
    private int chance = 50;
    private static String catchMessage, withoutBaitMessage;
    public GoldFish() {}
    public void onEnable() {
    final File configFile = new File(this.getDataFolder(), "config.yml");
    if (!configFile.exists()) {
    getConfig().options().copyDefaults(true);
    saveConfig();}
    this.getServer().getPluginManager().registerEvents(this, this);
    chance = getConfig().getInt("luring.lootChance");
    catchMessage = parseColors(getConfig().getString("luring.catchMessage"));
    withoutBaitMessage = parseColors(getConfig().getString("luring.withoutBaitMessage"));
    keyArray = getConfig().getConfigurationSection("luring.loot").getKeys(
    false).toArray(new String[0]);}
    public static String parseColors(String msg) {return msg.replaceAll("&", "ยง");}
    @EventHandler
    public void onPlayerFishEvent(final PlayerFishEvent event) {
    if (event.getState().equals(State.CAUGHT_FISH)) {
    if (rand.nextInt(100) <= chance) {
    reward(event.getPlayer(), getConfig().getIntegerList("luring.loot."+ keyArray[rand.nextInt(keyArray.length)]).toArray(new Integer[0]));
    }}}
    public static void reward(final Player player, final Integer[] loot) {
    final PlayerInventory inventory = player.getInventory();
    final ItemStack bait = new ItemStack(Material.getMaterial(loot[2]),loot[3]);
    final ItemStack lootStack = new ItemStack(Material.getMaterial(loot[0]),loot[1]);
    for (ItemStack stack : inventory.getContents()) {
    try {if (stack.getType().equals(bait.getType())&&stack.getAmount() >= bait.getAmount()) {
    inventory.addItem(lootStack);
    player.sendMessage(catchMessage);
    inventory.removeItem(bait);
    player.updateInventory();
    return;}
    } catch (Exception e) {}}
    player.sendMessage(withoutBaitMessage);}}
    
    Obviously that does not include the configuration file :D. You'll need a config file like this to run it:

    Code:
    luring:
      loot:
        rawFish:
        - 349 #extra loot that is possible to get from fishing, in this case its Raw Fish (itemID 349)
        - 10 #how many of that extra loot you are going to get, in here its 10 Raw Fishes
        - 331 #what bait you need to have in order to get that extra loot, in this case its Redstone Dust (itemID 331)
        - 20 #how many of bait is required to get extra loot
        rawFishx20:
        - 349
        - 20
        - 331
        - 50
        compass:
        - 345
        - 1
        - 331
        - 20
      lootChance: 100
      catchMessage: '&e Your hook caught some loot' #It supports standard Minecraft colour coding
      withoutBaitMessage: '&e Oi\! You are fishing without a bait\! Consider it to get extra catch\!' #Make sure to escape (\) special characters!
    
    The above code isn't efficient at all, as I had to compact 150 lines into 50 lines, but it should work.

    P.S. I couldn't get syntax=Java to work, so for your eyes I also put it on pastie. http://pastie.org/4372444
     
    zhuowei likes this.
  15. Offline

    MursheX

    ooo I want to join this! Just coded this today for funs! :D
    Code:
    package me.apie.potioneffect;
     
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
     
    public class Main extends JavaPlugin{
     
        public void onEnable(){
            System.out.println("[PotionEffect] v0.1 Enabled.");
        }
        public void onDisable(){
            System.out.println("[PotionEffect] v0.1 Disabled.");
        }
        @SuppressWarnings("deprecation")
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            Player player = (Player)sender;{
            }
            if(cmd.getName().equalsIgnoreCase("jump")){
                player.addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 20000, 1));
            }
            if(cmd.getName().equalsIgnoreCase("speed")){
                player.addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 20000, 1));
            }
            if(cmd.getName().equalsIgnoreCase("slow")){
                player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 20000, 1));
            }
            if(cmd.getName().equalsIgnoreCase("regeneration")){
                player.addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 20000, 1));
            }
            if(cmd.getName().equalsIgnoreCase("blindness")){
                player.addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 20000, 1));
            }
            if(cmd.getName().equalsIgnoreCase("fireresistance")){
                player.addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 20000, 1));
            }
            if(cmd.getName().equalsIgnoreCase("poison")){
                player.addPotionEffect(new PotionEffect(PotionEffectType.POISON, 20000, 1));
            }
            if(cmd.getName().equalsIgnoreCase("fastdigging")){
                player.addPotionEffect(new PotionEffect(PotionEffectType.FAST_DIGGING, 20000, 1));
            }
            if(cmd.getName().equalsIgnoreCase("confusion")){
                player.addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 20000, 1));
            }
            if(cmd.getName().equalsIgnoreCase("increasedamage")){
                player.addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 20000, 1));
            }
            if(cmd.getName().equalsIgnoreCase("damageresistance")){
                player.addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 20000, 1));
            }
            if(cmd.getName().equalsIgnoreCase("hunger")){
                player.addPotionEffect(new PotionEffect(PotionEffectType.HUNGER, 20000, 1));
            }
            if(cmd.getName().equalsIgnoreCase("heal")){
                player.addPotionEffect(new PotionEffect(PotionEffectType.HEAL, 20000, 1));
            }
            if(cmd.getName().equalsIgnoreCase("harm")){
                player.addPotionEffect(new PotionEffect(PotionEffectType.HARM, 20000, 1));
            }
            if(cmd.getName().equalsIgnoreCase("invisibility")){
                player.addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 20000, 1));
            }
            if(cmd.getName().equalsIgnoreCase("nightvision")){
                player.addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, 20000, 1));
            }
            if(cmd.getName().equalsIgnoreCase("slowdigging")){
                player.addPotionEffect(new PotionEffect(PotionEffectType.SLOW_DIGGING, 20000, 1));
            }
            if(cmd.getName().equalsIgnoreCase("waterbreathing")){
                player.addPotionEffect(new PotionEffect(PotionEffectType.WATER_BREATHING, 20000, 1));
            }
            if(cmd.getName().equalsIgnoreCase("weakness")){
                player.addPotionEffect(new PotionEffect(PotionEffectType.WEAKNESS, 20000, 1));
            }
            return true;
            }
        }
    I hope dats under 50 :c
    EDIT: Nvm, thats 82. oh wells I keep it here anywho
     
  16. Offline

    Codex Arcanum

    MursheX
    You should either use a switch statement, or at least make them else if's.
     
  17. Offline

    MursheX

    o okey! I am a nub coder, started 1 day ago give me credit :eek:
     
  18. Offline

    Fishrock123

    Mind if I help ya a bit? :p

    Code:java
    1. package me.apie.potioneffect;
    2. import org.bukkit.command.*; //Just wildcard it. Also, no need for those spaces.
    3. import org.bukkit.entity.Player;
    4. import org.bukkit.plugin.java.JavaPlugin;
    5. import org.bukkit.potion.*;
    6. public class Main extends JavaPlugin {
    7. //You don't need onEnable or onDisable. It prints plugin load messages anyway.
    8. @SuppressWarnings("deprecation")
    9. @Override
    10. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    11. //Don't make a player variable we don't need. Variables take up memory :p
    12. //Return the result of the potion effect. this allows us to use non block statements for each case.
    13. if (cmd.getName().equalsIgnoreCase("jump")) return ((Player)sender).addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 20000, 1));
    14. //Use else so that we don't make the server do unnecessary work.
    15. else if (cmd.getName().equalsIgnoreCase("speed")) return ((Player)sender).addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 20000, 1));
    16. else if (cmd.getName().equalsIgnoreCase("slow")) return ((Player)sender).addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 20000, 1));
    17. else if (cmd.getName().equalsIgnoreCase("regeneration")) return ((Player)sender).addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 20000, 1));
    18. else if (cmd.getName().equalsIgnoreCase("blindness")) return ((Player)sender).addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 20000, 1));
    19. else if (cmd.getName().equalsIgnoreCase("fireresistance")) return ((Player)sender).addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 20000, 1));
    20. else if (cmd.getName().equalsIgnoreCase("poison")) return ((Player)sender).addPotionEffect(new PotionEffect(PotionEffectType.POISON, 20000, 1));
    21. else if (cmd.getName().equalsIgnoreCase("fastdigging")) return ((Player)sender).addPotionEffect(new PotionEffect(PotionEffectType.FAST_DIGGING, 20000, 1));
    22. else if (cmd.getName().equalsIgnoreCase("confusion")) return ((Player)sender).addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 20000, 1));
    23. else if (cmd.getName().equalsIgnoreCase("increasedamage")) return ((Player)sender).addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 20000, 1));
    24. else if (cmd.getName().equalsIgnoreCase("damageresistance")) return ((Player)sender).addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 20000, 1));
    25. else if (cmd.getName().equalsIgnoreCase("hunger")) return ((Player)sender).addPotionEffect(new PotionEffect(PotionEffectType.HUNGER, 20000, 1));
    26. else if (cmd.getName().equalsIgnoreCase("heal")) return ((Player)sender).addPotionEffect(new PotionEffect(PotionEffectType.HEAL, 20000, 1));
    27. else if (cmd.getName().equalsIgnoreCase("harm")) return ((Player)sender).addPotionEffect(new PotionEffect(PotionEffectType.HARM, 20000, 1));
    28. else if (cmd.getName().equalsIgnoreCase("invisibility")) return ((Player)sender).addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 20000, 1));
    29. else if (cmd.getName().equalsIgnoreCase("nightvision")) return ((Player)sender).addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, 20000, 1));
    30. else if (cmd.getName().equalsIgnoreCase("slowdigging")) return ((Player)sender).addPotionEffect(new PotionEffect(PotionEffectType.SLOW_DIGGING, 20000, 1));
    31. else if (cmd.getName().equalsIgnoreCase("waterbreathing")) return ((Player)sender).addPotionEffect(new PotionEffect(PotionEffectType.WATER_BREATHING, 20000, 1));
    32. else if (cmd.getName().equalsIgnoreCase("weakness")) return ((Player)sender).addPotionEffect(new PotionEffect(PotionEffectType.WEAKNESS, 20000, 1));
    33. //Do not return true here! If the command does not match to what we want, say "false" so that the server will check other plugins.
    34. returnfalse;
    35. }
    36. }


    31 lines without the comments. :p (Note: I have not tested it.)
     
  19. Offline

    chaseoes

    It's more efficient to make the player variable and use it throughout the rest of your code since you use it so many times. Also, learn to indent!
     
  20. Offline

    JOPHESTUS

    CTRL + SHIFT + F
    will format all your code nicely
    (Eclipse)
     
  21. Offline

    Fishrock123

    You mean an instance variable of player? Are you sure? You'd still need to cast it each time.

    Yes, I know how to indent. I didn't pay attention to the indentation when I pasted. Stupid text formatting.
     
  22. Offline

    ACStache

    the casting here is fine, if he used the player object more than 1 time through each command, it might be worth it, but since each use of (Player)sender is in an "else if" (except the first one :D) statement, it will only ever use it once per command
     
    Fishrock123 likes this.
  23. Really? He uses it exactly one time each call as he returns right after using it.

    But Fishrock123 as you return in the ifs the elses are not needed (and just waste CPU time: If it doesn't return it will always enter the else, so there's no way that the else can't be entered, so it's a useless check).

    Fishrock123 MursheX You should also check if the sender is a player before casting (else your code will crash if you execute the command from the console). Last but not least use label instead of cmd.getName() as the second one returns the first, so why get what we still have?

    Code:java
    1. package me.apie.potioneffect;
    2. import org.bukkit.command.*;
    3. import org.bukkit.entity.Player;
    4. import org.bukkit.plugin.java.JavaPlugin;
    5. import org.bukkit.potion.*;
    6. public class Main extends JavaPlugin {
    7. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    8. if(!(sender instanceof Player)) return true;
    9. if (label.equalsIgnoreCase("jump")) return ((Player)sender).addPotionEffect(new PotionEffect(PotionEffectType.JUMP, 20000, 1));
    10. if (label.equalsIgnoreCase("speed")) return ((Player)sender).addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 20000, 1));
    11. if (label.equalsIgnoreCase("slow")) return ((Player)sender).addPotionEffect(new PotionEffect(PotionEffectType.SLOW, 20000, 1));
    12. if (label.equalsIgnoreCase("regeneration")) return ((Player)sender).addPotionEffect(new PotionEffect(PotionEffectType.REGENERATION, 20000, 1));
    13. if (label.equalsIgnoreCase("blindness")) return ((Player)sender).addPotionEffect(new PotionEffect(PotionEffectType.BLINDNESS, 20000, 1));
    14. if (label.equalsIgnoreCase("fireresistance")) return ((Player)sender).addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 20000, 1));
    15. if (label.equalsIgnoreCase("poison")) return ((Player)sender).addPotionEffect(new PotionEffect(PotionEffectType.POISON, 20000, 1));
    16. if (label.equalsIgnoreCase("fastdigging")) return ((Player)sender).addPotionEffect(new PotionEffect(PotionEffectType.FAST_DIGGING, 20000, 1));
    17. else if (label.equalsIgnoreCase("confusion")) return ((Player)sender).addPotionEffect(new PotionEffect(PotionEffectType.CONFUSION, 20000, 1));
    18. if (label.equalsIgnoreCase("increasedamage")) return ((Player)sender).addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 20000, 1));
    19. else if (label.equalsIgnoreCase("damageresistance")) return ((Player)sender).addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 20000, 1));
    20. if (label.equalsIgnoreCase("hunger")) return ((Player)sender).addPotionEffect(new PotionEffect(PotionEffectType.HUNGER, 20000, 1));
    21. if (label.equalsIgnoreCase("heal")) return ((Player)sender).addPotionEffect(new PotionEffect(PotionEffectType.HEAL, 20000, 1));
    22. if (label.equalsIgnoreCase("harm")) return ((Player)sender).addPotionEffect(new PotionEffect(PotionEffectType.HARM, 20000, 1));
    23. if (label.equalsIgnoreCase("invisibility")) return ((Player)sender).addPotionEffect(new PotionEffect(PotionEffectType.INVISIBILITY, 20000, 1));
    24. if (label.equalsIgnoreCase("nightvision")) return ((Player)sender).addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, 20000, 1));
    25. if (label.equalsIgnoreCase("slowdigging")) return ((Player)sender).addPotionEffect(new PotionEffect(PotionEffectType.SLOW_DIGGING, 20000, 1));
    26. if (label.equalsIgnoreCase("waterbreathing")) return ((Player)sender).addPotionEffect(new PotionEffect(PotionEffectType.WATER_BREATHING, 20000, 1));
    27. if (label.equalsIgnoreCase("weakness")) return ((Player)sender).addPotionEffect(new PotionEffect(PotionEffectType.WEAKNESS, 20000, 1));
    28. return false;
    29. }
    30. }
     
    Fishrock123 likes this.
  24. Offline

    jbman223

  25. Offline

    nala3

    49 lines, adds the players name to a list when they die and won't let them join while their name is on the list.
    basically being banned on death, but in a separate file :p
    PHP:
    import java.io.*;
    public class 
    Testing extends org.bukkit.plugin.java.JavaPlugin implements org.bukkit.event.Listener {
        private 
    File deathsFile null;
        @
    Override public void onDisable() {}
        @
    Override public void onEnable() {
            
    this.getServer().getPluginManager().registerEvents(thisthis);
            
    this.loadDeathsFile();
        }
        private 
    void loadDeathsFile() {
            
    this.deathsFile = new File(this.getDataFolder(), "deaths.txt");
            if(!
    this.getDataFolder().exists())
                
    this.getDataFolder().mkdirs();
            if (!
    deathsFile.exists())
                try {
                    
    deathsFile.createNewFile();
                } catch (
    IOException e) {
                    
    e.printStackTrace();
                }
        }
        private 
    boolean isDead(String name) {
            try {
                
    FileInputStream fstream = new FileInputStream(deathsFile);
                
    BufferedReader reader = new BufferedReader(new InputStreamReader(fstream));
                
    String line;
                while ((
    line reader.readLine()) != null) {
                    if (
    line.equalsIgnoreCase(name))
                        return 
    true;
                }
            } catch (
    IOException e) { e.printStackTrace(); }
            return 
    false;
        }
        private 
    void setDead(String name) {
            try {
                
    PrintWriter out = new PrintWriter(this.deathsFile"UTF-8");
                
    out.println(name);
                
    out.close();
            } catch (
    IOException e) { e.printStackTrace(); }
        }
        @
    org.bukkit.event.EventHandler public void onPlayerDeath(org.bukkit.event.entity.PlayerDeathEvent event) {
            
    String name event.getEntity().getName();
            
    this.setDead(name);
            
    event.getEntity().kickPlayer("You have died! Come back tomorrow or buy a life.");
        }
        @
    org.bukkit.event.EventHandler public void onPlayerJoin(org.bukkit.event.player.PlayerJoinEvent event) {
            
    String name event.getPlayer().getName();
            if (
    this.isDead(name))
                
    event.getPlayer().kickPlayer("You are dead! Wait until tomorrow, or buy a life.");
        }
    }
     
  26. Offline

    Fishrock123

    You are not required to implement @Override public void onDisable() {} //or onEnable

    Just FYI. That'll save you another line. :)
     
    nala3 likes this.
  27. Offline

    zhuowei

    BookToTxt: write out the contents of a Written Book to a .txt file: 45 lines
    Code:java
    1. package net.zhuoweizhang.booktotxt;
    2.  
    3. import java.io.*;
    4.  
    5. import net.minecraft.server.*;
    6.  
    7. import org.bukkit.command.*;
    8. import org.bukkit.craftbukkit.inventory.CraftItemStack;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.plugin.java.JavaPlugin;
    11.  
    12. public class BookToTxtPlugin extends JavaPlugin {
    13. public void onDisable() {
    14. }
    15.  
    16. public void onEnable() {
    17. getDataFolder().mkdirs();
    18. }
    19.  
    20. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    21. if (label.equalsIgnoreCase("booktotxt")) {
    22. Player player = (Player) sender;
    23. CraftItemStack stack = (CraftItemStack) player.getItemInHand();
    24. net.minecraft.server.ItemStack nmsStack = stack.getHandle();
    25. NBTTagCompound tag = nmsStack.getTag();
    26. File file = new File(getDataFolder(), args[0].replace("../", "").replace("..\\", "") + ".txt");
    27. PrintStream stream;
    28. try {
    29. stream = new PrintStream(file);
    30. } catch (Exception e) { return false; }
    31. stream.println(tag.getString("title"));
    32. stream.println("by " + tag.getString("author") + "\n");
    33. NBTTagList pagesTag = tag.getList("pages");
    34. for (int i = 0; i < pagesTag.size(); i++) {
    35. stream.println(((NBTTagString) pagesTag.get(i)).toString());
    36. stream.println();
    37. }
    38. stream.close();
    39. player.sendMessage("[BookToTxt] written book to " + file.getName());
    40. return true;
    41. }
    42. return false;
    43. }
    44.  
    45. }

    plugin.yml (open)

    Code:
    author: zhuowei
    main: net.zhuoweizhang.booktotxt.BookToTxtPlugin
    name: BookToTxt
    version: '1.0'
    commands:
        booktotxt:
            usage: /<command> <filename> (hold a book in your hand)
            permission: booktotxt.use
    permissions:
        booktotxt.use:
            description: Allows the user to use the /booktotxt command
            default: op

    GitHub and BukkitDev:

    https://github.com/zhuowei/BookToTxt

    http://dev.bukkit.org/server-mods/booktotxt/
     
  28. Offline

    zecheesy

    yay all these source codes make me learn more
     
    RainoBoy97 likes this.
  29. Offline

    jacklin213

    m2 anyone mind telling me how to make this code say to the console "u are not a player" and is this how you check for the permission
    Code:
    package me.jacklin213.sushi;
     
    import java.util.logging.Logger;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.PlayerInventory;
    import org.bukkit.permissions.Permission;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Sushi extends JavaPlugin implements Listener {
        // define stuff
        PluginDescriptionFile pdfFile;
        public static Sushi plugin;
        public final Logger log = Logger.getLogger("Minecraft");
        public static Permission Permissions = null;
        // what to do onDisable()
        public void onDisable() {
            this.pdfFile = getDescription();
            this.log.info(this.pdfFile.getName() + " is now disabled.");
        }
        public boolean onCommand(CommandSender sender, Command cmd, String label,
                String[] args) {
            if (cmd.getName().equalsIgnoreCase("sushi")
                    && sender.hasPermission("sushi.use") && args.length == 1) {
                ItemStack itemstack = new ItemStack(Material.RAW_FISH, 64);
                Player player = (Player) sender;
                PlayerInventory inventory = player.getInventory();
                inventory.addItem(itemstack);
                sender.sendMessage(ChatColor.YELLOW + "You have been givin Fish!");
                if (cmd.getName().equalsIgnoreCase("sushi") && args.length == 0) {
                    this.log.warning(ChatColor.RED + "Sorry you are not a player");
                }
            } else {
                sender.sendMessage(ChatColor.RED
                        + "You DO NOT have the permission to do this !");
                sender.sendMessage(ChatColor.DARK_GREEN
                        + "+------------------------------+");
                sender.sendMessage(ChatColor.DARK_AQUA + "Sushi: Spawn me Sushi!.");
                sender.sendMessage(ChatColor.GREEN + "By jacklin213");
                sender.sendMessage(ChatColor.YELLOW + "Version:"
                        + pdfFile.getVersion());
                sender.sendMessage(ChatColor.DARK_GREEN
                        + "+------------------------------+");
            }
            return super.onCommand(sender, cmd, label, args);
        }
        public void onEnable() {
            this.pdfFile = getDescription();
            this.log.info(this.pdfFile.getName() + " By "
                    + this.pdfFile.getAuthors() + " is now enabled!.");
        }
    }
     
    
    55 lines without comments
     
  30. Offline

    evilmidget38

    Not really sure why you're posting something here in a coding challenge if you're unsure of what it does, or how to do things. Seems like a better idea to post questions in Plugin development. You also have 55 lines of code, this is the 50 line challenge. By simply compacting your code a bit, I got it down to 49 lines. It can probably even be compacted further, but I'm not sure it currently works. Your sushi command seems off, you've got an if statement where it shouldn't, and checking if they didn't enter an argument doesn't really show whether or not they're a player.

    Code:
    package me.jacklin213.sushi;
     
    import java.util.logging.Logger;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.permissions.Permission;
    import org.bukkit.plugin.PluginDescriptionFile;
    import org.bukkit.plugin.java.JavaPlugin;
     
    public class Sushi extends JavaPlugin implements Listener {
        // define stuff
        PluginDescriptionFile pdfFile;
        public static Sushi plugin;
        public final Logger log = Logger.getLogger("Minecraft");
        public static Permission Permissions = null;
        // what to do onDisable()
        public void onDisable() {
            this.pdfFile = getDescription();
            this.log.info(this.pdfFile.getName() + " is now disabled.");
        }
        public boolean onCommand(CommandSender sender, Command cmd, String label,
                String[] args) {
            if (cmd.getName().equalsIgnoreCase("sushi") && sender.hasPermission("sushi.use") && args.length == 1) {
                Player player = (Player) sender;
                player.getInventory().addItem(new ItemStack(Material.RAW_FISH, 64));
                sender.sendMessage(ChatColor.YELLOW + "You have been givin Fish!");
                if (cmd.getName().equalsIgnoreCase("sushi") && args.length == 0) {
                    this.log.warning(ChatColor.RED + "Sorry you are not a player");
                }
            } else {
                sender.sendMessage(ChatColor.RED + "You DO NOT have the permission to do this !");
                sender.sendMessage(ChatColor.DARK_GREEN  + "+------------------------------+");
                sender.sendMessage(ChatColor.DARK_AQUA + "Sushi: Spawn me Sushi!.");
                sender.sendMessage(ChatColor.GREEN + "By jacklin213");
                sender.sendMessage(ChatColor.YELLOW + "Version:" + pdfFile.getVersion());
                sender.sendMessage(ChatColor.DARK_GREEN + "+------------------------------+");
            }
            return true
        }
        public void onEnable() {
            this.pdfFile = getDescription();
            this.log.info(this.pdfFile.getName() + " By "+ this.pdfFile.getAuthors() + " is now enabled!.");
        }
    }
     
Thread Status:
Not open for further replies.

Share This Page