Skip to content

Commit

Permalink
Give custom names for goomba variants spawn eggs
Browse files Browse the repository at this point in the history
  • Loading branch information
Hugman76 committed Sep 30, 2024
1 parent abbd053 commit 58ff207
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 26 deletions.
33 changes: 21 additions & 12 deletions src/main/java/fr/hugman/mubble/entity/GoombaVariant.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import fr.hugman.mubble.item.MubbleItems;
import fr.hugman.mubble.registry.MubbleRegistryKeys;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.attribute.EntityAttribute;
import net.minecraft.item.ItemStack;
import net.minecraft.network.RegistryByteBuf;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.network.codec.PacketCodecs;
Expand All @@ -23,36 +26,36 @@ public class GoombaVariant {
TextCodecs.CODEC.optionalFieldOf("name").forGetter(v -> v.name),
Identifier.CODEC.fieldOf("texture").forGetter(v -> v.texture),
Identifier.CODEC.fieldOf("surprised_texture").forGetter(v -> v.surprisedTexture),
Codec.unboundedMap(EntityAttribute.CODEC, Codec.DOUBLE).optionalFieldOf("base_attribute_values", Map.of()).forGetter(v -> v.baseAttributes)
Codec.unboundedMap(EntityAttribute.CODEC, Codec.DOUBLE).optionalFieldOf("base_attribute_values", Map.of()).forGetter(v -> v.baseAttributes),
TextCodecs.CODEC.optionalFieldOf("spawn_egg_name").forGetter(v -> v.spawnEggName)
).apply(instance, GoombaVariant::new));
public static final Codec<RegistryEntry<GoombaVariant>> ENTRY_CODEC = RegistryElementCodec.of(MubbleRegistryKeys.GOOMBA_VARIANT, CODEC);

public static final PacketCodec<RegistryByteBuf, GoombaVariant> PACKET_CODEC = PacketCodec.tuple(
TextCodecs.OPTIONAL_UNLIMITED_REGISTRY_PACKET_CODEC, (v -> v.name),
Identifier.PACKET_CODEC, (v -> v.texture),
Identifier.PACKET_CODEC, (v -> v.surprisedTexture),
PacketCodecs.map(Object2ObjectOpenHashMap::new, EntityAttribute.PACKET_CODEC, PacketCodecs.DOUBLE), (v -> v.baseAttributes),
TextCodecs.OPTIONAL_UNLIMITED_REGISTRY_PACKET_CODEC, (v -> v.spawnEggName),
GoombaVariant::new
);

public static final Codec<RegistryEntry<GoombaVariant>> ENTRY_CODEC = RegistryElementCodec.of(MubbleRegistryKeys.GOOMBA_VARIANT, CODEC);

public static final PacketCodec<RegistryByteBuf, RegistryEntry<GoombaVariant>> ENTRY_PACKET_CODEC = PacketCodecs.registryEntry(
MubbleRegistryKeys.GOOMBA_VARIANT, PACKET_CODEC
);
public static final PacketCodec<RegistryByteBuf, RegistryEntry<GoombaVariant>> ENTRY_PACKET_CODEC = PacketCodecs.registryEntry(MubbleRegistryKeys.GOOMBA_VARIANT, PACKET_CODEC);

private final Optional<Text> name;
private final Identifier texture;
private final Identifier surprisedTexture;
private final Map<RegistryEntry<EntityAttribute>, Double> baseAttributes;
private final Optional<Text> spawnEggName;

private final Identifier texturePath;
private final Identifier surprisedTexturePath;

