Style cleanup

This commit is contained in:
themode 2021-03-26 21:26:35 +01:00
parent d41402c2e7
commit 55626738e2
12 changed files with 95 additions and 60 deletions

View File

@ -2,8 +2,6 @@ package net.minestom.server;
import net.minestom.server.advancements.AdvancementManager;
import net.minestom.server.adventure.bossbar.BossBarManager;
import net.minestom.server.adventure.AdventureSerializer;
import net.minestom.server.adventure.audience.Audiences;
import net.minestom.server.benchmark.BenchmarkManager;
import net.minestom.server.command.CommandManager;
import net.minestom.server.data.DataManager;

View File

@ -9,12 +9,14 @@ import java.util.function.UnaryOperator;
/**
* Represents an object that holds some amount of components.
*
* @param <T> the holding class
*/
public interface ComponentHolder<T> {
/**
* Gets the components held by this object.
*
* @return the components
*/
@NotNull Collection<Component> components();
@ -22,6 +24,7 @@ public interface ComponentHolder<T> {
/**
* Returns a copy of this object. For each component this object holds, the operator
* is applied to the copy before returning.
*
* @param operator the operator
* @return the copy
*/
@ -29,6 +32,7 @@ public interface ComponentHolder<T> {
/**
* Visits each component held by this object.
*
* @param visitor the visitor
*/
default void visitComponents(@NotNull Consumer<Component> visitor) {

View File

@ -9,7 +9,8 @@ import org.jetbrains.annotations.NotNull;
import java.util.function.Predicate;
/**
* A generic provider of {@link Audience}s or some subtype.
* A generic provider of {@link Audience audiences} or some subtype.
*
* @param <A> the type that is provided
*/
public interface AudienceProvider<A> {
@ -18,18 +19,21 @@ public interface AudienceProvider<A> {
* Gets all audience members. This returns {@link #players()} combined with
* {@link #customs()} and {@link #console()}. This can be a costly operation, so it
* is often preferable to use {@link #server()} instead.
*
* @return all audience members
*/
@NotNull A all();
/**
* Gets all audience members that are of type {@link Player}.
*
* @return all players
*/
@NotNull A players();
/**
* Gets all audience members that are of type {@link Player} and match the predicate.
*
* @param filter the predicate
* @return all players matching the predicate
*/
@ -37,18 +41,21 @@ public interface AudienceProvider<A> {
/**
* Gets the console as an audience.
*
* @return the console
*/
@NotNull A console();
/**
* Gets the combination of {@link #players()} and {@link #console()}.
*
* @return the audience of all players and the console
*/
@NotNull A server();
/**
* Gets all custom audience members stored using the given keyed object.
*
* @param keyed the keyed object
* @return all custom audience members stored using the key of the object
*/
@ -58,6 +65,7 @@ public interface AudienceProvider<A> {
/**
* Gets all custom audience members stored using the given key.
*
* @param key the key
* @return all custom audience members stored using the key
*/
@ -66,7 +74,8 @@ public interface AudienceProvider<A> {
/**
* Gets all custom audience members stored using the given keyed object that match
* the given predicate.
* @param keyed the keyed object
*
* @param keyed the keyed object
* @param filter the predicate
* @return all custom audience members stored using the key
*/
@ -77,7 +86,8 @@ public interface AudienceProvider<A> {
/**
* Gets all custom audience members stored using the given key that match the
* given predicate.
* @param key the key
*
* @param key the key
* @param filter the predicate
* @return all custom audience members stored using the key
*/
@ -85,12 +95,14 @@ public interface AudienceProvider<A> {
/**
* Gets all custom audience members.
*
* @return all custom audience members
*/
@NotNull A customs();
/**
* Gets all custom audience members matching the given predicate.
*
* @param filter the predicate
* @return all matching custom audience members
*/
@ -98,6 +110,7 @@ public interface AudienceProvider<A> {
/**
* Gets all audience members that match the given predicate.
*
* @param filter the predicate
* @return all matching audience members
*/
@ -105,6 +118,7 @@ public interface AudienceProvider<A> {
/**
* Gets the audience registry used to register custom audiences.
*
* @return the registry
*/
@NotNull AudienceRegistry registry();

View File

@ -18,12 +18,14 @@ import java.util.stream.Collectors;
* Holder of custom audiences.
*/
public class AudienceRegistry {
private final Map<Key, Collection<Audience>> registry;
private final Function<Key, Collection<Audience>> provider;
/**
* Creates a new audience registrar with a given backing map.
* @param backingMap the backing map
*
* @param backingMap the backing map
* @param backingCollection a provider for the backing collection
*/
public AudienceRegistry(@NotNull Map<Key, Collection<Audience>> backingMap, @NotNull Supplier<Collection<Audience>> backingCollection) {
@ -33,6 +35,7 @@ public class AudienceRegistry {
/**
* Checks if this registry is empty.
*
* @return {@code true} if it is, {@code false} otherwise
*/
public boolean isEmpty() {
@ -41,7 +44,8 @@ public class AudienceRegistry {
/**
* Adds some audiences to the registry.
* @param keyed the provider of the key
*
* @param keyed the provider of the key
* @param audiences the audiences
*/
public void register(@NotNull Keyed keyed, @NotNull Audience... audiences) {
@ -50,7 +54,8 @@ public class AudienceRegistry {
/**
* Adds some audiences to the registry.
* @param keyed the provider of the key
*
* @param keyed the provider of the key
* @param audiences the audiences
*/
public void register(@NotNull Keyed keyed, @NotNull Collection<Audience> audiences) {
@ -59,7 +64,8 @@ public class AudienceRegistry {
/**
* Adds some audiences to the registry.
* @param key the key to store the audiences under
*
* @param key the key to store the audiences under
* @param audiences the audiences
*/
public void register(@NotNull Key key, @NotNull Audience... audiences) {
@ -72,7 +78,8 @@ public class AudienceRegistry {
/**
* Adds some audiences to the registry.
* @param key the key to store the audiences under
*
* @param key the key to store the audiences under
* @param audiences the audiences
*/
public void register(@NotNull Key key, @NotNull Collection<Audience> audiences) {
@ -83,6 +90,7 @@ public class AudienceRegistry {
/**
* Gets every audience in the registry.
*
* @return an iterable containing every audience member
*/
public @NotNull Iterable<? extends Audience> all() {
@ -95,6 +103,7 @@ public class AudienceRegistry {
/**
* Gets every audience in the registry under a specific key.
*
* @param keyed the key provider
* @return an iterable containing the audience members
*/
@ -104,6 +113,7 @@ public class AudienceRegistry {
/**
* Gets every audience in the registry under a specific key.
*
* @param key the key
* @return an iterable containing the audience members
*/
@ -113,6 +123,7 @@ public class AudienceRegistry {
/**
* Gets every audience member in the registry who matches a given predicate.
*
* @param filter the predicate
* @return the matching audience members
*/

View File

@ -18,6 +18,7 @@ public class Audiences {
/**
* Gets the {@link AudienceProvider} that provides forwarding audiences.
*
* @return the instance
*/
public static @NotNull AudienceProvider<Audience> single() {
@ -26,6 +27,7 @@ public class Audiences {
/**
* Gets the {@link AudienceProvider} that provides iterables of audience members.
*
* @return the instance
*/
public static @NotNull AudienceProvider<Iterable<? extends Audience>> iterable() {
@ -36,6 +38,7 @@ public class Audiences {
* Gets all audience members. This returns {@link #players()} combined with
* {@link #customs()} and {@link #console()}. This can be a costly operation, so it
* is often preferable to use {@link #server()} instead.
*
* @return all audience members
*/
public static @NotNull Audience all() {
@ -44,6 +47,7 @@ public class Audiences {
/**
* Gets all audience members that are of type {@link Player}.
*
* @return all players
*/
public static @NotNull Audience players() {
@ -52,6 +56,7 @@ public class Audiences {
/**
* Gets all audience members that are of type {@link Player} and match the predicate.
*
* @param filter the predicate
* @return all players matching the predicate
*/
@ -61,6 +66,7 @@ public class Audiences {
/**
* Gets the console as an audience.
*
* @return the console
*/
public static @NotNull Audience console() {
@ -69,6 +75,7 @@ public class Audiences {
/**
* Gets the combination of {@link #players()} and {@link #console()}.
*
* @return the audience of all players and the console
*/
public static @NotNull Audience server() {
@ -77,6 +84,7 @@ public class Audiences {
/**
* Gets all custom audience members.
*
* @return all custom audience members
*/
public static @NotNull Audience customs() {
@ -85,6 +93,7 @@ public class Audiences {
/**
* Gets all custom audience members stored using the given keyed object.
*
* @param keyed the keyed object
* @return all custom audience members stored using the key of the object
*/
@ -94,6 +103,7 @@ public class Audiences {
/**
* Gets all custom audience members stored using the given key.
*
* @param key the key
* @return all custom audience members stored using the key
*/
@ -104,7 +114,8 @@ public class Audiences {
/**
* Gets all custom audience members stored using the given keyed object that match
* the given predicate.
* @param keyed the keyed object
*
* @param keyed the keyed object
* @param filter the predicate
* @return all custom audience members stored using the key
*/
@ -115,7 +126,8 @@ public class Audiences {
/**
* Gets all custom audience members stored using the given key that match the
* given predicate.
* @param key the key
*
* @param key the key
* @param filter the predicate
* @return all custom audience members stored using the key
*/
@ -125,6 +137,7 @@ public class Audiences {
/**
* Gets all custom audience members matching the given predicate.
*
* @param filter the predicate
* @return all matching custom audience members
*/
@ -134,6 +147,7 @@ public class Audiences {
/**
* Gets all audience members that match the given predicate.
*
* @param filter the predicate
* @return all matching audience members
*/
@ -143,6 +157,7 @@ public class Audiences {
/**
* Gets the audience registry used to register custom audiences.
*
* @return the registry
*/
public static @NotNull AudienceRegistry registry() {

View File

@ -20,12 +20,11 @@ import java.util.stream.StreamSupport;
* A provider of iterable audiences.
*/
class IterableAudienceProvider implements AudienceProvider<Iterable<? extends Audience>> {
private final Collection<ConsoleSender> console;
private final AudienceRegistry registry;
IterableAudienceProvider() {
this.console = Collections.singleton(MinecraftServer.getCommandManager().getConsoleSender());
this.registry = new AudienceRegistry(new ConcurrentHashMap<>(), CopyOnWriteArrayList::new);
private final Collection<ConsoleSender> console = Collections.singleton(MinecraftServer.getCommandManager().getConsoleSender());
private final AudienceRegistry registry = new AudienceRegistry(new ConcurrentHashMap<>(), CopyOnWriteArrayList::new);
protected IterableAudienceProvider() {
}
@Override

View File

@ -1,6 +1,5 @@
package net.minestom.server.adventure.audience;
import com.google.common.collect.ImmutableList;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.audience.ForwardingAudience;
import net.kyori.adventure.audience.MessageType;
@ -30,6 +29,7 @@ public interface PacketGroupingAudience extends ForwardingAudience {
* Creates a packet grouping audience that copies an iterable of players. The
* underlying collection is not copied, so changes to the collection will be
* reflected in the audience.
*
* @param players the players
* @return the audience
*/
@ -39,6 +39,7 @@ public interface PacketGroupingAudience extends ForwardingAudience {
/**
* Gets an iterable of the players this audience contains.
*
* @return the connections
*/
@NotNull Collection<Player> getPlayers();

View File

@ -14,17 +14,17 @@ import java.util.stream.Collectors;
* {@link IterableAudienceProvider}.
*/
class SingleAudienceProvider implements AudienceProvider<Audience> {
final IterableAudienceProvider collection;
final Audience players, server;
SingleAudienceProvider() {
this.collection = new IterableAudienceProvider();
this.players = PacketGroupingAudience.of(MinecraftServer.getConnectionManager().getOnlinePlayers());
this.server = Audience.audience(this.players, MinecraftServer.getCommandManager().getConsoleSender());
protected final IterableAudienceProvider collection = new IterableAudienceProvider();
protected final Audience players = PacketGroupingAudience.of(MinecraftServer.getConnectionManager().getOnlinePlayers());
protected final Audience server = Audience.audience(this.players, MinecraftServer.getCommandManager().getConsoleSender());
protected SingleAudienceProvider() {
}
/**
* Gets the {@link IterableAudienceProvider} instance.
*
* @return the instance
*/
public @NotNull IterableAudienceProvider iterable() {

View File

@ -21,20 +21,20 @@ import static net.minestom.server.network.packet.server.play.BossBarPacket.Actio
* use {@link BossBarManager} to manage boss bars for players.
*/
final class BossBarHolder implements Viewable {
final UUID uuid;
final BossBar bar;
final Set<Player> players;
boolean registered;
protected final UUID uuid = UUID.randomUUID();
protected final Set<Player> players = new CopyOnWriteArraySet<>();
protected final BossBar bar;
protected boolean registered;
BossBarHolder(@NotNull BossBar bar) {
this.uuid = UUID.randomUUID();
this.bar = bar;
this.players = new CopyOnWriteArraySet<>();
this.registered = false;
}
@NotNull BossBarPacket createRemovePacket() {
return this.createGenericPacket(REMOVE, packet -> {});
return this.createGenericPacket(REMOVE, packet -> {
});
}
@NotNull BossBarPacket createAddPacket() {

View File

@ -20,6 +20,7 @@ class BossBarListener implements BossBar.Listener {
/**
* Creates a new boss bar listener.
*
* @param manager the manager instance
*/
BossBarListener(BossBarManager manager) {

View File

@ -1,20 +1,14 @@
package net.minestom.server.adventure.bossbar;
import com.google.common.collect.MapMaker;
import net.kyori.adventure.audience.Audience;
import net.kyori.adventure.bossbar.BossBar;
import net.kyori.adventure.bossbar.BossBar.Color;
import net.kyori.adventure.text.Component;
import net.minestom.server.MinecraftServer;
import net.minestom.server.entity.Player;
import net.minestom.server.event.player.PlayerDisconnectEvent;
import net.minestom.server.network.packet.server.play.BossBarPacket;
import net.minestom.server.utils.PacketUtils;
import org.jetbrains.annotations.NotNull;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;
/**
* Manages all boss bars known to this Minestom instance. Although this class can be used
@ -29,17 +23,16 @@ import java.util.concurrent.CopyOnWriteArraySet;
* @see Audience#hideBossBar(BossBar)
*/
public class BossBarManager {
private final BossBarListener listener;
private final Map<UUID, Set<BossBarHolder>> playerBars;
final Map<BossBar, BossBarHolder> bars;
private final BossBarListener listener = new BossBarListener(this);
private final Map<UUID, Set<BossBarHolder>> playerBars = new ConcurrentHashMap<>();
final Map<BossBar, BossBarHolder> bars = new ConcurrentHashMap<>();
/**
* Creates a new boss bar manager.
*
* @see MinecraftServer#getBossBarManager()
*/
public BossBarManager() {
this.listener = new BossBarListener(this);
this.playerBars = new ConcurrentHashMap<>();
this.bars = new ConcurrentHashMap<>();
}
/**
@ -47,7 +40,7 @@ public class BossBarManager {
* boss bar if needed.
*
* @param player the intended viewer
* @param bar the boss bar to show
* @param bar the boss bar to show
*/
public void addBossBar(@NotNull Player player, @NotNull BossBar bar) {
BossBarHolder holder = this.getOrCreateHandler(bar);
@ -62,7 +55,7 @@ public class BossBarManager {
* Removes the specified player from the boss bar's viewers and despawns the boss bar.
*
* @param player the intended viewer
* @param bar the boss bar to hide
* @param bar the boss bar to hide
*/
public void removeBossBar(@NotNull Player player, @NotNull BossBar bar) {
BossBarHolder holder = this.getOrCreateHandler(bar);
@ -78,7 +71,7 @@ public class BossBarManager {
* boss bar if needed.
*
* @param players the players
* @param bar the boss bar
* @param bar the boss bar
*/
public void addBossBar(@NotNull Collection<Player> players, @NotNull BossBar bar) {
BossBarHolder holder = this.getOrCreateHandler(bar);
@ -100,7 +93,7 @@ public class BossBarManager {
* Removes the specified players from the boss bar's viewers and despawns the boss bar.
*
* @param players the intended viewers
* @param bar the boss bar to hide
* @param bar the boss bar to hide
*/
public void removeBossBar(@NotNull Collection<Player> players, @NotNull BossBar bar) {
BossBarHolder holder = this.getOrCreateHandler(bar);
@ -156,7 +149,6 @@ public class BossBarManager {
* Gets or creates a handler for this bar.
*
* @param bar the bar
*
* @return the handler
*/
private @NotNull BossBarHolder getOrCreateHandler(@NotNull BossBar bar) {

View File

@ -59,9 +59,9 @@ import java.util.function.UnaryOperator;
*/
public class Entity implements Viewable, EventHandler, DataContainer, PermissionHandler, HoverEventSource<ShowEntity> {
private static final Map<Integer, Entity> entityById = new ConcurrentHashMap<>();
private static final Map<UUID, Entity> entityByUuid = new ConcurrentHashMap<>();
private static final AtomicInteger lastEntityId = new AtomicInteger();
private static final Map<Integer, Entity> ENTITY_BY_ID = new ConcurrentHashMap<>();
private static final Map<UUID, Entity> ENTITY_BY_UUID = new ConcurrentHashMap<>();
private static final AtomicInteger LAST_ENTITY_ID = new AtomicInteger();
protected Instance instance;
protected final Position position;
@ -138,8 +138,8 @@ public class Entity implements Viewable, EventHandler, DataContainer, Permission
setAutoViewable(true);
Entity.entityById.put(id, this);
Entity.entityByUuid.put(uuid, this);
Entity.ENTITY_BY_ID.put(id, this);
Entity.ENTITY_BY_UUID.put(uuid, this);
}
public Entity(@NotNull EntityType entityType) {
@ -180,7 +180,7 @@ public class Entity implements Viewable, EventHandler, DataContainer, Permission
*/
@Nullable
public static Entity getEntity(int id) {
return Entity.entityById.getOrDefault(id, null);
return Entity.ENTITY_BY_ID.getOrDefault(id, null);
}
/**
@ -191,7 +191,7 @@ public class Entity implements Viewable, EventHandler, DataContainer, Permission
*/
@Nullable
public static Entity getEntity(@NotNull UUID uuid) {
return Entity.entityByUuid.getOrDefault(uuid, null);
return Entity.ENTITY_BY_UUID.getOrDefault(uuid, null);
}
@ -203,7 +203,7 @@ public class Entity implements Viewable, EventHandler, DataContainer, Permission
* @return a newly generated entity id
*/
public static int generateId() {
return lastEntityId.incrementAndGet();
return LAST_ENTITY_ID.incrementAndGet();
}
/**
@ -762,8 +762,8 @@ public class Entity implements Viewable, EventHandler, DataContainer, Permission
*/
public void setUuid(@NotNull UUID uuid) {
// Refresh internal map
Entity.entityByUuid.remove(this.uuid);
Entity.entityByUuid.put(uuid, this);
Entity.ENTITY_BY_UUID.remove(this.uuid);
Entity.ENTITY_BY_UUID.put(uuid, this);
this.uuid = uuid;
}
@ -1440,8 +1440,8 @@ public class Entity implements Viewable, EventHandler, DataContainer, Permission
public void remove() {
this.removed = true;
this.shouldRemove = true;
Entity.entityById.remove(id);
Entity.entityByUuid.remove(uuid);
Entity.ENTITY_BY_ID.remove(id);
Entity.ENTITY_BY_UUID.remove(uuid);
if (instance != null)
instance.UNSAFE_removeEntity(this);
}