1
0
mirror of https://github.com/Anuken/Mindustry.git synced 2024-11-13 07:15:28 +03:00

Added name validation

This commit is contained in:
Anuken 2018-06-13 11:38:12 -04:00
parent 6b713b283a
commit 4ca9a26bdd
8 changed files with 67 additions and 6 deletions

View File

@ -19,7 +19,7 @@ public class Annotations {
Loc targets() default Loc.server;
/**Specifies which methods are generated. Only affects server-to-client methods.*/
Variant variants() default Variant.all;
/**The local locations where this method is called.*/
/**The local locations where this method is called locally, when invoked.*/
Loc called() default Loc.none;
/**Whether to forward this packet to all other clients upon recieval. Server only.*/
boolean forward() default false;

View File

@ -50,6 +50,7 @@ text.server.kicked.serverOutdated=Outdated server! Ask the host to update!
text.server.kicked.banned=You are banned on this server.
text.server.kicked.recentKick=You have been kicked recently.\nWait before connecting again.
text.server.kicked.nameInUse=There is someone with that name\nalready on this server.
text.server.kicked.nameEmpty=Your name must contain at least one character or number.
text.server.kicked.idInUse=You are already on this server! Connecting with two accounts is not permitted.
text.server.connected={0} has joined.
text.server.disconnected={0} has disconnected.

View File

@ -73,6 +73,8 @@ public class Vars{
public static boolean showBlockDebug = false;
public static final int maxTextLength = 150;
public static final int maxNameLength = 40;
public static final int maxCharNameLength = 20;
public static boolean headless = false;

View File

@ -1,5 +1,7 @@
package io.anuke.mindustry.core;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Colors;
import com.badlogic.gdx.utils.Array;
import com.badlogic.gdx.utils.Base64Coder;
import com.badlogic.gdx.utils.IntMap;
@ -98,6 +100,13 @@ public class NetServer extends Module{
}
}
packet.name = fixName(packet.name);
if(packet.name.trim().length() <= 0){
kick(id, KickReason.nameEmpty);
return;
}
Log.info("Recieved connect packet for player '{0}' / UUID {1} / IP {2}", packet.name, uuid, trace.ip);
String ip = Net.getConnection(id).address;
@ -201,6 +210,47 @@ public class NetServer extends Module{
return connections.get(connectionID).uuid;
}
String fixName(String name){
for(int i = 0; i < name.length(); i ++){
if(name.charAt(i) == '[' && i != name.length() - 1 && name.charAt(i + 1) != '[' && (i == 0 || name.charAt(i - 1) != '[')){
String prev = name.substring(0, i);
String next = name.substring(i);
String result = checkColor(next);
name = prev + result;
}
}
return name.substring(0, Math.min(name.length(), maxNameLength));
}
String checkColor(String str){
for(int i = 1; i < str.length(); i ++){
if(str.charAt(i) == ']'){
String color = str.substring(1, i);
if(Colors.get(color.toUpperCase()) != null || Colors.get(color.toLowerCase()) != null){
Color result = (Colors.get(color.toLowerCase()) == null ? Colors.get(color.toUpperCase()) : Colors.get(color.toLowerCase()));
if(result.a <= 0.8f){
return str.substring(i + 1);
}
}else{
try{
Color result = Color.valueOf(color);
if(result.a <= 0.8f){
return str.substring(i + 1);
}
}catch (Exception e){
return str;
}
}
}
}
return str;
}
void sync(){
try {

View File

@ -6,18 +6,23 @@ import io.anuke.annotations.Annotations.Variant;
import io.anuke.mindustry.Vars;
import io.anuke.mindustry.entities.Player;
import static io.anuke.mindustry.Vars.maxTextLength;
import static io.anuke.mindustry.Vars.playerGroup;
public class NetEvents {
@Remote(called = Loc.both, targets = Loc.both)
@Remote(called = Loc.server, targets = Loc.both, forward = true)
public static void sendMessage(Player player, String message){
if(message.length() > maxTextLength){
throw new ValidateException(player, "Player has sent a message above the text limit.");
}
if(Vars.ui != null){
Vars.ui.chatfrag.addMessage(message, player == null ? null : colorizeName(player.id, player.name));
}
}
@Remote(called = Loc.both, variants = Variant.both)
@Remote(called = Loc.server, variants = Variant.both, forward = true)
public static void sendMessage(String message){
if(Vars.ui != null){
Vars.ui.chatfrag.addMessage(message, null);

View File

@ -137,7 +137,7 @@ public class Packets {
}
public enum KickReason{
kick, invalidPassword, clientOutdated, serverOutdated, banned, gameover(true), recentKick, nameInUse, idInUse, fastShoot;
kick, invalidPassword, clientOutdated, serverOutdated, banned, gameover(true), recentKick, nameInUse, idInUse, fastShoot, nameEmpty;
public final boolean quiet;
KickReason(){ quiet = false; }

View File

@ -22,6 +22,7 @@ import io.anuke.ucore.util.Bundles;
import io.anuke.ucore.util.Log;
import io.anuke.ucore.util.Strings;
import static io.anuke.mindustry.Vars.maxNameLength;
import static io.anuke.mindustry.Vars.players;
import static io.anuke.mindustry.Vars.ui;
@ -211,7 +212,7 @@ public class JoinDialog extends FloatingDialog {
player.name = text;
Settings.put("name", text);
Settings.save();
}).grow().pad(8).get().setMaxLength(40);
}).grow().pad(8).get().setMaxLength(maxNameLength);
ImageButton button = t.addImageButton("white", 40, () -> {
new ColorPickDialog().show(color -> {

View File

@ -58,7 +58,9 @@ public class MenuFragment implements Fragment{
new imagebutton("icon-play-2", isize, ui.levels::show).text("$text.play").padTop(4f);
new imagebutton("icon-tutorial", isize, () -> {}).text("$text.tutorial").padTop(4f);
new imagebutton("icon-tutorial", isize, () -> {
}).text("$text.tutorial").padTop(4f);
new imagebutton("icon-load", isize, ui.load::show).text("$text.load").padTop(4f);