Skip to content

Commit

Permalink
Merge pull request #80 from TehBrian/fix/viaversion-warning
Browse files Browse the repository at this point in the history
  • Loading branch information
zml2008 authored Feb 4, 2022
2 parents 72b55ca + 98f9ed4 commit c51f500
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@ static BukkitAudiences instanceFor(final @NotNull Plugin plugin) {
BukkitAudiencesImpl(final @NotNull Plugin plugin, final @NotNull ComponentRenderer<Pointered> componentRenderer) {
super(componentRenderer);
this.plugin = requireNonNull(plugin, "plugin");
this.softDepend("ViaVersion");

final CommandSender console = this.plugin.getServer().getConsoleSender();
this.addViewer(console);
Expand Down Expand Up @@ -171,44 +170,47 @@ static final class Builder implements BukkitAudiences.Builder {

@Override
public @NotNull BukkitAudiences build() {
return INSTANCES.computeIfAbsent(this.plugin.getName(), name -> new BukkitAudiencesImpl(this.plugin, this.componentRenderer));
return INSTANCES.computeIfAbsent(this.plugin.getName(), name -> {
this.softDepend("ViaVersion");
return new BukkitAudiencesImpl(this.plugin, this.componentRenderer);
});
}
}

/**
* Add the provided plugin as a soft-depend of ourselves.
*
* <p>This removes the PluginClassLoader warning added by Spigot without
* requiring every user to add ViaVersion to their own plugin.yml.</p>
*
* <p>We do assume here that each copy of Adventure belongs to a JavaPlugin.
* If that is not true, we will silently fail to inject.</p>
*
* @param pluginName a plugin name
*/
@SuppressWarnings("unchecked")
private void softDepend(final @NotNull String pluginName) {
final PluginDescriptionFile file = this.plugin.getDescription();
if (file.getName().equals(pluginName)) return;
/**
* Add the provided plugin as a soft-depend of ourselves.
*
* <p>This removes the PluginClassLoader warning added by Spigot without
* requiring every user to add ViaVersion to their own plugin.yml.</p>
*
* <p>We do assume here that each copy of Adventure belongs to a JavaPlugin.
* If that is not true, we will silently fail to inject.</p>
*
* @param pluginName a plugin name
*/
@SuppressWarnings("unchecked")
private void softDepend(final @NotNull String pluginName) {
final PluginDescriptionFile file = this.plugin.getDescription();
if (file.getName().equals(pluginName)) return;

try {
final Field softDepend = needField(file.getClass(), "softDepend");
final List<String> dependencies = (List<String>) softDepend.get(file);
if (!dependencies.contains(pluginName)) {
final List<String> newList = ImmutableList.<String>builder().addAll(dependencies).add(pluginName).build();
softDepend.set(file, newList);
try {
final Field softDepend = needField(file.getClass(), "softDepend");
final List<String> dependencies = (List<String>) softDepend.get(file);
if (!dependencies.contains(pluginName)) {
final List<String> newList = ImmutableList.<String>builder().addAll(dependencies).add(pluginName).build();
softDepend.set(file, newList);
}
} catch (final Throwable error) {
logError(error, "Failed to inject softDepend in plugin.yml: %s %s", this.plugin, pluginName);
}
} catch (final Throwable error) {
logError(error, "Failed to inject softDepend in plugin.yml: %s %s", this.plugin, pluginName);
}

try {
final PluginManager manager = this.plugin.getServer().getPluginManager();
final Field dependencyGraphField = needField(manager.getClass(), "dependencyGraph");
final MutableGraph<String> graph = (MutableGraph<String>) dependencyGraphField.get(manager);
graph.putEdge(file.getName(), pluginName);
} catch (final Throwable error) {
// Fail silently, dependency graphs were added in 1.15, but the previous method still works
try {
final PluginManager manager = this.plugin.getServer().getPluginManager();
final Field dependencyGraphField = needField(manager.getClass(), "dependencyGraph");
final MutableGraph<String> graph = (MutableGraph<String>) dependencyGraphField.get(manager);
graph.putEdge(file.getName(), pluginName);
} catch (final Throwable error) {
// Fail silently, dependency graphs were added in 1.15, but the previous method still works
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public abstract class FacetAudienceProvider<V, A extends FacetAudience<V>>
protected final Map<V, A> viewers;
private final Map<UUID, A> players;
private final Set<A> consoles;
private final A empty;
private A empty;
private volatile boolean closed;

protected FacetAudienceProvider(final @NotNull ComponentRenderer<Pointered> componentRenderer) {
Expand All @@ -96,7 +96,6 @@ protected FacetAudienceProvider(final @NotNull ComponentRenderer<Pointered> comp
}
};
this.player = Audience.audience(this.players.values());
this.empty = this.createAudience(Collections.emptyList());
this.closed = false;
}

Expand Down Expand Up @@ -184,7 +183,14 @@ public void refreshViewer(final @NotNull V viewer) {

@Override
public @NotNull Audience player(final @NotNull UUID playerId) {
return this.players.getOrDefault(playerId, this.empty);
return this.players.getOrDefault(playerId, this.empty());
}

private @NotNull A empty() {
if (this.empty == null) {
this.empty = this.createAudience(Collections.emptyList());
}
return this.empty;
}

/**
Expand Down

0 comments on commit c51f500

Please sign in to comment.