Creating an Integer for Sleeping and Awake Players

Discussion in 'Plugin Development' started by ducksRus9, Jul 22, 2015.

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

    ducksRus9

    ** EDIT **
    Title change because I think I found a code that could possibly work. In the listener class there is an error for finding the total amount of online players which probably isn't even needed in the code. Also, the code below is the latest version of my code. The first time, I used a config file to log the players sleeping which now doesn't make sense to me at all and I changed it to what is below.
    ** END EDIT **



    I want to create a plugin that logs which players are sleeping and which players are not. If the amount of players sleeping is greater than the amount of players not sleeping then sleeping is ignored. However, I have hit a road block. I followed a tutorial (here) in hopes that making a counter will help me. I just don't know how to set the players not sleeping to sleeping ignored.

    Listener Class: Line 19 error!! how to fix?
    Code:
    package me.ducksRus9;
    
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerBedEnterEvent;
    import org.bukkit.event.player.PlayerEvent;
    
    public class PlayerListener implements Listener {
    
        public PlayerListener(BetterSleeping plugin) {
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
            int sleeping = 0;
            int awake = 0;
            int total = sleeping + awake;
            total = plugin.getServer().getOnlinePlayers();
            Player player = (Player) player;
          
            if (sleeping >= awake) {
                if (!player.isSleeping()) {
                    player.setSleepingIgnored(true);
                }
            }
        }
      
        @EventHandler
        public void onEnter(PlayerBedEnterEvent e) {
          
            Player player = e.getPlayer();
          
            player.chat(ChatColor.AQUA + player.getCustomName() + " is now sleeping!");
        }
      
    }
    
    

    Main Class:
    Code:
    package me.ducksRus9;
    
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class BetterSleeping extends JavaPlugin {
    
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        
            if (cmd.getName().equalsIgnoreCase("bed") && sender instanceof Player) {
            
                Player player = (Player) sender;
                Location bedSpawn = player.getBedSpawnLocation();
            
                player.teleport(bedSpawn);
            
                player.sendMessage(ChatColor.AQUA + "You have been teleported to your bed!");
                    
        
            }
            return false;
        }
    }
    
     
    Last edited: Jul 22, 2015
  2. Offline

    mythbusterma

  3. Offline

    ducksRus9

    Storing the values in config was the only way I found in order to get a bit closer to what I wanted to do.
    The problem is I can't seem to figure out how to set all players that aren't sleeping to sleeping ignored

    You can do "player.setSleepingIgnored(true);" but how do I implement this into my code? Or how can I do this without storing things in config

    Edit: I edited the code in my original post
     
    Last edited: Jul 22, 2015
Thread Status:
Not open for further replies.

Share This Page