Solved Figured it out

Discussion in 'Spigot Plugin Development' started by savfsfcwdvcfs, Jul 28, 2018.

Thread Status:
Not open for further replies.
  1. nvm
     
    #1 savfsfcwdvcfs, Jul 28, 2018
    Last edited: Jul 28, 2018
  2. ScarabCoder

    ScarabCoder Retired Resource Staff
    Retired

    Why not use the getters you made? i.e getReporter() if it's a Player?
     
  3. NathanWolf

    Supporter

    You can't access properties of an object without instantiating it. They literally don't exist. You may want to use statics, but if I'm being honest I think you may need to go back and read up on some OOP and Java fundamentals before you go much further here. Good luck!
     
    • Agree Agree x 2
  4. If you're asking for how to keep used instances, make a cache. That could be for example a Collection in your main class or in another appropriate place.
     
  5. FrostedSnowman

    Resource Staff

    where do you even draw the conclusion that he's asking for that? usually it's unclear on thread questions because OP is foreign or something related, but this time it's evident what he is trying to do.

    really rethink your approach. this is YOUR plugin, why try to hack away at your own code? if you aren't able to easily access something from one of your own objects then you're doing something wrong.
     
  6. So, if I initiate the object in another class lets say on a command. But I want to use variables from that object in an event class, how would I do that?
     
  7.  
  8. FrostedSnowman

    Resource Staff

     
    • Funny Funny x 2
  9. But I do initialize the object somewhere. I would just like to get the players in the object without having to initialize it again bc I don't have the parameters for the object. Example I already have a Report report = new Report(params). And now I'm just trying to figure out how to get the player in the Report object bc if I do report.getPlayer I have to make a new instance of report and I can't make one bc I don't have the parameters.
     
  10. List or Map ;)

    Liskov Substitution Principle, use the abstract types over the implementation types when it comes to defining fields, variables, parameters, etc.
     
    • Agree Agree x 1
    • Creative Creative x 1
  11. It sounds like what you're saying is that you have the report instantiated in one place, but you want to access that instance in another place that doesn't have access to the instance.

    If that's the case, what I'd suggest is storing a static list of instances somewhere (i.e. public static List<Report> reports) in a class that can be accessed elsewhere (perhaps the main plugin class if you like). Then, in any case that you'd like to access this list, you can do 'MainClass.reports'

    If you need to be able to fetch a specific report by player, then you could either iterate over the list and check for the player, or instead make the List a Map<Player, Report>, then 'MainClass.reports.get(player)' to get the report.

    Hope that helps. And by all means, if this isn't a good idea, do ammend my suggestion. I've heard static design can be a bad idea, but it seems in this case that it may be ideal.

    As for the question at face value, if you want to get a private member in a class, use a public getter. It shouldn't be more complicated than that if this is your own code.
     
    #12 OffLuffy, Jul 28, 2018
    Last edited: Jul 28, 2018
  12. @FrostedSnowman Tbh, I think @Sataniel just has a better understanding of what's being said than you. And he's obviously right in drawing a such conclusion. OP seems to be kinda new to OOP, and doesn't really understand how to ask the question correctly, so @Sataniel here just pointed him in the right direction.

    Bro, he obviously knows how to use getters, and there's no way to "hack" yourself to what he wants, so it's clear he wants something else.

    He's right... I too think this is what OP is trying to do: store an instance of the object and then be able to access it, OP is just a bob-- which is fine.

    It's actually not evident at all.
     
    • Like Like x 1
    • Agree Agree x 1
  13. As I said, add a cache.

    I think you can easily change this for whatever field of your Report class you need.

    Code (Text):
    private Collection<Report> reports = new ArrayList<>();

    public Report getReport(String identifier) {
        if (identifier == null)
            return null;
        for (Report report : reports)
            if (identifier.equals(report.getIdentifier())
                return report;
        return null;
    }
    Note that some data might be equal between some instances of Report. In these cases, methods returning a Collection of all these instances could be used.

    Methods like this should be put where they are easily accessible. Some people would put them statically in the same class, but in my opinion, putting them non-statically into a cache class or into the main class is cleaner.
     
  14. FrostedSnowman

    Resource Staff

    if we're going to be talking about storing object references, then for this case you'd rather use a Map with the reportee's id as the key or the report's id as key, or similar. the map would provide an O(1) lookup vs O(n). we can even make it better by implementing a custom hashcode & equals for the report objects and having them direct to the id.
     
  15. I've seen a few people make events IN their object class to get the private variables like this.variable. Is that a good way to go?
     
  16. I do not think you are talking about events. Using the "this" keyword references a class-level variable within that class you are using it in. With most IDEs, you can usually ignore the "this" keyword as it automatically appends this at compile-time (unless you got a local variable with the same name as well). But you can see with different scripting languages, for example JavaScript (has prototype OOP) which is not compiled, that you do have to manually append "this" keyword or errors will occur.

    I really suggest you look more into Java and OOP, as frankly, you have very little understanding of what you are asking, making it hard for us to understand what exactly you want to achieve.
     
  17. That's okay, but we as we are using JAVA 8 we should start using Java 8 methods:

    Code (Text):
    import java.util.HashSet;
    import java.util.Set;

    public class ReportManager {
        private Set<Report> reports = new HashSet<>();
     
        public Report getReport(String uuid){
            return reports.stream().filter(report -> report.getUUID() == uuid).findFirst().orElse(null);
        }
    }
     

     
  18. Just a big nonono. You do not want to iterate through all reports... You should use a Map with a key->value relation.
     
  19. I'm happy to learn everyday new stuff! Show me:)
     
Thread Status:
Not open for further replies.