Skip to content

Commit 12a9c73

Browse files
authored
Merge pull request #3 from GoingOffSkript/public-api
Add public Adapter API.
2 parents a8a387f + 91f3530 commit 12a9c73

File tree

9 files changed

+252
-53
lines changed

9 files changed

+252
-53
lines changed

.jitpack.yml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
jdk: openjdk8

.run/jitpack.run.xml

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<component name="ProjectRunConfigurationManager">
2+
<configuration default="false" name="jitpack" type="MavenRunConfiguration" factoryName="Maven">
3+
<MavenSettings>
4+
<option name="myGeneralSettings" />
5+
<option name="myRunnerSettings">
6+
<MavenRunnerSettings>
7+
<option name="delegateBuildToMaven" value="false" />
8+
<option name="environmentProperties">
9+
<map>
10+
<entry key="JITPACK" value="true" />
11+
</map>
12+
</option>
13+
<option name="jreName" value="#USE_PROJECT_JDK" />
14+
<option name="mavenProperties">
15+
<map />
16+
</option>
17+
<option name="passParentEnv" value="true" />
18+
<option name="runMavenInBackground" value="true" />
19+
<option name="skipTests" value="false" />
20+
<option name="vmOptions" value="" />
21+
</MavenRunnerSettings>
22+
</option>
23+
<option name="myRunnerParameters">
24+
<MavenRunnerParameters>
25+
<option name="profiles">
26+
<set />
27+
</option>
28+
<option name="goals">
29+
<list>
30+
<option value="clean" />
31+
<option value="install" />
32+
<option value="-DskipTests" />
33+
</list>
34+
</option>
35+
<option name="pomFileName" />
36+
<option name="profilesMap">
37+
<map />
38+
</option>
39+
<option name="resolveToWorkspace" value="false" />
40+
<option name="workingDirPath" value="$PROJECT_DIR$" />
41+
</MavenRunnerParameters>
42+
</option>
43+
</MavenSettings>
44+
<method v="2" />
45+
</configuration>
46+
</component>

README.md

+83
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
# skript-variable-dump
22

