Enchantment: use Translatable instead of hardcoded translation keys

This commit is contained in:
Dylan K. Taylor 2021-09-03 20:52:05 +01:00
parent fbbaef4401
commit aa5a9f6d12
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
7 changed files with 212 additions and 24 deletions

@ -1 +1 @@
Subproject commit 558e260fc5bfee5f0de8ce701be93eeac184f454 Subproject commit 95b9c82f25f2d216683769c326c06c88706ce3cc

View File

@ -23,6 +23,7 @@ declare(strict_types=1);
namespace pocketmine\item\enchantment; namespace pocketmine\item\enchantment;
use pocketmine\lang\Translatable;
use function constant; use function constant;
/** /**
@ -32,7 +33,7 @@ class Enchantment{
public function __construct( public function __construct(
private int $internalRuntimeId, private int $internalRuntimeId,
private string $name, private Translatable|string $name,
private int $rarity, private int $rarity,
private int $primaryItemFlags, private int $primaryItemFlags,
private int $secondaryItemFlags, private int $secondaryItemFlags,
@ -50,7 +51,7 @@ class Enchantment{
/** /**
* Returns a translation key for this enchantment's name. * Returns a translation key for this enchantment's name.
*/ */
public function getName() : string{ public function getName() : Translatable|string{
return $this->name; return $this->name;
} }

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace pocketmine\item\enchantment; namespace pocketmine\item\enchantment;
use pocketmine\event\entity\EntityDamageEvent; use pocketmine\event\entity\EntityDamageEvent;
use pocketmine\lang\Translatable;
use function array_flip; use function array_flip;
use function floor; use function floor;
@ -38,7 +39,7 @@ class ProtectionEnchantment extends Enchantment{
* *
* @param int[]|null $applicableDamageTypes EntityDamageEvent::CAUSE_* constants which this enchantment type applies to, or null if it applies to all types of damage. * @param int[]|null $applicableDamageTypes EntityDamageEvent::CAUSE_* constants which this enchantment type applies to, or null if it applies to all types of damage.
*/ */
public function __construct(int $internalRuntimeId, string $name, int $rarity, int $primaryItemFlags, int $secondaryItemFlags, int $maxLevel, float $typeModifier, ?array $applicableDamageTypes){ public function __construct(int $internalRuntimeId, Translatable|string $name, int $rarity, int $primaryItemFlags, int $secondaryItemFlags, int $maxLevel, float $typeModifier, ?array $applicableDamageTypes){
parent::__construct($internalRuntimeId, $name, $rarity, $primaryItemFlags, $secondaryItemFlags, $maxLevel); parent::__construct($internalRuntimeId, $name, $rarity, $primaryItemFlags, $secondaryItemFlags, $maxLevel);
$this->typeModifier = $typeModifier; $this->typeModifier = $typeModifier;

View File

@ -24,6 +24,7 @@ declare(strict_types=1);
namespace pocketmine\item\enchantment; namespace pocketmine\item\enchantment;
use pocketmine\event\entity\EntityDamageEvent; use pocketmine\event\entity\EntityDamageEvent;
use pocketmine\lang\KnownTranslationFactory;
use pocketmine\utils\RegistryTrait; use pocketmine\utils\RegistryTrait;
/** /**
@ -64,44 +65,44 @@ final class VanillaEnchantments{
} }
protected static function setup() : void{ protected static function setup() : void{
self::register("PROTECTION", new ProtectionEnchantment(self::newRtId(), "%enchantment.protect.all", Rarity::COMMON, ItemFlags::ARMOR, ItemFlags::NONE, 4, 0.75, null)); self::register("PROTECTION", new ProtectionEnchantment(self::newRtId(), KnownTranslationFactory::enchantment_protect_all(), Rarity::COMMON, ItemFlags::ARMOR, ItemFlags::NONE, 4, 0.75, null));
self::register("FIRE_PROTECTION", new ProtectionEnchantment(self::newRtId(), "%enchantment.protect.fire", Rarity::UNCOMMON, ItemFlags::ARMOR, ItemFlags::NONE, 4, 1.25, [ self::register("FIRE_PROTECTION", new ProtectionEnchantment(self::newRtId(), KnownTranslationFactory::enchantment_protect_fire(), Rarity::UNCOMMON, ItemFlags::ARMOR, ItemFlags::NONE, 4, 1.25, [
EntityDamageEvent::CAUSE_FIRE, EntityDamageEvent::CAUSE_FIRE,
EntityDamageEvent::CAUSE_FIRE_TICK, EntityDamageEvent::CAUSE_FIRE_TICK,
EntityDamageEvent::CAUSE_LAVA EntityDamageEvent::CAUSE_LAVA
//TODO: check fireballs //TODO: check fireballs
])); ]));
self::register("FEATHER_FALLING", new ProtectionEnchantment(self::newRtId(), "%enchantment.protect.fall", Rarity::UNCOMMON, ItemFlags::FEET, ItemFlags::NONE, 4, 2.5, [ self::register("FEATHER_FALLING", new ProtectionEnchantment(self::newRtId(), KnownTranslationFactory::enchantment_protect_fall(), Rarity::UNCOMMON, ItemFlags::FEET, ItemFlags::NONE, 4, 2.5, [
EntityDamageEvent::CAUSE_FALL EntityDamageEvent::CAUSE_FALL
])); ]));
self::register("BLAST_PROTECTION", new ProtectionEnchantment(self::newRtId(), "%enchantment.protect.explosion", Rarity::RARE, ItemFlags::ARMOR, ItemFlags::NONE, 4, 1.5, [ self::register("BLAST_PROTECTION", new ProtectionEnchantment(self::newRtId(), KnownTranslationFactory::enchantment_protect_explosion(), Rarity::RARE, ItemFlags::ARMOR, ItemFlags::NONE, 4, 1.5, [
EntityDamageEvent::CAUSE_BLOCK_EXPLOSION, EntityDamageEvent::CAUSE_BLOCK_EXPLOSION,
EntityDamageEvent::CAUSE_ENTITY_EXPLOSION EntityDamageEvent::CAUSE_ENTITY_EXPLOSION
])); ]));
self::register("PROJECTILE_PROTECTION", new ProtectionEnchantment(self::newRtId(), "%enchantment.protect.projectile", Rarity::UNCOMMON, ItemFlags::ARMOR, ItemFlags::NONE, 4, 1.5, [ self::register("PROJECTILE_PROTECTION", new ProtectionEnchantment(self::newRtId(), KnownTranslationFactory::enchantment_protect_projectile(), Rarity::UNCOMMON, ItemFlags::ARMOR, ItemFlags::NONE, 4, 1.5, [
EntityDamageEvent::CAUSE_PROJECTILE EntityDamageEvent::CAUSE_PROJECTILE
])); ]));
self::register("THORNS", new Enchantment(self::newRtId(), "%enchantment.thorns", Rarity::MYTHIC, ItemFlags::TORSO, ItemFlags::HEAD | ItemFlags::LEGS | ItemFlags::FEET, 3)); self::register("THORNS", new Enchantment(self::newRtId(), KnownTranslationFactory::enchantment_thorns(), Rarity::MYTHIC, ItemFlags::TORSO, ItemFlags::HEAD | ItemFlags::LEGS | ItemFlags::FEET, 3));
self::register("RESPIRATION", new Enchantment(self::newRtId(), "%enchantment.oxygen", Rarity::RARE, ItemFlags::HEAD, ItemFlags::NONE, 3)); self::register("RESPIRATION", new Enchantment(self::newRtId(), KnownTranslationFactory::enchantment_oxygen(), Rarity::RARE, ItemFlags::HEAD, ItemFlags::NONE, 3));
self::register("SHARPNESS", new SharpnessEnchantment(self::newRtId(), "%enchantment.damage.all", Rarity::COMMON, ItemFlags::SWORD, ItemFlags::AXE, 5)); self::register("SHARPNESS", new SharpnessEnchantment(self::newRtId(), KnownTranslationFactory::enchantment_damage_all(), Rarity::COMMON, ItemFlags::SWORD, ItemFlags::AXE, 5));
//TODO: smite, bane of arthropods (these don't make sense now because their applicable mobs don't exist yet) //TODO: smite, bane of arthropods (these don't make sense now because their applicable mobs don't exist yet)
self::register("KNOCKBACK", new KnockbackEnchantment(self::newRtId(), "%enchantment.knockback", Rarity::UNCOMMON, ItemFlags::SWORD, ItemFlags::NONE, 2)); self::register("KNOCKBACK", new KnockbackEnchantment(self::newRtId(), KnownTranslationFactory::enchantment_knockback(), Rarity::UNCOMMON, ItemFlags::SWORD, ItemFlags::NONE, 2));
self::register("FIRE_ASPECT", new FireAspectEnchantment(self::newRtId(), "%enchantment.fire", Rarity::RARE, ItemFlags::SWORD, ItemFlags::NONE, 2)); self::register("FIRE_ASPECT", new FireAspectEnchantment(self::newRtId(), KnownTranslationFactory::enchantment_fire(), Rarity::RARE, ItemFlags::SWORD, ItemFlags::NONE, 2));
self::register("EFFICIENCY", new Enchantment(self::newRtId(), "%enchantment.digging", Rarity::COMMON, ItemFlags::DIG, ItemFlags::SHEARS, 5)); self::register("EFFICIENCY", new Enchantment(self::newRtId(), KnownTranslationFactory::enchantment_digging(), Rarity::COMMON, ItemFlags::DIG, ItemFlags::SHEARS, 5));
self::register("SILK_TOUCH", new Enchantment(self::newRtId(), "%enchantment.untouching", Rarity::MYTHIC, ItemFlags::DIG, ItemFlags::SHEARS, 1)); self::register("SILK_TOUCH", new Enchantment(self::newRtId(), KnownTranslationFactory::enchantment_untouching(), Rarity::MYTHIC, ItemFlags::DIG, ItemFlags::SHEARS, 1));
self::register("UNBREAKING", new Enchantment(self::newRtId(), "%enchantment.durability", Rarity::UNCOMMON, ItemFlags::DIG | ItemFlags::ARMOR | ItemFlags::FISHING_ROD | ItemFlags::BOW, ItemFlags::TOOL | ItemFlags::CARROT_STICK | ItemFlags::ELYTRA, 3)); self::register("UNBREAKING", new Enchantment(self::newRtId(), KnownTranslationFactory::enchantment_durability(), Rarity::UNCOMMON, ItemFlags::DIG | ItemFlags::ARMOR | ItemFlags::FISHING_ROD | ItemFlags::BOW, ItemFlags::TOOL | ItemFlags::CARROT_STICK | ItemFlags::ELYTRA, 3));
self::register("POWER", new Enchantment(self::newRtId(), "%enchantment.arrowDamage", Rarity::COMMON, ItemFlags::BOW, ItemFlags::NONE, 5)); self::register("POWER", new Enchantment(self::newRtId(), KnownTranslationFactory::enchantment_arrowDamage(), Rarity::COMMON, ItemFlags::BOW, ItemFlags::NONE, 5));
self::register("PUNCH", new Enchantment(self::newRtId(), "%enchantment.arrowKnockback", Rarity::RARE, ItemFlags::BOW, ItemFlags::NONE, 2)); self::register("PUNCH", new Enchantment(self::newRtId(), KnownTranslationFactory::enchantment_arrowKnockback(), Rarity::RARE, ItemFlags::BOW, ItemFlags::NONE, 2));
self::register("FLAME", new Enchantment(self::newRtId(), "%enchantment.arrowFire", Rarity::RARE, ItemFlags::BOW, ItemFlags::NONE, 1)); self::register("FLAME", new Enchantment(self::newRtId(), KnownTranslationFactory::enchantment_arrowFire(), Rarity::RARE, ItemFlags::BOW, ItemFlags::NONE, 1));
self::register("INFINITY", new Enchantment(self::newRtId(), "%enchantment.arrowInfinite", Rarity::MYTHIC, ItemFlags::BOW, ItemFlags::NONE, 1)); self::register("INFINITY", new Enchantment(self::newRtId(), KnownTranslationFactory::enchantment_arrowInfinite(), Rarity::MYTHIC, ItemFlags::BOW, ItemFlags::NONE, 1));
self::register("MENDING", new Enchantment(self::newRtId(), "%enchantment.mending", Rarity::RARE, ItemFlags::NONE, ItemFlags::ALL, 1)); self::register("MENDING", new Enchantment(self::newRtId(), KnownTranslationFactory::enchantment_mending(), Rarity::RARE, ItemFlags::NONE, ItemFlags::ALL, 1));
self::register("VANISHING", new Enchantment(self::newRtId(), "%enchantment.curse.vanishing", Rarity::MYTHIC, ItemFlags::NONE, ItemFlags::ALL, 1)); self::register("VANISHING", new Enchantment(self::newRtId(), KnownTranslationFactory::enchantment_curse_vanishing(), Rarity::MYTHIC, ItemFlags::NONE, ItemFlags::ALL, 1));
} }
protected static function register(string $name, Enchantment $member) : void{ protected static function register(string $name, Enchantment $member) : void{

View File

@ -759,6 +759,154 @@ final class KnownTranslationFactory{
return new Translatable(KnownTranslationKeys::DISCONNECTIONSCREEN_SERVERFULL, []); return new Translatable(KnownTranslationKeys::DISCONNECTIONSCREEN_SERVERFULL, []);
} }
public static function enchantment_arrowDamage() : Translatable{
return new Translatable(KnownTranslationKeys::ENCHANTMENT_ARROWDAMAGE, []);
}
public static function enchantment_arrowFire() : Translatable{
return new Translatable(KnownTranslationKeys::ENCHANTMENT_ARROWFIRE, []);
}
public static function enchantment_arrowInfinite() : Translatable{
return new Translatable(KnownTranslationKeys::ENCHANTMENT_ARROWINFINITE, []);
}
public static function enchantment_arrowKnockback() : Translatable{
return new Translatable(KnownTranslationKeys::ENCHANTMENT_ARROWKNOCKBACK, []);
}
public static function enchantment_crossbowMultishot() : Translatable{
return new Translatable(KnownTranslationKeys::ENCHANTMENT_CROSSBOWMULTISHOT, []);
}
public static function enchantment_crossbowPiercing() : Translatable{
return new Translatable(KnownTranslationKeys::ENCHANTMENT_CROSSBOWPIERCING, []);
}
public static function enchantment_crossbowQuickCharge() : Translatable{
return new Translatable(KnownTranslationKeys::ENCHANTMENT_CROSSBOWQUICKCHARGE, []);
}
public static function enchantment_curse_binding() : Translatable{
return new Translatable(KnownTranslationKeys::ENCHANTMENT_CURSE_BINDING, []);
}
public static function enchantment_curse_vanishing() : Translatable{
return new Translatable(KnownTranslationKeys::ENCHANTMENT_CURSE_VANISHING, []);
}
public static function enchantment_damage_all() : Translatable{
return new Translatable(KnownTranslationKeys::ENCHANTMENT_DAMAGE_ALL, []);
}
public static function enchantment_damage_arthropods() : Translatable{
return new Translatable(KnownTranslationKeys::ENCHANTMENT_DAMAGE_ARTHROPODS, []);
}
public static function enchantment_damage_undead() : Translatable{
return new Translatable(KnownTranslationKeys::ENCHANTMENT_DAMAGE_UNDEAD, []);
}
public static function enchantment_digging() : Translatable{
return new Translatable(KnownTranslationKeys::ENCHANTMENT_DIGGING, []);
}
public static function enchantment_durability() : Translatable{
return new Translatable(KnownTranslationKeys::ENCHANTMENT_DURABILITY, []);
}
public static function enchantment_fire() : Translatable{
return new Translatable(KnownTranslationKeys::ENCHANTMENT_FIRE, []);
}
public static function enchantment_fishingSpeed() : Translatable{
return new Translatable(KnownTranslationKeys::ENCHANTMENT_FISHINGSPEED, []);
}
public static function enchantment_frostwalker() : Translatable{
return new Translatable(KnownTranslationKeys::ENCHANTMENT_FROSTWALKER, []);
}
public static function enchantment_knockback() : Translatable{
return new Translatable(KnownTranslationKeys::ENCHANTMENT_KNOCKBACK, []);
}
public static function enchantment_lootBonus() : Translatable{
return new Translatable(KnownTranslationKeys::ENCHANTMENT_LOOTBONUS, []);
}
public static function enchantment_lootBonusDigger() : Translatable{
return new Translatable(KnownTranslationKeys::ENCHANTMENT_LOOTBONUSDIGGER, []);
}
public static function enchantment_lootBonusFishing() : Translatable{
return new Translatable(KnownTranslationKeys::ENCHANTMENT_LOOTBONUSFISHING, []);
}
public static function enchantment_mending() : Translatable{
return new Translatable(KnownTranslationKeys::ENCHANTMENT_MENDING, []);
}
public static function enchantment_oxygen() : Translatable{
return new Translatable(KnownTranslationKeys::ENCHANTMENT_OXYGEN, []);
}
public static function enchantment_protect_all() : Translatable{
return new Translatable(KnownTranslationKeys::ENCHANTMENT_PROTECT_ALL, []);
}
public static function enchantment_protect_explosion() : Translatable{
return new Translatable(KnownTranslationKeys::ENCHANTMENT_PROTECT_EXPLOSION, []);
}
public static function enchantment_protect_fall() : Translatable{
return new Translatable(KnownTranslationKeys::ENCHANTMENT_PROTECT_FALL, []);
}
public static function enchantment_protect_fire() : Translatable{
return new Translatable(KnownTranslationKeys::ENCHANTMENT_PROTECT_FIRE, []);
}
public static function enchantment_protect_projectile() : Translatable{
return new Translatable(KnownTranslationKeys::ENCHANTMENT_PROTECT_PROJECTILE, []);
}
public static function enchantment_soul_speed() : Translatable{
return new Translatable(KnownTranslationKeys::ENCHANTMENT_SOUL_SPEED, []);
}
public static function enchantment_thorns() : Translatable{
return new Translatable(KnownTranslationKeys::ENCHANTMENT_THORNS, []);
}
public static function enchantment_tridentChanneling() : Translatable{
return new Translatable(KnownTranslationKeys::ENCHANTMENT_TRIDENTCHANNELING, []);
}
public static function enchantment_tridentImpaling() : Translatable{
return new Translatable(KnownTranslationKeys::ENCHANTMENT_TRIDENTIMPALING, []);
}
public static function enchantment_tridentLoyalty() : Translatable{
return new Translatable(KnownTranslationKeys::ENCHANTMENT_TRIDENTLOYALTY, []);
}
public static function enchantment_tridentRiptide() : Translatable{
return new Translatable(KnownTranslationKeys::ENCHANTMENT_TRIDENTRIPTIDE, []);
}
public static function enchantment_untouching() : Translatable{
return new Translatable(KnownTranslationKeys::ENCHANTMENT_UNTOUCHING, []);
}
public static function enchantment_waterWalker() : Translatable{
return new Translatable(KnownTranslationKeys::ENCHANTMENT_WATERWALKER, []);
}
public static function enchantment_waterWorker() : Translatable{
return new Translatable(KnownTranslationKeys::ENCHANTMENT_WATERWORKER, []);
}
public static function gameMode_adventure() : Translatable{ public static function gameMode_adventure() : Translatable{
return new Translatable(KnownTranslationKeys::GAMEMODE_ADVENTURE, []); return new Translatable(KnownTranslationKeys::GAMEMODE_ADVENTURE, []);
} }

View File

@ -163,6 +163,43 @@ final class KnownTranslationKeys{
public const DISCONNECTIONSCREEN_OUTDATEDSERVER = "disconnectionScreen.outdatedServer"; public const DISCONNECTIONSCREEN_OUTDATEDSERVER = "disconnectionScreen.outdatedServer";
public const DISCONNECTIONSCREEN_RESOURCEPACK = "disconnectionScreen.resourcePack"; public const DISCONNECTIONSCREEN_RESOURCEPACK = "disconnectionScreen.resourcePack";
public const DISCONNECTIONSCREEN_SERVERFULL = "disconnectionScreen.serverFull"; public const DISCONNECTIONSCREEN_SERVERFULL = "disconnectionScreen.serverFull";
public const ENCHANTMENT_ARROWDAMAGE = "enchantment.arrowDamage";
public const ENCHANTMENT_ARROWFIRE = "enchantment.arrowFire";
public const ENCHANTMENT_ARROWINFINITE = "enchantment.arrowInfinite";
public const ENCHANTMENT_ARROWKNOCKBACK = "enchantment.arrowKnockback";
public const ENCHANTMENT_CROSSBOWMULTISHOT = "enchantment.crossbowMultishot";
public const ENCHANTMENT_CROSSBOWPIERCING = "enchantment.crossbowPiercing";
public const ENCHANTMENT_CROSSBOWQUICKCHARGE = "enchantment.crossbowQuickCharge";
public const ENCHANTMENT_CURSE_BINDING = "enchantment.curse.binding";
public const ENCHANTMENT_CURSE_VANISHING = "enchantment.curse.vanishing";
public const ENCHANTMENT_DAMAGE_ALL = "enchantment.damage.all";
public const ENCHANTMENT_DAMAGE_ARTHROPODS = "enchantment.damage.arthropods";
public const ENCHANTMENT_DAMAGE_UNDEAD = "enchantment.damage.undead";
public const ENCHANTMENT_DIGGING = "enchantment.digging";
public const ENCHANTMENT_DURABILITY = "enchantment.durability";
public const ENCHANTMENT_FIRE = "enchantment.fire";
public const ENCHANTMENT_FISHINGSPEED = "enchantment.fishingSpeed";
public const ENCHANTMENT_FROSTWALKER = "enchantment.frostwalker";
public const ENCHANTMENT_KNOCKBACK = "enchantment.knockback";
public const ENCHANTMENT_LOOTBONUS = "enchantment.lootBonus";
public const ENCHANTMENT_LOOTBONUSDIGGER = "enchantment.lootBonusDigger";
public const ENCHANTMENT_LOOTBONUSFISHING = "enchantment.lootBonusFishing";
public const ENCHANTMENT_MENDING = "enchantment.mending";
public const ENCHANTMENT_OXYGEN = "enchantment.oxygen";
public const ENCHANTMENT_PROTECT_ALL = "enchantment.protect.all";
public const ENCHANTMENT_PROTECT_EXPLOSION = "enchantment.protect.explosion";
public const ENCHANTMENT_PROTECT_FALL = "enchantment.protect.fall";
public const ENCHANTMENT_PROTECT_FIRE = "enchantment.protect.fire";
public const ENCHANTMENT_PROTECT_PROJECTILE = "enchantment.protect.projectile";
public const ENCHANTMENT_SOUL_SPEED = "enchantment.soul_speed";
public const ENCHANTMENT_THORNS = "enchantment.thorns";
public const ENCHANTMENT_TRIDENTCHANNELING = "enchantment.tridentChanneling";
public const ENCHANTMENT_TRIDENTIMPALING = "enchantment.tridentImpaling";
public const ENCHANTMENT_TRIDENTLOYALTY = "enchantment.tridentLoyalty";
public const ENCHANTMENT_TRIDENTRIPTIDE = "enchantment.tridentRiptide";
public const ENCHANTMENT_UNTOUCHING = "enchantment.untouching";
public const ENCHANTMENT_WATERWALKER = "enchantment.waterWalker";
public const ENCHANTMENT_WATERWORKER = "enchantment.waterWorker";
public const GAMEMODE_ADVENTURE = "gameMode.adventure"; public const GAMEMODE_ADVENTURE = "gameMode.adventure";
public const GAMEMODE_CHANGED = "gameMode.changed"; public const GAMEMODE_CHANGED = "gameMode.changed";
public const GAMEMODE_CREATIVE = "gameMode.creative"; public const GAMEMODE_CREATIVE = "gameMode.creative";

View File

@ -103,7 +103,7 @@ class ItemTest extends TestCase{
continue 2; continue 2;
} }
} }
self::fail("Unknown extra enchantment found: " . $enchantment->getType()->getName() . " x" . $enchantment->getLevel()); self::fail("Unknown extra enchantment found");
} }
self::assertEmpty($enchantments, "Expected all enchantments to be present"); self::assertEmpty($enchantments, "Expected all enchantments to be present");
} }