Add RandomLib Signed-off-by: yoyosource <yoyosource@nidido.de>
Dieser Commit ist enthalten in:
Ursprung
5f9d3b1e5b
Commit
6bb9900646
@ -0,0 +1,95 @@
|
||||
/*
|
||||
* This file is a part of the SteamWar software.
|
||||
*
|
||||
* Copyright (C) 2023 SteamWar.de-Serverteam
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package de.steamwar.bausystem.features.script.lua.libs;
|
||||
|
||||
import de.steamwar.linkage.Linked;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.luaj.vm2.LuaError;
|
||||
import org.luaj.vm2.LuaTable;
|
||||
import org.luaj.vm2.LuaValue;
|
||||
import org.luaj.vm2.Varargs;
|
||||
import org.luaj.vm2.lib.VarArgFunction;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
@Linked
|
||||
public class RandomLib implements LuaLib {
|
||||
|
||||
private static Random random = new Random();
|
||||
|
||||
@Override
|
||||
public String name() {
|
||||
return "random";
|
||||
}
|
||||
|
||||
@Override
|
||||
public LuaTable get(Player player) {
|
||||
LuaTable randomLib = new LuaTable();
|
||||
randomLib.set("nextInt", new NextInt());
|
||||
randomLib.set("nextDouble", new NextDouble());
|
||||
randomLib.set("nextBool", new NextBool());
|
||||
return randomLib;
|
||||
}
|
||||
|
||||
private static class NextInt extends VarArgFunction {
|
||||
|
||||
@Override
|
||||
public Varargs invoke(Varargs args) {
|
||||
if (args.narg() == 0) {
|
||||
return LuaValue.valueOf(random.nextInt());
|
||||
}
|
||||
if (args.narg() == 1) {
|
||||
return LuaValue.valueOf(random.nextInt(args.checkint(1)));
|
||||
}
|
||||
if (args.narg() == 2) {
|
||||
return LuaValue.valueOf(random.nextInt(args.checkint(1), args.checkint(2)));
|
||||
}
|
||||
throw new LuaError("Invalid number of arguments for random.nextInt() zero, one or two expected got " + args.narg());
|
||||
}
|
||||
}
|
||||
|
||||
private static class NextDouble extends VarArgFunction {
|
||||
|
||||
@Override
|
||||
public Varargs invoke(Varargs args) {
|
||||
if (args.narg() == 0) {
|
||||
return LuaValue.valueOf(random.nextDouble());
|
||||
}
|
||||
if (args.narg() == 1) {
|
||||
return LuaValue.valueOf(random.nextDouble(args.checkdouble(1)));
|
||||
}
|
||||
if (args.narg() == 2) {
|
||||
return LuaValue.valueOf(random.nextDouble(args.checkdouble(1), args.checkdouble(2)));
|
||||
}
|
||||
throw new LuaError("Invalid number of arguments for random.nextDouble() zero, one or two expected got " + args.narg());
|
||||
}
|
||||
}
|
||||
|
||||
private static class NextBool extends VarArgFunction {
|
||||
|
||||
@Override
|
||||
public Varargs invoke(Varargs args) {
|
||||
if (args.narg() == 0) {
|
||||
return LuaValue.valueOf(random.nextBoolean());
|
||||
}
|
||||
throw new LuaError("Invalid number of arguments for random.nextBoolean() zero expected got " + args.narg());
|
||||
}
|
||||
}
|
||||
}
|
14
SCRIPT.md
14
SCRIPT.md
@ -52,6 +52,7 @@ Diese können auch undokumentierte Funktionen enthalten, die nicht in der Dokume
|
||||
|
||||
In den Scripten gibt es dazu noch folgende globale Variablen:
|
||||
- [`player`](#player)
|
||||
- [`random`](#random)
|
||||
- [`region`](#region)
|
||||
- [`server`](#server)
|
||||
- [`storage`](#storage)
|
||||
@ -88,6 +89,19 @@ Es gibt folgende Funktionen:
|
||||
| `item` | item(): String | Gibt den Itemtyp der Main-Hand zurück |
|
||||
| `offHandItem` | offHandItem(): String | Gibt den Itemtyp der Off-Hand zurück |
|
||||
|
||||
### random
|
||||
Das `random`-Modul stellt Funktionen zur Verfügung, die Zufallszahlen betreffen.
|
||||
Es gibt folgende Funktionen:
|
||||
|
||||
| Name | Signature | Beschreibung |
|
||||
|-------------|------------------------------------|-------------------------------------------------------------------------------|
|
||||
| nextInt | nextInt(): Int | Gibt eine Zufallszahl zurück zwischen Integer.MIN_VALUE und Integer.MAX_VALUE |
|
||||
| -"- | nextInt(Int): Int | Gibt eine Zufallszahl zurück zischen 0 und dem Argument |
|
||||
| -"- | nextInt(Int, Int): Int | Gibt eine Zufallszahl zurück zwischen dem ersten und zweiten Argument |
|
||||
| nextDouble | nextDouble(): Double | Gibt eine Zufallszahl zurück zwischen 0 und 1 |
|
||||
| -"- | nextDouble(Double): Double | Gibt eine Zufallszahl zurück zwischen 0 und dem Argument |
|
||||
| -"- | nextDouble(Double, Double): Double | Gibt eine Zufallszahl zurück zwischen ersten und zweiten Argument |
|
||||
| nextBoolean | nextBoolean(): Boolean | Gibt true oder false zurück |
|
||||
|
||||
### region
|
||||
Das `region`-Modul stellt Funktion zur Verfügung, die die Region des Spielers betreffen.
|
||||
|
In neuem Issue referenzieren
Einen Benutzer sperren