1+ package net .badlion .velocityapi ;
2+
3+ import com .google .gson .JsonObject ;
4+ import com .google .gson .JsonParser ;
5+ import net .badlion .modapicommon .AbstractBadlionApi ;
6+ import net .badlion .modapicommon .AbstractPluginMessageSender ;
7+ import net .badlion .modapicommon .Config ;
8+
9+ import java .io .File ;
10+ import java .io .FileNotFoundException ;
11+ import java .io .FileReader ;
12+ import java .io .FileWriter ;
13+ import java .io .IOException ;
14+ import java .io .Reader ;
15+ import java .nio .file .Files ;
16+ import java .nio .file .Path ;
17+ import java .nio .file .Paths ;
18+
19+ public class VelocityBadlionApi extends AbstractBadlionApi {
20+ private final VelocityBadlionPlugin plugin ;
21+ private Config config ;
22+
23+ public VelocityBadlionApi (VelocityBadlionPlugin plugin ) {
24+ this .plugin = plugin ;
25+ }
26+
27+ @ Override
28+ public void loadConfig (File file ) throws IOException {
29+ try (Reader reader = new FileReader (file )) {
30+ this .config = AbstractBadlionApi .GSON_NON_PRETTY .fromJson (reader , Config .class );
31+
32+ } catch (FileNotFoundException ex ) {
33+ this .plugin .getLogger ().info ("No Config Found: Saving default..." );
34+
35+ final Config config = new Config ();
36+ this .saveConfig (config , new File (this .plugin .getDataFolder (), "config.json" ));
37+
38+ this .config = config ;
39+ }
40+
41+ try {
42+ this .findOtherConfigs ();
43+
44+ } catch (Exception ex ) {
45+ this .plugin .getLogger ().warn ("Problem occurred trying to read old config: " + ex .getMessage (), ex );
46+ }
47+ }
48+
49+ private void findOtherConfigs () throws IOException {
50+ final Path cpsConfig = Paths .get ("plugins/BadlionClientCPSAPI/config.json" );
51+
52+ if (Files .exists (cpsConfig )) {
53+ this .plugin .getLogger ().info ("Found config file from old Badlion Client CPS API, attempting to import config..." );
54+
55+ JsonObject oldConfig = new JsonParser ().parse (new String (Files .readAllBytes (cpsConfig ))).getAsJsonObject ();
56+
57+ this .config .setClicksPerSecondLimitRight (oldConfig .get ("clicksPerSecondLimitRight" ).getAsInt ());
58+ this .config .setClicksPerSecondLimit (oldConfig .get ("clicksPerSecondLimit" ).getAsInt ());
59+
60+ this .saveConfig (this .config , new File (this .plugin .getDataFolder (), "config.json" ));
61+
62+ this .plugin .getLogger ().warn ("Imported config from cps api to new config, deleting old config..." );
63+ this .plugin .getLogger ().warn ("Since this version of the Badlion Mod API, the CPS API plugin is no longer necessary! Please delete it." );
64+
65+ Files .deleteIfExists (cpsConfig );
66+ }
67+ }
68+
69+ @ Override
70+ public void saveConfig (Config config , File file ) {
71+ try (FileWriter writer = new FileWriter (file )) {
72+ AbstractBadlionApi .GSON_PRETTY .toJson (config , writer );
73+
74+ } catch (Exception ex ) {
75+ this .plugin .getLogger ().warn ("Failed to save config: " + ex .getMessage (), ex );
76+ }
77+ }
78+
79+ @ Override
80+ public Config getBadlionConfig () {
81+ return this .config ;
82+ }
83+
84+ @ Override
85+ public AbstractPluginMessageSender getPluginMessageSender () {
86+ return this .plugin .getMessageSender ();
87+ }
88+ }
0 commit comments