3+
[![](https://jitpack.io/v/goingoffskript/skript-variable-dump.svg)](https://jitpack.io/#goingoffskript/skript-variable-dump "Get maven artifacts on JitPack")
34
[![](https://img.shields.io/badge/License-MIT-blue)](./LICENSE "Project license: MIT")
45
[![](https://img.shields.io/badge/Java-8-orange)](# "This project targets Java 8")
6+
[![](https://img.shields.io/badge/View-Javadocs-%234D7A97)](https://javadoc.jitpack.io/com/github/goingoffskript/skript-variable-dump/latest/javadoc/ "View javadocs")
57

68
Dump [Skript](https://github.com/SkriptLang/Skript/) variables to YAML.
79

@@ -14,3 +16,84 @@ All loaded variables will be dumped into a YAML file generated at:
1416
`/plugins/Skript/dumps/`
1517

1618
That's all.
19+
20+
## Example
21+
22+
📜 ➡️ 📑
23+
24+
- Variables: `/plugins/Skript/variables.csv`
25+
26+
```csv
27+
# === Skript's variable storage ===
28+
# Please do not modify this file manually!
29+
#
30+
# version: 2.5.3
31+
32+
numbers::1, string, 80036F6E65
33+
numbers::2, string, 800374776F
34+
numbers::3, string, 80057468726565
35+
numbers::4, long, 0000000000000004
36+
numbers::5, long, 0000000000000005
37+
numbers::6, long, 0000000000000006
38+
numbers::7, double, 401E000000000000
39+
numbers::8, double, 4020666666666666
40+
numbers::9, double, 4022333333333333
41+
wait, timespan, 81066D696C6C6973040000000000000BB8
42+
```
43+
44+
- Run: **/skript-variable-dump**
45+
- Generates: `/plugins/Skript/dumps/skript-variables-dump.2021-05-12_1.yml`
46+
47+
```yaml
48+
# Skript Variable Dump: 2021-05-12T16:53:30.636775
49+
# Skript Version: 2.5.3
50+
51+
numbers:
52+
'1': one
53+
'2': two
54+
'3': three
55+
'4': 4
56+
'5': 5
57+
'6': 6
58+
'7': 7.5
59+
'8': 8.2
60+
'9': 9.1
61+
wait:
62+
==: Timespan
63+
milliseconds: 3000
64+
```
65+
66+
## Addons
67+
68+
Addons can register adapters for their data types with:
69+
70+
```java
71+
import com.github.goingoffskript.skriptvariabledump.SkriptToYaml;
72+
73+
SkriptToYaml.adapts(ExampleType.class, (example, map) -> {
74+
map.put("thing", example.thing());
75+
map.put("amount", example.amount());
76+
});
77+
```
78+
79+
### Dependency
80+
81+
Get it from JitPack: https://jitpack.io/#goingoffskript/skript-variable-dump
82+
83+
```xml
84+
<repositories>
85+
<repository>
86+
<id>jitpack.io</id>
87+
<url>https://jitpack.io</url>
88+
</repository>
89+
</repositories>
90+
91+
<dependencies>
92+
<dependency>
93+
<groupId>com.github.goingoffskript</groupId>
94+
<artifactId>skript-variable-dump</artifactId>
95+
<version><!-- Released Version --></version>
96+
<scope>provided</scope>
97+
</dependency>
98+
</dependencies>
99+
```

pom.xml

+57-1
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66

77
<groupId>io.github.goingoffskript</groupId>
88
<artifactId>skript-variable-dump</artifactId>
9-
<version>0.0.1</version>
9+
<version>0.0.2</version>
1010

1111
<properties>
1212
<maven.compiler.source>8</maven.compiler.source>
1313
<maven.compiler.target>8</maven.compiler.target>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1415
</properties>
1516

1617
<repositories>
@@ -26,13 +27,15 @@
2627

2728
<dependencies>
2829
<!-- Paper API (via papermc) -->
30+
<!-- Few versions behind to ensure compatibility -->
2931
<dependency>
3032
<groupId>com.destroystokyo.paper</groupId>
3133
<artifactId>paper-api</artifactId>
3234
<version>1.14.4-R0.1-SNAPSHOT</version>
3335
<scope>provided</scope>
3436
</dependency>
3537
<!-- Skript (via jitpack) -->
38+
<!-- Few versions behind to ensure compatibility -->
3639
<dependency>
3740
<groupId>com.github.skriptlang</groupId>
3841
<artifactId>skript</artifactId>
@@ -50,6 +53,7 @@
5053
<groupId>pl.tlinkowski.annotation</groupId>
5154
<artifactId>pl.tlinkowski.annotation.basic</artifactId>
5255
<version>0.2.0</version>
56+
<!-- Note: scope must be visible to dependent projects otherwise nullness annotations will not work as intended -->
5357
</dependency>
5458
</dependencies>
5559

@@ -79,4 +83,56 @@
7983
</resource>
8084
</resources>
8185
</build>
86+
87+
<profiles>
88+
<!-- JitPack (activated with `JITPACK=true` environment variable) -->
89+
<profile>
90+
<id>jitpack</id>
91+
<activation>
92+
<activeByDefault>false</activeByDefault>
93+
<property>
94+
<name>env.JITPACK</name>
95+
</property>
96+
</activation>
97+
<build>
98+
<plugins>
99+
<!-- Generate javadocs -->
100+
<plugin>
101+
<groupId>org.apache.maven.plugins</groupId>
102+
<artifactId>maven-javadoc-plugin</artifactId>
103+
<version>3.2.0</version>
104+
<configuration>
105+
<doclint>none</doclint>
106+
<links>
107+
<link>https://javadoc.io/doc/pl.tlinkowski.annotation/pl.tlinkowski.annotation.basic/0.2.0/</link>
108+
</links>
109+
</configuration>
110+
<executions>
111+
<execution>
112+
<id>attach-javadocs</id>
113+
<goals>
114+
<goal>jar</goal>
115+
</goals>
116+
</execution>
117+
</executions>
118+
</plugin>
119+
<!-- Attach sources -->
120+
<plugin>
121+
<groupId>org.apache.maven.plugins</groupId>
122+
<artifactId>maven-source-plugin</artifactId>
123+
<version>3.2.0</version>
124+
<executions>
125+
<execution>
126+
<id>attach-sources</id>
127+
<goals>
128+
<goal>jar-no-fork</goal>
129+
</goals>
130+
</execution>
131+
</executions>
132+
</plugin>
133+
</plugins>
134+
</build>
135+
</profile>
136+
</profiles>
137+
82138
</project>

src/main/java/io/github/goingoffskript/skriptvariabledump/SkriptToYaml.java renamed to src/main/java/com/github/goingoffskript/skriptvariabledump/SkriptToYaml.java

+34-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package io.github.goingoffskript.skriptvariabledump;
1+
package com.github.goingoffskript.skriptvariabledump;
22

33
import ch.njol.skript.classes.ClassInfo;
44
import ch.njol.skript.util.Color;
@@ -11,11 +11,14 @@
1111
import java.util.Map;
1212
import java.util.function.BiConsumer;
1313

14+
/**
15+
* Skript data-to-YAML adapters.
16+
*/
1417
public class SkriptToYaml
1518
{
1619
private SkriptToYaml() {}
1720

18-
private static final Map<Class<?>, BiConsumer<Object, Map<String, Object>>> ADAPTERS = new HashMap<>();
21+
private static final Map<Class<?>, Adapter<?>> ADAPTERS = new HashMap<>();
1922

2023
static
2124
{
@@ -36,22 +39,48 @@ private SkriptToYaml() {}
3639
});
3740
}
3841

42+
/**
43+
* Adapts data from a specific type into a
44+
* {@code Map<String, Object>}.
45+
*
46+
* @param <T> data type
47+
*/
48+
@FunctionalInterface
49+
public interface Adapter<T> extends BiConsumer<T, Map<String, Object>> {}
50+
51+
/**
52+
* Registers an adapter for a specific data type.
53+
*
54+
* @param clazz the data type's class
55+
* @param adapter the adapter to register
56+
* @param <T> data type
57+
*/
3958
@SuppressWarnings("unchecked")
40-
public static <T> void adapts(Class<T> clazz, BiConsumer<T, Map<String, Object>> adapter)
59+
public static <T> void adapts(Class<T> clazz, Adapter<T> adapter)
4160
{
4261
ADAPTERS.put(clazz, (object, map) -> {
4362
map.put("==", clazz.getSimpleName());
4463
adapter.accept((T) object, map);
4564
});
4665
}
4766

67+
/**
68+
* Adapts an object into a string map or returns
69+
* it as-is if no adapter exists for its type.
70+
*
71+
* @param object the object to adapt
72+
*
73+
* @return the provided object
74+
* or a {@code Map<String, Object>}
75+
*/
76+
@SuppressWarnings("unchecked")
4877
public static Object adapt(Object object)
4978
{
50-
@NullOr BiConsumer<Object, Map<String, Object>> adapter = ADAPTERS.get(object.getClass());
79+
@NullOr Adapter<?> adapter = ADAPTERS.get(object.getClass());
5180
if (adapter == null) { return object; }
5281

5382
Map<String, Object> map = new LinkedHashMap<>();
54-
adapter.accept(object, map);
83+
((Adapter<Object>) adapter).accept(object, map);
5584
return map;
5685
}
5786
}

0 commit comments

Comments
 (0)