mirror of
https://github.com/DiscordSRV/Ascension.git
synced 2024-12-26 17:18:29 +01:00
Moved ApiInstanceHolder to be a DiscordSRVApi subclass, renamed a few things
This commit is contained in:
parent
ca40da0c00
commit
ec6b15aad2
@ -1,40 +0,0 @@
|
||||
/*
|
||||
* This file is part of the DiscordSRV API, licensed under the MIT License
|
||||
* Copyright (c) 2016-2023 Austin "Scarsz" Shapiro, Henri "Vankka" Schubin and DiscordSRV contributors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package com.discordsrv.api;
|
||||
|
||||
import org.jetbrains.annotations.ApiStatus;
|
||||
|
||||
@SuppressWarnings("unused") // API, Reflection
|
||||
@ApiStatus.Internal
|
||||
final class ApiInstanceHolder {
|
||||
|
||||
static DiscordSRVApi API;
|
||||
|
||||
private ApiInstanceHolder() {}
|
||||
|
||||
private static void provide(DiscordSRVApi api) {
|
||||
ApiInstanceHolder.API = api;
|
||||
}
|
||||
|
||||
}
|
@ -56,7 +56,7 @@ public interface DiscordSRVApi {
|
||||
*/
|
||||
@Nullable
|
||||
static DiscordSRVApi get() {
|
||||
return ApiInstanceHolder.API;
|
||||
return InstanceHolder.API;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -66,7 +66,7 @@ public interface DiscordSRVApi {
|
||||
*/
|
||||
@NotNull
|
||||
static Optional<DiscordSRVApi> optional() {
|
||||
return Optional.ofNullable(ApiInstanceHolder.API);
|
||||
return Optional.ofNullable(InstanceHolder.API);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -74,7 +74,7 @@ public interface DiscordSRVApi {
|
||||
* @return true if {@link #get()} and {@link #optional()} will return the API instance
|
||||
*/
|
||||
static boolean isAvailable() {
|
||||
return ApiInstanceHolder.API != null;
|
||||
return InstanceHolder.API != null;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -295,14 +295,27 @@ public interface DiscordSRVApi {
|
||||
|
||||
interface ReloadResult {
|
||||
|
||||
ReloadResult RESTART_REQUIRED = Results.RESTART_REQUIRED;
|
||||
ReloadResult RESTART_REQUIRED = DefaultConstants.RESTART_REQUIRED;
|
||||
|
||||
String name();
|
||||
|
||||
enum Results implements ReloadResult {
|
||||
enum DefaultConstants implements ReloadResult {
|
||||
|
||||
RESTART_REQUIRED
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ApiStatus.Internal
|
||||
@SuppressWarnings("unused") // API, Reflection
|
||||
final class InstanceHolder {
|
||||
|
||||
static DiscordSRVApi API;
|
||||
|
||||
private InstanceHolder() {}
|
||||
|
||||
private static void provide(DiscordSRVApi api) {
|
||||
InstanceHolder.API = api;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -265,7 +265,7 @@ public class SendableDiscordMessageImpl implements SendableDiscordMessage {
|
||||
private Function<Matcher, Object> wrapFunction(Function<Matcher, Object> function) {
|
||||
return matcher -> {
|
||||
Object result = function.apply(matcher);
|
||||
if (result instanceof FormattedText || DiscordPlaceholders.MAPPING_STATE.get() != DiscordPlaceholders.MappingState.NORMAL) {
|
||||
if (result instanceof FormattedText || DiscordPlaceholders.FORMATTING.get() != DiscordPlaceholders.Formatting.NORMAL) {
|
||||
// Process as regular text
|
||||
return result.toString();
|
||||
} else if (result instanceof CharSequence) {
|
||||
@ -321,7 +321,7 @@ public class SendableDiscordMessageImpl implements SendableDiscordMessage {
|
||||
DiscordMessageEmbed.Builder embedBuilder = embed.toBuilder();
|
||||
|
||||
// TODO: check which parts allow formatting more thoroughly
|
||||
DiscordPlaceholders.with(DiscordPlaceholders.MappingState.PLAIN, () -> {
|
||||
DiscordPlaceholders.with(DiscordPlaceholders.Formatting.PLAIN, () -> {
|
||||
embedBuilder.setAuthor(
|
||||
cutToLength(
|
||||
placeholders.apply(embedBuilder.getAuthorName()),
|
||||
@ -380,7 +380,7 @@ public class SendableDiscordMessageImpl implements SendableDiscordMessage {
|
||||
builder.addEmbed(embedBuilder.build());
|
||||
}
|
||||
|
||||
DiscordPlaceholders.with(DiscordPlaceholders.MappingState.PLAIN, () -> {
|
||||
DiscordPlaceholders.with(DiscordPlaceholders.Formatting.PLAIN, () -> {
|
||||
builder.setWebhookUsername(placeholders.apply(builder.getWebhookUsername()));
|
||||
builder.setWebhookAvatarUrl(placeholders.apply(builder.getWebhookAvatarUrl()));
|
||||
});
|
||||
|
@ -30,16 +30,16 @@ import java.util.function.Function;
|
||||
*/
|
||||
public interface DiscordPlaceholders {
|
||||
|
||||
ThreadLocal<MappingState> MAPPING_STATE = ThreadLocal.withInitial(() -> MappingState.NORMAL);
|
||||
ThreadLocal<Formatting> FORMATTING = ThreadLocal.withInitial(() -> Formatting.NORMAL);
|
||||
|
||||
static void with(MappingState mappingState, Runnable runnable) {
|
||||
MappingState before = MAPPING_STATE.get();
|
||||
MAPPING_STATE.set(mappingState);
|
||||
static void with(Formatting formatting, Runnable runnable) {
|
||||
Formatting before = FORMATTING.get();
|
||||
FORMATTING.set(formatting);
|
||||
runnable.run();
|
||||
MAPPING_STATE.set(before);
|
||||
FORMATTING.set(before);
|
||||
}
|
||||
|
||||
enum MappingState {
|
||||
enum Formatting {
|
||||
NORMAL,
|
||||
PLAIN,
|
||||
ANSI
|
||||
|
@ -19,7 +19,6 @@
|
||||
package com.discordsrv.bukkit.config.main;
|
||||
|
||||
import com.discordsrv.common.config.main.linking.ServerRequiredLinkingConfig;
|
||||
import org.bukkit.event.EventPriority;
|
||||
import org.spongepowered.configurate.objectmapping.ConfigSerializable;
|
||||
import org.spongepowered.configurate.objectmapping.meta.Comment;
|
||||
|
||||
@ -35,7 +34,7 @@ public class BukkitRequiredLinkingConfig extends ServerRequiredLinkingConfig {
|
||||
public String event = "AsyncPlayerPreLoginEvent";
|
||||
|
||||
@Comment("The event priority to use for the kick")
|
||||
public String priority = EventPriority.NORMAL.name();
|
||||
public String priority = "NORMAL";
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -30,6 +30,7 @@ import com.discordsrv.common.channel.ChannelLockingModule;
|
||||
import com.discordsrv.common.channel.ChannelUpdaterModule;
|
||||
import com.discordsrv.common.channel.GlobalChannelLookupModule;
|
||||
import com.discordsrv.common.command.game.GameCommandModule;
|
||||
import com.discordsrv.common.command.game.command.subcommand.reload.ReloadResults;
|
||||
import com.discordsrv.common.component.ComponentFactory;
|
||||
import com.discordsrv.common.config.connection.ConnectionConfig;
|
||||
import com.discordsrv.common.config.connection.UpdateConfig;
|
||||
|
@ -152,15 +152,4 @@ public interface DiscordSRV extends DiscordSRVApi {
|
||||
CompletableFuture<Void> invokeDisable();
|
||||
CompletableFuture<List<ReloadResult>> invokeReload(Set<ReloadFlag> flags, boolean silent);
|
||||
|
||||
enum ReloadResults implements ReloadResult {
|
||||
|
||||
// Internal reasons
|
||||
SUCCESS,
|
||||
SECURITY_FAILED,
|
||||
STORAGE_CONNECTION_FAILED,
|
||||
DISCORD_CONNECTION_RELOAD_REQUIRED,
|
||||
DISCORD_CONNECTION_FAILED
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -35,7 +35,7 @@ public final class ApiInstanceUtil {
|
||||
public static void setInstance(@NotNull DiscordSRV discordSRV) {
|
||||
// Avoids illegal access
|
||||
try {
|
||||
Class<?> apiProviderClass = Class.forName("com.discordsrv.api.ApiInstanceHolder");
|
||||
Class<?> apiProviderClass = Class.forName("com.discordsrv.api.DiscordSRVApi$InstanceHolder");
|
||||
Method provideMethod = apiProviderClass.getDeclaredMethod("provide", DiscordSRVApi.class);
|
||||
provideMethod.setAccessible(true);
|
||||
provideMethod.invoke(null, discordSRV);
|
||||
|
@ -24,6 +24,7 @@ import com.discordsrv.common.command.game.abstraction.GameCommand;
|
||||
import com.discordsrv.common.command.game.abstraction.GameCommandArguments;
|
||||
import com.discordsrv.common.command.game.abstraction.GameCommandExecutor;
|
||||
import com.discordsrv.common.command.game.command.subcommand.*;
|
||||
import com.discordsrv.common.command.game.command.subcommand.reload.ReloadCommand;
|
||||
import com.discordsrv.common.command.game.sender.ICommandSender;
|
||||
import com.discordsrv.common.component.util.ComponentUtil;
|
||||
|
||||
|
@ -16,7 +16,7 @@
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.discordsrv.common.command.game.command.subcommand;
|
||||
package com.discordsrv.common.command.game.command.subcommand.reload;
|
||||
|
||||
import com.discordsrv.api.DiscordSRVApi;
|
||||
import com.discordsrv.common.DiscordSRV;
|
||||
@ -92,19 +92,19 @@ public class ReloadCommand implements GameCommandExecutor, GameCommandSuggester
|
||||
|
||||
for (DiscordSRV.ReloadResult result : results) {
|
||||
String res = result.name();
|
||||
if (res.equals(DiscordSRV.ReloadResults.SECURITY_FAILED.name())) {
|
||||
if (res.equals(ReloadResults.SECURITY_FAILED.name())) {
|
||||
sender.sendMessage(Component.text(
|
||||
"DiscordSRV is disabled due to a security check failure. "
|
||||
+ "Please check console for more details", NamedTextColor.DARK_RED));
|
||||
} else if (res.equals(DiscordSRV.ReloadResults.SUCCESS.name())) {
|
||||
} else if (res.equals(ReloadResults.SUCCESS.name())) {
|
||||
sender.sendMessage(Component.text("Reload successful", NamedTextColor.GRAY));
|
||||
} else if (res.equals(DiscordSRV.ReloadResults.RESTART_REQUIRED.name())) {
|
||||
} else if (res.equals(ReloadResults.RESTART_REQUIRED.name())) {
|
||||
sender.sendMessage(Component.text("Some changes require a server restart"));
|
||||
} else if (res.equals(DiscordSRV.ReloadResults.STORAGE_CONNECTION_FAILED.name())) {
|
||||
} else if (res.equals(ReloadResults.STORAGE_CONNECTION_FAILED.name())) {
|
||||
sender.sendMessage(Component.text("Storage connection failed, please check console for details.", NamedTextColor.RED));
|
||||
} else if (res.equals(DiscordSRV.ReloadResults.DISCORD_CONNECTION_FAILED.name())) {
|
||||
} else if (res.equals(ReloadResults.DISCORD_CONNECTION_FAILED.name())) {
|
||||
sender.sendMessage(Component.text("Discord connection failed, please check console for details.", NamedTextColor.RED));
|
||||
} else if (res.equals(DiscordSRV.ReloadResults.DISCORD_CONNECTION_RELOAD_REQUIRED.name())) {
|
||||
} else if (res.equals(ReloadResults.DISCORD_CONNECTION_RELOAD_REQUIRED.name())) {
|
||||
String command = "discordsrv reload " + DiscordSRVApi.ReloadFlag.DISCORD_CONNECTION.name().toLowerCase(Locale.ROOT) + " -confirm";
|
||||
Component child;
|
||||
if (sender instanceof IPlayer) {
|
@ -0,0 +1,11 @@
|
||||
package com.discordsrv.common.command.game.command.subcommand.reload;
|
||||
|
||||
import com.discordsrv.api.DiscordSRVApi;
|
||||
|
||||
public enum ReloadResults implements DiscordSRVApi.ReloadResult {
|
||||
SUCCESS,
|
||||
SECURITY_FAILED,
|
||||
STORAGE_CONNECTION_FAILED,
|
||||
DISCORD_CONNECTION_RELOAD_REQUIRED,
|
||||
DISCORD_CONNECTION_FAILED
|
||||
}
|
@ -237,7 +237,7 @@ public class MinecraftToDiscordChatModule extends AbstractGameMessageModule<Mine
|
||||
.addContext(context)
|
||||
.addPlaceholder("message", () -> {
|
||||
String finalMessage = channelMessagePlaceholders.toString();
|
||||
if (DiscordPlaceholders.MAPPING_STATE.get() != DiscordPlaceholders.MappingState.NORMAL) {
|
||||
if (DiscordPlaceholders.FORMATTING.get() != DiscordPlaceholders.Formatting.NORMAL) {
|
||||
return preventEveryoneMentions(everyone, finalMessage, false);
|
||||
} else {
|
||||
String formattedMessage = DiscordFormattingUtil.escapeFormatting(
|
||||
|
@ -27,6 +27,7 @@ import com.discordsrv.api.event.bus.Subscribe;
|
||||
import com.discordsrv.api.event.events.lifecycle.DiscordSRVShuttingDownEvent;
|
||||
import com.discordsrv.api.module.type.Module;
|
||||
import com.discordsrv.common.DiscordSRV;
|
||||
import com.discordsrv.common.command.game.command.subcommand.reload.ReloadResults;
|
||||
import com.discordsrv.common.debug.DebugGenerateEvent;
|
||||
import com.discordsrv.common.debug.file.TextDebugFile;
|
||||
import com.discordsrv.common.discord.connection.jda.JDAConnectionManager;
|
||||
@ -206,7 +207,7 @@ public class ModuleManager {
|
||||
}
|
||||
|
||||
if (fail) {
|
||||
reloadResults.add(DiscordSRV.ReloadResults.DISCORD_CONNECTION_RELOAD_REQUIRED);
|
||||
reloadResults.add(ReloadResults.DISCORD_CONNECTION_RELOAD_REQUIRED);
|
||||
}
|
||||
|
||||
// Check if the module needs to be enabled or disabled
|
||||
@ -229,7 +230,7 @@ public class ModuleManager {
|
||||
|
||||
List<DiscordSRVApi.ReloadResult> results = new ArrayList<>();
|
||||
|
||||
List<DiscordSRV.ReloadResult> validResults = Arrays.asList(DiscordSRVApi.ReloadResult.Results.values());
|
||||
List<DiscordSRV.ReloadResult> validResults = Arrays.asList(DiscordSRVApi.ReloadResult.DefaultConstants.values());
|
||||
for (DiscordSRVApi.ReloadResult reloadResult : reloadResults) {
|
||||
if (validResults.contains(reloadResult)) {
|
||||
results.add(reloadResult);
|
||||
|
@ -69,18 +69,18 @@ public class DiscordPlaceholdersImpl implements DiscordPlaceholders {
|
||||
String childText = ((TextNode<?>) node.getChildren().get(0)).getContent();
|
||||
|
||||
if (textStyle.getType() == TextStyle.Type.CODE_STRING) {
|
||||
DiscordPlaceholders.with(MappingState.PLAIN, () -> finalText.append("`").append(placeholders.apply(childText)).append("`"));
|
||||
DiscordPlaceholders.with(Formatting.PLAIN, () -> finalText.append("`").append(placeholders.apply(childText)).append("`"));
|
||||
} else if (textStyle.getType() == TextStyle.Type.CODE_BLOCK) {
|
||||
String language = textStyle.getExtra().get("language");
|
||||
|
||||
if (language != null && language.equals("ansi")) {
|
||||
DiscordPlaceholders.with(MappingState.ANSI, () -> finalText
|
||||
DiscordPlaceholders.with(Formatting.ANSI, () -> finalText
|
||||
.append("```ansi\n")
|
||||
.append(placeholders.apply(childText))
|
||||
.append("```")
|
||||
);
|
||||
} else {
|
||||
DiscordPlaceholders.with(MappingState.PLAIN, () -> finalText
|
||||
DiscordPlaceholders.with(Formatting.PLAIN, () -> finalText
|
||||
.append("```")
|
||||
.append(language != null ? language : "")
|
||||
.append("\n")
|
||||
|
@ -43,7 +43,7 @@ public class ComponentResultStringifier implements PlaceholderResultMapper {
|
||||
}
|
||||
if (result instanceof Component) {
|
||||
Component component = (Component) result;
|
||||
DiscordPlaceholders.MappingState mappingState = DiscordPlaceholders.MAPPING_STATE.get();
|
||||
DiscordPlaceholders.Formatting mappingState = DiscordPlaceholders.FORMATTING.get();
|
||||
switch (mappingState) {
|
||||
case ANSI: // TODO: ansi serializer (?)
|
||||
case PLAIN:
|
||||
|
Loading…
Reference in New Issue
Block a user