-
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make codecs work enough for basic text serialization
- Loading branch information
Showing
21 changed files
with
408 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
{ | ||
"text": "", | ||
"extra": [ | ||
{ | ||
"text": "Hello world!", | ||
"bold": true, | ||
"color": "red" | ||
}, | ||
{ | ||
"text": "\n\nTesting standalone build of " | ||
}, | ||
{ | ||
"text": "Text Placeholder API", | ||
"bold": true, | ||
"color": "gold" | ||
}, | ||
{ | ||
"text": " by " | ||
}, | ||
{ | ||
"text": "Patbox" | ||
}, | ||
{ | ||
"text": "!\n\n" | ||
}, | ||
{ | ||
"text": "Base stuff works, but more is needed! The Placeholder API part isn't even\nmodified, instead it mocks rest of used MC code!", | ||
"italic": true | ||
}, | ||
{ | ||
"text": "\n\n" | ||
}, | ||
{ | ||
"text": "Was it bad idea? Maybe.", | ||
"color": "yellow" | ||
}, | ||
{ | ||
"text": "\n\n" | ||
}, | ||
{ | ||
"text": "", | ||
"extra": [ | ||
{ | ||
"text": "T", | ||
"color": "#ff0000" | ||
}, | ||
{ | ||
"text": "a", | ||
"color": "#ffff00" | ||
}, | ||
{ | ||
"text": "t", | ||
"color": "#ff00" | ||
}, | ||
{ | ||
"text": "e", | ||
"color": "#ffff" | ||
}, | ||
{ | ||
"text": "r", | ||
"color": "#ff" | ||
} | ||
] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,32 @@ | ||
package eu.pb4.placeholderstandalone; | ||
|
||
import com.google.gson.Gson; | ||
import com.google.gson.GsonBuilder; | ||
import com.mojang.serialization.JsonOps; | ||
import eu.pb4.placeholders.api.PlaceholderContext; | ||
import eu.pb4.placeholders.api.node.TextNode; | ||
import eu.pb4.placeholders.api.parsers.NodeParser; | ||
import net.minecraft.server.network.ServerPlayerEntity; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.StandardOpenOption; | ||
|
||
public class Main { | ||
public static void main(String... args) { | ||
var parser = NodeParser.builder().quickText().globalPlaceholders().build(); | ||
|
||
try { | ||
var input = Files.readString(Path.of("input.qtxt").toAbsolutePath()); | ||
var node = TextNode.asSingle(parser.parseNode(input)); | ||
var text = node.toText(PlaceholderContext.of(new ServerPlayerEntity("Patbox"))); | ||
var gson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create(); | ||
|
||
Files.writeString(Path.of("output.json"), gson.toJson(TextCodecs.CODEC.encodeStart(JsonOps.INSTANCE, text).result().get()), StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING); | ||
} catch (Throwable e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
System.out.println(TextNode.asSingle(parser.parseNode("He<i>llo</> <rb>World</> <b>Tes</b>ting %player:name%"))); | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
src/main/java/eu/pb4/placeholderstandalone/TextCodecs.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package eu.pb4.placeholderstandalone; | ||
|
||
import com.mojang.serialization.Codec; | ||
import com.mojang.serialization.DataResult; | ||
import com.mojang.serialization.MapCodec; | ||
import com.mojang.serialization.codecs.RecordCodecBuilder; | ||
import net.minecraft.text.*; | ||
import net.minecraft.util.Identifier; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.List; | ||
import java.util.Locale; | ||
import java.util.Optional; | ||
public class TextCodecs { | ||
public static final Codec<Identifier> IDENTIFIER_CODEC = Codec.STRING.xmap(Identifier::of, Identifier::toString); | ||
public static final Codec<TextColor> COLOR_CODEC = Codec.STRING.flatXmap(TextColor::parse, (x) -> DataResult.success(x.name())); | ||
public static final Codec<HoverEvent<?>> HOVER_EVENT_CODEC = Codec.unit(null); | ||
public static final Codec<ClickEvent> CLICK_EVENT_CODEC = RecordCodecBuilder.create(instance -> instance.group( | ||
Codec.stringResolver(x -> x.name().toLowerCase(Locale.ROOT), x -> ClickEvent.Action.valueOf(x.toUpperCase(Locale.ROOT))) | ||
.fieldOf("action").forGetter(ClickEvent::action), | ||
Codec.STRING.fieldOf("value").forGetter(ClickEvent::value) | ||
).apply(instance, ClickEvent::new)); | ||
|
||
public static final MapCodec<Style> STYLE_CODEC = RecordCodecBuilder.mapCodec(instance -> instance.group( | ||
nullableCodec(COLOR_CODEC, "color").forGetter(Style::color), | ||
nullableCodec(Codec.BOOL, "italic").forGetter(Style::italic), | ||
nullableCodec(Codec.BOOL, "bold").forGetter(Style::bold), | ||
nullableCodec(Codec.BOOL, "underlined").forGetter(Style::underlined), | ||
nullableCodec(Codec.BOOL, "strikethrough").forGetter(Style::strikethrough), | ||
nullableCodec(Codec.BOOL, "obfuscated").forGetter(Style::obfuscated), | ||
nullableCodec(HOVER_EVENT_CODEC, "hoverEvent").forGetter(Style::hoverEvent), | ||
nullableCodec(CLICK_EVENT_CODEC, "clickEvent").forGetter(Style::clickEvent), | ||
nullableCodec(Codec.STRING, "insertion").forGetter(Style::insertion), | ||
nullableCodec(IDENTIFIER_CODEC, "font").forGetter(Style::font) | ||
).apply(instance, Style::new)); | ||
|
||
|
||
|
||
@SuppressWarnings("unchecked") | ||
public static final MapCodec<TextContent> CONTENT_CODEC = | ||
(MapCodec<TextContent>) (Object) Codec.STRING.xmap(PlainTextContent.Literal::new, PlainTextContent.Literal::string).fieldOf("text"); | ||
|
||
public static final Codec<Text> RAW_CODEC = Codec.recursive("text", (self) -> RecordCodecBuilder.create(instance -> instance.group( | ||
CONTENT_CODEC.forGetter(Text::getContent), | ||
STYLE_CODEC.forGetter(Text::getStyle), | ||
self.listOf().optionalFieldOf("extra", List.of()).forGetter(Text::getSiblings) | ||
).apply(instance, MutableText::new))); | ||
|
||
public static final Codec<Text> CODEC = Codec.withAlternative(RAW_CODEC, Codec.STRING, Text::literal); | ||
|
||
public static <B, T> MapCodec<@Nullable T> nullableCodec(Codec<T> codec, String field) { | ||
return codec.optionalFieldOf(field).xmap(x -> x.orElse(null), Optional::ofNullable); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
package net.minecraft.component; | ||
|
||
public record ComponentType() { | ||
import net.minecraft.util.Identifier; | ||
|
||
public record ComponentType(Identifier id) { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
package net.minecraft.component; | ||
|
||
import net.minecraft.util.Identifier; | ||
|
||
public class DataComponentTypes { | ||
public static final ComponentType CUSTOM_NAME = new ComponentType(); | ||
public static final ComponentType CUSTOM_NAME = new ComponentType(Identifier.of("custom_name")); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
package net.minecraft.item; | ||
|
||
public class Item { | ||
import net.minecraft.util.Identifier; | ||
|
||
public record Item(Identifier id) { | ||
public ItemStack getDefaultStack() { | ||
return new ItemStack(); | ||
return id.equals(ItemStack.EMPTY.getItemId()) ? ItemStack.EMPTY : new ItemStack(id, 1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,12 @@ | ||
package net.minecraft.registry; | ||
|
||
import net.minecraft.item.Item; | ||
import net.minecraft.item.ItemStack; | ||
import net.minecraft.stat.StatType; | ||
import net.minecraft.util.Identifier; | ||
|
||
public class Registries { | ||
public static final Registry<Item> ITEM = new Registry<>(new Item()); | ||
public static final Registry<Item> ITEM = new Registry<>(new Item(ItemStack.EMPTY.getItemId())); | ||
public static final Registry<Identifier> CUSTOM_STAT = new Registry<>(Identifier.of("a")); | ||
public static final Registry<StatType> STAT_TYPE = new Registry<>(new StatType()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.