Skip to content

Commit

Permalink
fix: Simplifies create food and localizes. Fixes localization fallback (
Browse files Browse the repository at this point in the history
  • Loading branch information
kamronbatman committed Apr 6, 2024
1 parent 912eaab commit 6cb18f8
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 73 deletions.
12 changes: 6 additions & 6 deletions Projects/Server/Localization/Localization.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*************************************************************************
* ModernUO *
* Copyright 2019-2023 - ModernUO Development Team *
* Copyright 2019-2024 - ModernUO Development Team *
* Email: hi@modernuo.com *
* File: Localization.cs *
* *
Expand Down Expand Up @@ -44,7 +44,7 @@ public static void Configure()

public static void Add(string lang, int number, string text)
{
lang = lang.ToLower();
lang = lang?.ToLower() ?? FallbackLanguage;
var entry = new LocalizationEntry(lang, number, text);
if (!_localizations.TryGetValue(lang, out var entries))
{
Expand All @@ -61,7 +61,7 @@ public static void Add(string lang, int number, string text)

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))
{
return false;
Expand All @@ -86,7 +86,7 @@ public static void Clear()

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>();
if (lang == FallbackLanguage)
{
Expand Down Expand Up @@ -132,7 +132,7 @@ public static void Clear()
/// </summary>
/// <param name="number">Localization number</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) =>
TryGetLocalization(lang, number, out var entry) ? entry.Text : null;

Expand All @@ -154,7 +154,7 @@ public static void Clear()
/// <returns>True if the entry exists, otherwise false.</returns>
public static bool TryGetLocalization(string lang, int number, out LocalizationEntry entry)
{
lang = lang.ToLower();
lang = lang?.ToLower() ?? FallbackLanguage;
if (lang != FallbackLanguage)
{
if (!_localizations.TryGetValue(lang, out var entries))
Expand Down
111 changes: 44 additions & 67 deletions Projects/UOContent/Spells/First/CreateFood.cs
Original file line number Diff line number Diff line change
@@ -1,88 +1,65 @@
using System;
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",
"In Mani Ylem",
224,
9011,
Reagent.Garlic,
Reagent.Ginseng,
Reagent.MandrakeRoot
);
private static readonly SpellInfo _info = new(
"Create Food",
"In Mani Ylem",
224,
9011,
Reagent.Garlic,
Reagent.Ginseng,
Reagent.MandrakeRoot
);

private static readonly FoodInfo[] m_Food =
private static Item CreateRandomFood() =>
Utility.Random(10) switch
{
new(typeof(Grapes), "a grape bunch"),
new(typeof(Ham), "a ham"),
new(typeof(CheeseWedge), "a wedge of cheese"),
new(typeof(Muffins), "muffins"),
new(typeof(FishSteak), "a fish steak"),
new(typeof(Ribs), "cut of ribs"),
new(typeof(CookedBird), "a cooked bird"),
new(typeof(Sausage), "sausage"),
new(typeof(Apple), "an apple"),
new(typeof(Peach), "a peach")
0 => new Grapes(),
1 => new Ham(),
2 => new CheeseWedge(),
3 => new Muffins(),
4 => new FishSteak(),
5 => new Ribs(),
6 => new CookedBird(),
7 => new Sausage(),
8 => new Apple(),
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 SpellCircle Circle => SpellCircle.First;

public override void OnCast()
public override void OnCast()
{
if (CheckSequence())
{
if (CheckSequence())
var food = CreateRandomFood();

if (food != null)
{
var foodInfo = m_Food.RandomElement();
var food = foodInfo.Create();
Caster.AddToBackpack(food);

if (food != null)
if (Caster.NetState != null)
{
Caster.AddToBackpack(food);
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, $" {foodInfo.Name}");

Caster.FixedParticles(0, 10, 5, 2003, EffectLayer.RightHand);
Caster.PlaySound(0x1E2);
Caster.SendLocalizedMessage(1042695, true, $" {foodName}");
}
}

FinishSequence();
}
}

public class FoodInfo
{
public FoodInfo(Type type, string name)
{
Type = type;
Name = name;
}

public Type Type { get; set; }

public string Name { get; set; }

public Item Create()
{
try
{
return Type.CreateInstance<Item>();
}
catch
{
// ignored
Caster.FixedParticles(0, 10, 5, 2003, EffectLayer.RightHand);
Caster.PlaySound(0x1E2);
}

return null;
}

FinishSequence();
}
}

0 comments on commit 6cb18f8

Please sign in to comment.