Skip to content

Commit 9e18f74

Browse files
committed
complete editor module
1 parent e7a6fc6 commit 9e18f74

File tree

6 files changed

+364
-116
lines changed

6 files changed

+364
-116
lines changed

editor/src/main/java/io/github/projectunified/minigamecore/editor/Editor.java

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,46 @@
55
import java.util.Map;
66
import java.util.Optional;
77

8-
public interface Editor<A, T> extends EditorAction<A> {
9-
Map<String, EditorAction<A>> actions();
8+
/**
9+
* The Editor
10+
*
11+
* @param <T> the type of the exported object
12+
*/
13+
public interface Editor<T> extends EditorAction {
14+
/**
15+
* The map of all available actions of the editor
16+
*
17+
* @return the action map
18+
*/
19+
Map<String, EditorAction> actions();
1020

21+
/**
22+
* Reset the editor
23+
*/
1124
void reset();
1225

26+
/**
27+
* Get an object representing the status of the editor
28+
*
29+
* @return the status
30+
*/
1331
Object status();
1432

15-
Optional<T> export(A actor);
33+
/**
34+
* Export the object from the editor
35+
*
36+
* @param actor the actor
37+
* @return the object if it's exported successfully, otherwise empty
38+
*/
39+
Optional<T> export(EditorActor actor);
1640

41+
/**
42+
* Migrate from the object
43+
*
44+
* @param data the object
45+
*/
1746
void migrate(T data);
1847

19-
default boolean sendActionUsage(A actor) {
20-
return false;
21-
}
22-
2348
@Override
2449
default String description() {
2550
return getClass().getSimpleName();
@@ -31,16 +56,16 @@ default String usage() {
3156
}
3257

3358
@Override
34-
default Collection<String> complete(A actor, String[] args) {
59+
default Collection<String> complete(EditorActor actor, String[] args) {
3560
if (args.length == 0) {
3661
return Collections.emptyList();
3762
}
38-
Map<String, EditorAction<A>> actions = actions();
63+
Map<String, EditorAction> actions = actions();
3964
if (args.length == 1) {
4065
return actions.keySet();
4166
}
4267
String actionName = args[0];
43-
EditorAction<A> action = actions.get(actionName);
68+
EditorAction action = actions.get(actionName);
4469
if (action == null) {
4570
return Collections.emptyList();
4671
}
@@ -49,12 +74,13 @@ default Collection<String> complete(A actor, String[] args) {
4974
return action.complete(actor, subArgs);
5075
}
5176

52-
default boolean execute(A actor, String[] args) {
77+
@Override
78+
default boolean execute(EditorActor actor, String[] args) {
5379
if (args.length == 0) {
54-
return sendActionUsage(actor);
80+
return actor.sendUsage(this);
5581
}
5682
String command = args[0];
57-
EditorAction<A> action = actions().get(command);
83+
EditorAction action = actions().get(command);
5884
if (action == null) {
5985
return false;
6086
}

editor/src/main/java/io/github/projectunified/minigamecore/editor/EditorAction.java

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,51 @@
33
import java.util.Collection;
44
import java.util.Collections;
55

6-
public interface EditorAction<A> {
7-
boolean execute(A actor, String[] args);
6+
/**
7+
* An action of the editor
8+
*/
9+
public interface EditorAction {
10+
/**
11+
* Execute the action
12+
*
13+
* @param actor the actor
14+
* @param args the arguments
15+
* @return true if the action is executed successfully
16+
*/
17+
boolean execute(EditorActor actor, String[] args);
818

19+
/**
20+
* Get the description of the action
21+
*
22+
* @return the description
23+
*/
924
String description();
1025

11-
default Collection<String> complete(A actor, String[] args) {
26+
/**
27+
* Get the completion suggestions, given the arguments
28+
*
29+
* @param actor the action
30+
* @param args the argument
31+
* @return the suggestions
32+
*/
33+
default Collection<String> complete(EditorActor actor, String[] args) {
1234
return Collections.emptyList();
1335
}
1436

37+
/**
38+
* Get the usage of the action
39+
*
40+
* @return the usage
41+
*/
1542
default String usage() {
1643
return "";
1744
}
1845

46+
/**
47+
* Check if the action requires arguments
48+
*
49+
* @return true if it does
50+
*/
1951
default boolean requiresArgs() {
2052
return !usage().isEmpty();
2153
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package io.github.projectunified.minigamecore.editor;
2+
3+
/**
4+
* The actor that interacts the editor
5+
*/
6+
public interface EditorActor {
7+
/**
8+
* Send the message
9+
*
10+
* @param message the message
11+
* @param success true if it's a success message
12+
*/
13+
void sendMessage(String message, boolean success);
14+
15+
/**
16+
* Send the usage of the action
17+
*
18+
* @param action the action
19+
* @return true if the usage is sent successfully
20+
*/
21+
boolean sendUsage(EditorAction action);
22+
23+
/**
24+
* Send the usage of the editor
25+
*
26+
* @param editor the editor
27+
* @return true if the usage is sent successfully
28+
*/
29+
boolean sendUsage(Editor<?> editor);
30+
}

editor/src/main/java/io/github/projectunified/minigamecore/editor/EditorStatusDisplay.java

Lines changed: 99 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,103 @@
33
import java.lang.reflect.Array;
44
import java.util.*;
55

6-
public abstract class EditorStatusDisplay<B, T> {
6+
/**
7+
* The helper class to handle editor status
8+
*
9+
* @param <B> the builder
10+
* @param <S> the section
11+
* @param <T> the object that is built from the builder
12+
*/
13+
public abstract class EditorStatusDisplay<B, S, T> {
14+
/**
15+
* Create a new builder
16+
*
17+
* @return the builder
18+
*/
719
protected abstract B newBuilder();
820

9-
protected abstract void appendPadding(B builder, int level);
10-
11-
protected abstract void appendKey(B builder, String key, boolean fromCollection);
12-
13-
protected abstract void appendSize(B builder, int size);
14-
15-
protected abstract void appendValue(B builder, Object value, int level, Editor<?, ?> editor);
16-
21+
/**
22+
* Create a new section
23+
*
24+
* @param builder the builder
25+
* @return the section
26+
*/
27+
protected abstract S newSection(B builder);
28+
29+
/**
30+
* Add padding to the section
31+
*
32+
* @param section the section
33+
* @param level the padding level
34+
*/
35+
protected abstract void appendPadding(S section, int level);
36+
37+
/**
38+
* Add key to the section
39+
*
40+
* @param section the section
41+
* @param key the key
42+
* @param fromCollection whether the key is from a collection
43+
*/
44+
protected abstract void appendKey(S section, String key, boolean fromCollection);
45+
46+
/**
47+
* Add size to the section
48+
*
49+
* @param section the section
50+
* @param size the size
51+
*/
52+
protected abstract void appendSize(S section, int size);
53+
54+
/**
55+
* Add value to the section
56+
*
57+
* @param section the section
58+
* @param value the value
59+
* @param editor the current editor
60+
*/
61+
protected abstract void appendValue(S section, Object value, Editor<?> editor);
62+
63+
/**
64+
* Add section to the builder
65+
*
66+
* @param section the section
67+
* @param builder the builder
68+
* @return the new builder
69+
*/
70+
protected abstract B addToBuilder(S section, B builder);
71+
72+
/**
73+
* Build the final object from the builder
74+
*
75+
* @param builder the builder
76+
* @return the object
77+
*/
1778
protected abstract T build(B builder);
1879

19-
protected Object preprocessValue(Object value, Editor<?, ?> editor) {
80+
/**
81+
* Preprocess the editor value
82+
*
83+
* @param value the value
84+
* @param editor the editor
85+
* @return the preprocessed object
86+
*/
87+
protected Object preprocessValue(Object value, Editor<?> editor) {
88+
while (value instanceof Editor<?>) {
89+
Editor<?> subEditor = (Editor<?>) value;
90+
value = subEditor.status();
91+
}
2092
return value;
2193
}
2294

23-
public List<T> display(Editor<?, ?> editor) {
24-
List<T> list = new ArrayList<>();
95+
/**
96+
* Display the editor by converting the editor status to an object
97+
*
98+
* @param editor the editor
99+
* @return the status object
100+
*/
101+
public T display(Editor<?> editor) {
102+
B builder = newBuilder();
25103
Deque<Entry> deque = new ArrayDeque<>();
26104
deque.addFirst(new Entry(0, "ROOT", editor, false));
27105
while (!deque.isEmpty()) {
@@ -34,32 +112,28 @@ public List<T> display(Editor<?, ?> editor) {
34112
continue;
35113
}
36114

37-
while (value instanceof Editor<?, ?>) {
38-
Editor<?, ?> subEditor = (Editor<?, ?>) value;
39-
value = subEditor.status();
40-
}
115+
S section = newSection(builder);
41116

42-
B builder = newBuilder();
43-
appendPadding(builder, level);
44-
appendKey(builder, entry.key(), entry.fromCollection());
117+
appendPadding(section, level);
118+
appendKey(section, entry.key(), entry.fromCollection());
45119
if (value instanceof Collection<?>) {
46120
Collection<?> collection = (Collection<?>) value;
47-
appendSize(builder, collection.size());
121+
appendSize(section, collection.size());
48122
List<Object> copyList = new ArrayList<>(collection);
49123
for (int i = copyList.size() - 1; i >= 0; i--) {
50124
Object o = copyList.get(i);
51125
deque.addFirst(new Entry(level + 1, Integer.toString(i), o, true));
52126
}
53127
} else if (value.getClass().isArray()) {
54128
int length = Array.getLength(value);
55-
appendSize(builder, length);
129+
appendSize(section, length);
56130
for (int i = length - 1; i >= 0; i--) {
57131
Object o = Array.get(value, i);
58132
deque.addFirst(new Entry(level + 1, Integer.toString(i), o, true));
59133
}
60134
} else if (value instanceof Map<?, ?>) {
61135
Map<?, ?> subMap = (Map<?, ?>) value;
62-
appendSize(builder, subMap.size());
136+
appendSize(section, subMap.size());
63137
List<Map.Entry<?, ?>> copyList = new ArrayList<>(subMap.entrySet());
64138
for (int i = subMap.size() - 1; i >= 0; i--) {
65139
Map.Entry<?, ?> o = copyList.get(i);
@@ -69,11 +143,11 @@ public List<T> display(Editor<?, ?> editor) {
69143
Map.Entry<?, ?> subEntry = (Map.Entry<?, ?>) value;
70144
deque.addFirst(new Entry(level + 1, Objects.toString(subEntry.getKey()), subEntry.getValue(), false));
71145
} else {
72-
appendValue(builder, value, level, editor);
146+
appendValue(section, value, editor);
73147
}
74-
list.add(build(builder));
148+
builder = addToBuilder(section, builder);
75149
}
76-
return list;
150+
return build(builder);
77151
}
78152

79153
private static final class Entry {
@@ -120,14 +194,5 @@ public boolean equals(Object obj) {
120194
public int hashCode() {
121195
return Objects.hash(level, key, value, fromCollection);
122196
}
123-
124-
@Override
125-
public String toString() {
126-
return "Entry[" +
127-
"level=" + level + ", " +
128-
"key=" + key + ", " +
129-
"value=" + value + ", " +
130-
"fromCollection=" + fromCollection + ']';
131-
}
132197
}
133198
}

0 commit comments

Comments
 (0)