Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add samplePlayer(Collection<ServerPing.SamplePlayer>) method for ServerPing.Builder #1465

Open
Andre601 opened this issue Dec 3, 2024 · 0 comments
Labels
type: feature New feature or request

Comments

@Andre601
Copy link
Contributor

Andre601 commented Dec 3, 2024

Requested Feature

ServerPing.Builder should receive a samplePlayer(Collection<ServerPing.SamplePlayer>) method, so that you could use Lists, Sets, etc directly instead of an array (or having to convert a list to an array).

Why is this needed?

It seems weird to have a method accept an array, only to convert it into a list internally, while not having a method allowing to provide a list directly.

With the current method available, you would do either something like this:

public void setPlayers(List<String> names) {
    ServerPing.SamplePlayer[] players = new ServerPing.SamplePlayer[names.size()];
    
    for (int i = 0; i < names.size(); i++) {
        players[i] = new ServerPing.SamplePlayer(names.get(i), UUID.randomUUID());
    }
    
    // builder us a ServerPing.Builder instance
    builder.clearSamplePlayers().samplePlayers(players);
}

...or alternatively this:

public void setPlayers(List<String> names) {
    List<ServerPing.SamplePlayer> players = new ArrayList<>(names.size());
    
    for (String name : names) {
        players.add(new ServerPing.SamplePlayer(name, UUID.randomUUID()));
    }
    
    // builder us a ServerPing.Builder instance
    builder.clearSamplePlayers().samplePlayers(players.toArray(new ServerPing.SamplePlayer[0]));
}

The first aproach seems like extra steps for something as simple as this while the second seems like a waste, as why would you make a list, only to need to convert to an array, to then have it be a list again in the end?

The new method would remove the need of either making and populating an array, or calling the toArray on a List only for it to be made a list again internally.

public void setPlayers(List<String> names) {
    List<ServerPing.SamplePlayer> players = new ArrayList<>(names.size());
    
    for (String name : names) {
        players.add(new ServerPing.SamplePlayer(name, UUID.randomUUID()));
    }
    
    // builder us a ServerPing.Builder instance
    builder.clearSamplePlayers().samplePlayers(players);
}

Alternative Solutions

Current solution (And easiest) is to use the toArray List aproach, giving the wasted feeling.

Additional Information

No response

@Andre601 Andre601 added the type: feature New feature or request label Dec 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant