Skip to content

Commit

Permalink
Update 0.6-beta
Browse files Browse the repository at this point in the history
 - Added '/vector config' options
  • Loading branch information
patrick-choe committed Mar 23, 2020
1 parent 8209eff commit e33b90b
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 22 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
[![GitHub Downloads](https://img.shields.io/github/downloads/patrick-mc/vector-game/total)](https://github.com/patrick-mc/vector-game/releases)
[![GitHub Issues](https://img.shields.io/github/issues-raw/patrick-mc/vector-game)](https://github.com/patrick-mc/vector-game/issues?q=is%3Aissue+is%3Aopen)
[![GitHub Closed Issues](https://img.shields.io/github/issues-closed-raw/patrick-mc/vector-game)](https://github.com/patrick-mc/vector-game/issues?q=is%3Aissue+is%3Aclosed)
[![GitHub Issues](https://img.shields.io/github/issues-pr-raw/patrick-mc/vector-game)](https://github.com/patrick-mc/vector-game/pulls?q=is%3Apr+is%3Aopen)
[![GitHub Closed Issues](https://img.shields.io/github/issues-pr-closed-raw/patrick-mc/vector-game)](https://github.com/patrick-mc/vector-game/pulls?q=is%3Apr+is%3Aclosed)
[![GitHub Pull Requests](https://img.shields.io/github/issues-pr-raw/patrick-mc/vector-game)](https://github.com/patrick-mc/vector-game/pulls?q=is%3Apr+is%3Aopen)
[![GitHub Closed Pull Requests](https://img.shields.io/github/issues-pr-closed-raw/patrick-mc/vector-game)](https://github.com/patrick-mc/vector-game/pulls?q=is%3Apr+is%3Aclosed)

> A vector game for education
Expand All @@ -41,7 +41,7 @@
>
>...
>dependencies {
> implementation 'com.github.patrick-mc:vector-game:0.5.1-beta'
> implementation 'com.github.patrick-mc:vector-game:0.6-beta'
>}
>```
Expand All @@ -56,6 +56,6 @@
>
>...
>dependencies {
> implementation("com.github.patrick-mc:vector-game:0.5.1-beta")
> implementation("com.github.patrick-mc:vector-game:0.6-beta")
>}
>```
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group = "com.github.patrick-mc"
version = "0.5.1-beta"
version = "0.6-beta"

repositories {
maven("https://repo.maven.apache.org/maven2/")
Expand Down
91 changes: 79 additions & 12 deletions src/main/kotlin/com/github/patrick/vector/VectorPlugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -44,17 +44,22 @@ import org.bukkit.event.player.PlayerInteractEvent
import org.bukkit.event.player.PlayerQuitEvent
import org.bukkit.plugin.java.JavaPlugin
import java.io.File
import java.io.IOException
import java.nio.charset.StandardCharsets.UTF_8
import java.nio.file.Files
import java.nio.file.Paths
import kotlin.random.Random.Default.nextDouble

class VectorPlugin : JavaPlugin(), Listener {
private var status = false
private val selectedEntities = HashMap<Player, Entity>()

private var lastModified: Long? = null
private var bothHands: Boolean = false
private var singleTime: Boolean = false
private var velocityModifier: Double = 0.0
private var maxVelocity: Double = 0.0
private var bothHands = false
private var singleTime = false
private var visibilityLength = 0.0
private var velocityModifier = 0.0
private var maxVelocity = 0.0

private fun statusOn(): Boolean {
status = true
Expand All @@ -72,17 +77,25 @@ class VectorPlugin : JavaPlugin(), Listener {
return true
}

private fun CommandSender.requireMessage(message: String) = sendMessage("Required: $message")

private fun CommandSender.unrecognizedMessage(message: String, value: String) = sendMessage("Unrecognized $message: '$value'")

private fun Player.getTarget(): Location {
val loc = eyeLocation.clone()
val view = loc.clone().add(loc.clone().direction.normalize().multiply(5))
val view = loc.clone().add(loc.clone().direction.normalize().multiply(visibilityLength))
val block =
MATH.rayTraceBlock(loc.world, Vector(loc.x, loc.y, loc.z), Vector(view.x, view.y, view.z), 0)
?: return loc.clone().add(eyeLocation.direction.clone().normalize().multiply(5))
?: return loc.clone().add(eyeLocation.direction.clone().normalize().multiply(visibilityLength))
block.blockPoint.let {
return loc.world.getBlockAt(it.x, it.y, it.z).getRelative(block.face).location.add(0.5, 0.5, 0.5)
}
}

private fun String.resetRegexMatch(): Boolean = contains(Regex("(?i)conf|set"))

private fun getKeys() = config.getKeys(false)

private fun setTargetVelocity(player: Player, remove: Boolean): Boolean? {
selectedEntities[player]?.let {
val vector = player.getTarget().subtract(it.location).toVector()
Expand Down Expand Up @@ -139,17 +152,71 @@ class VectorPlugin : JavaPlugin(), Listener {
reloadConfig()
bothHands = config.getBoolean("use-both-hands")
singleTime = config.getBoolean("set-single-time")
velocityModifier = config.getDouble("velocity-modifier")
maxVelocity = config.getDouble("max-velocity")
visibilityLength = config.getDouble("visibility-length-double")
velocityModifier = config.getDouble("velocity-modifier-double")
maxVelocity = config.getDouble("max-velocity-double")
}
}, 0, 1)
}

override fun onCommand(sender: CommandSender?, command: Command?, label: String?, args: Array<out String>?) =
if (!status) statusOn() else statusOff()
@Throws(IOException::class, NumberFormatException::class)
override fun onCommand(sender: CommandSender, command: Command?, label: String?, args: Array<out String>): Boolean {
if (args.isNotEmpty()) {
if (args[0].resetRegexMatch()) {
when (args.size) {
1 -> sender.requireMessage("key, value").also { return false }
2 -> {
if (args[1].contains("reset", true)) {
File(dataFolder, "config.yml").delete()
saveDefaultConfig()
return true
}
if (config.getKeys(false).contains(args[1])) sender.requireMessage("value").also { return true }
sender.unrecognizedMessage("key", args[1])
}
3 -> {
if (config.getKeys(false).contains(args[1])) {
try {
val path = Paths.get(dataFolder.toURI().path + File.separator + "config.yml")
val lines = Files.readAllLines(path, UTF_8)
for (i in 0 until lines.count()) {
if (lines[i].contains(args[1])) when {
args[1].contains("double") -> lines[i] = "${args[1]}: ${args[2].toDouble()}"
args[2].contains("true", true) -> lines[i] = "${args[1]}: true"
args[2].contains("false", true) -> lines[i] = "${args[1]}: false"
else -> sender.unrecognizedMessage("value", args[2])
}
}
Files.write(path, lines, UTF_8)
return true
} catch (e: Exception) {
when (e) {
is IOException -> logger.info("Cannot read/write to config.yml")
is NumberFormatException -> sender.unrecognizedMessage("value", args[2])
else -> throw e
}
return false
}
}
}
}
}
return false
}
return if (!status) statusOn() else statusOff()
}

override fun onTabComplete(sender: CommandSender?, command: Command?, alias: String?, args: Array<out String>?) =
emptyList<String>()
override fun onTabComplete(sender: CommandSender?, command: Command?, alias: String?, args: Array<out String>) =
when (args.size) {
1 -> setOf("config").filter { it.startsWith(args[0], true) }
2 -> if (args[0].resetRegexMatch()) getKeys().filter { it.startsWith(args[1], true) } else emptyList()
3 -> if (args[0].resetRegexMatch() && getKeys().contains(args[1])) {
if (!args[1].contains("double", true))
setOf("true, false").filter { it.startsWith(args[1], true) }
else emptyList()
} else emptyList()
else -> emptyList()
}

@EventHandler
fun onInteract(event: PlayerInteractEvent) {
Expand Down
13 changes: 9 additions & 4 deletions src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,18 @@ use-both-hands: false
# If false, players can set velocity multiple times until selecting new entity.
set-single-time: true

# Sets players' visibility length when selecting entities
# Put double value to modify visibility length.
# Recommended value: 4.0 ~
visibility-length-double: 5.0

# Sets velocity modifier
# Put Double value to modify velocity.
# If set to 1.0, the velocity would be so big that entity could move too far.
# Recommended Value: 0.2 ~ 0.4
velocity-modifier: 0.3
# Recommended value: 0.2 ~ 0.4
velocity-modifier-double: 0.3

# Sets maximum velocity
# Put Double value to set maximum velocity.
# Recommended Value: 20 ~ 30
max-velocity: 24
# Recommended value: 20 ~ 30
max-velocity-double: 24
2 changes: 1 addition & 1 deletion src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: VectorGame
version: 0.5.1-beta
version: 0.6-beta
description: A vector game for education
api-version: "1.12"
load: POSTWORLD
Expand Down

0 comments on commit e33b90b

Please sign in to comment.