aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/com/anthonyhilyard/iceberg/compat/configured/IcebergFolderEntry.java
blob: c9d361253eb7bdab189ff5ccb5ecfda5774910b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package com.anthonyhilyard.iceberg.compat.configured;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Nullable;

import com.anthonyhilyard.iceberg.config.IcebergConfigSpec;
import com.electronwill.nightconfig.core.UnmodifiableConfig;
import com.google.common.collect.ImmutableList;
import com.mrcrayfish.configured.api.IConfigEntry;
import com.mrcrayfish.configured.api.IConfigValue;
import com.mrcrayfish.configured.api.ValueEntry;
import com.mrcrayfish.configured.impl.forge.ForgeEnumValue;
import com.mrcrayfish.configured.impl.forge.ForgeListValue;
import com.mrcrayfish.configured.impl.forge.ForgeValue;

import net.minecraft.client.resources.language.I18n;
import net.minecraft.network.chat.Component;
import net.minecraftforge.common.ForgeConfigSpec;

public class IcebergFolderEntry implements IConfigEntry
{
	protected final List<String> path;
	protected final UnmodifiableConfig config;
	protected final IcebergConfigSpec spec;
	private final String customComment;
	protected List<IConfigEntry> entries;

	public IcebergFolderEntry(UnmodifiableConfig config, IcebergConfigSpec spec)
	{
		this(new ArrayList<>(), config, spec);
	}

	public IcebergFolderEntry(List<String> path, UnmodifiableConfig config, IcebergConfigSpec spec)
	{
		this(path, config, spec, null);
	}

	public IcebergFolderEntry(List<String> path, UnmodifiableConfig config, IcebergConfigSpec spec, String customComment)
	{
		this.path = path;
		this.config = config;
		this.spec = spec;
		this.customComment = customComment;
	}

	@Override
	@SuppressWarnings("unchecked")
	public List<IConfigEntry> getChildren()
	{
		if (this.entries == null)
		{
			ImmutableList.Builder<IConfigEntry> builder = ImmutableList.builder();

			this.config.valueMap().forEach((key, value) ->
			{
				if (value instanceof ForgeConfigSpec.ValueSpec valueSpec && valueSpec.getDefault() instanceof UnmodifiableConfig)
				{
					value = valueSpec.getDefault();
				}

				if (value instanceof UnmodifiableConfig)
				{
					List<String> path = new ArrayList<>(this.path);
					path.add(key);
					builder.add(new IcebergFolderEntry(path, (UnmodifiableConfig) value, this.spec));
				}
				else if (value instanceof ForgeConfigSpec.ConfigValue<?> configValue)
				{
					ForgeConfigSpec.ValueSpec valueSpec = this.spec.getRaw(configValue.getPath());
					if (valueSpec != null)
					{
						if (configValue.get() instanceof List<?>)
						{
							builder.add(new ValueEntry(new ForgeListValue((ForgeConfigSpec.ConfigValue<List<?>>) configValue, valueSpec)));
						}
						else if (configValue.get() instanceof Enum<?>)
						{
							builder.add(new ValueEntry(new ForgeEnumValue<>((ForgeConfigSpec.EnumValue<?>) configValue, valueSpec)));
						}
						// Configured doesn't currently support map configuration values, so leave a message for users.
						else if (configValue.get() instanceof UnmodifiableConfig)
						{
							List<String> path = new ArrayList<>(this.path);
							path.add(key);
							builder.add(new IcebergFolderEntry(path, (UnmodifiableConfig) configValue.get(), this.spec, "This is a map configuration value, which is not currently supported by Configured. To edit this value, please modify the configuration file directly."));
						}
						else
						{
							builder.add(new ValueEntry(new ForgeValue<>(configValue, valueSpec)));
						}
					}
				}
			});
			this.entries = builder.build();
		}
		return this.entries;
	}

	@Override
	public boolean isRoot()
	{
		return this.path.isEmpty();
	}

	@Override
	public boolean isLeaf()
	{
		return false;
	}

	@Override
	public IConfigValue<?> getValue()
	{
		return null;
	}

	@Override
	public String getEntryName()
	{
		return ForgeValue.lastValue(this.path, "Root");
	}

	@Nullable
	@Override
	public Component getTooltip()
	{
		if (customComment != null)
		{
			return Component.translatable(customComment);
		}
		else
		{
			String translationKey = this.getTranslationKey();
			if (translationKey != null)
			{
				String tooltipKey = translationKey + ".tooltip";
				if (I18n.exists(tooltipKey))
				{
					return Component.translatable(tooltipKey);
				}
			}
			String comment = this.spec.getLevelComment(this.path);
			if (comment != null)
			{
				return Component.literal(comment);
			}
			return null;
		}
	}

	@Nullable
	@Override
	public String getTranslationKey()
	{
		return this.spec.getLevelTranslationKey(this.path);
	}
	
}