Strange code behaviour

Discussion in 'Plugin Development' started by kacpicygan123, Jan 13, 2022 at 12:44 PM.

  1. Offline

    kacpicygan123

    I post here because i am literally confused by what happens when my code executes. Read comments in code please.
    I have snippets of code:

    QuestManager.java
    Code:
    package quest;
    import java.util.HashMap;
    import java.util.UUID;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDeathEvent;
    import org.bukkit.event.player.PlayerJoinEvent;
    import osl.First.Main;
    import stats.Statistics;
    public class QuestManager implements Listener {
        private UUID playerID = null; //player's UUID
        private Player player = null; //player's object
        HashMap<UUID, QuestManager> playerQuests = Main.getQuests(); //get Main instance for static hashmap containing players and their quests
        private Quest1 quest1 = null; //quest1 instance within class
       
        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent event) {
            player = event.getPlayer(); //set player field to joined player's object
            if (!(playerQuests.containsKey(player.getUniqueId()))) { //check if player is in hashmap
                this.quest1 = new Quest1(player.getUniqueId(), "The beginning", "The description", "You've begun your first quest!", "Your first quest ends here. Good luck!"); //assign quest1 field if he isnt in hashmap
                playerQuests.put(player.getUniqueId(), this); //insert quest for player
                player.sendMessage("Quests have been assigned.");
                playerQuests.get(player.getUniqueId()).quest1.completeQuest(); //this code executes when player joins after enabling plugin
            }
            else
            {
                player.sendMessage("You're already in hashmap."); //this message gets displayed
                playerQuests.get(player.getUniqueId()).quest1.completeQuest(); //but for some reason this code doesn't execute
            }
        }
    }
    
    Quest1.java
    Code:
    package quest;
    import java.util.UUID;
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.EntityType;
    import org.bukkit.entity.Player;
    import org.bukkit.inventory.ItemStack;
    public class Quest1 implements IQuest {
        public UUID playerID;
        private String questName = null;
        private String questDescription = null;
        private String beginMessage = null;
        private String completeMessage = null;
        public Player player;
        private boolean isFinished = false;
        private boolean isActive = false;
       
        public Quest1(UUID id, String qName, String qDesc, String bMessage, String cMessage) {
            questName = qName;
            questDescription = qDesc;
            beginMessage = bMessage;
            completeMessage = cMessage;
            this.playerID = id;
            this.player = Bukkit.getPlayer(id);
           //some code..
        }
        public void completeQuest() {
            player.sendMessage(completeMessage);
            player.getInventory().addItem(new ItemStack(Material.STONE, 1));
        }
    
    completeQuest() is only executed when player wasn't added yet to hashmap. If he was added, it just displays message "You're already in hashmap" and doesnt execute this function. Why?
     
  2. Offline

    The_Spaceman

    Code:java
    1. HashMap<UUID, QuestManager> playerQuests = Main.getQuests(); //get Main instance for static hashmap containing players and their quests

    Shoudn't this be static?
    Code:java
    1. playerQuests.put(player.getUniqueId(), this);

    I guess you need to put quest1 in the HashMap, this way everybody shares 'this.quest1'

    Not sure if this fixes your problem, but still helps you on the right tracks.
     

Share This Page