Solved Multiple Dungeons

Discussion in 'Plugin Development' started by _Belknap_, Apr 20, 2014.

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

    _Belknap_

    Basically, how would I create an ArrayList<String> or some sort of storage for every dungeon that is created? Here is what I have so far:
    Code:java
    1. public void checktoStartDungeon(Player player,String dungeon) {
    2. ArrayList<String> players = new ArrayList<String>();
    3. Location join = j.get(dungeon);
    4. Location start = s.get(dungeon);
    5. Location end = e.get(dungeon);
    6. int mP = mp.get(dungeon);
    7. long ltime = li.get(dungeon);
    8. long gtime = ti.get(dungeon);
    9. Iterator<String> startPlayers = players.iterator();
    10. int playersLeft = (mP - players.size());
    11. if (players.size() < (mP - 1)) {
    12. players.add(player.getName());
    13. inLobby.add(player.getName());
    14. while (startPlayers.hasNext()) {
    15. String next = startPlayers.next();
    16. @SuppressWarnings("deprecation")
    17. Player p = Bukkit.getPlayer(next);
    18. p.sendMessage(ChatColor.AQUA + p.getName() + " has joined! Dungeon needs " + playersLeft + " players to start!");
    19. }
    20. }else if (players.size() >= mP) {
    21. while (startPlayers.hasNext()) {
    22. String next = startPlayers.next();
    23. inLobby.remove(next);
    24. inDungeon.add(next);
    25.  
    26.  
    27.  
    28. }}}
     
  2. Offline

    coasterman10

    Your best bet would be to create a Dungeon object, and then move your dungeon code there. Then you can create a collection of Dungeon objects and work with that.
     
  3. Offline

    _Belknap_

    Explain it deeper to me if you can, because right now, I store everything in HashMaps by having the dungeon name as the identifier, thus easily allowing me to get the information about that dungeon. I store a dungeons's join, start, and end locations, while also storing its maximum players, lobby time and game time. Here is what one of them saved in a configuration looks like:
    HTML:
    Haven:
        joinLocation:
          X: 300
          Y: 74
          Z: 228
        startLocation:
          X: 297
          Y: 74
          Z: 227
        endLocation:
          X: 292
          Y: 74
          Z: 227
        maxPlayers: 10
        world: world
        gameTime: 300
        lobbyTime: 30
    coasterman10

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

    coasterman10

    You would create a Dungeon object with all relevant variables and methods:
    Code:java
    1. public class Dungeon {
    2. private List<String> players;
    3. private Location joinLocation;
    4. private Location startLocation;
    5. // Whatever other variables apply to each dungeon
    6.  
    7. public void checkToStart(Player p) {
    8. // Rework your existing code to work with above variables
    9. }
    10. }


    And then, you would have just one Map to hold all your dungeons:
    Code:java
    1. Map<String, Dungeon> dungeons = new HashMap<String, Dungeon>();


    It should start to make sense from here.
     
  5. Offline

    _Belknap_

    coasterman10
    Would I put the Map storing all the dungeons in my main class or this one, because getting some sort of list or map from another class never works good for me. Anyways, until you reply, I'll get going on the 1st part
    !

    Oh...... I get what you are saying! Are you trying to follow that minigame tut on here?

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

    coasterman10

    _Belknap_ I'm not entirely fond of the actual code in the minigame tutorial, however the concept is very much the same.
     
  7. Offline

    _Belknap_

    coasterman10
    Ok, so how could I add information from my main class if all the variables are private, because I have a command that starts a setup prompt, and you type in the things that you want. Or
     
  8. Offline

    coasterman10

    Setter methods. You could also just make them public. I personally use almost purely getters and setters.
     
  9. Offline

    _Belknap_

    Ok, sry about all the questions but I've never tried using this method in coding before lol
    coasterman10

    coasterman10
    Ok, so I have rewritten my code to fit storing dungeons through a class, the only thing is, I have no idea how to save all that stuff into the dungeons(String,Dungeon) HashMap. I have the setup thing that I can show you. Basically, when you type /dungeon create, it starts the setup prompt. Oh, and saveDungeons() in this code basically saves the dungeon to the config:
    Code:java
    1. @EventHandler
    2. public void duringSetup(AsyncPlayerChatEvent en) {
    3. Player p = en.getPlayer();
    4. String m = en.getMessage();
    5. String name = p.getName();
    6. int x = p.getLocation().getBlockX();
    7. int y = (p.getLocation().getBlockY() + 1);
    8. int z = p.getLocation().getBlockZ();
    9. World w = p.getWorld();
    10. Location pl = new Location(w,x,y,z);
    11. if (dSetup1.contains(name)) {
    12. temporary.put(name, m);
    13. dSetup1.remove(name);
    14. dSetup2.add(name);
    15. en.setCancelled(true);
    16. p.sendMessage("" + ChatColor.YELLOW + ChatColor.BOLD + ChatColor.STRIKETHROUGH + "====================");
    17. p.sendMessage(Step(2,"Join Location"));
    18. p.sendMessage(ChatColor.GREEN + "Stand in the location you want,");
    19. p.sendMessage(ChatColor.GREEN + "and type OK!");
    20. }else if (dSetup2.contains(name)) {
    21. if (m.equalsIgnoreCase("ok")) {
    22. String dName = temporary.get(name);
    23. manager.getDungeon(dName).setJoinLocation(pl);
    24. dSetup2.remove(name);
    25. dSetup3.add(name);
    26. en.setCancelled(true);
    27. p.sendMessage("" + ChatColor.YELLOW + ChatColor.BOLD + ChatColor.STRIKETHROUGH + "====================");
    28. p.sendMessage(Step(3,"Start Location"));
    29. p.sendMessage(ChatColor.GREEN + "Stand in the location you want,");
    30. p.sendMessage(ChatColor.GREEN + "and type OK!");
    31.  
    32. }}else if (dSetup3.contains(name)) {
    33. if (m.equalsIgnoreCase("ok")) {
    34. String dName = temporary.get(name);
    35. manager.getDungeon(dName).setStartLocation(pl);
    36. dSetup3.remove(name);
    37. dSetup4.add(name);
    38. en.setCancelled(true);
    39. p.sendMessage("" + ChatColor.YELLOW + ChatColor.BOLD + ChatColor.STRIKETHROUGH + "====================");
    40. p.sendMessage(Step(4,"End Location"));
    41. p.sendMessage(ChatColor.GREEN + "Stand in the location you want,");
    42. p.sendMessage(ChatColor.GREEN + "and type OK!");
    43.  
    44. }
    45. }else if (dSetup4.contains(name)) {
    46. if (m.equalsIgnoreCase("ok")) {
    47. String dName = temporary.get(name);
    48. manager.getDungeon(dName).setEndLocation(pl);
    49. dSetup4.remove(name);
    50. dSetup5.add(name);
    51. en.setCancelled(true);
    52. p.sendMessage("" + ChatColor.YELLOW + ChatColor.BOLD + ChatColor.STRIKETHROUGH + "====================");
    53. p.sendMessage(Step(5,"Maximum Players"));
    54. p.sendMessage(ChatColor.GREEN + "Type the maximum amount");
    55. p.sendMessage(ChatColor.GREEN + "of players for the dungeon!");
    56. }
    57.  
    58. }else if (dSetup5.contains(name)) {
    59. int amount = Integer.parseInt(m);
    60. String dName = temporary.get(name);
    61. manager.getDungeon(dName).setMaxPlayers(amount);
    62. dSetup5.remove(name);
    63. dSetup6.add(name);
    64. en.setCancelled(true);
    65. p.sendMessage("" + ChatColor.YELLOW + ChatColor.BOLD + ChatColor.STRIKETHROUGH + "====================");
    66. p.sendMessage(Step(7,"Game Time"));
    67. p.sendMessage(ChatColor.GREEN + "Type the amount of");
    68. p.sendMessage(ChatColor.GREEN + "time in seconds!");
    69. }else if (dSetup6.contains(name)) {
    70. String dName = temporary.get(name);
    71. en.setCancelled(true);
    72. long time = Long.parseLong(m);
    73. manager.getDungeon(dName).setLobbyTime(time);
    74. dSetup6.remove(name);
    75. dSetup7.add(name);
    76. p.sendMessage("" + ChatColor.YELLOW + ChatColor.BOLD + ChatColor.STRIKETHROUGH + "====================");
    77. p.sendMessage(Step(8,"Lobby Time"));
    78. p.sendMessage(ChatColor.GREEN + "Type the amount of");
    79. p.sendMessage(ChatColor.GREEN + "time in seconds!");
    80.  
    81.  
    82. }else if (dSetup7.contains(name)) {
    83. String dName = temporary.get(name);
    84. long time = Long.parseLong(m);
    85. manager.getDungeon(dName).setGameTime(time);
    86. Location joinLocation = manager.getDungeon(dName).getJoinLocation();
    87. Location startLocation = manager.getDungeon(dName).getStartLocation();;
    88. Location endLocation = manager.getDungeon(dName).getEndLocation();
    89. int jX = joinLocation.getBlockX();
    90. int jY = (joinLocation.getBlockY() + 1);
    91. int jZ = joinLocation.getBlockZ();
    92. int eX = endLocation.getBlockX();
    93. int eY = (endLocation.getBlockY() + 1);
    94. int eZ = endLocation.getBlockZ();
    95. int sX = startLocation.getBlockX();
    96. int sY = (startLocation.getBlockY() + 1);
    97. int sZ = startLocation.getBlockZ();
    98. int mP = manager.getDungeon(dName).getMaxPlayers();
    99. long ltime = manager.getDungeon(dName).getLobbyTime();
    100. long gtime = manager.getDungeon(dName).getGameTime();
    101. p.sendMessage("" + ChatColor.YELLOW + ChatColor.BOLD + ChatColor.STRIKETHROUGH + "====================");
    102. p.sendMessage(locations("Join",jX,jY,jZ));
    103. p.sendMessage(locations("Start",sX,sY,sZ));
    104. p.sendMessage(locations("End",eX,eY,eZ));
    105. p.sendMessage("" + ChatColor.WHITE + ChatColor.BOLD + "Maximum Players: " + ChatColor.GREEN + mP);
    106. p.sendMessage("" + ChatColor.WHITE + ChatColor.BOLD + "Lobby Time: " + ChatColor.GREEN + ltime);
    107. p.sendMessage("" + ChatColor.WHITE + ChatColor.BOLD + "Game Time: " + ChatColor.GREEN + gtime);
    108.  
    109. temporary.remove(name);
    110. dSetup8.remove(name);
    111. saveDungeon(dName);
    112.  
    113.  
    114. }
    115. }


    Unfortunately so I an error when trying to set the dungeon's join location, which means I will get an error for the rest of the steps:
    HTML:
    [09:36:31 ERROR]: Could not pass event AsyncPlayerChatEvent to Skyren v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:294) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-16-g46c2c64-b3044jnks]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-16-g46c2c64-b3044jnks]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:501) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-16-g46c2c64-b3044jnks]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:483) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-16-g46c2c64-b3044jnks]
            at net.minecraft.server.v1_7_R3.PlayerConnection.chat(PlayerConnection.j
    ava:879) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-16-g46c2c64-b3044jnks]
            at net.minecraft.server.v1_7_R3.PlayerConnection.a(PlayerConnection.java
    :829) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-16-g46c2c64-b3044jnks]
            at net.minecraft.server.v1_7_R3.PacketPlayInChat.a(PacketPlayInChat.java
    :28) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-16-g46c2c64-b3044jnks]
            at net.minecraft.server.v1_7_R3.PacketPlayInChat.handle(PacketPlayInChat
    .java:47) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-16-g46c2c64-b3044jnks]
            at net.minecraft.server.v1_7_R3.NetworkManager.a(NetworkManager.java:75)
    [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-16-g46c2c64-b3044jnks]
            at net.minecraft.server.v1_7_R3.NetworkManager.channelRead0(NetworkManag
    er.java:195) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-16-g46c2c64-b3044jnks]
            at net.minecraft.util.io.netty.channel.SimpleChannelInboundHandler.chann
    elRead(SimpleChannelInboundHandler.java:98) [craftbukkit.jar:git-Bukkit-1.7.2-R0
    .3-16-g46c2c64-b3044jnks]
            at net.minecraft.util.io.netty.channel.DefaultChannelHandlerContext.invo
    keChannelRead(DefaultChannelHandlerContext.java:337) [craftbukkit.jar:git-Bukkit
    -1.7.2-R0.3-16-g46c2c64-b3044jnks]
            at net.minecraft.util.io.netty.channel.DefaultChannelHandlerContext.fire
    ChannelRead(DefaultChannelHandlerContext.java:323) [craftbukkit.jar:git-Bukkit-1
    .7.2-R0.3-16-g46c2c64-b3044jnks]
            at net.minecraft.util.io.netty.handler.codec.ByteToMessageDecoder.channe
    lRead(ByteToMessageDecoder.java:173) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-16-g
    46c2c64-b3044jnks]
            at net.minecraft.util.io.netty.channel.DefaultChannelHandlerContext.invo
    keChannelRead(DefaultChannelHandlerContext.java:337) [craftbukkit.jar:git-Bukkit
    -1.7.2-R0.3-16-g46c2c64-b3044jnks]
            at net.minecraft.util.io.netty.channel.DefaultChannelHandlerContext.fire
    ChannelRead(DefaultChannelHandlerContext.java:323) [craftbukkit.jar:git-Bukkit-1
    .7.2-R0.3-16-g46c2c64-b3044jnks]
            at net.minecraft.util.io.netty.handler.codec.ByteToMessageDecoder.channe
    lRead(ByteToMessageDecoder.java:173) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-16-g
    46c2c64-b3044jnks]
            at net.minecraft.util.io.netty.channel.DefaultChannelHandlerContext.invo
    keChannelRead(DefaultChannelHandlerContext.java:337) [craftbukkit.jar:git-Bukkit
    -1.7.2-R0.3-16-g46c2c64-b3044jnks]
            at net.minecraft.util.io.netty.channel.DefaultChannelHandlerContext.fire
    ChannelRead(DefaultChannelHandlerContext.java:323) [craftbukkit.jar:git-Bukkit-1
    .7.2-R0.3-16-g46c2c64-b3044jnks]
            at net.minecraft.util.io.netty.handler.codec.MessageToMessageDecoder.cha
    nnelRead(MessageToMessageDecoder.java:103) [craftbukkit.jar:git-Bukkit-1.7.2-R0.
    3-16-g46c2c64-b3044jnks]
            at net.minecraft.util.io.netty.channel.DefaultChannelHandlerContext.invo
    keChannelRead(DefaultChannelHandlerContext.java:337) [craftbukkit.jar:git-Bukkit
    -1.7.2-R0.3-16-g46c2c64-b3044jnks]
            at net.minecraft.util.io.netty.channel.DefaultChannelHandlerContext.fire
    ChannelRead(DefaultChannelHandlerContext.java:323) [craftbukkit.jar:git-Bukkit-1
    .7.2-R0.3-16-g46c2c64-b3044jnks]
            at net.minecraft.util.io.netty.handler.timeout.ReadTimeoutHandler.channe
    lRead(ReadTimeoutHandler.java:149) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-16-g46
    c2c64-b3044jnks]
            at net.minecraft.util.io.netty.channel.DefaultChannelHandlerContext.invo
    keChannelRead(DefaultChannelHandlerContext.java:337) [craftbukkit.jar:git-Bukkit
    -1.7.2-R0.3-16-g46c2c64-b3044jnks]
            at net.minecraft.util.io.netty.channel.DefaultChannelHandlerContext.fire
    ChannelRead(DefaultChannelHandlerContext.java:323) [craftbukkit.jar:git-Bukkit-1
    .7.2-R0.3-16-g46c2c64-b3044jnks]
            at net.minecraft.util.io.netty.channel.DefaultChannelPipeline.fireChanne
    lRead(DefaultChannelPipeline.java:785) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-16
    -g46c2c64-b3044jnks]
            at net.minecraft.util.io.netty.channel.nio.AbstractNioByteChannel$NioByt
    eUnsafe.read(AbstractNioByteChannel.java:100) [craftbukkit.jar:git-Bukkit-1.7.2-
    R0.3-16-g46c2c64-b3044jnks]
            at net.minecraft.util.io.netty.channel.nio.NioEventLoop.processSelectedK
    ey(NioEventLoop.java:480) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-16-g46c2c64-b30
    44jnks]
            at net.minecraft.util.io.netty.channel.nio.NioEventLoop.processSelectedK
    eysOptimized(NioEventLoop.java:447) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-16-g4
    6c2c64-b3044jnks]
            at net.minecraft.util.io.netty.channel.nio.NioEventLoop.run(NioEventLoop
    .java:341) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-16-g46c2c64-b3044jnks]
            at net.minecraft.util.io.netty.util.concurrent.SingleThreadEventExecutor
    $2.run(SingleThreadEventExecutor.java:101) [craftbukkit.jar:git-Bukkit-1.7.2-R0.
    3-16-g46c2c64-b3044jnks]
            at java.lang.Thread.run(Unknown Source) [?:1.7.0_51]
    Caused by: java.lang.NullPointerException
            at plugins.skyren.Skyren.duringSetup(Skyren.java:1237) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0
    _51]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0
    _51]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1
    .7.0_51]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_51]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:292) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-16-g46c2c64-b3044jnks]
            ... 31 more
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  10. Offline

    coasterman10

    You should minimize the amount of code in your async player chat event. Since it is asynchronous, it may clash with the main server thread, since Bukkit is not thread safe. Check if the player is in the process of setup, and if so, cancel the event, and then run a scheduler to a synchronous method that processes a setup message.

    Additionally, you will first need to create a Dungeon object, and then pop that in the map. I would create a temporary object linked with the player, and have them set it up, then finally insert it into the String-Dungeon map. Then you can get it by name to get any of its properties.
     
  11. Offline

    _Belknap_

    coasterman10
    Ok, I know this might sound like a long haul, but could you show me an example of creating a new dungeon? Don't create a new class, but just give me the basics on how to do it. I have all the getters and setters and booleans setup already, so I'm fine their, I just don't know how to create a new dungeon with all its aspects.
     
  12. Offline

    coasterman10

    Code:java
    1. Map<String, Dungeon> dungeons;
    2.  
    3. Dungeon d = new Dungeon();
    4. // Run your getters/setters
    5.  
    6. // Adding the dungeon
    7. dungeons.put(name, d);
    8.  
    9. // Getting dungeon by name
    10. Dungeon d = dungeons.get(name);
    11.  
    12. // Changing properties of the dungeon after it's been added
    13. dungeons.get(name).someSetterMethod();
    14.  
    15. // etc etc etc

    Of course, dungeons is just a dummy variable here, replace references with your actual map.
     
  13. Offline

    _Belknap_

    How did I not see this? Well, here goes nothing!​

    coasterman10
    I'm just runnin this by you to see if there is anything I need to change:
    Code:java
    1. public void createDungeon(String dungeonName,Location join,Location start,Location end, int max,long lobby,long game, String auth) {
    2. DungeonManager d = new DungeonManager();
    3. d.setJoinLocation(join);
    4. d.setStartLocation(start);
    5. d.setEndLocation(end);
    6. d.setMaxPlayers(max);
    7. d.setLobbyTime(lobby);
    8. d.setGameTime(game);
    9. d.setAuthor(auth);
    10. dungeons.put(dungeonName, d);
    11.  
    12. }


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

    coasterman10

    _Belknap_ likes this.
  15. Offline

    _Belknap_

    coasterman10
    Ugh it turns out all my errors were just because Java doesn't allow you to access a HashMap from a seperate class in your package and instead is a jerk and throws an error! Well anyways I got everything working fine!
     
  16. _Belknap_ Java would be less of a jerk if you learned to use it properly.
     
  17. Offline

    _Belknap_

    AdamQpzm
    What do you mean use it properly?
     
  18. _Belknap_ There are specific ways on how to access fields and methods from other classes, and specific reasons why you can't just do it the way I'm assuming you tried to do it.
     
  19. Offline

    coasterman10

    It's important to understand how class structure works in Java. Class files are never supposed to be divisions of code but instead divisions of objects and then their associated code.
     
  20. Offline

    _Belknap_

    coasterman10
    Ahhh ok I get it now. Thankyou for all your help!
     
Thread Status:
Not open for further replies.

Share This Page