11package de.randombyte.unity
22
3+ import com.flowpowered.math.vector.Vector3d
34import com.google.inject.Inject
45import de.randombyte.kosp.config.ConfigManager
56import de.randombyte.kosp.extensions.toOptional
@@ -10,6 +11,8 @@ import de.randombyte.unity.Unity.Companion.NAME
1011import de.randombyte.unity.Unity.Companion.NUCLEUS_ID
1112import de.randombyte.unity.Unity.Companion.VERSION
1213import de.randombyte.unity.commands.*
14+ import de.randombyte.unity.config.Config
15+ import de.randombyte.unity.config.ConfigAccessor
1316import io.github.nucleuspowered.nucleus.api.service.NucleusMessageTokenService
1417import ninja.leaping.configurate.commented.CommentedConfigurationNode
1518import ninja.leaping.configurate.loader.ConfigurationLoader
@@ -19,8 +22,15 @@ import org.spongepowered.api.Sponge
1922import org.spongepowered.api.command.args.GenericArguments.player
2023import org.spongepowered.api.command.spec.CommandSpec
2124import org.spongepowered.api.config.DefaultConfig
25+ import org.spongepowered.api.data.key.Keys
26+ import org.spongepowered.api.data.property.entity.EyeLocationProperty
27+ import org.spongepowered.api.effect.particle.ParticleEffect
28+ import org.spongepowered.api.effect.particle.ParticleTypes
2229import org.spongepowered.api.entity.living.player.Player
2330import org.spongepowered.api.event.Listener
31+ import org.spongepowered.api.event.entity.InteractEntityEvent
32+ import org.spongepowered.api.event.filter.Getter
33+ import org.spongepowered.api.event.filter.cause.Root
2434import org.spongepowered.api.event.game.GameReloadEvent
2535import org.spongepowered.api.event.game.state.GameInitializationEvent
2636import org.spongepowered.api.event.game.state.GameStartingServerEvent
@@ -44,7 +54,7 @@ class Unity @Inject constructor(
4454 companion object {
4555 const val ID = " unity"
4656 const val NAME = " Unity"
47- const val VERSION = " 2.1 "
57+ const val VERSION = " 2.2 "
4858 const val AUTHOR = " RandomByte"
4959
5060 const val NUCLEUS_ID = " nucleus"
@@ -63,9 +73,28 @@ class Unity @Inject constructor(
6373 simpleTextTemplateSerialization = true
6474 )
6575
76+ private lateinit var config: Config
77+
78+ private val configAccessor = object : ConfigAccessor () {
79+ override fun get () = config
80+ override fun set (config : Config ) {
81+ this @Unity.config = config
82+ saveConfig() // always save config in case of sudden server shutdown
83+ }
84+ }
85+
6686 // <requestee, requesters>
6787 private val unityRequests: MutableMap <UUID , List <UUID >> = mutableMapOf ()
6888
89+ private val kissingParticleEffect = lazy {
90+ ParticleEffect .builder()
91+ .type(ParticleTypes .HEART )
92+ .quantity(1 )
93+ .offset(Vector3d (0.3 , 0.3 , 0.3 ))
94+ .velocity(Vector3d (0.1 , 0.1 , 0.0 ))
95+ .build()
96+ }
97+
6998 @Listener
7099 fun onInit (event : GameInitializationEvent ) {
71100 registerCommands()
@@ -76,12 +105,14 @@ class Unity @Inject constructor(
76105 @Listener
77106 fun onWorldsLoaded (event : GameStartingServerEvent ) {
78107 // do this here to ensure all worlds are loaded for location deserialization
79- configManager.generate()
108+ loadConfig()
109+ saveConfig()
80110 }
81111
82112 @Listener
83113 fun onReload (event : GameReloadEvent ) {
84- configManager.generate()
114+ loadConfig()
115+ saveConfig()
85116 unityRequests.clear()
86117
87118 logger.info(" Reloaded!" )
@@ -103,6 +134,16 @@ class Unity @Inject constructor(
103134 })
104135 }
105136
137+ @Listener
138+ fun onKissPartner (event : InteractEntityEvent .Secondary .MainHand , @Root player : Player , @Getter(" getTargetEntity" ) partner : Player ) {
139+ if (! config.kissingEnabled || ! player.get(Keys .IS_SNEAKING ).orElse(false )) return
140+ with (config.unities) {
141+ val unity = getUnity(player.uniqueId) ? : return
142+ if (unity.getOtherMember(player.uniqueId) != partner.uniqueId) return
143+ partner.world.spawnParticles(kissingParticleEffect.value, partner.getProperty(EyeLocationProperty ::class .java).get().value)
144+ }
145+ }
146+
106147 private fun registerCommands () {
107148 val removeRequest = { requester: UUID , requestee: UUID ->
108149 unityRequests + = (requestee to (unityRequests[requestee] ? : emptyList()).filterNot { it == requester })
@@ -113,7 +154,7 @@ class Unity @Inject constructor(
113154 .permission(PLAYER_PERMISSION )
114155 .arguments(player(PLAYER_ARG .toText()))
115156 .executor(RequestUnityCommand (
116- configManager ,
157+ configAccessor ,
117158 addRequest = { requester, requestee ->
118159 val existingRequesters = unityRequests[requestee] ? : emptyList()
119160 if (requester in existingRequesters) return @RequestUnityCommand false
@@ -124,47 +165,55 @@ class Unity @Inject constructor(
124165
125166 .child(CommandSpec .builder()
126167 .permission(PLAYER_PERMISSION )
127- .executor(HelpCommand (configManager ))
168+ .executor(HelpCommand (configAccessor ))
128169 .build(), " help" )
129170 .child(CommandSpec .builder()
130171 .permission(PLAYER_PERMISSION )
131172 .arguments(player(PLAYER_ARG .toText()))
132- .executor(AcceptRequestCommand (configManager , this ::unityRequests, removeRequest))
173+ .executor(AcceptRequestCommand (configAccessor , this ::unityRequests, removeRequest))
133174 .build(), " accept" )
134175 .child(CommandSpec .builder()
135176 .permission(PLAYER_PERMISSION )
136177 .arguments(player(PLAYER_ARG .toText()))
137- .executor(DeclineRequestCommand (configManager , this ::unityRequests, removeRequest))
178+ .executor(DeclineRequestCommand (configAccessor , this ::unityRequests, removeRequest))
138179 .build(), " decline" )
139180 .child(CommandSpec .builder()
140181 .permission(PLAYER_PERMISSION )
141182 .arguments(player(PLAYER_ARG .toText()))
142- .executor(CancelRequestCommand (configManager , this ::unityRequests, removeRequest))
183+ .executor(CancelRequestCommand (configAccessor , this ::unityRequests, removeRequest))
143184 .build(), " cancel" )
144185 .child(CommandSpec .builder()
145186 .permission(PLAYER_PERMISSION )
146- .executor(ListUnitiesCommand (configManager ))
187+ .executor(ListUnitiesCommand (configAccessor ))
147188 .build(), " list" )
148189 .child(CommandSpec .builder()
149190 .permission(PLAYER_PERMISSION )
150- .executor(DivorceCommand (configManager ))
191+ .executor(DivorceCommand (configAccessor ))
151192 .build(), " divorce" )
152193 .child(CommandSpec .builder()
153194 .permission(PLAYER_PERMISSION )
154- .executor(TeleportCommand (configManager ))
195+ .executor(TeleportCommand (configAccessor ))
155196 .build(), " teleport" , " tp" )
156197 .child(CommandSpec .builder()
157198 .permission(PLAYER_PERMISSION )
158- .executor(GiftCommand (configManager ))
199+ .executor(GiftCommand (configAccessor ))
159200 .build(), " gift" )
160201 .child(CommandSpec .builder()
161202 .permission(PLAYER_PERMISSION )
162- .executor(HomeCommand (configManager ))
203+ .executor(HomeCommand (configAccessor ))
163204 .child(CommandSpec .builder()
164205 .permission(PLAYER_PERMISSION )
165- .executor(SetHomeCommand (configManager ))
206+ .executor(SetHomeCommand (configAccessor ))
166207 .build(), " set" )
167208 .build(), " home" )
168209 .build(), " unity" , " marry" )
169210 }
211+
212+ private fun loadConfig () {
213+ config = configManager.get()
214+ }
215+
216+ private fun saveConfig () {
217+ configManager.save(config)
218+ }
170219}
0 commit comments