Skip to content

Commit 07fb6f9

Browse files
committed
Initial commit
0 parents  commit 07fb6f9

12 files changed

+1462
-0
lines changed

.classpath

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" output="target/classes" path="src/main/java">
4+
<attributes>
5+
<attribute name="optional" value="true"/>
6+
<attribute name="maven.pomderived" value="true"/>
7+
</attributes>
8+
</classpathentry>
9+
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
10+
<attributes>
11+
<attribute name="maven.pomderived" value="true"/>
12+
</attributes>
13+
</classpathentry>
14+
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
15+
<attributes>
16+
<attribute name="optional" value="true"/>
17+
<attribute name="maven.pomderived" value="true"/>
18+
</attributes>
19+
</classpathentry>
20+
<classpathentry excluding="**" kind="src" output="target/test-classes" path="src/test/resources">
21+
<attributes>
22+
<attribute name="maven.pomderived" value="true"/>
23+
</attributes>
24+
</classpathentry>
25+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8">
26+
<attributes>
27+
<attribute name="maven.pomderived" value="true"/>
28+
</attributes>
29+
</classpathentry>
30+
<classpathentry kind="con" path="org.eclipse.m2e.MAVEN2_CLASSPATH_CONTAINER">
31+
<attributes>
32+
<attribute name="maven.pomderived" value="true"/>
33+
</attributes>
34+
</classpathentry>
35+
<classpathentry kind="output" path="target/classes"/>
36+
</classpath>

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/target/

.project

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>PAPI-Expansion-Javascript</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.eclipse.m2e.core.maven2Builder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
</buildSpec>
19+
<natures>
20+
<nature>org.eclipse.jdt.core.javanature</nature>
21+
<nature>org.eclipse.m2e.core.maven2Nature</nature>
22+
</natures>
23+
</projectDescription>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
eclipse.preferences.version=1
2+
encoding//src/main/java=UTF-8
3+
encoding//src/test/java=UTF-8

.settings/org.eclipse.jdt.core.prefs

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8
3+
org.eclipse.jdt.core.compiler.compliance=1.8
4+
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
5+
org.eclipse.jdt.core.compiler.source=1.8

.settings/org.eclipse.m2e.core.prefs

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
activeProfiles=
2+
eclipse.preferences.version=1
3+
resolveWorkspaceProjects=true
4+
version=1

LICENSE

+674
Large diffs are not rendered by default.

pom.xml

