mirror of
https://github.com/Anuken/Mindustry.git
synced 2025-02-06 08:57:45 +07:00
Logic data
instruction (#9935)
* Logic `data` instruction * Rename to `clientdata` & reliable option * frog Co-authored-by: Anuken <arnukren@gmail.com> --------- Co-authored-by: Anuken <arnukren@gmail.com>
This commit is contained in:
parent
2e460bf483
commit
58dbc5104f
@ -117,6 +117,8 @@ public class NetServer implements ApplicationListener{
|
||||
private DataOutputStream dataStream = new DataOutputStream(syncStream);
|
||||
/** Packet handlers for custom types of messages. */
|
||||
private ObjectMap<String, Seq<Cons2<Player, String>>> customPacketHandlers = new ObjectMap<>();
|
||||
/** Packet handlers for logic client data */
|
||||
private ObjectMap<String, Seq<Cons2<Player, Object>>> logicClientDataHandlers = new ObjectMap<>();
|
||||
|
||||
public NetServer(){
|
||||
|
||||
@ -515,6 +517,10 @@ public class NetServer implements ApplicationListener{
|
||||
return customPacketHandlers.get(type, Seq::new);
|
||||
}
|
||||
|
||||
public void addLogicDataHandler(String type, Cons2<Player, Object> handler){
|
||||
logicClientDataHandlers.get(type, Seq::new).add(handler);
|
||||
}
|
||||
|
||||
public static void onDisconnect(Player player, String reason){
|
||||
//singleplayer multiplayer weirdness
|
||||
if(player.con == null){
|
||||
@ -583,6 +589,21 @@ public class NetServer implements ApplicationListener{
|
||||
serverPacketReliable(player, type, contents);
|
||||
}
|
||||
|
||||
@Remote(targets = Loc.client)
|
||||
public static void clientLogicDataReliable(Player player, String channel, Object value){
|
||||
Seq<Cons2<Player, Object>> handlers = netServer.logicClientDataHandlers.get(channel);
|
||||
if(handlers != null){
|
||||
for(Cons2<Player, Object> handler : handlers){
|
||||
handler.get(player, value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Remote(targets = Loc.client, unreliable = true)
|
||||
public static void clientLogicDataUnreliable(Player player, String channel, Object value){
|
||||
clientLogicDataReliable(player, channel, value);
|
||||
}
|
||||
|
||||
private static boolean invalid(float f){
|
||||
return Float.isInfinite(f) || Float.isNaN(f);
|
||||
}
|
||||
|
@ -203,6 +203,8 @@ public class Rules{
|
||||
public @Nullable PlanetParams planetBackground;
|
||||
/** Rules from this planet are applied. If it's {@code sun}, mixed tech is enabled. */
|
||||
public Planet planet = Planets.serpulo;
|
||||
/** If the `data` instruction is allowed for world processors */
|
||||
public boolean allowLogicData = false;
|
||||
|
||||
/** Copies this ruleset exactly. Not efficient at all, do not use often. */
|
||||
public Rules copy(){
|
||||
|
@ -41,6 +41,7 @@ public class LExecutor{
|
||||
maxDisplayBuffer = 1024,
|
||||
maxTextBuffer = 400;
|
||||
|
||||
|
||||
public LInstruction[] instructions = {};
|
||||
/** Non-constant variables used for network sync */
|
||||
public LVar[] vars = {};
|
||||
@ -1761,6 +1762,31 @@ public class LExecutor{
|
||||
}
|
||||
}
|
||||
|
||||
public static class ClientDataI implements LInstruction{
|
||||
public LVar channel, value, reliable;
|
||||
|
||||
public ClientDataI(LVar channel, LVar value, LVar reliable){
|
||||
this.channel = channel;
|
||||
this.value = value;
|
||||
this.reliable = reliable;
|
||||
}
|
||||
|
||||
public ClientDataI() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(LExecutor exec) {
|
||||
if(channel.obj() instanceof String c){
|
||||
Object v = value.isobj ? value.objval : value.numval;
|
||||
if(reliable.bool()){
|
||||
Call.clientLogicDataReliable(c, v);
|
||||
}else{
|
||||
Call.clientLogicDataUnreliable(c, v);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class GetFlagI implements LInstruction{
|
||||
public LVar result, flag;
|
||||
|
||||
|
@ -1911,6 +1911,42 @@ public class LStatements{
|
||||
}
|
||||
}
|
||||
|
||||
@RegisterStatement("clientdata")
|
||||
public static class ClientDataStatement extends LStatement{
|
||||
public String channel = "\"frog\"", value = "\"bar\"", reliable = "0";
|
||||
|
||||
@Override
|
||||
public void build(Table table){
|
||||
table.add("send ");
|
||||
fields(table, value, str -> value = str);
|
||||
table.add(" on ");
|
||||
fields(table, channel, str -> channel = str);
|
||||
table.add(", reliable ");
|
||||
fields(table, channel, str -> channel = str);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hidden(){
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean privileged(){
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public LInstruction build(LAssembler builder){
|
||||
if(!state.rules.allowLogicData) return null;
|
||||
return new ClientDataI(builder.var(channel), builder.var(value), builder.var(reliable));
|
||||
}
|
||||
|
||||
@Override
|
||||
public LCategory category(){
|
||||
return LCategory.world;
|
||||
}
|
||||
}
|
||||
|
||||
@RegisterStatement("getflag")
|
||||
public static class GetFlagStatement extends LStatement{
|
||||
public String result = "result", flag = "\"flag\"";
|
||||
|
Loading…
Reference in New Issue
Block a user