Make pet names per-class configurable.

Adds a new optional `pet-name` property to arena class configurations that lets server owners set a custom pet name template instead of the hardcoded "<player>'s pet". This allows for translation and color coding. To accommodate setups where the player's "display name" isn't a good fit, e.g. because it is too long, the more generic "player name" is available for use instead.

Closes #595 

Co-authored-by: Bobcat00 <Bobcat00@users.noreply.github.com>
Co-authored-by: Andreas Troelsen <garbagemule@gmail.com>
This commit is contained in:
Bobcat00 2021-08-07 09:13:44 -04:00 committed by GitHub
parent a21f47e193
commit 6be130daf1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 25 additions and 4 deletions

View File

@ -13,6 +13,7 @@ These changes will (most likely) be included in the next version.
## [Unreleased]
### Added
- New monster variant `angry-bees` can be used to spawn angry bees.
- Pet names are now per-class configurable via the optional `pet-name` property, which defaults to `<display-name>'s pet` (the `<player-name>` variable is also supported).
- (API) MobArena's internal command handler now supports registering pre-instantiated subcommand instances. This should make it easier for extensions to avoid the Singleton anti-pattern for command dependencies.
- (API) MobArena now fires MobArenaPreReloadEvent and MobArenaReloadEvent before and after, respectively, reloading its config-file. This should allow extensions and other plugins to better respond to configuration changes.

View File

@ -27,6 +27,7 @@ public class ArenaClass
private boolean unbreakableWeapons, unbreakableArmor;
private Thing price;
private Location classchest;
private String petName;
/**
* Create a new, empty arena class with the given name.
@ -146,6 +147,14 @@ public class ArenaClass
this.effects = effects;
}
public String getPetName() {
return this.petName;
}
public void setPetName(String petName) {
this.petName = petName;
}
public boolean hasPermission(Player p) {
String key = "mobarena.classes." + slug;
if (p.isPermissionSet(key)) {

View File

@ -393,6 +393,10 @@ public class ArenaMasterImpl implements ArenaMaster
throw new ConfigError("Failed to parse classchest location for class " + classname + " because: " + e.getMessage());
}
// Load pet name
String petName = section.getString("pet-name", "<display-name>'s pet");
arenaClass.setPetName(petName);
// Finally add the class to the classes map.
classes.put(arenaClass.getSlug(), arenaClass);
return arenaClass;

View File

@ -1,6 +1,7 @@
package com.garbagemule.MobArena;
import com.garbagemule.MobArena.framework.Arena;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
@ -42,11 +43,11 @@ public class SpawnsPets {
}
for (Map.Entry<Material, EntityType> entry : materialToEntity.entrySet()) {
spawnPetsFor(player, arena, entry.getKey(), entry.getValue());
spawnPetsFor(player, arena, entry.getKey(), entry.getValue(), ac.getPetName());
}
}
private void spawnPetsFor(Player player, Arena arena, Material material, EntityType entity) {
private void spawnPetsFor(Player player, Arena arena, Material material, EntityType entity, String petName) {
PlayerInventory inv = player.getInventory();
int index = inv.first(material);
@ -57,8 +58,14 @@ public class SpawnsPets {
int amount = inv.getItem(index).getAmount();
for (int i = 0; i < amount; i++) {
Entity pet = arena.getWorld().spawn(player.getLocation(), entity.getEntityClass());
pet.setCustomName(player.getDisplayName() + "'s pet");
if (!petName.isEmpty()) {
String resolved = petName
.replace("<player-name>", player.getName())
.replace("<display-name>", player.getDisplayName());
String colorized = ChatColor.translateAlternateColorCodes('&', resolved);
pet.setCustomName(colorized);
pet.setCustomNameVisible(true);
}
if (pet instanceof Tameable) {
Tameable tameable = (Tameable) pet;
tameable.setTamed(true);