Hi! 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<Location> mines = new ArrayList<Location>(); Now that we have that, we will want to listen for BlockPlaceEvent. We will first want to create this event: PHP: @EventHandlerpublic 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: @EventHandlerpublic 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: @EventHandlerpublic 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: @EventHandlerpublic 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: @EventHandlerpublic 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: @EventHandlerpublic 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: @EventHandlerpublic 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(), 2, false); Now, the event will look like this! PHP: @EventHandlerpublic 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(), 2, false); } } }} Here is both of the events together: PHP: @EventHandlerpublic 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(), 2, false); } } } @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!
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().
The only reason to use == over equals() when comparing enums is it's null-safe. Otherwise equals() defaults to ==
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.
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.
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 package me.HeavyMine13.jm; import java.util.ArrayList;import java.util.List; import org.bukkit.Bukkit;import org.bukkit.ChatColor;import org.bukkit.Location;import org.bukkit.Material;import org.bukkit.event.EventHandler;import org.bukkit.event.Listener;import org.bukkit.event.block.Action;import org.bukkit.event.block.BlockPlaceEvent;import org.bukkit.event.player.PlayerInteractEvent;import org.bukkit.plugin.Plugin;import org.bukkit.plugin.java.JavaPlugin;import org.bukkit.potion.PotionEffect;import org.bukkit.potion.PotionEffectType; public class Jm extends JavaPlugin implements Listener { private List<Location> mines = new ArrayList<Location>(); public void onEnable() { Bukkit.getServer().getPluginManager().registerEvents(this, (Plugin) this);} public void onDisable() { } public List<Location> getMines() { return mines; } public void setMines(List<Location> mines) { this.mines = mines; } @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(), 0, false); event.getPlayer().setHealth(10.0); event.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 100, 1)); } } } } @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!"); } } } EDIT by Moderator: merged posts, please use the edit button instead of double posting.
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.
HeavyMine13 Maybe try this? Code:java public void locToConfig(Location l) {double x = l.getX();double y = l.getY();double z = l.getZ();String w = l.getWorld().getName();String sep = ",";getConfig().set("locations", "|" + w + sep + Double.toString(x) + sep + Double.toString(y) + sep + Double.toString(z));} Then to get it Code:java public void getLocFromConfig(Location l) {String x = Double.toString(l.getX());String y = Double.toString(l.getY());String z = Double.toString(l.getZ());String w = l.getWorld().getName();String toSplit = l.getString("locations");String[] bannanaSplit = toSplit.split(",").replace("|", ",");for(String allOfTheStrings : bannanaSplit) {if(allOfTheStrings.equalsIgnoreCase(x) && allOfTheStrings.equalsIgnoreCase(y) && allOfTheStrings.equalsIgnoreCase(z) && allOfTheStrings.equalsIgnoreCase(w);}} Untested! Goodluck