Skip to content

Commit

Permalink
Fix many account bugs. Close #54
Browse files Browse the repository at this point in the history
  • Loading branch information
Kieaer committed May 17, 2020
1 parent 060ce83 commit 221a430
Show file tree
Hide file tree
Showing 14 changed files with 460 additions and 260 deletions.
27 changes: 13 additions & 14 deletions src/main/java/essentials/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -458,8 +458,9 @@ public void accept(String[] strings, Object o) {
playerData.permission(arg[1]);
perm.permission_user.get(playerData.uuid()).asObject().set("group", arg[1]);
perm.update();
perm.reload(false);

target.isAdmin = perm.isAdmin(target);
target.isAdmin = perm.isAdmin(playerData);

Log.info(bundle.get("success"));
target.sendMessage(new Bundle(playerDB.get(target.uuid).locale()).prefix("perm-changed"));
Expand Down Expand Up @@ -530,7 +531,7 @@ public void registerClientCommands(CommandHandler handler) {
}
try {
Class.forName("org.mindrot.jbcrypt.BCrypt");
playerData.accountpw(BCrypt.hashpw(arg[0], BCrypt.gensalt(11)));
playerData.accountpw(BCrypt.hashpw(arg[0], BCrypt.gensalt(12)));
player.sendMessage(bundle.prefix("success"));
} catch (ClassNotFoundException e) {
new CrashReport(e);
Expand Down Expand Up @@ -755,19 +756,16 @@ public void registerClientCommands(CommandHandler handler) {
PlayerData playerData = playerDB.get(player.uuid);
if (config.loginEnable()) {
if (playerData.error()) {
if (playerCore.login(player, arg[0], arg[1])) {
if (config.passwordMethod().equals("discord")) {
playerCore.load(player, arg[0]);
} else {
playerCore.load(player);
if (playerCore.login(arg[0], arg[1])) {
if (playerCore.load(player, arg[0])) {
player.sendMessage(new Bundle(playerData.locale()).prefix("system.login.success"));
}
player.sendMessage(new Bundle(playerData.locale()).prefix("system.login.success"));
} else {
player.sendMessage("[green][EssentialPlayer] [scarlet]Login failed/로그인 실패!!");
}
} else {
if (config.passwordMethod().equals("mixed")) {
if (playerCore.login(player, arg[0], arg[1])) Call.onConnect(player.con, vars.serverIP(), 7060);
if (playerCore.login(arg[0], arg[1])) Call.onConnect(player.con, vars.serverIP(), 7060);
} else {
player.sendMessage("[green][EssentialPlayer] [scarlet]You're already logged./이미 로그인한 상태입니다.");
}
Expand All @@ -781,11 +779,11 @@ public void registerClientCommands(CommandHandler handler) {

PlayerData playerData = playerDB.get(player.uuid);
Bundle bundle = new Bundle(playerData.locale());
if (config.loginEnable()) {
if (config.loginEnable() && !playerData.error()) {
playerData.connected(false);
playerData.connserver("none");
playerData.uuid("Logout");
Call.onKick(player.con, bundle.get("system.logout"));
Call.onKick(player.con, new Bundle(playerData.locale()).get("system.logout"));
} else {
player.sendMessage(bundle.prefix("system.login.disabled"));
}
Expand Down Expand Up @@ -905,10 +903,11 @@ public void registerClientCommands(CommandHandler handler) {
default:
case "password":
Locale lc = tool.getGeo(player);
boolean register = playerDB.register(player.name, player.uuid, lc.getDisplayCountry(), lc.toString(), lc.getDisplayLanguage(), true, vars.serverIP(), "default", 0L, arg[0], arg[1]);
String hash = BCrypt.hashpw(arg[1], BCrypt.gensalt(12));
boolean register = playerDB.register(player.name, player.uuid, lc.getDisplayCountry(), lc.toString(), lc.getDisplayLanguage(), true, vars.serverIP(), "default", 0L, arg[0], hash);
if (register) {
PlayerData playerData = playerDB.load(player.uuid, arg[0]);
player.sendMessage(new Bundle(playerData.locale()).prefix("register-success"));
playerCore.load(player);
player.sendMessage(new Bundle(playerDB.get(player.uuid).locale()).prefix("register-success"));
} else {
player.sendMessage("[green][Essentials] [scarlet]Register failed/계정 등록 실패!");
}
Expand Down
40 changes: 23 additions & 17 deletions src/main/java/essentials/core/player/PlayerCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import mindustry.entities.type.Player;
import mindustry.gen.Call;
import mindustry.net.Packets;
import org.mindrot.jbcrypt.BCrypt;

import java.net.InetAddress;
import java.net.NetworkInterface;
Expand All @@ -24,6 +25,7 @@ public boolean load(Player player, String... AccountID) {
} else {
playerData = playerDB.load(player.uuid, AccountID);
}

if (playerData.error()) {
new CrashReport(new Exception("DATA NOT FOUND"));
return false;
Expand All @@ -44,26 +46,27 @@ public boolean load(Player player, String... AccountID) {
}

if (playerData.colornick()) colornick.targets.add(player);
if (perm.permission_user.get(playerData.uuid()) == null) {
perm.create(playerData);
perm.saveAll();
} else {
if (config.realName() || config.passwordMethod().equals("discord")) {
player.name = playerData.name();
} else {
player.name = perm.permission_user.get(playerData.uuid()).asObject().get("name").asString();
}
}

player.isAdmin = perm.isAdmin(player);

String oldUUID = playerData.uuid();
playerData.uuid(player.uuid);
playerData.uuid();
playerData.connected(true);
playerData.lastdate(tool.getTime());
playerData.connserver(vars.serverIP());
playerData.exp(playerData.exp() + playerData.joincount());
playerData.joincount(playerData.joincount() + 1);
playerData.login(true);

perm.setPermission_user(oldUUID, player.uuid);

if (perm.permission_user.get(player.uuid) == null) {
perm.create(playerData);
perm.saveAll();
} else {
player.name = perm.permission_user.get(playerData.uuid()).asObject().get("name").asString();
}

player.isAdmin = perm.isAdmin(playerData);
return true;
}

Expand Down Expand Up @@ -115,20 +118,23 @@ public PlayerData NewData(String name, String uuid, String country, String count

public boolean isLocal(Player player) {
try {
InetAddress addr = InetAddress.getByName(netServer.admins.getInfo(player.uuid).lastIP);
InetAddress addr = InetAddress.getByName(player.con.address);
if (addr.isAnyLocalAddress() || addr.isLoopbackAddress()) return true;
return NetworkInterface.getByInetAddress(addr) != null;
} catch (Exception e) {
return false;
}
}

public boolean login(Player player, String id, String pw) {
try (PreparedStatement pstmt = database.conn.prepareStatement("SELECT * from players WHERE accountid=? AND accountpw=?")) {
public boolean login(String id, String pw) {
try (PreparedStatement pstmt = database.conn.prepareStatement("SELECT * from players WHERE accountid=?")) {
pstmt.setString(1, id);
pstmt.setString(2, pw);
try (ResultSet rs = pstmt.executeQuery()) {
return rs.next();
if (rs.next()) {
return BCrypt.checkpw(pw, rs.getString("accountpw"));
} else {
return false;
}
}
} catch (SQLException e) {
new CrashReport(e);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/essentials/core/player/PlayerDB.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public PlayerData load(String uuid, String... AccountID) {

try (PreparedStatement pstmt = database.conn.prepareStatement(sql.toString())) {
pstmt.setString(1, uuid);
if (AccountID != null && AccountID.length != 0) pstmt.setString(2, AccountID[0]);
try (ResultSet rs = pstmt.executeQuery()) {
if (AccountID != null && AccountID.length != 0) pstmt.setString(2, AccountID[0]);
if (rs.next()) {
PlayerData data = new PlayerData(
rs.getString("name"),
Expand Down
42 changes: 22 additions & 20 deletions src/main/java/essentials/core/plugin/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -367,28 +367,30 @@ public void LegacyUpgrade() {
network.add("bantrust", obj.get("bantrust") == null ? readJSON("[\"127.0.0.1\",\"localhost\"]").asArray() : obj.get("bantrust").asArray(), bundle.get("config.server.bantrust"));
network.add("query", obj.getBoolean("query", false), bundle.get("config.server.query"));

anti.add("antigrief", obj.getBoolean("antigrief", false), bundle.get("config.anti-grief.desc"));
anti.add("antivpn", obj.getBoolean("antivpn", false), bundle.get("config.anti-grief.vpn"));
anti.add("antirush", obj.getBoolean("antirush", false), bundle.get("config.anti-grief.pvprush"));
anti.add("antirushtime", LocalTime.parse(obj.getString("antirushtime", "00:10:00"), DateTimeFormatter.ofPattern("HH:mm:ss")).format(DateTimeFormatter.ofPattern("HH:mm:ss")));
anti.add("alert-action", obj.getBoolean("alertaction", false), bundle.get("config-alert-action-description"));
anti.add("realname", obj.getBoolean("realname", false), bundle.get("config.anti-grief.realname"));
anti.add("strict-name", obj.getBoolean("strictname", false), bundle.get("config-strict-name-description"));
anti.add("scanresource", obj.getBoolean("scanresource", false), bundle.get("config.anti-grief.scan-resource"));

JsonObject antiObject = obj.get("antigrief").asObject();
anti.add("antigrief", antiObject.getBoolean("antigrief", false), bundle.get("config.anti-grief.desc"));
anti.add("antivpn", antiObject.getBoolean("antivpn", false), bundle.get("config.anti-grief.vpn"));
anti.add("antirush", antiObject.getBoolean("antirush", false), bundle.get("config.anti-grief.pvprush"));
anti.add("antirushtime", LocalTime.parse(antiObject.getString("antirushtime", "00:10:00"), DateTimeFormatter.ofPattern("HH:mm:ss")).format(DateTimeFormatter.ofPattern("HH:mm:ss")));
anti.add("alert-action", antiObject.getBoolean("alertaction", false), bundle.get("config-alert-action-description"));
anti.add("realname", antiObject.getBoolean("realname", false), bundle.get("config.anti-grief.realname"));
anti.add("strict-name", antiObject.getBoolean("strictname", false), bundle.get("config-strict-name-description"));
anti.add("scanresource", antiObject.getBoolean("scanresource", false), bundle.get("config.anti-grief.scan-resource"));

JsonObject featureObject = obj.get("features").asObject();
features.add("explimit", obj.getBoolean("explimit", false), bundle.get("config.feature.exp.limit"));
features.add("basexp", obj.getDouble("basexp", 500.0), bundle.get("config.feature.exp.basexp"));
features.add("exponent", obj.getDouble("exponent", 1.12), bundle.get("config.feature.exp.exponent"));
features.add("levelupalarm", obj.getBoolean("levelupalarm", false), bundle.get("config.feature.exp.levelup-alarm"));
features.add("alarm-minimal-level", obj.getInt("alarmlevel", 20), bundle.get("config.feature.exp.minimal-level"));
features.add("basexp", featureObject.getDouble("basexp", 500.0), bundle.get("config.feature.exp.basexp"));
features.add("exponent", featureObject.getDouble("exponent", 1.12), bundle.get("config.feature.exp.exponent"));
features.add("levelupalarm", featureObject.getBoolean("levelupalarm", false), bundle.get("config.feature.exp.levelup-alarm"));
features.add("alarm-minimal-level", featureObject.getInt("alarmlevel", 20), bundle.get("config.feature.exp.minimal-level"));
features.add("vote", true, bundle.get("config.feature.vote"));
features.add("savetime", LocalTime.parse(obj.getString("savetime", "00:10:00"), DateTimeFormatter.ofPattern("HH:mm:ss")).format(DateTimeFormatter.ofPattern("HH:mm:ss")), bundle.get("config.feature.save-time"));
features.add("rollback", obj.getBoolean("rollback", false), bundle.get("config.feature.slot-number"));
features.add("slotnumber", obj.getInt("slotnumber", 1000));
features.add("border", obj.getBoolean("border", false), bundle.get("config.feature.border"));
features.add("spawnlimit", obj.getInt("spawnlimit", 500), bundle.get("config.feature.spawn-limit"));
features.add("eventport", obj.getString("eventport", "8000-8050"), bundle.get("config.feature.event.port"));
features.add("cupdatei", obj.getInt("cupdatei", 1000), bundle.get("config.feature.colornick"));
features.add("savetime", LocalTime.parse(featureObject.getString("savetime", "00:10:00"), DateTimeFormatter.ofPattern("HH:mm:ss")).format(DateTimeFormatter.ofPattern("HH:mm:ss")), bundle.get("config.feature.save-time"));
features.add("rollback", featureObject.getBoolean("rollback", false), bundle.get("config.feature.slot-number"));
features.add("slotnumber", featureObject.getInt("slotnumber", 1000));
features.add("border", featureObject.getBoolean("border", false), bundle.get("config.feature.border"));
features.add("spawnlimit", featureObject.getInt("spawnlimit", 500), bundle.get("config.feature.spawn-limit"));
features.add("eventport", featureObject.getString("eventport", "8000-8050"), bundle.get("config.feature.event.port"));
features.add("cupdatei", featureObject.getInt("cupdatei", 1000), bundle.get("config.feature.colornick"));

features.add("difficulty", difficulty, bundle.get("config.auto-difficulty"));
difficulty.add("auto-difficulty", obj.getBoolean("autodifficulty", false));
Expand Down
6 changes: 0 additions & 6 deletions src/main/java/essentials/external/DataMigration.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,4 @@ public void MigrateDB() {
new CrashReport(e);
}
}

public void move(String path) {
String data = root.child(path + ".yml").readString();
root.child(path + ".hjson").writeString("{\n" + data + "\n}");
root.child(path + ".yml").delete();
}
}
30 changes: 16 additions & 14 deletions src/main/java/essentials/feature/Permission.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
import java.io.IOException;
import java.text.ParseException;

import static essentials.Main.playerDB;
import static essentials.Main.root;
import static essentials.Main.*;
import static mindustry.Vars.playerGroup;

public class Permission {
Expand Down Expand Up @@ -116,7 +115,7 @@ public void reload(boolean init) {
try {
permission_user = JsonValue.readHjson(root.child("permission_user.hjson").reader()).asObject();
for (Player p : playerGroup.all()) {
p.isAdmin = isAdmin(p);
p.isAdmin = isAdmin(vars.playerData().find(d -> d.name().equals(p.name)));
}
} catch (IOException e) {
// 이것도 유저들이 알아야 고침
Expand All @@ -131,11 +130,12 @@ public boolean check(Player player, String command) {
PlayerData p = playerDB.get(player.uuid);

if (!p.error()) {
JsonObject object = permission_user.get(player.uuid).asObject();
JsonValue object = permission_user.get(player.uuid);
if (object != null) {
int size = permission.get(object.get("group").asString()).asObject().get("permission").asArray().size();
JsonObject obj = object.asObject();
int size = permission.get(obj.get("group").asString()).asObject().get("permission").asArray().size();
for (int a = 0; a < size; a++) {
String permlevel = permission.get(object.get("group").asString()).asObject().get("permission").asArray().get(a).asString();
String permlevel = permission.get(obj.get("group").asString()).asObject().get("permission").asArray().get(a).asString();
if (permlevel.equals(command) || permlevel.equals("ALL")) {
return true;
}
Expand All @@ -147,14 +147,16 @@ public boolean check(Player player, String command) {
return false;
}

public boolean isAdmin(Player player) {
if (player == null) {
new CrashReport(new Exception("isAdmin player NULL!"));
Core.app.dispose();
Core.app.exit();
System.exit(0);
public boolean isAdmin(PlayerData player) {
return permission_user.get(player.uuid()).asObject().getBoolean("admin", false);
}

public void setPermission_user(String old, String newid) {
if (!old.equals(newid)) {
JsonObject oldJson = permission_user.get(old).asObject();
permission_user.set(newid, oldJson);
permission_user.remove(old);
update();
}
PlayerData p = playerDB.get(player.uuid);
return permission_user.get(p.uuid()).asObject().getBoolean("admin", false);
}
}
13 changes: 0 additions & 13 deletions src/main/java/essentials/internal/Bundle.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,6 @@ public String get(String key, Object... params) {
}
}

public String get(boolean NotNull, String key, Object... params) {
try {
return MessageFormat.format(RESOURCE_BUNDLE.getString(key), params);
} catch (MissingResourceException e) {
if (NotNull) {
ResourceBundle bundle = ResourceBundle.getBundle("bundle.bundle", Locale.US, new UTF8Control());
return MessageFormat.format(bundle.getString(key), params);
} else {
return null;
}
}
}

public String prefix(String key, Object... params) {
try {
return MessageFormat.format(config.prefix() + RESOURCE_BUNDLE.getString(key), params);
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/essentials/internal/CrashReport.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public CrashReport(Throwable e) {
String logs = "플러그인 버전: " + vars.pluginVersion() + "\n" +
"서버 버전: " + Version.build + "." + Version.revision + " " + Version.modifier + "\n" +
"OS: " + System.getProperty("os.name") + "\n" +
"플러그인 목록: " + plugins.toString().substring(0, plugins.length() - 2) + "\n" +
"플러그인 목록: " + (plugins.toString().contains(", ") ? plugins.toString().substring(0, plugins.length() - 2) : plugins.toString()) + "\n" +
"== 설정파일 ==\n" + JsonValue.readHjson(root.child("config.hjson").readString()).toString(Stringify.HJSON) + "\n" +
"== Stacktrace ==\n" + sb.toString() + "\n!exit!\n";

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/essentials/internal/Log.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public static void write(LogType type, String value, Object... params) {
}

if (mainlog == null) mainlog = logfolder.child(type + ".log");
mainlog.writeString("[" + tool.getTime() + "]" + config.bundle.get(value, params) + "\n", true);
mainlog.writeString("[" + tool.getTime() + "] " + (config.bundle.get(value, params) != null ? config.bundle.get(value, params) : value) + "\n", true);
}

public enum LogType {
Expand Down
1 change: 1 addition & 0 deletions src/main/java/essentials/internal/Tools.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ public byte[] decrypt(byte[] data, SecretKeySpec spec, Cipher cipher) throws Exc
public Locale getGeo(Object data) {
String ip = data instanceof Player ? netServer.admins.getInfo(((Player) data).uuid).lastIP : (String) data;
Locale loc = Locale.US;
if (ip.equals("<unknown>")) return loc;
JsonObject result = readJSON(getWebContent("https://ipapi.co/" + ip + "/json")).asObject();

if (result.get("reserved") != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,7 @@ public PluginException(String message) {
super(message);
}

public PluginException(String message, Throwable cause) {
super(message, cause);
}

public PluginException(Throwable cause) {
super(cause);
}

protected PluginException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
Loading

0 comments on commit 221a430

Please sign in to comment.