From eb69dce5856dd0e6aa2da17968930ce338041a46 Mon Sep 17 00:00:00 2001 From: Anuken Date: Mon, 14 May 2018 06:40:23 -0700 Subject: [PATCH] Removed Invoke class --- .../mindustry/input/DefaultKeybinds.java | 2 +- core/src/io/anuke/mindustry/net/Invoke.java | 173 ------------------ .../mindustry/ui/dialogs/AdminsDialog.java | 5 +- .../ui/fragments/PlayerListFragment.java | 10 +- 4 files changed, 7 insertions(+), 183 deletions(-) delete mode 100644 core/src/io/anuke/mindustry/net/Invoke.java diff --git a/core/src/io/anuke/mindustry/input/DefaultKeybinds.java b/core/src/io/anuke/mindustry/input/DefaultKeybinds.java index 478811cd6b..0f3142e22a 100644 --- a/core/src/io/anuke/mindustry/input/DefaultKeybinds.java +++ b/core/src/io/anuke/mindustry/input/DefaultKeybinds.java @@ -11,7 +11,7 @@ import io.anuke.ucore.input.Input; public class DefaultKeybinds { public static void load(){ - String[] sections = {"player_1", "player_2", "player_3", "player_4"}; + String[] sections = {"player_1"}; for(String section : sections) { diff --git a/core/src/io/anuke/mindustry/net/Invoke.java b/core/src/io/anuke/mindustry/net/Invoke.java deleted file mode 100644 index cfb7850bb6..0000000000 --- a/core/src/io/anuke/mindustry/net/Invoke.java +++ /dev/null @@ -1,173 +0,0 @@ -package io.anuke.mindustry.net; - -import com.badlogic.gdx.utils.ObjectMap; -import com.badlogic.gdx.utils.reflect.ClassReflection; -import com.badlogic.gdx.utils.reflect.Method; -import com.badlogic.gdx.utils.reflect.ReflectionException; -import io.anuke.annotations.Annotations.Local; -import io.anuke.annotations.Annotations.Remote; -import io.anuke.mindustry.Vars; -import io.anuke.mindustry.entities.Player; -import io.anuke.mindustry.game.Team; -import io.anuke.mindustry.net.Net.SendMode; -import io.anuke.mindustry.net.Packets.InvokePacket; -import io.anuke.mindustry.world.Tile; -import io.anuke.ucore.entities.Entities; -import io.anuke.ucore.entities.Entity; -import io.anuke.ucore.util.IOUtils; - -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.nio.ByteBuffer; - -import static io.anuke.mindustry.Vars.playerGroup; - -/**Class for invoking static methods of other classes remotely.*/ -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. - * @param type Type of class to invoke method on - * @param methodName Name of method to invoke. Overloads are not allowed! - * @param args List of arguments to invoke the method with. - */ - public static void on(Class type, String methodName, Object... args){ - try { - Method method = getMethod(type, methodName); - if(methodLocal.get(method)){ - method.invoke(null, args); - } - InvokePacket packet = new InvokePacket(); - - Net.send(packet, SendMode.tcp); - }catch (ReflectionException e){ - throw new RuntimeException(e); - } - } - - /** - * Invokes an event in the {@link NetEvents} class by name. - * @param methodName the name of the event method to invoke - * @param args the method arguments - */ - public static void event(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){ - Class type = o.getClass(); - - if(type == int.class){ - buffer.putInt((Integer)o); - }else if(type == float.class){ - buffer.putFloat((Float)o); - }else if(type == short.class){ - buffer.putShort((Short)o); - }else if(type == boolean.class){ - buffer.put((byte)((Boolean)o ? 1 : 0)); - }else if(type == byte.class){ - buffer.put((Byte)o); - }else if(type == long.class){ - buffer.putLong((Long)o); - }else if(type == short.class){ - buffer.putShort((Short)o); - }else if(type == Tile.class){ - buffer.putInt(((Tile)o).packedPosition()); - }else if(type == Entity.class){ - buffer.put((byte)((Entity)o).getGroup().getID()); - buffer.putInt(((Entity)o).id); - }else if(type == Player.class){ - buffer.putInt(((Player)o).id); - }else if(type == Team.class){ - buffer.put((byte)((Team)o).ordinal()); - }else if(type == String.class){ - IOUtils.writeString(buffer, (String)o); - } - } - } - - static Object[] readObjects(ByteBuffer buffer, Class[] types){ - Object[] result = new Object[types.length]; - for (int i = 0; i < result.length; i++) { - Class type = types[i]; - Object obj = null; - - if(type == int.class){ - obj = buffer.getInt(); - }else if(type == float.class){ - obj = buffer.getFloat(); - }else if(type == short.class){ - obj = buffer.getShort(); - }else if(type == boolean.class){ - obj = buffer.get() == 1; - }else if(type == byte.class){ - obj = buffer.get(); - }else if(type == long.class){ - obj = buffer.getLong(); - }else if(type == short.class){ - obj = buffer.getShort(); - }else if(type == Tile.class){ - obj = Vars.world.tile(buffer.getInt()); - }else if(type == Entity.class){ - byte group = buffer.get(); - int id = buffer.getInt(); - obj = Entities.getGroup(group).getByID(id); - }else if(type == Player.class){ - int id = buffer.getInt(); - obj = playerGroup.getByID(id); - }else if(type == Team.class){ - obj = Team.values()[buffer.get()]; - }else if(type == String.class){ - obj = IOUtils.readString(buffer); - } - - if(obj != null){ - result[i] = obj; - }else{ - throw new RuntimeException("Unable to read object of type '" + type + "'!"); - } - } - - return result; - } - - static Class findClass(String name) throws ReflectionException{ - Class cl = classes.get(name); - if(cl == null){ - cl = ClassReflection.forName(name); - classes.put(name, cl); - } - return cl; - } - - static Method getMethod(Class type, String methodname) throws ReflectionException{ - ObjectMap map = methods.get(type); - - if(map == null){ - map = new ObjectMap<>(); - methods.put(type, map); - } - - Method method = map.get(methodname); - - if(method == null){ - method = ClassReflection.getMethod(type, methodname, (Class[])null); - 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); - - } - - return method; - - } - - -} diff --git a/core/src/io/anuke/mindustry/ui/dialogs/AdminsDialog.java b/core/src/io/anuke/mindustry/ui/dialogs/AdminsDialog.java index 3715c755dd..0979c798eb 100644 --- a/core/src/io/anuke/mindustry/ui/dialogs/AdminsDialog.java +++ b/core/src/io/anuke/mindustry/ui/dialogs/AdminsDialog.java @@ -1,11 +1,10 @@ package io.anuke.mindustry.ui.dialogs; import io.anuke.mindustry.entities.Player; +import io.anuke.mindustry.gen.CallEvent; import io.anuke.mindustry.net.Administration.PlayerInfo; -import io.anuke.mindustry.net.Invoke; import io.anuke.mindustry.net.Net; import io.anuke.mindustry.net.NetConnection; -import io.anuke.mindustry.net.NetEvents; import io.anuke.ucore.scene.ui.ScrollPane; import io.anuke.ucore.scene.ui.layout.Table; @@ -50,7 +49,7 @@ public class AdminsDialog extends FloatingDialog { for(Player player : playerGroup.all()){ NetConnection c = Net.getConnection(player.clientid); if(c != null){ - Invoke.event("adminSet", player, false); + CallEvent.adminSet(player, false); break; } } diff --git a/core/src/io/anuke/mindustry/ui/fragments/PlayerListFragment.java b/core/src/io/anuke/mindustry/ui/fragments/PlayerListFragment.java index cda10bc00c..f632e0d243 100644 --- a/core/src/io/anuke/mindustry/ui/fragments/PlayerListFragment.java +++ b/core/src/io/anuke/mindustry/ui/fragments/PlayerListFragment.java @@ -1,10 +1,9 @@ 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; -import io.anuke.mindustry.net.Invoke; +import io.anuke.mindustry.gen.CallEvent; import io.anuke.mindustry.net.Net; import io.anuke.mindustry.net.NetConnection; import io.anuke.mindustry.net.NetEvents; @@ -50,8 +49,7 @@ public class PlayerListFragment implements Fragment{ margin(12f); get().addCheck("$text.server.friendlyfire", b -> { - state.friendlyFire = b; - Invoke.event("friendlyFireChange", b); + CallEvent.friendlyFireChange(b); }).growX().update(i -> i.setChecked(state.friendlyFire)).disabled(b -> Net.client()).padRight(5); new button("$text.server.bans", () -> { @@ -162,12 +160,12 @@ public class PlayerListFragment implements Fragment{ if(netServer.admins.isAdmin(id, connection.address)){ ui.showConfirm("$text.confirm", "$text.confirmunadmin", () -> { netServer.admins.unAdminPlayer(id); - Invoke.event("adminSet", player, false); + CallEvent.adminSet(player, false); }); }else{ ui.showConfirm("$text.confirm", "$text.confirmadmin", () -> { netServer.admins.adminPlayer(id, connection.address); - Invoke.event("adminSet", player, true); + CallEvent.adminSet(player, true); }); } }).update(b ->{