Skip to content

Commit

Permalink
Make codecs work enough for basic text serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
Patbox committed Jul 1, 2024
1 parent c9ae142 commit d871331
Show file tree
Hide file tree
Showing 21 changed files with 408 additions and 83 deletions.
66 changes: 66 additions & 0 deletions output.json
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"
}
]
}
]
}
21 changes: 20 additions & 1 deletion src/main/java/eu/pb4/placeholderstandalone/Main.java
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 src/main/java/eu/pb4/placeholderstandalone/TextCodecs.java
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);
}

}
4 changes: 3 additions & 1 deletion src/main/java/net/minecraft/component/ComponentType.java
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) {
}
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"));
}
3 changes: 2 additions & 1 deletion src/main/java/net/minecraft/entity/Entity.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@
import net.minecraft.server.world.ServerWorld;
import net.minecraft.world.World;

import java.nio.charset.StandardCharsets;
import java.util.UUID;

public class Entity {
private final static UUID ENTITY_ID = UUID.fromString("Placeholder API");
private final static UUID ENTITY_ID = UUID.nameUUIDFromBytes("Placeholder API".getBytes(StandardCharsets.UTF_8));



Expand Down
6 changes: 4 additions & 2 deletions src/main/java/net/minecraft/item/Item.java
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);
}
}
43 changes: 41 additions & 2 deletions src/main/java/net/minecraft/item/ItemStack.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,38 @@
package net.minecraft.item;

import com.google.gson.JsonElement;
import net.minecraft.component.ComponentType;
import net.minecraft.nbt.StringNbtReader;
import net.minecraft.registry.DynamicRegistryManager;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.util.Identifier;
import net.minecraft.util.Rarity;
import org.checkerframework.checker.units.qual.N;
import org.jetbrains.annotations.Nullable;

import java.util.HashMap;
import java.util.Map;

public class ItemStack {
public static final ItemStack EMPTY = new ItemStack();
public static final ItemStack EMPTY = new ItemStack(Identifier.of("air"), 0);

public ItemStack(Identifier air, int i) {
this.identifier = air;
this.count = i;
}

public static ItemStack fromNbtOrEmpty(DynamicRegistryManager empty, StringNbtReader parse) {
return EMPTY;
}


private int count = 1;
private final Identifier identifier;
private final Map<Identifier, JsonElement> components = new HashMap<>();

public void setCount(int i) {
this.count = count;
}

public boolean isEmpty() {
Expand All @@ -25,10 +44,30 @@ public MutableText getName() {
}

public boolean contains(ComponentType type) {
return false;
return components.containsKey(type.id());
}
public boolean contains(Identifier type) {
return components.containsKey(type);
}

@Nullable
public JsonElement get(Identifier type) {
return this.components.get(type);
}

public void set(Identifier type, JsonElement element) {
if (element == null) {
this.components.remove(type);
} else {
this.components.put(type, element);
}
}

public Rarity getRarity() {
return Rarity.COMMON;
}

public Identifier getItemId() {
return this.identifier;
}
}
3 changes: 2 additions & 1 deletion src/main/java/net/minecraft/registry/Registries.java
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());
}
5 changes: 5 additions & 0 deletions src/main/java/net/minecraft/text/KeybindTextContent.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,9 @@ public record KeybindTextContent(String key) implements TextContent {
public String getKey() {
return key;
}

@Override
public String getString() {
return "<key>";
}
}
38 changes: 27 additions & 11 deletions src/main/java/net/minecraft/text/MutableText.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,25 @@

public class MutableText implements Text {
private Style style = Style.EMPTY;
private List<Text> siblings = new ArrayList<>();
private final List<Text> siblings = new ArrayList<>();
private final TextContent contant;
public MutableText(TextContent contant) {
this.contant = contant;
}

public MutableText(TextContent contant, Style style, List<Text> siblings) {
this.contant = contant;
this.style = style;
this.siblings.addAll(siblings);
}

public MutableText formatted(Formatting... formattings) {
this.setStyle(this.getStyle().withFormatting(formattings));
return this;
}

public Text withColor(int i) {
this.setStyle(this.getStyle().withColor(i));
return this;
}
public MutableText setStyle(Style style) {
Expand All @@ -25,40 +40,41 @@ public Style getStyle() {

@Override
public MutableText copyContentOnly() {
return this;
return new MutableText(this.contant).setStyle(this.style);
}

@Override
public MutableText copy() {
return this;
var c = copyContentOnly();
c.siblings.addAll(this.siblings);
return c;
}

@Override
public String getString() {
return "";
var b = new StringBuilder();
b.append(this.contant.getString());
this.siblings.forEach(x -> b.append(x.getString()));
return b.toString();
}

@Override
public TextContent getContent() {
return null;
return this.contant;
}

@Override
public List<Text> getSiblings() {
return null;
return this.siblings;
}

public MutableText append(Text mutableText) {
this.siblings.add(mutableText);
return this;
}

public void styled(Function<Style, Style> styled) {
public MutableText styled(Function<Style, Style> styled) {
this.setStyle(styled.apply(this.getStyle()));
}

public Text withColor(int i) {
this.setStyle(this.getStyle().withColor(i));
return this;
}
}
5 changes: 5 additions & 0 deletions src/main/java/net/minecraft/text/NbtTextContent.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,9 @@ public Optional<Text> getSeparator() {
public NbtDataSource getDataSource() {
return dataSource;
}

@Override
public String getString() {
return "<nbt>";
}
}
Loading

0 comments on commit d871331

Please sign in to comment.