From 50ca68e52ab3f6d43c5cf05e9f4b8b5f06874c60 Mon Sep 17 00:00:00 2001 From: Sollace Date: Tue, 1 Oct 2024 19:37:57 +0100 Subject: [PATCH] Create a component for sunglasses breaking --- .../com/minelittlepony/unicopia/USounds.java | 2 +- .../providers/tag/UEntityTypeTagProvider.java | 2 +- .../unicopia/entity/Living.java | 21 ++++++--- .../minelittlepony/unicopia/item/UItems.java | 11 ++++- .../component/BreaksIntoItemComponent.java | 43 +++++++++++++++++++ .../item/component/UDataComponentTypes.java | 1 + 6 files changed, 70 insertions(+), 10 deletions(-) create mode 100644 src/main/java/com/minelittlepony/unicopia/item/component/BreaksIntoItemComponent.java diff --git a/src/main/java/com/minelittlepony/unicopia/USounds.java b/src/main/java/com/minelittlepony/unicopia/USounds.java index 88dcf351..cd9fa8f7 100644 --- a/src/main/java/com/minelittlepony/unicopia/USounds.java +++ b/src/main/java/com/minelittlepony/unicopia/USounds.java @@ -102,7 +102,7 @@ public interface USounds { SoundEvent ITEM_ROCK_LAND = BLOCK_STONE_HIT; RegistryEntry.Reference ITEM_MUFFIN_BOUNCE = BLOCK_NOTE_BLOCK_BANJO; - SoundEvent ITEM_SUNGLASSES_SHATTER = BLOCK_GLASS_BREAK; + RegistryEntry ITEM_SUNGLASSES_SHATTER = Registries.SOUND_EVENT.getEntry(BLOCK_GLASS_BREAK); SoundEvent ITEM_APPLE_ROT = register("item.apple.rot"); SoundEvent ITEM_BRACELET_SIGN = register("item.bracelet.sign"); diff --git a/src/main/java/com/minelittlepony/unicopia/datagen/providers/tag/UEntityTypeTagProvider.java b/src/main/java/com/minelittlepony/unicopia/datagen/providers/tag/UEntityTypeTagProvider.java index 9cc5741b..41be4587 100644 --- a/src/main/java/com/minelittlepony/unicopia/datagen/providers/tag/UEntityTypeTagProvider.java +++ b/src/main/java/com/minelittlepony/unicopia/datagen/providers/tag/UEntityTypeTagProvider.java @@ -26,7 +26,7 @@ public class UEntityTypeTagProvider extends FabricTagProvider> { EntityType.CREEPER, EntityType.VILLAGER, EntityType.WANDERING_TRADER, EntityType.PILLAGER, EntityType.ILLUSIONER, EntityType.EVOKER, EntityType.WITCH, EntityType.TURTLE, - EntityType.BLAZE, //TODO: 1.20.5 EntityType.BREEZE, + EntityType.BLAZE, EntityType.BREEZE, EntityType.SHEEP, EntityType.PIG, EntityType.GOAT, EntityType.RABBIT, EntityType.POLAR_BEAR, EntityType.PANDA, EntityType.COW, EntityType.MOOSHROOM, diff --git a/src/main/java/com/minelittlepony/unicopia/entity/Living.java b/src/main/java/com/minelittlepony/unicopia/entity/Living.java index b882b4b6..55ed1370 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/Living.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/Living.java @@ -31,6 +31,8 @@ import com.minelittlepony.unicopia.input.Heuristic; import com.minelittlepony.unicopia.input.Interactable; import com.minelittlepony.unicopia.item.GlassesItem; import com.minelittlepony.unicopia.item.UItems; +import com.minelittlepony.unicopia.item.component.BreaksIntoItemComponent; +import com.minelittlepony.unicopia.item.component.UDataComponentTypes; import com.minelittlepony.unicopia.item.enchantment.EnchantmentUtil; import com.minelittlepony.unicopia.item.enchantment.UEnchantments; import com.minelittlepony.unicopia.network.track.DataTracker; @@ -381,13 +383,18 @@ public abstract class Living implements Equine, Caste this.attacker = attacker; } - if (magical.isIn(UTags.DamageTypes.BREAKS_SUNGLASSES)) { - ItemStack glasses = GlassesItem.getForEntity(entity).stack(); - if (glasses.isOf(UItems.SUNGLASSES)) { - // TODO: BreaksIntoItemComponent - ItemStack broken = glasses.withItem(UItems.BROKEN_SUNGLASSES); - TrinketsDelegate.getInstance(entity).setEquippedStack(entity, TrinketsDelegate.FACE, broken); - playSound(USounds.ITEM_SUNGLASSES_SHATTER, 1, 1); + ItemStack glasses = GlassesItem.getForEntity(entity).stack(); + BreaksIntoItemComponent afterBroken = glasses.get(UDataComponentTypes.ITEM_AFTER_BREAKING); + + if (afterBroken != null && afterBroken.damageType().contains(magical.getTypeRegistryEntry())) { + if (afterBroken != null) { + afterBroken.getItemAfterBreaking().ifPresent(b -> { + ItemStack broken = glasses.withItem(b); + TrinketsDelegate.getInstance(entity).setEquippedStack(entity, TrinketsDelegate.FACE, broken); + afterBroken.getBreakingSound().ifPresent(sound -> { + playSound(USounds.ITEM_SUNGLASSES_SHATTER, 1, 1); + }); + }); } } } diff --git a/src/main/java/com/minelittlepony/unicopia/item/UItems.java b/src/main/java/com/minelittlepony/unicopia/item/UItems.java index 8899241f..b2dc0746 100644 --- a/src/main/java/com/minelittlepony/unicopia/item/UItems.java +++ b/src/main/java/com/minelittlepony/unicopia/item/UItems.java @@ -8,6 +8,7 @@ import com.minelittlepony.unicopia.block.cloud.CloudBedBlock; import com.minelittlepony.unicopia.entity.mob.AirBalloonEntity; import com.minelittlepony.unicopia.entity.mob.UEntities; import com.minelittlepony.unicopia.item.cloud.CloudBedItem; +import com.minelittlepony.unicopia.item.component.BreaksIntoItemComponent; import com.minelittlepony.unicopia.item.component.UDataComponentTypes; import com.minelittlepony.unicopia.item.enchantment.UEnchantments; import com.minelittlepony.unicopia.item.group.ItemGroupRegistry; @@ -33,6 +34,7 @@ import net.minecraft.util.Rarity; import net.minecraft.util.UseAction; import net.minecraft.registry.Registry; import net.minecraft.registry.RegistryKey; +import net.minecraft.registry.RegistryKeys; import net.minecraft.registry.Registries; public interface UItems { @@ -225,7 +227,14 @@ public interface UItems { .maxDamage(16) .rarity(Rarity.UNCOMMON), 0), ItemGroups.TOOLS); - GlassesItem SUNGLASSES = register("sunglasses", new GlassesItem(new Item.Settings().maxCount(1)), ItemGroups.COMBAT); + GlassesItem SUNGLASSES = register("sunglasses", new GlassesItem(new Item.Settings() + .maxCount(1) + .component(UDataComponentTypes.ITEM_AFTER_BREAKING, new BreaksIntoItemComponent( + UTags.DamageTypes.BREAKS_SUNGLASSES, + RegistryKey.of(RegistryKeys.ITEM, Unicopia.id("broken_sunglasses")), + USounds.ITEM_SUNGLASSES_SHATTER.getKey().get()) + ) + ), ItemGroups.COMBAT); GlassesItem BROKEN_SUNGLASSES = register("broken_sunglasses", new GlassesItem(new Item.Settings().maxCount(1)), ItemGroups.COMBAT); Item CLAM_SHELL = register("clam_shell", new Item(new Item.Settings()), ItemGroups.INGREDIENTS); diff --git a/src/main/java/com/minelittlepony/unicopia/item/component/BreaksIntoItemComponent.java b/src/main/java/com/minelittlepony/unicopia/item/component/BreaksIntoItemComponent.java new file mode 100644 index 00000000..c5d60941 --- /dev/null +++ b/src/main/java/com/minelittlepony/unicopia/item/component/BreaksIntoItemComponent.java @@ -0,0 +1,43 @@ +package com.minelittlepony.unicopia.item.component; + +import java.util.Optional; + +import com.mojang.serialization.Codec; +import com.mojang.serialization.codecs.RecordCodecBuilder; + +import net.minecraft.entity.damage.DamageType; +import net.minecraft.item.Item; +import net.minecraft.network.RegistryByteBuf; +import net.minecraft.network.codec.PacketCodec; +import net.minecraft.registry.Registries; +import net.minecraft.registry.RegistryKey; +import net.minecraft.registry.RegistryKeys; +import net.minecraft.registry.tag.TagKey; +import net.minecraft.sound.SoundEvent; +import net.minecraft.util.Identifier; + +public record BreaksIntoItemComponent( + TagKey damageType, + RegistryKey itemAfterBreaking, + RegistryKey breakingSound + ) { + public static final Codec CODEC = RecordCodecBuilder.create(instance -> instance.group( + TagKey.codec(RegistryKeys.DAMAGE_TYPE).fieldOf("damage_type").forGetter(BreaksIntoItemComponent::damageType), + RegistryKey.createCodec(RegistryKeys.ITEM).fieldOf("item_after_breaking").forGetter(BreaksIntoItemComponent::itemAfterBreaking), + RegistryKey.createCodec(RegistryKeys.SOUND_EVENT).fieldOf("breaking_sound").forGetter(BreaksIntoItemComponent::breakingSound) + ).apply(instance, BreaksIntoItemComponent::new)); + public static final PacketCodec PACKET_CODEC = PacketCodec.tuple( + Identifier.PACKET_CODEC.xmap(id -> TagKey.of(RegistryKeys.DAMAGE_TYPE, id), key -> key.id()), BreaksIntoItemComponent::damageType, + RegistryKey.createPacketCodec(RegistryKeys.ITEM), BreaksIntoItemComponent::itemAfterBreaking, + RegistryKey.createPacketCodec(RegistryKeys.SOUND_EVENT), BreaksIntoItemComponent::breakingSound, + BreaksIntoItemComponent::new + ); + + public Optional getItemAfterBreaking() { + return Registries.ITEM.getOrEmpty(itemAfterBreaking()); + } + + public Optional getBreakingSound() { + return Registries.SOUND_EVENT.getOrEmpty(breakingSound); + } +} diff --git a/src/main/java/com/minelittlepony/unicopia/item/component/UDataComponentTypes.java b/src/main/java/com/minelittlepony/unicopia/item/component/UDataComponentTypes.java index 169353c7..434757e9 100644 --- a/src/main/java/com/minelittlepony/unicopia/item/component/UDataComponentTypes.java +++ b/src/main/java/com/minelittlepony/unicopia/item/component/UDataComponentTypes.java @@ -27,6 +27,7 @@ public interface UDataComponentTypes { ComponentType CHARGES = register("charges", builder -> builder.codec(Charges.CODEC).packetCodec(Charges.PACKET_CODEC)); ComponentType APPEARANCE = register("appearance", builder -> builder.codec(Appearance.CODEC).packetCodec(Appearance.PACKET_CODEC)); ComponentType DIET_PROFILE = register("diet_profile", builder -> builder.codec(DietProfile.CODEC).packetCodec(DietProfile.PACKET_CODEC)); + ComponentType ITEM_AFTER_BREAKING = register("item_after_breaking", builder -> builder.codec(BreaksIntoItemComponent.CODEC).packetCodec(BreaksIntoItemComponent.PACKET_CODEC)); private static ComponentType register(String name, UnaryOperator> builderOperator) { return Registry.register(Registries.DATA_COMPONENT_TYPE, Unicopia.id(name), builderOperator.apply(ComponentType.builder()).build());