Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ private static Map<String, PetTypeDef> readTypes(ConfigurationSection pets, Conf
if (section == null) {
continue;
}
String displayName = blankToNull(section.getString("display-name")); // Own entry only; never inherited from species.
PetDefinitions.Resolved resolved;
try {
resolved = definitions.resolve(section);
Expand Down Expand Up @@ -365,7 +366,7 @@ private static Map<String, PetTypeDef> readTypes(ConfigurationSection pets, Conf
sexMode,
appearance,
petItems, tricks, defaults, resolved.behavior(),
PetSounds.read(section, entity, logger), section.getBoolean("native-combat", false), resolved.species(), hatchPermission));
PetSounds.read(section, entity, logger), section.getBoolean("native-combat", false), resolved.species(), hatchPermission, displayName));
}
return Collections.unmodifiableMap(types);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,25 @@ public record PetTypeDef(
PetSounds sounds,
boolean nativeCombat,
String species,
String hatchPermission) {
String hatchPermission,
String displayName) {

public PetTypeDef {
tricks = Set.copyOf(tricks);
defaultTricks = List.copyOf(defaultTricks);
java.util.Objects.requireNonNull(behavior);
}

/** Player-facing species name: display-name from this pet's config entry, else a readable name from its ID. */
public String speciesName() {
return net.tfminecraft.companionpets.text.PetTexts.speciesName(displayName, id);
}

/** Player-facing name for a saved pet's type, also when that type is no longer configured. */
public static String speciesName(PetTypeDef type, String typeId) {
return type == null ? net.tfminecraft.companionpets.text.PetTexts.speciesName(typeId) : type.speciesName();
}

/** A null hatch permission lets anyone hatch this type's egg. */
public boolean canHatch(Permissible player) {
return hatchPermission == null || player.hasPermission(hatchPermission);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/net/tfminecraft/companionpets/gui/PetMenus.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private void fillProfile(Inventory inventory, Pet pet, boolean management, boole
ItemStack food = type == null || type.foodIcon() == null ? new ItemStack(Material.COOKED_BEEF) : type.foodIcon().icon(Material.COOKED_BEEF);
boolean sick = pet.illness() != Illness.NONE;

inventory.setItem(4, named(egg, PetTexts.speciesName(pet.typeId()), NamedTextColor.WHITE,
inventory.setItem(4, named(egg, PetTypeDef.speciesName(type, pet.typeId()), NamedTextColor.WHITE,
line("Species", NamedTextColor.GRAY)));
inventory.setItem(11, named(Material.WHITE_DYE, "Sex", NamedTextColor.WHITE,
line(PetTexts.sexName(pet.sex()), NamedTextColor.GRAY)));
Expand Down Expand Up @@ -245,7 +245,7 @@ public void openKennel(org.bukkit.entity.Player player, int requestedPage, org.b
ItemStack icon = type == null ? new ItemStack(Material.BONE) : type.eggIcon();
boolean stored = pet.stored();
ItemStack item = named(icon, pet.name(), NamedTextColor.GOLD,
line(PetTexts.sexName(pet.sex()) + " " + PetTexts.speciesName(pet.typeId()).toLowerCase(java.util.Locale.ROOT),
line(PetTexts.sexName(pet.sex()) + " " + PetTypeDef.speciesName(type, pet.typeId()),
NamedTextColor.GRAY),
line(stored ? "Resting in the Pet House" : "Out and about", stored ? NamedTextColor.DARK_GRAY : NamedTextColor.GREEN),
line("Energy ", StatLook.theme(Need.ENERGY)).append(StatLook.bar(pet.need(Need.ENERGY))),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package net.tfminecraft.companionpets.runtime;

import java.util.Locale;
import java.util.UUID;

import org.bukkit.entity.Entity;
Expand Down Expand Up @@ -40,7 +39,7 @@ void begin(Player player, PetTypeDef type) {
}
HatchPrompt prompt = new HatchPrompt(type.id(), System.currentTimeMillis() + PROMPT_MILLIS);
runtime.sessions().hatch(player.getUniqueId(), prompt);
String species = PetTexts.speciesName(type.id()).toLowerCase(Locale.ROOT);
String species = type.speciesName();
if (type.sexMode() == SexMode.CHOOSE) {
PetFx.tell(player, "Will your new " + species + " be a boy or a girl? Type \"male\" or \"female\", or \"cancel\" to keep the egg.");
return;
Expand Down
33 changes: 30 additions & 3 deletions src/main/java/net/tfminecraft/companionpets/text/PetTexts.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,40 @@ public static String sexName(PetSex sex) {
return sex == PetSex.FEMALE ? "Female" : "Male";
}

/** Readable names for TFMC pet type IDs whose IDs run words together; configs may override them with display-name. */
private static final java.util.Map<String, String> BUILT_IN_NAMES = java.util.Map.ofEntries(
java.util.Map.entry("golden", "Golden Retriever"),
java.util.Map.entry("catblack", "Black Cat"),
java.util.Map.entry("catfunny", "Funny Cat"),
java.util.Map.entry("catorange", "Orange Cat"),
java.util.Map.entry("catgray", "Gray Cat"),
java.util.Map.entry("cattabby", "Tabby Cat"),
java.util.Map.entry("mainecoon", "Maine Coon"),
java.util.Map.entry("bernesse", "Bernese Mountain Dog"),
java.util.Map.entry("bordercollie", "Border Collie"),
java.util.Map.entry("bordercolliegray", "Gray Border Collie"),
java.util.Map.entry("lagottoromagnolo", "Lagotto Romagnolo"),
java.util.Map.entry("yorkshire", "Yorkshire Terrier"),
java.util.Map.entry("sphynx", "Sphynx Cat"));

/** Player-facing species name: the configured display name, else a built-in or Title Case name from the type ID. */
public static String speciesName(String displayName, String typeId) {
return displayName == null || displayName.isBlank() ? speciesName(typeId) : displayName.trim();
}

public static String speciesName(String typeId) {
if (typeId == null || typeId.isBlank()) {
return "Pet";
}
String clean = typeId.replace('_', ' ').replace('-', ' ').trim();
if (clean.isEmpty()) return "Pet";
return clean.substring(0, 1).toUpperCase(Locale.ROOT) + clean.substring(1).toLowerCase(Locale.ROOT);
String builtIn = BUILT_IN_NAMES.get(typeId.trim().toLowerCase(Locale.ROOT));
if (builtIn != null) return builtIn;
StringBuilder name = new StringBuilder();
for (String word : typeId.replace('_', ' ').replace('-', ' ').trim().split(" +")) {
if (word.isEmpty()) continue;
if (!name.isEmpty()) name.append(' ');
name.append(word.substring(0, 1).toUpperCase(Locale.ROOT)).append(word.substring(1).toLowerCase(Locale.ROOT));
}
return name.isEmpty() ? "Pet" : name.toString();
}

public static String itemName(String material) {
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ pets:
# cat = restrained, chatty greetings and stalking toys; basic = neutral hops/sounds.
# fox_example:
# species: dog # Inherits global defaults and species.dog settings.
# display-name: Arctic Fox # Species name players see; default is a readable name from the ID. Not inherited.
# entity: WOLF # Only WOLF or CAT, even for a visible fox.
# mythic-mob: "" # Optional MythicMobs mob ID; nonempty requires MythicMobs.
# egg: EGG # Vanilla, MMOItems or ItemsAdder selector.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,21 @@ class CompanionConfigCoverageTest {
assertTrue(warnings.stream().anyMatch(s -> s.contains("max-out exceeds limits.max-pets") && s.endsWith("2")));
}

@Test void displayNameComesOnlyFromThePetEntryAndBlankFallsBackToTheId() throws Exception {
var config = load("""
species: {fox: {entity: WOLF, display-name: Fox Kit}}
pets:
catblack: {species: cat, egg: CAT_SPAWN_EGG, display-name: " Shadow Cat "}
fox: {species: fox, egg: FOX_SPAWN_EGG}
mainecoon: {species: cat, egg: OCELOT_SPAWN_EGG, display-name: " "}
""");
assertEquals("Shadow Cat", config.type("catblack").speciesName());
assertNull(config.type("fox").displayName());
assertEquals("Fox", config.type("fox").speciesName());
assertNull(config.type("mainecoon").displayName());
assertEquals("Maine Coon", config.type("mainecoon").speciesName());
}

@Test void hatchPermissionInheritsThroughSpeciesAndFalseOrBlankClearsIt() throws Exception {
var config = load("""
hatching: {permission: " roles.pet_master "}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,8 @@ private String uncomment(String text, String first, String last) {
assertEquals(BehaviorProfile.CAT, fox.behavior());
assertEquals(SexMode.CHOOSE, fox.sexMode());
assertEquals(12002, fox.eggCustomModelData());
assertEquals("Arctic Fox", fox.speciesName());
assertEquals("Black Cat", config.type("catblack").speciesName());
assertEquals("companionpets.hatch.fox", fox.hatchPermission());
assertEquals("companionpets.hatch", config.type("rabbit").hatchPermission());
assertEquals(List.of(ItemRef.vanilla(Material.SALMON)), fox.treats());
Expand Down
72 changes: 69 additions & 3 deletions src/test/java/net/tfminecraft/companionpets/text/PetTextsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,77 @@ void ageUsesTheTimeSinceBirth() {
@Test
void speciesAndItemNamesReadNaturally() {
assertEquals("Wolf", PetTexts.speciesName("wolf"));
assertEquals("Snow fox", PetTexts.speciesName("snow_fox"));
assertEquals("Snow Fox", PetTexts.speciesName("snow_fox"));
assertEquals("Pet", PetTexts.speciesName(""));
assertEquals("glow berries", PetTexts.itemName("GLOW_BERRIES"));
}

@Test
void everyTfmcPetTypeHasAReadableSpeciesNameAndConfigNamesWin() {
var expected = new java.util.LinkedHashMap<String, String>();
expected.put("wolf", "Wolf"); expected.put("cat", "Cat"); expected.put("beagle", "Beagle");
expected.put("chihuahua", "Chihuahua"); expected.put("corgi", "Corgi"); expected.put("golden", "Golden Retriever");
expected.put("husky", "Husky"); expected.put("catblack", "Black Cat"); expected.put("catfunny", "Funny Cat");
expected.put("catorange", "Orange Cat"); expected.put("mainecoon", "Maine Coon"); expected.put("fox", "Fox");
expected.put("frog", "Frog"); expected.put("bernesse", "Bernese Mountain Dog"); expected.put("bordercollie", "Border Collie");
expected.put("catgray", "Gray Cat"); expected.put("cattabby", "Tabby Cat"); expected.put("lagottoromagnolo", "Lagotto Romagnolo");
expected.put("yorkshire", "Yorkshire Terrier"); expected.put("bordercolliegray", "Gray Border Collie");
expected.put("cavalier_king_charles_spaniel", "Cavalier King Charles Spaniel"); expected.put("sphynx", "Sphynx Cat");
expected.forEach((id, name) -> assertEquals(name, PetTexts.speciesName(id), id));
assertEquals("Maine Coon", PetTexts.speciesName(" MaineCoon "));
assertEquals("Ginger Tom", PetTexts.speciesName(" Ginger Tom ", "catorange"));
assertEquals("Orange Cat", PetTexts.speciesName(" ", "catorange"));
assertEquals("Orange Cat", PetTexts.speciesName(null, "catorange"));
assertEquals("Black Cat", net.tfminecraft.companionpets.config.PetTypeDef.speciesName(null, "catblack"));
}

@Test
void hatchPromptAndPetHouseUseTheReadableSpeciesName() throws Exception {
var server = MockBukkit.mock();
PetStore store = null;
PetActions actions = null;
try {
server.addSimpleWorld("world");
var plugin = net.tfminecraft.companionpets.testutil.OwnershipFixture.plugin();
var owner = server.addPlayer();
var yaml = new YamlConfiguration();
yaml.loadFromString("pets: {mainecoon: {species: cat, egg: CAT_SPAWN_EGG, sex: choose}, wolf: {species: dog, egg: WOLF_SPAWN_EGG, display-name: Grey Wolf}}");
var config = CompanionConfig.load(plugin, yaml);
store = new PetStore(plugin);
assertTrue(store.load());
var visual = new IdleVisual();
var key = new NamespacedKey(plugin, "pet");
var runtime = new PetRuntime(plugin, config, store, new Sessions(), new Bodies(plugin, key, visual),
visual, key, new NamespacedKey(plugin, "toy"));
actions = new PetActions(runtime);
var egg = new ItemStack(Material.CAT_SPAWN_EGG);
owner.getInventory().setItemInMainHand(egg);
actions.useWorld(owner, egg, null, null, false, true);
assertTrue(owner.nextMessage().contains("Will your new Maine Coon be a boy or a girl?"));
actions.onChat(owner, "cancel");
var wolfEgg = new ItemStack(Material.WOLF_SPAWN_EGG);
owner.getInventory().setItemInMainHand(wolfEgg);
owner.nextMessage();
actions.useWorld(owner, wolfEgg, null, null, false, true);
String told = owner.nextMessage();
assertTrue(told.contains("Your new Grey Wolf is a "), told);
actions.onChat(owner, "cancel");
var pet = net.tfminecraft.companionpets.testutil.OwnershipFixture.pet(UUID.randomUUID(), owner.getUniqueId(), "mainecoon", "Luna", PetSex.FEMALE);
pet.stored(true);
store.add(pet);
actions.menus().openCare(owner, pet);
var species = owner.getOpenInventory().getTopInventory().getItem(4).getItemMeta().displayName();
assertEquals("Maine Coon", PlainTextComponentSerializer.plainText().serialize(species));
actions.menus().openKennel(owner);
var lore = owner.getOpenInventory().getTopInventory().getItem(0).getItemMeta().lore();
assertEquals("Female Maine Coon", PlainTextComponentSerializer.plainText().serialize(lore.get(0)));
} finally {
if (actions != null) actions.holograms().clear();
if (store != null) assertTrue(store.close());
MockBukkit.unmock();
}
}

@Test
void aConfiguredTypeMadeOnlyOfSeparatorsCanHatchAndOpenItsPetProfile() throws Exception {
var server = MockBukkit.mock();
Expand All @@ -66,7 +132,7 @@ void aConfiguredTypeMadeOnlyOfSeparatorsCanHatchAndOpenItsPetProfile() throws Ex
var egg = new ItemStack(Material.WOLF_SPAWN_EGG);
owner.getInventory().setItemInMainHand(egg);
actions.useWorld(owner, egg, null, null, false, true);
assertTrue(owner.nextMessage().contains("Will your new pet be a boy or a girl?"));
assertTrue(owner.nextMessage().contains("Will your new Pet be a boy or a girl?"));
assertEquals("_-_", runtime.sessions().hatch(owner.getUniqueId()).typeId());
actions.onChat(owner, "cancel");
assertNull(runtime.sessions().hatch(owner.getUniqueId()));
Expand Down Expand Up @@ -108,7 +174,7 @@ void absentNamesUseReadableFallbacksAndMaterialFormattingIgnoresServerLocale() {
Locale previous = Locale.getDefault();
try {
Locale.setDefault(Locale.forLanguageTag("tr-TR"));
assertEquals("Arctic fox", PetTexts.speciesName("-ARCTIC_FOX-"));
assertEquals("Arctic Fox", PetTexts.speciesName("-ARCTIC_FOX-"));
assertEquals("iron ingot", PetTexts.itemName("IRON_INGOT"));
assertEquals("high five", PetTexts.trickName(Trick.valueOf("HIGH_FIVE")));
} finally { Locale.setDefault(previous); }
Expand Down