Skip to content

Commit 1de8740

Browse files
committed
Events?
1 parent 18154e0 commit 1de8740

File tree

5 files changed

+185
-31
lines changed

5 files changed

+185
-31
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/**
2+
* LightConfig
3+
* A config library.
4+
* <p>
5+
* Copyright (C) 2025 lowercasebtw
6+
* Copyright (C) 2025 mixces
7+
* Copyright (C) 2025 Contributors to the project retain their copyright
8+
* <p>
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU General Public License as published by
11+
* the Free Software Foundation, either version 3 of the License, or
12+
* (at your option) any later version.
13+
* <p>
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU General Public License for more details.
18+
* <p>
19+
* You should have received a copy of the GNU General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
* <p>
22+
* "MINECRAFT" LINKING EXCEPTION TO THE GPL
23+
*/
24+
25+
package org.visuals.legacy.lightconfig.lib.v1.events;
26+
27+
public abstract class CancellableEvent extends Event {
28+
private boolean cancelled = false;
29+
30+
public boolean isCancelled() {
31+
return this.cancelled;
32+
}
33+
34+
public void setCancelled(boolean cancelled) {
35+
this.cancelled = cancelled;
36+
}
37+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.visuals.legacy.lightconfig.lib.v1.events;
2+
3+
public class ConfigValueChanged<T> extends Event {
4+
private final T oldValue;
5+
private final T newValue;
6+
7+
public ConfigValueChanged(T oldValue, T newValue) {
8+
this.oldValue = oldValue;
9+
this.newValue = newValue;
10+
}
11+
12+
public T getOldValue() {
13+
return this.oldValue;
14+
}
15+
16+
public T getNewValue() {
17+
return this.newValue;
18+
}
19+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* LightConfig
3+
* A config library.
4+
* <p>
5+
* Copyright (C) 2025 lowercasebtw
6+
* Copyright (C) 2025 mixces
7+
* Copyright (C) 2025 Contributors to the project retain their copyright
8+
* <p>
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU General Public License as published by
11+
* the Free Software Foundation, either version 3 of the License, or
12+
* (at your option) any later version.
13+
* <p>
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU General Public License for more details.
18+
* <p>
19+
* You should have received a copy of the GNU General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
* <p>
22+
* "MINECRAFT" LINKING EXCEPTION TO THE GPL
23+
*/
24+
25+
package org.visuals.legacy.lightconfig.lib.v1.events;
26+
27+
public abstract class Event {
28+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/**
2+
* LightConfig
3+
* A config library.
4+
* <p>
5+
* Copyright (C) 2025 lowercasebtw
6+
* Copyright (C) 2025 mixces
7+
* Copyright (C) 2025 Contributors to the project retain their copyright
8+
* <p>
9+
* This program is free software: you can redistribute it and/or modify
10+
* it under the terms of the GNU General Public License as published by
11+
* the Free Software Foundation, either version 3 of the License, or
12+
* (at your option) any later version.
13+
* <p>
14+
* This program is distributed in the hope that it will be useful,
15+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
16+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17+
* GNU General Public License for more details.
18+
* <p>
19+
* You should have received a copy of the GNU General Public License
20+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
21+
* <p>
22+
* "MINECRAFT" LINKING EXCEPTION TO THE GPL
23+
*/
24+
25+
package org.visuals.legacy.lightconfig.lib.v1.events;
26+
27+
import java.util.List;
28+
import java.util.Map;
29+
import java.util.concurrent.ConcurrentHashMap;
30+
import java.util.concurrent.CopyOnWriteArrayList;
31+
import java.util.function.Consumer;
32+
33+
public final class EventManager {
34+
private final Map<Class<?>, List<Consumer<? super Event>>> listeners = new ConcurrentHashMap<>();
35+
36+
public <T extends Event> void listen(Class<T> eventClass, Consumer<? super T> consumer) {
37+
listeners.computeIfAbsent(eventClass, __ -> new CopyOnWriteArrayList<>()).add((Consumer<? super Event>) consumer);
38+
}
39+
40+
public <T extends Event> T dispatch(T event) {
41+
final Class<?> eventClass = event.getClass();
42+
for (var entry : listeners.entrySet()) {
43+
if (!entry.getKey().isAssignableFrom(eventClass)) {
44+
continue;
45+
}
46+
47+
for (Consumer<? super Event> consumer : entry.getValue()) {
48+
consumer.accept(event);
49+
if (event instanceof CancellableEvent cancellableEvent && cancellableEvent.isCancelled()) {
50+
return event;
51+
}
52+
}
53+
}
54+
55+
return event;
56+
}
57+
}
58+

src/main/java/org/visuals/legacy/lightconfig/lib/v1/field/AbstractConfigField.java

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -26,49 +26,61 @@
2626

2727
import net.minecraft.client.gui.components.AbstractWidget;
2828
import org.visuals.legacy.lightconfig.lib.v1.Config;
29+
import org.visuals.legacy.lightconfig.lib.v1.events.ConfigValueChanged;
30+
import org.visuals.legacy.lightconfig.lib.v1.events.EventManager;
2931
import org.visuals.legacy.lightconfig.lib.v1.serialization.ConfigDeserializer;
3032
import org.visuals.legacy.lightconfig.lib.v1.serialization.ConfigSerializer;
3133

34+
import java.util.function.BiConsumer;
35+
3236
public abstract class AbstractConfigField<T> {
33-
protected final Config config;
34-
protected final String name;
35-
protected final T defaultValue;
36-
protected T value;
37+
protected final Config config;
38+
protected final String name;
39+
protected final T defaultValue;
40+
protected T value;
41+
protected EventManager eventManager;
42+
43+
public AbstractConfigField(final Config config, final String name, final T defaultValue) {
44+
this.config = config;
45+
this.name = name;
46+
this.value = defaultValue;
47+
this.defaultValue = defaultValue;
48+
}
3749

38-
public AbstractConfigField(final Config config, final String name, final T defaultValue) {
39-
this.config = config;
40-
this.name = name;
41-
this.value = defaultValue;
42-
this.defaultValue = defaultValue;
43-
}
50+
public abstract void load(ConfigDeserializer<?> deserializer) throws Exception;
4451

45-
public abstract void load(ConfigDeserializer<?> deserializer) throws Exception;
52+
public abstract void save(ConfigSerializer<?> serializer) throws Exception;
4653

47-
public abstract void save(ConfigSerializer<?> serializer) throws Exception;
54+
public abstract AbstractWidget createWidget();
4855

49-
public abstract AbstractWidget createWidget();
56+
public String getTranslationKey() {
57+
return String.format("options.%s.%s", this.config.getModContainer().getMetadata().getId(), this.getName());
58+
}
5059

51-
public String getTranslationKey() {
52-
return String.format("options.%s.%s", this.config.getModContainer().getMetadata().getId(), this.getName());
53-
}
60+
public String getName() {
61+
return name;
62+
}
5463

55-
public String getName() {
56-
return name;
57-
}
64+
public T getValue() {
65+
return value;
66+
}
5867

59-
public T getValue() {
60-
return value;
61-
}
68+
public void setValue(T value) {
69+
this.eventManager.dispatch(new ConfigValueChanged<>(this.value, value));
70+
this.value = value;
71+
}
6272

63-
public void setValue(T value) {
64-
this.value = value;
65-
}
73+
public T getDefaultValue() {
74+
return this.defaultValue;
75+
}
6676

67-
public T getDefaultValue() {
68-
return this.defaultValue;
69-
}
77+
public void restore() {
78+
this.setValue(this.getDefaultValue());
79+
}
7080

71-
public void restore() {
72-
this.setValue(this.getDefaultValue());
73-
}
81+
public void onValueChanged(BiConsumer<T, T> biConsumer) {
82+
this.eventManager.listen(ConfigValueChanged.class, event -> {
83+
biConsumer.accept((T) event.getOldValue(), (T) event.getNewValue());
84+
});
85+
}
7486
}

0 commit comments

Comments
 (0)