generated from kikugie/mod-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Defaults.java
163 lines (153 loc) · 6.58 KB
/
Defaults.java
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
161
162
163
package dev.kikugie.commandconfig.api.util;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.datafixers.util.Pair;
import dev.kikugie.commandconfig.Reference;
import dev.kikugie.commandconfig.api.option.access.ListElementAccess;
import dev.kikugie.commandconfig.api.option.access.OptionValueAccess;
import net.minecraft.command.CommandSource;
import net.minecraft.server.command.ServerCommandSource;
import net.minecraft.text.Text;
import org.slf4j.Logger;
import java.util.List;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Supplier;
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource;
@SuppressWarnings("unused")
public class Defaults {
/**
* Provides default value access with following responses:
* <pre>{@code
* - get: Option value: <value>
* - set: Option value set to: <value>
* }</pre>
*
* @param getter Value getter
* @param setter Value setter
* @param <T> Value type
* @return {@link OptionValueAccess} to be passed to an option
*/
public static <T, S extends CommandSource> OptionValueAccess<T, S> defaultValueAccess(Supplier<T> getter, Consumer<T> setter) {
return new OptionValueAccess<>(
(context) -> Reference.translated("commandconfig.response.option.get", getter.get()),
(context, val) -> {
setter.accept(val);
return Reference.translated("commandconfig.response.option.set", val);
});
}
/**
* Provides default list element access with following responses:
* <pre>{@code
* - get: Element value: <value>
* - set: Set element value <value> at index <index>
* - add: Added element value <value> at index <index>
* - remove: Removed element value <value> at index <index>
* - invalid index: Index <index> is out of bounds
* }</pre>
*
* @param listSupplier Supplier that returns target list reference
* @param <L> List type
* @param <T> Value type
* @return {@link ListElementAccess} to be passed to a list option
*/
public static <L extends List<T>, T, S extends CommandSource> ListElementAccess<T, S> defaultElementAccess(Supplier<L> listSupplier) {
return defaultElementAccess("commandconfig.response.option.element", listSupplier);
}
/**
* Alternative default element access provider with specified translation key.
* For given key following variants should exist:
* <pre>{@code
* - "{key}.get"
* - "{key}.set"
* - "{key}.add"
* - "{key}.remove"
* }</pre>
*
* @param listSupplier Supplier that returns target list reference
* @param <L> List type
* @param <T> Value type
* @return {@link ListElementAccess} to be passed to a list option
*/
public static <L extends List<T>, T, S extends CommandSource> ListElementAccess<T, S> defaultElementAccess(String baseTranslationKey, Supplier<L> listSupplier) {
return new ListElementAccess<>(
(context, index) -> {
L list = listSupplier.get();
return index >= 0 && index < list.size() ? Reference.translated(baseTranslationKey + ".get", list.get(index)) : Reference.translated("commandconfig.response.error.invalid_index", index, list.size());
},
(context, index, value) -> {
L list = listSupplier.get();
if (index >= 0 && index < list.size()) {
list.set(index, value);
return Reference.translated(baseTranslationKey + ".set", value, index);
}
return Reference.translated("commandconfig.response.error.invalid_index", index, list.size());
},
(context, value) -> {
L list = listSupplier.get();
list.add(value);
return Reference.translated(baseTranslationKey + ".add", value, list.size());
},
(context, index) -> {
L list = listSupplier.get();
if (index >= 0 && index < list.size()) {
T value = list.get(index);
// Uses removeAll to avoid remove method ambiguity
list.removeAll(List.of(value));
return new Pair<>(value, Reference.translated(baseTranslationKey + ".remove", value, index));
}
return new Pair<>(null, Reference.translated("commandconfig.response.error.invalid_index", index, list.size()));
});
}
/**
* Client-side print function that responds in the game chat.
*
* @return Print function to be passed to {@code printFunc()} method
*/
public static BiFunction<CommandContext<FabricClientCommandSource>, Text, Integer> clientPrintFunc() {
return (context, text) -> {
context.getSource().sendFeedback(text);
return 1;
};
}
/**
* Server-side print function that responds to the source player in the game chat.
*
* @return Print function to be passed to {@code printFunc()} method
*/
public static BiFunction<CommandContext<ServerCommandSource>, Text, Integer> serverPrintFunc() {
return (context, text) -> {
//#if MC >= 12000
context.getSource().sendFeedback(() -> text, false);
//#else
//$$ context.getSource().sendFeedback(text, false);
//#endif
return 1;
};
}
/**
* Server-side print function that responds to the source player in the game chat and broadcasts to server operators.
*
* @return Print function to be passed to {@code printFunc()} method
*/
public static BiFunction<CommandContext<ServerCommandSource>, Text, Integer> broadcastPrintFunc() {
return (context, text) -> {
//#if MC >= 12000
context.getSource().sendFeedback(() -> text, true);
//#else
//$$ context.getSource().sendFeedback(text, true);
//#endif
return 1;
};
}
/**
* Print function that only writes responses to the provided logger.
*
* @return Print function to be passed to {@code printFunc()} method
*/
public static BiFunction<CommandContext<CommandContext<?>>, Text, Integer> loggerPrintFunc(Logger logger) {
return (context, text) -> {
logger.info(text.toString());
return 1;
};
}
}