Two small things I need help with.

Discussion in 'Plugin Development' started by VinexAx789, Jun 16, 2015.

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

    VinexAx789

    LandMine Part: What I want to do is make a "ssssssssss" sound when a player steps on the landmine then the explosion will be delayed for 1 second then explode who ever places the land mine it shouldn't hurt them nor there teammates I have four teams red team, green team, blue team then purple team I also do not want it damaging blocks just players it should almost kill them too but not the team that uses it.

    LandMine Code:

    Code:
        private List<Location> mines = new ArrayList<Location>();
       
        @EventHandler
        public void onActivate(final PlayerInteractEvent event) {
            final Player p = event.getPlayer();
            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());
                        Bukkit.getScheduler().scheduleSyncDelayedTask(
                                Bukkit.getPluginManager().getPlugin(
                                        "MyTestPlugin"), new Runnable() {
                            public void run(){
                                p.playSound(p.getLocation(), Sound.FUSE, 50F, 50F);
                                event.getClickedBlock().getWorld().createExplosion(event.getClickedBlock().getLocation(), 2, false);
                            }
                        },1L);
                    }
                }
            }
        }
       
            @EventHandler
            public void onPlaceMine(BlockPlaceEvent event) {
                if(event.getBlock().getType().equals(Material.STONE_PLATE)) {
                    mines.add(event.getBlock().getLocation());
                    event.getPlayer().sendMessage(ChatColor.YELLOW + "You have placed a" + ChatColor.RED + " landmine!");
                }
            }
    Team Players Code:

    I use this to sort who gets certain things.

    Code:
        ArrayList<Player> redplayers = new ArrayList<Player>();
        ArrayList<Player> greenplayers = new ArrayList<Player>();
        ArrayList<Player> blueplayers = new ArrayList<Player>();
        ArrayList<Player> purpleplayers = new ArrayList<Player>();
        
    Pyro Kit: So I have a small issue when a bow with flame 2 is shot at tnt it turns into primed and blows up so I can't have that happen so what I need to do is make it so when a player gets hit with an arrow from kit pyro they get fire on them for the amount of time the fire ticks stay on so this is what I did.

    Pyro Code:

    Code:
        @EventHandler
        public void onEntityDamageByEntity11(EntityDamageByEntityEvent event) {
            Entity entity = event.getDamager();
            if ((entity instanceof Arrow)) {
                Arrow arrow = (Arrow) entity;
                Entity shooter = arrow.getShooter();
                if(kitselected.get(arrow).equals("pyro")){
                if ((shooter instanceof Player)) {
                        Entity entity1 = event.getEntity();
                        if (players.contains(((Player) shooter).getPlayer().getName())) {
                            entity1.setFireTicks(80);
                        }
    
                    }
                }
    Please help me fix my buggy code thanks!
     
    Last edited: Jun 17, 2015
  2. Offline

    JUSTCAMH

    @VinexAx789 Well, I can't do everything for you, so instead of giving code I'll give u an idea of what to do:

    For your first problem, you will first need to make a HashMap. Make it that when you place the landmine (BlockPlaceEvent), it will add to the hashmap the Block and team, so your HashMap might look something like this:
    HashMap<Block,String>
    For the delay, all you have to do is make a BukkitRunnable and then run it later.
    Than, to make the mine only damage players of the opposite teams, make it that when you stand on the mine, it will get the hashmap we made earlier and get the team that placed the mine. It will then go through all the nearby players of that team and do p.setNoDamageTicks(1)
    Than run the explosion and they shouldn't take damage.

    For your second problem, have a class that has a runnable running every tick or two and set the block at the location of any primed tnt to tnt and kill the primed tnt.
     
  3. Offline

    VinexAx789

    @JUSTCAMH So let me sum this up

    1. Add a HashMap such as this for an example
    Code:
    HashMap<Block, Player> stoneplate = new HashMap<Block, Player>();
    2. Make a BukkitRunnble such as this for an example
    Code:
        private List<Location> mines = new ArrayList<Location>();
    
        @EventHandler
        public void onActivate(final PlayerInteractEvent event) {
            final Player p = event.getPlayer();
            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());
                        new BukkitRunnable() {
                        
                            public void run() {
                                p.playSound(p.getLocation(), Sound.FUSE, 50F, 50F);
                                event.getClickedBlock().getWorld().createExplosion(event.getClickedBlock().getLocation(), 2, false);
                            
                            }
                        };
                    }
                    }
                }
        }
    3. Now for this one I'm having trouble doing since I'm sorta a moderate developer still I'm horrible with HashMaps so I might of made the one I created up there incorrect.

    4. I basically get the block that is TNTPrimed then delete it then spawn a TNT block there again.

    5. Now the only thing do not understand how to do is the hashmap I should say I do not understand because I do I'm just having a brain fart atm.

    If you could help me out some more it would be much appreciated! :) Thanks for the help so far!
     
    Last edited: Jun 17, 2015
  4. Offline

    JUSTCAMH

    @VinexAx789 Ok, so for the Hashmap, you would need to have a BlockPlaceEvent. In this event you would want to set the block parameter to the block that was placed and the String to the team that placed it. Then when you have your runnable, just give all nearby players of that team invulnerability.
     
  5. You need to use
    Code:
    new BukkitRunnable() {
    //Code and run() method
    }.runTaskLater(this, 0, 20);
    to make it run a second later. And I would suggest you putting the ssssssssssss sound before the runnable

    Thats bad. A better way is to use the ExplosionPrimeEvent, check if the entity type is TNT and then cancel
     
    VinexAx789 likes this.
  6. Offline

    VinexAx789

    @FisheyLP thanks for the help! @JUSTCAMH so would my hash map be like this:
    Code (Java):
    1. HashMap<BlockPlaceEvent, String> stoneplate = new HashMap<BlockPlaceEvent , String>();
     
  7. @VinexAx789 Nope.
    The idea behind this is to store the block (landmine) and the team name (who created the landmine).
    HashMap<Block, String>

    Then you need to get the team name:
    Code:
    String teamName = "";
    if (redPlayers.contains(p) teamName = "red";
    if (greenPlayers.contains(p) teamName = "green";
    if (bluePlayers.contains(p) teamName = "blue";
    if (purplePlayers.contains(p) teamName = "purple";
    And in the PlayerInteractEvent, use the same thing but reverse (hm is the HashMap):
    Code:
    String tn = hm.get(block);
    if ((tn == "red" && !redPlayers.contains(p) || (tn == "green" && !greenPlayers.contains(p)) || ...
     
  8. Offline

    VinexAx789

    @FisheyLP thanks

    @FisheyLP @JUSTCAMH

    Hopefully I did this correct.


    Code:
        private List<Location> mines = new ArrayList<Location>();
    
        @EventHandler
        public void onActivate(final PlayerInteractEvent event) {
            final Player p = event.getPlayer();
            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());
                        p.playSound(p.getLocation(), Sound.FUSE, 50F, 50F);
                        new BukkitRunnable() {
    
                            public void run() {
                                event.getClickedBlock()
                                        .getWorld()
                                        .createExplosion(
                                                event.getClickedBlock()
                                                        .getLocation(), 2, false);
    
                            }
                        }.runTaskLater(this, 20);
                    }
                }
            }
        }
       
        HashMap<Block, Player> stoneplate = new HashMap<Block, Player>();
        HashMap<Block, String> landmineteams = new HashMap<Block , String>();
       
            @EventHandler
            public void onPlaceMine(BlockPlaceEvent event) {
                Player p = event.getPlayer();
                String teamName = "";
                if (redplayers.contains(p)) teamName = "red";
                if (greenplayers.contains(p)) teamName = "green";
                if (blueplayers.contains(p)) teamName = "blue";
                if (purpleplayers.contains(p)) teamName = "purple";
                if(event.getBlock().getType().equals(Material.STONE_PLATE)) {
                    String tn = landmineteams.get(Material.STONE_PLATE);
                    if ((tn == "red" && !redplayers.contains(p) || (tn == "green" && !greenplayers.contains(p)) || ...
                    mines.add(event.getBlock().getLocation())));
                    event.getPlayer().sendMessage(ChatColor.YELLOW + "You have placed a" + ChatColor.RED + " landmine!");
                }
            }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  9. Offline

    Lilret123

    @VinexAx789 is no one gonna talk about storing player objects?..... no?... ok nvm then....
     
  10. Offline

    VinexAx789

  11. Offline

    Zombie_Striker

    @VinexAx789 @Lilret123 To try and get someone (like me) to tell you not to store the whole player in anything (hasmaps, arrays ect.), but instead to store their UUID. Change all the variables from Player to UUID, and instead of putting in the player, you would put in (Player#getUniqueID())
     
  12. Offline

    VinexAx789

    @Zombie_Striker Yes I know that's a good way to store but that's not what I need help with my plugin is over 4 thousand lines of code and I have to many methods to change to UUID I will do it over time but again that's not what I'm needing.
     
  13. Offline

    Zombie_Striker

    @VinexAx789
    But, if you're putting a lot of players into your hashmap, then you have a massive map of useless information that's just taking up memory for you server (Causing your server to lag) . Storing the UUID is all you need and saves tons of memory. So is it so bad to take about 2 - 3 minutes of your day to copy and paste a small bit of text to save alot of memory?
     
  14. Offline

    VinexAx789

    @Zombie_Striker Yes as I said I'm not worried about it now I will do it later. I'm stuck on the problem above. Thanks for telling me though.

    @Zombie_Striker @FisheyLP @JUSTCAMH

    Updated Code is this correct?

    Code:
        @EventHandler
        public void onActivate(final PlayerInteractEvent event) {
            final Player p = event.getPlayer();
            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());
                        p.playSound(p.getLocation(), Sound.FUSE, 50F, 50F);
                        new BukkitRunnable() {
    
                            public void run() {
                                event.getClickedBlock()
                                        .getWorld()
                                        .createExplosion(
                                                event.getClickedBlock()
                                                        .getLocation(), 2, false);
    
                            }
                        }.runTaskLater(this, 20);
                    }
                }
            }
        }
       
        HashMap<Block, Player> stoneplate = new HashMap<Block, Player>();
        HashMap<Block, String> landmineteams = new HashMap<Block , String>();
       
            @EventHandler
            public void onPlaceMine(BlockPlaceEvent event) {
                Player p = event.getPlayer();
                String teamName = "";
                if (redplayers.contains(p)) teamName = "red";
                if (greenplayers.contains(p)) teamName = "green";
                if (blueplayers.contains(p)) teamName = "blue";
                if (purpleplayers.contains(p)) teamName = "purple";
                if(event.getBlock().getType().equals(Material.STONE_PLATE)) {
                    String tn = landmineteams.get(Material.STONE_PLATE);
                    if ((tn == "red" && !redplayers.contains(p) || (tn == "green" && !greenplayers.contains(p)) ||
                    mines.add(event.getBlock().getLocation())));
                    event.getPlayer().sendMessage(ChatColor.YELLOW + "You have placed a" + ChatColor.RED + " landmine!");
                }
            }
    Please help.


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

    Zombie_Striker

    @VinexAx789
    Test it your self. Does it run? Does it do the things you want. If so, it is correct.
     
Thread Status:
Not open for further replies.

Share This Page