fix: Simplifies create food and localizes. Fixes localization fallback (#1721)

This commit is contained in:
Kamron Batman 2024-04-05 19:45:42 -07:00 • committed by GitHub
parent 912eaab460
commit 6cb18f87fa
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 56 additions and 79 deletions

View file

@ -1,6 +1,6 @@
/************************************************************************* /*************************************************************************
* ModernUO * * ModernUO *
* Copyright 2019-2023 - ModernUO Development Team * * Copyright 2019-2024 - ModernUO Development Team *
* Email: hi@modernuo.com * * Email: hi@modernuo.com *
* File: Localization.cs * * File: Localization.cs *
* * * *
@ -44,7 +44,7 @@ public static class Localization
public static void Add(string lang, int number, string text) public static void Add(string lang, int number, string text)
{ {
lang = lang.ToLower(); lang = lang?.ToLower() ?? FallbackLanguage;
var entry = new LocalizationEntry(lang, number, text); var entry = new LocalizationEntry(lang, number, text);
if (!_localizations.TryGetValue(lang, out var entries)) if (!_localizations.TryGetValue(lang, out var entries))
{ {
@ -61,7 +61,7 @@ public static class Localization
public static bool Remove(string lang, int number) public static bool Remove(string lang, int number)
{ {
lang = lang.ToLower(); lang = lang?.ToLower() ?? FallbackLanguage;
if (!_localizations.TryGetValue(lang, out var entries) || !entries.Remove(number)) if (!_localizations.TryGetValue(lang, out var entries) || !entries.Remove(number))
{ {
return false; return false;
@ -86,7 +86,7 @@ public static class Localization
private static Dictionary<int, LocalizationEntry> LoadClilocs(string lang, string file) private static Dictionary<int, LocalizationEntry> LoadClilocs(string lang, string file)
{ {
lang = lang.ToLower(); lang = lang?.ToLower() ?? FallbackLanguage;
Dictionary<int, LocalizationEntry> entries = _localizations[lang] = new Dictionary<int, LocalizationEntry>(); Dictionary<int, LocalizationEntry> entries = _localizations[lang] = new Dictionary<int, LocalizationEntry>();
if (lang == FallbackLanguage) if (lang == FallbackLanguage)
{ {
@ -132,7 +132,7 @@ public static class Localization
/// </summary> /// </summary>
/// <param name="number">Localization number</param> /// <param name="number">Localization number</param>
/// <param name="lang">Language in ISO 639‑2 format</param> /// <param name="lang">Language in ISO 639‑2 format</param>
/// <returns>Original text for the localizaton entry</returns> /// <returns>Original text for the localization entry</returns>
public static string GetText(int number, string lang = FallbackLanguage) => public static string GetText(int number, string lang = FallbackLanguage) =>
TryGetLocalization(lang, number, out var entry) ? entry.Text : null; TryGetLocalization(lang, number, out var entry) ? entry.Text : null;
@ -154,7 +154,7 @@ public static class Localization
/// <returns>True if the entry exists, otherwise false.</returns> /// <returns>True if the entry exists, otherwise false.</returns>
public static bool TryGetLocalization(string lang, int number, out LocalizationEntry entry) public static bool TryGetLocalization(string lang, int number, out LocalizationEntry entry)
{ {
lang = lang.ToLower(); lang = lang?.ToLower() ?? FallbackLanguage;
if (lang != FallbackLanguage) if (lang != FallbackLanguage)
{ {
if (!_localizations.TryGetValue(lang, out var entries)) if (!_localizations.TryGetValue(lang, out var entries))

View file

@ -1,88 +1,65 @@
using System;
using Server.Items; using Server.Items;
using Server.Utilities;
namespace Server.Spells.First namespace Server.Spells.First;
public class CreateFoodSpell : MagerySpell
{ {
public class CreateFoodSpell : MagerySpell private static readonly SpellInfo _info = new(
{ "Create Food",
private static readonly SpellInfo _info = new( "In Mani Ylem",
"Create Food", 224,
"In Mani Ylem", 9011,
224, Reagent.Garlic,
9011, Reagent.Ginseng,
Reagent.Garlic, Reagent.MandrakeRoot
Reagent.Ginseng, );
Reagent.MandrakeRoot
);
private static readonly FoodInfo[] m_Food = private static Item CreateRandomFood() =>
Utility.Random(10) switch
{ {
new(typeof(Grapes), "a grape bunch"), 0 => new Grapes(),
new(typeof(Ham), "a ham"), 1 => new Ham(),
new(typeof(CheeseWedge), "a wedge of cheese"), 2 => new CheeseWedge(),
new(typeof(Muffins), "muffins"), 3 => new Muffins(),
new(typeof(FishSteak), "a fish steak"), 4 => new FishSteak(),
new(typeof(Ribs), "cut of ribs"), 5 => new Ribs(),
new(typeof(CookedBird), "a cooked bird"), 6 => new CookedBird(),
new(typeof(Sausage), "sausage"), 7 => new Sausage(),
new(typeof(Apple), "an apple"), 8 => new Apple(),
new(typeof(Peach), "a peach") 9 => new Peach(),
_ => null
}; };
public CreateFoodSpell(Mobile caster, Item scroll = null) : base(caster, scroll, _info) public CreateFoodSpell(Mobile caster, Item scroll = null) : base(caster, scroll, _info)
{ {
}
public override SpellCircle Circle => SpellCircle.First;
public override void OnCast()
{
if (CheckSequence())
{
var foodInfo = m_Food.RandomElement();
var food = foodInfo.Create();
if (food != null)
{
Caster.AddToBackpack(food);
// You magically create food in your backpack:
Caster.SendLocalizedMessage(1042695, true, $" {foodInfo.Name}");
Caster.FixedParticles(0, 10, 5, 2003, EffectLayer.RightHand);
Caster.PlaySound(0x1E2);
}
}
FinishSequence();
}
} }
public class FoodInfo public override SpellCircle Circle => SpellCircle.First;
public override void OnCast()
{ {
public FoodInfo(Type type, string name) if (CheckSequence())
{ {
Type = type; var food = CreateRandomFood();
Name = name;
if (food != null)
{
Caster.AddToBackpack(food);
if (Caster.NetState != null)
{
var foodName = Localization.GetText(food.LabelNumber, Caster.Language);
// Note: On OSI, the food name is not localized.
// You magically create food in your backpack:
Caster.SendLocalizedMessage(1042695, true, $" {foodName}");
}
Caster.FixedParticles(0, 10, 5, 2003, EffectLayer.RightHand);
Caster.PlaySound(0x1E2);
}
} }
public Type Type { get; set; } FinishSequence();
public string Name { get; set; }
public Item Create()
{
try
{
return Type.CreateInstance<Item>();
}
catch
{
// ignored
}
return null;
}
} }
} }