Solved How to change the block under a player to air? and how to set an area where the plugin can work?

Discussion in 'Plugin Development' started by IdiotBoxGuyMC, Mar 26, 2014.

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


    I want to create a plugin in which players spawn in an arena and when the game starts, the blocks below them disappears (changes to air block), in order to live, they have to run.

    Now, I can't seem to find a way to make the plugin work for all the players in only a specific area... So I've used Bukkit.getOnlinePlayers
    But my main question is, how do I change the block below them?

    I've messed around bukkit before, but this time I really wanna learn stuff. I tried codes from various sources and some of my as well, but still can't seem to get it working...
    Here's my code so far...
    1. package me.idiotboxguy;
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.Material;
    5. import org.bukkit.block.BlockFace;
    6. import org.bukkit.entity.Player;
    7. import;
    10. public class Runn4Life extends JavaPlugin{
    12. public void onEnable(){
    13. Player[] i = Bukkit.getOnlinePlayers();
    14. for(int players = 0 ; players < i.length ; players++){
    15. if(i[players].getLocation().getBlock().getRelative(BlockFace.DOWN).getType() == Material.BEDROCK) {
    17. }
    18. }
    19. }
    22. public void onDisable(){
    24. }
    25. }

    Please help, I know I'm a newbie, but have mercy :)
  2. Offline


    When you get the specific block that you want to change to air just use the setType() method. Make sure you use Material.AIR and NOT ID's because that setType() method has been deprecated.
  3. IdiotBoxGuyMC
    You would better add the players in a HashMap after they join the game ... because with this code if you have a player standing on bedrock and not playing he will fall.
  4. Offline


    I tried that, but all it does it removes only 1 block under me. Should I be using a loop?

    I've not yet learned about HashMap, but I'll surely check it out right now!
  5. This is what i use:

    if(i[players].getLocation().substract(0, 1, 0).getBlock().getType() == Material.BEDROCK) {
              players.getLocation().substract(0, 1, 0).getBlock().setType(Type.AIR);
  6. Offline


    Let me explain.

    A Hashmap is a kind of list. In an ArrayList the values of the list are ordened by number. The first one 0, the second 1, the third 2 and so one. So, the value of an ArrayList can be anything (String, Integer, Byte...) but the Key (witch ordens the values) is always an integer starting at 0 and counting on. The fun thing about HashMaps is that the Key can also be anything. You can asign a Value to a Key. For example: I want a list of Players and the team they're in. So what I do, is create a HashMap with the structure: "KEY= STRING & VALUE= STRING" (!-> Note: Never ever put a Player in a hashmap! Always use Player.getName() and Bukkit.getPlayer(String) !!!) E.g.: Player "Steve" joins "TEAM A", so I put the string "TEAM A" to the key "Steve".

    HashMaps are easy to create, here's an example of a code witch creates a HashMap with the structure: "KEY= STRING & VALUE= STRING" and puts the players in it when they join
    public HashMap<String, String> playersList = new HashMap<String, String>();
    public void onPlayerJoin(PlayerJoinEvent event) {
      Player player = event.getPlayer();
      String playerName = player.getName();
      String teamName = "TEAM A";
      playersList.put(playerName, teamName);
    You can use the same system to create a HashMap that holds the Players names and if they are 'Active'. In that case you'd use "K= String & V= Boolean"

    public HashMap<String, Boolean> playersList = new HashMap<String, Boolean>();
    public void onPlayerJoin(PlayerJoinEvent event) {
      Player player = event.getPlayer();
      String playerName = player.getName();
      Boolean isActive = true;
      playerList.put(playerName, isActive);
    Now, here's the code for an event that changes the blocks below all active players into Air.

    public void changeBlocks() {
      for (Player player : Bukkit.getOnlinePlayers()) {
          String playerName = player.getName();
          if (playersList.containsKey(playerName)) {
            boolean isActive = playersList.get(playerName);
            if (isActive) {
                Location loc = player.getLocation();
                loc.add(0, -1, 0);
                Block block = loc.getBlock();
    Hope it helps!
  7. Offline



    THANK YOU! Dude you have helped me a lot to get started! I will surely use the knowledge you have given me in the future projects. Thanks for providing codes too, I be lazy and copy paste, but I'll surely refer them. Thank you again. I'm not marking this thread as Solved, in case I face any future problems. Thank you again dude! :D
  8. You kinda messed up there you know change == != otherwise only bedrock will be removed derp
  9. Offline


    You are very welcome!

    One thing: I suggest you do mark the post as 'SOLVED' so people who do (google)searches are helped to! If you do have any more problems, just create a new topic or send me a PM!
  10. goeiecool9999

    Yeah, i didn't really pay attention to that...
  11. Offline


Thread Status:
Not open for further replies.

Share This Page