Skip to content

Commit 3208ab8

Browse files
committed
* Un-deprecated and re-purposed Applicable#apply
* Added silent array processing. * Added new Comment annotation. * Added new Counter object for cross-thread atomic usage. * Optimized skull cache. * Added new scoreboard utility mimicking the tablist utility. * Added new WebResponse interface, static method available for downloading resources.
1 parent b00ca8a commit 3208ab8

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+910
-64
lines changed

labyrinth-common/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<artifactId>labyrinth</artifactId>
77
<groupId>com.github.the-h-team</groupId>
8-
<version>1.7.6</version>
8+
<version>1.7.7</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
1111

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.github.sanctum.labyrinth.annotation;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
/**
9+
* <p>A developer comment.</p>
10+
*/
11+
@Retention(RetentionPolicy.RUNTIME)
12+
@Target(value = {ElementType.TYPE, ElementType.METHOD, ElementType.PARAMETER, ElementType.FIELD, ElementType.CONSTRUCTOR, ElementType.ANNOTATION_TYPE, ElementType.TYPE_USE})
13+
public @interface Comment {
14+
15+
/**
16+
* @return The developer comment for this example.
17+
*/
18+
String value() default "no comment";
19+
20+
}

labyrinth-common/src/main/java/com/github/sanctum/labyrinth/data/Configurable.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public static void registerClass(@NotNull Class<? extends JsonAdapter<?>> c, Obj
156156
* @return The desired Json element adapter or null if non existent.
157157
*/
158158
public static <V> JsonAdapter<V> getAdapter(@NotNull Class<V> type) {
159-
return serializers.entrySet().stream().filter(e -> e.getKey().equals(type.getName()) || type.isAssignableFrom(e.getValue().getSubClass())).map(Map.Entry::getValue).map(c -> (JsonAdapter<V>) c).findFirst().orElse(null);
159+
return serializers.entrySet().stream().filter(e -> e.getKey().equals(type.getName()) || type.isAssignableFrom(e.getValue().getClassType())).map(Map.Entry::getValue).map(c -> (JsonAdapter<V>) c).findFirst().orElse(null);
160160
}
161161

162162
public static <V> JsonAdapter<V> getAdapter(@NotNull String pointer) {

labyrinth-common/src/main/java/com/github/sanctum/labyrinth/data/JsonAdapter.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.github.sanctum.labyrinth.data;
22

33
import com.github.sanctum.labyrinth.annotation.Note;
4-
import com.github.sanctum.labyrinth.library.TypeFlag;
54
import com.google.gson.GsonBuilder;
65
import com.google.gson.InstanceCreator;
76
import com.google.gson.JsonElement;
@@ -37,13 +36,12 @@ public interface JsonAdapter<T> extends InstanceCreator<T> {
3736
*/
3837
T read(Map<String, Object> object);
3938

40-
@Deprecated
4139
Class<T> getClassType();
4240

4341
/**
4442
* @return The class this serializer represents in relation to T.
4543
*/
46-
@Note("To be overridden!")
44+
@Deprecated
4745
default Class<? extends T> getSubClass() {
4846
return getClassType();
4947
}
@@ -52,9 +50,9 @@ default Class<? extends T> getSubClass() {
5250
@Note("Non bare constructors should have this method overridden!")
5351
default T createInstance(Type type) {
5452
Class<?> c = TypeToken.get(type).getRawType();
55-
if (getSubClass().isAssignableFrom(c)) {
53+
if (getClassType().isAssignableFrom(c)) {
5654
try {
57-
return getSubClass().cast(c.getDeclaredConstructor().newInstance());
55+
return getClassType().cast(c.getDeclaredConstructor().newInstance());
5856
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
5957
e.printStackTrace();
6058
}
@@ -64,7 +62,7 @@ default T createInstance(Type type) {
6462

6563
static GsonBuilder getJsonBuilder() {
6664
GsonBuilder builder = new GsonBuilder();
67-
Configurable.serializers.forEach((key, value) -> builder.registerTypeHierarchyAdapter(value.getSubClass(), value));
65+
Configurable.serializers.forEach((key, value) -> builder.registerTypeHierarchyAdapter(value.getClassType(), value));
6866
return builder;
6967
}
7068

@@ -82,5 +80,9 @@ static void register(Class<? extends JsonAdapter<?>> adapterClass, Object... arg
8280
Configurable.registerClass(adapterClass, args);
8381
}
8482

83+
static <T> JsonAdapter<T> get(Class<T> c) {
84+
return Configurable.getAdapter(c);
85+
}
86+
8587

8688
}

labyrinth-common/src/main/java/com/github/sanctum/labyrinth/data/JsonAdapterInput.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public T deserialize(JsonElement jsonElement, Type type, JsonDeserializationCont
4949

5050
public String getKey() {
5151
String test = AnnotationDiscovery.of(NodePointer.class, serializer).mapFromClass((r, u) -> r.value());
52-
return test != null ? test : AnnotationDiscovery.of(NodePointer.class, serializer.getSubClass()).mapFromClass((r, u) -> r.value());
52+
return test != null ? test : AnnotationDiscovery.of(NodePointer.class, serializer.getClassType()).mapFromClass((r, u) -> r.value());
5353
}
5454

5555
@Override

labyrinth-common/src/main/java/com/github/sanctum/labyrinth/data/JsonConfiguration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ Object checkObject(Type type, boolean array, Object object) {
198198
JSONObject j = (JSONObject) object;
199199
Gson g = JsonAdapter.getJsonBuilder().create();
200200

201-
Map.Entry<String, JsonAdapterInput<?>> d = serializers.entrySet().stream().filter(de -> de.getKey().equals(cl.getTypeName()) || cl.isAssignableFrom(de.getValue().getSubClass())).findFirst().orElse(null);
201+
Map.Entry<String, JsonAdapterInput<?>> d = serializers.entrySet().stream().filter(de -> de.getKey().equals(cl.getTypeName()) || cl.isAssignableFrom(de.getValue().getClassType())).findFirst().orElse(null);
202202
if (d != null) {
203203
if (j.containsKey(d.getKey())) {
204204
Object ob = j.get(d.getKey());
@@ -218,7 +218,7 @@ Object checkObject(Type type, boolean array, Object object) {
218218
}
219219
if (target instanceof JSONArray && array) {
220220
JSONArray j = (JSONArray) object;
221-
Map.Entry<String, JsonAdapterInput<?>> d = serializers.entrySet().stream().filter(de -> cl.isAssignableFrom(de.getValue().getSubClass())).findFirst().orElse(null);
221+
Map.Entry<String, JsonAdapterInput<?>> d = serializers.entrySet().stream().filter(de -> cl.isAssignableFrom(de.getValue().getClassType())).findFirst().orElse(null);
222222
if (d != null) {
223223
Object[] copy = (Object[]) Array.newInstance(cl, j.size());
224224
for (int i = 0; i < j.size(); i++) {

labyrinth-common/src/main/java/com/github/sanctum/labyrinth/data/LabyrinthUser.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.github.sanctum.labyrinth.data;
22

33
import com.github.sanctum.labyrinth.LabyrinthProvider;
4+
import com.github.sanctum.labyrinth.formatting.ScoreboardInstance;
45
import com.github.sanctum.labyrinth.formatting.TablistInstance;
56
import com.github.sanctum.labyrinth.interfacing.Nameable;
67
import com.github.sanctum.labyrinth.library.Cooldown;
@@ -15,6 +16,8 @@ public interface LabyrinthUser extends Nameable {
1516

1617
@NotNull String getName();
1718

19+
String[] getPreviousNames();
20+
1821
@NotNull UUID getId();
1922

2023
@NotNull OfflinePlayer getPlayer();
@@ -23,6 +26,10 @@ default TablistInstance getTablist() {
2326
return isOnline() ? TablistInstance.get(getPlayer().getPlayer()) : null;
2427
}
2528

29+
default ScoreboardInstance getScoreboard() {
30+
return isOnline() ? ScoreboardInstance.get(getPlayer().getPlayer()) : null;
31+
}
32+
2633
default @Nullable Cooldown getCooldown(@NotNull String key) {
2734
return LabyrinthProvider.getInstance().getCooldown(getId().toString() + "-" + key);
2835
}

labyrinth-common/src/main/java/com/github/sanctum/labyrinth/data/MemorySpace.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.github.sanctum.labyrinth.data;
22

3+
import com.github.sanctum.labyrinth.annotation.See;
4+
import com.github.sanctum.labyrinth.data.container.LabyrinthAtlas;
35
import java.util.Map;
46
import java.util.Set;
57

@@ -9,6 +11,7 @@
911
* @author Hempfest
1012
* @version 1.0
1113
*/
14+
@See({Node.class, Configurable.class, Atlas.class, LabyrinthAtlas.class})
1215
public interface MemorySpace {
1316

1417
/**

labyrinth-common/src/main/java/com/github/sanctum/labyrinth/data/container/CollectionTask.java

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ public void reset() {
222222
public static <T> CollectionTask<T> process(T[] elements, String table, int interval, Consumer<T> action) {
223223
return new CollectionTask<T>(table) {
224224

225+
private static final long serialVersionUID = -3595452685342615045L;
225226
final T[] collector = elements;
226227
int index = 0;
227228
long started = 0, lastRan = 0;
@@ -295,6 +296,80 @@ public void reset() {
295296
};
296297
}
297298

299+
public static <T> CollectionTask<T> processSilent(T[] elements, String table, int interval, Consumer<T> action) {
300+
return new CollectionTask<T>(table) {
301+
302+
private static final long serialVersionUID = -3595452685342615045L;
303+
final T[] collector = elements;
304+
int index = 0;
305+
long started = 0, lastRan = 0;
306+
T current;
307+
308+
public double getCompletion() {
309+
return Math.min(100.00, new ProgressBar().setProgress(index + 1).setGoal(collector.length).getPercentage());
310+
}
311+
312+
public T current() {
313+
return current;
314+
}
315+
316+
@Override
317+
public long getTimeStarted() {
318+
return started;
319+
}
320+
321+
@Override
322+
public long getRecentExecution() {
323+
return lastRan;
324+
}
325+
326+
@Override
327+
public boolean hasNext() {
328+
return index < collector.length;
329+
}
330+
331+
@Override
332+
public boolean hasNext(int bounds) {
333+
return index + bounds < collector.length;
334+
}
335+
336+
@Ordinal
337+
public T next() {
338+
return next(interval);
339+
}
340+
341+
@Ordinal(1)
342+
public T next(int bounds) {
343+
int processed = 0;
344+
if (isPaused()) return current;
345+
if (index < collector.length) {
346+
if (started == 0) started = System.currentTimeMillis();
347+
lastRan = System.currentTimeMillis();
348+
for (int i = index; i < collector.length; i++) {
349+
if (processed <= bounds) {
350+
T o = collector[i];
351+
current = o;
352+
action.accept(o);
353+
index++;
354+
processed++;
355+
}
356+
}
357+
return current;
358+
}
359+
cancel();
360+
return current;
361+
}
362+
363+
@Override
364+
public void reset() {
365+
index = 0;
366+
started = 0;
367+
lastRan = 0;
368+
current = null;
369+
}
370+
};
371+
}
372+
298373
public static <T> CollectionTask<T> merge(LabyrinthCollection<T> target, LabyrinthCollection<T> additive, String table, int interval) {
299374
return new LabyrinthCollectionMergeProcess<>(target, additive, interval, table);
300375
}

labyrinth-common/src/main/java/com/github/sanctum/labyrinth/data/service/AnvilMechanics.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
package com.github.sanctum.labyrinth.data.service;
22

3+
import com.github.sanctum.labyrinth.annotation.Note;
4+
import com.github.sanctum.labyrinth.api.Service;
5+
import java.util.Objects;
6+
import org.bukkit.Bukkit;
37
import org.bukkit.entity.Player;
48
import org.bukkit.inventory.Inventory;
9+
import org.jetbrains.annotations.NotNull;
510

611
/**
712
* Wraps versions to be able to easily use different NMS server versions
813
*
914
* @author Wesley Smith
1015
* @since 1.0
1116
*/
12-
public interface AnvilMechanics {
17+
public interface AnvilMechanics extends Service {
1318

1419
/**
1520
* Gets the next available NMS container id for the player
@@ -92,4 +97,8 @@ public interface AnvilMechanics {
9297
*/
9398
Object newContainerAnvil(Player player, String title);
9499

100+
static @Note @NotNull AnvilMechanics getInstance() {
101+
return Objects.requireNonNull(Bukkit.getServicesManager().load(AnvilMechanics.class));
102+
}
103+
95104
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.github.sanctum.labyrinth.data.service;
2+
3+
import com.github.sanctum.labyrinth.api.Service;
4+
import com.github.sanctum.labyrinth.library.TypeFlag;
5+
6+
public interface Counter<N extends Number> extends Service {
7+
8+
void add();
9+
10+
void subtract();
11+
12+
N get();
13+
14+
static <N extends Number> Counter<N> newInstance() {
15+
return new Counter<N>() {
16+
17+
Number i;
18+
boolean integer = false;
19+
20+
{
21+
TypeFlag<N> flag = TypeFlag.get();
22+
Class<N> type = flag.getType();
23+
if (type == Integer.class) {
24+
i = 0L;
25+
integer = true;
26+
} else i = 0.0D;
27+
}
28+
29+
@Override
30+
public void add() {
31+
if (integer) {
32+
i = (i.longValue() + 1L);
33+
} else i = (i.doubleValue() + 1D);
34+
}
35+
36+
@Override
37+
public void subtract() {
38+
if (integer) {
39+
i = (i.longValue() - 1L);
40+
} else i = (i.doubleValue() - 1D);
41+
}
42+
43+
@Override
44+
public N get() {
45+
return (N) i;
46+
}
47+
};
48+
}
49+
50+
static <N extends Number> Counter<N> newInstance(N influence) {
51+
return new Counter<N>() {
52+
53+
Number i;
54+
final Number inf = influence;
55+
boolean integer = false;
56+
57+
{
58+
TypeFlag<N> flag = TypeFlag.get();
59+
Class<N> type = flag.getType();
60+
if (type == Integer.class) {
61+
i = 0L;
62+
integer = true;
63+
} else i = 0.0D;
64+
}
65+
66+
@Override
67+
public void add() {
68+
if (integer) {
69+
i = (i.longValue() + inf.longValue());
70+
} else i = (i.doubleValue() + inf.doubleValue());
71+
}
72+
73+
@Override
74+
public void subtract() {
75+
if (integer) {
76+
i = (i.longValue() - inf.longValue());
77+
} else i = (i.doubleValue() - inf.doubleValue());
78+
}
79+
80+
@Override
81+
public N get() {
82+
return (N) i;
83+
}
84+
};
85+
}
86+
87+
}
Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package com.github.sanctum.labyrinth.data.service;
22

33
import com.github.sanctum.labyrinth.annotation.Json;
4+
import java.util.HashMap;
45
import java.util.Map;
56

6-
public class DummyReducer implements Json.Reducer {
7+
public final class DummyReducer implements Json.Reducer {
78
@Override
89
public Map<String, Object> reduce(Object o) {
9-
return null;
10+
Map<String, Object> map = new HashMap<>();
11+
map.put("element", o.toString());
12+
return map;
1013
}
1114
}

0 commit comments

Comments
 (0)