Skip to content

Commit

Permalink
Bug fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
toxicity188 committed May 16, 2024
1 parent b3b0cb8 commit f0ab985
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ public interface Codec<T> {
@NotNull
T decode(@NotNull JsonElement t) throws IllegalStateException;

Codec<Integer> INTEGER = of(Integer.TYPE, JsonPrimitive::new, JsonElement::getAsInt);
Codec<String> STRING = of(String.class, JsonPrimitive::new, JsonElement::getAsString);
Codec<Boolean> BOOL = of(Boolean.TYPE, JsonPrimitive::new, JsonElement::getAsBoolean);
Codec<Integer> INTEGER = of(JsonPrimitive::new, JsonElement::getAsInt);
Codec<String> STRING = of(JsonPrimitive::new, JsonElement::getAsString);
Codec<Boolean> BOOL = of(JsonPrimitive::new, JsonElement::getAsBoolean);

static <T> @NotNull Codec<T> of(@NotNull Class<T> tClass, @NotNull Function<T, JsonElement> encoder, @NotNull Function<JsonElement, T> decoder) {
static <T> @NotNull Codec<T> of(@NotNull Function<T, JsonElement> encoder, @NotNull Function<JsonElement, T> decoder) {
return new Codec<>() {
@Override
public @NotNull JsonElement encode(@NotNull T t) throws IllegalStateException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,56 @@
package kr.toxicity.libraries.datacomponent.api;

import com.google.gson.JsonElement;
import net.kyori.adventure.text.Component;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import kr.toxicity.libraries.datacomponent.api.wrapper.*;

import java.util.List;
import java.util.function.Supplier;

import static kr.toxicity.libraries.datacomponent.api.NMS.nms;

@SuppressWarnings("unused")
public interface DataComponentType<T> {

Supplier<DataComponentType<Integer>> MAX_STACK_SIZE = () -> nms().maxStackSize();
Supplier<DataComponentType<Integer>> MAX_DAMAGE = () -> nms().maxDamage();
Supplier<DataComponentType<Integer>> DAMAGE = () -> nms().damage();
Supplier<DataComponentType<Component>> CUSTOM_NAME = () -> nms().customName();
Supplier<DataComponentType<Component>> ITEM_NAME = () -> nms().itemName();
Supplier<DataComponentType<ItemLore>> LORE = () -> nms().lore();
Supplier<DataComponentType<Rarity>> RARITY = () -> nms().rarity();
Supplier<DataComponentType<AdventureModePredicate>> CAN_PLACE_ON = () -> nms().canPlaceOn();
Supplier<DataComponentType<AdventureModePredicate>> CAN_BREAK = () -> nms().canBreak();
Supplier<DataComponentType<CustomModelData>> CUSTOM_MODEL_DATA = () -> nms().customModelData();
Supplier<DataComponentType<Integer>> REPAIR_COST = () -> nms().repairCost();
Supplier<DataComponentType<Unit>> CREATIVE_SLOT_LOCK = () -> nms().creativeSlotLock();
Supplier<DataComponentType<Boolean>> ENCHANTMENT_GLINT_OVERRIDE = () -> nms().enchantmentGlintOverride();
Supplier<DataComponentType<Unit>> INTANGIBLE_PROJECTILE = () -> nms().intangibleProjectile();
Supplier<DataComponentType<FoodProperties>> FOOD = () -> nms().food();
Supplier<DataComponentType<Unit>> FIRE_RESISTANT = () -> nms().fireResistant();
Supplier<DataComponentType<Tool>> TOOL = () -> nms().tool();
Supplier<DataComponentType<DyedItemColor>> DYED_COLOR = () -> nms().dyedColor();
Supplier<DataComponentType<MapItemColor>> MAP_COLOR = () -> nms().mapColor();
Supplier<DataComponentType<MapId>> MAP_ID = () -> nms().mapId();
Supplier<DataComponentType<BundleContents>> BUNDLE_CONTENTS = () -> nms().bundleContents();
Supplier<DataComponentType<PotionContents>> POTION_CONTENTS = () -> nms().potionContents();
Supplier<DataComponentType<SuspiciousStewEffects>> SUSPICIOUS_STEW_EFFECTS = () -> nms().suspiciousStewEffects();
Supplier<DataComponentType<WritableBookContent>> WRITABLE_BOOK_CONTENT = () -> nms().writableBookContent();
Supplier<DataComponentType<WrittenBookContent>> WRITTEN_BOOK_CONTENT = () -> nms().writtenBookContent();
Supplier<DataComponentType<ArmorTrim>> TRIM = () -> nms().trim();
Supplier<DataComponentType<BlockItemStateProperties>> BLOCK_STATE = () -> nms().blockState();
Supplier<DataComponentType<CustomData>> ENTITY_DATA = () -> nms().entityData();
Supplier<DataComponentType<CustomData>> BUCKET_ENTITY_DATA = () -> nms().bucketEntityData();
Supplier<DataComponentType<CustomData>> BLOCK_ENTITY_DATA = () -> nms().blockEntityData();
Supplier<DataComponentType<Integer>> OMINOUS_BOTTLE_AMPLIFIER = () -> nms().ominousBottleAmplifier();
Supplier<DataComponentType<List<String>>> RECIPES = () -> nms().recipes();

static Registry<? extends DataComponentType<?>> registry() {
return NMS.nms().componentRegistry();
}

@NotNull String key();
@NotNull Codec<T> codec();

Expand All @@ -16,4 +59,5 @@ static Registry<? extends DataComponentType<?>> registry() {

@Nullable T setToJson(@NotNull ItemAdapter adapter, @Nullable JsonElement element);
@Nullable JsonElement getToJson(@NotNull ItemAdapter adapter);

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.function.Supplier;

@SuppressWarnings("unused")
public interface ItemAdapter {
@NotNull
Expand All @@ -17,13 +19,25 @@ default <T> void set(@NotNull DataComponentType<T> type, @Nullable T t) {
default <T> T get(@NotNull DataComponentType<T> type) {
return type.get(this);
}
default <T> void set(@NotNull Supplier<DataComponentType<T>> type, @Nullable T t) {
set(type.get(), t);
}
default <T> T get(@NotNull Supplier<DataComponentType<T>> type) {
return get(type.get());
}

default void setToJson(@NotNull DataComponentType<?> type, @Nullable JsonElement element) {
type.setToJson(this, element);
}
default JsonElement getToJson(@NotNull DataComponentType<?> type) {
return type.getToJson(this);
}
default void setToJson(@NotNull Supplier<DataComponentType<?>> type, @Nullable JsonElement element) {
setToJson(type.get(), element);
}
default JsonElement getToJson(@NotNull Supplier<DataComponentType<?>> type) {
return getToJson(type.get());
}

default void deserialize(@NotNull JsonObject object) {
DataComponentAPI.api().deserializer().deserialize(object).set(this);
Expand Down
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.9"
version = "1.0.10"

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,13 @@ final class DataComponentTypeImpl<T, R> implements kr.toxicity.libraries.datacom
);
static final DataComponentTypeImpl<CustomData, net.minecraft.world.item.component.CustomData> BUCKET_ENTITY_DATA = register(
"bucket_entity_data",
DataComponents.ENTITY_DATA,
DataComponents.BUCKET_ENTITY_DATA,
Converters.CUSTOM_DATA,
CodecImpl.CUSTOM_DATA
);
static final DataComponentTypeImpl<CustomData, net.minecraft.world.item.component.CustomData> BLOCK_ENTITY_DATA = register(
"block_entity_data",
DataComponents.ENTITY_DATA,
DataComponents.BLOCK_ENTITY_DATA,
Converters.CUSTOM_DATA,
CodecImpl.CUSTOM_DATA
);
Expand Down
12 changes: 3 additions & 9 deletions test-plugin/shade/src/main/java/kr/toxicity/test/TestPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,21 @@
import kr.toxicity.libraries.datacomponent.api.DataComponentAPI;
import kr.toxicity.libraries.datacomponent.api.DataComponentType;
import kr.toxicity.libraries.datacomponent.api.NMS;
import kr.toxicity.libraries.datacomponent.api.wrapper.CompoundTag;
import kr.toxicity.libraries.datacomponent.api.wrapper.CustomData;
import kr.toxicity.libraries.datacomponent.api.wrapper.Rarity;
import kr.toxicity.libraries.datacomponent.api.wrapper.Tag;
import net.kyori.adventure.text.Component;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.HashMap;
import java.util.List;

public class TestPlugin extends JavaPlugin {
@Override
public void onEnable() {
DataComponentAPIBukkit.load();

var apply = DataComponentAPI.api().adapter(new ItemStack(Material.DIAMOND_SWORD));
apply.set(NMS.nms().damage(), 3);
apply.set(NMS.nms().repairCost(), 20);
apply.set(NMS.nms().rarity(), Rarity.EPIC);
apply.set(DataComponentType.DAMAGE, 3);
apply.set(DataComponentType.REPAIR_COST, 20);
apply.set(DataComponentType.RARITY, Rarity.EPIC);
getLogger().info(apply.serialize().toString());

// Serialization.
Expand Down

0 comments on commit f0ab985

Please sign in to comment.