Creating a Simple Red.vs.Blue Game

Discussion in 'Resources' started by SuperOmegaCow, Aug 20, 2013.

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

    SuperOmegaCow

    Hello and welcome to my humble tutorial. It may suck since I am new to this kind of thing (doing it for 1-2 months on and off) so don't expect much! Anyway today we shall be creating a simple red vs blue game type but can support different maps and different game types. So first lets start with the onEnable and onDisable, lets call it Main and register the Listener classes we will be using!

    Code:java
    1. package dem.coolz.packages;
    2.  
    3. import org.bukkit.plugin.java.JavaPlugin;
    4.  
    5. public class Main extends JavaPlugin{
    6.  
    7. @Override
    8. public void onEnable() {
    9. PluginManager pm = getServer().getPluginManager();
    10. pm.registerEvents(new DragonsKeepManager(), this);
    11. }
    12.  
    13. @Override
    14. public void onDisable() {
    15.  
    16. }
    17.  
    18. }


    (Explanations with be under the code from now on) So as you can see we have our 2 methods onEnable and onDisable that we registered the class called DragonsKeepManager. The reasoning behind the name is the name of the map which the contains the gametype red vs blue (too lazy to make a system that actually works well).

    Code:java
    1. package dem.coolz.packages.GameTypes;
    2.  
    3. public class DragonsKeepManager {
    4.  
    5. }
    6.  
    7. package dem.coolz.packages.GameTypes;
    8.  
    9. public class DragonsKeep {
    10.  
    11. }


    Now create two classes one is for the methods the other is 2 manage everything. So lets start off with the DragonsKeep or the methods class.

    Code:java
    1. ArrayList<String> players = new ArrayList<String>();
    2. List<String> red = new ArrayList<>();
    3. List<String> blue = new ArrayList<>();
    4.  
    5. public void addPlayerLobby(Player player){
    6. String p = player.getName();
    7. if(players.size() <=16){
    8. players.add(p);
    9. World w = player.getWorld();
    10. double x = 0;
    11. double y = 64;
    12. double z = 0;
    13. Location lobby = new Location(w, x, y, z);
    14. player.teleport(lobby);
    15. player.sendMessage( ChatColor.GREEN + "You joined" + ChatColor.RED + "DragonsKeep");
    16.  
    17. int maxPlayers = 16
    18. if(maxPlayers == 16{
    19.  
    20. startGame();
    21. }
    22.  
    23. }


    With this method it will add the player you plug-in into the arraylist players and teleport them to the waiting area of the map. It also checks to see if the size of players contains 16 players meaning the game needs to start which will run the startGame method which I will make next.

    Code:java
    1. public void startGame(){
    2.  
    3. Collections.shuffle(players);
    4.  
    5. for(String pa : players){
    6. Player player = Bukkit.getPlayer(pa);
    7. String p = ( player + "");
    8. int stuff = 0;
    9. if(stuff == 0){
    10. red.add(p);
    11. stuff++;
    12. }
    13. if(stuff == 1){
    14. blue.add(p);
    15. stuff--;
    16. }
    17. }
    18. for(String s : red){
    19. Player player = Bukkit.getPlayer(s);
    20. World w = player.getWorld();
    21. double x = 100;
    22. double y = 64;
    23. double z = 100;
    24. Location redspawn = new Location(w, x, y, z);
    25. player.teleport(redspawn);
    26. }
    27.  
    28. for(String s : red){
    29. Player player = Bukkit.getPlayer(s);
    30. World w = player.getWorld();
    31. double x = 100;
    32. double y = 64;
    33. double z = 50;
    34. Location bluespawn = new Location(w, x, y, z);
    35. player.teleport(bluespawn);
    36. }
    37.  
    38. }


    As you can see what we do is shuffle players then divide it into red and blue. Then we teleport red and blue to their spawn points (give them items if you want).

    Code:java
    1. public void resetArea(){
    2. for(String s : red){
    3. Player player = Bukkit.getPlayer(s);
    4. World w = player.getWorld();
    5. double x = 2000;
    6. double y = 64;
    7. double z = 2000;
    8. Location lobby = new Location(w, x, y, z);
    9. player.teleport(lobby);
    10. }
    11. for(String s : blue){
    12. Player player = Bukkit.getPlayer(s);
    13. World w = player.getWorld();
    14. double x = 2000;
    15. double y = 64;
    16. double z = 2000;
    17. Location lobby = new Location(w, x, y, z);
    18. player.teleport(lobby);
    19. }
    20. red.clear();
    21. blue.clear();
    22. players.clear();
    23.  
    24. }
    25.  
    26. }


    Here when reseting the game we teleport them to the server spawn point and clear all the arrays to prepare for the next wave of players (remove items if needed). Now lets work with the DragonKeepManager!

    Code:java
    1. @EventHandler
    2. public void on(PlayerInteractEvent event) {
    3. Block block = event.getClickedBlock();
    4. Player p = event.getPlayer();
    5. Material type = block.getType();
    6. if (type == Material.WALL_SIGN || type == Material.SIGN_POST || type == Material.SIGN) {
    7. BlockState state = block.getState();
    8. if (state instanceof Sign) {
    9. Sign sign = (Sign) state;
    10. if (sign.getLine(0).equalsIgnoreCase("DragonsKeep")) {
    11. if (gameFull() == false) {
    12. addPlayerLobby(p);
    13. sign.setLine(1, "[Open]");
    14. sign.update();
    15. }else if(gameFull() == true){
    16. sign.setLine(1, "[Full]");
    17. p.sendMessage( ChatColor.DARK_RED + "Full");
    18. sign.update();
    19.  
    20.  
    21. }
    22. }
    23. }
    24. }
    25. }
    26. }


    So when you create a sign with the first and second line saying:
    • Line 1: DragonsKeep
    • Line 2: [Open]
    Then it will add the player to the game using the method addPlayerLobby(Player) (Use a command or anything you feel like).

    Code:java
    1. @EventHandler
    2. public void onPlayerDeath(PlayerDeathEvent event){
    3. Player q = event.getEntity();
    4. Player p = q.getPlayer();
    5. Player killer = q.getKiller();
    6. if(red.contains(p)){
    7. --redtickets;
    8. }
    9. if(blue.contains(p)){
    10. --bluetickets;
    11. }
    12.  
    13. if(redtickets == 40){
    14. if(red.contains(p)){
    15. event.setDeathMessage( p.getName() + ChatColor.DARK_RED + "" + ChatColor.GRAY + " was killed by" + ChatColor.DARK_RED + killer.getName() + "!");
    16. event.setDeathMessage( ChatColor.GRAY + "Red team has 40 tickets remaining!");
    17. }else if(redtickets == 30){
    18. event.setDeathMessage( p.getName() + ChatColor.DARK_RED + "" + ChatColor.GRAY + " was killed by" + ChatColor.DARK_RED + killer.getName() + "!");
    19. event.setDeathMessage( ChatColor.GRAY + "Red team has 30 tickets remaining!");
    20. }else if(redtickets == 20){
    21. event.setDeathMessage( p.getName() + ChatColor.DARK_RED + "" + ChatColor.GRAY + " was killed by" + ChatColor.DARK_RED + killer.getName() + "!");
    22. event.setDeathMessage( ChatColor.GRAY + "Red team has 20 tickets remaining!");
    23. }else if(redtickets == 10){
    24. event.setDeathMessage( p.getName() + ChatColor.DARK_RED + "" + ChatColor.GRAY + " was killed by" + ChatColor.DARK_RED + killer.getName() + "!");
    25. event.setDeathMessage( ChatColor.GRAY + "Red team has 10 tickets remaining!");
    26. }else if(redtickets == 0){
    27. event.setDeathMessage( p.getName() + ChatColor.DARK_RED + "" + ChatColor.GRAY + " was killed by" + ChatColor.DARK_RED + killer.getName() + "!");
    28. event.setDeathMessage( ChatColor.GRAY + "Red was wipped out! Blue team is the winners!");
    29. plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable(){
    30. public void run(){
    31. resetArea();
    32. }
    33. }, 100L);
    34.  
    35. }
    36. }
    37. if(bluetickets == 40){
    38. if(blue.contains(p)){
    39. event.setDeathMessage( p.getName() + ChatColor.DARK_RED + "" + ChatColor.GRAY + " was killed by" + ChatColor.DARK_RED + killer.getName() + "!");
    40. event.setDeathMessage( ChatColor.GRAY + "Blue team has 40 tickets remaining!");
    41. }else if(bluetickets == 30){
    42. event.setDeathMessage( p.getName() + ChatColor.DARK_RED + "" + ChatColor.GRAY + " was killed by" + ChatColor.DARK_RED + killer.getName() + "!");
    43. event.setDeathMessage( ChatColor.GRAY + "Blue team has 30 tickets remaining!");
    44. }else if(bluetickets == 20){
    45. event.setDeathMessage( p.getName() + ChatColor.DARK_RED + "" + ChatColor.GRAY + " was killed by" + ChatColor.DARK_RED + killer.getName() + "!");
    46. event.setDeathMessage( ChatColor.GRAY + "Blue team has 20 tickets remaining!");
    47. }else if(bluetickets == 10){
    48. event.setDeathMessage( p.getName() + ChatColor.DARK_RED + "" + ChatColor.GRAY + " was killed by" + ChatColor.DARK_RED + killer.getName() + "!");
    49. event.setDeathMessage( ChatColor.GRAY + "Blue team has 10 tickets remaining!");
    50. }else if(bluetickets == 0){
    51. event.setDeathMessage( p.getName() + ChatColor.DARK_RED + "" + ChatColor.GRAY + " was killed by" + ChatColor.DARK_RED + killer.getName() + "!");
    52. event.setDeathMessage( ChatColor.GRAY + "Blue was wipped out! Blue team is the winners!");
    53. plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable(){
    54. public void run(){
    55. resetArea();
    56. }
    57. }, 100L);
    58. }
    59. }
    60. }


    This is a little bonus it is the tickets system. So when a player dies it subtracts a ticket from the teamticket and when it reaches 0 we use the method resetArena();

    Code:java
    1. @EventHandler
    2. public void onPlayerRespawn(PlayerRespawnEvent event){
    3. Player p = event.getPlayer();
    4. if(players.contains(p)){
    5. if(red.contains(p)){
    6. World w = p.getWorld();
    7. double x = 0;
    8. double y = 64;
    9. double z = 0;
    10. Location redteamspawn = new Location(w, x, y, z);
    11. p.teleport(redteamspawn);
    12. }else if(blue.contains(p)){
    13. World w = p.getWorld();
    14. double x = 0;
    15. double y = 64;
    16. double z = 0;
    17. Location blueteamspawn = new Location(w, x, y, z);
    18. p.teleport(blueteamspawn);
    19. }
    20.  
    21. }
    22.  
    23. }


    Now when a player dies we want to teleport them to their spawn point. Now for the last part lets disable friendly fire without scoreboards.

    Code:java
    1. @EventHandler
    2. public void onEntityDamage(EntityDamageByEntityEvent event){
    3. Entity entity = event.getEntity();
    4. Entity damager = event.getDamager();
    5. if(entity instanceof Player && damager instanceof Player){
    6. Player player = (Player) entity;
    7. Player playerhunter = (Player) damager;
    8. if(red.contains(player) && red.contains(playerhunter));
    9. event.setCancelled(true);
    10. if(blue.contains(player) && blue.contains(playerhunter)){
    11. event.setCancelled(true);
    12. }
    13. }
    14. }


    And that pretty much sums up my bad tutorial. If you want me to show how to make classes (Like Archers, etc)and maybe even a system to get points with kills then leave a comment and I will edit the tutorial to add more functional/ awesome features! Any questions please ask in the comments since I don't really check my pms.
     
  2. Okay I only read a little, but one thing I noticed is you use
    Code:text
    1. if (players.size() <= 16) { }
    I'd make that a variable maxPlayers so people understand it better :)
    Anyway, this looks pretty good!
     
  3. Offline

    SuperOmegaCow

    tips48 just did that, thanks for the input.
     
  4. Sure mate, and I just read a little more. For the red ticket/blue ticket thing.
    1) Once again, magic number. Magic numbers are bad ;)
    2) To check if it's a multiple of 10,
    Code:text
    1. if (tickets % 10 == 0) { tickets is divisible by 10 }
     
  5. Offline

    SuperOmegaCow

    tips48 too lazy to change that I will let them just look at your comment!
     
  6. SuperOmegaCow
    Not so good tutorial, nor noob friendlyish.

    Few things I'd like to note:
    1. I don't see a "redtickets" nor "bluetickets" integer variable being created at any point. That might confuse some people.
    2. What is the "players" arraylist for? Right now it looks like a player counter, which you could achieve with blue.size() + red.size(). Also the maxPlayers integer is always 16, therefore the game will start no matter how many players are on teams.
    2_2. You're checking if players.size() is less or equal to 16. This will allow a player to join the lobby even if the game is full/has 16 players.
    3. String p = player + "'; - that will return CraftPlayer{name=playername} or something like that. Use player.getName() instead.
    4. Afaik, you can't have more than one setDeathMessage() in the same statement, one will override another. Use "/n" to split the line.
    5. Lots of syntax errors, and errors in general. In the end, your code is going to spit errors like no tomorrow.
     
  7. The whole respawn system is a little screwed up too. What if (0, 64, 0) is not air? If your not going to make it configurable where they spawn, you should at least use world.getHighestBlockYAt(x, z)
     
  8. Offline

    Ivan

    I would do this differently, using switch statements and enums for gamestates.
     
Thread Status:
Not open for further replies.

Share This Page