Archived
0

переход на groovy конфигурацию

This commit is contained in:
2019-02-17 23:26:03 +03:00
parent f08190fba7
commit 72c24b2adf
17 changed files with 198 additions and 211 deletions

View File

@@ -2,7 +2,7 @@
Ядро сервера.
Пример настройки можно посмотреть в файле `sample-config.xml`.
Пример настройки можно посмотреть в файле [`sample-config.groovy`](./sample-config.groovy).
## Spring beans
@@ -14,8 +14,10 @@
**Bean example:**
```xml
<bean class="mc.core.CoreEventListener"/>
```groovy
beans {
coreEventListener(mc.core.CoreEventListener)
}
```
#### ConfigFromSpring
@@ -31,12 +33,14 @@
**Bean example:**
```xml
<bean id="config" class="mc.core.embedded.ConfigFromSpring">
<property name="descriptionServer" value="MC Core"/>
<property name="maxPlayers" value="100"/>
<property name="favicon" value="icon.png"/>
</bean>
```groovy
beans {
config(mc.core.embedded.ConfigFromSpring) {
descriptionServer = 'MC Core - LIMBO'
maxPlayers = 1
favicon = 'icon.png'
}
}
```
#### GameLoop
@@ -47,11 +51,13 @@
* `gameTimer` - бин, управляющий ходом времени
* `percentWarnLowTps` - порог "низкого" значения TPS, в процентах
```xml
<bean id="gameLoop" class="mc.core.GameLoop">
<property name="gameTimer" ref="timeProcessor"/>
<property name="percentWarnLowTps" value="15"/>
</bean>
```groovy
beans {
gameLoop(mc.core.GameLoop) {
gameTimer = ref('timeProcessor')
percentWarnLowTps = 15
}
}
```
#### SimpleChatProcessor
@@ -62,8 +68,10 @@
**Bean example:**
```xml
<bean id="chatProcessor" class="mc.core.chat.SimpleChatProcessor" />
```groovy
beans {
chatProcessor(mc.core.chat.SimpleChatProcessor)
}
```
### Время
@@ -79,10 +87,12 @@
**Bean example:**
```xml
<bean id="idleTime" class="mc.core.time.IdleTime">
<property name="gameTime" value="1000"/>
</bean>
```groovy
beans {
timeProcessor(mc.core.time.IdleTime) {
gameTime = 1000
}
}
```
#### TimePerTick
@@ -96,10 +106,12 @@
**Bean example:**
```xml
<bean id="timePerTick" class="mc.core.time.TimePerTick">
<property name="startGameTime" value="1000"/>
</bean>
```groovy
beans {
timeProcessor(mc.core.time.TimePerTick) {
startGameTime = 1000
}
}
```
#### RealTime
@@ -110,6 +122,8 @@
**Bean example:**
```xml
<bean id="realTime" class="mc.core.time.RealTime"/>
```groovy
beans {
timeProcessor(mc.core.time.RealTime)
}
```

View File

@@ -1,14 +1,22 @@
version '0.2'
version '0.2.1'
apply plugin: 'maven'
apply plugin: 'application'
mainClassName = "mc.core.Main"
mainClassName = 'mc.core.Main'
ext {
groovy_version = '2.5.6'
}
dependencies {
/* Spring Groovy Bean Configuration */
compile (group: 'org.codehaus.groovy', name: 'groovy', version: groovy_version)
compile (group: 'org.codehaus.groovy', name: 'groovy-xml', version: groovy_version)
/* Components */
compile (group: 'commons-io', name: 'commons-io', version: '2.6')
compile (group: 'com.google.guava', name: 'guava', version: '26.0-jre')
/* Named Binary Tags */
compile (group: 'commons-io', name: 'commons-io', version: '2.6')
compile (group: 'com.google.guava', name: 'guava', version: '26.0-jre')
/* NBT, Named Binary Tags */
compile (group: 'com.flowpowered', name: 'flow-nbt', version: '1.0.1-SNAPSHOT')
}

26
core/sample-config.groovy Normal file
View File

@@ -0,0 +1,26 @@
beans {
xmlns([context:'http://www.springframework.org/schema/context'])
context.'annotation-config'()
config(mc.core.embedded.ConfigFromSpring) {
descriptionServer = 'MC Core - LIMBO'
maxPlayers = 1
}
playerManager(mc.core.embedded.FakePlayerManager)
timer(mc.core.time.IdleTime) {
gameTime = 1000
}
coreEventListener(mc.core.CoreEventListener)
gameLoop(mc.core.GameLoop) {
gameTimer = timer
percentWarnLowTps = 15
}
chatProcessor(mc.core.chat.SimpleChatProcessor)
server(mc.core.embedded.FakeServer)
}

View File

@@ -1,28 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config />
<bean id="config" class="mc.core.embedded.ConfigFromSpring">
<property name="descriptionServer" value="MC Core - LIMBO"/>
<property name="maxPlayers" value="1"/>
</bean>
<bean id="timer" class="mc.core.time.IdleTime">
<property name="gameTime" value="1000"/>
</bean>
<bean class="mc.core.CoreEventListener"/>
<bean id="gameLoop" class="mc.core.GameLoop">
<property name="gameTimer" ref="timer"/>
<property name="percentWarnLowTps" value="15"/>
</bean>
<bean id="chatProcessor" class="mc.core.chat.SimpleChatProcessor" />
</beans>

View File

@@ -5,7 +5,7 @@ import mc.core.network.Server;
import mc.core.network.StartServerException;
import org.apache.commons.io.IOUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import org.springframework.context.support.GenericGroovyApplicationContext;
import java.io.FileOutputStream;
import java.io.IOException;
@@ -15,19 +15,20 @@ import java.nio.file.Paths;
@Slf4j
public class Main {
private static ApplicationContext createContext() {
final String springXml = System.getProperty("springConfig", "./spring.xml");
final String defautlSpringConfig = "spring.groovy";
final String springConfig = System.getProperty("springConfig", "./" + defautlSpringConfig);
if (!Files.exists(Paths.get(springXml))) {
log.info("File \"{}\" not found. Get default config.", springXml);
try (FileOutputStream fos = new FileOutputStream(springXml)) {
IOUtils.copy(Main.class.getResourceAsStream("/spring.xml"), fos);
if (!Files.exists(Paths.get(springConfig))) {
log.info("File \"{}\" not found. Get default config.", springConfig);
try (FileOutputStream fos = new FileOutputStream(springConfig)) {
IOUtils.copy(Main.class.getResourceAsStream("/" + defautlSpringConfig), fos);
} catch (IOException e) {
log.error("Get default spring config", e);
System.exit(-1);
}
}
return new FileSystemXmlApplicationContext(springXml);
return new GenericGroovyApplicationContext("file:" + springConfig);
}
public static void main(String[] args) {

View File

@@ -0,0 +1,14 @@
beans {
xmlns([context:'http://www.springframework.org/schema/context'])
context.'annotation-config'()
playerManager(mc.core.embedded.FakePlayerManager)
timer(mc.core.time.TimePerTick)
gameLoop(mc.core.GameLoop) {
gameTimer = timer
}
server(mc.core.embedded.FakeServer)
}

View File

@@ -1,20 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config />
<bean id="playerManager" class="mc.core.embedded.FakePlayerManager"/>
<bean id="gameLoop" class="mc.core.GameLoop">
<property name="gameTimer">
<bean class="mc.core.time.TimePerTick"/>
</property>
</bean>
<bean id="server" class="mc.core.embedded.FakeServer"/>
</beans>