@@ -8,25 +8,31 @@ import ru.endlesscode.bukkitgradle.BukkitGradlePlugin
88import ru.endlesscode.bukkitgradle.extension.Bukkit
99import ru.endlesscode.bukkitgradle.util.MavenApi
1010
11+ import javax.annotation.Nullable
1112import java.nio.file.Files
1213import java.nio.file.Path
1314import java.nio.file.Paths
1415
1516class ServerCore {
1617 public static final String CORE_NAME = " core.jar"
18+ public static final String SERVER_HOME_PROPERTY = " server.dir"
19+ public static final String SERVER_HOME_ENV = " BUKKIT_DEV_SERVER_HOME"
20+ public static final String BUILDTOOLS_NAME = " BuildTools.jar"
21+ public static final String BUILDTOOLS_HOME_PROPERTY = " buildtools.dir"
22+ public static final String BUILDTOOLS_HOME_ENV = " BUILDTOOLS_HOME"
1723
1824 private static final String MAVEN_METADATA = " maven-metadata.xml"
1925
2026 private final Project project
2127
2228 private Path bukkitGradleDir
2329 private boolean forceRebuild = false
30+ private Properties localProps = new Properties ()
2431
2532 ServerCore (Project project ) {
2633 this . project = project
2734
2835 MavenApi . init(project)
29-
3036 this . initDir()
3137
3238 project. afterEvaluate {
@@ -47,8 +53,8 @@ class ServerCore {
4753 */
4854 void registerTasks () {
4955 registerBukkitMetaTask()
50- registerCoreCopyTask()
5156 registerBuildServerCoreTask()
57+ registerCoreCopyTask()
5258 }
5359
5460 /**
@@ -83,6 +89,11 @@ class ServerCore {
8389 group = BukkitGradlePlugin . GROUP
8490 description = ' Copy built server core to server directory'
8591
92+ if (! tasks. buildServerCore. enabled) {
93+ enabled = false
94+ return
95+ }
96+
8697 def coreName = getCoreName()
8798 from MavenApi . getSpigotDir(realVersion)
8899 include coreName
@@ -109,17 +120,23 @@ class ServerCore {
109120 return ! MavenApi . hasSpigot(getRealVersion())
110121 }
111122
112- def path = Paths . get(project. bukkit. buildtools as String )
123+ if (buildToolsPath == null || serverDir == null ) {
124+ project. logger. warn(" You can't use server running feature." )
125+ enabled = false
126+ return
127+ }
128+
129+ def path = buildToolsPath. resolve(BUILDTOOLS_NAME )
113130 def absolutePath = path. toAbsolutePath(). toString()
114- if (Files . notExists(path) || ! Files . isRegularFile (path)) {
131+ if (Files . notExists(path) || Files . isDirectory (path)) {
115132 project. logger. warn(" BuildTools not found on path: '$absolutePath '\n " +
116- ' It should be path to .jar file of BuildTools .' )
133+ ' BuildTools directory should contains BuildTools .jar file.' )
117134 enabled = false
118135 return
119136 }
120137
121138 main = ' -jar'
122- args absolutePath, ' --rev' , getSimpleVersion()
139+ args( absolutePath, ' --rev' , getSimpleVersion() )
123140 workingDir = path. getParent(). toAbsolutePath(). toString()
124141 standardInput = System . in
125142 }
@@ -150,6 +167,67 @@ class ServerCore {
150167 return getRealVersion(). replace(Bukkit . REVISION_SUFFIX , ' ' )
151168 }
152169
170+ /**
171+ * Returns server directory
172+ *
173+ * @return Server directory or null if dev server location not defined
174+ */
175+ @Nullable
176+ Path getServerDir () {
177+ return getDirFromPropsOrEnv(SERVER_HOME_PROPERTY , SERVER_HOME_ENV , " Dev server location" )
178+ }
179+
180+ private @Nullable
181+ Path getBuildToolsPath () {
182+ return getDirFromPropsOrEnv(BUILDTOOLS_HOME_PROPERTY , BUILDTOOLS_HOME_ENV , " BuildTools location" )
183+ }
184+
185+ private @Nullable
186+ Path getDirFromPropsOrEnv (String propertyName , String envVariable , String comment ) {
187+ this . initLocalProps()
188+
189+ def localProp = localProps. getProperty(propertyName)
190+ def globalEnv = System . getenv(envVariable)
191+ if (localProp == null && globalEnv == null ) {
192+ project. logger. warn(" $comment not found. It can be fixed by two ways:\n " +
193+ " 1. Define location with '$propertyName ' in the local.properties file\n " +
194+ " 2. Define $envVariable environment variable" )
195+ return null
196+ }
197+ def dir = Paths . get(localProp ?: globalEnv)
198+ Files . createDirectories(dir)
199+
200+ return dir
201+ }
202+
203+ private initLocalProps () {
204+ Path propsFile = this . project. file(" local.properties" ). toPath()
205+ if (Files . exists(propsFile)) {
206+ localProps. load(propsFile. newReader())
207+ return
208+ }
209+
210+ project. logger. info(" Local properties file not found. Creating..." )
211+ Files . createFile(propsFile)
212+ localProps. load(propsFile. newReader())
213+
214+ if (System . getenv(SERVER_HOME_ENV ) == null ) {
215+ localProps. setProperty(SERVER_HOME_PROPERTY , project. file(" build/server" ). absolutePath)
216+ }
217+
218+ if (System . getenv(BUILDTOOLS_HOME_ENV ) == null ) {
219+ localProps. setProperty(BUILDTOOLS_HOME_PROPERTY , project. file(" build/buildtools" ). absolutePath)
220+ }
221+
222+ localProps. store(propsFile. newWriter(), " This file should *NOT* be checked into Version Control Systems,\n " +
223+ " as it contains information specific to your local configuration.\n " +
224+ " \n " +
225+ " Location of the dev server and BuildTools.\n " +
226+ " For customization when using a Version Control System, please read the\n " +
227+ " header note."
228+ )
229+ }
230+
153231 /**
154232 * Resolves and returns dynamic version
155233 *
@@ -178,16 +256,4 @@ class ServerCore {
178256 def metadata = new XmlSlurper (). parse(metaFile. toFile())
179257 return metadata. versioning. latest. toString()
180258 }
181-
182- /**
183- * Returns server directory
184- *
185- * @return Server directory
186- */
187- Path getServerDir () {
188- Path serverDir = this . project. bukkit. run. dir. resolve(getSimpleVersion())
189- Files . createDirectories(serverDir)
190-
191- return serverDir
192- }
193259}
0 commit comments