Skip to content

Commit f24b390

Browse files
committed
document Lua libraries
1 parent bf4b775 commit f24b390

File tree

4 files changed

+71
-43
lines changed

4 files changed

+71
-43
lines changed

_includes/head.html

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@
2020

2121
aside .simplebar-content-wrapper {
2222

23-
li:first-child, li:nth-last-child(2) {
23+
li:first-child, li:nth-last-child(3) {
2424
margin-top: 0.75em;
2525
padding-top: 0.75em;
2626
border-top: 1px solid color-mix(in srgb, gray 12.5%, transparent);
2727
}
2828

29-
/* Currently, the first entry is 'Home' and last one is 'Reference' */
3029
li:first-child, li:last-child {
3130
margin-bottom: 0.75em;
3231
padding-bottom: 0.75em;

community-libraries.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
icon: people
3+
order: 93
4+
---
5+
6+
# Community Libraries
7+
In this section you can find community-made libraries that were written specifically for use with LuaLink. Want to see your project here? **[Submit a PR](https://github.com/LuaLink/docs/pulls)** and we'll consider adding it to the list.
8+
9+
Nothing to see here yet.

examples.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ icon: play
33
order: 94
44
---
55
# Examples
6-
In this section you can find some example scripts written with LuaLink. Want to see your script here? **[Submit a PR](https://github.com/LuaLink/docs/pulls)** and we'll add it to the list.
6+
In this section you can find some example scripts written with LuaLink. Want to see your script here? **[Submit a PR](https://github.com/LuaLink/docs/pulls)** and we'll consider adding it to the list.
77

88
<br />
99

libraries.md

Lines changed: 60 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4,69 +4,86 @@ order: 96
44
---
55

66
# Libraries
7-
LuaLink provides a way to load external **Java** libraries for use in your scripts, as well as to define and use external **Lua** libraries.
7+
LuaLink provides a way to load external **Java** libraries for use in your scripts, as well as to define and use embedded **Lua** libraries.
88

99
<br />
1010

11-
<!--
1211
## Lua Libraries
13-
External Lua libraries can be added by creating or copying files to the `/plugins/LuaLink/libs/` folder.
12+
Lua libraries are almost identical to scripts, but there are a few differences.
13+
- Each Lua library should be placed in a separate folder in the `/plugins/LuaLink/libs` directory.
14+
<sup>For example `/plugins/LuaLink/libs/example/main.lua` will be available as `example` or `example.main`.</sup>
15+
- Entry point of a Lua library should be always be a `main.lua` file.
16+
<sup>Different entry points are still possible however, and can be imported using a `.` denoted path, for example: `require("my_lib.my_entry_point")`</sup>
17+
- Lua Libraries are loaded on demand, using the `require` keyword.
18+
<sup>For example, if we want to use the `example` library in our script, we can import it using the `require("example")`.</sup>
1419

15-
==- Example Lua Library
16-
```lua **/plugins/LuaLink/libs/example_library/main.lua**
17-
Counter = {}
20+
<br />
21+
22+
### Definition
23+
==- Example Lua Library &ensp;&ensp; Counter
24+
```lua /plugins/LuaLink/libs/counter/main.lua
25+
local Counter = {}
1826

1927
-- Creates a new Counter instance.
2028
function Counter.new()
21-
local self = setmetatable({}, {__index = Counter})
22-
self.count = 0
23-
return self
24-
end
25-
26-
-- Increments the counter by one.
27-
function Counter:increment()
28-
self.count = self.count + 1
29+
return {
30+
count = 0,
31+
32+
-- Increments this Counter by 1.
33+
increment = function(self)
34+
self.count = self.count + 1
35+
end,
36+
37+
-- Decrements this Counter by 1.
38+
decrement = function(self)
39+
self.count = self.count - 1
40+
end,
41+
42+
-- Returns the current Counter value.
43+
get = function(self)
44+
return self.count
45+
end
46+
}
2947
end
3048

31-
-- Decrements the counter by one.
32-
function Counter:decrement()
33-
self.count = self.count - 1
34-
end
35-
36-
-- Returns the current counter value.
37-
function Counter:get()
38-
return self.count
39-
end
49+
return Counter
4050
```
4151
==-
52+
This is a simple `Counter` class that can be used to increment and decrement a counter.
4253

43-
This is an example of a Counter class that allows you to increment, decrement, and get the current value of a counter.
54+
<br />
4455

45-
Now, if you want to use this class in your script, you can import it using the `require` keyword.
56+
### Usage
4657

58+
Now, if we want to use the `Counter` class in our script, we can import it using the `require` keyword.
4759
```lua /plugins/LuaLink/scripts/example_script/main.lua
48-
local Counter = require("example_library")
49-
50-
script:onLoad(function()
51-
-- Creating a new instance of the Counter class.
52-
local counter = Counter:new()
53-
-- Incrementing the counter three times.
54-
counter:increment()
55-
counter:increment()
56-
counter:increment()
57-
-- Printing current value of the counter to the console.
58-
script.logger:info(counter:get() .. " is the current value of the counter.")
59-
end)
60+
local Counter = require("counter")
61+
62+
-- Creating a new instance of the Counter class.
63+
local counter = Counter:new()
64+
-- Incrementing the counter three times.
65+
counter:increment()
66+
counter:increment()
67+
counter:increment()
68+
-- Decrementing the counter once.
69+
counter:decrement()
70+
-- Printing current value of the counter to the console.
71+
print(counter:get() .. " is the current value.")
72+
```
73+
```log Console Output
74+
[00:00:00 INFO]: [LuaLink/example_script] 2 is the current value.
6075
```
6176

6277
<br />
63-
64-
-->
78+
<hr>
79+
<br />
6580

6681
## Java Libraries
6782
External Java/Kotlin libraries can be added by configuring the `/plugins/LuaLink/libraries.json` file. Dependencies will be downloaded and exposed to the scripting runtime after server restart.
6883

69-
### Configuration Example
84+
<br />
85+
86+
### Configuration
7087
```json5 /plugins/LuaLink/libraries.json
7188
{
7289
// Repositories to be used for dependency resolution.
@@ -90,6 +107,9 @@ External Java/Kotlin libraries can be added by configuring the `/plugins/LuaLink
90107

91108
In this example, we are adding **[PaperLib](https://github.com/PaperMC/PaperLib)** library of version **1.0.7** from **[PaperMC](https://repo.papermc.io/repository/maven-public/)** repository. You can also see how and authenticate with a private repository using credentials, which might be essential when working with closed-source projects or **[GitHub Packages](https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-apache-maven-registry)**.
92109

110+
<br />
111+
112+
### Usage
93113
After restarting the server, we should be able to import and access any class that belongs to specified library(-ies).
94114

95115
```lua /plugins/LuaLink/scripts/example_script/main.lua

0 commit comments

Comments
 (0)