Development Assistance Loops

Discussion in 'Plugin Help/Development/Requests' started by 2008Choco, May 13, 2015.

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

    2008Choco

    I'm attempting to make a plugin where players take damage periodically if they're in a specified ArrayList. My first attempt was to run a while loop (set to true so it would run indefinitely) on a SyncRepeatingTask. I put that in a main method because I was pretty sure that a main method in the main class ran first regardless of whether it was called or not. I suppose I was wrong :p

    Could someone help me? In simpler terms, I just need to get a loop running as long as the plugin is active
     
  2. Offline

    mine-care

    @2008Choco umm you don't need an infinite loop in any case...
    All you need is a Bukkit runnable and love xD also please provide your code so we can see what you have tried so far to direct you properly.
     
  3. Offline

    2008Choco

    @mine-care
    Code:
        Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
            @Override
            public void run() {
                for (Player player : Bukkit.getOnlinePlayers()){
                    if (Bleeding.bleeding.contains(player)){
                        player.damage(getConfig().getDouble("BleedDamage"));
                    }//Close if player is bleeding
                }//Close filter through all players on the server
            }//Close runnable
        }, 0, getConfig().getInt("BleedDamageDelay"));
    That's what I have so far :/ It gives me an error on the method above telling me to "delete this token". If you need the full code, that's fine, but this is just the runnable I'm trying to accomplish. To note, this is in the main class
     
  4. Offline

    mine-care

    @2008Choco umm dont abuse static please...
    In what line you get the error?
     
  5. Offline

    2008Choco

    @mine-care Not sure where static was used in that method :p But okay

    Code:
    package me.choco.conditions;
    
    import java.io.File;
    import java.util.ArrayList;
    import java.util.Arrays;
    
    import me.choco.conditions.particles.ParticleEffect;
    import me.choco.conditions.particles.ParticleEffect.BlockData;
    import me.choco.conditions.particles.ParticleEffect.ParticleData;
    import me.choco.conditions.utils.BandageClickEvent;
    import me.choco.conditions.utils.BleedRandomizer;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.event.entity.PlayerDeathEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.ShapedRecipe;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    
    @SuppressWarnings("deprecation")
    public class Bleeding extends JavaPlugin implements Listener{
       
        FileConfiguration config;
        File cfile;
        public static ArrayList<String> bleeding = new ArrayList<String>();
        static int loop = 1;
       
        @Override
        public void onEnable(){
            getServer().getPluginManager().registerEvents(this, this);
            getServer().getPluginManager().registerEvents(new BandageClickEvent(), this);
            this.config = getConfig();
            saveDefaultConfig();
            this.cfile = new File(getDataFolder(), "config.yml");
    
            ItemStack bandage = new ItemStack(Material.PAPER);
            ItemMeta bandageMeta = bandage.getItemMeta();
            bandageMeta.setDisplayName(ChatColor.RED + "Bandage");
            bandageMeta.setLore(new ArrayList<String>(Arrays.asList(
                    ChatColor.GRAY + "Right click with this to",
                    ChatColor.GRAY + "stop yourself from bleeding")));
            bandage.setItemMeta(bandageMeta);
           
            ShapedRecipe bandageRecipe1 = new ShapedRecipe(bandage).shape("   ","   ","WRW").setIngredient('W', Material.WOOL).setIngredient('R', Material.INK_SACK, 1);
            ShapedRecipe bandageRecipe2 = new ShapedRecipe(bandage).shape("   ","WRW","   ").setIngredient('W', Material.WOOL).setIngredient('R', Material.INK_SACK, 1);
            ShapedRecipe bandageRecipe3 = new ShapedRecipe(bandage).shape("WRW","   ","   ").setIngredient('W', Material.WOOL).setIngredient('R', Material.INK_SACK, 1);
           
            getServer().addRecipe(bandageRecipe1);
            getServer().addRecipe(bandageRecipe2);
            getServer().addRecipe(bandageRecipe3);
        }//Close onEnable method
       
        @EventHandler
        public void onPlayerDamagePlayer(EntityDamageByEntityEvent event){
            Player damaged = (Player) event.getEntity();
            Player damager = (Player) event.getDamager();
            ParticleData ParticleData = new BlockData(Material.REDSTONE_BLOCK, (byte) 0);
            ParticleEffect.BLOCK_DUST.display(ParticleData, (float) 0.1, (float) 0.6, (float) 0.1, (float) 0.1, 20, damaged.getLocation().add(0, 1, 0), damager, damaged);
            BleedRandomizer.randomBleed(damaged);
        }//Close onPlayerDamagePlayer method
       
        @EventHandler
        public void onPlayerDeath(PlayerDeathEvent event){
            Player killed = (Player) event.getEntity();
            if (bleeding.contains(killed.getName())){
                if (event.getDeathMessage().contains(" died")){
                    event.setDeathMessage(killed.getName() + " has bled to death");
                }//Close if player died abruptedly
                bleeding.remove(killed.getName());
            }//Close if player died to bleeding
        }//Close onPlayerDeath event
       
        Bukkit.getScheduler().scheduleSyncRepeatingTask(this, new Runnable(){
            public void run() {
                for (Player player : Bukkit.getOnlinePlayers()){
                    if (Bleeding.bleeding.contains(player)){
                        player.damage(getConfig().getDouble("BleedDamage"));
                    }//Close if player is bleeding
                }//Close filter through all players on the server
            }//Close runnable
        }, 0, getConfig().getInt("BleedDamageDelay"));
       
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            if (commandLabel.equalsIgnoreCase("bleed") || commandLabel.equalsIgnoreCase("bleeding")){
                if (sender instanceof Player){
                    Player player = (Player) sender;
                    if (player.hasPermission("conditions.admin")){
                        if (args.length >= 1){
                            if (args[0].equalsIgnoreCase("reload")){
                                reloadConfig();
                                player.sendMessage(ChatColor.DARK_RED + "Bleeding> " + ChatColor.GRAY + "Configuration reloaded");
                                return true;
                            }//Close if args[0] == reload
                            if (args[0].equalsIgnoreCase("force")){
                                if (args.length == 2){
                                    Player targetPlayer = Bukkit.getPlayer(args[1]);
                                    if (Bukkit.getOnlinePlayers().contains(targetPlayer)){
                                        if (!bleeding.contains(targetPlayer.getName())){
                                            bleeding.add(targetPlayer.getName());
                                            player.sendMessage(ChatColor.DARK_RED + "Bleeding> " + ChatColor.GRAY + args[1] + " is now bleeding");
                                            targetPlayer.sendMessage(ChatColor.DARK_RED + "Condition> " + ChatColor.GRAY + player.getName() + " has forced you to bleed");
                                            return true;
                                        }//Close if player is already bleeding
                                        else{
                                            player.sendMessage(ChatColor.DARK_RED + "Bleeding> " + ChatColor.GRAY + args[1] + " is already bleeding! How cruel!");
                                            return true;
                                        }//Close if player is NOT already bleeding
                                    }//Close if player is online
                                    else{
                                        player.sendMessage(ChatColor.DARK_RED + "Bleeding> " + ChatColor.GRAY + args[1] + " is not currently online");
                                        return true;
                                    }//Close if player is offline
                                }//Close if args == 2
                            }//Close if args[0] == force
                            if (args[0].equalsIgnoreCase("stop")){
                                if (args.length == 2){
                                    Player targetPlayer = Bukkit.getPlayer(args[1]);
                                    if (Bukkit.getOnlinePlayers().contains(targetPlayer)){
                                        if (bleeding.contains(targetPlayer.getName())){
                                            bleeding.remove(targetPlayer.getName());
                                            player.sendMessage(ChatColor.DARK_RED + "Bleeding> " + ChatColor.GRAY + args[1] + " has stopped bleeding");
                                            targetPlayer.sendMessage(ChatColor.DARK_RED + "Condition> " + ChatColor.GRAY + player.getName() + " has cured your bleeding");
                                            return true;
                                        }//Close if player is already bleeding
                                        else{
                                            player.sendMessage(ChatColor.DARK_RED + "Bleeding> " + ChatColor.GRAY + args[1] + " isn't bleeding. Cannot stop");
                                            return true;
                                        }//Close if player is NOT already bleeding
                                    }//Close if player is online
                                    else{
                                        player.sendMessage(ChatColor.DARK_RED + "Bleeding> " + ChatColor.GRAY + args[1] + " is not currently online");
                                        return true;
                                    }//Close if player is offline
                                }//Close if args == 2
                            }//Close if args[0] == force
                            else{
                                player.sendMessage(ChatColor.DARK_RED + "Bleeding> " + ChatColor.GRAY + "Invalid arguments: " + ChatColor.AQUA + args[0]);
                                return true;
                            }//Close if args are invalid
                        }//Close if args.length == 1
                        if (args.length == 0){
                            player.sendMessage(ChatColor.WHITE + "----------" + ChatColor.DARK_RED + " Player Conditions " + ChatColor.WHITE + "----------");
                            player.sendMessage(ChatColor.RED + "/bleed reload " + ChatColor.WHITE + "- Reload the configuration");
                            player.sendMessage(ChatColor.RED + "/bleed force <player> " + ChatColor.WHITE + "Force another player to bleed");
                            player.sendMessage(ChatColor.RED + "/bleed stop <player> " + ChatColor.WHITE + "Stop another player from bleeding");
                            player.sendMessage(ChatColor.WHITE + "---------------------------------------");
                            return true;
                        }//Close if args.length == 0
                        else{
                            player.sendMessage(ChatColor.DARK_RED + "Bleeding> " + ChatColor.GRAY + "Invalid arguments");
                            return true;
                        }//Close if args.length == 0 or > 1
                    }//Close if permissions == true
                    else{
                        player.sendMessage(ChatColor.DARK_RED + "Bleeding> " + ChatColor.GRAY + "You do not have the sufficient permissions to run this command");
                        return true;
                    }//Close if permissions == false
                }//Close if sender == player
                else{
                    if (args.length == 1){
                        if (args[0].equalsIgnoreCase("reload")){
                            reloadConfig();
                            getLogger().info("Bleeding> Configuration reloaded");
                            return true;
                        }//Close if args[0] == reload
                        else{
                            getLogger().info("Bleeding> Invalid arguments: " + args[0]);
                            return true;
                        }//Close if args are invalid
                    }//Close if args.length == 1
                    else{
                        getLogger().info("Bleeding> Invalid arguments");
                        return true;
                    }//Close if args.length == 0 or > 1
                }//Close if sender != player
            }//Close reload command
            return false;
        }//Close onCommand method
    }//Close class
    Line 83 is the line where the code I'm having issues with is. I'm getting an error on line 80 telling me to delete this token "}", and an error on line 90 saying, insert "}" to complete the block statement. Hope that helps a bit more :/
     
  6. Offline

    mine-care

    This is static abuse.. :/ a class extending JavaPlugin cannot be instanciated over once.
    you cast player to player?
    Thats because your sheduler does not belong in any body of method, constructor or anything. it cant belong in the class.
    :p
    Please take a look at the basics of java and especially on synatx erros, if you need help leme know! :D
     
    Last edited: May 14, 2015
  7. Offline

    2008Choco

    @mine-care Unfortunately, if the methods listed in the first quote are not static, I cannot use them in another class. These need to be static because public will not let me use it on it's own. In my other class, it's literally telling me, "change 'bleeding' to static". So therefore, it's static. The loop method can be deleted. That was yet again another test method

    As for the second quote, I casted a player to a player beacuse event.getEntity() seemed vague and I copied and pasted from the event above. I must have forgotten to take away the cast. My bad

    The scheduler, I'm aware it needs to be in a method, and I understand that. I was messing around with different ways to use it and clearly I couldn't figure it out. When I attempted using it in a method, it seemed to give me a similar error. I put it in the main method so it would run indefinitely as long as the plugin was enabled. That's my entire goal is to try and get that to run. I don't want this to run when it's called by a command, and I don't want this to be run when its enabled by a player. I want it run throughout the entire life-span of the plugin.

    I'm not making a post to understand something I can improve on. I'm making this post to know how to fix an error I cannot seem to figure out. For teaching myself Java and the Bukkit API, I think I've done a very good job, thank you.

    Is there anyone else who would be able to help me run this loop indefinitely and not criticize the rest of my work in doing so?
     
  8. Offline

    mine-care

    Let me fist get a thing out of the way, i am not in any manner trying to critisize your work. it is pretty good. i just pointed out a few imperfections and ways you can improve it as i do in most if not every of my posts, please excuse me if i sounded mean at any point, it was really not my intention.
    I am not here to critisize you, and i have no right to. i only wanted to add some 'notes' to what can be improved :oops:

    To answer yout question try puting the whole repeating task in the onEnable() method of your plugin where it will start by the time the plugin is enabled untill server stops/restarts/reloads.
     
  9. Offline

    2008Choco

    @mine-care Putting the repeating task in the onEnable method will not work. I just tried it

    I'm a liiiittle bit early on my bump, but I won't be here at 12:30 and it's technically tomorrow :p

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  10. Offline

    2008Choco

    Bumpedy bump bump
     
Thread Status:
Not open for further replies.

Share This Page