Skip to content

Commit

Permalink
Removed the majority of Kosp
Browse files Browse the repository at this point in the history
  • Loading branch information
Hi-Im-Ina committed Sep 17, 2021
1 parent 48ddc52 commit 10f6963
Show file tree
Hide file tree
Showing 13 changed files with 192 additions and 138 deletions.
27 changes: 1 addition & 26 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ plugins {
java
kotlin("jvm") version "1.5.30"
id("net.kyori.blossom") version "1.3.0"
id("com.github.johnrengelman.shadow") version "7.0.0"
id("org.spongepowered.gradle.plugin") version "2.0.0"
}

Expand Down Expand Up @@ -63,7 +62,6 @@ val minecraft by configurations

dependencies {
minecraft("net.minecraftforge:forge:$forgeVersion")
shadow("com.github.randombyte-developer.kosp:kosp:v2.2.3")
compileOnly("pixelmon:Pixelmon-1.12.2-$pixelmonVersion-server:$pixelmonVersion")
compileOnly("org.spongepowered:spongeapi:$spongeVersion")
}
Expand Down Expand Up @@ -95,27 +93,4 @@ sponge {
blossom {
replaceTokenIn("src/main/kotlin/de/randombyte/entityparticles/EntityParticles.kt")
replaceToken("@version@", modVersion)
}

tasks {

shadowJar {
archiveBaseName.set(modName)
archiveClassifier.set("")
archiveVersion.set(modVersion)
configurations = listOf(project.configurations.shadow.get())

relocate ("de.randombyte.kosp", "de.randombyte.entityparticles.shaded.kosp")

exclude("module-info.class", "META-INF/maven/**", "META-INF/proguard/**", "META-INF/versions/**")
minimize()

finalizedBy("reobfJar")
}

artifacts {
archives(shadowJar)
shadow(shadowJar)
}

}
}
41 changes: 19 additions & 22 deletions src/main/kotlin/de/randombyte/entityparticles/EntityParticles.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,14 @@ import de.randombyte.entityparticles.commands.NewParticleConfigCommand
import de.randombyte.entityparticles.commands.SetParticleCommand
import de.randombyte.entityparticles.commands.SetParticleCommand.Companion.ENTITY_UUID_ARG
import de.randombyte.entityparticles.commands.SetParticleCommand.Companion.WORLD_UUID_ARG
import de.randombyte.entityparticles.config.Config
import de.randombyte.entityparticles.config.ConfigManager
import de.randombyte.entityparticles.data.EntityParticlesKeys
import de.randombyte.entityparticles.data.EntityParticlesKeys.IS_REMOVER
import de.randombyte.entityparticles.data.EntityParticlesKeys.PARTICLE_ID
import de.randombyte.entityparticles.data.ParticleData
import de.randombyte.entityparticles.data.RemoverItemData
import de.randombyte.entityparticles.data.particleId
import de.randombyte.kosp.config.ConfigManager
import de.randombyte.kosp.extensions.executeAsConsole
import de.randombyte.kosp.extensions.orNull
import de.randombyte.kosp.extensions.red
import de.randombyte.kosp.extensions.toText
import ninja.leaping.configurate.commented.CommentedConfigurationNode
import ninja.leaping.configurate.loader.ConfigurationLoader
import org.slf4j.Logger
Expand Down Expand Up @@ -50,6 +47,8 @@ import org.spongepowered.api.item.inventory.ItemStack
import org.spongepowered.api.plugin.Dependency
import org.spongepowered.api.plugin.Plugin
import org.spongepowered.api.scheduler.Task
import org.spongepowered.api.text.Text
import org.spongepowered.api.text.serializer.TextSerializers
import org.spongepowered.api.util.Color
import java.util.*

