Skip to content

Commit fce6489

Browse files
authored
Update showscript3.md
1 parent bf1caf8 commit fce6489

File tree

1 file changed

+150
-1
lines changed

1 file changed

+150
-1
lines changed

docs/showscript3.md

Lines changed: 150 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,120 @@ ticks(0) {
306306

307307
### Sharing data between shows: `export` and `load`
308308

309-
// TODO: add
309+
ShowScript 3 provides a powerful mechanism to share data and functions between shows using `export` and `load`. This allows you to define reusable components in one show and use them in another, promoting code reuse and modularity. Let's go through both concepts with examples.
310+
311+
#### Exporting and Loading Data
312+
313+
Imagine you have a scenario where multiple shows need to use the same piece of data, such as the coordinates of a specific location. Instead of defining the coordinates in each show, you can define them once and export them.
314+
315+
```groovy
316+
// locationData.groovy
317+
def mainLocation = [world: "world", x: 100, y: 64, z: 100]
318+
319+
export("mainLocation", mainLocation)
320+
321+
ticks(0) {
322+
cmd {
323+
...
324+
}
325+
}
326+
```
327+
328+
In this example, `mainLocation` is exported with the name "mainLocation".
329+
330+
You can then load this data in another show:
331+
332+
```groovy
333+
// useLocationData.groovy
334+
def importedData = load("locationData")
335+
336+
ticks(0) {
337+
cmd {
338+
def loc = importedData.mainLocation
339+
"teleport @a ${loc.x} ${loc.y} ${loc.z}"
340+
}
341+
}
342+
```
343+
344+
Running `/show start useLocationData` will teleport all players to the coordinates defined in `mainLocation`.
345+
346+
#### Exporting and Loading Functions
347+
348+
Similarly, you can export functions from one show and use them in another, allowing you to create reusable components.
349+
350+
```groovy
351+
// greetingFunctions.groovy
352+
def sayHello = { name ->
353+
return "hello ${name}"
354+
}
355+
356+
export("sayHello", sayHello)
357+
358+
ticks(0) {
359+
cmd {
360+
sayHello("world")
361+
}
362+
}
363+
```
364+
365+
In this example, the function `sayHello` is exported with the name "sayHello". When this show is run, it will broadcast "hello world".
366+
367+
You can then load and use this function in another show:
368+
369+
```groovy
370+
// useGreetingFunction.groovy
371+
def importedFunctions = load("greetingFunctions")
372+
373+
ticks(0) {
374+
cmd {
375+
importedFunctions.sayHello("tyler")
376+
}
377+
}
378+
```
379+
380+
Running `/show start useGreetingFunction` will broadcast "hello tyler".
381+
382+
#### Combining Data and Functions
383+
384+
You can combine both concepts to create even more powerful and reusable components. For example, you can have a show that exports both data and functions:
385+
386+
```groovy
387+
// dataAndFunctions.groovy
388+
def mainLocation = [world: "world", x: 100, y: 64, z: 100]
389+
def sayHello = { name ->
390+
return "hello ${name}"
391+
}
392+
393+
export("mainLocation", mainLocation)
394+
export("sayHello", sayHello)
395+
396+
ticks(0) {
397+
cmd {
398+
sayHello("world")
399+
}
400+
}
401+
```
402+
403+
In this example, both `mainLocation` and `sayHello` are exported. You can then load and use both in another show:
404+
405+
```groovy
406+
// useDataAndFunctions.groovy
407+
def imported = load("dataAndFunctions")
408+
409+
ticks(0) {
410+
def loc = imported.mainLocation
411+
cmd {
412+
"teleport @a ${loc.x} ${loc.y} ${loc.z}"
413+
}
414+
cmd {
415+
imported.sayHello("everyone")
416+
}
417+
}
418+
```
419+
420+
Running `/show start useDataAndFunctions` will teleport all players to the coordinates defined in `mainLocation` and broadcast "hello everyone".
421+
422+
By using `export` and `load`, you can create modular and reusable code components in ShowScript, making it easier to maintain and extend your shows.
310423

311424
### Accessing server info
312425

@@ -385,3 +498,39 @@ Get the mathematical cosine of an angle
385498

386499
#### `tan(double angle)` (returns double)
387500
Get the mathematical tangent of an angle
501+
502+
503+
### Accessing information from ANY plugin
504+
505+
The Groovy interpreter that runs ShowScript shows has access to the entire Java classpath. You can `import` any Java class you want for use in your shows, even classes from plugins you have installed.
506+
507+
Here’s an example of how to use TrainCarts to get information about a specific train:
508+
509+
```groovy
510+
// Import the necessary TrainCarts classes
511+
import com.bergerkiller.bukkit.tc.controller.MinecartGroup
512+
import com.bergerkiller.bukkit.tc.controller.MinecartMember
513+
import com.bergerkiller.bukkit.tc.properties.TrainPropertiesStore
514+
515+
ticks(0) {
516+
// Get the train by its name
517+
def trainName = "MyTrain"
518+
def train = TrainPropertiesStore.get(trainName).getHolder()
519+
520+
if (train != null) {
521+
// Get the first member of the train and its location
522+
def firstMember = train.get(0)
523+
def location = firstMember.getEntity().getLocation()
524+
525+
cmd {
526+
"broadcast The first member of train ${trainName} is at ${location.getX()}, ${location.getY()}, ${location.getZ()}"
527+
}
528+
} else {
529+
cmd {
530+
"broadcast Train ${trainName} not found!"
531+
}
532+
}
533+
}
534+
```
535+
536+
This allows you to use ShowScript to take practically any action on your server

0 commit comments

Comments
 (0)