Skip to content

Commit 3eea82e

Browse files
v2.2
1 parent 54e0cd2 commit 3eea82e

4 files changed

Lines changed: 12 additions & 264 deletions

File tree

‎src/main/java/com/vortex/blackjack/BlackjackPlugin.java‎

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -80,22 +80,7 @@ public void onEnable() {
8080
messagesConfig = YamlConfiguration.loadConfiguration(messagesFile);
8181
}
8282

83-
configManager = new ConfigManager(this, getConfig(), messagesConfig);
84-
85-
// Migrate and validate configuration files
86-
getLogger().info("Checking configuration files for updates...");
87-
if (configManager.migrateConfiguration()) {
88-
// Save the updated configurations
89-
try {
90-
saveConfig();
91-
messagesConfig.save(messagesFile);
92-
getLogger().info("Configuration files updated with new settings!");
93-
} catch (IOException e) {
94-
getLogger().warning("Failed to save updated configuration files: " + e.getMessage());
95-
}
96-
} else {
97-
getLogger().info("Configuration files are up to date.");
98-
}
83+
configManager = new ConfigManager(getConfig(), messagesConfig);
9984

10085
// Initialize core managers
10186
tableManager = new TableManager(this, configManager);
@@ -642,20 +627,7 @@ private boolean handleReload(Player player) {
642627
FileConfiguration messagesConfig = YamlConfiguration.loadConfiguration(messagesFile);
643628

644629
configManager.reload(getConfig(), messagesConfig);
645-
646-
// Run migration to add any new config options
647-
if (configManager.migrateConfiguration()) {
648-
try {
649-
saveConfig();
650-
messagesConfig.save(messagesFile);
651-
player.sendMessage(configManager.getMessage("config-reloaded") + " &7(Updated with new settings)");
652-
} catch (IOException e) {
653-
getLogger().warning("Failed to save updated configuration files during reload: " + e.getMessage());
654-
player.sendMessage(configManager.getMessage("config-reloaded"));
655-
}
656-
} else {
657-
player.sendMessage(configManager.getMessage("config-reloaded"));
658-
}
630+
player.sendMessage(configManager.getMessage("config-reloaded"));
659631
return true;
660632
}
661633

‎src/main/java/com/vortex/blackjack/config/ConfigManager.java‎

Lines changed: 2 additions & 232 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,11 @@
77
import org.bukkit.Registry;
88
import org.bukkit.Sound;
99
import org.bukkit.configuration.file.FileConfiguration;
10-
import org.bukkit.plugin.java.JavaPlugin;
1110

