HashMap issue in a guild plugin?

Discussion in 'Plugin Development' started by Gonmarte, Nov 13, 2015.

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

    Mrs. bwfctower

    @Gonmarte It might be beneficial to create a Guild object, with Players stored in it (UUID, name, player, whatever), and have a map of all of the guilds with the key as their name.
     
  2. Offline

    Gonmarte

    @Mrs. bwfctower rDo you want to me to post the code here? Once im doing everything by my own, it probably isnt the best way to do it :/
     
  3. Offline

    Mrs. bwfctower

    @Gonmarte No.. I'm saying you might want to create a Guild object, which stores its players, and store all of the guilds in a map with their names as the key.
     
  4. Offline

    Gonmarte

    @Mrs. bwfctower Thats why i would like to share with you the code before i make changes ok? Can i post it here for you?
     
  5. Offline

    Mrs. bwfctower

  6. Offline

    Gonmarte

    @Mrs. bwfctower How i format the intellij code? without using [code /coede
     
  7. Offline

    teej107

    Control + Alt + L
     
  8. Offline

    Mrs. bwfctower

    Gonmarte likes this.
  9. Offline

    Gonmarte

    its not working the CNT+ALT+L

    @teej107 @Mrs. bwfctower
    Code:
    package Clans.ClansSettings;
    import Clans.SettingsManager.SettingsManager;
    import org.bukkit.entity.Player;
    import java.util.HashMap;
    public class ClansManager {
    
    SettingsManager sm = new SettingsManager();
    private HashMap<String,String > clan = new HashMap<>();
    public void createClan(Player player, String[] args, int number) {
    if(!clanExists(args[number])) {
    clan.put(player.getName(), args[number]);
    }
    player.sendMessage("Nome ja existente");
    }
    
    public void deleteClan(Player player, String[] args, int number){
    
    if(clanExists(args[number])){
    if(hasClan(player)){
    // how i delete the clan?
    }
    }
    player.sendMessage("Clan n existente");
    }
    
    public void addPlayer(Player player,Player target,String[] args, int number) {
    if (clanExists(args[number])) {
    if (hasClan(player)) {
    
    if (hasClan(target)) {
    player.sendMessage("Ele ja tem um clan");
    }
    clan.put(player.getName(), args[number]);
    }
    player.sendMessage("Nao tens um clan");
    }
    }
    
    public void removePlayer(Player player,Player target,String[] args, int number) {
    if (clanExists(args[number])) {
    if (hasClan(player)) {
    
    if (getClan(player)==getClan(target)) {
    clan.remove(target.getName());
    }
    player.sendMessage("Ele ja tem um clan");
    }
    player.sendMessage("Nao tens um clan");
    }
    }
    
    public boolean hasClan(Player player) {
    
    if(clan.containsKey(player.getName())){
    return true;
    }
    
    return false;
    }
    
    
    public boolean clanExists(String clanName){
    if(clan.containsValue(clanName)){
    return true;
    }
    return false;
    }
    
    public String getClan(Player player){
    
    return clan.get(player.getName());
    }
    
    
    }
    
    @Mrs. bwfctower
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited: Nov 14, 2015
  10. Offline

    Scimiguy

    Yeah you're better off doing what Tower is suggesting, as I suggested way back at the start of this conversation.
     
  11. Offline

    Gonmarte

    @Scimiguy I made some changes. I created an array list that contains all the clans, and then im using a constructor, to create a clan, and add the name of the clan to that arraylist. The im using an hashmap to store the players name and the clan.
    Take a look at the code if you didnt understand pls.

    Code:
    package Clans.ClansSettings;
    import Clans.SettingsManager.SettingsManager;
    import org.bukkit.entity.Player;
    import java.util.ArrayList;
    import java.util.HashMap;
    public class Clans {
    String clanName;
    SettingsManager sm = new SettingsManager();
    
    private ArrayList<Clans> clans = new ArrayList<>();
    
    private HashMap<String,Clans> playersClan = new HashMap<>();
    
    public Clans(String clanname){
    clanName = clanname;
    clans.add(this);
    }
    
    public void createClan(String nameOfClan, Player player){
    Clans clan = new Clans(nameOfClan);
    playersClan.put(player.getName(), clan);}
    
    public void deleteClan(Player player,Clans clan){
    
    if(clanExists(clan)){
    if(hasClan(player)){
    clans.remove(clan);
    }
    }
    player.sendMessage("Clan n existente");
    }
    
    public void addPlayer(Player player,Player target,Clans clan) {
    if (clanExists(clan)) {
    if (hasClan(player)) {
    
    if (hasClan(target)) {
    player.sendMessage("Ele ja tem um clan");
    }
    playersClan.put(player.getName(), clan);
    }
    player.sendMessage("Nao tens um clan");
    }
    }
    
    public void removePlayer(Player player,Player target,Clans clan) {
    if (clanExists(clan)) {
    if (hasClan(player)) {
    
    if (getClan(player)==getClan(target)) {
    playersClan.remove(target.getName());
    }
    player.sendMessage("Ele nao é do teu clan");
    }
    player.sendMessage("Nao tens um clan");
    }
    player.sendMessage("Clan n existe");
    }
    
    public boolean hasClan(Player player) {
    
    if(playersClan.containsKey(player.getName())){
    return true;
    }
    
    return false;
    }
    
    public boolean clanExists(Clans clan){
    
    if(clans.contains(clan)){
    return true;
    }
    
    return false;
    }
    
    
    public Clans getClan(Player player){
    
    return playersClan.get(player.getName());
    }
    
    public String getClan(){
    return clanName;
    }
    }
    
    
    Thank you
    @Scimiguy
     
    Last edited: Nov 15, 2015
  12. Offline

    Scimiguy

    I forget what the problem is
     
  13. Offline

    Gonmarte

    @Scimiguy I created an guild object that contains all the guilds, if you could just take a look in deep to my code i would apreciate, because it might have sometihng bad
     
  14. Offline

    teej107

    It's kind of hard when
    a) I don't want to because it is unformatted
    b) It's hard to catch potential errors with bad formatting.

    Edit: Your Clans class. Does that represent an individual clan or is it like a clan manager?
     
  15. Offline

    Gonmarte

    @teej107 Its like clanManager, it represents all the clans, i dont know how to formatt in intellij, ao copy the code to here, and then i get the code and i do ctrl+ alt + L and it doesnt work...
     
  16. Offline

    teej107

    You do that IN IntelliJ. Not here
     
  17. Offline

    Gonmarte

    @teej107
    i get the code on intellij and i co contrl+alt+L and then i go here and i do the same and it doesnt paste anthing
     
  18. Offline

    Scimiguy

    @Gonmarte
    So have you actually tried your code?

    It's far easier for you to just test it than for us to spend much of our time trying to paw through it for you
     
  19. Offline

    Gonmarte

    @Scimiguy I need to create the commands first. Can i create multiple onCommands methods in different commandexecuter classes? But all of then with the same command : /clan
    Is that possible?
     
  20. Offline

    Scimiguy

    No, what possible use could you have for that?
    Just handle all of that in one CommandExecutor
     
  21. Offline

    Gonmarte

    @Scimiguy It would be much more orgranized if i could split the diferent commands in different classes... But its ok ill do it on the same.
     
  22. Offline

    Scimiguy

    Huh?
    Do you have multiple commands or just /clan?

    If you have more than one /command, you can separate them into separate command executors if you want

    If it's just /clan though, you have to do it in one
     
    Gonmarte likes this.
  23. Offline

    Gonmarte

    @Scimiguy Its only the /clan command. This makes any sense?:
    Code:
                                  //HERE
    if(args[1].equalsIgnoreCase(args[1]))[
          Clans.createClan(args[1], player);
    }
    
    I dont rly think so..
     
  24. Offline

    Scimiguy

    When will this:
    if(args[1].equalsIgnoreCase(args[1]))[

    EVER return false? Ever?

    If it's just the /clan command, do it in one onCommand
     
  25. Offline

    Gonmarte

    @Scimiguy Is this correct right?
    Code:
    if(args[0].equalsIgnoreCase("create")){
               if(args.length==2){
                        Clans.createClan(args[1] , player);
    
                         return true;
                }
        }
    
    
     
  26. Offline

    Scimiguy

    Did you try it
     
  27. Offline

    Mrs. bwfctower

    @Gonmarte Are you on a mac? If so, the formatting keyboard shortcut is cmd + option + L.
     
  28. Offline

    Gonmarte

    @Mrs. bwfctower Im on windows!

    @Scimiguy I tested it and the commands doesnt work. When i do /clan create <clanname> or whatever is the command with more then 0(first argument) arguments it will return the message /clan. If i do /clan it will run a huge error in console, but its just the command /clan the others return the message /clan not even return the messages that i have...

    StackTrace of </clan> command
    Code:
    
    
    My CommandsExeuter class:
    Code:
    package Clans.ClanCommands;
    
    import Clans.Main;
    import Clans.ClansSettings.Clans;
    import Clans.SettingsMessages.MessageManager;
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    
    public class Commands implements CommandExecutor{
    Main main;
    MessageManager mm = new MessageManager();
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    
    if(!((sender) instanceof Player)){
    
    }else{
    Player player = (Player) sender;
    Player target = Bukkit.getServer().getPlayer(args[1]); 
    
    if(cmd.getName().equalsIgnoreCase("clan")){
      if(args.length==0){
      mm.info(player , "Clans command");
    return true;
    }
    else if(args.length==1){
    
        if(args[0].equalsIgnoreCase("create")){
           if(args.length==2){
          Clans.createClan(args[1], player);
    }
    }else if(args[0].equalsIgnoreCase("delete")){
    if(args.length==2){
    Clans.deleteClan(player, Clans.getClan(player));
    }
    }else if(args[0].equalsIgnoreCase("add")){
    if(args.length==2){
    if(args[1].equalsIgnoreCase(target.getName())){
    if(target==null){
    mm.severe(player , "Player not found");
    return true;}
    else{
    Clans.addPlayer(player,target,Clans.getClan(player));
    return true;
    }
    
    }
    }
    }else if(args[0].equalsIgnoreCase("remove")){
    if(args.length==2){
    if(args[1].equalsIgnoreCase(target.getName())){
    if(target==null){
    mm.severe(player , "Player not found");
    return true;
    }else{
    Clans.removePlayer(player,target,Clans.getClan(player));
    return true;
    }
    
    }
    }
    }else if(args[0].equalsIgnoreCase("leave")){
    Clans.leaveClan(player, Clans.getClan(player));
    }else if(args[0].equalsIgnoreCase("chat")){
    if(args.length==2){
    if(args[1].equalsIgnoreCase("on")){
    if(main.chatClan.contains(player.getName())){
    mm.severe(player , "Ja tens o chat do clan ligado");
    return true;
    }else{
    main.chatClan.add(player.getName());
    mm.info(player , "Chat do clan ligado");
    return true;
    }
    }else if(args[1].equalsIgnoreCase("off")){
    if(main.chatClan.contains(player.getName())){
    main.chatClan.remove(player.getName());mm.info(player , "saiste do chat co clan!");
    return true;
    }else{
    mm.info(player , "Nao podes sair, pk nao tas no chat co clan!");
    return true;
    }
    }
    }
    }else if(args[0].equalsIgnoreCase("friendlyfire")){
    if(args.length==2){
    if(args[1].equalsIgnoreCase("on")){
    if(main.friendlyFireClan.contains(player.getName())){
    mm.severe(player, "o Friendlyfire do teu clan ta ligado"); return true;
    }
    else{
    main.friendlyFireClan.add(player.getName());
    mm.info(player, "ligaste o friendly fire");
    return true;
    }
    }else if(args[1].equalsIgnoreCase("off")){
    if(main.friendlyFireClan.contains(player.getName())){
    main.friendlyFireClan.remove(player.getName());
    mm.info(player , "desligaste o friendly fire");
    return true;
    }
    else{
    mm.severe(player , "primeiro liga o e depois e que sais!");
    return true;
    }
    }
    }
    }
    }
    
    }
    
    }
    
    return false;}
    }
    
    
    PS: in my onEnable class i have getCommand("clan").setExecuter(new Commands());
    I dont know how i format the code in inteliij... sorry
     
    Last edited: Nov 18, 2015
  29. Offline

    mcdorli

    I didn't take a part in this thread, but as I can see, the last 10-15 answers are all about the same problem, because you didn't understand, what they meant.
     
    Mrs. bwfctower likes this.
  30. Offline

    Gonmarte

    @mcdorli I did it by my own, i test it by my own, now i wanna know what i did wrong before starting to delete all my code and do what you suggested. Thats my point, if i can do it by my own i do it if i cant ill do what ever you suggest, so if i can fix this problem, a command problem not a ClanManager problem, GG, if i cant ill do what you suggested.
     
Thread Status:
Not open for further replies.

Share This Page