Skip to content

Commit

Permalink
Add ContextTagResolver#ofUnsafe
Browse files Browse the repository at this point in the history
  • Loading branch information
Almighty-Satan committed Mar 5, 2024
1 parent 26e775c commit 178a0a4
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
package io.github.almightysatan.slams.minimessage;

import io.github.almightysatan.slams.Context;
import io.github.almightysatan.slams.Placeholder;
import io.github.almightysatan.slams.PlaceholderResolver;
import net.kyori.adventure.text.minimessage.ParsingException;
import net.kyori.adventure.text.minimessage.tag.Tag;
Expand All @@ -31,8 +30,6 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

Expand Down Expand Up @@ -174,30 +171,18 @@ public boolean has(@NotNull String name) {
* @return a new {@link ContextTagResolver}
*/
static @NotNull ContextTagResolver of(@NotNull PlaceholderResolver placeholderResolver) {
Objects.requireNonNull(placeholderResolver);
return new ContextTagResolver() {

@Override
public @Nullable Tag resolve(@TagPattern @NotNull String name, @NotNull ArgumentQueue arguments, net.kyori.adventure.text.minimessage.@NotNull Context ctx, @Nullable Context context) throws ParsingException {
Placeholder placeholder = placeholderResolver.resolve(name);
return placeholder == null ? null : Tag.selfClosingInserting(ctx.deserialize(placeholder.value(context, this.argumentQueueToList(arguments))));
}

@Override
public boolean has(@NotNull String name) {
return placeholderResolver.resolve(name) != null;
}

private List<String> argumentQueueToList(ArgumentQueue argumentQueue) {
if (!argumentQueue.hasNext())
return Collections.emptyList();
return ContextTagResolverImpl.ofPlaceholderResolver(placeholderResolver, false);
}

List<String> argumentList = new ArrayList<>();
while (argumentQueue.hasNext())
argumentList.add(argumentQueue.pop().value());
return Collections.unmodifiableList(argumentList);
}
};
/**
* Creates a new {@link ContextTagResolver} from the given {@link PlaceholderResolver}. Any string returned by
* placeholders will be deserialized.
*
* @param placeholderResolver the {@link PlaceholderResolver}
* @return a new {@link ContextTagResolver}
*/
static @NotNull ContextTagResolver ofUnsafe(@NotNull PlaceholderResolver placeholderResolver) {
return ContextTagResolverImpl.ofPlaceholderResolver(placeholderResolver, true);
}

// TODO add builder
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* SLAMS - Simple Language And Message System
* Copyright (C) 2023 Almighty-Satan, LeStegii
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*/

package io.github.almightysatan.slams.minimessage;

import io.github.almightysatan.slams.Context;
import io.github.almightysatan.slams.Placeholder;
import io.github.almightysatan.slams.PlaceholderResolver;
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.ParsingException;
import net.kyori.adventure.text.minimessage.tag.Tag;
import net.kyori.adventure.text.minimessage.tag.TagPattern;
import net.kyori.adventure.text.minimessage.tag.resolver.ArgumentQueue;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

class ContextTagResolverImpl {

static @NotNull ContextTagResolver ofPlaceholderResolver(@NotNull PlaceholderResolver placeholderResolver, boolean eval) {
Objects.requireNonNull(placeholderResolver);
return new ContextTagResolver() {

@Override
public @Nullable Tag resolve(@TagPattern @NotNull String name, @NotNull ArgumentQueue arguments, net.kyori.adventure.text.minimessage.@NotNull Context ctx, @Nullable Context context) throws ParsingException {
Placeholder placeholder = placeholderResolver.resolve(name);
if (placeholder == null)
return null;
String value = placeholder.value(context, this.argumentQueueToList(arguments));
return Tag.selfClosingInserting(eval ? ctx.deserialize(value) : Component.text(value));
}

@Override
public boolean has(@NotNull String name) {
return placeholderResolver.resolve(name) != null;
}

private List<String> argumentQueueToList(ArgumentQueue argumentQueue) {
if (!argumentQueue.hasNext())
return Collections.emptyList();

List<String> argumentList = new ArrayList<>();
while (argumentQueue.hasNext())
argumentList.add(argumentQueue.pop().value());
return Collections.unmodifiableList(argumentList);
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,22 @@ public void testConditionalPlaceholder() throws IOException {
langManager.load("0", values -> values.put("test", "Hello <ifn:fail:'<abc>'>"));

TextComponent component = (TextComponent) entry.value();
assertEquals("Hello World", component.content());
assertEquals("Hello <abc>", component.content());
}

@Test
public void testConditionalContextTagResolver() throws IOException {
Slams langManager = Slams.create("0");
AdventureMessage entry = AdventureMessage.of("test", langManager, ContextTagResolver.of(
ContextTagResolver.ofUnsafe(Placeholder.conditional("ifn", () -> false)),
ContextTagResolver.of(Placeholder.constant("abc", "<def>")),
ContextTagResolver.of(Placeholder.constant("def", "World"))
));

langManager.load("0", values -> values.put("test", "Hello <ifn:fail:'<abc>'>"));

TextComponent component = (TextComponent) entry.value();
assertEquals("Hello <def>", component.content());
}

@Test
Expand Down

0 comments on commit 178a0a4

Please sign in to comment.