Skip to content

Commit

Permalink
Null safety PLEASE
Browse files Browse the repository at this point in the history
  • Loading branch information
NahuLD committed Oct 19, 2023
1 parent 600b994 commit 61fd879
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public boolean playerHasQuit(@NonNull String player) {
}

private @NonNull String injectPlayerInformation(@NonNull String message, @NonNull Player player) {
final VaultHook vault = (VaultHook) this.plugin.getHookManager().getHook(HookType.Vault);
// final VaultHook vault = (VaultHook) this.plugin.getHookManager().getHook(HookType.Vault);
message = message.replace("%p", player.getName());
message = message.replace("%d", player.getDisplayName());
if (this.placeholderAPI) {
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/org/kitteh/vanish/VanishPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.bukkit.plugin.java.JavaPlugin;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.kitteh.vanish.hooks.Hook;
import org.kitteh.vanish.hooks.HookManager;
import org.kitteh.vanish.hooks.HookManager.HookType;
import org.kitteh.vanish.listeners.ListenEntity;
Expand Down Expand Up @@ -258,17 +259,17 @@ public void onEnable() {
Settings.freshStart(this);

if (this.getConfig().getBoolean("hooks.essentials", false)) {
this.hookManager.getHook(HookType.Essentials).onEnable();
this.hookManager.getHook(HookType.Essentials).ifPresent(Hook::onEnable);
}
if (this.getConfig().getBoolean("hooks.dynmap", false)) {
this.hookManager.getHook(HookType.Dynmap).onEnable();
this.hookManager.getHook(HookType.Dynmap).ifPresent(Hook::onEnable);
}
if (this.getConfig().getBoolean("hooks.discordsrv", false) && this.getServer().getPluginManager().isPluginEnabled("DiscordSRV")) {
// Shouldn't happen here, but if the load order gets broken...
this.hookManager.getHook(HookType.DiscordSRV).onEnable();
this.hookManager.getHook(HookType.DiscordSRV).ifPresent(Hook::onEnable);
}
if (this.getConfig().getBoolean("hooks.squaremap", false)) {
this.hookManager.getHook(HookType.squaremap).onEnable();
this.hookManager.getHook(HookType.squaremap).ifPresent(Hook::onEnable);
}

final VanishPlugin self = this;
Expand All @@ -295,7 +296,7 @@ public void onEnable() {
@EventHandler
public void onPluginEnable(PluginEnableEvent event) {
if (event.getPlugin().getName().equalsIgnoreCase("DiscordSRV") && this.getConfig().getBoolean("hooks.discordsrv", false)) {
this.hookManager.getHook(HookType.DiscordSRV).onEnable();
this.hookManager.getHook(HookType.DiscordSRV).ifPresent(Hook::onEnable);
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/main/java/org/kitteh/vanish/hooks/HookManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

public final class HookManager {
public enum HookType {
Expand Down Expand Up @@ -96,11 +97,11 @@ public HookManager(@NonNull VanishPlugin plugin) {
* @param hooktype desired hook type
* @return the named Hook if registered, null if no match.
*/
public @NonNull Hook getHook(@NonNull HookType hooktype) {
public @NonNull Optional<Hook> getHook(@NonNull HookType hooktype) {
if (!this.hooks.containsKey(hooktype.name())) {
this.registerHook(hooktype);
}
return this.hooks.get(hooktype.name());
return Optional.ofNullable(this.hooks.get(hooktype.name()));
}

/**
Expand Down

0 comments on commit 61fd879

Please sign in to comment.