Skip to content

Commit 7fa3d1d

Browse files
committed
block list
1 parent 4e86c2b commit 7fa3d1d

File tree

2 files changed

+108
-64
lines changed

2 files changed

+108
-64
lines changed

src/main/java/meteordevelopment/meteorclient/settings/AbstractRegistryListSetting.java

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,107 @@
55

66
package meteordevelopment.meteorclient.settings;
77

8-
public abstract class AbstractRegistryListSetting {
8+
import net.minecraft.nbt.NbtCompound;
9+
import net.minecraft.nbt.NbtElement;
10+
import net.minecraft.nbt.NbtList;
11+
import net.minecraft.nbt.NbtString;
12+
import net.minecraft.registry.Registry;
13+
import net.minecraft.util.Identifier;
14+
import org.apache.commons.lang3.StringUtils;
15+
16+
import java.util.ArrayList;
17+
import java.util.Collection;
18+
import java.util.List;
19+
import java.util.function.Consumer;
20+
import java.util.function.Predicate;
21+
22+
public abstract class AbstractRegistryListSetting<C extends Collection<V>, V> extends Setting<C> {
23+
public final Predicate<V> filter;
24+
public final Registry<V> registry;
25+
26+
protected AbstractRegistryListSetting(String name, String description, C defaultValue, Consumer<C> onChanged, Consumer<Setting<C>> onModuleActivated, IVisible visible, Predicate<V> filter, Registry<V> registry) {
27+
super(name, description, defaultValue, onChanged, onModuleActivated, visible);
28+
this.filter = filter;
29+
this.registry = registry;
30+
}
31+
32+
protected abstract C transferCollection(Collection<V> from);
33+
34+
@Override
35+
public final void resetImpl() {
36+
value = transferCollection(defaultValue);
37+
}
38+
39+
@Override
40+
protected final C parseImpl(String str) {
41+
String[] values = StringUtils.split(str, ',');
42+
List<V> items = new ArrayList<>(values.length);
43+
44+
try {
45+
for (String string : values) {
46+
V value = parseId(this.registry, string);
47+
if (value != null && (filter == null || filter.test(value))) items.add(value);
48+
}
49+
} catch (Exception ignored) {}
50+
51+
return transferCollection(items);
52+
}
53+
54+
@Override
55+
protected boolean isValueValid(C value) {
56+
return true;
57+
}
58+
59+
@Override
60+
public final Iterable<Identifier> getIdentifierSuggestions() {
61+
return this.registry.getIds();
62+
}
63+
64+
@Override
65+
protected final NbtCompound save(NbtCompound tag) {
66+
NbtList valueTag = new NbtList();
67+
for (V value : get()) {
68+
valueTag.add(NbtString.of(this.registry.getId(value).toString()));
69+
}
70+
tag.put("value", valueTag);
71+
72+
return tag;
73+
}
74+
75+
@Override
76+
protected final C load(NbtCompound tag) {
77+
get().clear();
78+
79+
NbtList valueTag = tag.getListOrEmpty("value");
80+
for (NbtElement tagI : valueTag) {
81+
V value = this.registry.get(Identifier.of(tagI.asString().orElse("")));
82+
83+
if (filter == null || filter.test(value)) get().add(value);
84+
}
85+
86+
return get();
87+
}
88+
89+
// public static class Builder extends SettingBuilder<BlockListSetting.Builder, List<Block>, BlockListSetting> {
90+
// private Predicate<Block> filter;
91+
//
92+
// public Builder() {
93+
// super(new ArrayList<>(0));
94+
// }
95+
//
96+
// public BlockListSetting.Builder defaultValue(Block... defaults) {
97+
// return defaultValue(defaults != null ? Arrays.asList(defaults) : new ArrayList<>());
98+
// }
99+
//
100+
// public BlockListSetting.Builder filter(Predicate<Block> filter) {
101+
// this.filter = filter;
102+
// return this;
103+
// }
104+
//
105+
// @Override
106+
// public BlockListSetting build() {
107+
// return new BlockListSetting(name, description, defaultValue, onChanged, onModuleActivated, filter, visible);
108+
// }
109+
// }
110+
9111
}

src/main/java/meteordevelopment/meteorclient/settings/BlockListSetting.java

Lines changed: 5 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -6,81 +6,23 @@
66
package meteordevelopment.meteorclient.settings;
77

88
import net.minecraft.block.Block;
9-
import net.minecraft.nbt.NbtCompound;
10-
import net.minecraft.nbt.NbtElement;
11-
import net.minecraft.nbt.NbtList;
12-
import net.minecraft.nbt.NbtString;
139
import net.minecraft.registry.Registries;
14-
import net.minecraft.util.Identifier;
1510

1611
import java.util.ArrayList;
1712
import java.util.Arrays;
13+
import java.util.Collection;
1814
import java.util.List;
1915
import java.util.function.Consumer;
2016
import java.util.function.Predicate;
2117

22-
public class BlockListSetting extends Setting<List<Block>> {
23-
public final Predicate<Block> filter;
24-
18+
public class BlockListSetting extends AbstractRegistryListSetting<List<Block>, Block> {
2519
public BlockListSetting(String name, String description, List<Block> defaultValue, Consumer<List<Block>> onChanged, Consumer<Setting<List<Block>>> onModuleActivated, Predicate<Block> filter, IVisible visible) {
26-
super(name, description, defaultValue, onChanged, onModuleActivated, visible);
27-
28-
this.filter = filter;
29-
}
30-
31-
@Override
32-
public void resetImpl() {
33-
value = new ArrayList<>(defaultValue);
34-
}
35-
36-
@Override
37-
protected List<Block> parseImpl(String str) {
38-
String[] values = str.split(",");
39-
List<Block> blocks = new ArrayList<>(values.length);
40-
41-
try {
42-
for (String value : values) {
43-
Block block = parseId(Registries.BLOCK, value);
44-
if (block != null && (filter == null || filter.test(block))) blocks.add(block);
45-
}
46-
} catch (Exception ignored) {}
47-
48-
return blocks;
49-
}
50-
51-
@Override
52-
protected boolean isValueValid(List<Block> value) {
53-
return true;
20+
super(name, description, defaultValue, onChanged, onModuleActivated, visible, filter, Registries.BLOCK);
5421
}
5522

5623
@Override
57-
public Iterable<Identifier> getIdentifierSuggestions() {
58-
return Registries.BLOCK.getIds();
59-
}
60-
61-
@Override
62-
protected NbtCompound save(NbtCompound tag) {
63-
NbtList valueTag = new NbtList();
64-
for (Block block : get()) {
65-
valueTag.add(NbtString.of(Registries.BLOCK.getId(block).toString()));
66-
}
67-
tag.put("value", valueTag);
68-
69-
return tag;
70-
}
71-
72-
@Override
73-
protected List<Block> load(NbtCompound tag) {
74-
get().clear();
75-
76-
NbtList valueTag = tag.getListOrEmpty("value");
77-
for (NbtElement tagI : valueTag) {
78-
Block block = Registries.BLOCK.get(Identifier.of(tagI.asString().orElse("")));
79-
80-
if (filter == null || filter.test(block)) get().add(block);
81-
}
82-
83-
return get();
24+
protected List<Block> transferCollection(Collection<Block> from) {
25+
return new ArrayList<>(from);
8426
}
8527

8628
public static class Builder extends SettingBuilder<Builder, List<Block>, BlockListSetting> {

0 commit comments

Comments
 (0)