Conditions | 9 |
Total Lines | 67 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | package de.kleiner3.lasertag.lasertaggame.management.team; |
||
35 | public TeamConfigManager() { |
||
36 | // Get config file |
||
37 | var teamConfigFile = new File(teamConfigFilePath); |
||
38 | |||
39 | // If the config file exists |
||
40 | if (teamConfigFile.exists()) { |
||
41 | try { |
||
42 | // Read config file |
||
43 | var configFileContents = FileIO.readAllFile(teamConfigFile); |
||
44 | |||
45 | // get gson builder |
||
46 | var gsonBuilder = new GsonBuilder(); |
||
47 | |||
48 | // Get deserializer |
||
49 | var deserializer = TeamConfigManagerDeserializer.getDeserializer(); |
||
50 | |||
51 | // Register type |
||
52 | gsonBuilder.registerTypeAdapter(HashMap.class, deserializer); |
||
53 | |||
54 | // Parse |
||
55 | teamConfig = gsonBuilder.create().fromJson(configFileContents, new TypeToken<HashMap<String, TeamDto>>() { |
||
56 | }.getType()); |
||
57 | } catch (IOException ex) { |
||
58 | LasertagMod.LOGGER.warn("Reading of team config file failed: " + ex.getMessage()); |
||
59 | } |
||
60 | } |
||
61 | |||
62 | // If config couldn't be loaded from file |
||
63 | if (teamConfig == null) { |
||
64 | // Create map |
||
65 | teamConfig = new HashMap<>(); |
||
66 | |||
67 | // Fill map with default values |
||
68 | teamConfig.put("Red", new TeamDto("Red", 255, 0, 0, Blocks.RED_CONCRETE)); |
||
69 | teamConfig.put("Green", new TeamDto("Green", 0, 255, 0, Blocks.LIME_CONCRETE)); |
||
70 | teamConfig.put("Blue", new TeamDto("Blue", 0, 0, 255, Blocks.BLUE_CONCRETE)); |
||
71 | teamConfig.put("Orange", new TeamDto("Orange", 255, 128, 0, Blocks.ORANGE_CONCRETE)); |
||
72 | teamConfig.put("Teal", new TeamDto("Teal", 0, 128, 255, Blocks.LIGHT_BLUE_CONCRETE)); |
||
73 | teamConfig.put("Pink", new TeamDto("Pink", 255, 0, 255, Blocks.PINK_CONCRETE)); |
||
74 | |||
75 | // Get gson builder |
||
76 | var gsonBuilder = new GsonBuilder(); |
||
77 | |||
78 | // Register type adapter |
||
79 | gsonBuilder.registerTypeAdapter(TeamDto.class, TeamDtoSerializer.getSerializer()); |
||
80 | |||
81 | // Serialize |
||
82 | var configJson = gsonBuilder.setPrettyPrinting().create().toJson(teamConfig); |
||
83 | |||
84 | // Persist |
||
85 | try { |
||
86 | var dir = new File(LasertagMod.configFolderPath); |
||
87 | |||
88 | // Create directory if not exists |
||
89 | if (!dir.exists() && !dir.mkdir()) { |
||
90 | throw new IOException("Make directory for team config failed!"); |
||
91 | } |
||
92 | |||
93 | // Create file if not exists |
||
94 | if (!teamConfigFile.exists() && !teamConfigFile.createNewFile()) { |
||
95 | throw new IOException("Creation of file for team config failed!"); |
||
96 | } |
||
97 | |||
98 | // Write to file |
||
99 | FileIO.writeAllFile(teamConfigFile, configJson); |
||
100 | } catch (IOException e) { |
||
101 | LasertagMod.LOGGER.error("Writing to team config file failed: " + e.getMessage()); |
||
102 | } |
||
125 |