+59
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>com.extendedclip.papi.expansion.javascript</groupId>
4+
<artifactId>javascript-expansion</artifactId>
5+
<version>0.0.1-SNAPSHOT</version>
6+
<name>PAPI-Expansion-Javascript</name>
7+
<description>PlaceholderAPI expansion for javascript placeholders</description>
8+
9+
<repositories>
10+
<repository>
11+
<id>spigot-repo</id>
12+
<url>https://hub.spigotmc.org/nexus/content/repositories/snapshots/</url>
13+
</repository>
14+
<repository>
15+
<id>placeholderapi</id>
16+
<url>http://repo.extendedclip.com/content/repositories/placeholderapi/</url>
17+
</repository>
18+
</repositories>
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>org.spigotmc</groupId>
23+
<artifactId>spigot-api</artifactId>
24+
<version>1.12-R0.1-SNAPSHOT</version>
25+
<scope>compile</scope>
26+
</dependency>
27+
<dependency>
28+
<groupId>me.clip</groupId>
29+
<artifactId>placeholderapi</artifactId>
30+
<version>2.8.4</version>
31+
<scope>provided</scope>
32+
</dependency>
33+
</dependencies>
34+
35+
<build>
36+
<plugins>
37+
<plugin>
38+
<groupId>org.apache.maven.plugins</groupId>
39+
<artifactId>maven-jar-plugin</artifactId>
40+
<version>2.3.2</version>
41+
<configuration>
42+
<finalName>${name}</finalName>
43+
</configuration>
44+
</plugin>
45+
<plugin>
46+
<groupId>org.apache.maven.plugins</groupId>
47+
<artifactId>maven-compiler-plugin</artifactId>
48+
<version>3.2</version>
49+
<configuration>
50+
<target>1.8</target>
51+
<source>1.8</source>
52+
<encoding>UTF-8</encoding>
53+
<useIncrementalCompilation>false</useIncrementalCompilation>
54+
</configuration>
55+
</plugin>
56+
</plugins>
57+
</build>
58+
59+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
1+
/*
2+
*
3+
* Javascript-Expansion
4+
* Copyright (C) 2018 Ryan McCarthy
5+
*
6+
* This program is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* This program is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
18+
*
19+
*
20+
*/
21+
package com.extendedclip.papi.expansion.javascript;
22+
23+
import java.util.HashMap;
24+
import java.util.HashSet;
25+
import java.util.Map;
26+
import java.util.Set;
27+
28+
import javax.script.ScriptEngine;
29+
import javax.script.ScriptEngineManager;
30+
31+
import me.clip.placeholderapi.PlaceholderAPI;
32+
import me.clip.placeholderapi.PlaceholderAPIPlugin;
33+
import me.clip.placeholderapi.expansion.Cacheable;
34+
import me.clip.placeholderapi.expansion.Configurable;
35+
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
36+
37+
import org.bukkit.Bukkit;
38+
import org.bukkit.entity.Player;
39+
40+
public class JavascriptExpansion extends PlaceholderExpansion implements Cacheable, Configurable {
41+
42+
private ScriptEngine engine = null;
43+
44+
private String engineType = "javascript";
45+
46+
private JavascriptPlaceholdersConfig config;
47+
48+
private final Set<JavascriptPlaceholder> scripts = new HashSet<JavascriptPlaceholder>();
49+
50+
@Override
51+
public boolean register() {
52+
53+
engineType = getString("engine", "javascript");
54+
55+
if (engine == null) {
56+
try {
57+
engine = new ScriptEngineManager().getEngineByName(engineType);
58+
} catch (NullPointerException ex) {
59+
PlaceholderAPIPlugin.getInstance().getLogger().warning("Javascript engine type was invalid! Defaulting to 'javascript'");
60+
engine = new ScriptEngineManager().getEngineByName("javascript");
61+
}
62+
63+
engine.put("BukkitServer", Bukkit.getServer());
64+
}
65+
66+
config = new JavascriptPlaceholdersConfig(this);
67+
68+
config.loadPlaceholders();
69+
70+
return PlaceholderAPI.registerPlaceholderHook(getIdentifier(), this);
71+
}
72+
73+
@Override
74+
public void clear() {
75+
scripts.clear();
76+
engine = null;
77+
}
78+
79+
@Override
80+
public String onPlaceholderRequest(Player p, String identifier) {
81+
82+
if (p == null) {
83+
return "";
84+
}
85+
86+
if (scripts.isEmpty() || engine == null) {
87+
return null;
88+
}
89+
90+
for (JavascriptPlaceholder script : scripts) {
91+
92+
if (script.getIdentifier().equalsIgnoreCase(identifier)) {
93+
94+
return script.evaluate(engine, p);
95+
}
96+
}
97+
98+
return null;
99+
}
100+
101+
@Override
102+
public boolean canRegister() {
103+
return true;
104+
}
105+
106+
@Override
107+
public String getAuthor() {
108+
return "clip";
109+
}
110+
111+
@Override
112+
public String getIdentifier() {
113+
return "javascript";
114+
}
115+
116+
@Override
117+
public String getPlugin() {
118+
return null;
119+
}
120+
121+
122+
@Override
123+
public String getVersion() {
124+
return "1.2.0";
125+
}
126+
127+
public boolean addJavascriptPlaceholder(JavascriptPlaceholder p) {
128+
129+
if (p == null) {
130+
return false;
131+
}
132+
133+
if (scripts.isEmpty()) {
134+
scripts.add(p);
135+
return true;
136+
}
137+
138+
139+
for (JavascriptPlaceholder pl : scripts) {
140+
if (pl.getIdentifier().equalsIgnoreCase(p.getIdentifier())) {
141+
return false;
142+
}
143+
}
144+
scripts.add(p);
145+
return true;
146+
}
147+
148+
public int getJavascriptPlaceholdersAmount() {
149+
return scripts == null ? 0 : scripts.size();
150+
}
151+
152+
@Override
153+
public Map<String, Object> getDefaults() {
154+
Map<String, Object> def = new HashMap<String, Object>();
155+
def.put("engine", "javascript");
156+
return def;
157+
}
158+
}

0 commit comments

Comments
 (0)