Skip to content

Commit 51a3f49

Browse files
committed
Cleanup and crash fix
1 parent 660ba94 commit 51a3f49

3 files changed

Lines changed: 21 additions & 112 deletions

File tree

README.md

Lines changed: 2 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,3 @@
1-
# Lambda Plugin SDK
1+
# StockTicker
22

3-
This project in an example to show how a proper plugin for [Lambda Client](https://github.com/lambda-client/lambda) is set up.
4-
The advantage of plugins for a utility mod is that they allow the user to decide what features their personalized client will have. Plugins work in plug and play manner, and can be downloaded and activated inside the ingame menu without reloading the client as long as no mixins are used for the plugin.
5-
If you are scared about the Kotlin in this project be aware that Kotlin is a wrapper language for Java. That means that plugins can also be natively written in Java.
6-
7-
## Setup
8-
9-
To achieve coding building and publishing your own plugin most of the following steps are required.
10-
11-
### Fork
12-
This is a template repository and can be used as a base to create a custom plugin. Press the `Use as template` button on GitHub to automatically create a linked fork to this repository.
13-
14-
### Clone Repository
15-
16-
Clone the repository to your local machine. Use the link of either your fork or the main repository.
17-
```
18-
git clone https://github.com/lambda-client/plugin-sdk
19-
```
20-
21-
### Setup IDE
22-
23-
In this guide we will use [IntelliJ IDEA](https://www.jetbrains.com/idea/) as IDE.
24-
1. Open the project from `File > Open...`
25-
2. Let the IDE collect dependencies and index the code.
26-
3. Goto `File > Project Structure... > SDKs` and make sure an SDK for Java 8 is installed and selected, if not download
27-
it [here](https://adoptopenjdk.net/index.html?variant=openjdk8&jvmVariant=hotspot)
28-
4. Run the `genIntellijRuns` Gradle task, or run `./gradlew genIntellijRuns`
29-
30-
### Configure Gradle
31-
32-
Test if the environment is set up correctly by building the plugin jar using the Gradle tab on the right side of the IDE.
33-
1. Go to `PluginExample > Tasks > build > jar` in the Gradle tab and run the script
34-
2. IntelliJ will create a new directory called `build`. The final built jar will be in `build/libs`
35-
36-
### Config
37-
38-
Configure the metadata of your plugin in `plugin_info.json`.
39-
The flag `main_class` must contain the target main class `Plugin` in this case it is `PluginExample.kt`
40-
41-
### Plugin
42-
43-
The plugin main class will act as a register for the functions a plugin can provide.
44-
For example when a new module class is created you have to add this to the `onLoad()` function of the plugin class.
45-
```
46-
modules.add(ModuleExample)
47-
```
48-
Every service is required to be added to the main class in order to index the contents.
49-
50-
### PluginModule
51-
52-
A module represents a utility module inside the game.
53-
The `PluginModule` class acts as a wrapper for `Module` class. For many examples on how a module can work check out the [native modules](https://github.com/lambda-client/lambda/tree/master/src/main/kotlin/com/lambda/client/module/modules) of lambda, or the given example in this project.
54-
The difference from the native `Module` class is that each component of a plugin requires a reference to the main `Plugin` class.
55-
```
56-
pluginMain = PluginExample
57-
```
58-
Every PluginModule class will need to be registered to the main plugin class
59-
60-
### ClientCommand
61-
62-
Plugins use the same class as the native client for registering commands. Feel free to check out the [commands of Lambda Client](https://github.com/lambda-client/lambda/tree/master/src/main/kotlin/com/lambda/client/command/commands) as a reference.
63-
64-
### PluginLabelHud
65-
66-
A LabelHud is used to display information in the player GUI.
67-
The `PluginLabelHud` class acts as a wrapper for `LabelHud` class. For many examples on how a hud can work check out the [native hud elements](https://github.com/lambda-client/lambda/tree/master/src/main/kotlin/com/lambda/client/gui/hudgui/elements) of lambda, or the given example in this project.
68-
The difference to the native `LabelHud` class is that a referral to the main plugin class is given in the object data.
69-
```
70-
pluginMain = PluginExample
71-
```
72-
Every `PluginLabelHud` class will need to be registered to the main `Plugin` class.
73-
74-
### Background Jobs
75-
76-
If coroutines are needed background jobs can be registered using
77-
```
78-
bgJobs.add(BackgroundJob)
79-
```
80-
81-
### Mixin
82-
Example coming soon. Plugin won't be able to hot reload anymore because mixins need to be triggered on client start.
83-
84-
### Build
85-
86-
1. Go to `PluginExample > Tasks > build > jar` in the Gradle tab and run the script
87-
2. IntelliJ will create a new directory called `build` the final built jar will be in `build/libs`
88-
3. Put the `ExamplePlugin-1.0.jar` into your `./minecraft/lambda/plugins` folder and run the game.
89-
90-
### Publish (coming soon)
91-
92-
Insert the link of your created fork into the plugin manager to load the plugin, or transfer your repository to official
93-
plugin [organization of Lambda](https://github.com/lambda-plugins/). After review, your plugin may get added to the native
94-
marketplace.
3+
Live updating stock price.

settings.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
rootProject.name = 'PluginExample'
1+
rootProject.name = 'StockTicker'

src/main/kotlin/StocksHud.kt

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import com.google.gson.Gson
2+
import com.lambda.client.LambdaMod
23
import com.lambda.client.util.TickTimer
34
import com.lambda.client.util.TimeUnit
45
import com.lambda.client.util.WebUtils
56
import com.lambda.client.util.text.MessageSendHelper
67
import com.lambda.client.event.SafeClientEvent
78
import com.lambda.client.plugin.api.PluginLabelHud
9+
import com.lambda.client.util.threads.defaultScope
10+
import kotlinx.coroutines.launch
811

912
internal object StocksHud: PluginLabelHud(
1013
name = "Stocks",
@@ -13,35 +16,32 @@ internal object StocksHud: PluginLabelHud(
1316
pluginMain = PluginStocks
1417
) {
1518
private var symbol by setting("Symbol", "TSLA")
16-
private val tickdelay by setting("Delay", 30, 20..120, 1)
19+
private val tickDelay by setting("Delay", 30, 20..120, 1)
1720
private var token by setting("Token", "Set your token with the command ;set Stocks Token (token)")
18-
private val ticktimer = TickTimer(TimeUnit.SECONDS)
21+
private val tickTimer = TickTimer(TimeUnit.SECONDS)
1922
private var url = "https://finnhub.io/api/v1/quote?symbol=$symbol&token=$token"
20-
private var stockData = StockData(0.0)
2123
private var price = 0.0
22-
private var sentwarning = false
24+
private var sentWarning = false
2325

2426
override fun SafeClientEvent.updateText() {
25-
if (sentwarning == false) {
27+
if (!sentWarning) {
2628
sendWarning()
2729
}
2830

29-
if (ticktimer.tick(tickdelay)) {
30-
updateStockData()
31+
if (tickTimer.tick(tickDelay) && !token.startsWith("Set")) {
32+
defaultScope.launch {
33+
try {
34+
url = "https://finnhub.io/api/v1/quote?symbol=${symbol.uppercase()}&token=$token"
35+
price = Gson().fromJson(WebUtils.getUrlContents(url), StockData::class.java).c
36+
} catch (e: Exception) {
37+
LambdaMod.LOG.error("Failed to connect to finnhub api", e)
38+
}
39+
}
3140
}
32-
displayText.add("Current Price of ${symbol.toUpperCase()} is", primaryColor)
41+
displayText.add("Current Price of ${symbol.uppercase()} is", primaryColor)
3342
displayText.add("$price", secondaryColor)
3443
}
3544

36-
private fun updateStockData() {
37-
if (token.length != 20) {
38-
39-
} else {
40-
url = "https://finnhub.io/api/v1/quote?symbol=${symbol.toUpperCase()}&token=$token"
41-
price = Gson().fromJson(WebUtils.getUrlContents(url), StockData::class.java).c
42-
}
43-
}
44-
4545

4646
private fun sendWarning() {
4747
MessageSendHelper.sendWarningMessage(
@@ -50,7 +50,7 @@ internal object StocksHud: PluginLabelHud(
5050
"Once you have gotten your api token, you can run this command: " +
5151
";set Stocks Token (paste token here)"
5252
)
53-
sentwarning = true
53+
sentWarning = true
5454
}
5555

5656
private class StockData(

0 commit comments

Comments
 (0)