diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/GameProfile.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/GameProfile.java similarity index 59% rename from api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/GameProfile.java rename to api/src/main/java/com/viaversion/viaversion/api/minecraft/GameProfile.java index 4f61b1941..c1d8f23cc 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/GameProfile.java +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/GameProfile.java @@ -20,44 +20,13 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -package com.viaversion.viaversion.api.minecraft.item.data; +package com.viaversion.viaversion.api.minecraft; -import com.viaversion.viaversion.api.type.Type; -import io.netty.buffer.ByteBuf; import java.util.UUID; import org.checkerframework.checker.nullness.qual.Nullable; public final class GameProfile { - public static final Type TYPE = new Type(GameProfile.class) { - @Override - public GameProfile read(final ByteBuf buffer) throws Exception { - final String name = Type.STRING.read(buffer); - final UUID id = Type.OPTIONAL_UUID.read(buffer); - final int propertyCount = Type.VAR_INT.readPrimitive(buffer); - final Property[] properties = new Property[propertyCount]; - for (int i = 0; i < propertyCount; i++) { - final String propertyName = Type.STRING.read(buffer); - final String propertyValue = Type.STRING.read(buffer); - final String propertySignature = Type.OPTIONAL_STRING.read(buffer); - properties[i] = new Property(propertyName, propertyValue, propertySignature); - } - return new GameProfile(name, id, properties); - } - - @Override - public void write(final ByteBuf buffer, final GameProfile value) throws Exception { - Type.STRING.write(buffer, value.name); - Type.OPTIONAL_UUID.write(buffer, value.id); - Type.VAR_INT.writePrimitive(buffer, value.properties.length); - for (final Property property : value.properties) { - Type.STRING.write(buffer, property.name); - Type.STRING.write(buffer, property.value); - Type.OPTIONAL_STRING.write(buffer, property.signature); - } - } - }; - private final String name; private final UUID id; private final Property[] properties; diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/HolderSet.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/HolderSet.java new file mode 100644 index 000000000..b9288956d --- /dev/null +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/HolderSet.java @@ -0,0 +1,42 @@ +/* + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * Copyright (C) 2016-2024 ViaVersion and contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.viaversion.viaversion.api.minecraft; + +import com.viaversion.viaversion.util.Either; + +public final class HolderSet { + + private final Either values; + + public HolderSet(final String tagKey) { + this.values = Either.left(tagKey); + } + + public HolderSet(final int[] ids) { + this.values = Either.right(ids); + } + + public Either values() { + return values; + } +} diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/data/StructuredDataKey.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/data/StructuredDataKey.java index 1a266b8ca..c4e34f233 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/minecraft/data/StructuredDataKey.java +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/data/StructuredDataKey.java @@ -24,14 +24,20 @@ package com.viaversion.viaversion.api.minecraft.data; import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import com.github.steveice10.opennbt.tag.builtin.Tag; +import com.viaversion.viaversion.api.minecraft.GameProfile; import com.viaversion.viaversion.api.minecraft.item.Item; +import com.viaversion.viaversion.api.minecraft.item.data.AttributeModifiers; import com.viaversion.viaversion.api.minecraft.item.data.BannerPattern; import com.viaversion.viaversion.api.minecraft.item.data.Bee; import com.viaversion.viaversion.api.minecraft.item.data.BlockStateProperties; +import com.viaversion.viaversion.api.minecraft.item.data.AdventureModePredicate; import com.viaversion.viaversion.api.minecraft.item.data.DyedColor; import com.viaversion.viaversion.api.minecraft.item.data.Enchantments; -import com.viaversion.viaversion.api.minecraft.item.data.GameProfile; +import com.viaversion.viaversion.api.minecraft.item.data.FireworkExplosion; +import com.viaversion.viaversion.api.minecraft.item.data.Fireworks; import com.viaversion.viaversion.api.minecraft.item.data.LodestoneTarget; +import com.viaversion.viaversion.api.minecraft.item.data.PotionContents; +import com.viaversion.viaversion.api.minecraft.item.data.SuspiciousStewEffect; import com.viaversion.viaversion.api.minecraft.item.data.WrittenBook; import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.types.version.Types1_20_5; @@ -45,15 +51,15 @@ public final class StructuredDataKey { public static final StructuredDataKey CUSTOM_NAME = new StructuredDataKey<>("custom_name", Type.TAG); public static final StructuredDataKey LORE = new StructuredDataKey<>("lore", Type.TAG_ARRAY); public static final StructuredDataKey ENCHANTMENTS = new StructuredDataKey<>("enchantments", Enchantments.TYPE); - public static final StructuredDataKey CAN_PLACE_ON = new StructuredDataKey<>("can_place_on", Type.UNIT); // TODO - public static final StructuredDataKey CAN_BREAK = new StructuredDataKey<>("can_break", Type.UNIT); // TODO - public static final StructuredDataKey ATTRIBUTE_MODIFIERS = new StructuredDataKey<>("attribute_modifiers", Type.UNIT); // TODO + public static final StructuredDataKey CAN_PLACE_ON = new StructuredDataKey<>("can_place_on", AdventureModePredicate.TYPE); + public static final StructuredDataKey CAN_BREAK = new StructuredDataKey<>("can_break", AdventureModePredicate.TYPE); + public static final StructuredDataKey ATTRIBUTE_MODIFIERS = new StructuredDataKey<>("attribute_modifiers", AttributeModifiers.TYPE); public static final StructuredDataKey CUSTOM_MODEL_DATA = new StructuredDataKey<>("custom_model_data", Type.VAR_INT); - public static final StructuredDataKey HIDE_ADDITIONAL_TOOLTIP = new StructuredDataKey<>("hide_additional_tooltip", Type.UNIT); + public static final StructuredDataKey HIDE_ADDITIONAL_TOOLTIP = new StructuredDataKey<>("hide_additional_tooltip", Type.EMPTY); public static final StructuredDataKey REPAIR_COST = new StructuredDataKey<>("repair_cost", Type.VAR_INT); - public static final StructuredDataKey CREATIVE_SLOT_LOCK = new StructuredDataKey<>("creative_slot_lock", Type.UNIT); + public static final StructuredDataKey CREATIVE_SLOT_LOCK = new StructuredDataKey<>("creative_slot_lock", Type.EMPTY); public static final StructuredDataKey ENCHANTMENT_GLINT_OVERRIDE = new StructuredDataKey<>("enchantment_glint_override", Type.BOOLEAN); - public static final StructuredDataKey INTANGIBLE_PROJECTILE = new StructuredDataKey<>("intangible_projectile", Type.UNIT); + public static final StructuredDataKey INTANGIBLE_PROJECTILE = new StructuredDataKey<>("intangible_projectile", Type.EMPTY); public static final StructuredDataKey STORED_ENCHANTMENTS = new StructuredDataKey<>("storded_enchantments", Enchantments.TYPE); public static final StructuredDataKey DYED_COLOR = new StructuredDataKey<>("dyed_color", DyedColor.TYPE); public static final StructuredDataKey MAP_COLOR = new StructuredDataKey<>("map_color", Type.INT); @@ -62,21 +68,21 @@ public final class StructuredDataKey { public static final StructuredDataKey MAP_POST_PROCESSING = new StructuredDataKey<>("map_post_processing", Type.VAR_INT); public static final StructuredDataKey CHARGED_PROJECTILES = new StructuredDataKey<>("charged_projectiles", Types1_20_5.ITEM_ARRAY); public static final StructuredDataKey BUNDLE_CONTENTS = new StructuredDataKey<>("bundle_contents", Types1_20_5.ITEM_ARRAY); - public static final StructuredDataKey POTION_CONTENTS = new StructuredDataKey<>("potion_contents", Type.UNIT); // TODO - public static final StructuredDataKey SUSPICIOUS_STEW_EFFECTS = new StructuredDataKey<>("suspicious_stew_effects", Type.UNIT); // TODO + public static final StructuredDataKey POTION_CONTENTS = new StructuredDataKey<>("potion_contents", PotionContents.TYPE); + public static final StructuredDataKey SUSPICIOUS_STEW_EFFECTS = new StructuredDataKey<>("suspicious_stew_effects", SuspiciousStewEffect.ARRAY_TYPE); public static final StructuredDataKey WRITABLE_BOOK_CONTENT = new StructuredDataKey<>("writable_book_content", Type.STRING_ARRAY); public static final StructuredDataKey WRITTEN_BOOK_CONTENT = new StructuredDataKey<>("written_book_content", WrittenBook.TYPE); - public static final StructuredDataKey TRIM = new StructuredDataKey<>("trim", Type.UNIT); // TODO + public static final StructuredDataKey TRIM = new StructuredDataKey<>("trim", Type.EMPTY); // TODO public static final StructuredDataKey DEBUG_STICK_STATE = new StructuredDataKey<>("debug_stick_state", Type.COMPOUND_TAG); public static final StructuredDataKey ENTITY_DATA = new StructuredDataKey<>("entity_data", Type.COMPOUND_TAG); public static final StructuredDataKey BUCKET_ENTITY_DATA = new StructuredDataKey<>("bucket_entity_data", Type.COMPOUND_TAG); public static final StructuredDataKey BLOCK_ENTITY_DATA = new StructuredDataKey<>("block_entity_data", Type.COMPOUND_TAG); - public static final StructuredDataKey INSTRUMENT = new StructuredDataKey<>("instrument", Type.UNIT); // TODO + public static final StructuredDataKey INSTRUMENT = new StructuredDataKey<>("instrument", Type.EMPTY); // TODO public static final StructuredDataKey RECIPES = new StructuredDataKey<>("recipes", Type.STRING_ARRAY); public static final StructuredDataKey LODESTONE_TARGET = new StructuredDataKey<>("lodestone_target", LodestoneTarget.TYPE); - public static final StructuredDataKey FIREWORK_EXPLOSION = new StructuredDataKey<>("firework_explosion", Type.UNIT); // TODO - public static final StructuredDataKey FIREWORKS = new StructuredDataKey<>("fireworks", Type.UNIT); // TODO - public static final StructuredDataKey PROFILE = new StructuredDataKey<>("profile", GameProfile.TYPE); + public static final StructuredDataKey FIREWORK_EXPLOSION = new StructuredDataKey<>("firework_explosion", FireworkExplosion.TYPE); + public static final StructuredDataKey FIREWORKS = new StructuredDataKey<>("fireworks", Fireworks.TYPE); + public static final StructuredDataKey PROFILE = new StructuredDataKey<>("profile", Type.GAME_PROFILE); public static final StructuredDataKey NOTE_BLOCK_SOUND = new StructuredDataKey<>("note_block_sound", Type.STRING); public static final StructuredDataKey BANNER_PATTERNS = new StructuredDataKey<>("banner_patterns", BannerPattern.ARRAY_TYPE); public static final StructuredDataKey BASE_COLOR = new StructuredDataKey<>("base_color", Type.VAR_INT); @@ -90,7 +96,7 @@ public final class StructuredDataKey { private final String identifier; private final Type type; - public StructuredDataKey(final String identifier, Type type) { + public StructuredDataKey(final String identifier, final Type type) { this.identifier = identifier; this.type = type; } diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/AdventureModePredicate.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/AdventureModePredicate.java new file mode 100644 index 000000000..454ad03c9 --- /dev/null +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/AdventureModePredicate.java @@ -0,0 +1,61 @@ +/* + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * Copyright (C) 2016-2024 ViaVersion and contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.viaversion.viaversion.api.minecraft.item.data; + +import com.viaversion.viaversion.api.type.Type; +import io.netty.buffer.ByteBuf; + +// This goes DEEEP +public final class AdventureModePredicate { + + public static final Type TYPE = new Type(AdventureModePredicate.class) { + @Override + public AdventureModePredicate read(final ByteBuf buffer) throws Exception { + final BlockPredicate[] predicates = BlockPredicate.ARRAY_TYPE.read(buffer); + final boolean showInTooltip = buffer.readBoolean(); + return new AdventureModePredicate(predicates, showInTooltip); + } + + @Override + public void write(final ByteBuf buffer, final AdventureModePredicate value) throws Exception { + BlockPredicate.ARRAY_TYPE.write(buffer, value.predicates); + buffer.writeBoolean(value.showInTooltip); + } + }; + + private final BlockPredicate[] predicates; + private final boolean showInTooltip; + + public AdventureModePredicate(final BlockPredicate[] predicates, final boolean showInTooltip) { + this.predicates = predicates; + this.showInTooltip = showInTooltip; + } + + public BlockPredicate[] predicates() { + return predicates; + } + + public boolean showInTooltip() { + return showInTooltip; + } +} diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/AttributeModifier.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/AttributeModifier.java new file mode 100644 index 000000000..5db6b6865 --- /dev/null +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/AttributeModifier.java @@ -0,0 +1,70 @@ +/* + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * Copyright (C) 2016-2024 ViaVersion and contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.viaversion.viaversion.api.minecraft.item.data; + +import com.viaversion.viaversion.api.type.Type; +import com.viaversion.viaversion.api.type.types.ArrayType; +import io.netty.buffer.ByteBuf; + +public final class AttributeModifier { + + public static final Type TYPE = new Type(AttributeModifier.class) { + @Override + public AttributeModifier read(final ByteBuf buffer) throws Exception { + final int attribute = Type.VAR_INT.readPrimitive(buffer); + final ModifierData modifier = ModifierData.TYPE.read(buffer); + final int slot = Type.VAR_INT.readPrimitive(buffer); + return new AttributeModifier(attribute, modifier, slot); + } + + @Override + public void write(final ByteBuf buffer, final AttributeModifier value) throws Exception { + Type.VAR_INT.writePrimitive(buffer, value.attribute); + ModifierData.TYPE.write(buffer, value.modifier); + Type.VAR_INT.writePrimitive(buffer, value.slot); + } + }; + public static final Type ARRAY_TYPE = new ArrayType<>(TYPE); + + private final int attribute; + private final ModifierData modifier; + private final int slot; + + public AttributeModifier(final int attribute, final ModifierData modifier, final int slot) { + this.attribute = attribute; + this.modifier = modifier; + this.slot = slot; + } + + public int attribute() { + return attribute; + } + + public ModifierData modifier() { + return modifier; + } + + public int slot() { + return slot; + } +} diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/AttributeModifiers.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/AttributeModifiers.java new file mode 100644 index 000000000..da2a3ff85 --- /dev/null +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/AttributeModifiers.java @@ -0,0 +1,60 @@ +/* + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * Copyright (C) 2016-2024 ViaVersion and contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.viaversion.viaversion.api.minecraft.item.data; + +import com.viaversion.viaversion.api.type.Type; +import io.netty.buffer.ByteBuf; + +public final class AttributeModifiers { + + public static final Type TYPE = new Type(AttributeModifiers.class) { + @Override + public AttributeModifiers read(final ByteBuf buffer) throws Exception { + final AttributeModifier[] modifiers = AttributeModifier.ARRAY_TYPE.read(buffer); + final boolean showInTooltip = buffer.readBoolean(); + return new AttributeModifiers(modifiers, showInTooltip); + } + + @Override + public void write(final ByteBuf buffer, final AttributeModifiers value) throws Exception { + AttributeModifier.ARRAY_TYPE.write(buffer, value.modifiers()); + buffer.writeBoolean(value.showInTooltip()); + } + }; + + private final AttributeModifier[] modifiers; + private final boolean showInTooltip; + + public AttributeModifiers(final AttributeModifier[] modifiers, final boolean showInTooltip) { + this.modifiers = modifiers; + this.showInTooltip = showInTooltip; + } + + public AttributeModifier[] modifiers() { + return modifiers; + } + + public boolean showInTooltip() { + return showInTooltip; + } +} diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/BlockPredicate.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/BlockPredicate.java new file mode 100644 index 000000000..32e4f0173 --- /dev/null +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/BlockPredicate.java @@ -0,0 +1,78 @@ +/* + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * Copyright (C) 2016-2024 ViaVersion and contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.viaversion.viaversion.api.minecraft.item.data; + +import com.github.steveice10.opennbt.tag.builtin.CompoundTag; +import com.viaversion.viaversion.api.minecraft.HolderSet; +import com.viaversion.viaversion.api.type.Type; +import com.viaversion.viaversion.api.type.types.ArrayType; +import io.netty.buffer.ByteBuf; +import org.checkerframework.checker.nullness.qual.Nullable; + +public final class BlockPredicate { + + public static final Type TYPE = new Type(BlockPredicate.class) { + @Override + public BlockPredicate read(final ByteBuf buffer) throws Exception { + final HolderSet holders = Type.OPTIONAL_HOLDER_SET.read(buffer); + final StatePropertyMatcher[] propertyMatchers = buffer.readBoolean() ? StatePropertyMatcher.ARRAY_TYPE.read(buffer) : null; + final CompoundTag tag = Type.OPTIONAL_COMPOUND_TAG.read(buffer); + return new BlockPredicate(holders, propertyMatchers, tag); + } + + @Override + public void write(final ByteBuf buffer, final BlockPredicate value) throws Exception { + Type.OPTIONAL_HOLDER_SET.write(buffer, value.holders); + + buffer.writeBoolean(value.propertyMatchers != null); + if (value.propertyMatchers != null) { + StatePropertyMatcher.ARRAY_TYPE.write(buffer, value.propertyMatchers); + } + + Type.OPTIONAL_COMPOUND_TAG.write(buffer, value.tag); + } + }; + public static final Type ARRAY_TYPE = new ArrayType<>(TYPE); + + private final HolderSet holders; + private final StatePropertyMatcher[] propertyMatchers; + private final CompoundTag tag; + + public BlockPredicate(@Nullable final HolderSet holders, final StatePropertyMatcher @Nullable [] propertyMatchers, @Nullable final CompoundTag tag) { + this.holders = holders; + this.propertyMatchers = propertyMatchers; + this.tag = tag; + } + + public @Nullable HolderSet predicates() { + return holders; + } + + public StatePropertyMatcher @Nullable [] propertyMatchers() { + return propertyMatchers; + } + + public @Nullable CompoundTag tag() { + return tag; + } +} diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/FireworkExplosion.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/FireworkExplosion.java new file mode 100644 index 000000000..72cf1e83f --- /dev/null +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/FireworkExplosion.java @@ -0,0 +1,85 @@ +/* + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * Copyright (C) 2016-2024 ViaVersion and contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.viaversion.viaversion.api.minecraft.item.data; + +import com.viaversion.viaversion.api.type.Type; +import com.viaversion.viaversion.api.type.types.ArrayType; +import io.netty.buffer.ByteBuf; + +public final class FireworkExplosion { + public static final Type TYPE = new Type(FireworkExplosion.class) { + @Override + public FireworkExplosion read(final ByteBuf buffer) throws Exception { + final int shape = Type.VAR_INT.readPrimitive(buffer); + final int[] colors = Type.INT_ARRAY_PRIMITIVE.read(buffer); + final int[] fadeColors = Type.INT_ARRAY_PRIMITIVE.read(buffer); + final boolean hasTrail = buffer.readBoolean(); + final boolean hasTwinkle = buffer.readBoolean(); + return new FireworkExplosion(shape, colors, fadeColors, hasTrail, hasTwinkle); + } + + @Override + public void write(final ByteBuf buffer, final FireworkExplosion value) throws Exception { + Type.VAR_INT.writePrimitive(buffer, value.shape); + Type.INT_ARRAY_PRIMITIVE.write(buffer, value.colors); + Type.INT_ARRAY_PRIMITIVE.write(buffer, value.fadeColors); + buffer.writeBoolean(value.hasTrail); + buffer.writeBoolean(value.hasTwinkle); + } + }; + public static final Type ARRAY_TYPE = new ArrayType<>(TYPE); + + private final int shape; + private final int[] colors; + private final int[] fadeColors; + private final boolean hasTrail; + private final boolean hasTwinkle; + + public FireworkExplosion(final int shape, final int[] colors, final int[] fadeColors, final boolean hasTrail, final boolean hasTwinkle) { + this.shape = shape; + this.colors = colors; + this.fadeColors = fadeColors; + this.hasTrail = hasTrail; + this.hasTwinkle = hasTwinkle; + } + + public int shape() { + return shape; + } + + public int[] colors() { + return colors; + } + + public int[] fadeColors() { + return fadeColors; + } + + public boolean hasTrail() { + return hasTrail; + } + + public boolean hasTwinkle() { + return hasTwinkle; + } +} \ No newline at end of file diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/Fireworks.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/Fireworks.java new file mode 100644 index 000000000..751792b4c --- /dev/null +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/Fireworks.java @@ -0,0 +1,60 @@ +/* + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * Copyright (C) 2016-2024 ViaVersion and contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.viaversion.viaversion.api.minecraft.item.data; + +import com.viaversion.viaversion.api.type.Type; +import io.netty.buffer.ByteBuf; + +public final class Fireworks { + + public static final Type TYPE = new Type(Fireworks.class) { + @Override + public Fireworks read(final ByteBuf buffer) throws Exception { + final int flightDuration = Type.VAR_INT.readPrimitive(buffer); + final FireworkExplosion[] explosions = FireworkExplosion.ARRAY_TYPE.read(buffer); + return new Fireworks(flightDuration, explosions); + } + + @Override + public void write(final ByteBuf buffer, final Fireworks value) throws Exception { + Type.VAR_INT.writePrimitive(buffer, value.flightDuration); + FireworkExplosion.ARRAY_TYPE.write(buffer, value.explosions); + } + }; + + private final FireworkExplosion[] explosions; + private final int flightDuration; + + public Fireworks(final int flightDuration, final FireworkExplosion[] explosions) { + this.flightDuration = flightDuration; + this.explosions = explosions; + } + + public int flightDuration() { + return flightDuration; + } + + public FireworkExplosion[] explosions() { + return explosions; + } +} diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/ModifierData.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/ModifierData.java new file mode 100644 index 000000000..60827ecf9 --- /dev/null +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/ModifierData.java @@ -0,0 +1,77 @@ +/* + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * Copyright (C) 2016-2024 ViaVersion and contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.viaversion.viaversion.api.minecraft.item.data; + +import com.viaversion.viaversion.api.type.Type; +import io.netty.buffer.ByteBuf; +import java.util.UUID; + +public final class ModifierData { + + public static final Type TYPE = new Type(ModifierData.class) { + @Override + public ModifierData read(final ByteBuf buffer) throws Exception { + final UUID uuid = Type.UUID.read(buffer); + final String name = Type.STRING.read(buffer); + final double amount = buffer.readDouble(); + final int operation = Type.VAR_INT.readPrimitive(buffer); + return new ModifierData(uuid, name, amount, operation); + } + + @Override + public void write(final ByteBuf buffer, final ModifierData value) throws Exception { + Type.UUID.write(buffer, value.uuid); + Type.STRING.write(buffer, value.name); + buffer.writeDouble(value.amount); + Type.VAR_INT.writePrimitive(buffer, value.operation); + } + }; + + private final UUID uuid; + private final String name; + private final double amount; + private final int operation; + + public ModifierData(final UUID uuid, final String name, final double amount, final int operation) { + this.uuid = uuid; + this.name = name; + this.amount = amount; + this.operation = operation; + } + + public UUID uuid() { + return uuid; + } + + public String name() { + return name; + } + + public double amount() { + return amount; + } + + public int operation() { + return operation; + } +} diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/PotionContents.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/PotionContents.java new file mode 100644 index 000000000..f9af9a6d7 --- /dev/null +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/PotionContents.java @@ -0,0 +1,77 @@ +/* + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * Copyright (C) 2016-2024 ViaVersion and contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.viaversion.viaversion.api.minecraft.item.data; + +import com.viaversion.viaversion.api.type.Type; +import io.netty.buffer.ByteBuf; +import org.checkerframework.checker.nullness.qual.Nullable; + +public final class PotionContents { + + public static final Type TYPE = new Type(PotionContents.class) { + @Override + public PotionContents read(final ByteBuf buffer) throws Exception { + final Integer potion = buffer.readBoolean() ? Type.VAR_INT.readPrimitive(buffer) : null; + final Integer customColor = buffer.readBoolean() ? buffer.readInt() : null; + final PotionEffect[] customEffects = PotionEffect.ARRAY_TYPE.read(buffer); + return new PotionContents(potion, customColor, customEffects); + } + + @Override + public void write(final ByteBuf buffer, final PotionContents value) throws Exception { + buffer.writeBoolean(value.potion != null); + if (value.potion != null) { + Type.VAR_INT.writePrimitive(buffer, value.potion); + } + + buffer.writeBoolean(value.customColor != null); + if (value.customColor != null) { + buffer.writeInt(value.customColor); + } + + PotionEffect.ARRAY_TYPE.write(buffer, value.customEffects); + } + }; + + private final Integer potion; + private final Integer customColor; + private final PotionEffect[] customEffects; + + public PotionContents(@Nullable final Integer potion, @Nullable final Integer customColor, final PotionEffect[] customEffects) { + this.potion = potion; + this.customColor = customColor; + this.customEffects = customEffects; + } + + public @Nullable Integer potion() { + return potion; + } + + public @Nullable Integer customColor() { + return customColor; + } + + public PotionEffect[] customEffects() { + return customEffects; + } +} diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/PotionEffect.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/PotionEffect.java new file mode 100644 index 000000000..59e05bc9f --- /dev/null +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/PotionEffect.java @@ -0,0 +1,62 @@ +/* + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * Copyright (C) 2016-2024 ViaVersion and contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.viaversion.viaversion.api.minecraft.item.data; + +import com.viaversion.viaversion.api.type.Type; +import com.viaversion.viaversion.api.type.types.ArrayType; +import io.netty.buffer.ByteBuf; + +public final class PotionEffect { + + public static final Type TYPE = new Type(PotionEffect.class) { + @Override + public PotionEffect read(final ByteBuf buffer) throws Exception { + final int effect = Type.VAR_INT.readPrimitive(buffer); + final PotionEffectData effectData = PotionEffectData.TYPE.read(buffer); + return new PotionEffect(effect, effectData); + } + + @Override + public void write(final ByteBuf buffer, final PotionEffect value) throws Exception { + Type.VAR_INT.writePrimitive(buffer, value.effect); + PotionEffectData.TYPE.write(buffer, value.effectData); + } + }; + public static final Type ARRAY_TYPE = new ArrayType<>(TYPE); + + private final int effect; + private final PotionEffectData effectData; + + public PotionEffect(final int effect, final PotionEffectData effectData) { + this.effect = effect; + this.effectData = effectData; + } + + public int effect() { + return effect; + } + + public PotionEffectData effectData() { + return effectData; + } +} diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/PotionEffectData.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/PotionEffectData.java new file mode 100644 index 000000000..fa25067f9 --- /dev/null +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/PotionEffectData.java @@ -0,0 +1,98 @@ +/* + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * Copyright (C) 2016-2024 ViaVersion and contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.viaversion.viaversion.api.minecraft.item.data; + +import com.viaversion.viaversion.api.type.OptionalType; +import com.viaversion.viaversion.api.type.Type; +import com.viaversion.viaversion.api.type.types.ArrayType; +import io.netty.buffer.ByteBuf; +import org.checkerframework.checker.nullness.qual.Nullable; + +public final class PotionEffectData { + + public static final Type TYPE = new Type(PotionEffectData.class) { + @Override + public PotionEffectData read(final ByteBuf buffer) throws Exception { + final int amplifier = Type.VAR_INT.readPrimitive(buffer); + final int duration = Type.VAR_INT.readPrimitive(buffer); + final boolean ambient = buffer.readBoolean(); + final boolean showParticles = buffer.readBoolean(); + final boolean showIcon = buffer.readBoolean(); + final PotionEffectData hiddenEffect = OPTIONAL_TYPE.read(buffer); + return new PotionEffectData(amplifier, duration, ambient, showParticles, showIcon, hiddenEffect); + } + + @Override + public void write(final ByteBuf buffer, final PotionEffectData value) throws Exception { + Type.VAR_INT.writePrimitive(buffer, value.amplifier); + Type.VAR_INT.writePrimitive(buffer, value.duration); + buffer.writeBoolean(value.ambient); + buffer.writeBoolean(value.showParticles); + buffer.writeBoolean(value.showIcon); + OPTIONAL_TYPE.write(buffer, value.hiddenEffect); + } + }; + public static final Type OPTIONAL_TYPE = new OptionalType(TYPE) { + }; + + private final int amplifier; + private final int duration; + private final boolean ambient; + private final boolean showParticles; + private final boolean showIcon; + private final PotionEffectData hiddenEffect; // RECURSIVESIVESIVESIVESIVESIVESIVESIVESIVESIVESIVESIVESIVESIVESIVESIVESIVESIVESIVESIVESIVESIVESIVESIVESIVESIVESIVESIVESIVESIVESIVESIVESIVESIVESIVESIVESIVESIVESIVESIVESIVESIVESIVESIVESIVE + + public PotionEffectData(final int amplifier, final int duration, final boolean ambient, final boolean showParticles, + final boolean showIcon, @Nullable final PotionEffectData hiddenEffect) { + this.amplifier = amplifier; + this.duration = duration; + this.ambient = ambient; + this.showParticles = showParticles; + this.showIcon = showIcon; + this.hiddenEffect = hiddenEffect; + } + + public int amplifier() { + return amplifier; + } + + public int duration() { + return duration; + } + + public boolean ambient() { + return ambient; + } + + public boolean showParticles() { + return showParticles; + } + + public boolean showIcon() { + return showIcon; + } + + public @Nullable PotionEffectData hiddenEffect() { + return hiddenEffect; + } +} diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/StatePropertyMatcher.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/StatePropertyMatcher.java new file mode 100644 index 000000000..b4cb23e7a --- /dev/null +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/StatePropertyMatcher.java @@ -0,0 +1,96 @@ +/* + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * Copyright (C) 2016-2024 ViaVersion and contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.viaversion.viaversion.api.minecraft.item.data; + +import com.viaversion.viaversion.api.type.Type; +import com.viaversion.viaversion.api.type.types.ArrayType; +import com.viaversion.viaversion.util.Either; +import io.netty.buffer.ByteBuf; +import org.checkerframework.checker.nullness.qual.Nullable; + +public final class StatePropertyMatcher { + + // TODO Abstract Either reading + public static final Type TYPE = new Type(StatePropertyMatcher.class) { + @Override + public StatePropertyMatcher read(final ByteBuf buffer) throws Exception { + final String name = Type.STRING.read(buffer); + if (buffer.readBoolean()) { + final String value = Type.STRING.read(buffer); + return new StatePropertyMatcher(name, Either.left(value)); + } else { + final String minValue = Type.OPTIONAL_STRING.read(buffer); + final String maxValue = Type.OPTIONAL_STRING.read(buffer); + return new StatePropertyMatcher(name, Either.right(new RangedMatcher(minValue, maxValue))); + } + } + + @Override + public void write(final ByteBuf buffer, final StatePropertyMatcher value) throws Exception { + Type.STRING.write(buffer, value.name); + if (value.matcher.isLeft()) { + buffer.writeBoolean(true); + Type.STRING.write(buffer, value.matcher.left()); + } else { + buffer.writeBoolean(false); + Type.OPTIONAL_STRING.write(buffer, value.matcher.right().minValue()); + Type.OPTIONAL_STRING.write(buffer, value.matcher.right().maxValue()); + } + } + }; + public static final Type ARRAY_TYPE = new ArrayType<>(TYPE); + + private final String name; + private final Either matcher; + + public StatePropertyMatcher(final String name, final Either matcher) { + this.name = name; + this.matcher = matcher; + } + + public String name() { + return name; + } + + public Either matcher() { + return matcher; + } + + public static final class RangedMatcher { + private final String minValue; + private final String maxValue; + + public RangedMatcher(@Nullable final String minValue, @Nullable final String maxValue) { + this.minValue = minValue; + this.maxValue = maxValue; + } + + public String minValue() { + return minValue; + } + + public String maxValue() { + return maxValue; + } + } +} diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/SuspiciousStewEffect.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/SuspiciousStewEffect.java new file mode 100644 index 000000000..52855ee48 --- /dev/null +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/SuspiciousStewEffect.java @@ -0,0 +1,62 @@ +/* + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * Copyright (C) 2016-2024 ViaVersion and contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.viaversion.viaversion.api.minecraft.item.data; + +import com.viaversion.viaversion.api.type.Type; +import com.viaversion.viaversion.api.type.types.ArrayType; +import io.netty.buffer.ByteBuf; + +public final class SuspiciousStewEffect { + + public static final Type TYPE = new Type(SuspiciousStewEffect.class) { + @Override + public SuspiciousStewEffect read(final ByteBuf buffer) { + final int effect = Type.VAR_INT.readPrimitive(buffer); + final int duration = Type.VAR_INT.readPrimitive(buffer); + return new SuspiciousStewEffect(effect, duration); + } + + @Override + public void write(final ByteBuf buffer, final SuspiciousStewEffect value) { + Type.VAR_INT.writePrimitive(buffer, value.effect); + Type.VAR_INT.writePrimitive(buffer, value.duration); + } + }; + public static final Type ARRAY_TYPE = new ArrayType<>(TYPE); + + private final int effect; + private final int duration; + + public SuspiciousStewEffect(final int effect, final int duration) { + this.effect = effect; + this.duration = duration; + } + + public int mobEffect() { + return effect; + } + + public int duration() { + return duration; + } +} diff --git a/api/src/main/java/com/viaversion/viaversion/api/type/Type.java b/api/src/main/java/com/viaversion/viaversion/api/type/Type.java index b42566ec0..821f368a2 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/type/Type.java +++ b/api/src/main/java/com/viaversion/viaversion/api/type/Type.java @@ -27,7 +27,9 @@ import com.github.steveice10.opennbt.tag.builtin.Tag; import com.google.gson.JsonElement; import com.viaversion.viaversion.api.minecraft.BlockChangeRecord; import com.viaversion.viaversion.api.minecraft.EulerAngle; +import com.viaversion.viaversion.api.minecraft.GameProfile; import com.viaversion.viaversion.api.minecraft.GlobalPosition; +import com.viaversion.viaversion.api.minecraft.HolderSet; import com.viaversion.viaversion.api.minecraft.PlayerMessageSignature; import com.viaversion.viaversion.api.minecraft.Position; import com.viaversion.viaversion.api.minecraft.ProfileKey; @@ -47,6 +49,7 @@ import com.viaversion.viaversion.api.type.types.ByteType; import com.viaversion.viaversion.api.type.types.ComponentType; import com.viaversion.viaversion.api.type.types.DoubleType; import com.viaversion.viaversion.api.type.types.FloatType; +import com.viaversion.viaversion.api.type.types.IntArrayType; import com.viaversion.viaversion.api.type.types.IntType; import com.viaversion.viaversion.api.type.types.LongArrayType; import com.viaversion.viaversion.api.type.types.LongType; @@ -56,7 +59,7 @@ import com.viaversion.viaversion.api.type.types.ShortByteArrayType; import com.viaversion.viaversion.api.type.types.ShortType; import com.viaversion.viaversion.api.type.types.StringType; import com.viaversion.viaversion.api.type.types.UUIDType; -import com.viaversion.viaversion.api.type.types.UnitType; +import com.viaversion.viaversion.api.type.types.EmptyType; import com.viaversion.viaversion.api.type.types.UnsignedByteType; import com.viaversion.viaversion.api.type.types.UnsignedShortType; import com.viaversion.viaversion.api.type.types.VarIntArrayType; @@ -76,6 +79,8 @@ import com.viaversion.viaversion.api.type.types.math.GlobalPositionType; import com.viaversion.viaversion.api.type.types.item.ItemType1_20_2; import com.viaversion.viaversion.api.type.types.item.ItemShortArrayType1_8; import com.viaversion.viaversion.api.type.types.item.ItemType1_8; +import com.viaversion.viaversion.api.type.types.misc.GameProfileType; +import com.viaversion.viaversion.api.type.types.misc.HolderSetType; import com.viaversion.viaversion.api.type.types.misc.NamedCompoundTagType; import com.viaversion.viaversion.api.type.types.OptionalVarIntType; import com.viaversion.viaversion.api.type.types.misc.PlayerMessageSignatureType; @@ -98,7 +103,7 @@ import java.util.UUID; */ public abstract class Type implements ByteBufReader, ByteBufWriter { - public static final Type UNIT = new UnitType(); + public static final Type EMPTY = new EmptyType(); public static final ByteType BYTE = new ByteType(); public static final UnsignedByteType UNSIGNED_BYTE = new UnsignedByteType(); @@ -106,6 +111,7 @@ public abstract class Type implements ByteBufReader, ByteBufWriter { public static final Type OPTIONAL_BYTE_ARRAY_PRIMITIVE = new ByteArrayType.OptionalByteArrayType(); public static final Type SHORT_BYTE_ARRAY = new ShortByteArrayType(); public static final Type REMAINING_BYTES = new RemainingBytesType(); + public static final Type INT_ARRAY_PRIMITIVE = new IntArrayType(); public static final ShortType SHORT = new ShortType(); public static final UnsignedShortType UNSIGNED_SHORT = new UnsignedShortType(); @@ -174,6 +180,7 @@ public abstract class Type implements ByteBufReader, ByteBufWriter { public static final Type VILLAGER_DATA = new VillagerDataType(); + public static final Type GAME_PROFILE = new GameProfileType(); public static final Type PROFILE_KEY = new ProfileKeyType(); public static final Type OPTIONAL_PROFILE_KEY = new ProfileKeyType.OptionalProfileKeyType(); @@ -188,6 +195,9 @@ public abstract class Type implements ByteBufReader, ByteBufWriter { public static final Type REGISTRY_ENTRY = new RegistryEntryType(); public static final Type REGISTRY_ENTRY_ARRAY = new ArrayType<>(REGISTRY_ENTRY); + public static final Type HOLDER_SET = new HolderSetType(); + public static final Type OPTIONAL_HOLDER_SET = new HolderSetType.OptionalHolderSetType(); + public static final Type ITEM1_8 = new ItemType1_8(); public static final Type ITEM1_13 = new ItemType1_13(); public static final Type ITEM1_13_2 = new ItemType1_13_2(); diff --git a/api/src/main/java/com/viaversion/viaversion/api/type/types/UnitType.java b/api/src/main/java/com/viaversion/viaversion/api/type/types/EmptyType.java similarity index 95% rename from api/src/main/java/com/viaversion/viaversion/api/type/types/UnitType.java rename to api/src/main/java/com/viaversion/viaversion/api/type/types/EmptyType.java index 0e9de32a2..a54b4a71d 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/type/types/UnitType.java +++ b/api/src/main/java/com/viaversion/viaversion/api/type/types/EmptyType.java @@ -26,9 +26,9 @@ import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.util.Unit; import io.netty.buffer.ByteBuf; -public final class UnitType extends Type { +public final class EmptyType extends Type { - public UnitType() { + public EmptyType() { super(Unit.class); } diff --git a/api/src/main/java/com/viaversion/viaversion/api/type/types/IntArrayType.java b/api/src/main/java/com/viaversion/viaversion/api/type/types/IntArrayType.java new file mode 100644 index 000000000..bc6d4adff --- /dev/null +++ b/api/src/main/java/com/viaversion/viaversion/api/type/types/IntArrayType.java @@ -0,0 +1,66 @@ +/* + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * Copyright (C) 2016-2024 ViaVersion and contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.viaversion.viaversion.api.type.types; + +import com.google.common.base.Preconditions; +import com.viaversion.viaversion.api.type.OptionalType; +import com.viaversion.viaversion.api.type.Type; +import io.netty.buffer.ByteBuf; + +public class IntArrayType extends Type { + + private final int length; + + public IntArrayType(final int length) { + super(int[].class); + this.length = length; + } + + public IntArrayType() { + super(int[].class); + this.length = -1; + } + + @Override + public void write(final ByteBuf buffer, final int[] object) throws Exception { + if (this.length != -1) { + Preconditions.checkArgument(length == object.length, "Length does not match expected length"); + } else { + Type.VAR_INT.writePrimitive(buffer, object.length); + } + for (final int i : object) { + buffer.writeInt(i); + } + } + + @Override + public int[] read(final ByteBuf buffer) throws Exception { + final int length = this.length == -1 ? Type.VAR_INT.readPrimitive(buffer) : this.length; + Preconditions.checkArgument(buffer.isReadable(length), "Length is fewer than readable bytes"); + final int[] array = new int[length]; + for (int i = 0; i < length; i++) { + array[i] = buffer.readInt(); + } + return array; + } +} diff --git a/api/src/main/java/com/viaversion/viaversion/api/type/types/misc/GameProfileType.java b/api/src/main/java/com/viaversion/viaversion/api/type/types/misc/GameProfileType.java new file mode 100644 index 000000000..d6d6f5035 --- /dev/null +++ b/api/src/main/java/com/viaversion/viaversion/api/type/types/misc/GameProfileType.java @@ -0,0 +1,61 @@ +/* + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * Copyright (C) 2016-2024 ViaVersion and contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.viaversion.viaversion.api.type.types.misc; + +import com.viaversion.viaversion.api.minecraft.GameProfile; +import com.viaversion.viaversion.api.type.Type; +import io.netty.buffer.ByteBuf; + +public final class GameProfileType extends Type { + + public GameProfileType() { + super(GameProfile.class); + } + + @Override + public GameProfile read(final ByteBuf buffer) throws Exception { + final String name = Type.STRING.read(buffer); + final java.util.UUID id = Type.OPTIONAL_UUID.read(buffer); + final int propertyCount = Type.VAR_INT.readPrimitive(buffer); + final GameProfile.Property[] properties = new GameProfile.Property[propertyCount]; + for (int i = 0; i < propertyCount; i++) { + final String propertyName = Type.STRING.read(buffer); + final String propertyValue = Type.STRING.read(buffer); + final String propertySignature = Type.OPTIONAL_STRING.read(buffer); + properties[i] = new GameProfile.Property(propertyName, propertyValue, propertySignature); + } + return new GameProfile(name, id, properties); + } + + @Override + public void write(final ByteBuf buffer, final GameProfile value) throws Exception { + Type.STRING.write(buffer, value.name()); + Type.OPTIONAL_UUID.write(buffer, value.id()); + Type.VAR_INT.writePrimitive(buffer, value.properties().length); + for (final GameProfile.Property property : value.properties()) { + Type.STRING.write(buffer, property.name()); + Type.STRING.write(buffer, property.value()); + Type.OPTIONAL_STRING.write(buffer, property.signature()); + } + } +} diff --git a/api/src/main/java/com/viaversion/viaversion/api/type/types/misc/HolderSetType.java b/api/src/main/java/com/viaversion/viaversion/api/type/types/misc/HolderSetType.java new file mode 100644 index 000000000..f9a65e0ac --- /dev/null +++ b/api/src/main/java/com/viaversion/viaversion/api/type/types/misc/HolderSetType.java @@ -0,0 +1,71 @@ +/* + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * Copyright (C) 2016-2024 ViaVersion and contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.viaversion.viaversion.api.type.types.misc; + +import com.viaversion.viaversion.api.minecraft.HolderSet; +import com.viaversion.viaversion.api.type.OptionalType; +import com.viaversion.viaversion.api.type.Type; +import io.netty.buffer.ByteBuf; + +public class HolderSetType extends Type { + + public HolderSetType() { + super(HolderSet.class); + } + + @Override + public HolderSet read(final ByteBuf buffer) throws Exception { + final int size = Type.VAR_INT.readPrimitive(buffer); + if (size == -1) { + final String tag = Type.STRING.read(buffer); + return new HolderSet(tag); + } + + final int[] values = new int[size]; + for (int i = 0; i < size; i++) { + values[i] = Type.VAR_INT.readPrimitive(buffer); + } + return new HolderSet(values); + } + + @Override + public void write(final ByteBuf buffer, final HolderSet object) throws Exception { + if (object.values().isLeft()) { + Type.VAR_INT.writePrimitive(buffer, -1); + Type.STRING.write(buffer, object.values().left()); + } else { + final int[] values = object.values().right(); + Type.VAR_INT.writePrimitive(buffer, values.length); + for (final int value : values) { + Type.VAR_INT.writePrimitive(buffer, value); + } + } + } + + public static final class OptionalHolderSetType extends OptionalType { + + public OptionalHolderSetType() { + super(Type.HOLDER_SET); + } + } +} diff --git a/api/src/main/java/com/viaversion/viaversion/util/Either.java b/api/src/main/java/com/viaversion/viaversion/util/Either.java new file mode 100644 index 000000000..55a2d4e9f --- /dev/null +++ b/api/src/main/java/com/viaversion/viaversion/util/Either.java @@ -0,0 +1,85 @@ +/* + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * Copyright (C) 2016-2024 ViaVersion and contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +package com.viaversion.viaversion.util; + +import com.google.common.base.Preconditions; +import java.util.Objects; +import org.checkerframework.checker.nullness.qual.Nullable; + +public final class Either { + private final X left; + private final Y right; + + private Either(final X left, final Y value) { + this.left = left; + this.right = value; + Preconditions.checkArgument(left == null || value == null, "Either.left and Either.right are both present"); + } + + public static Either left(final X left) { + Preconditions.checkNotNull(left); + return new Either<>(left, null); + } + + public static Either right(final Y right) { + Preconditions.checkNotNull(right); + return new Either<>(null, right); + } + + public boolean isLeft() { + return left != null; + } + + public boolean isRight() { + return right != null; + } + + public @Nullable X left() { + return left; + } + + public @Nullable Y right() { + return right; + } + + @Override + public String toString() { + return "Either{" + left + ", " + right + '}'; + } + + @Override + public boolean equals(final Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + final Either pair = (Either) o; + if (!Objects.equals(left, pair.left)) return false; + return Objects.equals(right, pair.right); + } + + @Override + public int hashCode() { + int result = left != null ? left.hashCode() : 0; + result = 31 * result + (right != null ? right.hashCode() : 0); + return result; + } +} diff --git a/common/src/main/java/com/viaversion/viaversion/rewriter/StructuredItemRewriter.java b/common/src/main/java/com/viaversion/viaversion/rewriter/StructuredItemRewriter.java index 161a3b360..3260e41b0 100644 --- a/common/src/main/java/com/viaversion/viaversion/rewriter/StructuredItemRewriter.java +++ b/common/src/main/java/com/viaversion/viaversion/rewriter/StructuredItemRewriter.java @@ -55,7 +55,7 @@ public class StructuredItemRewriter