public GoombaVariant(Optional<Text> name, Identifier texture, Identifier surprisedTexture, Map<RegistryEntry<EntityAttribute>, Double> baseAttributes) {
public GoombaVariant(Optional<Text> name, Identifier texture, Identifier surprisedTexture, Map<RegistryEntry<EntityAttribute>, Double> baseAttributes, Optional<Text> spawnEggName) {
this.name = name;
this.texture = texture;
this.surprisedTexture = surprisedTexture;
this.baseAttributes = baseAttributes;
this.spawnEggName = spawnEggName;

this.texturePath = getTexturePath(texture);
this.surprisedTexturePath = getTexturePath(surprisedTexture);
Expand All @@ -70,12 +73,18 @@ public Identifier surprisedTexture() {
return surprisedTexturePath;
}

public Map<RegistryEntry<EntityAttribute>, Double> baseAttributes() {
return this.baseAttributes;
public void applyAttributes(LivingEntity livingEntity) {
baseAttributes.forEach((attribute, value) -> livingEntity.getAttributeInstance(attribute).setBaseValue(value));
}

public void applyAttributes(LivingEntity livingEntity) {
this.baseAttributes.forEach((attribute, value) -> livingEntity.getAttributeInstance(attribute).setBaseValue(value));
public Optional<Text> spawnEggName() {
return spawnEggName;
}

public ItemStack getSpawnEggStack() {
ItemStack stack = new ItemStack(MubbleItems.GOOMBA_SPAWN_EGG);
spawnEggName.ifPresent(name -> stack.set(DataComponentTypes.ITEM_NAME, name));
return stack;
}

//TODO: move to utility class
Expand Down
29 changes: 18 additions & 11 deletions src/main/java/fr/hugman/mubble/item/MubbleItemGroups.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import fr.hugman.mubble.block.MubbleBlocks;
import fr.hugman.mubble.entity.GoombaEntity;
import fr.hugman.mubble.entity.GoombaVariant;
import fr.hugman.mubble.entity.GoombaVariants;
import fr.hugman.mubble.entity.MubbleEntityTypeKeys;
import fr.hugman.mubble.registry.MubbleRegistryKeys;
import net.fabricmc.fabric.api.itemgroup.v1.FabricItemGroup;
Expand Down Expand Up @@ -62,16 +63,16 @@ public static void appendItemGroups() {
entries.add(MubbleBlocks.BLUE_BEEP_BLOCK);
entries.add(MubbleItems.CAPE_FEATHER);
entries.add(MubbleItems.SUPER_CAPE_FEATHER);
entries.add(MubbleItems.GOOMBA_SPAWN_EGG);
context.lookup()
.getOptional(MubbleRegistryKeys.GOOMBA_VARIANT)
.ifPresent(registryWrapper -> addGoombaSpawnEggs(
.ifPresent(registryWrapper -> addGoombaVariantsSpawnEggs(
entries,
context.lookup(),
registryWrapper,
registryEntry -> true,
ItemGroup.StackVisibility.PARENT_AND_SEARCH_TABS
));

});

appendSpawnEgg(MubbleItems.GOOMBA_SPAWN_EGG);
Expand Down Expand Up @@ -117,7 +118,7 @@ public static void appendSpawnEgg(Item spawnEgg) {
append(ItemGroups.SPAWN_EGGS, e -> e.addAfter(predicate, Collections.singleton(new ItemStack(spawnEgg)), ItemGroup.StackVisibility.PARENT_AND_SEARCH_TABS));
}

private static void addGoombaSpawnEggs(
private static void addGoombaVariantsSpawnEggs(
ItemGroup.Entries entries,
RegistryWrapper.WrapperLookup registries,
RegistryWrapper.Impl<GoombaVariant> registryWrapper,
Expand All @@ -129,14 +130,20 @@ private static void addGoombaSpawnEggs(
.filter(filter)
//TODO: sort?
.forEach(
goombaVariantEntry -> {
ItemStack itemStack = new ItemStack(MubbleItems.GOOMBA_SPAWN_EGG);
itemStack.set(DataComponentTypes.ENTITY_DATA, NbtComponent.DEFAULT
.with(registryOps, GoombaEntity.VARIANT_MAP_CODEC, goombaVariantEntry)
.getOrThrow()
.apply(nbt -> nbt.putString("id", MubbleEntityTypeKeys.GOOMBA.getValue().toString())));
goombaVariantEntry.value().name().ifPresent(text -> itemStack.set(DataComponentTypes.ITEM_NAME, text)); //TODO: add a proper field for spawn egg name
entries.add(itemStack, stackVisibility);
entry -> {
if (GoombaVariants.NORMAL.getValue().equals(entry.registryKey().getValue())) {
return;
}
var stack = new ItemStack(MubbleItems.GOOMBA_SPAWN_EGG);
entry.value().spawnEggName().ifPresent(name -> stack.set(DataComponentTypes.ITEM_NAME, name));
if (stack.isEmpty()) {
return;
}
stack.set(DataComponentTypes.ENTITY_DATA, NbtComponent.DEFAULT
.apply(nbt -> nbt.putString("id", MubbleEntityTypeKeys.GOOMBA.getValue().toString()))
.with(registryOps, GoombaEntity.VARIANT_MAP_CODEC, entry)
.getOrThrow());
entries.add(stack, stackVisibility);
}
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/fr/hugman/mubble/item/MubbleItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class MubbleItems {
public static final CapeFeatherItem CAPE_FEATHER = of(MubbleItemKeys.CAPE_FEATHER, s -> new CapeFeatherItem(s, false));
public static final CapeFeatherItem SUPER_CAPE_FEATHER = of(MubbleItemKeys.SUPER_CAPE_FEATHER, s -> new CapeFeatherItem(s.rarity(Rarity.EPIC), true));

public static final Item GOOMBA_SPAWN_EGG = of(MubbleItemKeys.GOOMBA_SPAWN_EGG, s -> new SpawnEggItem(MubbleEntityTypes.GOOMBA, 0x96664f, 0xf3dca6, s));
public static final SpawnEggItem GOOMBA_SPAWN_EGG = of(MubbleItemKeys.GOOMBA_SPAWN_EGG, s -> new SpawnEggItem(MubbleEntityTypes.GOOMBA, 0x96664f, 0xf3dca6, s));

private static <O extends Item> O of(RegistryKey<Item> key, Function<Item.Settings, O> factory, Item.Settings settings) {
return Registry.register(Registries.ITEM, key, factory.apply(settings.registryKey(key)));
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/assets/mubble/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@
"item.mubble.cape_feather": "Cape Feather",
"item.mubble.super_cape_feather": "Super Cape Feather",
"item.mubble.goomba_spawn_egg": "Goomba Spawn Egg",
"item.mubble.goomba_spawn_egg.mini": "Mini Goomba Spawn Egg",

"entity.mubble.goomba": "Goomba",
"entity.mubble.mini_goomba": "Mini Goomba",
"entity.mubble.goomba.mini": "Mini Goomba",

"block.mubble.blue_egg_block": "Blue Egg Block",
"block.mubble.cyan_egg_block": "Cyan Egg Block",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"name": {
"translate": "entity.mubble.mini_goomba"
"translate": "entity.mubble.goomba.mini"
},
"spawn_egg_name": {
"translate": "item.mubble.goomba_spawn_egg.mini"
},
"texture": "mubble:entity/goomba/mini/normal",
"surprised_texture": "mubble:entity/goomba/mini/surprised",
Expand Down

0 comments on commit 58ff207

Please sign in to comment.