Skip to content

Commit

Permalink
Fix application of styles
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed Jan 19, 2024
1 parent ad332a1 commit 8d89b1a
Show file tree
Hide file tree
Showing 20 changed files with 107 additions and 80 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'fabric-loom' version '0.12-SNAPSHOT'
id 'fabric-loom' version '1.4.+'
id 'maven-publish'
id "com.modrinth.minotaur" version "2.+"
}
Expand Down
10 changes: 5 additions & 5 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ org.gradle.jvmargs=-Xmx1G

# Fabric Properties
# check these on https://fabricmc.net/use
minecraft_version=1.20.3-rc1
yarn_mappings=1.20.3-rc1+build.1
loader_version=0.14.24
minecraft_version=1.20.4
yarn_mappings=1.20.4+build.1
loader_version=0.15.0

#Fabric api
fabric_version=0.90.9+1.20.3
fabric_version=0.91.1+1.20.4

# Mod Properties
mod_version = 2.3.0+1.20.3
mod_version = 2.3.1+1.20.4
maven_group = eu.pb4
archives_base_name = placeholder-api

2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-7.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
import net.minecraft.util.Identifier;
import net.minecraft.util.math.Vec2f;
import net.minecraft.util.math.Vec3d;
import org.jetbrains.annotations.Nullable;

import javax.annotation.Nullable;
import java.util.function.Supplier;

