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

SimplyVanilla / SimplyRank / #50

09 Mar 2024 06:51PM UTC coverage: 19.974% (+1.5%) from 18.514%
#50

push

github

web-flow
Bump 0.8.4 (#97)

0 of 1 new or added line in 1 file covered. (0.0%)

152 of 761 relevant lines covered (19.97%)

0.2 hits per line

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

2.17
/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 net.kyori.adventure.text.format.NamedTextColor;
7
import net.kyori.adventure.text.format.TextColor;
8
import net.simplyvanilla.simplyrank.addresswhitelist.AddressWhitelistService;
9
import net.simplyvanilla.simplyrank.command.SimplyRankCommandExecutor;
10
import net.simplyvanilla.simplyrank.command.address.AddressWhitelistCommand;
11
import net.simplyvanilla.simplyrank.database.exception.DatabaseConnectionFailException;
12
import net.simplyvanilla.simplyrank.database.group.GroupData;
13
import net.simplyvanilla.simplyrank.database.sql.MySqlClient;
14
import net.simplyvanilla.simplyrank.database.sql.MySqlRepository;
15
import net.simplyvanilla.simplyrank.gson.TextColorGsonDeserializer;
16
import net.simplyvanilla.simplyrank.listener.PlayerLoginEventListener;
17
import net.simplyvanilla.simplyrank.listener.PlayerQuitEventListener;
18
import net.simplyvanilla.simplyrank.permission.GroupPermissionService;
19
import net.simplyvanilla.simplyrank.permission.PermissionApplyService;
20
import net.simplyvanilla.simplyrank.permission.PlayerDataService;
21
import net.simplyvanilla.simplyrank.permission.PlayerPermissionService;
22
import net.simplyvanilla.simplyrank.placeholder.MiniPlaceholderRegister;
23
import net.simplyvanilla.simplyrank.proxy.ProxyService;
24
import net.simplyvanilla.simplyrank.proxy.ProxyTtlCleanupTask;
25
import net.simplyvanilla.simplyrank.proxy.provider.ProxyCheckProvider;
26
import org.bukkit.Bukkit;
27
import org.bukkit.configuration.ConfigurationSection;
28
import org.bukkit.configuration.file.FileConfiguration;
29
import org.bukkit.configuration.file.YamlConfiguration;
30
import org.bukkit.plugin.Plugin;
31
import org.bukkit.plugin.java.JavaPlugin;
32
import org.jetbrains.annotations.Nullable;
33

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

42
public class SimplyRankPlugin extends JavaPlugin {
×
43

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

47
    private MySqlClient mySqlClient = null;
×
48

49

50
    @Override
51
    public void onEnable() {
52
        ProxyService proxyService;
53
        MySqlRepository mySqlRepository;
54
        FileConfiguration config;
55
        instance = this;
×
56

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

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

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

85
        mySqlRepository = new MySqlRepository(this.mySqlClient, gson);
×
86

87
        File dataFolder = this.getDataFolder();
×
88

89
        if (!dataFolder.exists()) {
×
90
            dataFolder.mkdirs();
×
91
        }
92

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

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

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

118
            Set<String> keys = permsFile.getKeys(false);
×
119

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

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

132
            AddressWhitelistService addressWhitelistService = new AddressWhitelistService(mySqlRepository);
×
133

134
            this.getServer()
×
135
                .getPluginManager()
×
136
                .registerEvents(new PlayerQuitEventListener(playerPermissionService), this);
×
137
            this.getServer()
×
138
                .getPluginManager()
×
139
                .registerEvents(new PlayerLoginEventListener(this, permissionApplyService, proxyService, addressWhitelistService), this);
×
140

141
            this.getCommand("simplyrank")
×
142
                .setExecutor(new SimplyRankCommandExecutor(this.playerDataService, permissionApplyService));
×
143

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

NEW
151
        @Nullable Plugin plugin = this.getServer().getPluginManager().getPlugin("MiniPlaceholders");
×
152
        if (plugin != null && plugin.isEnabled())
×
153
            new MiniPlaceholderRegister(this).register();
×
154
    }
×
155

156
    @Override
157
    public void onDisable() {
158
        instance = null;
×
159
        if (this.mySqlClient != null) {
×
160
            this.mySqlClient.close();
×
161
        }
162
    }
×
163

164
    public PlayerDataService getDataManager() {
165
        return this.playerDataService;
×
166
    }
167

168
    public static SimplyRankPlugin getInstance() {
169
        return instance;
×
170
    }
171

172
    private FileConfiguration loadConfig(String name) throws IOException {
173
        File dataFolder = this.getDataFolder();
×
174

175
        if (!dataFolder.exists()) {
×
176
            dataFolder.mkdirs();
×
177
        }
178

179
        File configFile = new File(dataFolder, name);
×
180

181
        if (!configFile.exists()) {
×
182
            Files.copy(this.getClassLoader().getResourceAsStream(name), configFile.toPath());
×
183
        }
184

185
        return YamlConfiguration.loadConfiguration(configFile);
×
186
    }
187

188
    private MySqlClient createSQLHandlerFromConfig(FileConfiguration config) {
189

190
        try {
191
            String url = config.getString("database.url");
×
192
            String password = config.getString("database.password");
×
193
            String user = config.getString("database.username");
×
194

195
            return new MySqlClient(url, user, password);
×
196

197
        } catch (NullPointerException e) {
×
198
            throw new DatabaseConnectionFailException();
×
199
        }
200
    }
201

202
    public static boolean isFolia() {
203
        try {
204
            Class.forName("io.papermc.paper.threadedregions.RegionizedServer");
×
205
            return true;
×
206
        } catch (ClassNotFoundException e) {
1✔
207
            return false;
1✔
208
        }
209
    }
210
}
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

© 2026 Coveralls, Inc