How would I use a BukkitRunnable here?

Discussion in 'Plugin Development' started by Harrulolz, Oct 6, 2022.

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

    Harrulolz

    Hi guys, I'm learning bukkit for a server I'm making with school friends and I've been working on this spleef gamemode. I need a bukkitrunnable for checking every tick if something is which then starting events and etc.
    Code:
    public class spleef implements Listener {
        public spleef(Harrowv1 plugin){ Bukkit.getPluginManager().registerEvents(this, plugin);}
        public static ArrayList<Player> InSpleef = new ArrayList<>();
        public static ArrayList<Player> PlayingSpleef = new ArrayList<>();
    
        @EventHandler
        public void OnTeleport(PlayerTeleportEvent event){}
        @EventHandler
        public void OnLeave(PlayerQuitEvent event){}
    }
    
    This is the basics of my code without all the imports and code inside the void.
    Hope I'm not being confusing, as I've been trying to figure this out for 2 days.
     
  2. Offline

    timtower Administrator Administrator Moderator

    @Harrulolz What kind of thing do you want to check then?
     
  3. Offline

    Harrulolz

    Well, I've got this code here that is meant to run on every tick. It has a little intermission system, then sends players into spleef and waits for there to be one last player before ending the game.
    Code:
    public long lasttick = 0;
                public long currenttick = 0;
                public boolean gameplaying = false;
                public boolean finished = false;
                public boolean disabled = false;
    
                @Override
                public void run() {
                    if(!disabled){
                        int ArraySize = spleef.InSpleef.size();
                        World world = Bukkit.getWorld("spleef");
    
                        if(gameplaying){
                            if(!finished){
                                lasttick = 0;
                            }
    
                            int PlayingSize = spleef.PlayingSpleef.size();
    
                            if(PlayingSize < 2 && !finished){
                                finished = true;
                                lasttick = world.getFullTime();
    
                                Player winner = null;
                                for (int i = 0; i < spleef.PlayingSpleef.size(); i++) {
                                    winner = spleef.PlayingSpleef.get(i);
                                }
    
                                if(winner != null){
                                    winner.sendMessage();
                                }
                            }else if(PlayingSize < 2 && finished){
                                currenttick = world.getFullTime();
                                long currenttime =  currenttick - lasttick;
    
                                if(currenttime >= 5){
                                    spleef.PlayingSpleef.removeAll(spleef.PlayingSpleef);
    
                                    finished = false;
                                    gameplaying = false;
                                }
                            }
                        }else {
                            if(ArraySize > 1){
                                if(lasttick == 0){
                                    lasttick = world.getFullTime();
    
                                    Bukkit.broadcastMessage(ChatColor.YELLOW + "Server | " + ChatColor.WHITE + "Starting spleef in 15 seconds!");
                                }else {
                                    currenttick = world.getFullTime();
                                    long currenttime =  currenttick - lasttick;
    
                                    if(currenttime >= 15){
                                        spleef.PlayingSpleef.addAll(spleef.InSpleef);
    
                                        CommandSender sender = Bukkit.getConsoleSender();
                                        Bukkit.getServer().dispatchCommand(sender, "fill -8 0 -23 8 0 -39 snow");
                                        for(int i = 0; i < spleef.PlayingSpleef.size(); i++){
                                            Player plr = spleef.PlayingSpleef.get(i);
    
                                            double randomx = -25 - (Math.random() * 16);
                                            double randomz = 6 - (Math.random() * 12 + 1);
    
                                            Location location = new Location(world, randomx, 3.0, randomz);
                                            plr.teleport(location);
                                        }
    
                                        Bukkit.broadcastMessage(ChatColor.YELLOW + "Server | " + ChatColor.WHITE + "Spleef has started!");
                                        gameplaying = true;
                                    }
                                }
                            }else {
                                gameplaying = false;
                                finished = false;
                                lasttick = 0;
                            }
                        }
                    }
                }
     
  4. Offline

    timtower Administrator Administrator Moderator

    @Harrulolz Then you just start one in the onEnable and have it run forever.
     
    Harrulolz likes this.
  5. Offline

    Harrulolz

    Thats actually really simple! Thank you.

    Hi sorry, I'm back again just to ask on how would I get the java plugin? Everytime I try to it throws this error:
    'getPlugin(java.lang.Class<org.bukkit.plugin.java.JavaPlugin>)' in 'org.bukkit.plugin.java.JavaPlugin' cannot be applied to '()'
    But then I try this to fix it:
    Code:
     JavaPlugin plugin = JavaPlugin.getPlugin();
    And it asks for a class, but I have no idea how to get the class?

    Nevermind! managed to fix it.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Oct 9, 2022
  6. Offline

    Strahan

    Not to be pedantic, but you should follow Java naming conventions. i.e. the class should be Spleef (classes are PascalCase) and the vars should be like inSpleef (vars are camelCase). Also I'd not use static if I were you. It's fine so long as you only intend to allow one Spleef game at a time, but IMO it's best to code for flexibility.

    It's also a good idea to adhere to the single responsibility principle; I would be mixing all the listeners and data together in one class. I'd make Spleef just be a data object holding things related to the game directly, then have a collection of said objects to use as needed. It would clean things up, plus allow you the flexibility to have multiple instances of the game if you wish.
     
Thread Status:
Not open for further replies.

Share This Page