public record PlaceholderContext(MinecraftServer server,
Expand Down
17 changes: 9 additions & 8 deletions src/main/java/eu/pb4/placeholders/api/node/parent/BoldNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,20 @@

import eu.pb4.placeholders.api.ParserContext;
import eu.pb4.placeholders.api.node.TextNode;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.text.Style;

import java.util.Arrays;

public final class BoldNode extends ParentNode {
public final class BoldNode extends SimpleStylingNode {
private static final Style TRUE = Style.EMPTY.withBold(true);
private static final Style FALSE = Style.EMPTY.withBold(false);
private final boolean value;

public BoldNode(TextNode[] nodes, boolean value) {
super(nodes);
this.value = value;
}

@Override
protected Text applyFormatting(MutableText out, ParserContext context) {
return out.setStyle(out.getStyle().withBold(this.value));
}

@Override
public ParentTextNode copyWith(TextNode[] children) {
return new BoldNode(children, this.value);
Expand All @@ -32,4 +28,9 @@ public String toString() {
", children=" + Arrays.toString(children) +
'}';
}

@Override
protected Style style(ParserContext context) {
return this.value ? TRUE : FALSE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
import eu.pb4.placeholders.api.node.TextNode;
import eu.pb4.placeholders.api.parsers.NodeParser;
import net.minecraft.text.ClickEvent;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.text.Style;

public final class ClickActionNode extends ParentNode {
public final class ClickActionNode extends SimpleStylingNode {
private final ClickEvent.Action action;
private final TextNode value;

Expand All @@ -26,8 +25,8 @@ public TextNode value() {
}

@Override
protected Text applyFormatting(MutableText out, ParserContext context) {
return out.setStyle(out.getStyle().withClickEvent(new ClickEvent(this.action, this.value.toText(context, true).getString())));
protected Style style(ParserContext context) {
return Style.EMPTY.withClickEvent(new ClickEvent(this.action, this.value.toText(context, true).getString()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

import eu.pb4.placeholders.api.ParserContext;
import eu.pb4.placeholders.api.node.TextNode;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.text.Style;
import net.minecraft.text.TextColor;

import java.util.Arrays;

public final class ColorNode extends ParentNode {
public final class ColorNode extends SimpleStylingNode {
private final TextColor color;

public ColorNode(TextNode[] children, TextColor color) {
Expand All @@ -17,8 +16,8 @@ public ColorNode(TextNode[] children, TextColor color) {
}

@Override
protected Text applyFormatting(MutableText out, ParserContext context) {
return out.setStyle(out.getStyle().withColor(this.color));
protected Style style(ParserContext context) {
return Style.EMPTY.withColor(this.color);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

import eu.pb4.placeholders.api.ParserContext;
import eu.pb4.placeholders.api.node.TextNode;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.text.Style;
import net.minecraft.util.Identifier;

import java.util.Arrays;

public final class FontNode extends ParentNode {
public final class FontNode extends SimpleStylingNode {
private final Identifier font;

public FontNode(TextNode[] children, Identifier font) {
Expand All @@ -17,8 +16,8 @@ public FontNode(TextNode[] children, Identifier font) {
}

@Override
protected Text applyFormatting(MutableText out, ParserContext context) {
return out.setStyle(out.getStyle().withFont(font));
protected Style style(ParserContext context) {
return Style.EMPTY.withFont(font);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import eu.pb4.placeholders.api.ParserContext;
import eu.pb4.placeholders.api.node.TextNode;
import net.minecraft.text.MutableText;
import net.minecraft.text.Style;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;

import java.util.Arrays;


public final class FormattingNode extends ParentNode {
public final class FormattingNode extends SimpleStylingNode {
private final Formatting[] formatting;

public FormattingNode(TextNode[] children, Formatting formatting) {
Expand All @@ -22,8 +23,8 @@ public FormattingNode(TextNode[] children, Formatting... formatting) {
}

@Override
protected Text applyFormatting(MutableText out, ParserContext context) {
return out.formatted(this.formatting);
protected Style style(ParserContext context) {
return Style.EMPTY.withFormatting(this.formatting);
}

@Override
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/eu/pb4/placeholders/api/node/parent/HoverNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
import eu.pb4.placeholders.api.parsers.NodeParser;
import net.minecraft.entity.EntityType;
import net.minecraft.text.HoverEvent;
import net.minecraft.text.MutableText;
import net.minecraft.text.Style;
import net.minecraft.text.Text;
import org.jetbrains.annotations.Nullable;

import java.util.Arrays;
import java.util.UUID;

public final class HoverNode<T, H> extends ParentNode {
public final class HoverNode<T, H> extends SimpleStylingNode {
private final Action<T, H> action;
private final T value;

Expand All @@ -23,13 +23,13 @@ public HoverNode(TextNode[] children, Action<T, H> action, T value) {
}

@Override
protected Text applyFormatting(MutableText out, ParserContext context) {
protected Style style(ParserContext context) {
if (this.action == Action.TEXT) {
return out.setStyle(out.getStyle().withHoverEvent(new HoverEvent((HoverEvent.Action<Object>) this.action.vanillaType(), ((TextNode) this.value).toText(context, true))));
return Style.EMPTY.withHoverEvent(new HoverEvent((HoverEvent.Action<Object>) this.action.vanillaType(), ((TextNode) this.value).toText(context, true)));
} else if (this.action == Action.ENTITY) {
return out.setStyle(out.getStyle().withHoverEvent(new HoverEvent((HoverEvent.Action<Object>) this.action.vanillaType(), ((EntityNodeContent) this.value).toVanilla(context))));
return Style.EMPTY.withHoverEvent(new HoverEvent((HoverEvent.Action<Object>) this.action.vanillaType(), ((EntityNodeContent) this.value).toVanilla(context)));
} else {
return out.setStyle(out.getStyle().withHoverEvent(new HoverEvent((HoverEvent.Action<Object>) this.action.vanillaType(), this.value)));
return Style.EMPTY.withHoverEvent(new HoverEvent((HoverEvent.Action<Object>) this.action.vanillaType(), this.value));
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
import eu.pb4.placeholders.api.ParserContext;
import eu.pb4.placeholders.api.node.TextNode;
import eu.pb4.placeholders.api.parsers.NodeParser;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.text.Style;

import java.util.Arrays;

public final class InsertNode extends ParentNode {
public final class InsertNode extends SimpleStylingNode {
private final TextNode value;

public InsertNode(TextNode[] children, TextNode value) {
Expand All @@ -21,8 +20,8 @@ public TextNode value() {
}

@Override
protected Text applyFormatting(MutableText out, ParserContext context) {
return out.setStyle(out.getStyle().withInsertion(value.toText(context, true).getString()));
protected Style style(ParserContext context) {
return Style.EMPTY.withInsertion(value.toText(context, true).getString());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

import eu.pb4.placeholders.api.ParserContext;
import eu.pb4.placeholders.api.node.TextNode;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.text.Style;

import java.util.Arrays;

public final class ItalicNode extends ParentNode {
public final class ItalicNode extends SimpleStylingNode {
private static final Style TRUE = Style.EMPTY.withItalic(true);
private static final Style FALSE = Style.EMPTY.withItalic(false);

private final boolean value;

public ItalicNode(TextNode[] nodes, boolean value) {
Expand All @@ -16,8 +18,8 @@ public ItalicNode(TextNode[] nodes, boolean value) {
}

@Override
protected Text applyFormatting(MutableText out, ParserContext context) {
return out.setStyle(out.getStyle().withItalic(this.value));
protected Style style(ParserContext context) {
return this.value ? TRUE : FALSE;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

import eu.pb4.placeholders.api.ParserContext;
import eu.pb4.placeholders.api.node.TextNode;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.text.Style;

import java.util.Arrays;

public final class ObfuscatedNode extends ParentNode {
public final class ObfuscatedNode extends SimpleStylingNode {
private static final Style TRUE = Style.EMPTY.withObfuscated(true);
private static final Style FALSE = Style.EMPTY.withObfuscated(false);
private final boolean value;

public ObfuscatedNode(TextNode[] nodes, boolean value) {
Expand All @@ -16,8 +17,8 @@ public ObfuscatedNode(TextNode[] nodes, boolean value) {
}

@Override
protected Text applyFormatting(MutableText out, ParserContext context) {
return out.setStyle(out.getStyle().withObfuscated(this.value));
protected Style style(ParserContext context) {
return this.value ? TRUE : FALSE;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import eu.pb4.placeholders.api.node.TextNode;
import eu.pb4.placeholders.impl.GeneralUtils;
import net.minecraft.text.MutableText;
import net.minecraft.text.Style;
import net.minecraft.text.Text;

import java.util.Arrays;
Expand Down Expand Up @@ -74,7 +75,11 @@ public final Text toText(ParserContext context, boolean removeBackslashes) {
}
}

protected Text applyFormatting(MutableText out, ParserContext context) { return out; };
protected Text applyFormatting(MutableText out, ParserContext context) { return out.setStyle(applyFormatting(out.getStyle(), context)); };

protected Style applyFormatting(Style style, ParserContext context) {
return style;
}

@Override
public String toString() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package eu.pb4.placeholders.api.node.parent;

import eu.pb4.placeholders.api.ParserContext;
import eu.pb4.placeholders.api.node.TextNode;
import net.minecraft.text.Style;

import java.util.Collection;

public abstract class SimpleStylingNode extends ParentNode {

public SimpleStylingNode(TextNode... children) {
super(children);
}

public SimpleStylingNode(Collection<TextNode> children) {
super(children);
}

@Override
protected Style applyFormatting(Style style, ParserContext context) {
return style.withParent(this.style(context));
}
protected abstract Style style(ParserContext context);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

import eu.pb4.placeholders.api.ParserContext;
import eu.pb4.placeholders.api.node.TextNode;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.text.Style;

import java.util.Arrays;

public final class StrikethroughNode extends ParentNode {
public final class StrikethroughNode extends SimpleStylingNode {
private static final Style TRUE = Style.EMPTY.withStrikethrough(true);
private static final Style FALSE = Style.EMPTY.withStrikethrough(false);
private final boolean value;

public StrikethroughNode(TextNode[] nodes, boolean value) {
Expand All @@ -16,8 +17,8 @@ public StrikethroughNode(TextNode[] nodes, boolean value) {
}

@Override
protected Text applyFormatting(MutableText out, ParserContext context) {
return out.setStyle(out.getStyle().withStrikethrough(this.value));
protected Style style(ParserContext context) {
return this.value ? TRUE : FALSE;
}

@Override
Expand Down
Loading

0 comments on commit 8d89b1a

Please sign in to comment.