-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #53 from flytegg/clone/master
Clone/master
- Loading branch information
Showing
28 changed files
with
739 additions
and
77 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
2 changes: 1 addition & 1 deletion
2
...e/twilight/itembuilder/ItemInteraction.kt → ...twilight/builders/item/ItemInteraction.kt
This file contains 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
2 changes: 1 addition & 1 deletion
2
...te/twilight/itembuilder/PersistentData.kt → .../twilight/builders/item/PersistentData.kt
This file contains 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
43 changes: 43 additions & 0 deletions
43
src/main/kotlin/gg/flyte/twilight/builders/recipe/RecipeBuilder.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package gg.flyte.twilight.builders.recipe | ||
|
||
import org.bukkit.Material | ||
import org.bukkit.NamespacedKey | ||
import org.bukkit.inventory.ItemStack | ||
import org.bukkit.inventory.ShapedRecipe | ||
import org.bukkit.plugin.java.JavaPlugin | ||
|
||
class RecipeBuilder(result: ItemStack, plugin: JavaPlugin, key: String) { | ||
private val recipe: ShapedRecipe = ShapedRecipe(NamespacedKey(plugin, key), result) | ||
private val ingredients: MutableMap<Char, RecipeIngredient> = HashMap() | ||
|
||
fun setShape(vararg shape: String): RecipeBuilder { | ||
recipe.shape(*shape) | ||
return this | ||
} | ||
|
||
fun setIngredient(key: Char, material: Material, amount: Int): RecipeBuilder { | ||
val recipeIngredient = RecipeIngredient(ItemStack(material, amount)) | ||
recipe.setIngredient(key, recipeIngredient.materialData) | ||
ingredients[key] = recipeIngredient | ||
return this | ||
} | ||
|
||
fun setIngredient(key: Char, itemStack: ItemStack, amount: Int): RecipeBuilder { | ||
val recipeIngredient = RecipeIngredient(itemStack.clone(), amount) | ||
recipe.setIngredient(key, recipeIngredient.materialData) | ||
ingredients[key] = recipeIngredient | ||
return this | ||
} | ||
|
||
fun build(): ShapedRecipe { | ||
ingredients.forEach { (key, value) -> | ||
recipe.setIngredient(key, value.materialData) | ||
} | ||
return recipe | ||
} | ||
|
||
private inner class RecipeIngredient(private val itemStack: ItemStack, private val amount: Int = 1) { | ||
val materialData: ItemStack | ||
get() = itemStack.clone().apply { this.amount = amount } | ||
} | ||
} |
This file contains 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
61 changes: 61 additions & 0 deletions
61
src/main/kotlin/gg/flyte/twilight/data/sql/AsyncSQLWrapper.kt
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
package gg.flyte.twilight.data.sql | ||
|
||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.async | ||
import kotlinx.coroutines.coroutineScope | ||
import java.sql.Connection | ||
import java.sql.DriverManager | ||
import java.sql.PreparedStatement | ||
import java.sql.Statement | ||
|
||
class AsyncSQLWrapper(private val url: String, private val user: String, private val password: String) { | ||
|
||
private var connection: Connection? = null | ||
|
||
suspend fun connect() { | ||
coroutineScope { | ||
connection = DriverManager.getConnection(url, user, password) | ||
} | ||
} | ||
|
||
suspend fun executeQuery(query: String): Results? { | ||
return coroutineScope { | ||
val resultSet = async(Dispatchers.IO) { | ||
val statement: Statement? = connection?.createStatement() | ||
statement?.executeQuery(query) | ||
} | ||
val rs = resultSet.await() | ||
rs?.toResults() | ||
} | ||
} | ||
|
||
suspend fun execute(sql: String): Boolean { | ||
return coroutineScope { | ||
var result = false | ||
try { | ||
val statement: Statement? = connection?.createStatement() | ||
statement?.execute(sql) | ||
result = true | ||
} catch (e: Exception) { | ||
e.printStackTrace() | ||
} | ||
result | ||
} | ||
} | ||
|
||
suspend fun executeUpdate(query: String): Int { | ||
return coroutineScope { | ||
val rowsAffected = async(Dispatchers.IO) { | ||
val statement: PreparedStatement? = connection?.prepareStatement(query) | ||
statement?.executeUpdate() ?: 0 | ||
} | ||
rowsAffected.await() | ||
} | ||
} | ||
|
||
suspend fun close() { | ||
coroutineScope { | ||
connection?.close() | ||
} | ||
} | ||
} |
Oops, something went wrong.