1211
/**
1312
* Centralized configuration management with validation and caching
1413
*/
1514
public class ConfigManager {
16-
private final JavaPlugin plugin;
1715
private FileConfiguration config;
1816
private FileConfiguration messagesConfig;
1917

@@ -29,238 +27,19 @@ public class ConfigManager {
2927
private boolean particlesEnabled;
3028
private boolean hitSoft17;
3129

32-
public ConfigManager(JavaPlugin plugin, FileConfiguration config, FileConfiguration messagesConfig) {
33-
this.plugin = plugin;
30+
public ConfigManager(FileConfiguration config, FileConfiguration messagesConfig) {
3431
this.config = config;
3532
this.messagesConfig = messagesConfig;
3633
loadAndValidateConfig();
3734
}
3835

3936
// Backward compatibility constructor
40-
public ConfigManager(JavaPlugin plugin, FileConfiguration config) {
41-
this.plugin = plugin;
37+
public ConfigManager(FileConfiguration config) {
4238
this.config = config;
4339
this.messagesConfig = config; // Use main config for messages if no separate messages config
4440
loadAndValidateConfig();
4541
}
4642

47-
/**
48-
* Migrates and validates configuration files by adding missing keys and removing obsolete ones
49-
*/
50-
public boolean migrateConfiguration() {
51-
boolean configChanged = false;
52-
boolean messagesChanged = false;
53-
54-
// Migrate main config
55-
configChanged = migrateMainConfig();
56-
57-
// Migrate messages config
58-
messagesChanged = migrateMessagesConfig();
59-
60-
return configChanged || messagesChanged;
61-
}
62-
63-
private boolean migrateMainConfig() {
64-
boolean changed = false;
65-
int addedKeys = 0;
66-
int removedKeys = 0;
67-
68-
try {
69-
// Load the latest config from GitHub repository
70-
String githubConfigUrl = "https://raw.githubusercontent.com/DefectiveVortex/Blackjack/main/src/main/resources/config.yml";
71-
org.bukkit.configuration.file.YamlConfiguration defaultConfig = loadConfigFromUrl(githubConfigUrl);
72-
73-
if (defaultConfig == null) {
74-
plugin.getLogger().warning("Failed to load default config from GitHub, falling back to JAR resources");
75-
// Fallback to JAR resources
76-
java.io.InputStream defaultConfigStream = getClass().getClassLoader().getResourceAsStream("config.yml");
77-
if (defaultConfigStream == null) {
78-
plugin.getLogger().warning("No default config.yml found in resources");
79-
return false;
80-
}
81-
defaultConfig = org.bukkit.configuration.file.YamlConfiguration.loadConfiguration(
82-
new java.io.InputStreamReader(defaultConfigStream, java.nio.charset.StandardCharsets.UTF_8)
83-
);
84-
defaultConfigStream.close();
85-
}
86-
87-
// Get all keys from default config
88-
java.util.Set<String> defaultKeys = getAllKeys(defaultConfig);
89-
java.util.Set<String> currentKeys = getAllKeys(config);
90-
91-
// Protected keys that should never be removed (runtime data)
92-
java.util.Set<String> protectedKeys = new java.util.HashSet<>();
93-
protectedKeys.add("tables");
94-
protectedKeys.add("tables.world");
95-
96-
// Add missing keys from default config
97-
for (String key : defaultKeys) {
98-
if (!config.contains(key)) {
99-
Object value = defaultConfig.get(key);
100-
config.set(key, value);
101-
addedKeys++;
102-
changed = true;
103-
plugin.getLogger().info("Added missing config key: " + key);
104-
}
105-
}
106-
107-
// Remove obsolete keys (keys that exist in current but not in default)
108-
// BUT preserve protected keys that contain runtime data
109-
for (String key : currentKeys) {
110-
if (!defaultKeys.contains(key)) {
111-
// Check if this key or any parent key is protected
112-
boolean isProtected = false;
113-
for (String protectedKey : protectedKeys) {
114-
if (key.equals(protectedKey) || key.startsWith(protectedKey + ".")) {
115-
isProtected = true;
116-
break;
117-
}
118-
}
119-
120-
if (!isProtected) {
121-
config.set(key, null);
122-
removedKeys++;
123-
changed = true;
124-
plugin.getLogger().info("Removed obsolete config key: " + key);
125-
} else {
126-
plugin.getLogger().info("Preserved protected key: " + key);
127-
}
128-
}
129-
}
130-
131-
if (changed) {
132-
plugin.getLogger().info("Config migration: Added " + addedKeys + " keys, removed " + removedKeys + " keys");
133-
} else {
134-
plugin.getLogger().info("Config migration: No changes needed");
135-
}
136-
137-
} catch (Exception e) {
138-
// If something goes wrong, log it but don't fail the migration
139-
plugin.getLogger().severe("Error during config migration: " + e.getMessage());
140-
e.printStackTrace();
141-
}
142-
143-
return changed;
144-
}
145-
146-
private boolean migrateMessagesConfig() {
147-
boolean changed = false;
148-
int addedKeys = 0;
149-
int removedKeys = 0;
150-
151-
try {
152-
// Load the latest messages from GitHub repository
153-
String githubMessagesUrl = "https://raw.githubusercontent.com/DefectiveVortex/Blackjack/main/src/main/resources/messages.yml";
154-
org.bukkit.configuration.file.YamlConfiguration defaultMessages = loadConfigFromUrl(githubMessagesUrl);
155-
156-
if (defaultMessages == null) {
157-
plugin.getLogger().warning("Failed to load default messages from GitHub, falling back to JAR resources");
158-
// Fallback to JAR resources
159-
java.io.InputStream defaultMessagesStream = getClass().getClassLoader().getResourceAsStream("messages.yml");
160-
if (defaultMessagesStream == null) {
161-
plugin.getLogger().warning("No default messages.yml found in resources");
162-
return false;
163-
}
164-
defaultMessages = org.bukkit.configuration.file.YamlConfiguration.loadConfiguration(
165-
new java.io.InputStreamReader(defaultMessagesStream, java.nio.charset.StandardCharsets.UTF_8)
166-
);
167-
defaultMessagesStream.close();
168-
}
169-
170-
// Get all keys from default messages
171-
java.util.Set<String> defaultKeys = getAllKeys(defaultMessages);
172-
java.util.Set<String> currentKeys = getAllKeys(messagesConfig);
173-
174-
// Add missing keys from default messages
175-
for (String key : defaultKeys) {
176-
if (!messagesConfig.contains(key)) {
177-
Object value = defaultMessages.get(key);
178-
messagesConfig.set(key, value);
179-
addedKeys++;
180-
changed = true;
181-
plugin.getLogger().info("Added missing message key: " + key);
182-
}
183-
}
184-
185-
// Remove obsolete keys (keys that exist in current but not in default)
186-
for (String key : currentKeys) {
187-
if (!defaultKeys.contains(key)) {
188-
messagesConfig.set(key, null);
189-
removedKeys++;
190-
changed = true;
191-
plugin.getLogger().info("Removed obsolete message key: " + key);
192-
}
193-
}
194-
195-
if (changed) {
196-
plugin.getLogger().info("Messages migration: Added " + addedKeys + " keys, removed " + removedKeys + " keys");
197-
} else {
198-
plugin.getLogger().info("Messages migration: No changes needed");
199-
}
200-
201-
} catch (Exception e) {
202-
// If something goes wrong, log it but don't fail the migration
203-
plugin.getLogger().severe("Error during messages migration: " + e.getMessage());
204-
e.printStackTrace();
205-
}
206-
207-
return changed;
208-
}
209-
210-
/**
211-
* Loads a YAML configuration from a URL (like GitHub raw file)
212-
*/
213-
private org.bukkit.configuration.file.YamlConfiguration loadConfigFromUrl(String url) {
214-
try {
215-
java.net.URI githubUri = java.net.URI.create(url);
216-
java.net.HttpURLConnection connection = (java.net.HttpURLConnection) githubUri.toURL().openConnection();
217-
connection.setRequestMethod("GET");
218-
connection.setConnectTimeout(5000); // 5 second timeout
219-
connection.setReadTimeout(10000); // 10 second read timeout
220-
221-
int responseCode = connection.getResponseCode();
222-
if (responseCode == 200) {
223-
try (java.io.InputStream inputStream = connection.getInputStream();
224-
java.io.InputStreamReader reader = new java.io.InputStreamReader(inputStream, java.nio.charset.StandardCharsets.UTF_8)) {
225-
226-
return org.bukkit.configuration.file.YamlConfiguration.loadConfiguration(reader);
227-
}
228-
} else {
229-
plugin.getLogger().warning("Failed to fetch config from GitHub: HTTP " + responseCode);
230-
return null;
231-
}
232-
} catch (Exception e) {
233-
plugin.getLogger().warning("Error fetching config from GitHub: " + e.getMessage());
234-
return null;
235-
}
236-
}
237-
238-
/**
239-
* Recursively gets all keys from a configuration file
240-
*/
241-
private java.util.Set<String> getAllKeys(org.bukkit.configuration.file.FileConfiguration config) {
242-
java.util.Set<String> keys = new java.util.HashSet<>();
243-
getAllKeysRecursive(config, "", keys);
244-
return keys;
245-
}
246-
247-
/**
248-
* Helper method to recursively collect all keys from a configuration section
249-
*/
250-
private void getAllKeysRecursive(org.bukkit.configuration.ConfigurationSection section, String prefix, java.util.Set<String> keys) {
251-
for (String key : section.getKeys(false)) {
252-
String fullKey = prefix.isEmpty() ? key : prefix + "." + key;
253-
254-
if (section.isConfigurationSection(key)) {
255-
// If it's a section, recurse into it
256-
getAllKeysRecursive(section.getConfigurationSection(key), fullKey, keys);
257-
} else {
258-
// If it's a value, add the key
259-
keys.add(fullKey);
260-
}
261-
}
262-
}
263-
26443
private void loadAndValidateConfig() {
26544
// Betting settings
26645
minBet = Math.max(1, config.getInt("betting.min-bet", 10));
@@ -459,13 +238,4 @@ public int getStatsSaveInterval() {
459238
public boolean shouldRefundOnLeave() {
460239
return config.getBoolean("game-settings.refund-on-leave", true);
461240
}
462-
463-
// Getters for configuration access (for saving during migration)
464-
public FileConfiguration getConfig() {
465-
return config;
466-
}
467-
468-
public FileConfiguration getMessagesConfig() {
469-
return messagesConfig;
470-
}
471241
}

‎src/main/java/com/vortex/blackjack/table/BlackjackTable.java‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@
2020
import org.joml.AxisAngle4f;
2121
import org.joml.Vector3f;
2222
import java.math.BigDecimal;
23-
import java.util.*;
23+
import java.util.ArrayList;
24+
import java.util.HashMap;
25+
import java.util.HashSet;
26+
import java.util.List;
27+
import java.util.Map;
28+
import java.util.Set;
29+
import java.util.UUID;
2430
import java.util.concurrent.ConcurrentHashMap;
2531

2632
/**

‎src/main/resources/config.yml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
betting:
33
min-bet: 10
44
max-bet: 10000
5-
cooldown-ms: 1000
5+
cooldown-ms: 2000
66

77
# Quick Bet Options (shown in chat when betting)
88
quick-bets:

0 commit comments

Comments
 (0)