Skip to content

Commit 28f7294

Browse files
committed
Initial commit.
0 parents  commit 28f7294

File tree

8 files changed

+389
-0
lines changed

8 files changed

+389
-0
lines changed

.gitignore

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Compiled class file
2+
*.class
3+
4+
# Log file
5+
*.log
6+
7+
# BlueJ files
8+
*.ctxt
9+
10+
# IntelliJ
11+
.idea/
12+
*.iml
13+
14+
# Mobile Tools for Java (J2ME)
15+
.mtj.tmp/
16+
17+
# Package Files
18+
*.jar
19+
*.war
20+
*.nar
21+
*.ear
22+
*.zip
23+
*.tar.gz
24+
*.rar
25+
26+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
27+
hs_err_pid*
28+
29+
# Maven
30+
target/
31+
dependency-reduced-pom.xml
32+
.flattened-pom.xml

LICENSE

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2021 Going Off Skript
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
SOFTWARE.

pom.xml

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>io.github.goingoffskript</groupId>
8+
<artifactId>skript-variable-dump</artifactId>
9+
<version>0.0.1</version>
10+
11+
<properties>
12+
<maven.compiler.source>8</maven.compiler.source>
13+
<maven.compiler.target>8</maven.compiler.target>
14+
</properties>
15+
16+
<repositories>
17+
<repository>
18+
<id>papermc</id>
19+
<url>https://papermc.io/repo/repository/maven-public/</url>
20+
</repository>
21+
<repository>
22+
<id>jitpack.io</id>
23+
<url>https://jitpack.io</url>
24+
</repository>
25+
</repositories>
26+
27+
<dependencies>
28+
<!-- Paper API (via papermc) -->
29+
<dependency>
30+
<groupId>com.destroystokyo.paper</groupId>
31+
<artifactId>paper-api</artifactId>
32+
<version>1.14.4-R0.1-SNAPSHOT</version>
33+
<scope>provided</scope>
34+
</dependency>
35+
<!-- Skript (via jitpack) -->
36+
<dependency>
37+
<groupId>com.github.skriptlang</groupId>
38+
<artifactId>skript</artifactId>
39+
<version>2.4.1</version>
40+
<scope>provided</scope>
41+
<exclusions>
42+
<exclusion>
43+
<groupId>*</groupId>
44+
<artifactId>*</artifactId>
45+
</exclusion>
46+
</exclusions>
47+
</dependency>
48+
<!-- Nullness Annotations: @NullOr (via maven-central) -->
49+
<dependency>
50+
<groupId>pl.tlinkowski.annotation</groupId>
51+
<artifactId>pl.tlinkowski.annotation.basic</artifactId>
52+
<version>0.2.0</version>
53+
</dependency>
54+
</dependencies>
55+
56+
<build>
57+
<defaultGoal>package</defaultGoal>
58+
<finalName>${project.name}-v${project.version}</finalName>
59+
<plugins>
60+
<!-- Compile project -->
61+
<plugin>
62+
<groupId>org.apache.maven.plugins</groupId>
63+
<artifactId>maven-compiler-plugin</artifactId>
64+
<version>3.8.1</version>
65+
<configuration>
66+
<source>${maven.compiler.source}</source>
67+
<target>${maven.compiler.target}</target>
68+
<compilerArgs>
69+
<arg>-parameters</arg>
70+
</compilerArgs>
71+
</configuration>
72+
</plugin>
73+
</plugins>
74+
<resources>
75+
<!-- Filter resource files -->
76+
<resource>
77+
<directory>src/main/resources</directory>
78+
<filtering>true</filtering>
79+
</resource>
80+
</resources>
81+
</build>
82+
</project>
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package io.github.goingoffskript.skriptvariabledump;
2+
3+
import ch.njol.skript.classes.ClassInfo;
4+
import ch.njol.skript.util.Color;
5+
import ch.njol.skript.util.Date;
6+
import ch.njol.skript.util.Timespan;
7+
import pl.tlinkowski.annotation.basic.NullOr;
8+
9+
import java.util.HashMap;
10+
import java.util.LinkedHashMap;
11+
import java.util.Map;
12+
import java.util.function.BiConsumer;
13+
14+
public class SkriptToYaml
15+
{
16+
private SkriptToYaml() {}
17+
18+
private static final Map<Class<?>, BiConsumer<Object, Map<String, Object>>> ADAPTERS = new HashMap<>();
19+
20+
static
21+
{
22+
adapts(ClassInfo.class, (info, map) -> {
23+
map.put("type", info.getName().getSingular());
24+
});
25+
26+
adapts(Color.class, (color, map) -> {
27+
map.put("color", color.getName());
28+
});
29+
30+
adapts(Date.class, (date, map) -> {
31+
map.put("timestamp", date.getTimestamp());
32+
});
33+
34+
adapts(Timespan.class, (timespan, map) -> {
35+
map.put("milliseconds", timespan.getMilliSeconds());
36+
});
37+
}
38+
39+
@SuppressWarnings("unchecked")
40+
private static <T> void adapts(Class<T> clazz, BiConsumer<T, Map<String, Object>> adapter)
41+
{
42+
ADAPTERS.put(clazz, (object, map) -> {
43+
map.put("==", clazz.getSimpleName());
44+
adapter.accept((T) object, map);
45+
});
46+
}
47+
48+
public static Object adapt(Object object)
49+
{
50+
@NullOr BiConsumer<Object, Map<String, Object>> adapter = ADAPTERS.get(object.getClass());
51+
if (adapter == null) { return object; }
52+
53+
Map<String, Object> map = new LinkedHashMap<>();
54+
adapter.accept(object, map);
55+
return map;
56+
}
57+
}
Lines changed: 164 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
1+
package io.github.goingoffskript.skriptvariabledump;
2+
3+
import ch.njol.skript.Skript;
4+
import ch.njol.skript.variables.Variables;
5+
import org.bukkit.ChatColor;
6+
import org.bukkit.command.CommandSender;
7+
import org.bukkit.configuration.ConfigurationSection;
8+
import org.bukkit.configuration.file.YamlConfiguration;
9+
import pl.tlinkowski.annotation.basic.NullOr;
10+
11+
import java.io.IOException;
12+
import java.lang.reflect.InvocationTargetException;
13+
import java.lang.reflect.Method;
14+
import java.nio.charset.StandardCharsets;
15+
import java.nio.file.Files;
16+
import java.nio.file.Path;
17+
import java.nio.file.StandardOpenOption;
18+
import java.time.LocalDate;
19+
import java.time.LocalDateTime;
20+
import java.util.Map;
21+
import java.util.concurrent.atomic.AtomicBoolean;
22+
import java.util.concurrent.locks.Lock;
23+
24+
public class SkriptVariableDumper
25+
{
26+
private SkriptVariableDumper() {}
27+
28+
private static final AtomicBoolean IS_DUMPING_VARIABLES = new AtomicBoolean(false);
29+
30+
private static final @NullOr Method GET_VARIABLES;
31+
32+
static
33+
{
34+
@NullOr Method getVariables = null;
35+
36+
try
37+
{
38+
getVariables = Variables.class.getDeclaredMethod("getVariables");
39+
getVariables.setAccessible(true);
40+
}
41+
catch (NoSuchMethodException e) { e.printStackTrace(); }
42+
43+
GET_VARIABLES = getVariables;
44+
}
45+
46+
private static final @NullOr Method GET_READ_LOCK;
47+
48+
static
49+
{
50+
@NullOr Method getReadLock = null;
51+
52+
try
53+
{
54+
getReadLock = Variables.class.getDeclaredMethod("getReadLock");
55+
getReadLock.setAccessible(true);
56+
}
57+
catch (NoSuchMethodException e) { e.printStackTrace(); }
58+
59+
GET_READ_LOCK = getReadLock;
60+
}
61+
62+
static boolean isInvalid() { return GET_VARIABLES == null || GET_READ_LOCK == null; }
63+
64+
@SuppressWarnings("unchecked")
65+
private static Map<String, Object> variables()
66+
{
67+
if (GET_VARIABLES == null) { throw new IllegalStateException(); }
68+
try { return (Map<String, Object>) GET_VARIABLES.invoke(null); }
69+
catch (IllegalAccessException | InvocationTargetException e) { throw new RuntimeException(e); }
70+
}
71+
72+
private static Lock readLock()
73+
{
74+
if (GET_READ_LOCK == null) { throw new IllegalStateException(); }
75+
try { return (Lock) GET_READ_LOCK.invoke(null); }
76+
catch (IllegalAccessException | InvocationTargetException e) { throw new RuntimeException(e); }
77+
}
78+
79+
@SuppressWarnings("ConstantConditions")
80+
private static String key(Map.Entry<String, Object> entry)
81+
{
82+
@NullOr String key = entry.getKey();
83+
return (key == null || key.isEmpty()) ? "<none>" : key;
84+
}
85+
86+
@SuppressWarnings("ConstantConditions")
87+
private static @NullOr Object value(Map.Entry<String, Object> entry)
88+
{
89+
@NullOr Object value = entry.getValue();
90+
return (value == null) ? null : SkriptToYaml.adapt(value);
91+
}
92+
93+
@SuppressWarnings("unchecked")
94+
private static void dump(ConfigurationSection section, Map<String, Object> vars)
95+
{
96+
for (Map.Entry<String, Object> entry : vars.entrySet())
97+
{
98+
String key = key(entry);
99+
@NullOr Object value = value(entry);
100+
101+
if (value instanceof Map) { dump(section.createSection(key), (Map<String, Object>) value); }
102+
else { section.set(key, value); }
103+
}
104+
}
105+
106+
private static Path dumpFilePath(Path directory)
107+
{
108+
for (int i = 1 ;; i++)
109+
{
110+
Path path = directory.resolve("skript-variables-dump." + LocalDate.now() + "_" + i + ".yml");
111+
if (!Files.isRegularFile(path)) { return path; }
112+
}
113+
}
114+
115+
static Runnable task(CommandSender sender)
116+
{
117+
return () ->
118+
{
119+
boolean available = IS_DUMPING_VARIABLES.compareAndSet(false, true);
120+
121+
if (!available)
122+
{
123+
sender.sendMessage("Already dumping variables, be patient...");
124+
return;
125+
}
126+
127+
sender.sendMessage("Dumping skript variables...");
128+
129+
YamlConfiguration data = new YamlConfiguration();
130+
data.options().pathSeparator('\0');
131+
132+
data.options().header(
133+
"Skript Variable Dump: " + LocalDateTime.now() + "\n" +
134+
"Skript Version: " + Skript.getVersion() + "\n"
135+
);
136+
137+
try
138+
{
139+
readLock().lock();
140+
try { dump(data, variables()); }
141+
finally { readLock().unlock(); }
142+
143+
Path dumpsDir = Skript.getInstance().getDataFolder().toPath().resolve("dumps");
144+
Path dumpFile = dumpFilePath(dumpsDir);
145+
146+
try
147+
{
148+
if (!Files.isDirectory(dumpsDir)) { Files.createDirectories(dumpsDir); }
149+
150+
byte[] bytes = data.saveToString().getBytes(StandardCharsets.UTF_8);
151+
Files.write(dumpFile, bytes, StandardOpenOption.CREATE_NEW);
152+
153+
sender.sendMessage("Saved variable dump: " + ChatColor.GOLD + dumpFile);
154+
}
155+
catch (IOException e)
156+
{
157+
sender.sendMessage("Failed to dump variables: " + ChatColor.RED + e.getMessage());
158+
e.printStackTrace();
159+
}
160+
}
161+
finally { IS_DUMPING_VARIABLES.set(false); }
162+
};
163+
}
164+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package io.github.goingoffskript.skriptvariabledump;
2+
3+
import org.bukkit.command.Command;
4+
import org.bukkit.command.CommandSender;
5+
import org.bukkit.plugin.java.JavaPlugin;
6+
7+
public class VariableDumpPlugin extends JavaPlugin
8+
{
9+
@Override
10+
public void onEnable()
11+
{
12+
if (SkriptVariableDumper.isInvalid()) { setEnabled(false); }
13+
}
14+
15+
@Override
16+
public boolean onCommand(CommandSender sender, Command command, String label, String[] args)
17+
{
18+
getServer().getScheduler().runTaskAsynchronously(this, SkriptVariableDumper.task(sender));
19+
return true;
20+
}
21+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
@NonNullPackage
2+
package io.github.goingoffskript.skriptvariabledump;
3+
4+
import pl.tlinkowski.annotation.basic.NonNullPackage;

src/main/resources/plugin.yml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
main: io.github.goingoffskript.skriptvariabledump.VariableDumpPlugin
2+
name: ${project.name}
3+
version: ${project.version}
4+
api-version: 1.13
5+
depend: [Skript]
6+
7+
commands:
8+
skript-variable-dump:
9+
aliases: [skriptvariabledump, skvardump, dump-skript-variables, dumpskriptvariables, dumpskiptvars, dumpskvars]
10+
permission: skriptvariabledump.dump

0 commit comments

Comments
 (0)