Expand Down Expand Up @@ -169,20 +168,20 @@ class EntityParticles @Inject constructor(
@Listener
fun onRightClickEntity(event: InteractEntityEvent.Secondary.MainHand, @First player: Player, @Getter("getTargetEntity") targetEntity: Entity) {
if (targetEntity.type in config.blockedEntities) return
val itemInHand = player.getItemInHand(HandTypes.MAIN_HAND).orNull() ?: return
val itemInHand = player.getItemInHand(HandTypes.MAIN_HAND).orElse(null) ?: return

val particleId = itemInHand.get(PARTICLE_ID).orNull()
val particleId = itemInHand.get(PARTICLE_ID).orElse(null)
val isRemover = itemInHand.get(IS_REMOVER).orElse(false)

when {
particleId != null -> {
player.setItemInHand(HandTypes.MAIN_HAND, itemInHand.setAmount(itemInHand.quantity - 1))
("entityParticles set ${targetEntity.location.extent.uniqueId} ${targetEntity.uniqueId} $particleId").executeAsConsole()
Sponge.getCommandManager().process(Sponge.getServer().console, "entityParticles set ${targetEntity.location.extent.uniqueId} ${targetEntity.uniqueId} $particleId")
}
isRemover -> {
if (targetEntity.particleId == null) return
player.setItemInHand(HandTypes.MAIN_HAND, itemInHand.setAmount(itemInHand.quantity - 1))
("entityParticles set ${targetEntity.location.extent.uniqueId} ${targetEntity.uniqueId} nothing").executeAsConsole()
Sponge.getCommandManager().process(Sponge.getServer().console, "entityParticles set ${targetEntity.location.extent.uniqueId} ${targetEntity.uniqueId} nothing")
}
else -> return // nothing, no EntityParticle item, prevent cancelling the event
}
Expand All @@ -199,37 +198,35 @@ class EntityParticles @Inject constructor(
fun onUseItemEvent(event: UseItemStackEvent.Start, @First player: Player) = onUseItem(event, player)

private fun onUseItem(event: Cancellable, player: Player) {
val item = player.getItemInHand(HandTypes.MAIN_HAND).orNull() ?: return
val item = player.getItemInHand(HandTypes.MAIN_HAND).orElse(null) ?: return
if (item.get(PARTICLE_ID).isPresent || item.get(IS_REMOVER).isPresent) {
event.isCancelled = true
player.sendMessage("You can't use a ParticleItem!".red())
player.sendMessage(TextSerializers.FORMATTING_CODE.deserialize("&cYou can't use a ParticleItem!"))
}
}

private fun registerCommands() {
Sponge.getCommandManager().getOwnedBy(this).forEach { Sponge.getCommandManager().removeMapping(it) }

val particleIdChoices = config.particles.keys.map { it to it }.toMap()

val particleIdChoices = config.particles.keys.associateWith { it }
Sponge.getCommandManager().register(this, CommandSpec.builder()
.child(CommandSpec.builder()
.permission("$ROOT_PERMISSION.give")
.arguments(
playerOrSource(PLAYER_ARG.toText()),
choices(PARTICLE_ID_ARG.toText(), particleIdChoices))
playerOrSource(Text.of(PLAYER_ARG)),
choices(Text.of(PARTICLE_ID_ARG), particleIdChoices))
.executor(GiveParticleItemCommand(getParticle = { id -> config.particles[id] }))
.build(), "give")
.child(CommandSpec.builder()
.permission("$ROOT_PERMISSION.set")
.arguments(
string(WORLD_UUID_ARG.toText()),
string(ENTITY_UUID_ARG.toText()),
choices(PARTICLE_ID_ARG.toText(), particleIdChoices.plus("nothing" to "nothing")))
string(Text.of(WORLD_UUID_ARG)),
string(Text.of(ENTITY_UUID_ARG)),
choices(Text.of(PARTICLE_ID_ARG), particleIdChoices.plus("nothing" to "nothing")))
.executor(SetParticleCommand(getParticleConfig = { id -> config.particles[id] }))
.build(), "set")
.child(CommandSpec.builder()
.permission("$ROOT_PERMISSION.new-config")
.arguments(string(PARTICLE_ID_ARG.toText()))
.arguments(string(Text.of(PARTICLE_ID_ARG)))
.executor(NewParticleConfigCommand(
addNewConfig = { id, particle ->
config = config.copy(particles = config.particles + (id to particle))
Expand All @@ -241,7 +238,7 @@ class EntityParticles @Inject constructor(
.child(CommandSpec.builder()
.child(CommandSpec.builder()
.permission("$ROOT_PERMISSION.remover-item.give")
.arguments(playerOrSource(PLAYER_ARG.toText()))
.arguments(playerOrSource(Text.of(PLAYER_ARG)))
.executor(GiveRemoverItemCommand(getRemoverItem = { config.removerItem.createItemStack() }))
.build(), "give")
.build(), "removeritem")
Expand All @@ -259,7 +256,7 @@ class EntityParticles @Inject constructor(
Sponge.getServer().worlds.forEach worldLoop@ { world ->
(trackedEntities[world.uniqueId] ?: return@worldLoop)
.mapNotNull { (uuid, id) ->
val entity = world.getEntity(uuid).orNull()
val entity = world.getEntity(uuid).orElse(null)
?: return@mapNotNull null
entity to id
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,30 @@
package de.randombyte.entityparticles.commands

import de.randombyte.entityparticles.Config
import de.randombyte.entityparticles.config.Config
import de.randombyte.entityparticles.EntityParticles.Companion.PARTICLE_ID_ARG
import de.randombyte.entityparticles.EntityParticles.Companion.PLAYER_ARG
import de.randombyte.entityparticles.data.ParticleData
import de.randombyte.kosp.extensions.give
import de.randombyte.kosp.extensions.green
import de.randombyte.kosp.extensions.toText
import org.spongepowered.api.command.CommandException
import org.spongepowered.api.command.CommandResult
import org.spongepowered.api.command.CommandSource
import org.spongepowered.api.command.args.CommandContext
import org.spongepowered.api.command.spec.CommandExecutor
import org.spongepowered.api.entity.living.player.Player
import org.spongepowered.api.text.Text
import org.spongepowered.api.text.serializer.TextSerializers

internal class GiveParticleItemCommand(private val getParticle: (id: String) -> Config.Particle?): CommandExecutor {
override fun execute(src: CommandSource, args: CommandContext): CommandResult {
val player = args.getOne<Player>(PLAYER_ARG).get()
val particleId = args.getOne<String>(PARTICLE_ID_ARG).get()

val particle = getParticle(particleId)
?: throw CommandException("Particle '$particleId' is not available!".toText())
?: throw CommandException(Text.of("Particle '$particleId' is not available!"))

val itemStack = particle.createItemStack()
.apply { offer(ParticleData(id = particleId, isActive = false)) }
player.give(itemStack)

player.sendMessage("Given '$particleId' to ${player.name}!".green())

player.inventory.offer(itemStack)
player.sendMessage(TextSerializers.FORMATTING_CODE.deserialize("&aGave '$particleId' to ${player.name}!"))
return CommandResult.success()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,24 @@ package de.randombyte.entityparticles.commands
import de.randombyte.entityparticles.EntityParticles.Companion.PLAYER_ARG
import de.randombyte.entityparticles.data.RemoverItemData
import de.randombyte.entityparticles.singleCopy
import de.randombyte.kosp.extensions.give
import de.randombyte.kosp.extensions.green
import org.spongepowered.api.command.CommandResult
import org.spongepowered.api.command.CommandSource
import org.spongepowered.api.command.args.CommandContext
import org.spongepowered.api.command.spec.CommandExecutor
import org.spongepowered.api.entity.living.player.Player
import org.spongepowered.api.event.cause.Cause
import org.spongepowered.api.item.inventory.ItemStack
import org.spongepowered.api.item.inventory.ItemStackSnapshot
import org.spongepowered.api.text.serializer.TextSerializers

internal class GiveRemoverItemCommand(
private val getRemoverItem: () -> ItemStack
) : CommandExecutor {
override fun execute(src: CommandSource, args: CommandContext): CommandResult {
val player = args.getOne<Player>(PLAYER_ARG).get()

val itemStack = getRemoverItem()
.singleCopy()
.apply { offer(RemoverItemData(isRemover = true)) }
player.give(itemStack)

player.sendMessage("Given the remover item to ${player.name}!".green())

player.inventory.offer(itemStack)
player.sendMessage(TextSerializers.FORMATTING_CODE.deserialize("&aGave the remover to ${player.name}!"))
return CommandResult.success()
}
}
Original file line number Diff line number Diff line change
@@ -1,30 +1,31 @@
package de.randombyte.entityparticles.commands

import de.randombyte.entityparticles.Config
import de.randombyte.entityparticles.config.Config
import de.randombyte.entityparticles.EntityParticles
import de.randombyte.entityparticles.singleCopy
import de.randombyte.kosp.PlayerExecutedCommand
import de.randombyte.kosp.extensions.green
import de.randombyte.kosp.extensions.toText
import org.spongepowered.api.command.CommandException
import org.spongepowered.api.command.CommandResult
import org.spongepowered.api.command.CommandSource
import org.spongepowered.api.command.args.CommandContext
import org.spongepowered.api.data.type.HandTypes
import org.spongepowered.api.command.spec.CommandExecutor
import org.spongepowered.api.entity.living.player.Player
import org.spongepowered.api.text.Text
import org.spongepowered.api.text.serializer.TextSerializers

internal class NewParticleConfigCommand(
private val addNewConfig: (id: String, Config.Particle) -> Unit,
private val updateCommands: () -> Unit
) : PlayerExecutedCommand() {
override fun executedByPlayer(player: Player, args: CommandContext): CommandResult {
val newId = args.getOne<String>(EntityParticles.PARTICLE_ID_ARG).get()
private val addNewConfig: (id: String, Config.Particle) -> Unit,
private val updateCommands: () -> Unit
) : CommandExecutor {

addNewConfig(newId, Config().particles.getValue("love").copy(
override fun execute(src: CommandSource, args: CommandContext): CommandResult {
if (src !is Player)
throw CommandException(Text.of("Command must be executed by a player"))
val newId = args.getOne<String>(EntityParticles.PARTICLE_ID_ARG).get()
addNewConfig(
newId, Config().particles.getValue("love").copy(
item = "minecraft:stick"
))

player.sendMessage("Added to config!".green())

)
)
src.sendMessage(TextSerializers.FORMATTING_CODE.deserialize("&aAdded to config!"))
updateCommands()

return CommandResult.success()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package de.randombyte.entityparticles.commands

import de.randombyte.entityparticles.Config
import de.randombyte.entityparticles.config.Config
import de.randombyte.entityparticles.EntityParticles
import de.randombyte.entityparticles.EntityParticles.Companion.PARTICLE_ID_ARG
import de.randombyte.entityparticles.data.particleId
import de.randombyte.kosp.extensions.getWorld
import de.randombyte.kosp.extensions.orNull
import de.randombyte.kosp.extensions.toText
import de.randombyte.kosp.extensions.toUUID
import org.spongepowered.api.Sponge
import org.spongepowered.api.command.CommandException
import org.spongepowered.api.command.CommandResult
import org.spongepowered.api.command.CommandSource
import org.spongepowered.api.command.args.CommandContext
import org.spongepowered.api.command.spec.CommandExecutor
import org.spongepowered.api.data.key.Keys
import org.spongepowered.api.text.Text
import java.util.*

internal class SetParticleCommand(
private val getParticleConfig: (id: String) -> Config.Particle?
Expand All @@ -29,11 +27,9 @@ internal class SetParticleCommand(
val entityUuidString = args.getOne<String>(ENTITY_UUID_ARG).get()
val particleId = args.getOne<String>(PARTICLE_ID_ARG).get()

val world = worldUuidString.toUUID().getWorld()
?: throw CommandException("World '$worldUuidString' is not available!".toText())
val entity = (world.getEntity(entityUuidString.toUUID()).orNull()
?: throw CommandException("Entity '$entityUuidString' in world '$world' is not available!".toText()))

val world = Sponge.getServer().getWorld(UUID.fromString(worldUuidString)).orElse(null) ?: throw CommandException(Text.of("World '$worldUuidString' is not available!"))
val uuid = UUID.fromString(entityUuidString)
val entity = (world.getEntity(uuid).orElse(null) ?: throw CommandException(Text.of("Entity '$entityUuidString' in world '$world' is not available!")))
if (particleId == "nothing") {
entity.particleId = null
entity.offer(Keys.GLOWING, false)
Expand All @@ -43,8 +39,7 @@ internal class SetParticleCommand(
return CommandResult.success()
}

val particleConfig = getParticleConfig(particleId)
?: throw CommandException("Particle '$particleId' is not available!".toText())
val particleConfig = getParticleConfig(particleId) ?: throw CommandException(Text.of("Particle '$particleId' is not available!"))

entity.particleId = particleId

Expand Down
Loading

0 comments on commit 10f6963

Please sign in to comment.