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

SimplyVanilla / SimplyRank / #47

09 Mar 2024 04:48PM UTC coverage: 18.514% (-0.07%) from 18.582%
#47

Pull #96

github

web-flow
Merge c8d17f250 into fb98f1513
Pull Request #96: Removed PlaceholderAPI dependency

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

1 existing line in 1 file now uncovered.

152 of 821 relevant lines covered (18.51%)

0.19 hits per line

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

2.08
/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.placeholder.ScoreboardTeamsPlaceholderExtension;
24
import net.simplyvanilla.simplyrank.placeholder.SimplyRankPlaceholderExpansion;
25
import net.simplyvanilla.simplyrank.proxy.ProxyService;
26
import net.simplyvanilla.simplyrank.proxy.ProxyTtlCleanupTask;
27
import net.simplyvanilla.simplyrank.proxy.provider.ProxyCheckProvider;
28
import org.bukkit.Bukkit;
29
import org.bukkit.configuration.ConfigurationSection;
30
import org.bukkit.configuration.file.FileConfiguration;
31
import org.bukkit.configuration.file.YamlConfiguration;
32
import org.bukkit.plugin.Plugin;
33
import org.bukkit.plugin.java.JavaPlugin;
34
import org.jetbrains.annotations.Nullable;
35

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

44
public class SimplyRankPlugin extends JavaPlugin {
×
45

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

49
    private MySqlClient mySqlClient = null;
×
50

51

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

NEW
153
        @Nullable Plugin plugin = this.getServer().getPluginManager().getPlugin("PlaceholderAPI");
×
NEW
154
        if (plugin != null && plugin.isEnabled()) {
×
155
            new SimplyRankPlaceholderExpansion().register();
×
156
            new ScoreboardTeamsPlaceholderExtension().register();
×
157
        }
NEW
158
        plugin = this.getServer().getPluginManager().getPlugin("MiniPlaceholders");
×
NEW
159
        if (plugin != null && plugin.isEnabled())
×
NEW
160
            new MiniPlaceholderRegister(this).register();
×
UNCOV
161
    }
×
162

163
    @Override
164
    public void onDisable() {
165
        instance = null;
×
166
        if (this.mySqlClient != null) {
×
167
            this.mySqlClient.close();
×
168
        }
169
    }
×
170

171
    public PlayerDataService getDataManager() {
172
        return this.playerDataService;
×
173
    }
174

175
    public static SimplyRankPlugin getInstance() {
176
        return instance;
×
177
    }
178

179
    private FileConfiguration loadConfig(String name) throws IOException {
180
        File dataFolder = this.getDataFolder();
×
181

182
        if (!dataFolder.exists()) {
×
183
            dataFolder.mkdirs();
×
184
        }
185

186
        File configFile = new File(dataFolder, name);
×
187

188
        if (!configFile.exists()) {
×
189
            Files.copy(this.getClassLoader().getResourceAsStream(name), configFile.toPath());
×
190
        }
191

192
        return YamlConfiguration.loadConfiguration(configFile);
×
193
    }
194

195
    private MySqlClient createSQLHandlerFromConfig(FileConfiguration config) {
196

197
        try {
198
            String url = config.getString("database.url");
×
199
            String password = config.getString("database.password");
×
200
            String user = config.getString("database.username");
×
201

202
            return new MySqlClient(url, user, password);
×
203

204
        } catch (NullPointerException e) {
×
205
            throw new DatabaseConnectionFailException();
×
206
        }
207
    }
208

209
    public static boolean isFolia() {
210
        try {
211
            Class.forName("io.papermc.paper.threadedregions.RegionizedServer");
×
212
            return true;
×
213
        } catch (ClassNotFoundException e) {
1✔
214
            return false;
1✔
215
        }
216
    }
217
}
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