• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

SimplyVanilla / SimplyRank / #54

14 Mar 2024 02:15PM UTC coverage: 19.562% (-0.4%) from 19.974%
#54

Pull #100

github

web-flow
Merge 0adde466c into 1025ae803
Pull Request #100: Fix mysql connection #2

0 of 17 new or added lines in 2 files covered. (0.0%)

1 existing line in 1 file now uncovered.

152 of 777 relevant lines covered (19.56%)

0.2 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

2.15
/src/main/java/net/simplyvanilla/simplyrank/SimplyRankPlugin.java
1
package net.simplyvanilla.simplyrank;
2

3
import com.google.common.reflect.TypeToken;
4
import com.google.gson.Gson;
5
import com.google.gson.GsonBuilder;
6
import io.papermc.paper.threadedregions.scheduler.ScheduledTask;
7
import net.kyori.adventure.text.format.NamedTextColor;
8
import net.kyori.adventure.text.format.TextColor;
9
import net.simplyvanilla.simplyrank.addresswhitelist.AddressWhitelistService;
10
import net.simplyvanilla.simplyrank.command.SimplyRankCommandExecutor;
11
import net.simplyvanilla.simplyrank.command.address.AddressWhitelistCommand;
12
import net.simplyvanilla.simplyrank.database.exception.DatabaseConnectionFailException;
13
import net.simplyvanilla.simplyrank.database.group.GroupData;
14
import net.simplyvanilla.simplyrank.database.sql.MySqlClient;
15
import net.simplyvanilla.simplyrank.database.sql.MySqlRepository;
16
import net.simplyvanilla.simplyrank.gson.TextColorGsonDeserializer;
17
import net.simplyvanilla.simplyrank.listener.PlayerLoginEventListener;
18
import net.simplyvanilla.simplyrank.listener.PlayerQuitEventListener;
19
import net.simplyvanilla.simplyrank.permission.GroupPermissionService;
20
import net.simplyvanilla.simplyrank.permission.PermissionApplyService;
21
import net.simplyvanilla.simplyrank.permission.PlayerDataService;
22
import net.simplyvanilla.simplyrank.permission.PlayerPermissionService;
23
import net.simplyvanilla.simplyrank.placeholder.MiniPlaceholderRegister;
24
import net.simplyvanilla.simplyrank.proxy.ProxyService;
25
import net.simplyvanilla.simplyrank.proxy.ProxyTtlCleanupTask;
26
import net.simplyvanilla.simplyrank.proxy.provider.ProxyCheckProvider;
27
import org.bukkit.Bukkit;
28
import org.bukkit.configuration.ConfigurationSection;
29
import org.bukkit.configuration.file.FileConfiguration;
30
import org.bukkit.configuration.file.YamlConfiguration;
31
import org.bukkit.plugin.Plugin;
32
import org.bukkit.plugin.java.JavaPlugin;
33
import org.jetbrains.annotations.Nullable;
34

35
import java.io.File;
36
import java.io.IOException;
37
import java.nio.file.Files;
38
import java.util.Map;
39
import java.util.Set;
40
import java.util.concurrent.TimeUnit;
41
import java.util.logging.Level;
42

