forked from MeteorDevelopment/meteor-client
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSettingGroup.java
More file actions
89 lines (68 loc) · 2.39 KB
/
Copy pathSettingGroup.java
File metadata and controls
89 lines (68 loc) · 2.39 KB
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
/*
* This file is part of the Meteor Client distribution (https://github.com/MeteorDevelopment/meteor-client).
* Copyright (c) Meteor Development.
*/
package meteordevelopment.meteorclient.settings;
import meteordevelopment.meteorclient.utils.misc.ISerializable;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.nbt.NbtElement;
import net.minecraft.nbt.NbtList;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class SettingGroup implements ISerializable<SettingGroup>, Iterable<Setting<?>> {
public final String name;
public boolean sectionExpanded;
final List<Setting<?>> settings = new ArrayList<>(1);
SettingGroup(String name, boolean sectionExpanded) {
this.name = name;
this.sectionExpanded = sectionExpanded;
}
public Setting<?> get(String name) {
for (Setting<?> setting : this) {
if (setting.name.equals(name)) return setting;
}
return null;
}
public <T extends Setting<?>> T add(T setting) {
settings.add(setting);
return setting;
}
public Setting<?> getByIndex(int index) {
return settings.get(index);
}
public boolean wasChanged() {
for (Setting<?> setting : settings) {
if (setting.wasChanged()) return true;
}
return false;
}
@Override
public @NotNull Iterator<Setting<?>> iterator() {
return settings.iterator();
}
@Override
public NbtCompound toTag() {
NbtCompound tag = new NbtCompound();
tag.putString("name", name);
tag.putBoolean("sectionExpanded", sectionExpanded);
NbtList settingsTag = new NbtList();
for (Setting<?> setting : this) {
if (setting.wasChanged()) settingsTag.add(setting.toTag());
}
if (!settingsTag.isEmpty()) tag.put("settings", settingsTag);
return tag;
}
@Override
public SettingGroup fromTag(NbtCompound tag) {
sectionExpanded = tag.getBoolean("sectionExpanded", false);
NbtList settingsTag = tag.getListOrEmpty("settings");
for (NbtElement t : settingsTag) {
NbtCompound settingTag = (NbtCompound) t;
Setting<?> setting = get(settingTag.getString("name", ""));
if (setting != null) setting.fromTag(settingTag);
}
return this;
}
}