Added local method invocation annotation

This commit is contained in:
Anuken
2018-05-09 16:41:02 -07:00
parent ce2b73b737
commit 09bdfd7ac1
3 changed files with 23 additions and 15 deletions

View File

@@ -24,6 +24,7 @@ import static io.anuke.mindustry.Vars.playerGroup;
public class Invoke {
private static ObjectMap<Class, ObjectMap<String, Method>> methods = new ObjectMap<>();
private static ObjectMap<String, Class> classes = new ObjectMap<>();
private static ObjectMap<Method, Boolean> methodLocal = new ObjectMap<>();
/**
* Invokes a method remotely (on all clients) and locally.
@@ -34,7 +35,9 @@ public class Invoke {
public static void on(Class<?> type, String methodName, Object... args){
try {
Method method = getMethod(type, methodName);
method.invoke(null, args);
if(methodLocal.get(method)){
method.invoke(null, args);
}
InvokePacket packet = new InvokePacket();
packet.args = args;
packet.type = type;
@@ -55,10 +58,6 @@ public class Invoke {
on(NetEvents.class, methodName, args);
}
public static void eventRemote(String methodName, Object... args){
on(NetEvents.class, methodName, args);
}
//TODO refactor to serializer map!
static void writeObjects(ByteBuffer buffer, Object[] objects){
for(Object o : objects){
@@ -162,6 +161,7 @@ public class Invoke {
if(method.getDeclaredAnnotation(Remote.class) == null){
throw new RuntimeException("Attempt to invoke method '" + methodname + "', which is not Invokable!");
}
methodLocal.put(method, method.getDeclaredAnnotation(Local.class) != null);
map.put(methodname, method);
}
@@ -170,9 +170,12 @@ public class Invoke {
}
/**Marks a method as invokable remotely with {@link Invoke#on(Class, String, Object...)}*/
@Retention(RetentionPolicy.RUNTIME)
@interface Remote{
@interface Remote{}
}
/**Marks a method to be locally invoked as well as remotely invoked.*/
@Retention(RetentionPolicy.RUNTIME)
@interface Local{}
}

View File

@@ -8,6 +8,8 @@ import io.anuke.mindustry.entities.Player;
import io.anuke.mindustry.entities.SyncEntity;
import io.anuke.mindustry.entities.TileEntity;
import io.anuke.mindustry.entities.Unit;
import io.anuke.mindustry.net.Invoke.Local;
import io.anuke.mindustry.net.Invoke.Remote;
import io.anuke.mindustry.net.Net.SendMode;
import io.anuke.mindustry.net.Packets.*;
import io.anuke.mindustry.resource.Weapon;
@@ -114,12 +116,10 @@ public class NetEvents {
Net.send(packet, SendMode.tcp);
}
@Remote
@Local
public static void adminSet(Player player, boolean admin){
player.isAdmin = admin;
if(Net.client()){
ui.listfrag.rebuild();
}
}
public static void handleAdministerRequest(Player target, AdminAction action){