Paintball Plugin Issues

Discussion in 'Plugin Development' started by rocky0745, Mar 29, 2016.

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

    rocky0745

    I've been trying to make a paintball plugin for a couple of my friends to play with. However, when I throw a snowball at another player, nothing happens. I have a Team class and the event class:

    Teams:
    Code:
    public class Teams {
     
        List<String> redTeam = new ArrayList<String>();
        List<String> blueTeam = new ArrayList<String>();
    
        Location redSpawn = new Location(null, 0, 0, 0);
        Location blueSpawn = new Location(null, 0, 0, 0);
     
        public List<String> getRedTeam() {
            return redTeam;
        }
     
        public List<String> getBlueTeam() {
            return blueTeam;
        }
     
        public void addPlayerToRed(Player player) {
            String playerName = player.getName();
            redTeam.add(playerName);
        }
     
        public void addPlayerToBlue(Player player) {
            String playerName = player.getName();
            blueTeam.add(playerName);
        }
     
        public void removePlayerFromRed(Player player) {
            String playerName = player.getName();
            redTeam.remove(playerName);
        }
     
        public void removePlayerFromBlue(Player player) {
            String playerName = player.getName();
            blueTeam.remove(playerName);
        }
     
        public boolean isOnRedTeam(Player player) {
            String playerName = player.getName();
            return redTeam.contains(playerName);
        }
     
        public boolean isOnBlueTeam(Player player) {
            String playerName = player.getName();
            return blueTeam.contains(playerName);
        }
     
        public void setRedSpawn(Location redLoc) {
            redSpawn = redLoc;
        }
     
        public void setBlueSpawn(Location blueLoc) {
            blueSpawn = blueLoc;
        }
     
        public Location getRedSpawn() {
            return redSpawn;
        }
     
        public Location getBlueSpawn() {
            return blueSpawn;
        }
    }
    Event Class:
    Code:
    public class EntityDamageByEntityListener implements Listener {
      
        @EventHandler(priority = EventPriority.LOW, ignoreCancelled = false)
        public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
            Entity damaged = event.getEntity();
            Entity damageEntity = event.getDamager();
            System.out.println("EDBE event fired!");
            if(damaged instanceof Player && damageEntity instanceof Snowball) {
                Snowball snowball = (Snowball) damageEntity;
                LivingEntity entityThrower = (LivingEntity) snowball.getShooter();
                System.out.println("Damaged is player & damager is snowball.");
                if(entityThrower instanceof Player) {
                    System.out.println("entityThrower is an instanceof Player!");
                //if(snowball.getShooter() instanceof Player) {// && damaged instanceof Player) {
                  
                    Teams teams = new Teams();
                    ItemCreator creator = new ItemCreator();
                  
                    Player shooter = (Player) entityThrower;
                    Player victim = (Player) damaged;
                  
                    Location redSpawn = teams.getRedSpawn();
                    Location blueSpawn = teams.getBlueSpawn();
                  
                    ItemStack paintballs = creator.createItem(Material.SNOW_BALL, 16, ChatColor.AQUA, "Paintball", true);
                  
                    if(teams.isOnBlueTeam(shooter) && teams.isOnRedTeam(victim)) {
                        Bukkit.getServer().broadcastMessage(ChatColor.RED + victim.getName() + ChatColor.GREEN + " was paintballed by " + ChatColor.BLUE + shooter.getName() + "!");
                        victim.teleport(redSpawn);
                        victim.getInventory().clear();
                      
                        for(int i = 0; i < 4; i++) {
                            victim.getInventory().addItem(paintballs);
                        }
                    } else if(teams.isOnRedTeam(shooter) && teams.isOnBlueTeam(victim)) {
                        Bukkit.getServer().broadcastMessage(ChatColor.BLUE + victim.getName() + ChatColor.GREEN + " was paintballed by " + ChatColor.RED + shooter.getName() + "!");
                        victim.teleport(blueSpawn);
                        victim.getInventory().clear();
                      
                        for(int i = 0; i < 4; i++) {
                            victim.getInventory().addItem(paintballs);
                        }
                    }
                }
              
            }
        }
    }
    My commands are commands to manually join a team, set the team's spawn point, give yourself paintballs (which are just snowballs with a custom name), and a team leave command.
    As you can see, I've tried to do some debugging with the console, and all of them print out but nothing happens when I throw a snowball. Please let me know if you need anything else provided. Thank you for your time and assistance.

    PS: I can only answer tonight and in later afternoons.

    rocky0745
     
  2. Offline

    Zombie_Striker

    So every time a player is damaged by a snowball, you create a new team? So if you hit a player with a snowball 20 times, you create 20 new teams? Is this what you wanted?
     
  3. Offline

    rocky0745

    No, that was supposed to just be the initializer for the Teams class so I could use the methods in it. I have two teams pre-set within it, so I just want a red team and a blue team which are joined/left when you use the command. Should I move it above the event method so it's only called once?
     
  4. Offline

    Zombie_Striker

    Because of the way OOP works, every time you create a new instance of "Team", you are creating two new arrays to hold the "redteam" and the "blue team". In essence, hitting a player with 2 snowballs create 4 color teams/2 instances of "Team".

    Create getters and setters for the "Team" instance, or create one field which contains the instance. We only want one instance of that class.
     
    mine-care likes this.
  5. Offline

    rocky0745

    I tried to put the Teams initializer above the actual listener method but the same thing still happens :/
     
  6. Offline

    Kilorbine

    @rocky0745 and with this code you don't have anything of you debug printed?
     
  7. Offline

    rocky0745

    I haven't tested it out yet, but I will this afternoon.
     
  8. Offline

    mine-care

  9. Offline

    rocky0745

    @mine-care, @Kilorbine, @Zombie_Striker
    Ah, whoops, thanks.

    I tested it out with debug messages and this is what was printed in the console:

    Code:
    [17:49:55 INFO]: Event fired!
    [17:49:55 INFO]: Damaged is player & damageentity is snowball!
    [17:49:55 INFO]: Shooter & damaged are players!
    Also, here is all my code, @mine-care :

    Snowball Listener Class
    Code:
    public class EntityDamageByEntityListener implements Listener {
    
        Teams teams = new Teams();
    
        @EventHandler(priority = EventPriority.LOW, ignoreCancelled = false)
        public void onEntityDamageByEntity(EntityDamageByEntityEvent event) {
            Entity damaged = event.getEntity();
            Entity damageEntity = event.getDamager();
            System.out.println("Event fired!");
            if(damaged instanceof Player && damageEntity instanceof Snowball) {
                Snowball snowball = (Snowball) damageEntity;
                LivingEntity entityThrower = (LivingEntity) snowball.getShooter();
                System.out.println("Damaged is player & damageentity is snowball!");
                if(entityThrower instanceof Player)
                if(snowball.getShooter() instanceof Player && damaged instanceof Player) {
                  
                    System.out.println("Shooter & damaged are players!");
                    ItemCreator creator = new ItemCreator();
                  
                    Player shooter = (Player) entityThrower;
                    Player victim = (Player) damaged;
                  
                    Location redSpawn = teams.getRedSpawn();
                    Location blueSpawn = teams.getBlueSpawn();
                  
                    ItemStack paintballs = creator.createItem(Material.SNOW_BALL, 16, ChatColor.AQUA, "Paintball", true);
                  
                    if(teams.isOnBlueTeam(shooter) && teams.isOnRedTeam(victim)) {
                        Bukkit.getServer().broadcastMessage(ChatColor.RED + victim.getName() + ChatColor.GREEN + " was paintballed by " + ChatColor.BLUE + shooter.getName() + "!");
                        victim.teleport(redSpawn);
                        victim.getInventory().clear();
                        System.out.println("Blue team shooter & red team victim");
                        for(int i = 0; i < 4; i++) {
                            victim.getInventory().addItem(paintballs);
                        }
                    } else if(teams.isOnRedTeam(shooter) && teams.isOnBlueTeam(victim)) {
                        Bukkit.getServer().broadcastMessage(ChatColor.BLUE + victim.getName() + ChatColor.GREEN + " was paintballed by " + ChatColor.RED + shooter.getName() + "!");
                        victim.teleport(blueSpawn);
                        victim.getInventory().clear();
                        System.out.println("Red team shooter & blue team victim");
                        for(int i = 0; i < 4; i++) {
                            victim.getInventory().addItem(paintballs);
                        }
                    }
              
                }
              
            }
        }
    }
    Main Class
    Code:
    public class PaintballMain extends JavaPlugin {
        public void onEnable() {
          
            CommandRegistrator cRegistrator = new CommandRegistrator(this);
            EventRegistrator eRegistrator = new EventRegistrator();
          
            cRegistrator.registerCommand("paintball", new PaintballCommand());
            cRegistrator.registerCommand("givepaintballs", new GivePaintballCommand());
            cRegistrator.registerCommand("team", new TeamCommand());
          
            eRegistrator.registerEvent(new EntityDamageByEntityListener(), this);
        }
    }
    Teams Class
    Code:
    public class Teams {
      
        List<String> redTeam = new ArrayList<String>();
        List<String> blueTeam = new ArrayList<String>();
    
        Location redSpawn = new Location(null, 0, 0, 0);
        Location blueSpawn = new Location(null, 0, 0, 0);
    
        public List<String> getRedTeam() {
            return redTeam;
        }
      
        public List<String> getBlueTeam() {
            return blueTeam;
        }
      
        public void addPlayerToRed(Player player) {
            String playerName = player.getName();
            redTeam.add(playerName);
        }
      
        public void addPlayerToBlue(Player player) {
            String playerName = player.getName();
            blueTeam.add(playerName);
        }
      
        public void removePlayerFromRed(Player player) {
            String playerName = player.getName();
            redTeam.remove(playerName);
        }
      
        public void removePlayerFromBlue(Player player) {
            String playerName = player.getName();
            blueTeam.remove(playerName);
        }
      
        public boolean isOnRedTeam(Player player) {
            String playerName = player.getName();
            return redTeam.contains(playerName);
        }
      
        public boolean isOnBlueTeam(Player player) {
            String playerName = player.getName();
            return blueTeam.contains(playerName);
        }
      
        public void setRedSpawn(Location redLoc) {
            redSpawn = redLoc;
        }
      
        public void setBlueSpawn(Location blueLoc) {
            blueSpawn = blueLoc;
        }
      
        public Location getRedSpawn() {
            return redSpawn;
        }
      
        public Location getBlueSpawn() {
            return blueSpawn;
        }
    }
    That is all the code that has to do with the teams. Here is some other commands I have just in case you need them:

    /givepaintballs Command
    Code:
    public class GivePaintballCommand implements CommandExecutor {
    
        @SuppressWarnings("deprecation")
        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
          
            if(sender instanceof Player) {
              
                Player player = (Player) sender;
              
                if(command.getName().equalsIgnoreCase("givepaintballs")) {
                  
                    ItemCreator creator = new ItemCreator();
                  
                    if(args.length == 0) {
                        player.sendMessage(ChatColor.RED + "You must provide an amount of paintballs to give!");
                    } else if(args.length == 1) {
                        //Give sender number of paintballs
                        try {
                            int numberOfPaintballs = Integer.parseInt(args[0]);
                            ItemStack paintballs = creator.createItem(Material.SNOW_BALL, numberOfPaintballs, ChatColor.AQUA, "Paintball", true);
                            player.getInventory().addItem(paintballs);
                            player.sendMessage(ChatColor.GREEN + "Successfully gave " + player.getName() + " " + numberOfPaintballs + " paintballs!");  
                        } catch(Exception e) {
                            player.sendMessage(ChatColor.RED + "Invalid number!");
                        }
                    } else if(args.length == 2) {
                        //Give number of paintballs to player in argument #2
                        try {
                            int numberOfPaintballs = Integer.parseInt(args[0]);
                            Player receiver = Bukkit.getPlayer(args[1]);
                          
                            ItemStack paintballs = creator.createItem(Material.SNOW_BALL, numberOfPaintballs, ChatColor.AQUA, "Paintball", true);
                            receiver.getInventory().addItem(paintballs);
                            Bukkit.getServer().broadcastMessage(ChatColor.GREEN + player.getName() + " successfully gave " + receiver.getName() + " " + numberOfPaintballs + " paintballs!");  
                        } catch(Exception e) {
                            player.sendMessage(ChatColor.RED + "Invalid number or player!");
                        }
                    } else {
                        player.sendMessage(ChatColor.RED + "Invalid number of arguments!");
                    }
                }
              
            } else {
                sender.sendMessage("Only players may use this command!");
            }
          
            return true;
        }
      
      
    }
    /paintball Command
    Code:
    @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
          
            if(sender instanceof Player) {
              
                Player player = (Player) sender;
                TextBolder bolder = new TextBolder();
              
                if(command.getName().equalsIgnoreCase("paintball")) {
                    if(args.length == 1) {
                        if(args[0].equalsIgnoreCase("help")) {
                            player.sendMessage(bolder.boldTextWithColor(ChatColor.GOLD, "Throwing a snowball given with /snowball will kill a player, teleporting them back to their base."));
                            player.sendMessage(bolder.boldTextWithColor(ChatColor.GOLD, "Also, use /paintball setspawn <blue|red> to set the team's base."));
                        }
                    } else if(args.length == 2) {
                        if(args[0].equalsIgnoreCase("setspawn")) {
                            if(args[1].equalsIgnoreCase("red")) {
                                //set red spawn at player's location
                            } else if(args[1].equalsIgnoreCase("blue")) {
                                //set blue spawn at player's location
                            }
                        }
                    }
                }
            } else {
                sender.sendMessage("Only players may use this command!");
            }
            return true;
        }
    
    }
    /team Command
    Code:
    @Override
    
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    
           
    
            if(sender instanceof Player) {
    
               
    
                Player player = (Player) sender;
    
                //TextBolder bolder = new TextBolder();
    
                Teams teams = new Teams();
    
               
    
                if(command.getName().equalsIgnoreCase("team")) {
    
                   
    
                    ItemCreator creator = new ItemCreator();
    
                   
    
                    if(args.length == 1) {
    
                        if(args[0].equalsIgnoreCase("spawn")) {
    
                            if(teams.isOnRedTeam(player)) {
    
                                player.teleport(teams.getRedSpawn());
    
                            } else if(teams.isOnBlueTeam(player)) {
    
                                player.teleport(teams.getBlueSpawn());
    
                            }
    
                        }
    
                    }
    
                    if(args.length == 2) {
    
                        if(args[0].equalsIgnoreCase("join")) {
    
                           
    
                            PlayerInventory pi = player.getInventory();
    
                           
    
                            if(args[1].equalsIgnoreCase("red")) {
    
                                //Put player on red team
    
                                teams.addPlayerToRed(player);
    
                               
    
                                ItemStack redWoolHelmet = new ItemStack(Material.WOOL, 1, (byte) 14);
    
                               
    
                                ItemStack redChestplate = creator.createItem(Material.LEATHER_CHESTPLATE, 1, ChatColor.RED, "Red Team Chestplate", false);
    
                                LeatherArmorMeta chestMeta = (LeatherArmorMeta) redChestplate.getItemMeta();
    
                                chestMeta.setColor(Color.fromRGB(227, 68, 66));
    
                                redChestplate.setItemMeta(chestMeta);
    
                               
    
                                ItemStack redLegs = creator.createItem(Material.LEATHER_LEGGINGS, 1, ChatColor.RED, "Red Team Leggings", false);
    
                                LeatherArmorMeta legMeta = (LeatherArmorMeta) redLegs.getItemMeta();
    
                                legMeta.setColor(Color.fromRGB(227, 68, 66));
    
                                redLegs.setItemMeta(legMeta);
    
                               
    
                                ItemStack redBoots = creator.createItem(Material.LEATHER_BOOTS, 1, ChatColor.RED, "Red Team Boots", false);
    
                                LeatherArmorMeta bootMeta = (LeatherArmorMeta) redBoots.getItemMeta();
    
                                bootMeta.setColor(Color.fromRGB(227, 68, 66));
    
                                redBoots.setItemMeta(bootMeta);
    
                               
    
                                ItemStack[] armor = new ItemStack[]{redBoots, redLegs, redChestplate, redWoolHelmet};
    
                                pi.setArmorContents(armor);
    
                               
    
                                player.sendMessage(ChatColor.GREEN + "Added you to the " + ChatColor.RED + "RED" + ChatColor.GREEN + " team!");
    
                               
    
                            } else if(args[1].equalsIgnoreCase("blue")) {
    
                                //Put player on blue team
    
                                teams.addPlayerToBlue(player);
    
                               
    
                                ItemStack blueWoolHelmet = new ItemStack(Material.WOOL, 1, (byte) 11);
    
                               
    
                                ItemStack blueChestplate = creator.createItem(Material.LEATHER_CHESTPLATE, 1, ChatColor.BLUE, "Blue Team Chestplate", false);
    
                                LeatherArmorMeta chestMeta = (LeatherArmorMeta) blueChestplate.getItemMeta();
    
                                chestMeta.setColor(Color.fromRGB(0, 0, 255));
    
                                blueChestplate.setItemMeta(chestMeta);
    
                               
    
                                ItemStack blueLegs = creator.createItem(Material.LEATHER_LEGGINGS, 1, ChatColor.BLUE, "Blue Team Leggings", false);
    
                                LeatherArmorMeta legMeta = (LeatherArmorMeta) blueLegs.getItemMeta();
    
                                legMeta.setColor(Color.fromRGB(0, 0, 255));
    
                                blueLegs.setItemMeta(legMeta);
    
                               
    
                                ItemStack blueBoots = creator.createItem(Material.LEATHER_BOOTS, 1, ChatColor.BLUE, "Blue Team Boots", false);
    
                                LeatherArmorMeta bootMeta = (LeatherArmorMeta) blueBoots.getItemMeta();
    
                                bootMeta.setColor(Color.fromRGB(0, 0, 255));
    
                                blueBoots.setItemMeta(bootMeta);
    
                               
    
                                ItemStack[] armor = new ItemStack[]{blueBoots, blueLegs, blueChestplate, blueWoolHelmet};
    
                                pi.setArmorContents(armor);
    
                               
    
                                player.sendMessage(ChatColor.GREEN + "Added you to the " + ChatColor.BLUE + "BLUE" + ChatColor.GREEN + " team!");
    
                            }
    
                        } else if(args[0].equalsIgnoreCase("setspawn")) {
    
                           
    
                            Location playerLoc = player.getLocation();
    
                           
    
                            if(args[1].equalsIgnoreCase("red")) {
    
                                teams.setRedSpawn(playerLoc);
    
                               
    
                                int x = teams.getRedSpawn().getBlockX();
    
                                int y = teams.getRedSpawn().getBlockY();
    
                                int z = teams.getRedSpawn().getBlockZ();
    
                               
    
                                player.sendMessage(ChatColor.GREEN + "Set " + ChatColor.RED + "RED" + ChatColor.GREEN + " spawn to (" + x + "," + y + "," + z + ")!");
    
                               
    
                            } else if(args[1].equalsIgnoreCase("blue")) {
    
                                teams.setBlueSpawn(playerLoc);
    
                               
    
                                int x = teams.getBlueSpawn().getBlockX();
    
                                int y = teams.getBlueSpawn().getBlockY();
    
                                int z = teams.getBlueSpawn().getBlockZ();
    
                               
    
                                player.sendMessage(ChatColor.GREEN + "Set " + ChatColor.BLUE + "BLUE" + ChatColor.GREEN + " spawn to (" + x + "," + y + "," + z + ")!");
    
                            }
    
                        } else if(args[0].equalsIgnoreCase("leave")) {
    
                            if(teams.isOnRedTeam(player)) {
    
                                teams.removePlayerFromRed(player);
    
                                player.sendMessage(ChatColor.GREEN + "Removed you from the " + ChatColor.RED + "RED" + ChatColor.GREEN + " team!");
    
                            } else if(teams.isOnBlueTeam(player)) {
    
                                teams.removePlayerFromBlue(player);
    
                                player.sendMessage(ChatColor.GREEN + "Removed you from the " + ChatColor.BLUE + "BLUE" + ChatColor.GREEN + " team!");
    
                            } else {
    
                                player.sendMessage(ChatColor.RED + "You are not on a team!");
    
                            }
    
                        }
    
                    }
    
                } 
    
            } else {
    
                sender.sendMessage("Only players may use this command!");
    
            }
    
            returntrue;
    
        }
    
    
    
    }
    Sorry to post a bunch of stuff like this, but I appreciate everyone's help!
     
  10. Offline

    Dynamus

    LivingEntity entityThrower = (LivingEntity) snowball.getShooter();
    if(entityThrower instanceof Player)

    Is this really necessary?
    Cant you just cast Player player = (Player) snowball.getShooter(); after checking if the shooter is actually a player?
     
  11. Offline

    rocky0745

    You're right. That is unnecessary now that I think about it. Thanks for the suggestion. That wouldn't be what's causing my problem though, correct? I'm just wondering but I will make sure to change it.
     
  12. Offline

    Dynamus

    Do you know if "Teams teams = new Teams();" is working? Have you tried to broadcast teams.isOnmimi to see if it's working propely?
     
  13. Offline

    rocky0745

    No I have not. I will try that once I get a chance.
     
Thread Status:
Not open for further replies.

Share This Page