Compare commits
3 commits
56930a839d
...
f9f538589f
Author | SHA1 | Date | |
---|---|---|---|
f9f538589f | |||
3a9074b80e | |||
ffbbb2430f |
6 changed files with 182 additions and 71 deletions
|
@ -1,5 +1,6 @@
|
|||
package net.pietru.cookie_utils.scripting;
|
||||
|
||||
import com.badlogic.gdx.graphics.Color;
|
||||
import net.pietru.cookie_utils.scripting.tokens.*;
|
||||
|
||||
import java.io.File;
|
||||
|
@ -10,6 +11,8 @@ import java.util.Scanner;
|
|||
|
||||
public class BaseScript {
|
||||
public static HashMap<String, BaseToken> tokens = new HashMap<>();
|
||||
public static HashMap<String, Color> colors = new HashMap<>();
|
||||
public static final ExecutionContext dummy = new ExecutionContext(true);
|
||||
|
||||
|
||||
|
||||
|
@ -23,6 +26,42 @@ public class BaseScript {
|
|||
tokens.put("player", new TokenPlayer());
|
||||
for (String key : TokenMath.valid_tokens)
|
||||
tokens.put(key, new TokenMath());
|
||||
|
||||
tokens.put("message", new TokenMessage());
|
||||
|
||||
|
||||
colors.put("light_gray", Color.LIGHT_GRAY);
|
||||
colors.put("gray", Color.GRAY);
|
||||
colors.put("dark_gray", Color.DARK_GRAY);
|
||||
colors.put("black", Color.BLACK);
|
||||
colors.put("blue", Color.BLUE);
|
||||
colors.put("navy", Color.NAVY);
|
||||
colors.put("royal", Color.ROYAL);
|
||||
colors.put("slate", Color.SLATE);
|
||||
colors.put("sky", Color.SKY);
|
||||
colors.put("cyan", Color.CYAN);
|
||||
colors.put("teal", Color.TEAL);
|
||||
colors.put("green", Color.GREEN);
|
||||
colors.put("chartreuse", Color.CHARTREUSE);
|
||||
colors.put("lime", Color.LIME);
|
||||
colors.put("forest", Color.FOREST);
|
||||
colors.put("olive", Color.OLIVE);
|
||||
colors.put("yellow", Color.YELLOW);
|
||||
colors.put("gold", Color.GOLD);
|
||||
colors.put("goldenrod", Color.GOLDENROD);
|
||||
colors.put("orange", Color.ORANGE);
|
||||
colors.put("brown", Color.BROWN);
|
||||
colors.put("tan", Color.TAN);
|
||||
colors.put("firebrick", Color.FIREBRICK);
|
||||
colors.put("red", Color.RED);
|
||||
colors.put("scarlet", Color.SCARLET);
|
||||
colors.put("coral", Color.CORAL);
|
||||
colors.put("salmon", Color.SALMON);
|
||||
colors.put("pink", Color.PINK);
|
||||
colors.put("magenta", Color.MAGENTA);
|
||||
colors.put("purple", Color.PURPLE);
|
||||
colors.put("violet", Color.VIOLET);
|
||||
colors.put("maroon", Color.MAROON);
|
||||
}
|
||||
|
||||
|
||||
|
@ -127,24 +166,30 @@ public class BaseScript {
|
|||
public static ArrayList<String> get_string(String[] tokens, int step){
|
||||
ArrayList<String> text = new ArrayList<>();
|
||||
if (tokens[step].startsWith("\"")) {
|
||||
text.add(tokens[step]);
|
||||
for (int i = step + 1; i < tokens.length; i++) {
|
||||
if (tokens[step].endsWith("\""))
|
||||
text.add(tokens[step].substring(1,tokens[step].length()-1));
|
||||
else
|
||||
text.add(tokens[step].substring(1));
|
||||
for (int i = step+1; i < tokens.length; i++) {
|
||||
if (tokens[i].endsWith("\"")){
|
||||
text.add(tokens[i].substring(0,tokens[i].length()-1));
|
||||
break;
|
||||
} else {
|
||||
text.add(tokens[step]);
|
||||
text.add(tokens[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (tokens[step].startsWith("'")) {
|
||||
text.add(tokens[step].substring(1));
|
||||
for (int i = step + 1; i < tokens.length; i++) {
|
||||
if (tokens[step].endsWith("'"))
|
||||
text.add(tokens[step].substring(1,tokens[step].length()-1));
|
||||
else
|
||||
text.add(tokens[step].substring(1));
|
||||
for (int i = step+1; i < tokens.length; i++) {
|
||||
if (tokens[i].endsWith("'")) {
|
||||
text.add(tokens[i].substring(0,tokens[i].length()-1));
|
||||
break;
|
||||
} else {
|
||||
text.add(tokens[step]);
|
||||
text.add(tokens[i]);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
|
@ -154,20 +199,33 @@ public class BaseScript {
|
|||
}
|
||||
|
||||
public static String get_if_variable(String token,ExecutionContext context){
|
||||
if (token.startsWith("#"))
|
||||
if (token.startsWith("#") && !context.dummy)
|
||||
return String.valueOf(context.get_value(token.substring(1)));
|
||||
return token;
|
||||
}
|
||||
|
||||
public static HashMap<String,String> get_optional_args(String[] tokens, int step, List<String> valid_args){
|
||||
return get_optional_args(tokens, step, valid_args , dummy);
|
||||
}
|
||||
|
||||
public static HashMap<String,String> get_optional_args(String[] tokens, int step, List<String> valid_args, ExecutionContext context){
|
||||
HashMap<String,String> optional_args = new HashMap<>();
|
||||
|
||||
for (int i = step; i < tokens.length-1; i+=2) {
|
||||
for (int i = step; i < tokens.length-1;) {
|
||||
if (!valid_args.contains(tokens[i])){
|
||||
break;
|
||||
}
|
||||
optional_args.put(tokens[i],tokens[i+1]);
|
||||
ArrayList<String> val = get_string(tokens,i+1);
|
||||
optional_args.put(tokens[i],get_if_variable(String.join(" ",val),context));
|
||||
i+=(1+val.size());
|
||||
}
|
||||
return optional_args;
|
||||
}
|
||||
|
||||
public static int get_dynamic_tokens(HashMap<String,String> args){
|
||||
int skip = args.size();
|
||||
for (String s : args.values())
|
||||
skip += s.split(" ").length;
|
||||
return skip;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,6 +11,13 @@ import java.util.Map;
|
|||
public class ExecutionContext {
|
||||
public BaseScript script;
|
||||
public Map<String, Object> variable = new HashMap<>();
|
||||
public final boolean dummy;
|
||||
|
||||
public ExecutionContext(){this.dummy=false;}
|
||||
|
||||
public ExecutionContext(boolean dummy){
|
||||
this.dummy=dummy;
|
||||
}
|
||||
|
||||
public void set_value(String key, Object value){
|
||||
variable.remove(key);
|
||||
|
@ -28,6 +35,8 @@ public class ExecutionContext {
|
|||
|
||||
public void store_player_data(Account account){
|
||||
variable.put("event_player_id", account.getUniqueId());
|
||||
variable.put("event_player_username", account.getUsername());
|
||||
variable.put("event_player_displayname", account.getDisplayName());
|
||||
Player player = account.getPlayer();
|
||||
Vector3 pos = player.getPosition();
|
||||
variable.put("event_player_pos_x", pos.x);
|
||||
|
|
|
@ -0,0 +1,87 @@
|
|||
package net.pietru.cookie_utils.scripting.tokens;
|
||||
|
||||
import finalforeach.cosmicreach.GameSingletons;
|
||||
import finalforeach.cosmicreach.entities.player.Player;
|
||||
import finalforeach.cosmicreach.networking.packets.MessagePacket;
|
||||
import finalforeach.cosmicreach.networking.packets.ParticleSystemPacket;
|
||||
import finalforeach.cosmicreach.networking.server.ServerSingletons;
|
||||
import finalforeach.cosmicreach.world.Zone;
|
||||
import net.pietru.cookie_utils.scripting.BaseScript;
|
||||
import net.pietru.cookie_utils.scripting.ExecutionContext;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static net.pietru.cookie_utils.utils.player_utils.parseAsPlayer;
|
||||
|
||||
public class TokenMessage implements BaseToken {
|
||||
|
||||
public static final List<String> optional_args = List.of(
|
||||
"player",
|
||||
"zone"
|
||||
);
|
||||
|
||||
@Override
|
||||
public String get_token_checker_id() {
|
||||
return "token_message";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canHandle(String[] tokens, int step) {
|
||||
String token = tokens[step];
|
||||
return token.equals("message") && tokens.length>=step+next_token(tokens,step);
|
||||
}
|
||||
|
||||
/**
|
||||
* token_name
|
||||
* message
|
||||
* */
|
||||
@Override
|
||||
public int next_token(String[] tokens, int step) {
|
||||
ArrayList<String> msg_arr = BaseScript.get_string(tokens, step+1);
|
||||
return 1+msg_arr.size()+BaseScript.get_dynamic_tokens(BaseScript.get_optional_args(
|
||||
tokens,
|
||||
step+1+msg_arr.size(),
|
||||
optional_args
|
||||
));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean handle(String[] tokens, int step, ExecutionContext executionContext) {
|
||||
String token = tokens[step];
|
||||
if (!token.equals("message"))
|
||||
return false;
|
||||
|
||||
ArrayList<String> msg_arr = BaseScript.get_string(tokens, step+1);
|
||||
String msg = BaseScript.get_if_variable(String.join(" ", msg_arr),executionContext);
|
||||
|
||||
Map<String,String> args = BaseScript.get_optional_args(
|
||||
tokens,
|
||||
step+1+msg_arr.size(),
|
||||
optional_args,
|
||||
executionContext
|
||||
);
|
||||
|
||||
MessagePacket packet = new MessagePacket(msg);
|
||||
if (args.containsKey("player")) {
|
||||
Player player = parseAsPlayer(BaseScript.get_if_variable(args.get("player"), executionContext));
|
||||
if (player == null) {
|
||||
BaseScript.log_token_error(this, "Failed to parse player.");
|
||||
return false;
|
||||
}
|
||||
packet.setupAndSend(ServerSingletons.getConnection(player));
|
||||
} else {
|
||||
if (args.containsKey("zone")) {
|
||||
String zoneId = BaseScript.get_if_variable(args.get("zone"), executionContext);
|
||||
Zone zone = GameSingletons.world.getZoneIfExists(zoneId);
|
||||
if (zone != null)
|
||||
ServerSingletons.SERVER.broadcast(zone, packet);
|
||||
}
|
||||
else
|
||||
for (Zone wz : GameSingletons.world.getZones())
|
||||
ServerSingletons.SERVER.broadcast(wz, packet);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
|
@ -40,11 +40,7 @@ public class TokenParticle implements BaseToken {
|
|||
@Override
|
||||
public boolean canHandle(String[] tokens, int step) {
|
||||
String token = tokens[step];
|
||||
return token.equals("particle") && tokens.length>=step+5+BaseScript.get_optional_args(
|
||||
tokens,
|
||||
step+5,
|
||||
optional_args
|
||||
).size()*2;
|
||||
return token.equals("particle") && tokens.length>=step+next_token(tokens,step);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -56,11 +52,11 @@ public class TokenParticle implements BaseToken {
|
|||
* */
|
||||
@Override
|
||||
public int next_token(String[] tokens, int step) {
|
||||
return 5+BaseScript.get_optional_args(
|
||||
return 5+BaseScript.get_dynamic_tokens(BaseScript.get_optional_args(
|
||||
tokens,
|
||||
step+5,
|
||||
optional_args
|
||||
).size()*2;
|
||||
));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -95,48 +91,16 @@ public class TokenParticle implements BaseToken {
|
|||
Map<String,String> args = BaseScript.get_optional_args(
|
||||
tokens,
|
||||
step+5,
|
||||
optional_args
|
||||
optional_args,
|
||||
executionContext
|
||||
);
|
||||
GameParticleSystem system = GameParticleSystem.laserParticleSystem.copy();
|
||||
|
||||
String color_key = args.getOrDefault("color","SKY");
|
||||
Color c;
|
||||
switch (color_key.toLowerCase()){
|
||||
case "light_gray" -> c = Color.LIGHT_GRAY;
|
||||
case "gray" -> c = Color.GRAY;
|
||||
case "dark_gray" -> c = Color.DARK_GRAY;
|
||||
case "black" -> c = Color.BLACK;
|
||||
case "blue" -> c = Color.BLUE;
|
||||
case "navy" -> c = Color.NAVY;
|
||||
case "royal" -> c = Color.ROYAL;
|
||||
case "slate" -> c = Color.SLATE;
|
||||
case "sky" -> c = Color.SKY;
|
||||
case "cyan" -> c = Color.CYAN;
|
||||
case "teal" -> c = Color.TEAL;
|
||||
case "green" -> c = Color.GREEN;
|
||||
case "chartreuse" -> c = Color.CHARTREUSE;
|
||||
case "lime" -> c = Color.LIME;
|
||||
case "forest" -> c = Color.FOREST;
|
||||
case "olive" -> c = Color.OLIVE;
|
||||
case "yellow" -> c = Color.YELLOW;
|
||||
case "gold" -> c = Color.GOLD;
|
||||
case "goldenrod" -> c = Color.GOLDENROD;
|
||||
case "orange" -> c = Color.ORANGE;
|
||||
case "brown" -> c = Color.BROWN;
|
||||
case "tan" -> c = Color.TAN;
|
||||
case "firebrick" -> c = Color.FIREBRICK;
|
||||
case "red" -> c = Color.RED;
|
||||
case "scarlet" -> c = Color.SCARLET;
|
||||
case "coral" -> c = Color.CORAL;
|
||||
case "salmon" -> c = Color.SALMON;
|
||||
case "pink" -> c = Color.PINK;
|
||||
case "magenta" -> c = Color.MAGENTA;
|
||||
case "purple" -> c = Color.PURPLE;
|
||||
case "violet" -> c = Color.VIOLET;
|
||||
case "maroon" -> c = Color.MAROON;
|
||||
default -> c = Color.LIGHT_GRAY;
|
||||
}
|
||||
if (color_key.startsWith("#"))
|
||||
Color c = new Color();
|
||||
if (BaseScript.colors.containsKey(color_key))
|
||||
c=BaseScript.colors.get(color_key);
|
||||
else
|
||||
Color.valueOf(color_key,c);
|
||||
system.startColor.set(c);
|
||||
|
||||
|
|
|
@ -1,20 +1,11 @@
|
|||
package net.pietru.cookie_utils.scripting.tokens;
|
||||
|
||||
import com.badlogic.gdx.graphics.Color;
|
||||
import finalforeach.cosmicreach.GameSingletons;
|
||||
import finalforeach.cosmicreach.entities.player.Player;
|
||||
import finalforeach.cosmicreach.networking.packets.ParticleSystemPacket;
|
||||
import finalforeach.cosmicreach.networking.packets.entities.MaxHPEntityPacket;
|
||||
import finalforeach.cosmicreach.networking.packets.entities.PlayerPositionPacket;
|
||||
import finalforeach.cosmicreach.networking.server.ServerIdentity;
|
||||
import finalforeach.cosmicreach.networking.server.ServerSingletons;
|
||||
import finalforeach.cosmicreach.particles.GameParticleSystem;
|
||||
import finalforeach.cosmicreach.particles.IParticleComponent;
|
||||
import finalforeach.cosmicreach.particles.spawn.GameParticleSpawnSphere;
|
||||
import finalforeach.cosmicreach.world.Zone;
|
||||
import net.pietru.cookie_utils.scripting.BaseScript;
|
||||
import net.pietru.cookie_utils.scripting.ExecutionContext;
|
||||
import net.pietru.cookie_utils.utils.ParticleUtil;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
@ -39,11 +30,7 @@ public class TokenPlayer implements BaseToken {
|
|||
@Override
|
||||
public boolean canHandle(String[] tokens, int step) {
|
||||
String token = tokens[step];
|
||||
return token.equals("player") && tokens.length>=step+2+BaseScript.get_optional_args(
|
||||
tokens,
|
||||
step,
|
||||
optional_args
|
||||
).size()*2;
|
||||
return token.equals("player") && tokens.length>=step+next_token(tokens,step);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -52,11 +39,11 @@ public class TokenPlayer implements BaseToken {
|
|||
* */
|
||||
@Override
|
||||
public int next_token(String[] tokens, int step) {
|
||||
return 2+BaseScript.get_optional_args(
|
||||
return 2+BaseScript.get_dynamic_tokens(BaseScript.get_optional_args(
|
||||
tokens,
|
||||
step+2,
|
||||
optional_args
|
||||
).size()*2;
|
||||
));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -76,7 +63,8 @@ public class TokenPlayer implements BaseToken {
|
|||
Map<String,String> args = BaseScript.get_optional_args(
|
||||
tokens,
|
||||
step+2,
|
||||
optional_args
|
||||
optional_args,
|
||||
executionContext
|
||||
);
|
||||
if (args.containsKey("velocity_x"))
|
||||
try {
|
||||
|
|
|
@ -18,6 +18,11 @@ public class TokenSet implements BaseToken {
|
|||
return token.equals("set") && tokens.length>=step+3;
|
||||
}
|
||||
|
||||
/**
|
||||
* token_name
|
||||
* key
|
||||
* value (x)
|
||||
* */
|
||||
@Override
|
||||
public int next_token(String[] tokens, int step) {
|
||||
return 2+BaseScript.get_string(tokens, step+2).size();
|
||||
|
|
Loading…
Reference in a new issue