Skip to content

Commit

Permalink
Allow EnvPreset to be useable by plugins + minor code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
Goodlyay committed Jun 7, 2024
1 parent 8088496 commit 0872171
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 30 deletions.
30 changes: 6 additions & 24 deletions MCGalaxy/Commands/CPE/CmdEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ software distributed under the Licenses are distributed on an "AS IS"
permissions and limitations under the Licenses.
*/
using System;
using System.IO;

namespace MCGalaxy.Commands.CPE
{
Expand All @@ -29,7 +28,7 @@ public sealed class CmdEnvironment : Command2

public override void Use(Player p, string message, CommandData data) {
if (message.CaselessEq("preset")) {
MessagePresets(p); return;
ExplainPresets(p); return;
}

Level lvl = null;
Expand Down Expand Up @@ -59,8 +58,8 @@ public override void Use(Player p, string message, CommandData data) {

internal static bool Handle(Player p, Level lvl, string type, string value, EnvConfig cfg, string area) {
if (type.CaselessEq("preset")) {
EnvPreset preset = FindPreset(value);
if (preset == null) { MessagePresets(p); return false; }
EnvPreset preset = EnvPreset.Find(value);
if (preset == null) { ExplainPresets(p); return false; }

cfg.SkyColor = preset.Sky;
cfg.CloudColor = preset.Clouds;
Expand Down Expand Up @@ -96,27 +95,10 @@ static void SendEnv(Level lvl) {
pl.SendCurrentEnv();
}
}

static EnvPreset FindPreset(string value) {
EnvPreset preset = EnvPreset.Find(value);
if (preset != null) return preset;

if (File.Exists("presets/" + value.ToLower() + ".env")) {
string text = File.ReadAllText("presets/" + value.ToLower() + ".env");
return new EnvPreset(text);
}
return null;
}

static void MessagePresets(Player p) {
static void ExplainPresets(Player p) {
p.Message("&T/Env preset [type] &H- Applies an env preset on the map");
p.Message("&HPresets: &f{0}", EnvPreset.Presets.Join(pr => pr.Key));

string[] files = AtomicIO.TryGetFiles("presets", "*.env");
if (files == null) return;

string all = files.Join(f => Path.GetFileNameWithoutExtension(f));
if (all.Length > 0) p.Message("&HCustom presets: &f" + all);
EnvPreset.ListFor(p);
}

public override void Help(Player p) {
Expand All @@ -133,7 +115,7 @@ public override void Help(Player p, string message) {
p.Message("&HUse &T/Help env [variable] &Hto see details for that variable");
return;
} else if (message.CaselessEq("presets")) {
MessagePresets(p); return;
ExplainPresets(p); return;
}

EnvOption opt = EnvOptions.Find(message);
Expand Down
38 changes: 32 additions & 6 deletions MCGalaxy/Levels/EnvPreset.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,16 @@ permissions and limitations under the Licenses.
*/
using System;
using System.Collections.Generic;
using System.IO;

namespace MCGalaxy {
internal sealed class EnvPreset {
public string Fog, Sky, Clouds, Sun, Shadow;
public string LavaLight = "", LampLight = "";
public sealed class EnvPreset {

const string FOLDER = "presets";
const string FILE_EXTENSION = ".env";

public readonly string Fog, Sky, Clouds, Sun, Shadow;
public readonly string LavaLight = "", LampLight = "";

public EnvPreset(string raw) {
string[] args = raw.SplitSpaces();
Expand All @@ -30,7 +35,7 @@ public EnvPreset(string raw) {
LampLight = args.Length > 6 ? args[6] : "";
}

public static Dictionary<string, string> Presets = new Dictionary<string, string>() {
static Dictionary<string, string> Presets = new Dictionary<string, string>() {
// fog sky clouds sun shadow
{ "Cartoon", "00FFFF 1E90FF 00BFFF F5DEB3 F4A460" },
{ "Noir", "000000 1F1F1F 000000 696969 1F1F1F" },
Expand All @@ -43,12 +48,33 @@ public EnvPreset(string raw) {
{ "Midnight", "131947 070A23 1E223A 181828 0F0F19" },
{ "Normal", " " },
};

public static EnvPreset Find(string name) {

public static EnvPreset Find(string value) {
EnvPreset preset = FindDefault(value);
if (preset != null) return preset;

if (File.Exists(FOLDER + "/" + value.ToLower() + FILE_EXTENSION)) {
string text = File.ReadAllText(FOLDER + "/" + value.ToLower() + FILE_EXTENSION);
return new EnvPreset(text);
}
return null;
}

static EnvPreset FindDefault(string name) {
foreach (var kvp in Presets) {
if (kvp.Key.CaselessEq(name)) return new EnvPreset(kvp.Value);
}
return null;
}

public static void ListFor(Player p) {
p.Message("&HPresets: &f{0}", Presets.Join(pr => pr.Key));

string[] files = AtomicIO.TryGetFiles(FOLDER, "*" + FILE_EXTENSION);
if (files == null) return;

string all = files.Join(f => Path.GetFileNameWithoutExtension(f));
if (all.Length > 0) p.Message("&HCustom presets: &f" + all);
}
}
}

0 comments on commit 0872171

Please sign in to comment.