Clean up code & Upgrade syntax to new Java

This commit is contained in:
Đạt Nhân Trương
2022-08-07 01:35:02 +07:00
parent bbdf236a10
commit f983b4dccf
17 changed files with 44 additions and 61 deletions

View File

@@ -8,8 +8,9 @@ import server.ThreadManager;
import tools.exceptions.IdTypeNotSupportedException;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
@@ -75,12 +76,13 @@ public class IdCommand extends Command {
throw new IdTypeNotSupportedException();
}
itemMap.put(type, new HashMap<>());
BufferedReader reader = new BufferedReader(new FileReader(handbookDirectory.get(type)));
String line;
while ((line = reader.readLine()) != null) {
String[] row = line.split(" - ", 2);
if (row.length == 2) {
itemMap.get(type).put(row[1].toLowerCase(), row[0]);
try (BufferedReader reader = Files.newBufferedReader(Paths.get(handbookDirectory.get(type)))) {
String line;
while ((line = reader.readLine()) != null) {
String[] row = line.split(" - ", 2);
if (row.length == 2) {
itemMap.get(type).put(row[1].toLowerCase(), row[0]);
}
}
}
}

View File

@@ -48,19 +48,15 @@ public class WhereaMiCommand extends Command {
HashSet<Monster> mobs = new HashSet<>();
for (MapObject mmo : player.getMap().getMapObjects()) {
if (mmo instanceof NPC) {
NPC npc = (NPC) mmo;
if (mmo instanceof NPC npc) {
npcs.add(npc);
} else if (mmo instanceof Character) {
Character mc = (Character) mmo;
} else if (mmo instanceof Character mc) {
chars.add(mc);
} else if (mmo instanceof Monster) {
Monster mob = (Monster) mmo;
} else if (mmo instanceof Monster mob) {
if (mob.isAlive()) {
mobs.add(mob);
}
} else if (mmo instanceof PlayerNPC) {
PlayerNPC npc = (PlayerNPC) mmo;
} else if (mmo instanceof PlayerNPC npc) {
playernpcs.add(npc);
}
}

View File

@@ -35,9 +35,10 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tools.HexTool;
import java.io.FileReader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Properties;
public class PeCommand extends Command {
@@ -51,11 +52,9 @@ public class PeCommand extends Command {
public void execute(Client c, String[] params) {
Character player = c.getPlayer();
String packet = "";
try {
InputStreamReader is = new FileReader("pe.txt");
try (BufferedReader br = Files.newBufferedReader(Paths.get("pe.txt"))) {
Properties packetProps = new Properties();
packetProps.load(is);
is.close();
packetProps.load(br);
packet = packetProps.getProperty("pe");
} catch (IOException ex) {
ex.printStackTrace();