[TUTORIAL] Land Mines

Discussion in 'Resources' started by sgavster, Dec 11, 2013.

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

    sgavster

    Hi! :D

    I was recently working on a minigame, and I needed land mines. I spent a couple hours tweaking this and things and this is the result!

    So, the first thing is making an ArrayList. With this arraylist we will store where our land mines are. Since this ArrayList will store Locations, it will look like this:

    PHP:
    private List<Locationmines = new ArrayList<Location>();
    Now that we have that, we will want to listen for BlockPlaceEvent.
    We will first want to create this event:
    PHP:
    @EventHandler
    public void onPlace(BlockPlaceEvent event) {
    //TODO: Add logic
    }
    Now we will add a check if it is a STONE_PLATE (which is what we will use in this situation)

    PHP:
    if(event.getBlock().getType().equals(Material.STONE_PLATE)) {
    //TODO: More logic
    }
    So now the event will look like this:

    PHP:
    @EventHandler
    public void onPlace(BlockPlaceEvent event) {
        if(
    event.getBlock().getType().equals(Material.STONE_PLATE)) {
            
    //TODO: Add logic
        
    }
    }
    Now we want to add that block to the List we made.

    PHP:
    mines.add(event.getBlock().getLocation());
    the event will now look like this:

    PHP:
    @EventHandler
    public void onPlace(BlockPlaceEvent event) {
        if(
    event.getBlock().getType().equals(Material.STONE_PLATE)) {
            
    mines.add(event.getBlock().getLocation());
        }
    }
    now that we have that, you can add messages, I will do this:


    PHP:
    @EventHandler
    public void onPlace(BlockPlaceEvent event) {
        if(
    event.getBlock().getType().equals(Material.STONE_PLATE)) {
            
    mines.add(event.getBlock().getLocation());
            
    event.getPlayer().sendMessage(ChatColor.GOLD "You created a " ChatColor.DARK_RED " land mine!");
        }
    }
    We now have it set up!
    Now we need to detect if they trigger it, to do this we will use PlayerInteractEvent:

    PHP:
    @EventHandler
    public void onActivate(PlayerInteractEvent event) {
    //TODO: Add activation logic
    }
    now we want to check if the action is PHYSICAL and if it's a stone plate.

    PHP:
    if(event.getAction().equals(Action.PHYSICAL)) {
        if(
    event.getClickedBlock().getType().equals(Material.STONE_PLATE)) {
            
    //TODO: Logic
        
    }
    }
    So now the event would look like this:



    PHP:
    @EventHandler
    public void onActivate(PlayerInteractEvent event) {
        if(
    event.getAction().equals(Action.PHYSICAL)) {
            if(
    event.getClickedBlock().getType().equals(Material.STONE_PLATE)) {
                
    //TODO: Logic
            
    }
        }
    }
    Now we want to check if the block they triggered is in the mines arraylist.

    PHP:
    if(mines.contains(event.getClickedBlock().getLocation()) {
    //TODO: More logic
    }
    And the event will now look like this:



    PHP:
    @EventHandler
    public void onActivate(PlayerInteractEvent event) {
        if(
    event.getAction().equals(Action.PHYSICAL)) {
            if(
    event.getClickedBlock().getType().equals(Material.STONE_PLATE)) {
                if(
    mines.contains(event.getClickedBlock().getLocation()) {
                    
    //TODO: More logic
                
    }
            }
        }
    }
    The last thing we need to do is remove the block, remove it from the arraylist, and make a kaboom!

    PHP:
    event.getClickedBlock().setType(Material.AIR);
    mines.remove(event.getClickedBlock().getLocation());
    event.getClickedBlock().getWorld().createExplosion(event.getClickedBlock().getLocation(), 2false);
    Now, the event will look like this!



    PHP:
    @EventHandler
    public void onActivate(PlayerInteractEvent event) {
        if(
    event.getAction().equals(Action.PHYSICAL)) {
            if(
    event.getClickedBlock().getType().equals(Material.STONE_PLATE)) {
                if(
    mines.contains(event.getClickedBlock().getLocation()) {
                    
    event.getClickedBlock().setType(Material.AIR);
                    
    mines.remove(event.getClickedBlock().getLocation());
                    
    event.getClickedBlock().getWorld().createExplosion(event.getClickedBlock().getLocation(), 2false);
                }
            }
        }
    }
    Here is both of the events together:



    PHP:
    @EventHandler
    public void onActivate(PlayerInteractEvent event) {
        if(
    event.getAction().equals(Action.PHYSICAL)) {
            if(
    event.getClickedBlock().getType().equals(Material.STONE_PLATE)) {
                if(
    mines.contains(event.getClickedBlock().getLocation()) {
                    
    event.getClickedBlock().setType(Material.AIR);
                    
    mines.remove(event.getClickedBlock().getLocation());
                    
    event.getClickedBlock().getWorld().createExplosion(event.getClickedBlock().getLocation(), 2false);
                }
            }
        }
     
        @
    EventHandler
        
    public void onPlace(BlockPlaceEvent event) {
            if(
    event.getBlock().getType().equals(Material.STONE_PLATE)) {
                
    mines.add(event.getBlock().getLocation());
                
    event.getPlayer().sendMessage(ChatColor.GOLD "You created a " ChatColor.DARK_RED " land mine!");
            }
        }
    }
    Pastiebin of this class:

    http://pastiebin.com/52a8a36bd582f
    You also need to register your events and implement Listener!

    Let me know if it helped!

    :D
     
    reider45 and ArthurMaker like this.
  2. sgavster
    Not trying to be rude or anything, but did this really take you a couple hours to do? I remember when I made a claymore plugin for someone, which was nearly the same as this, but more complex, and it took me only about 15 minutes.

    Anyways, nice tutorial. When comparing enums, you should use == instead of equals().
     
    MarinD99 and Chinwe like this.
  3. Offline

    sgavster

    Assist Hours adding up to
    testing
    coding
    optimizing
    ect

    And thanks.

    I'll probably change that. :D
     
  4. Offline

    Not2EXceL


    The only reason to use == over equals() when comparing enums is it's null-safe. Otherwise equals() defaults to ==
     
  5. Offline

    BungeeTheCookie

    You just got Katnissed. Great tutorial sgavster
     
  6. Offline

    macguy8

    You should listen for WorldUnloadEvent and remove all locations from your list that are in that world, to allow the GC to unload the world and prevent memory leaks.
     
  7. Offline

    sgavster

  8. Offline

    HeavyMine13

    Can U make a claymore tut?
     
  9. HeavyMine13
    As I said, it was pretty much the same as this, but it supported different color claymores (using carpets). You place a snow layer, and it opens an inventory with all different carpets, you click a carpet of your choice, and the snow turns into that carpet. When player walks on a claymore, it blows up.

    On plugin disable, it also saved the claymores to a file, so they will continue to work after server reload.
     
  10. Offline

    HeavyMine13

    Thanks!

    I made something here for my minigame, but I cannot get the pressure plates to be saved for use after a restart or something (Help me please with an example or something):
    Code:java
    1. package me.HeavyMine13.jm;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.List;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.Location;
    9. import org.bukkit.Material;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.block.Action;
    13. import org.bukkit.event.block.BlockPlaceEvent;
    14. import org.bukkit.event.player.PlayerInteractEvent;
    15. import org.bukkit.plugin.Plugin;
    16. import org.bukkit.plugin.java.JavaPlugin;
    17. import org.bukkit.potion.PotionEffect;
    18. import org.bukkit.potion.PotionEffectType;
    19.  
    20. public class Jm extends JavaPlugin implements Listener {
    21. private List<Location> mines = new ArrayList<Location>();
    22.  
    23. public void onEnable() {
    24. Bukkit.getServer().getPluginManager().registerEvents(this, (Plugin) this);
    25. }
    26.  
    27. public void onDisable() {
    28. }
    29.  
    30. public List<Location> getMines() {
    31. return mines;
    32. }
    33.  
    34. public void setMines(List<Location> mines) {
    35. this.mines = mines;
    36. }
    37.  
    38. @EventHandler
    39. public void onActivate(PlayerInteractEvent event) {
    40. if(event.getAction().equals(Action.PHYSICAL)) {
    41. if(event.getClickedBlock().getType().equals(Material.STONE_PLATE)) {
    42. if(mines.contains(event.getClickedBlock().getLocation())) {
    43. event.getClickedBlock().setType(Material.AIR);
    44. mines.remove(event.getClickedBlock().getLocation());
    45. event.getClickedBlock().getWorld().createExplosion(event.getClickedBlock().getLocation(), 0, false);
    46. event.getPlayer().setHealth(10.0);
    47. event.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 100, 1));
    48.  
    49. }
    50. }
    51. }
    52. }
    53. @EventHandler
    54. public void onPlace(BlockPlaceEvent event) {
    55. if(event.getBlock().getType().equals(Material.STONE_PLATE)) {
    56. mines.add(event.getBlock().getLocation());
    57. event.getPlayer().sendMessage(ChatColor.GOLD + "You created a " + ChatColor.DARK_RED + " land mine!");
    58. }
    59. }
    60. }
    61.  
    62.  
    63.  
    64.  
    65.  


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
  11. HeavyMine13
    Well what I did was I created a string list in the config, then turned all of the claymore locations (in your case, land mines) to strings with this format
    Code:
    world:x:y:z
    then saved those strings in the string list. Then, on plugin enable, I loaded the strings from the config, turned them into locations, and saved in an ArrayList to be used again.

    We should probably continue this conversation somewhere else, as this is sgavster 's tutorial thread, and I don't want to fill it with unrelated stuff.
     
  12. Offline

    Skyost

    Use == instead of .equals to compare enums (it is more correct).
     
  13. Offline

    sgavster

    Skyost ...... Assist said that, and I answered. I do not need it repeated.
     
  14. Offline

    ZeusAllMighty11

    Enums should be compared with ==. Also, you should not be storing locations. Instead, store vectors.
     
  15. not sure if you noticed, but
     
  16. Offline

    ZeusAllMighty11

    Assist

    3rd time's the charm? :D
     
    Assist likes this.
  17. Offline

    sgavster

    HeavyMine13
    Maybe try this?

    Code:java
    1. public void locToConfig(Location l) {
    2. double x = l.getX();
    3. double y = l.getY();
    4. double z = l.getZ();
    5. String w = l.getWorld().getName();
    6. String sep = ",";
    7. getConfig().set("locations", "|" + w + sep + Double.toString(x) + sep + Double.toString(y) + sep + Double.toString(z));
    8. }


    Then to get it

    Code:java
    1. public void getLocFromConfig(Location l) {
    2. String x = Double.toString(l.getX());
    3. String y = Double.toString(l.getY());
    4. String z = Double.toString(l.getZ());
    5. String w = l.getWorld().getName();
    6. String toSplit = l.getString("locations");
    7. String[] bannanaSplit = toSplit.split(",").replace("|", ",");
    8. for(String allOfTheStrings : bannanaSplit) {
    9. if(allOfTheStrings.equalsIgnoreCase(x) && allOfTheStrings.equalsIgnoreCase(y) && allOfTheStrings.equalsIgnoreCase(z) && allOfTheStrings.equalsIgnoreCase(w);
    10. }
    11. }


    Untested! Goodluck :)
     
  18. Offline

    MrTurtle

    I get a error on "onPlace" could someone help?
     
Thread Status:
Not open for further replies.

Share This Page