Solved Making A Vote System - For Mini Games

Discussion in 'Plugin Development' started by thomasjcf21, Feb 2, 2014.

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

    thomasjcf21

    Hello Guys,

    I am trying to setup a custom coded mini games plugin for my server and I have just got to the part where I am making the voting part. I am getting the vote menu to pop up. However I am trying to make it so that when a user selects a vote icon from the inventory it is added to the hash map. After 30 seconds which ever hash map has the most amount of people voted for then gets played. How do I do that? Here is my code.

    Code:java
    1. package general;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.HashMap;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.block.Action;
    12. import org.bukkit.event.player.PlayerInteractEvent;
    13. import org.bukkit.inventory.Inventory;
    14. import org.bukkit.inventory.ItemStack;
    15. import org.bukkit.inventory.meta.ItemMeta;
    16.  
    17.  
    18. public class voteSystem implements Listener {
    19.  
    20. static HashMap<String, Integer> PrisonBreak = new HashMap<String, Integer>();
    21. static HashMap<String, Integer> TestPlugin = new HashMap<String, Integer>();
    22. @EventHandler
    23. public static void VoteItemInteract(PlayerInteractEvent event){
    24. Player ply = event.getPlayer();
    25.  
    26. if(event.getAction() == Action.RIGHT_CLICK_AIR && event.getPlayer().getItemInHand().getType() == globalData.VoteIcon.getType() || event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getPlayer().getItemInHand().getType() == globalData.VoteIcon.getType() ){
    27.  
    28. Inventory inv = Bukkit.createInventory(ply, 27,ChatColor.GOLD+"["+ChatColor.GOLD+"Vote"+ChatColor.GOLD+"]"+ChatColor.DARK_GRAY+" Gamemode");
    29. inv.clear();
    30. //inv.addItem(globalData.VoteIcon);
    31. inv.addItem(globalData.PrisonBreakIcon);
    32.  
    33. ply.openInventory(inv);
    34. event.setCancelled(true);
    35. ply.closeInventory();
    36. }
    37. if(event.getAction() == Action.RIGHT_CLICK_AIR && event.getPlayer().getItemInHand().getType() == globalData.PrisonBreakIcon.getType() || event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getPlayer().getItemInHand().getType() == globalData.PrisonBreakIcon.getType() ){
    38. PrisonBreak.put(event.getPlayer().getName(), 1);
    39. event.getPlayer().sendMessage("You have voted for Prison Break :D");
    40. }
    41. if(event.getAction() == Action.RIGHT_CLICK_AIR && event.getPlayer().getItemInHand().getType() == globalData.testicon.getType() || event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getPlayer().getItemInHand().getType() == globalData.testicon.getType() ){
    42. TestPlugin.put(event.getPlayer().getName(), 1);
    43. event.getPlayer().sendMessage("You have voted for some stupid test plugin :(");
    44. }
    45. }
    46. public static int PrisonBreakCounter(HashMap<String, ArrayList<Integer>> PrisonBreak){
    47. int PrisonBreakResults = 0;
    48. for(ArrayList<Integer> list: PrisonBreak.values()){
    49. PrisonBreakResults += list.size();
    50. }
    51. return PrisonBreakResults;
    52. }
    53. public static int TestPluginCounter(HashMap<String, ArrayList<Integer>> TestPlugin){
    54. int TestPluginResults = 0;
    55. for(ArrayList<Integer> list: TestPlugin.values()){
    56. TestPluginResults += list.size();
    57. }
    58. return TestPluginResults;
    59. }
    60.  
    61. public class MapChosen{
    62. public String Results;{
    63. if(voteSystem.PrisonBreakCounter(null) <= voteSystem.TestPluginCounter(null)){
    64. String mapChosen = "'prisonbreak'";
    65. }
    66. else{
    67. String mapChosen = "'testplugin'";
    68. }
    69. }
    70.  
    71. }
    72. }


    Thanks, Thomas
     
  2. Offline

    EliteLX

    This is not how you help somebody, at all.

    Learn to be constructive in your responses. Offering somebody a "wow you suck at coding here's a very general idea that would pull up things extremely advanced and probably off topic that show a general idea at what i'm talking about from a website not even related to what he's doing" won't help somebody that's new and struggling.

    Yeesch.
     
    playajames and nate_alex like this.
  3. Offline

    xTrollxDudex

    He was implying for OP to learn java.
     
    Caprei likes this.
  4. Offline

    Captain Dory

    I get that feeling so much xD
     
    bennie3211 likes this.
  5. Offline

    shawnGreene()

    The first issue I see here is that you're not showing us what some of these instances are of what I assume are supposed to be extended ItemStack objects. Honestly I think you should put your trust into using commands such as /vote as it creates far less issues and way easier to manage.
     
  6. Offline

    nate_alex

    Try looking up some bukkit tutorial(s). I think you should learn basic java before you do something more complex like this :p

    Plus I think if this is really one of your first plugins then I highly suggest a command based voter, or even a pre-made plugin.

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

    thomasjcf21

    nate_alex

    How would you make a command based voter?
     
  8. Offline

    Ad237

    thomasjcf21

    I don't understand why you are storing the votes in a HashMap with the player name and an integer. You should be storing the name of what you are voting for and the number of votes. Then to get the highest votes you loop through the values and compare them. However before doing this you should probably learn some Java.
     
  9. Offline

    thomasjcf21

    Ad237

    Hi,

    I am not that nooby at Java.

    However please can you give an example?

    Thanks, Thomas
     
  10. Offline

    Ad237

    thomasjcf21 I have already told you what you need to change. Also not to sound rude but you are "nooby at java".
     
  11. Offline

    thomasjcf21

    Ad237

    OK, I am kinda nooby but this isn't the first plugin I have coded. I was just looking for an example of how you would do it. I would like to expand my Java Knowledge.

    Thanks, Thomas
     
  12. Offline

    Ad237

    thomasjcf21 I'm not writing the full code out but this is what I would do.
    Code:
    public HashMap<String, Integer> votes = new HashMap<String, Integer>();
    Add the maps:
    Code:
    votes.put("Map1", 0);
    votes.put("Map2", 0);
    When a player votes:
    Code:
    String map = "Map1"; //The map they are voting for.
    votes.put(map, votes.get(map) + 1);
     
  13. Offline

    thomasjcf21

    Ad237

    Thanks, So Much that was what i was asking for :D

    Ad237

    Sorry to have to bother you again,

    Except when I do this, it always says 1. Even if two users select it. Why?

    Code:java
    1. if(event.getAction() == Action.RIGHT_CLICK_AIR && event.getPlayer().getItemInHand().getType() == globalData.VoteIcon.getType() || event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getPlayer().getItemInHand().getType() == globalData.VoteIcon.getType() ){
    2. String map = "prisonbreak";
    3. votes.put(map, votes.get(map) + 1);
    4. event.getPlayer().sendMessage("You have voted for Prison Break :D");
    5. event.getPlayer().sendMessage(votes.get(map) + " have voted for this map!");
    6. }


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

    Ad237

  15. Offline

    thomasjcf21

    Ad237

    Oh yhea sorry :/

    Code:java
    1. package general;
    2.  
    3. import java.util.ArrayList;
    4. import java.util.HashMap;
    5.  
    6. import org.bukkit.Bukkit;
    7. import org.bukkit.ChatColor;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11. import org.bukkit.event.block.Action;
    12. import org.bukkit.event.player.PlayerInteractEvent;
    13. import org.bukkit.inventory.Inventory;
    14. //import org.bukkit.inventory.ItemStack;
    15. //import org.bukkit.inventory.meta.ItemMeta;
    16.  
    17.  
    18. public class voteSystem implements Listener {
    19.  
    20. public static HashMap<String, Integer> votes = new HashMap<String, Integer>();
    21. @EventHandler
    22. public static void VoteItemInteract(PlayerInteractEvent event){
    23. Player ply = event.getPlayer();
    24. votes.put("prisonbreak", 0);
    25. votes.put("testplugin", 0);
    26. if(event.getAction() == Action.RIGHT_CLICK_AIR && event.getPlayer().getItemInHand().getType() == globalData.VoteIcon.getType() || event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getPlayer().getItemInHand().getType() == globalData.VoteIcon.getType() ){
    27.  
    28. Inventory inv = Bukkit.createInventory(ply, 27,ChatColor.GOLD+"["+ChatColor.GOLD+"Vote"+ChatColor.GOLD+"]"+ChatColor.DARK_GRAY+" Gamemode");
    29. inv.clear();
    30. //inv.addItem(globalData.VoteIcon);
    31. inv.addItem(globalData.PrisonBreakIcon);
    32. inv.addItem(globalData.testicon);
    33.  
    34. ply.openInventory(inv);
    35. event.setCancelled(true);
    36. }
    37. if(event.getAction() == Action.RIGHT_CLICK_AIR && event.getPlayer().getItemInHand().getType() == globalData.VoteIcon.getType() || event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getPlayer().getItemInHand().getType() == globalData.VoteIcon.getType() ){
    38. //String map = "prisonbreak";
    39. votes.put("prisonbreak", votes.get("prisonbreak")+1);
    40. event.getPlayer().sendMessage("You have voted for Prison Break :D");
    41. event.getPlayer().sendMessage(votes.get("prisonbreak") + " have voted for this map!");
    42. }
    43. if(event.getAction() == Action.RIGHT_CLICK_AIR && event.getPlayer().getItemInHand().getType() == globalData.testicon.getType() || event.getAction() == Action.RIGHT_CLICK_BLOCK && event.getPlayer().getItemInHand().getType() == globalData.testicon.getType() ){
    44. String map = "testplugin";
    45. votes.put(map, votes.get(map) + 1);
    46. event.getPlayer().sendMessage("You have voted for some stupid test plugin :(");
    47. event.getPlayer().sendMessage(votes.get(map) + " have voted for this map!");
    48. }
    49. }
    50. }
     
  16. Offline

    nate_alex

    So you were asking about a command based one. What you could do is when the player executes /vote it shows them a list of the maps to vote for. Then it says a number next to the map so they would do /vote <number>. Im not exactly sure of how you would do this, but that is sorta the basics of this method
     
  17. Offline

    thomasjcf21

    Ad237

    Yhea I would like to make a GUI one first, Just checking to see if Ad237 has seen this?

    Thanks Thomas

    Nevermind I have fixed it :D, It was resetting every time it was reopened xD

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

    vRiiP

    Hi, can you release this plugin? It sounds awesome!
     
Thread Status:
Not open for further replies.

Share This Page