From 09bdfd7ac196e80a4336692e6f9187ae0f99795b Mon Sep 17 00:00:00 2001 From: Anuken Date: Wed, 9 May 2018 16:41:02 -0700 Subject: [PATCH] Added local method invocation annotation --- core/src/io/anuke/mindustry/net/Invoke.java | 17 ++++++++++------- core/src/io/anuke/mindustry/net/NetEvents.java | 8 ++++---- .../ui/fragments/PlayerListFragment.java | 13 +++++++++---- 3 files changed, 23 insertions(+), 15 deletions(-) diff --git a/core/src/io/anuke/mindustry/net/Invoke.java b/core/src/io/anuke/mindustry/net/Invoke.java index 1dedc24206..ebde209e38 100644 --- a/core/src/io/anuke/mindustry/net/Invoke.java +++ b/core/src/io/anuke/mindustry/net/Invoke.java @@ -24,6 +24,7 @@ import static io.anuke.mindustry.Vars.playerGroup; public class Invoke { private static ObjectMap> methods = new ObjectMap<>(); private static ObjectMap classes = new ObjectMap<>(); + private static ObjectMap 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{} } diff --git a/core/src/io/anuke/mindustry/net/NetEvents.java b/core/src/io/anuke/mindustry/net/NetEvents.java index 2cddfbeddf..1b4d624d7a 100644 --- a/core/src/io/anuke/mindustry/net/NetEvents.java +++ b/core/src/io/anuke/mindustry/net/NetEvents.java @@ -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){ diff --git a/core/src/io/anuke/mindustry/ui/fragments/PlayerListFragment.java b/core/src/io/anuke/mindustry/ui/fragments/PlayerListFragment.java index c2a8dc68d0..c0540158ee 100644 --- a/core/src/io/anuke/mindustry/ui/fragments/PlayerListFragment.java +++ b/core/src/io/anuke/mindustry/ui/fragments/PlayerListFragment.java @@ -1,5 +1,6 @@ package io.anuke.mindustry.ui.fragments; +import com.badlogic.gdx.utils.ObjectMap; import io.anuke.mindustry.Vars; import io.anuke.mindustry.core.GameState.State; import io.anuke.mindustry.entities.Player; @@ -28,7 +29,7 @@ import static io.anuke.mindustry.Vars.*; public class PlayerListFragment implements Fragment{ public boolean visible = false; Table content = new Table(); - int last = 0; + ObjectMap checkmap = new ObjectMap<>(); @Override public void build(){ @@ -72,10 +73,14 @@ public class PlayerListFragment implements Fragment{ if(!(Net.active() && !state.is(State.menu))){ visible = false; } - if(playerGroup.size() != last){ - rebuild(); - last = playerGroup.size(); + boolean rebuild = false; + for(Player player : playerGroup.all()){ + if(!checkmap.containsKey(player) || checkmap.get(player, false) != player.isAdmin){ + rebuild = true; + } + checkmap.put(player, player.isAdmin); } + if(rebuild) rebuild(); }); visible(() -> visible);