Skip to content

Commit 64caac8

Browse files
committed
[Update/Feature] Use ChannelManager to control Yml loading./Remove Metrics code.
1 parent e6b277a commit 64caac8

4 files changed

Lines changed: 107 additions & 156 deletions

File tree

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<!-- Project information -->
55
<groupId>ca.q0r</groupId>
66
<artifactId>MChannels</artifactId>
7-
<version>1.6.4-R0.2</version>
7+
<version>1.6.4-R0.3</version>
88
<name>MChannels</name>
99
<url>http://mdev.in/</url>
1010
<description>MChat's Channel Implementation</description>

src/main/java/ca/q0r/mchannels/MChannels.java

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import ca.q0r.mchannels.channels.ChannelManager;
44
import ca.q0r.mchannels.commands.MChannelsCommand;
5-
import ca.q0r.mchannels.configs.ChannelUtil;
65
import ca.q0r.mchannels.events.ChannelListener;
76
import ca.q0r.mchat.util.MessageUtil;
87
import ca.q0r.mchat.util.Timer;
@@ -25,20 +24,6 @@ public void onEnable() {
2524
// Initialize and Start the Timer
2625
Timer timer = new Timer();
2726

28-
// Initialize Metrics
29-
/*getServer().getScheduler().runTaskLater(this, new BukkitRunnable(){
30-
@Override
31-
public void run() {
32-
try {
33-
Metrics metrics = new Metrics(Bukkit.getPluginManager().getPlugin("MChannels"));
34-
metrics.start();
35-
} catch (IOException ignored) {}
36-
}
37-
}, 200);*/
38-
39-
// Initialize Config
40-
ChannelUtil.initialize();
41-
4227
// Initialize Classes
4328
ChannelManager.initialize();
4429

@@ -67,9 +52,6 @@ public void onDisable() {
6752

6853
getServer().getScheduler().cancelTasks(this);
6954

70-
// Kill Config
71-
ChannelUtil.dispose();
72-
7355
// Stop the Timer
7456
timer.stop();
7557

src/main/java/ca/q0r/mchannels/channels/ChannelManager.java

Lines changed: 54 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
package ca.q0r.mchannels.channels;
22

3-
import ca.q0r.mchannels.configs.ChannelUtil;
43
import ca.q0r.mchannels.types.ChannelEditType;
54
import ca.q0r.mchannels.types.ChannelType;
65

76
import java.util.HashSet;
87
import java.util.Set;
98

109
public class ChannelManager {
11-
private static Set<Channel> channels = new HashSet<Channel>();
10+
private static Set<Channel> channels;
11+
private static ChannelYml yml;
1212

1313
public static void initialize() {
1414
channels = new HashSet<Channel>();
15+
yml = new ChannelYml();
1516

1617
loadChannels();
1718
}
@@ -23,17 +24,17 @@ public static Set<Channel> getChannels() {
2324
}
2425

2526
/**
26-
* Loads Channels from ChannelUtil to Memory.
27+
* Loads Channels from yml to Memory.
2728
*/
2829
public static void loadChannels() {
29-
for (String key : ChannelUtil.getConfig().getKeys(false)) {
30-
ChannelType type = ChannelType.fromName(ChannelUtil.getConfig().getString(key + ".type"));
31-
String prefix = ChannelUtil.getConfig().getString(key + ".prefix", "[");
32-
String suffix = ChannelUtil.getConfig().getString(key + ".suffix", "]");
33-
Boolean passworded = ChannelUtil.getConfig().getBoolean(key + ".passworded", false);
34-
String password = ChannelUtil.getConfig().getString(key + ".password", "");
35-
Integer distance = ChannelUtil.getConfig().getInt(key + ".distance", -1);
36-
Boolean defaulted = ChannelUtil.getConfig().getBoolean(key + ".default", false);
30+
for (String key : yml.getConfig().getKeys(false)) {
31+
ChannelType type = ChannelType.fromName(yml.getConfig().getString(key + ".type"));
32+
String prefix = yml.getConfig().getString(key + ".prefix", "[");
33+
String suffix = yml.getConfig().getString(key + ".suffix", "]");
34+
Boolean passworded = yml.getConfig().getBoolean(key + ".passworded", false);
35+
String password = yml.getConfig().getString(key + ".password", "");
36+
Integer distance = yml.getConfig().getInt(key + ".distance", -1);
37+
Boolean defaulted = yml.getConfig().getBoolean(key + ".default", false);
3738

3839
if (type == null) {
3940
type = ChannelType.GLOBAL;
@@ -47,41 +48,43 @@ public static void loadChannels() {
4748
* Loads a Channel from the config.
4849
*/
4950
public static void loadChannel(String name) {
50-
ChannelType type = ChannelType.fromName(ChannelUtil.getConfig().getString(name + ".type"));
51-
String prefix = ChannelUtil.getConfig().getString(name + ".prefix", "[");
52-
String suffix = ChannelUtil.getConfig().getString(name + ".suffix", "]");
53-
Boolean passworded = ChannelUtil.getConfig().getBoolean(name + ".passworded", false);
54-
String password = ChannelUtil.getConfig().getString(name + ".password", "");
55-
Integer distance = ChannelUtil.getConfig().getInt(name + ".distance", -1);
56-
Boolean defaulted = ChannelUtil.getConfig().getBoolean(name + ".default", false);
51+
ChannelType type = ChannelType.fromName(yml.getConfig().getString(name + ".type"));
52+
String prefix = yml.getConfig().getString(name + ".prefix", "[");
53+
String suffix = yml.getConfig().getString(name + ".suffix", "]");
54+
Boolean passworded = yml.getConfig().getBoolean(name + ".passworded", false);
55+
String password = yml.getConfig().getString(name + ".password", "");
56+
Integer distance = yml.getConfig().getInt(name + ".distance", -1);
57+
Boolean defaulted = yml.getConfig().getBoolean(name + ".default", false);
5758

5859
channels.add(new Channel(name.toLowerCase(), type, prefix, suffix, passworded, password, distance, defaulted));
5960
}
6061

6162

6263
/**
63-
* Reloads Channels from ChannelUtil to Memory.
64+
* Reloads Channels from yml to Memory.
6465
*/
6566
public static void reloadChannels() {
66-
for (String key : ChannelUtil.getConfig().getKeys(false)) {
67+
yml = new ChannelYml();
68+
69+
for (String key : yml.getConfig().getKeys(false)) {
6770
if (getChannel(key) != null) {
6871
Channel channel = getChannel(key);
6972

70-
channel.setType(ChannelType.fromName(ChannelUtil.getConfig().getString(key + ".type")));
71-
channel.setPrefix(ChannelUtil.getConfig().getString(key + ".prefix", "["));
72-
channel.setSuffix(ChannelUtil.getConfig().getString(key + ".suffix", "]"));
73-
channel.setPassword(ChannelUtil.getConfig().getString(key + ".password", ""));
74-
channel.setPassworded(ChannelUtil.getConfig().getBoolean(key + ".passworded", false), channel.getPassword());
75-
channel.setDistance(ChannelUtil.getConfig().getInt(key + ".distance", -1));
76-
channel.setDefault(ChannelUtil.getConfig().getBoolean(key + ".default", false));
73+
channel.setType(ChannelType.fromName(yml.getConfig().getString(key + ".type")));
74+
channel.setPrefix(yml.getConfig().getString(key + ".prefix", "["));
75+
channel.setSuffix(yml.getConfig().getString(key + ".suffix", "]"));
76+
channel.setPassword(yml.getConfig().getString(key + ".password", ""));
77+
channel.setPassworded(yml.getConfig().getBoolean(key + ".passworded", false), channel.getPassword());
78+
channel.setDistance(yml.getConfig().getInt(key + ".distance", -1));
79+
channel.setDefault(yml.getConfig().getBoolean(key + ".default", false));
7780
} else {
7881
loadChannel(key);
7982
}
8083
}
8184
}
8285

8386
/**
84-
* Loads Channels from ChannelUtil to Memory.
87+
* Loads Channels from yml to Memory.
8588
* @param name Name of Channel being created.
8689
* @param type Type of Channel being created.
8790
* @param prefix Prefix of Channel being created.
@@ -94,23 +97,23 @@ public static void reloadChannels() {
9497
public static void createChannel(String name, ChannelType type, String prefix, String suffix, Boolean passworded, String password, Integer distance, Boolean defaulted) {
9598
channels.add(new Channel(name.toLowerCase(), type, prefix, suffix, passworded, password, distance, defaulted));
9699

97-
ChannelUtil.getConfig().set(name.toLowerCase() + ".type", type.getName());
98-
ChannelUtil.getConfig().set(name.toLowerCase() + ".prefix", prefix);
99-
ChannelUtil.getConfig().set(name.toLowerCase() + ".suffix", suffix);
100-
ChannelUtil.getConfig().set(name.toLowerCase() + ".passworded", passworded);
101-
ChannelUtil.getConfig().set(name.toLowerCase() + ".password", password);
102-
ChannelUtil.getConfig().set(name.toLowerCase() + ".distance", distance);
103-
ChannelUtil.getConfig().set(name.toLowerCase() + ".default", defaulted);
100+
yml.getConfig().set(name.toLowerCase() + ".type", type.getName());
101+
yml.getConfig().set(name.toLowerCase() + ".prefix", prefix);
102+
yml.getConfig().set(name.toLowerCase() + ".suffix", suffix);
103+
yml.getConfig().set(name.toLowerCase() + ".passworded", passworded);
104+
yml.getConfig().set(name.toLowerCase() + ".password", password);
105+
yml.getConfig().set(name.toLowerCase() + ".distance", distance);
106+
yml.getConfig().set(name.toLowerCase() + ".default", defaulted);
104107

105108
if (defaulted) {
106109
setDefaultChannel(name);
107110
}
108111

109-
ChannelUtil.save();
112+
yml.save();
110113
}
111114

112115
/**
113-
* Removes a Channel from ChannelUtil/Memory.
116+
* Removes a Channel from yml/Memory.
114117
* @param name Name of Channel being removed.
115118
*/
116119
public static void removeChannel(String name) {
@@ -126,9 +129,9 @@ public static void removeChannel(String name) {
126129
if (isReal) {
127130
channels.remove(getChannel(name));
128131

129-
ChannelUtil.getConfig().set(name, null);
132+
yml.getConfig().set(name, null);
130133

131-
ChannelUtil.save();
134+
yml.save();
132135
}
133136
}
134137

@@ -148,7 +151,7 @@ public static Channel getChannel(String name) {
148151
}
149152

150153
/**
151-
* Saves all Channels in Memory to ChannelUtil.
154+
* Saves all Channels in Memory to yml.
152155
*/
153156
public static void saveChannels() {
154157
for (Channel channel : channels) {
@@ -161,15 +164,15 @@ public static void saveChannels() {
161164
* @param channel Channel being saved.
162165
*/
163166
public static void saveChannel(Channel channel) {
164-
ChannelUtil.getConfig().set(channel.getName().toLowerCase() + ".type", channel.getType().getName().toLowerCase());
165-
ChannelUtil.getConfig().set(channel.getName().toLowerCase() + ".prefix", channel.getPrefix());
166-
ChannelUtil.getConfig().set(channel.getName().toLowerCase() + ".suffix", channel.getSuffix());
167-
ChannelUtil.getConfig().set(channel.getName().toLowerCase() + ".passworded", channel.isPassworded());
168-
ChannelUtil.getConfig().set(channel.getName().toLowerCase() + ".password", channel.getPassword());
169-
ChannelUtil.getConfig().set(channel.getName().toLowerCase() + ".distance", channel.getDistance());
170-
ChannelUtil.getConfig().set(channel.getName().toLowerCase() + ".default", channel.isDefault());
171-
172-
ChannelUtil.save();
167+
yml.getConfig().set(channel.getName().toLowerCase() + ".type", channel.getType().getName().toLowerCase());
168+
yml.getConfig().set(channel.getName().toLowerCase() + ".prefix", channel.getPrefix());
169+
yml.getConfig().set(channel.getName().toLowerCase() + ".suffix", channel.getSuffix());
170+
yml.getConfig().set(channel.getName().toLowerCase() + ".passworded", channel.isPassworded());
171+
yml.getConfig().set(channel.getName().toLowerCase() + ".password", channel.getPassword());
172+
yml.getConfig().set(channel.getName().toLowerCase() + ".distance", channel.getDistance());
173+
yml.getConfig().set(channel.getName().toLowerCase() + ".default", channel.isDefault());
174+
175+
yml.save();
173176
}
174177

175178
/**
@@ -240,15 +243,15 @@ public static Set<Channel> getPlayersChannels(String player) {
240243
}
241244

242245
/**
243-
* Loads Channels from ChannelUtil to Memory.
246+
* Loads Channels from yml to Memory.
244247
* @param channel Name of Channel being edited.
245248
* @param type EditType being used.
246249
* @param option Option being used.
247250
*/
248251
public static void editChannel(Channel channel, ChannelEditType type, Object option) {
249252
if (option.getClass() == type.getOptionClass()) {
250253
if (type.getName().equalsIgnoreCase("name")) {
251-
ChannelUtil.getConfig().set(channel.getName(), null);
254+
yml.getConfig().set(channel.getName(), null);
252255

253256
channel.setName((String) option);
254257
} else if (type.getName().equalsIgnoreCase("default")) {

src/main/java/ca/q0r/mchannels/configs/ChannelUtil.java renamed to src/main/java/ca/q0r/mchannels/channels/ChannelYml.java

Lines changed: 52 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,52 @@
1-
package ca.q0r.mchannels.configs;
2-
3-
import org.bukkit.configuration.file.YamlConfiguration;
4-
5-
import java.io.File;
6-
7-
public class ChannelUtil {
8-
private static YamlConfiguration config;
9-
private static File file;
10-
11-
public static void initialize() {
12-
load();
13-
}
14-
15-
public static void dispose() {
16-
config = null;
17-
file = null;
18-
}
19-
20-
public static void load() {
21-
file = new File("plugins/MChannels/channels.yml");
22-
23-
config = YamlConfiguration.loadConfiguration(file);
24-
25-
config.options().indent(4);
26-
config.options().header("MChat Channels");
27-
28-
if (!file.exists()) {
29-
loadDefaults();
30-
}
31-
}
32-
33-
private static void loadDefaults() {
34-
set("global.prefix", "[");
35-
set("global.suffix", "]");
36-
set("global.type", "global");
37-
set("global.distance", 0);
38-
set("global.default", true);
39-
set("global.prefix", "[");
40-
set("global.suffix", "]");
41-
set("global.type", "global");
42-
set("global.distance", 60);
43-
set("global.default", false);
44-
set("private.prefix", "[");
45-
set("private.suffix", "]");
46-
set("private.type", "private");
47-
set("private.distance", 0);
48-
set("private.default", false);
49-
set("world.prefix", "[");
50-
set("world.suffix", "]");
51-
set("world.type", "world");
52-
set("world.distance", 0);
53-
set("world.default", false);
54-
set("chunk.prefix", "[");
55-
set("chunk.suffix", "]");
56-
set("chunk.type", "chunk");
57-
set("chunk.distance", 5);
58-
set("chunk.default", false);
59-
set("password.prefix", "[");
60-
set("password.suffix", "]");
61-
set("password.type", "password");
62-
set("password.distance", 0);
63-
set("password.password", "hello");
64-
set("password.passworded", true);
65-
set("password.default", false);
66-
67-
save();
68-
}
69-
70-
public static void set(String key, Object obj) {
71-
config.set(key, obj);
72-
}
73-
74-
public static Boolean save() {
75-
try {
76-
config.save(file);
77-
return true;
78-
} catch (Exception ignored) {
79-
return false;
80-
}
81-
}
82-
83-
public static YamlConfiguration getConfig() {
84-
return config;
85-
}
86-
}
1+
package ca.q0r.mchannels.channels;
2+
3+
import ca.q0r.mchat.configs.Yml;
4+
5+
import java.io.File;
6+
7+
public class ChannelYml extends Yml {
8+
public ChannelYml() {
9+
super(new File("plugins/MChannels/channels.yml"), "MChat Channels");
10+
11+
if (!file.exists()) {
12+
loadDefaults();
13+
}
14+
}
15+
16+
public void loadDefaults() {
17+
set("global.prefix", "[");
18+
set("global.suffix", "]");
19+
set("global.type", "global");
20+
set("global.distance", 0);
21+
set("global.default", true);
22+
set("global.prefix", "[");
23+
set("global.suffix", "]");
24+
set("global.type", "global");
25+
set("global.distance", 60);
26+
set("global.default", false);
27+
set("private.prefix", "[");
28+
set("private.suffix", "]");
29+
set("private.type", "private");
30+
set("private.distance", 0);
31+
set("private.default", false);
32+
set("world.prefix", "[");
33+
set("world.suffix", "]");
34+
set("world.type", "world");
35+
set("world.distance", 0);
36+
set("world.default", false);
37+
set("chunk.prefix", "[");
38+
set("chunk.suffix", "]");
39+
set("chunk.type", "chunk");
40+
set("chunk.distance", 5);
41+
set("chunk.default", false);
42+
set("password.prefix", "[");
43+
set("password.suffix", "]");
44+
set("password.type", "password");
45+
set("password.distance", 0);
46+
set("password.password", "hello");
47+
set("password.passworded", true);
48+
set("password.default", false);
49+
50+
save();
51+
}
52+
}

0 commit comments

Comments
 (0)