Skip to content

Commit

Permalink
api: Nag when somebody does something naughty
Browse files Browse the repository at this point in the history
  • Loading branch information
kashike committed Mar 8, 2021
1 parent 64ac1ba commit afa9446
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* This file is part of adventure, licensed under the MIT License.
*
* Copyright (c) 2017-2021 KyoriPowered
*
* 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 net.kyori.adventure.text;

import net.kyori.adventure.util.Nag;

final class LegacyFormattingDetected extends Nag {
LegacyFormattingDetected(final Component component) {
super("Legacy formatting codes have been detected in a component - this is unsupported behaviour. Please refer to the Adventure documentation (https://docs.adventure.kyori.net) for more information. Component: " + component);
}
}
10 changes: 10 additions & 0 deletions api/src/main/java/net/kyori/adventure/text/TextComponentImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,17 @@
import java.util.Objects;
import java.util.stream.Stream;
import net.kyori.adventure.text.format.Style;
import net.kyori.adventure.util.Nag;
import net.kyori.examination.ExaminableProperty;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.checkerframework.checker.nullness.qual.Nullable;

import static java.util.Objects.requireNonNull;

final class TextComponentImpl extends AbstractComponent implements TextComponent {
private static final boolean WARN_WHEN_LEGACY_FORMATTING_DETECTED = Boolean.getBoolean(String.join(".", "net", "kyori", "adventure", "text", "warnWhenLegacyFormattingDetected"));
private static final char SECTION_CHAR = '§';

static final TextComponent EMPTY = createDirect("");
static final TextComponent NEWLINE = createDirect("\n");
static final TextComponent SPACE = createDirect(" ");
Expand All @@ -48,6 +52,12 @@ final class TextComponentImpl extends AbstractComponent implements TextComponent
TextComponentImpl(final @NonNull List<? extends ComponentLike> children, final @NonNull Style style, final @NonNull String content) {
super(children, style);
this.content = content;

if(WARN_WHEN_LEGACY_FORMATTING_DETECTED) {
if(content.indexOf(SECTION_CHAR) != -1) {
Nag.print(new LegacyFormattingDetected(this));
}
}
}

@Override
Expand Down
53 changes: 53 additions & 0 deletions api/src/main/java/net/kyori/adventure/util/Nag.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* This file is part of adventure, licensed under the MIT License.
*
* Copyright (c) 2017-2021 KyoriPowered
*
* 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 net.kyori.adventure.util;

import org.checkerframework.checker.nullness.qual.NonNull;

/**
* A nag.
*
* @since 4.7.0
*/
public abstract class Nag extends RuntimeException {
/**
* Prints a nag.
*
* @param nag the nag
* @since 4.7.0
*/
public static void print(final @NonNull Nag nag) {
nag.printStackTrace();
}

/**
* Constructs with a message.
*
* @param message the message
* @since 4.7.0
*/
protected Nag(final String message) {
super(message);
}
}

0 comments on commit afa9446

Please sign in to comment.