From ef01f32989422c314d624ac4e0c4344ba9cb861c Mon Sep 17 00:00:00 2001 From: "Ash (ashisbored)" Date: Thu, 9 Sep 2021 21:07:07 +0100 Subject: [PATCH] Small amount of code cleanup --- .gitignore | 4 ++ .../playerpronouns/PlayerPronouns.java | 53 +++++++++---------- 2 files changed, 28 insertions(+), 29 deletions(-) diff --git a/.gitignore b/.gitignore index 09cd281..5edd12e 100644 --- a/.gitignore +++ b/.gitignore @@ -31,3 +31,7 @@ bin/ # fabric run/ + +# crashes + +*.hprof diff --git a/src/main/java/io/github/ashisbored/playerpronouns/PlayerPronouns.java b/src/main/java/io/github/ashisbored/playerpronouns/PlayerPronouns.java index b1d500f..1a0ec87 100644 --- a/src/main/java/io/github/ashisbored/playerpronouns/PlayerPronouns.java +++ b/src/main/java/io/github/ashisbored/playerpronouns/PlayerPronouns.java @@ -1,6 +1,7 @@ package io.github.ashisbored.playerpronouns; import eu.pb4.placeholders.PlaceholderAPI; +import eu.pb4.placeholders.PlaceholderContext; import eu.pb4.placeholders.PlaceholderResult; import io.github.ashisbored.playerpronouns.command.PronounsCommand; import io.github.ashisbored.playerpronouns.data.PronounDatabase; @@ -62,37 +63,31 @@ public class PlayerPronouns implements ModInitializer { PronounsCommand.register(dispatcher); }); - PlaceholderAPI.register(new Identifier(MOD_ID, "pronouns"), ctx -> { - if (!ctx.hasPlayer()) { - return PlaceholderResult.invalid("missing player"); - } - String defaultMessage = ctx.hasArgument() ? ctx.getArgument() : config.getDefaultPlaceholder(); - ServerPlayerEntity player = ctx.getPlayer(); - if (pronounDatabase == null) { - return PlaceholderResult.value(defaultMessage); - } - Pronouns pronouns = pronounDatabase.get(player.getUuid()); - if (pronouns == null) { - return PlaceholderResult.value(defaultMessage); - } - return PlaceholderResult.value(pronouns.formatted()); - }); + PlaceholderAPI.register(new Identifier(MOD_ID, "pronouns"), ctx -> + PlayerPronouns.fromContext(ctx, false)); - PlaceholderAPI.register(new Identifier(MOD_ID, "raw_pronouns"), ctx -> { - if (!ctx.hasPlayer()) { - return PlaceholderResult.invalid("missing player"); - } - String defaultMessage = ctx.hasArgument() ? ctx.getArgument() : config.getDefaultPlaceholder(); - ServerPlayerEntity player = ctx.getPlayer(); - if (pronounDatabase == null) { - return PlaceholderResult.value(defaultMessage); - } - Pronouns pronouns = pronounDatabase.get(player.getUuid()); - if (pronouns == null) { - return PlaceholderResult.value(defaultMessage); - } + PlaceholderAPI.register(new Identifier(MOD_ID, "raw_pronouns"), ctx -> + PlayerPronouns.fromContext(ctx, false)); + } + + private static PlaceholderResult fromContext(PlaceholderContext ctx, boolean formatted) { + if (!ctx.hasPlayer()) { + return PlaceholderResult.invalid("missing player"); + } + String defaultMessage = ctx.hasArgument() ? ctx.getArgument() : config.getDefaultPlaceholder(); + ServerPlayerEntity player = ctx.getPlayer(); + if (pronounDatabase == null) { + return PlaceholderResult.value(defaultMessage); + } + Pronouns pronouns = pronounDatabase.get(player.getUuid()); + if (pronouns == null) { + return PlaceholderResult.value(defaultMessage); + } + if (formatted) { + return PlaceholderResult.value(pronouns.formatted()); + } else { return PlaceholderResult.value(pronouns.raw()); - }); + } } public static void reloadConfig() {