Created
September 8, 2016 11:23
-
-
Save orekyuu/2bee1ffac7c8bcffbbc6afd3ca6cf8d7 to your computer and use it in GitHub Desktop.
SpongeプラグインでSpringを使う
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.orekyuu.springdemo.core; | |
import net.orekyuu.springdemo.SpringDemo; | |
import org.spongepowered.api.Sponge; | |
import org.spongepowered.api.event.EventManager; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.context.annotation.Bean; | |
import org.springframework.context.annotation.Configuration; | |
//イベント周りの設定とか | |
@Configuration | |
public class EventConfig { | |
@Autowired | |
private EventConverter eventConverter; | |
//EventConverterをEventManagerに登録する | |
@Bean | |
public EventManager eventManager() { | |
EventManager manager = Sponge.getEventManager(); | |
manager.registerListeners(springDemo(), eventConverter); | |
return manager; | |
} | |
@Bean | |
public SpringDemo springDemo() { | |
return SpringDemo.getInstance(); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.orekyuu.springdemo.core; | |
import org.spongepowered.api.event.Event; | |
import org.spongepowered.api.event.Listener; | |
import org.spongepowered.api.event.network.ClientConnectionEvent; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.context.ApplicationEventPublisher; | |
import org.springframework.stereotype.Component; | |
//全てのSpongeのイベントを受け取って、Springのイベントに変換する | |
@Component | |
public class EventConverter { | |
@Autowired | |
private ApplicationEventPublisher publisher; | |
@Listener | |
public void onSpongeEvent(Event event) { | |
//SpongeのイベントをSpringに流す | |
publisher.publishEvent(new SpongeEvent<>(event)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.orekyuu.springdemo.core; | |
import net.orekyuu.springdemo.SpringDemo; | |
import org.spongepowered.api.event.Event; | |
import org.springframework.context.ApplicationEvent; | |
import java.util.Objects; | |
public class SpongeEvent<T extends Event> extends ApplicationEvent { | |
private final T event; | |
public SpongeEvent(T event) { | |
super(SpringDemo.getInstance()); //とりあえず適当に渡しておく | |
Objects.requireNonNull(event); | |
this.event = event; | |
} | |
public T getEvent() { | |
return event; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.orekyuu.springdemo.event; | |
import net.orekyuu.springdemo.service.PlayerMessageService; | |
import net.orekyuu.springdemo.core.SpongeEvent; | |
import org.spongepowered.api.event.network.ClientConnectionEvent; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.context.event.EventListener; | |
import org.springframework.stereotype.Component; | |
//プレイヤーがサーバーに入った時のイベントを受け取ってなにかする | |
@Component | |
public class PlayerJoinEventListener { | |
@Autowired | |
public PlayerMessageService playerMessageService; | |
@EventListener | |
public void handleOnPlayerJoin(SpongeEvent<ClientConnectionEvent.Join> event) { | |
playerMessageService.sendMessageToPlayer(event.getEvent().getTargetEntity(), "Hello!"); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.orekyuu.springdemo.service; | |
import org.spongepowered.api.entity.living.player.Player; | |
import org.spongepowered.api.text.Text; | |
import org.spongepowered.api.text.format.TextColors; | |
import org.spongepowered.api.text.format.TextStyles; | |
import org.springframework.stereotype.Component; | |
//プレイヤーにメッセージを送る機能を提供するサービス | |
@Component | |
public class PlayerMessageService { | |
public void sendMessageToPlayer(Player player, String message) { | |
player.sendMessage(Text.of(TextColors.AQUA, TextStyles.BOLD, message + " " + player.getName())); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package net.orekyuu.springdemo; | |
import org.spongepowered.api.event.Listener; | |
import org.spongepowered.api.event.game.state.GameConstructionEvent; | |
import org.spongepowered.api.plugin.Plugin; | |
import org.springframework.context.ApplicationContext; | |
import org.springframework.context.annotation.AnnotationConfigApplicationContext; | |
@Plugin( | |
id = "springdemo", | |
name = "SpringDemo", | |
version = "1.0-SNAPSHOT" | |
) | |
public class SpringDemo { | |
private ApplicationContext context; | |
private static SpringDemo INSTANCE; | |
@Listener | |
public void onServerStart(GameConstructionEvent event) { | |
INSTANCE = this; | |
context = new AnnotationConfigApplicationContext("net.orekyuu.springdemo"); | |
} | |
public static SpringDemo getInstance() { | |
return INSTANCE; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment