Unable to put instance of object into HashMap

Discussion in 'Plugin Development' started by kacpicygan123, Jan 6, 2022.

  1. Offline

    kacpicygan123

    Hello. I've been testing my code below, however it doesn't work. The problem indicates that plugin
    Code:
     Couldn't pass event PlayerJoinEvent() to Main.java 
    Problem vanishes when I delete
    Code:
     playerStats.put(player.getUniqueId(), stats); 
    Is there anything wrong with my Strength instance? I would be glad for any kind of help. I know I have many unnecessary imports and empty class extending Base class isn't necessary. Everything was for the sake of testing.


    Main.java:
    Code:
    package osl.First;
    import java.util.HashMap;
    import java.util.UUID;
    import org.bukkit.ChatColor;
    import org.bukkit.entity.Player;
    import org.bukkit.plugin.java.JavaPlugin;
    import commands.NewCommand;
    import events.TutorialEvents;
    import stats.Statistics;
    import stats.Strength;
    public class Main extends JavaPlugin {
        private static HashMap<UUID, Strength> playerStats = new HashMap<UUID, Strength>();
       
        @Override
        public void onEnable() {
            NewCommand cmd = new NewCommand();
            getServer().getPluginManager().registerEvents(new TutorialEvents(), this);
            getServer().getConsoleSender().sendMessage(ChatColor.RED + "PLUGIN IS ENABLEEEEEEEEEEED.");
        }
       
        @Override
        public void onDisable() {
            System.out.println(ChatColor.GREEN + "Plugin disabled.");
        }
       
        public static HashMap<UUID, Strength> getMap() {
            return playerStats;
        }
    }
    
    Strength.java
    Code:
    package stats;
    public class Strength extends Base {
       
    }
    
    Base.java:
    Code:
    package stats;
    public class Base {
        protected int amount;
        public void setAmount(int newAmount) {
            amount = newAmount;
        }
        public int getAmount() {
            return amount;
        }
    }
    
    TutorialEvents.java
    Code:
    package events;
    import java.util.HashMap;
    import java.util.List;
    import java.util.UUID;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Zombie;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDeathEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import org.bukkit.event.player.PlayerMoveEvent;
    import org.bukkit.inventory.ItemStack;
    import osl.First.Main;
    import stats.Statistics;
    import stats.Strength;
    public class TutorialEvents implements Listener {
        @EventHandler
        public static void onPlayerJoin(PlayerJoinEvent event) {
            Strength stats = new Strength();
            HashMap<UUID, Strength> playerStats = Main.getMap();
            Player player = event.getPlayer();
            if (!(playerStats.containsKey(event.getPlayer().getUniqueId()))) {
                playerStats.put(player.getUniqueId(), stats);
                player.sendMessage("You've been put.");
            }
            else
            {
                player.sendMessage("You're already in hashmap ");
            }
        }
    
     
  2. Offline

    rudraksha007

    i guess this is because the class is just empty?
    u need to make base and Strength class a constructor like this:

    public class Base{
    public Base(int amount){
    // i believe u know how to do this and i m posting this structure for fun... :|
    }
    }
    also... i m not sure if this would work... so try it out and please post the whole whole error log...
     
  3. Offline

    timtower Administrator Administrator Moderator

    @rudraksha007 Adding a constructor will only change that you initialize the value right away, now it is just undefined.
     
  4. Offline

    Strahan

    Post the full stack trace.
     
  5. Offline

    kacpicygan123

    The solution was simple, I should initialize amount variable. So you can do it as
    Code:
    protected int amount = 0;
    
     

Share This Page