43
public class SimplyRankPlugin extends JavaPlugin {
×
44

45
    private static SimplyRankPlugin instance;
46
    private PlayerDataService playerDataService;
47

48
    private MySqlClient mySqlClient = null;
×
49

50
    private ScheduledTask cleanerTask;
51

52

53
    @Override
54
    public void onEnable() {
55
        ProxyService proxyService;
56
        MySqlRepository mySqlRepository;
57
        FileConfiguration config;
58
        instance = this;
×
59

60
        try {
61
            config = this.loadConfig("config.yml");
×
62
        } catch (IOException e) {
×
63
            e.printStackTrace();
×
64
            this.getLogger().log(Level.SEVERE, "Could not load config file! Disabling plugin...");
×
65
            this.getServer().getPluginManager().disablePlugin(this);
×
66
            return;
×
67
        }
×
68

69
        try {
70
            this.mySqlClient = this.createSQLHandlerFromConfig(config);
×
71
        } catch (NullPointerException e) {
×
72
            this.getLogger()
×
73
                .log(
×
74
                    Level.SEVERE,
75
                    "Could not establish connection to database. Presumably, there are credentials missing!");
76
            this.getServer().getPluginManager().disablePlugin(this);
×
77
            return;
×
78
        }
×
79

80
        Gson gson =
×
81
            new GsonBuilder()
82
                .setPrettyPrinting()
×
83
                .registerTypeAdapter(
×
84
                    new TypeToken<TextColor>() {
×
85
                    }.getType(), new TextColorGsonDeserializer())
×
86
                .create();
×
87

88
        mySqlRepository = new MySqlRepository(this.mySqlClient, gson);
×
89

90
        File dataFolder = this.getDataFolder();
×
91

92
        if (!dataFolder.exists()) {
×
93
            dataFolder.mkdirs();
×
94
        }
95

96
        this.playerDataService =
×
97
            new PlayerDataService(mySqlRepository, mySqlRepository);
98
        proxyService = new ProxyService(mySqlRepository, new ProxyCheckProvider(this.getConfig().getString("proxycheck-api-url", "https://proxycheck.io/v2/%s&vpn=1")));
×
NEW
99
        this.cleanerTask = Bukkit.getAsyncScheduler().runAtFixedRate(this, new ProxyTtlCleanupTask(proxyService, this.getConfig().getInt("proxycache-ttl", 720)), 1, 10, TimeUnit.SECONDS);
×
100

101
        if (!this.playerDataService.groupExists("default")) {
×
102
            GroupData defaultData = new GroupData(NamedTextColor.GRAY, "Member ");
×
103
            try {
104
                this.playerDataService.saveGroupData(
×
105
                    "default",
106
                    defaultData);
107
                this.getLogger().info("Successfully created default group!");
×
108
            } catch (Exception e) {
×
109
                this.getSLF4JLogger().info("There was an error creating the default group", e);
×
110
            }
×
111
        }
112

113
        try {
114
            FileConfiguration permsFile = this.loadConfig("perms.yml");
×
115
            PlayerPermissionService playerPermissionService =
×
116
                new PlayerPermissionService(this, this.playerDataService);
117
            GroupPermissionService groupPermissionService = new GroupPermissionService();
×
118
            PermissionApplyService permissionApplyService =
×
119
                new PermissionApplyService(this, this.playerDataService, playerPermissionService, groupPermissionService);
120

121
            Set<String> keys = permsFile.getKeys(false);
×
122

123
            for (String key : keys) {
×
124
                ConfigurationSection section = permsFile.getConfigurationSection(key);
×
125
                Map<String, Object> sectionKeys = section.getValues(true);
×
126

127
                sectionKeys.forEach(
×
128
                    (k, v) -> {
129
                        if (v instanceof Boolean value) {
×
130
                            groupPermissionService.setPermission(key, k, value);
×
131
                        }
132
                    });
×
133
            }
×
134

135
            AddressWhitelistService addressWhitelistService = new AddressWhitelistService(mySqlRepository);
×
136

137
            this.getServer()
×
138
                .getPluginManager()
×
139
                .registerEvents(new PlayerQuitEventListener(playerPermissionService), this);
×
140
            this.getServer()
×
141
                .getPluginManager()
×
142
                .registerEvents(new PlayerLoginEventListener(this, permissionApplyService, proxyService, addressWhitelistService), this);
×
143

144
            this.getCommand("simplyrank")
×
145
                .setExecutor(new SimplyRankCommandExecutor(this.playerDataService, permissionApplyService));
×
146

147
            this.getCommand("vpn-whitelist")
×
148
                .setExecutor(new AddressWhitelistCommand(addressWhitelistService));
×
149
        } catch (IOException e) {
×
150
            this.getLogger().severe("Could not load perms.yml");
×
151
            e.printStackTrace();
×
152
        }
×
153

154
        @Nullable Plugin plugin = this.getServer().getPluginManager().getPlugin("MiniPlaceholders");
×
155
        if (plugin != null && plugin.isEnabled())
×
156
            new MiniPlaceholderRegister(this).register();
×
157
    }
×
158

159
    @Override
160
    public void onDisable() {
161
        instance = null;
×
NEW
162
        if (this.cleanerTask != null) this.cleanerTask.cancel();
×
163
        if (this.mySqlClient != null) {
×
164
            this.mySqlClient.close();
×
165
        }
166
    }
×
167

168
    public PlayerDataService getDataManager() {
169
        return this.playerDataService;
×
170
    }
171

172
    public static SimplyRankPlugin getInstance() {
173
        return instance;
×
174
    }
175

176
    private FileConfiguration loadConfig(String name) throws IOException {
177
        File dataFolder = this.getDataFolder();
×
178

179
        if (!dataFolder.exists()) {
×
180
            dataFolder.mkdirs();
×
181
        }
182

183
        File configFile = new File(dataFolder, name);
×
184

185
        if (!configFile.exists()) {
×
186
            Files.copy(this.getClassLoader().getResourceAsStream(name), configFile.toPath());
×
187
        }
188

189
        return YamlConfiguration.loadConfiguration(configFile);
×
190
    }
191

192
    private MySqlClient createSQLHandlerFromConfig(FileConfiguration config) {
193

194
        try {
195
            String url = config.getString("database.url");
×
196
            String password = config.getString("database.password");
×
197
            String user = config.getString("database.username");
×
198

199
            return new MySqlClient(url, user, password);
×
200

201
        } catch (NullPointerException e) {
×
202
            throw new DatabaseConnectionFailException();
×
203
        }
204
    }
205

206
    public static boolean isFolia() {
207
        try {
208
            Class.forName("io.papermc.paper.threadedregions.RegionizedServer");
×
209
            return true;
×
210
        } catch (ClassNotFoundException e) {
1✔
211
            return false;
1✔
212
        }
213
    }
214
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc