Random player disguise

Discussion in 'Plugin Development' started by TheBigSuperCraft, Dec 7, 2013.

Thread Status:
Not open for further replies.
  1. Hi, I'm trying to build a minigame and i need to disguise a random player to Zombie with the DisguiseCraft API.
     
  2. Offline

    Retherz_

    make a arraylist with the player names, get a random numberl ike Random random = new Random(yourlist.size);
    then Player player = Bukkit.getPlayer(yourlist.get(random)); and then disguise that player
     
  3. It says: "The method get(int) in the type ArrayList<Player> is not applicable for the arguments (Random)" + I don't know how to make it disguise players with the random method
     
  4. Offline

    Necrodoom

  5. Necrodoom
    Code:java
    1. package me.TheBigSuperCraft.minigame;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.Location;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.inventory.ItemStack;
    7.  
    8. import pgDev.bukkit.DisguiseCraft.DisguiseCraft;
    9. import pgDev.bukkit.DisguiseCraft.api.DisguiseCraftAPI;
    10. import pgDev.bukkit.DisguiseCraft.disguise.Disguise;
    11. import pgDev.bukkit.DisguiseCraft.disguise.DisguiseType;
    12.  
    13. import java.util.ArrayList;
    14. import java.util.HashMap;
    15. import java.util.List;
    16. import java.util.Map;
    17. import java.util.Random;
    18.  
    19. public class ArenaManager{
    20.  
    21. //save where the player teleported
    22. public Map<String, Location> locs = new HashMap<String, Location>();
    23. //make a new instance of the class
    24. public static ArenaManager am = new ArenaManager();
    25. //a few other fields
    26. Map<String, ItemStack[]> inv = new HashMap<String, ItemStack[]>();
    27. Map<String, ItemStack[]> armor = new HashMap<String, ItemStack[]>();
    28. //list of arenas
    29. List<Arena> arenas = new ArrayList<Arena>();
    30. //The ArrayList
    31. ArrayList<Player> players = new ArrayList<Player>();
    32. int arenaSize = 0;
    33.  
    34. static ArenaPVP plugin;
    35. public ArenaManager(ArenaPVP arenaPVP) {
    36. plugin = arenaPVP;
    37. }
    38.  
    39. public ArenaManager(){
    40.  
    41. }
    42.  
    43. //we want to get an instance of the manager to work with it statically
    44. public static ArenaManager getManager(){
    45. return am;
    46. }
    47.  
    48. //get an Arena object from the list
    49. public Arena getArena(int i){
    50. for(Arena a : arenas){
    51. if(a.getId() == i){
    52. return a;
    53. }
    54. }
    55. return null;
    56. }
    57. //DisguiseCraft API
    58. DisguiseCraftAPI dcAPI;
    59. public void setupDisguiseCraft() {
    60. dcAPI = DisguiseCraft.getAPI();
    61. }
    62.  
    63. //add players to the arena, save their inventory
    64. public void addPlayer(Player p, int i){
    65. Arena a = getArena(i);//get the arena you want to join
    66. if(a == null){//make sure it is not null
    67. p.sendMessage("Invalid arena!");
    68. return;
    69. }
    70. //The code I have problams with it
    71. Random random = new Random(players.size());
    72. Player user = Bukkit.getPlayer(players.get(random));
    73.  
    74. a.getPlayers().add(p.getName());//add them to the arena list of players
    75. inv.put(p.getName(), p.getInventory().getContents());//save inventory
    76. armor.put(p.getName(), p.getInventory().getArmorContents());
    77.  
    78. p.getInventory().setArmorContents(null);
    79. p.getInventory().clear();
    80.  
    81. p.teleport(a.spawn);//teleport to the arena spawn
    82. dcAPI.disguisePlayer(random, DisguiseType.Zombie);
    83. }
    84.  
    85. //remove players
    86. public void removePlayer(Player p){
    87. Arena a = null;//make an arena
    88. for(Arena arena : arenas){
    89. if(arena.getPlayers().contains(p.getName())){
    90. a = arena;//if the arena has the player, the arena field would be the arena containing the player
    91. }
    92. //if none is found, the arena will be null
    93. }
    94. if(a == null || !a.getPlayers().contains(p.getName())){//make sure it is not null
    95. p.sendMessage("Invalid operation!");
    96. return;
    97. }
    98.  
    99. a.getPlayers().remove(p.getName());//remove from arena
    100.  
    101. p.getInventory().clear();
    102. p.getInventory().setArmorContents(null);
    103.  
    104. p.getInventory().setContents(inv.get(p.getName()));//restore inventory
    105. p.getInventory().setArmorContents(armor.get(p.getName()));
    106.  
    107. inv.remove(p.getName());//remove entries from hashmaps
    108. armor.remove(p.getName());
    109. p.teleport(locs.get(p.getName()));
    110. locs.remove(p.getName());
    111. dcAPI.undisguisePlayer((Player) players);
    112.  
    113. p.setFireTicks(0);
    114. }
    115.  
    116. //create arena
    117. public Arena createArena(Location l){
    118. int num = arenaSize + 1;
    119. arenaSize++;
    120.  
    121. Arena a = new Arena(l, num);
    122. arenas.add(a);
    123.  
    124. plugin.getConfig().set("Arenas." + num, serializeLoc(l));
    125. List<Integer> list = plugin.getConfig().getIntegerList("Arenas.Arenas");
    126. list.add(num);
    127. plugin.getConfig().set("Arenas.Arenas", list);
    128. plugin.saveConfig();
    129.  
    130. return a;
    131. }
    132.  
    133. public Arena reloadArena(Location l) {
    134. int num = arenaSize + 1;
    135. arenaSize++;
    136.  
    137. Arena a = new Arena(l, num);
    138. arenas.add(a);
    139.  
    140. return a;
    141. }
    142.  
    143. public void removeArena(int i) {
    144. Arena a = getArena(i);
    145. if(a == null) {
    146. return;
    147. }
    148. arenas.remove(a);
    149.  
    150. plugin.getConfig().set("Arenas." + i, null);
    151. List<Integer> list = plugin.getConfig().getIntegerList("Arenas.Arenas");
    152. list.remove(i);
    153. plugin.getConfig().set("Arenas.Arenas", list);
    154. plugin.saveConfig();
    155. }
    156.  
    157. public boolean isInGame(Player p){
    158. for(Arena a : arenas){
    159. if(a.getPlayers().contains(p.getName()))
    160. return true;
    161. }
    162. return false;
    163. }
    164.  
    165. public void loadGames(){
    166. arenaSize = 0;
    167.  
    168. if(plugin.getConfig().getIntegerList("Arenas.Arenas").isEmpty()){
    169. return;
    170. }
    171.  
    172. for(int i : plugin.getConfig().getIntegerList("Arenas.Arenas")){
    173. Arena a = reloadArena(deserializeLoc(plugin.getConfig().getString("Arenas." + i)));
    174. a.id = i;
    175. }
    176. }
    177.  
    178. public String serializeLoc(Location l){
    179. return l.getWorld().getName()+","+l.getBlockX()+","+l.getBlockY()+","+l.getBlockZ();
    180. }
    181. public Location deserializeLoc(String s){
    182. String[] st = s.split(",");
    183. return new Location(Bukkit.getWorld(st[0]), Integer.parseInt(st[1]), Integer.parseInt(st[2]), Integer.parseInt(st[3]));
    184. }
    185. }


    Necrodoom?

    Necrodoom What about the code?
     
  6. No one is going to help me?
     
  7. Offline

    TheUpdater

    how did you download the DisguiseCraft api pls post link
     
  8. Just download the server jar (It's also the API :D)

    TheUpdater
    Download link for the DisguiseCraft API (I still going to send it): http://build.yu8.me:8080/job/DisguiseCraft/40/artifact/target/DisguiseCraft.jar
    NOTE: This is the download for the 1.7.2 TEST build.
    If you want the last release version (1.6.4): http://dev.bukkit.org/media/files/742/149/DisguiseCraft.jar

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

    Garris0n

    First of all, don't save player instances, save their names. Second, you need to do random.nextInt(integer), where the integer is non-inclusive and 0 is inclusive. What you would do in this case is yourList.get(random.nextInt(yourList.size()). And, finally, don't save player instances. Seriously, don't. Save their names.
     
    L33m4n123 likes this.
  10. Offline

    TheUpdater

  11. Garris0n But what about the disguise? I don't know how...
     
Thread Status:
Not open for further replies.

Share This Page