-2

I have an interface called Dog, by default this "dog" is spawned for all players in the game i'm making, so I have extended this interface and created a new one called PerPlayerDog that basically spawn different "dogs" depending on player, for this I have added a method called spawn(Player). I also have a registry class that stores all the dogs, so I iterate over all the spawned dogs and i check if the dog is instanceof PerPlayerDog and then I spawn this dog using my own logic, basically only for players that are nearby in a X radius all this in a repeated task that runs every second to check for the players to spawn the dog for, the problem is that using instance of for more than "100+" spawned dogs and every second is expensive and not optimized, so I wanted to know a better option, also maybe it is better to rename the class to SubscribedDog?, and have methods to subscribe to this dog for a player is this correct?, tell me your ideas or how i can improve the code, thanks.

interface Dog {
    String getDogName();
    UUID getUUID();
    
    DogModel getDogModel();
}


interface PerPlayerDog extends Dog {
    void spawn(Player player);

    /** players that the dog has already been spawned for */
    Iterable<Player> getReceipts();
}
Profeta
  • 13
  • 2

1 Answers1

0

Question

It's hard to suggest to you something particular because the right solution depends on business logic a lot. Someday the current chosen solution will have to be changed when the requirements change. We can't know your environment and circumstances and your question should be more clarified and more focused.

In any case, I can offer you a few simple options:


Add the spawn method to the Dog interface

First of all, you can add the spawn method for Dog interface directly:

public interface Dog {
    String getDogName();
    UUID getUUID();
    DogModel getDogModel();
    void spawn(Player player);
}

Then you can just ignore spawn for some dogs:

public class IgnoredDog implements Dog{
    @Override
    public void spawn(final Player ignore) {}
    
    //other methods 
}

Or implement logic for another dogs :

public class PerPlayerDog implements Dog{
    @Override
    public void spawn(final Player player) {
       if(player.getDistance(getDogModel().getLocation()) < 5){
           //todo
       }
    }

    //other methods
}

Your task in this case:

@Override
public void run() {
    for (Dog dog : dogRegistry.getDogs()) {
        for (Player player : players) {
            dog.spawn(player);
        }
    }
}

Change the DogRegistry

You can change DogRegistry method List<Dog> getDogs(); - to the next: List<PerPlayerDog> getDogs();, or you can add another method List<PerPlayerDog> getPlayersDogs() to DogRegistry.

Your task in this case:

@Override
public void run() {
    for (Dog dog : dogRegistry.getPlayersDogs()) {
        for (Player player : players) {
            dog.spawn(player);
        }
    }
}

Or you can create another Registry and store PerPlayerDog list separately.

Volodya Lombrozo
  • 2,325
  • 2
  • 16
  • 34