Solved Getting class problem

Discussion in 'Plugin Development' started by mAndAle, Jul 1, 2020.

  1. Offline

    mAndAle

    Here my code in the command class:

    Code:
    public class MCommands implements CommandExecutor{
       
        private int randomcode;
        private DiscordEvent dv = new DiscordEvent();
        private List<UUID> verified = new ArrayList<UUID>;
    
        @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            String prefix = ChatColor.translateAlternateColorCodes('&', Main.plugin.getConfig().getString("General-Prefix"));
            if (!(sender instanceof Player)) {
                sender.sendMessage(prefix + "Only players can do this command!");
                return true;
            }
            Player p = (Player) sender;
            if (args.length != 0) {
                p.sendMessage(prefix + ChatColor.RED + "Wrong usage: '/verify'");
                return true;
            }
           
            if (dv.getCodeMap().containsKey(p.getUniqueId())) {
                p.sendMessage(prefix + ChatColor.RED + "Sorry, but you have already generated a code!");
                return true;
            }
            this.randomcode = new Random().nextInt(800000)+200000;
            p.sendMessage(prefix + ChatColor.GREEN + "Your code is:"+" "+this.randomcode);
            p.sendMessage(prefix + ChatColor.GREEN + "Please put in our discord: !verify" + " " + this.randomcode);
            setRandomcode(this.randomcode);
            dv.getCodeMap().put(p.getUniqueId(), this.randomcode);
            dv.getUuidMap().put(this.randomcode, p.getUniqueId());
            return true;
        }
    
        /**
         * @return the verified
         */
        public List<UUID> getVerified() {
            return verified;
        }
    
        /**
         * @param verified the verified to set
         */
        public void setVerified(List<UUID> verified) {
            this.verified = verified;
        }
    
        /**
         * @return the randomcode
         */
        public int getRandomcode() {
            return randomcode;
        }
    
        /**
         * @param randomcode the randomcode to set
         */
        public void setRandomcode(int randomcode) {
            this.randomcode = randomcode;
        }
    
    }
    and the class where i call it:


    Code:
    public class DiscordEvent extends ListenerAdapter{
       
        private MCommands cmd = new MCommands();
        private HashMap<Integer, UUID> uuidMap = new HashMap<Integer, UUID>();
        private HashMap<UUID, Integer> codeMap = new HashMap<UUID, Integer>();
        private final long channelid = 726420397209419796L;
       
        @Override
        public void onGuildMessageReceived(GuildMessageReceivedEvent event) {
            if(event.getAuthor().isBot()||event.getAuthor().isFake()||event.isWebhookMessage())return;
            if (event.getChannel() != event.getGuild().getGuildChannelById(channelid)) return;
           
            String[] args = event.getMessage().getContentRaw().split(" ");
            if(args[0].equalsIgnoreCase("!verify")){
                if(event.getMember().getRoles().stream().filter(role -> role.getName().equals("Verified")).findAny().orElse(null) != null){
                    event.getChannel().sendMessage(":x: **|** Error! "+event.getAuthor().getAsMention()+", you are already verified!").queue();
                    return;
                }
               
                if (args.length != 2) {
                    event.getChannel().sendMessage(event.getAuthor().getAsMention() + "Wrong usage, use: '!verify <code>").queue();
                    return;
                }
               
                UUID puuid = uuidMap.get(cmd.getRandomcode());
               
                if (codeMap.containsKey(puuid)) {
                    event.getChannel().sendMessage(event.getAuthor().getAsMention() + "funziona fratelli").queue();
                } else {
                    event.getChannel().sendMessage(event.getAuthor().getAsMention() + ", you ensered an invalid verification code").queue();
                }
            }   
        }
    
        /**
         * @return the uuidMap
         */
        public HashMap<Integer, UUID> getUuidMap() {
            return uuidMap;
        }
    
        /**
         * @param uuidMap the uuidMap to set
         */
        public void setUuidMap(HashMap<Integer, UUID> uuidMap) {
            this.uuidMap = uuidMap;
        }
    
        /**
         * @return the codeMap
         */
        public HashMap<UUID, Integer> getCodeMap() {
            return codeMap;
        }
    
        /**
         * @param codeMap the codeMap to set
         */
        public void setCodeMap(HashMap<UUID, Integer> codeMap) {
            this.codeMap = codeMap;
        }
    }

    the error:

    Code:
    java.lang.StackOverflowError: null
        at org.mAndAle.Tokens.discord.commands.MCommands.<init>(MCommands.java:19) ~[?:?]
        at org.mAndAle.Tokens.discord.DiscordEvent.<init>(DiscordEvent.java:13) ~[?:?]
        at org.mAndAle.Tokens.discord.commands.MCommands.<init>(MCommands.java:19) ~[?:?]
        at org.mAndAle.Tokens.discord.DiscordEvent.<init>(DiscordEvent.java:13) ~[?:?]
        at org.mAndAle.Tokens.discord.commands.MCommands.<init>(MCommands.java:19) ~[?:?]
        at org.mAndAle.Tokens.discord.DiscordEvent.<init>(DiscordEvent.java:13) ~[?:?]
        at org.mAndAle.Tokens.discord.commands.MCommands.<init>(MCommands.java:19) ~[?:?]
        at org.mAndAle.Tokens.discord.DiscordEvent.<init>(DiscordEvent.java:13) ~[?:?]
        at org.mAndAle.Tokens.discord.commands.MCommands.<init>(MCommands.java:19) ~[?:?]
        at org.mAndAle.Tokens.discord.DiscordEvent.<init>(DiscordEvent.java:13) ~[?:?]
        at org.mAndAle.Tokens.discord.commands.MCommands.<init>(MCommands.java:19) ~[?:?]
        at org.mAndAle.Tokens.discord.DiscordEvent.<init>(DiscordEvent.java:13) ~[?:?]
        at org.mAndAle.Tokens.discord.commands.MCommands.<init>(MCommands.java:19) ~[?:?]
        at org.mAndAle.Tokens.discord.DiscordEvent.<init>(DiscordEvent.java:13) ~[?:?]
        at org.mAndAle.Tokens.discord.commands.MCommands.<init>(MCommands.java:19) ~[?:?]
        at org.mAndAle.Tokens.discord.DiscordEvent.<init>(DiscordEvent.java:13) ~[?:?]
        at org.mAndAle.Tokens.discord.commands.MCommands.<init>(MCommands.java:19) ~[?:?]
        at org.mAndAle.Tokens.discord.DiscordEvent.<init>(DiscordEvent.java:13) ~[?:?]
        at org.mAndAle.Tokens.discord.commands.MCommands.<init>(MCommands.java:19) ~[?:?]
        at org.mAndAle.Tokens.discord.DiscordEvent.<init>(DiscordEvent.java:13) ~[?:?]
        at org.mAndAle.Tokens.discord.commands.MCommands.<init>
    Can anyone help me?
    I've been trying for 2 days but I can't, so I'm turning to you
     
  2. Offline

    KarimAKL

    @mAndAle It's a StackOverflow error, you're in an endless loop.
    A new instance of DiscordEvent creates a new instance of MCommands which then creates a new instance of DiscordEvent, etc.

    Code:Java
    1. //MCommands class, line 19
    2. private DiscordEvent dv = new DiscordEvent();
    3.  
    4. // DiscordEvent class, line 13
    5. private MCommands cmd = new MCommands();
     
  3. Offline

    mAndAle

    The it's only private class bot;?
     
  4. Offline

    timtower Moderator Moderator

Share This Page