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 <[email protected]>
Co-authored-by: Andreas Troelsen <[email protected]>
This commit is contained in:
Bobcat00
2021-08-07 15:13:44 +02:00
committed by GitHub
co-authored by Bobcat00 Andreas Troelsen
parent a21f47e193
commit 6be130daf1
4 changed files with 25 additions and 4 deletions
@@ -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)) {
@@ -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;
@@ -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");
pet.setCustomNameVisible(true);
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);