Skip to content

Commit

Permalink
1.0.7
Browse files Browse the repository at this point in the history
  • Loading branch information
toxicity188 committed May 15, 2024
1 parent c9d800a commit bcd42c1
Show file tree
Hide file tree
Showing 16 changed files with 367 additions and 75 deletions.
2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package kr.toxicity.libraries.datacomponent.api;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonPrimitive;
import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;

@SuppressWarnings("unused")
Expand All @@ -12,8 +15,6 @@ public interface Codec<T> {
JsonElement encode(@NotNull T t) throws IllegalStateException;
@NotNull
T decode(@NotNull JsonElement t) throws IllegalStateException;
@NotNull
Class<T> returnType();

Codec<Integer> INTEGER = of(Integer.TYPE, JsonPrimitive::new, JsonElement::getAsInt);
Codec<String> STRING = of(String.class, JsonPrimitive::new, JsonElement::getAsString);
Expand All @@ -31,13 +32,9 @@ public interface Codec<T> {
return decoder.apply(t);
}

@Override
public @NotNull Class<T> returnType() {
return tClass;
}
};
}
default <R> @NotNull Codec<R> map(@NotNull Class<R> rClass, @NotNull Converter<R, T> converter) {
default <R> @NotNull Codec<R> map(@NotNull Converter<R, T> converter) {
return new Codec<>() {
@Override
public @NotNull JsonElement encode(@NotNull R r) throws IllegalStateException {
Expand All @@ -48,10 +45,22 @@ public interface Codec<T> {
public @NotNull R decode(@NotNull JsonElement t) throws IllegalStateException {
return converter.asWrapper(Codec.this.decode(t));
}
};
}
default Codec<List<T>> list() {
return new Codec<>() {
@Override
public @NotNull JsonArray encode(@NotNull List<T> t) throws IllegalStateException {
var array = new JsonArray();
t.forEach(e -> array.add(Codec.this.encode(e)));
return array;
}

@Override
public @NotNull Class<R> returnType() {
return rClass;
public @NotNull List<T> decode(@NotNull JsonElement t) throws IllegalStateException {
var list = new ArrayList<T>();
t.getAsJsonArray().forEach(e -> list.add(Codec.this.decode(e)));
return list;
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.jetbrains.annotations.NotNull;

import java.util.List;
import java.util.function.Function;

public interface Converter<T, R> {
Expand Down Expand Up @@ -34,4 +35,17 @@ default Converter<R, T> reversed() {
}
};
}
default Converter<List<T>, List<R>> list() {
return new Converter<>() {
@Override
public @NotNull List<R> asVanilla(@NotNull List<T> list) {
return list.stream().map(Converter.this::asVanilla).toList();
}

@Override
public @NotNull List<T> asWrapper(@NotNull List<R> rs) {
return rs.stream().map(Converter.this::asWrapper).toList();
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.NotNull;

import java.util.List;

@SuppressWarnings("unused")
public interface NMS {
static @NotNull NMS nms() {
Expand All @@ -24,14 +26,18 @@ public interface NMS {
@NotNull
DataComponentType<Component> customName();
@NotNull
DataComponentType<ItemLore> itemLore();
DataComponentType<Component> itemName();
@NotNull
DataComponentType<ItemLore> lore();
@NotNull
DataComponentType<Rarity> rarity();
@NotNull
DataComponentType<AdventureModePredicate> canPlaceOn();
@NotNull
DataComponentType<AdventureModePredicate> canBreak();
@NotNull
DataComponentType<CustomModelData> customModelData();
@NotNull
DataComponentType<Integer> repairCost();
@NotNull
DataComponentType<Unit> creativeSlotLock();
Expand Down Expand Up @@ -65,4 +71,14 @@ public interface NMS {
DataComponentType<ArmorTrim> trim();
@NotNull
DataComponentType<BlockItemStateProperties> blockState();
@NotNull
DataComponentType<CustomData> entityData();
@NotNull
DataComponentType<CustomData> bucketEntityData();
@NotNull
DataComponentType<CustomData> blockEntityData();
@NotNull
DataComponentType<Integer> ominousBottleAmplifier();
@NotNull
DataComponentType<List<String>> recipes();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package kr.toxicity.libraries.datacomponent.api.wrapper;

import org.jetbrains.annotations.NotNull;

import java.util.Map;

public record CompoundTag(@NotNull Map<String, Tag<?>> tags) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package kr.toxicity.libraries.datacomponent.api.wrapper;

import kr.toxicity.libraries.datacomponent.api.Codec;
import kr.toxicity.libraries.datacomponent.api.NMS;
import org.jetbrains.annotations.NotNull;

public record CustomData(@NotNull CompoundTag tag) implements ComponentData<CustomData> {
@Override
public Codec<CustomData> codec() {
return NMS.nms().entityData().codec();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package kr.toxicity.libraries.datacomponent.api.wrapper;

public record CustomModelData(int value) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
public record ItemLore(@NotNull List<Component> lines, @NotNull List<Component> styledLines) implements ComponentData<ItemLore> {
@Override
public Codec<ItemLore> codec() {
return NMS.nms().itemLore().codec();
return NMS.nms().lore().codec();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package kr.toxicity.libraries.datacomponent.api.wrapper;

import org.jetbrains.annotations.NotNull;

import java.util.UUID;

public record Tag<T>(@NotNull Type<T> type, @NotNull T value) {

public static final Type<Byte> BYTE = new Type<>(Byte.class);
public static final Type<Short> SHORT = new Type<>(Short.class);
public static final Type<Integer> INT = new Type<>(Integer.class);
public static final Type<Long> LONG = new Type<>(Long.class);
public static final Type<UUID> UUID = new Type<>(UUID.class);
public static final Type<Float> FLOAT = new Type<>(Float.class);
public static final Type<String> STRING = new Type<>(String.class);
public static final Type<Double> DOUBLE = new Type<>(Double.class);

public static final Type<byte[]> BYTE_ARRAY = new Type<>(byte[].class);
public static final Type<int[]> INT_ARRAY = new Type<>(int[].class);
public static final Type<long[]> LONG_ARRAY = new Type<>(long[].class);

public record Type<T>(@NotNull Class<T> valueType) {
}
}
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ allprojects {
apply(plugin = "java")

group = "kr.toxicity.libraries.datacomponent"
version = "1.0.6"
version = "1.0.7"

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,11 @@ private DataComponentAPIImpl(@NotNull String version) {
}
private DataComponentAPIImpl(@NotNull MinecraftVersion current){
this.current = current;
String version;
if (current.equals(MinecraftVersionImpl.V1_20_5) || current.equals(MinecraftVersionImpl.V1_20_6)) {
version = "v1_20_R4";
nms = new kr.toxicity.libraries.datacomponent.nms.v1_20_R4.NMSImpl();
} else {
throw new UnsupportedOperationException("Unsupported minecraft version: " + current);
}
try {
nms = (NMS) Class.forName(getClass().getPackage().getName() + ".nms." + version + ".NMSImpl").getConstructor().newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
deserializer = e -> {
Map<DataComponentType<?>, JsonElement> consumer = Collections.synchronizedMap(new HashMap<>());
for (Map.Entry<String, JsonElement> entry : e.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.ComponentSerialization;
import net.minecraft.resources.RegistryOps;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.util.Unit;
import net.minecraft.world.food.FoodProperties;
import net.minecraft.world.item.AdventureModePredicate;
Expand All @@ -29,41 +30,37 @@ final class CodecImpl<T> implements kr.toxicity.libraries.datacomponent.api.Code

private static final RegistryOps<JsonElement> OPS = RegistryAccess.fromRegistryOfRegistries(BuiltInRegistries.REGISTRY).createSerializationContext(JsonOps.INSTANCE);

static final CodecImpl<Component> COMPONENT = of(Component.class, ComponentSerialization.CODEC, t -> new JsonObject());
static final CodecImpl<ItemLore> ITEM_LORE = of(ItemLore.class, ItemLore.CODEC, t -> new JsonArray());
static final CodecImpl<Rarity> RARITY = of(Rarity.class, Rarity.CODEC, t -> new JsonPrimitive(t.name().toLowerCase()));
static final CodecImpl<Unit> UNIT = of(Unit.class, Codec.unit(Unit.INSTANCE), t -> new JsonPrimitive(true));
static final CodecImpl<AdventureModePredicate> ADVENTURE_MODE_PREDICATE = of(AdventureModePredicate.class, AdventureModePredicate.CODEC, t -> new JsonObject());
static final CodecImpl<Tool> TOOL = of(Tool.class, Tool.CODEC, t -> new JsonObject());
static final CodecImpl<DyedItemColor> DYED_ITEM_COLOR = of(DyedItemColor.class, DyedItemColor.CODEC, t -> new JsonObject());
static final CodecImpl<MapItemColor> MAP_ITEM_COLOR = of(MapItemColor.class, MapItemColor.CODEC, t -> new JsonPrimitive(t.rgb()));
static final CodecImpl<MapId> MAP_ID = of(MapId.class, MapId.CODEC, t -> new JsonPrimitive(t.id()));
static final CodecImpl<BundleContents> BUNDLE_CONTENTS = of(BundleContents.class, BundleContents.CODEC, t -> new JsonArray());
static final CodecImpl<PotionContents> POTION_CONTENTS = of(PotionContents.class, PotionContents.CODEC, t -> new JsonObject());
static final CodecImpl<SuspiciousStewEffects> SUSPICIOUS_STEW_EFFECTS = of(SuspiciousStewEffects.class, SuspiciousStewEffects.CODEC, t -> new JsonArray());
static final CodecImpl<WritableBookContent> WRITABLE_BOOK_CONTENT = of(WritableBookContent.class, WritableBookContent.CODEC, t -> new JsonObject());
static final CodecImpl<WrittenBookContent> WRITTEN_BOOK_CONTENT = of(WrittenBookContent.class, WrittenBookContent.CODEC, t -> new JsonObject());
static final CodecImpl<BlockItemStateProperties> BLOCK_STATE = of(BlockItemStateProperties.class, BlockItemStateProperties.CODEC, t -> new JsonObject());
static final CodecImpl<FoodProperties> FOOD = of(FoodProperties.class, FoodProperties.DIRECT_CODEC, t -> new JsonObject());
static final CodecImpl<ArmorTrim> TRIM = of(ArmorTrim.class, ArmorTrim.CODEC, t -> new JsonObject());
static final CodecImpl<Component> COMPONENT = of(ComponentSerialization.CODEC, t -> new JsonObject());
static final CodecImpl<ItemLore> ITEM_LORE = of(ItemLore.CODEC, t -> new JsonArray());
static final CodecImpl<Rarity> RARITY = of(Rarity.CODEC, t -> new JsonPrimitive(t.name().toLowerCase()));
static final CodecImpl<Unit> UNIT = of(Codec.unit(Unit.INSTANCE), t -> new JsonPrimitive(true));
static final CodecImpl<AdventureModePredicate> ADVENTURE_MODE_PREDICATE = of(AdventureModePredicate.CODEC, t -> new JsonObject());
static final CodecImpl<Tool> TOOL = of(Tool.CODEC, t -> new JsonObject());
static final CodecImpl<DyedItemColor> DYED_ITEM_COLOR = of(DyedItemColor.CODEC, t -> new JsonObject());
static final CodecImpl<MapItemColor> MAP_ITEM_COLOR = of(MapItemColor.CODEC, t -> new JsonPrimitive(t.rgb()));
static final CodecImpl<MapId> MAP_ID = of(MapId.CODEC, t -> new JsonPrimitive(t.id()));
static final CodecImpl<BundleContents> BUNDLE_CONTENTS = of(BundleContents.CODEC, t -> new JsonArray());
static final CodecImpl<PotionContents> POTION_CONTENTS = of(PotionContents.CODEC, t -> new JsonObject());
static final CodecImpl<SuspiciousStewEffects> SUSPICIOUS_STEW_EFFECTS = of(SuspiciousStewEffects.CODEC, t -> new JsonArray());
static final CodecImpl<WritableBookContent> WRITABLE_BOOK_CONTENT = of(WritableBookContent.CODEC, t -> new JsonObject());
static final CodecImpl<WrittenBookContent> WRITTEN_BOOK_CONTENT = of(WrittenBookContent.CODEC, t -> new JsonObject());
static final CodecImpl<BlockItemStateProperties> BLOCK_STATE = of(BlockItemStateProperties.CODEC, t -> new JsonObject());
static final CodecImpl<FoodProperties> FOOD = of(FoodProperties.DIRECT_CODEC, t -> new JsonObject());
static final CodecImpl<ArmorTrim> TRIM = of(ArmorTrim.CODEC, t -> new JsonObject());
static final CodecImpl<CustomModelData> CUSTOM_MODEL_DATA = of(CustomModelData.CODEC, t -> new JsonPrimitive(t.value()));
static final CodecImpl<CustomData> CUSTOM_DATA = of(CustomData.CODEC, t -> new JsonObject());
static final CodecImpl<ResourceLocation> RESOURCE_LOCATION = of(ResourceLocation.CODEC, t -> new JsonPrimitive(t.toString()));

private static <T> @NotNull CodecImpl<T> of(@NotNull Class<T> clazz, @NotNull Codec<T> codec, Function<T, JsonElement> function) {
private static <T> @NotNull CodecImpl<T> of(@NotNull Codec<T> codec, Function<T, JsonElement> function) {
return new CodecImpl<>(
clazz,
codec,
function
);
}

private final Class<T> clazz;
private final Codec<T> codec;
private final Function<T, JsonElement> function;

@Override
public @NotNull Class<T> returnType() {
return clazz;
}

@Override
public @NotNull JsonElement encode(@NotNull T t) {
var element = function.apply(t);
Expand Down
Loading

0 comments on commit bcd42c1

Please sign in to comment.