How would I detect when a player is outside (or standing in Light Level 15)

Discussion in 'Plugin Development' started by JamEngulfer221, Aug 26, 2011.

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

    JamEngulfer221

    The title is pretty self explanatory.

    The plugin I am trying to code will detect if someone is outside, then ignite them. Kind of like with the zombies.

    I also need to know how to only effect one specific player or permissions group, not everyone.

    I would like the code and stuff for this, and a little guidance. I am using Eclipse if that makes any difference.

    Just so you can have a look at it and fix some stuff, here is my full code:

    Note: I may have some unnecessary code in there, like in the imports and the presence of the playerListener. That is from a removed bit of code.

    Code:java
    1. package me.jamengulfer221.burnoutside;
    2.  
    3. package me.jamengulfer221.burnoutside;
    4.  
    5. import java.io.File;
    6. import java.io.FileInputStream;
    7. import java.io.FileOutputStream;
    8. import java.io.IOException;
    9. import java.util.Properties;
    10. import java.util.logging.Logger;
    11.  
    12. import org.bukkit.Location;
    13. import org.bukkit.World;
    14. import org.bukkit.block.Block;
    15. import org.bukkit.command.Command;
    16. import org.bukkit.command.CommandSender;
    17. import org.bukkit.entity.Player;
    18. import org.bukkit.event.Event;
    19. import org.bukkit.event.player.PlayerMoveEvent;
    20. import org.bukkit.plugin.PluginManager;
    21. import org.bukkit.plugin.java.JavaPlugin;
    22.  
    23. public class burnoutside extends JavaPlugin{
    24. Logger log = Logger.getLogger("Minecraft");
    25. static String mainDirectory = "plugins/BurnOutside";
    26. static File config = new File(mainDirectory + File.separator + "config.yml");
    27. static Properties prop = new Properties();
    28. public void onEnable() {
    29. log.info("[BurnOutside] v0.1 Has been successfully enabled!");
    30.  
    31.  
    32.  
    33. PluginManager pm = this.getServer().getPluginManager();
    34. final BurnOutsidePlayerListener playerListener = new BurnOutsidePlayerListener();
    35. pm.registerEvent(Event.Type.PLAYER_MOVE, playerListener, Event.Priority.Normal, this);
    36. }
    37. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    38. if(cmd.getName().equalsIgnoreCase("protect")){
    39. int protect = 1;
    40. return true;
    41. } //If this has happened the function will break and return true. if this hasn't happened the a value of false will be returned.
    42. return false;
    43. }
    44. public void onPlayerMove(PlayerMoveEvent event) {
    45. Player player = event.getPlayer();
    46. Location loc = player.getLocation();
    47. World world = loc.getWorld();
    48. Block block = player.getLocation().getBlock();
    49.  
    50. if (protect == 1){
    51. if (world.getHighestBlockYAt(loc) < loc.getY()) {
    52. && if(block.getLightLevel() == (byte) 15){
    53. player.setFireTicks(20 * 60 * 1);
    54. }
    55. }
    56. } else {
    57. return;
    58. }
    59. }
    60. public void onDisable(){
    61. log.info("[BurnOutside] v0.1 has been successfully disabled");
    62. }
    63.  
    64. }
    65.  
    66.  
     
  2. Offline

    thescreem

    I would say do something like get the block at the players feet, and then create an if statement which checks the light level of that block like this:

    if(block.getLightLevel == (byte) 15){
    //do something
    }
     
  3. Offline

    JamEngulfer221

    Just wondering, could you point me to where I could find out how to get the block at the player's feet :) Thanks.

    Also, if there a way to hook onto or use the code zombies use, because I think glowstone emits light at level 15, but if the glowstone and zombie are outside, the zombie isn't burnt

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 19, 2016
  4. Offline

    Sagacious_Zed Bukkit Docs

  5. Offline

    Baummann

    To check if the player is outside:

    Code:java
    1. Location loc = player.getLocation();
    2. World world = loc.getWorld();
    3. if (world.getHighestBlockY(loc).getLocation().getY() < loc.getY()) {
    4. //Do stuff
    5. }


    To get the block at the player's feet:

    Code:java
    1. Block block = player.getLocation().getBlock();


    Or do you mean the block under the player's feet?:

    Code:java
    1. Block block = player.getLocation().getBlock().getRelative(BlockFace.DOWN);
     
  6. Offline

    JamEngulfer221

    I will try that, thank you :) I think that almost all of my problems are solved, all I need to do is ignite the player when all of these conditions are true. (Any way of doing that?)

    PS: You just got an internet
     
  7. Offline

    Baummann

    Code:java
    1. player.setFireTicks(20);

    That'll set the player on fire for 1 second to make him burn for 1 minute just use 20 * 60 * 1. And for 2 minutes 20 * 60 * 2 etc. I think you know what I mean ;)

    PS: Thanks for the internet :)
     
  8. Offline

    JamEngulfer221

    You're welcome :) Is there a way I can target only one player with this (12squares) or a permissions group (necromancers), or will it only effect every player?

    Also, I am getting a problem with 'player' as in

    Code:java
    1. player.getLocation();


    It says "player cannot be resolved" and gives me options like make new variable player and things like that.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 19, 2016
  9. Offline

    Baummann

    Could you give me your whole code? And where you wanna place this?
     
  10. Offline

    JamEngulfer221

    Oh, ok, that should clarify things :)

    Code:java
    1. package me.jamengulfer221.burnoutside;
    2.  
    3. import java.io.File;
    4. import java.io.FileInputStream;
    5. import java.io.FileOutputStream;
    6. import java.io.IOException;
    7. import java.util.Properties;
    8. import java.util.logging.Logger;
    9.  
    10. import org.bukkit.Location;
    11. import org.bukkit.World;
    12. import org.bukkit.block.Block;
    13. import org.bukkit.event.Event;
    14. import org.bukkit.plugin.PluginManager;
    15. import org.bukkit.plugin.java.JavaPlugin;
    16.  
    17. public class burnoutside extends JavaPlugin{
    18. Logger log = Logger.getLogger("Minecraft");
    19. static String mainDirectory = "plugins/BurnOutside";
    20. static File config = new File(mainDirectory + File.separator + "config.yml");
    21. static Properties prop = new Properties();
    22. public void onEnable() {
    23. log.info("[BurnOutside] v0.1 Has been successfully enabled!");
    24. Location loc = player.getLocation();
    25. World world = loc.getWorld();
    26. Block block = player.getLocation().getBlock();
    27. if (world.getHighestBlockY(loc).getLocation().getY() < loc.getY()) {
    28. && if(block.getLightLevel == (byte) 15){
    29. //do something
    30. }
    31. }
    32.  
    33. PluginManager pm = this.getServer().getPluginManager();
    34. final BurnOutsidePlayerListener playerListener = new BurnOutsidePlayerListener();
    35. pm.registerEvent(Event.Type.PLAYER_MOVE, playerListener, Event.Priority.Normal, this);
    36. }
    37. public void onDisable(){
    38. log.info("[BurnOutside] v0.1 has been successfully disabled");
    39. }
    40.  
    41. }
    42.  


    Also from now on, I will put the code as I update it into the OP
     
  11. Offline

    Baummann

    Code:java
    1.  
    2. Location loc = player.getLocation();
    3. World world = loc.getWorld();
    4. Block block = player.getLocation().getBlock();
    5. if (world.getHighestBlockY(loc).getLocation().getY() < loc.getY()) {
    6. && if(block.getLightLevel == (byte) 15){
    7. //do something
    8. }

    That code needs to be in your PLAYER_MOVE event.

    And to make "player" work use:

    Code:java
    1. Player player = event.getPlayer();
     
  12. Offline

    JamEngulfer221

    Inside my PLAYER_MOVE event? You mean my player listener?

    Hmm, now 'event' is throwing up errors. this is getting weird.

    Also, 'getHighestBlockY' Is wanting to be changed into 'getHighestBlockAt' or 'getHighestBlockYAt' iI don't know what is causing this, so I am wondering wether I haven't imported something or called something.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 19, 2016
  13. Offline

    Baummann

    Use getHighestBlockYAt. Everything should look like this:
    Code:java
    1. public void onPlayerMove(PlayerMoveEvent event) {
    2. //Do stuff
    3. }


    But replace the //Do stuff with
    Code:java
    1. Player player = event.getPlayer();
    2. Location loc = player.getLocation();
    3. if (loc.getWorld().getHighestBlockYAt(loc) < loc.getY()) {
    4. player.setFireTicks(20 * 4);
    5. }


    That'll set the player on fire for 4 seconds if he's outside.
     
  14. Offline

    JamEngulfer221

    Ok, by the way, I want to have a second if statement that will only ignite when the light level is 15 (I.E. Daytime). Would I use
    Code:java
    1.  
    Code:java
    1.  
    2. if (world.getHighestBlockYAt(loc).getLocation().getY() < loc.getY()) {
    3. && if(block.getLightLevel == (byte) 15){
    4. player.setFireTicks(20 * 60 * 1);
    5. }
    6. [FONT=georgia] }[/FONT]

    Because Eclipse is getting annoyed over the && (which I think is AND). I am wondering if that is how I should put it.

    Eventually I would like the whole thing to be stopped with a command (they get burned outside unless they put in the command)

    Also, how would I do an individual player or a group?

    Also, when I change it to GetHighestBlockYAt, it says it "cannot invoke getLocation() on the primitive type int"

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 19, 2016
  15. Offline

    Baummann

    Then remove the getLocation()

    Ok to prevent players from burning with a command you should use a HashMap.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 19, 2016
  16. Offline

    JamEngulfer221

    Ok, I will try. But I am only targeting one player. ever. (12squares). Will that still need a HashMap?

    Or have I got HashMaps completely wrong?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 19, 2016
  17. Offline

    Baummann

    HashMaps can store data but not like int or booleans they can store more than 1 player.

    So you could check if the player is in the HashMap and just return
     
Thread Status:
Not open for further replies.

Share This Page