Skip to content

Commit 1ee506d

Browse files
committed
Use the new configuration system
1 parent b4cb38a commit 1ee506d

File tree

7 files changed

+81
-117
lines changed

7 files changed

+81
-117
lines changed

base/src/main/java/net/techcable/spawnshield/BlockMode.java

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,9 @@
2222
*/
2323
package net.techcable.spawnshield;
2424

25-
import lombok.RequiredArgsConstructor;
26-
import net.techcable.techutils.yamler.InternalConverter;
27-
import net.techcable.techutils.yamler.converter.Converter;
25+
import net.techcable.techutils.config.ConfigSerializer;
2826

29-
import java.lang.reflect.ParameterizedType;
27+
import org.bukkit.configuration.InvalidConfigurationException;
3028

3129
public enum BlockMode {
3230
TELEPORT,
@@ -38,30 +36,28 @@ public String toString() {
3836
return name().toLowerCase();
3937
}
4038

41-
public static class BlockModeConverter implements Converter {
42-
public BlockModeConverter(InternalConverter internalConverter) {
43-
//I don't know what to do with an internal converter
44-
}
39+
public static class BlockModeConverter implements ConfigSerializer<BlockMode> {
40+
4541

4642
@Override
47-
public Object toConfig(Class<?> aClass, Object o, ParameterizedType parameterizedType) throws Exception {
48-
BlockMode mode = (BlockMode) o;
49-
return o.toString();
43+
public Object serialize(BlockMode blockMode) {
44+
return blockMode.toString();
5045
}
5146

5247
@Override
53-
public Object fromConfig(Class<?> aClass, Object o, ParameterizedType parameterizedType) throws Exception {
54-
String name = o.toString();
48+
public BlockMode deserialize(Object o, Class<? extends BlockMode> aClass) throws InvalidConfigurationException {
49+
if (!(o instanceof String)) throw new InvalidConfigurationException("block mode must be a string");
50+
String name = (String) o;
5551
try {
5652
return BlockMode.valueOf(name.toUpperCase());
5753
} catch (IllegalArgumentException e) {
58-
throw new RuntimeException(name + " is not a valid mode for SpawnShield");
54+
throw new InvalidConfigurationException(name + " is not a valid mode for SpawnShield");
5955
}
6056
}
6157

6258
@Override
63-
public boolean supports(Class<?> aClass) {
64-
return BlockMode.class.isAssignableFrom(aClass);
59+
public boolean canHandle(Class<?> aClass) {
60+
return BlockMode.class.equals(aClass);
6561
}
6662
}
6763
}

base/src/main/java/net/techcable/spawnshield/SpawnShield.java

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,10 @@
3333
import net.techcable.spawnshield.tasks.TeleportSafezoningTask;
3434
import net.techcable.techutils.TechPlugin;
3535
import org.bukkit.Bukkit;
36+
import org.bukkit.configuration.InvalidConfigurationException;
3637
import org.bukkit.plugin.java.JavaPlugin;
3738

39+
import java.io.File;
3840
import java.io.IOException;
3941
import java.util.UUID;
4042

@@ -59,10 +61,16 @@ protected void startup() {
5961
setEnabled(false);
6062
return;
6163
}
62-
settings = new SpawnShieldConfig(this);
63-
messages = new SpawnShieldMessages(this);
64-
settings.init();
65-
messages.init();
64+
settings = new SpawnShieldConfig();
65+
messages = new SpawnShieldMessages();
66+
try {
67+
settings.load(new File(getDataFolder(), "config.yml"), SpawnShield.class.getResource("/config.yml"));
68+
messages.load(new File(getDataFolder(), "messages.yml"), SpawnShield.class.getResource("/messages.yml"));
69+
} catch (IOException | InvalidConfigurationException e) {
70+
Utils.severe("Disabling, Unable to load configuration files", e);
71+
setEnabled(false);
72+
return;
73+
}
6674
try {
6775
Metrics metrics = new Metrics(this);
6876
Metrics.Graph mode = metrics.createGraph("Mode");
@@ -138,7 +146,12 @@ public void clearRequest(UUID id) {
138146

139147
@Override
140148
protected void shutdown() {
141-
getSettings().save();
149+
try {
150+
getSettings().save(new File(getDataFolder(), "config.yml"), SpawnShield.class.getResource("/config.yml"));
151+
getMessages().save(new File(getDataFolder(), "messages.yml"), SpawnShield.class.getResource("/messages.yml"));
152+
} catch (InvalidConfigurationException | IOException e) {
153+
Utils.severe("Unable to save configuration", e);
154+
}
142155
}
143156

144157
@Override

base/src/main/java/net/techcable/spawnshield/Utils.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
import java.lang.reflect.Constructor;
3636
import java.util.Collection;
37+
import java.util.logging.Level;
3738

3839
@NoArgsConstructor(access = AccessLevel.PRIVATE)
3940
public class Utils {
@@ -71,6 +72,10 @@ public static void severe(String error) {
7172
Bukkit.getLogger().severe("[SpawnShield] " + error);
7273
}
7374

75+
public static void severe(String error, Throwable t) {
76+
Bukkit.getLogger().log(Level.SEVERE, "[SpawnShield] " + error, t);
77+
}
78+
7479
public static void warning(String error) {
7580
Bukkit.getLogger().warning("[SpawnShield] " + error);
7681
}

base/src/main/java/net/techcable/spawnshield/config/SpawnShieldConfig.java

Lines changed: 13 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@
2929
import lombok.Getter;
3030
import lombok.Synchronized;
3131
import net.techcable.spawnshield.BlockMode;
32+
import net.techcable.spawnshield.BlockMode.BlockModeConverter;
3233
import net.techcable.spawnshield.SpawnShield;
3334
import net.techcable.spawnshield.Utils;
3435
import net.techcable.spawnshield.compat.ProtectionPlugin;
3536
import net.techcable.spawnshield.compat.Region;
3637
import net.techcable.techutils.collect.Pair;
37-
import net.techcable.techutils.yamler.Comments;
38-
import net.techcable.techutils.yamler.Config;
39-
import net.techcable.techutils.yamler.InvalidConfigurationException;
40-
import net.techcable.techutils.yamler.InvalidConverterException;
38+
import net.techcable.techutils.config.AnnotationConfig;
39+
import net.techcable.techutils.config.Setting;
40+
4141
import org.bukkit.Bukkit;
4242
import org.bukkit.World;
4343

@@ -49,47 +49,9 @@
4949
import java.util.concurrent.ConcurrentHashMap;
5050

5151
@Getter
52-
public class SpawnShieldConfig extends Config {
53-
public SpawnShieldConfig(SpawnShield plugin) {
54-
CONFIG_FILE = new File(plugin.getDataFolder(), "config.yml");
55-
try {
56-
addConverter(BlockMode.BlockModeConverter.class);
57-
} catch (InvalidConverterException e) {
58-
throw Throwables.propagate(e);
59-
}
60-
}
61-
62-
@Override
63-
@Synchronized("lock")
64-
public void init() {
65-
Utils.assertMainThread();
66-
try {
67-
super.init();
68-
} catch (InvalidConfigurationException e) {
69-
throw Throwables.propagate(e);
70-
}
71-
}
72-
73-
@Override
74-
@Synchronized("lock")
75-
public void load() {
76-
Utils.assertMainThread();
77-
try {
78-
super.load();
79-
} catch (InvalidConfigurationException e) {
80-
throw Throwables.propagate(e);
81-
}
82-
}
83-
84-
@Override
85-
@Synchronized("lock")
86-
public void save() {
87-
Utils.assertMainThread();
88-
try {
89-
super.save();
90-
} catch (InvalidConfigurationException e) {
91-
throw Throwables.propagate(e);
92-
}
52+
public class SpawnShieldConfig extends AnnotationConfig {
53+
static {
54+
addSerializer(new BlockModeConverter());
9355
}
9456

9557
public void addRegionToBlock(Region r) {
@@ -104,29 +66,17 @@ public void removeRegionToBlock(Region r) {
10466
refreshRegionsToBlock();
10567
}
10668

107-
@Comments({
108-
"The list of regions to block entry into in combat",
109-
"This option can be controlled by the command /spawnshield block command",
110-
"Please use the command to edit this option, as it checks for errors when you add them"
111-
})
69+
@Setting("blockRegions")
11270
@Getter(AccessLevel.NONE) //Use the cached and thread safe version
11371
private List<String> blockRegions = Lists.newArrayList("example", "example2");
114-
@Comments({
115-
"The prevention mode to put the plugin in",
116-
"'teleport' teleports the player to their last known location outside of the safezone if they enter a safezone",
117-
"'knockback' knocks the player back when they enter a safezone and is experimental",
118-
"'forcefield' is currently in development, and should not be used unless you know what you are doing"
119-
})
72+
73+
@Setting("mode")
12074
private BlockMode mode = BlockMode.TELEPORT;
121-
@Comments({
122-
"Print out a ****load of information about the plugin",
123-
"You probably shouldn't use this unless you are testing the plugin"
124-
})
75+
76+
@Setting("debug")
12577
private boolean debug = false;
12678

127-
@Comments(
128-
"The range in which players will see a forcefield"
129-
)
79+
@Setting("forcefieldRange")
13080
private int forcefieldRange = 50;
13181

13282
@Synchronized("lock")

base/src/main/java/net/techcable/spawnshield/config/SpawnShieldMessages.java

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -22,43 +22,15 @@
2222
*/
2323
package net.techcable.spawnshield.config;
2424

25-
import com.google.common.base.Throwables;
2625
import net.techcable.spawnshield.SpawnShield;
27-
import net.techcable.techutils.collect.Pair;
28-
import net.techcable.techutils.yamler.Comments;
29-
import net.techcable.techutils.yamler.Config;
30-
import net.techcable.techutils.yamler.InvalidConfigurationException;
31-
import org.bukkit.Bukkit;
32-
import org.bukkit.ChatColor;
33-
import org.bukkit.World;
34-
35-
import java.io.File;
36-
import java.util.Collection;
37-
import java.util.List;
38-
import java.util.Set;
26+
import net.techcable.techutils.config.AnnotationConfig;
27+
import net.techcable.techutils.config.Setting;
3928

40-
public class SpawnShieldMessages extends Config {
41-
public SpawnShieldMessages(SpawnShield plugin) {
42-
CONFIG_FILE = new File(plugin.getDataFolder(), "messages.yml");
43-
CONFIG_HEADER = new String[] {
44-
"Messagse for spawnshield",
45-
"Supports color codes with the '&' character"
46-
};
47-
}
29+
import org.bukkit.ChatColor;
4830

49-
@Override
50-
public void init() {
51-
try {
52-
super.init();
53-
} catch (InvalidConfigurationException e) {
54-
throw Throwables.propagate(e);
55-
}
56-
}
31+
public class SpawnShieldMessages extends AnnotationConfig {
5732

58-
@Comments({
59-
"In mode teleport this message will be sent if they enter a safezone in combat",
60-
"Will be sent at max once a second"
61-
})
33+
@Setting("cantEnterSafezone")
6234
private String cantEnterSafezone = "&4[SpawnShield] You can't enter a safezone in combat!";
6335

6436
public String getCantEnterSafezone() {

base/src/main/resources/config.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# SpawnShield Config
2+
3+
# The list of regions to block entry into in combat
4+
# This option can be controlled by the command /spawnshield block command
5+
# Please use the command to edit this option, as it checks for errors when you add them
6+
blockRegions:
7+
- "example"
8+
- "example2"
9+
10+
# The prevention mode to put the plugin in
11+
# 'teleport' teleports the player to their last known location outside of the safezone if they enter a safezone
12+
# 'knockback' knocks the player back when they enter a safezone and is experimental
13+
# 'forcefield' is currently in development, and should not be used unless you know what you are doing
14+
mode: "teleport"
15+
16+
# Print out a ****load of information about the plugin
17+
# You probably shouldn't use this unless you are testing the plugin
18+
debug: false
19+
20+
# The range in which players will see a forcefield
21+
forcefieldRange: 50;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# SpawnShield Configuration
2+
# Messages for spawnshield
3+
# Supports color codes with the '&' character
4+
5+
# In mode teleport this message will be sent if they enter a safezone in combat
6+
# Will be sent at max once a second
7+
cantEnterSafezone: "&4[SpawnShield] You can't enter a safezone in combat!"

0 commit comments

Comments
 (0)