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

platform-bukkit: fix ViaVersion softDepend occurring too late #80

Merged
merged 3 commits into from
Feb 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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