Solved Using morphia - Cached data?

Discussion in 'Spigot Plugin Development' started by Maxx_Qc, Feb 7, 2020.

Thread Status:
Not open for further replies.
  1. Hi guys, I've started using morphia for the first time and I was just wondering if I need to use cached data or does morphia does that already? I'm not quite sure how it is actually working but I'm trying to figure it out :p

    Thank you :D
     
  2. You can use redis with mongo for cache data, since MongoDB does not cache
     
  3. Alright, I'll go read about redis

    I have another question regarding morphia though.
    So when a player connects I get his user from the mongodb and update his lastip, lastname, etc., but do I need to do save() each time I change something? I guess so but I just wanna make sure

    EDIT: found this tutorial but I'm more confused about Redis than I used to be
     
  4. I always make a profile class , so u dont have to save everytime

    Code (Text):

    public class Profile {

        private UUID uuid;
        private String name;

        private boolean connected;
     
    So when the player join you get the document from mongodb and u create a new profile with your variables, you can use PlayerPreLoginEvent

    For better optimization , and then you change the variables depending on the actions, but when the player leaves the server PlayerQuitEvent
    You have to get the profile variables again to save them in the player's document in the database
     
  5. Yeah that's what I was doing before using morphia. So I still need a collection to store all profiles, right?
     
  6. No , just use this

    Profile
    Code (Text):

    public class Profile {

        private UUID uuid;

        private boolean health;

        public Profile(UUID uuid) {
            this.uuid = uuid;
        }

        public void setHealth(boolean health) {
            this.health = health;
        }
    }
     
    ProfileManager
    Code (Text):

    public class ProfileManager {

        private LinkedHashSet<Profile> profiles;

        public ProfileManager() {
            this.profiles = new LinkedHashSet<>();
        }

        public LinkedHashSet<Profile> getProfiles() {
            return profiles;
        }

        public void createProfile(final Player player) {
            final Profile profile = new Profile(player.getUniqueId());
            profile.setHealth(//mongodb_get_player_health);

            getProfiles().add(profile);
        }

        public void unregisterProfile(final Player player) {
            final Profile profile = getProfiles().stream().filter(profile1 -> profile1.getUuid() == player.getUniqueId()).findFirst().orElse(null);

            if (profile == null)
                return;


            //set_mongodb_player_health
            //save

           getProfiles().remove(profile);
        }
    }
     
    Events
    Code (Text):

    public class Main extends JavaPlugin {

        private ProfileManager profileManager;

        @Override
        public void onEnable() {
            this.profileManager = new ProfileManager();
        }

        @EventHandler
        public void onPreLogin(final AsyncPlayerPreLoginEvent event) {
            this.profileManager.createProfile(event.getUniqueId());
        }

        @EventHandler
        public void onQuit(final PlayerQuitEvent event) {
            this.profileManager.unregisterProfile(event.getPlayer());
        }
    }
     
     
    • Like Like x 1
Thread Status:
Not open for further replies.