Skip to content

Commit 21f122c

Browse files
authored
Merge pull request #10 from TheNextLvl-net/config
converted to gson config
2 parents 3c163c7 + 45b6f1d commit 21f122c

File tree

10 files changed

+139
-247
lines changed

10 files changed

+139
-247
lines changed

build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ dependencies {
3333
implementation("org.bstats:bstats-bukkit:3.0.2")
3434
// Internationalization
3535
implementation("net.thenextlvl.core:i18n:1.0.18")
36+
implementation("net.thenextlvl.core:adapters:1.0.9")
3637

3738
annotationProcessor("org.projectlombok:lombok:1.18.32")
3839
}
Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,43 @@
1-
/*
2-
* goPaint is designed to simplify painting inside of Minecraft.
3-
* Copyright (C) Arcaniax-Development
4-
* Copyright (C) Arcaniax team and contributors
5-
*
6-
* This program is free software: you can redistribute it and/or modify
7-
* it under the terms of the GNU General Public License as published by
8-
* the Free Software Foundation, either version 3 of the License, or
9-
* (at your option) any later version.
10-
*
11-
* This program is distributed in the hope that it will be useful,
12-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14-
* GNU General Public License for more details.
15-
*
16-
* You should have received a copy of the GNU General Public License
17-
* along with this program. If not, see <https://www.gnu.org/licenses/>.
18-
*/
191
package net.thenextlvl.gopaint;
202

3+
import com.google.gson.GsonBuilder;
4+
import core.file.FileIO;
5+
import core.file.format.GsonFile;
216
import core.i18n.file.ComponentBundle;
7+
import core.io.IO;
8+
import core.paper.adapters.inventory.MaterialAdapter;
229
import lombok.Getter;
2310
import lombok.experimental.Accessors;
2411
import net.kyori.adventure.text.minimessage.MiniMessage;
2512
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder;
2613
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver;
2714
import net.thenextlvl.gopaint.brush.PlayerBrushManager;
2815
import net.thenextlvl.gopaint.command.GoPaintCommand;
16+
import net.thenextlvl.gopaint.objects.other.PluginConfig;
2917
import net.thenextlvl.gopaint.listeners.ConnectListener;
3018
import net.thenextlvl.gopaint.listeners.InteractListener;
3119
import net.thenextlvl.gopaint.listeners.InventoryListener;
32-
import net.thenextlvl.gopaint.objects.other.Settings;
20+
import net.thenextlvl.gopaint.objects.other.SurfaceMode;
3321
import org.bstats.bukkit.Metrics;
22+
import org.bukkit.Axis;
3423
import org.bukkit.Bukkit;
3524
import org.bukkit.Material;
3625
import org.bukkit.entity.Player;
3726
import org.bukkit.event.Listener;
3827
import org.bukkit.plugin.java.JavaPlugin;
3928

4029
import java.io.File;
30+
import java.util.ArrayList;
4131
import java.util.Locale;
4232

43-
@Getter
4433
@Accessors(fluent = true)
4534
public class GoPaintPlugin extends JavaPlugin implements Listener {
46-
47-
public static final String PAPER_DOCS = "https://jd.papermc.io/paper/1.20.6/org/bukkit/Material.html#enum-constant-summary";
48-
4935
public static final String USE_PERMISSION = "gopaint.use";
5036
public static final String ADMIN_PERMISSION = "gopaint.admin";
5137
public static final String WORLD_BYPASS_PERMISSION = "gopaint.world.bypass";
5238

5339
private final File translations = new File(getDataFolder(), "translations");
54-
private final ComponentBundle bundle = new ComponentBundle(translations, audience ->
40+
private final @Getter ComponentBundle bundle = new ComponentBundle(translations, audience ->
5541
audience instanceof Player player ? player.locale() : Locale.US)
5642
.register("messages", Locale.US)
5743
.register("messages_german", Locale.GERMANY)
@@ -60,7 +46,18 @@ public class GoPaintPlugin extends JavaPlugin implements Listener {
6046
Placeholder.component("prefix", bundle.component(Locale.US, "prefix"))
6147
)).build());
6248

63-
private final PlayerBrushManager brushManager = new PlayerBrushManager(bundle);
49+
private final FileIO<PluginConfig> configFile = new GsonFile<>(IO.of(getDataFolder(), "config.json"), new PluginConfig(
50+
new PluginConfig.Generic(Material.FEATHER, 100, 10, 50, Axis.Y, 50, 50, new ArrayList<>(), true, true, Material.SPONGE, SurfaceMode.DIRECT),
51+
new PluginConfig.Thickness(1, 5),
52+
new PluginConfig.Angle(2, 5, 10, 40, 85),
53+
new PluginConfig.Fracture(2, 5)
54+
), new GsonBuilder()
55+
.registerTypeAdapter(Material.class, MaterialAdapter.NotNull.INSTANCE)
56+
.setPrettyPrinting()
57+
.create()
58+
).validate().save();
59+
60+
private final @Getter PlayerBrushManager brushManager = new PlayerBrushManager(this);
6461
private final Metrics metrics = new Metrics(this, 22279);
6562

6663
@Override
@@ -73,15 +70,6 @@ public void onEnable() {
7370
return;
7471
}
7572

76-
reloadConfig();
77-
78-
Material brush = Settings.settings().GENERIC.DEFAULT_BRUSH;
79-
if (!brush.isItem()) {
80-
getComponentLogger().error("{} is not a valid default brush, it has to be an item", brush.name());
81-
getComponentLogger().error("For more information visit {}", PAPER_DOCS);
82-
Bukkit.getPluginManager().disablePlugin(this);
83-
}
84-
8573
registerListeners();
8674
registerCommands();
8775
}
@@ -91,21 +79,26 @@ public void onDisable() {
9179
metrics.shutdown();
9280
}
9381

82+
@Override
9483
public void reloadConfig() {
95-
Settings.settings().reload(this, new File(getDataFolder(), "config.yml"));
84+
configFile.reload();
9685
}
9786

9887
private void registerCommands() {
9988
new GoPaintCommand(this).register();
10089
}
10190

10291
private void registerListeners() {
103-
Bukkit.getPluginManager().registerEvents(new InventoryListener(brushManager()), this);
92+
Bukkit.getPluginManager().registerEvents(new InventoryListener(this), this);
10493
Bukkit.getPluginManager().registerEvents(new InteractListener(this), this);
10594
Bukkit.getPluginManager().registerEvents(new ConnectListener(brushManager()), this);
10695
}
10796

10897
private boolean hasOriginalGoPaint() {
10998
return Bukkit.getPluginManager().getPlugin("goPaint") != this;
11099
}
100+
101+
public PluginConfig config() {
102+
return configFile.getRoot();
103+
}
111104
}

src/main/java/net/thenextlvl/gopaint/brush/ExportedPlayerBrush.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
package net.thenextlvl.gopaint.brush;
2020

2121
import net.thenextlvl.gopaint.objects.brush.Brush;
22-
import net.thenextlvl.gopaint.objects.other.Settings;
2322
import net.thenextlvl.gopaint.objects.other.SurfaceMode;
2423
import org.bukkit.Axis;
2524
import org.bukkit.Material;
@@ -94,7 +93,7 @@ public static final class Builder {
9493
private final Brush brush;
9594

9695
private List<Material> blocks = Collections.emptyList();
97-
private Axis axis = Settings.settings().GENERIC.DEFAULT_AXIS;
96+
private Axis axis = Axis.Y; // todo: plugin.config().GENERIC.DEFAULT_AXIS;
9897
private SurfaceMode surfaceMode = SurfaceMode.DISABLED;
9998

10099
private @Nullable Material mask;

src/main/java/net/thenextlvl/gopaint/brush/PlayerBrush.java

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
import net.kyori.adventure.text.format.NamedTextColor;
2626
import net.kyori.adventure.text.format.Style;
2727
import net.kyori.adventure.text.format.TextDecoration;
28+
import net.thenextlvl.gopaint.GoPaintPlugin;
2829
import net.thenextlvl.gopaint.objects.brush.*;
29-
import net.thenextlvl.gopaint.objects.other.Settings;
3030
import net.thenextlvl.gopaint.objects.other.SurfaceMode;
3131
import net.thenextlvl.gopaint.utils.GUI;
3232
import org.bukkit.Axis;
@@ -50,7 +50,7 @@
5050
@Accessors(fluent = true)
5151
public final class PlayerBrush implements BrushSettings {
5252

53-
private final PlayerBrushManager brushManager;
53+
private final GoPaintPlugin plugin;
5454
private final Random random = new SecureRandom();
5555

5656
private boolean maskEnabled;
@@ -72,23 +72,23 @@ public final class PlayerBrush implements BrushSettings {
7272

7373
private final Inventory gui;
7474

75-
public PlayerBrush(PlayerBrushManager brushManager) {
76-
this.brushManager = brushManager;
77-
78-
surfaceMode = Settings.settings().GENERIC.SURFACE_MODE;
79-
maskEnabled = Settings.settings().GENERIC.MASK_ENABLED;
80-
enabled = Settings.settings().GENERIC.ENABLED_BY_DEFAULT;
81-
chance = Settings.settings().GENERIC.DEFAULT_CHANCE;
82-
thickness = Settings.settings().THICKNESS.DEFAULT_THICKNESS;
83-
fractureDistance = Settings.settings().FRACTURE.DEFAULT_FRACTURE_DISTANCE;
84-
angleDistance = Settings.settings().ANGLE.DEFAULT_ANGLE_DISTANCE;
85-
angleHeightDifference = Settings.settings().ANGLE.DEFAULT_ANGLE_HEIGHT_DIFFERENCE;
86-
falloffStrength = Settings.settings().GENERIC.DEFAULT_FALLOFF_STRENGTH;
87-
mixingStrength = Settings.settings().GENERIC.DEFAULT_MIXING_STRENGTH;
88-
axis = Settings.settings().GENERIC.DEFAULT_AXIS;
89-
size = Settings.settings().GENERIC.DEFAULT_SIZE;
90-
mask = Settings.settings().GENERIC.DEFAULT_MASK;
91-
brush = brushManager.cycleForward(null);
75+
public PlayerBrush(GoPaintPlugin plugin) {
76+
this.plugin = plugin;
77+
78+
surfaceMode = plugin.config().generic().surfaceMode();
79+
maskEnabled = plugin.config().generic().maskEnabled();
80+
enabled = plugin.config().generic().enabledByDefault();
81+
chance = plugin.config().generic().defaultChance();
82+
thickness = plugin.config().thickness().defaultThickness();
83+
fractureDistance = plugin.config().fracture().defaultFractureDistance();
84+
angleDistance = plugin.config().angle().defaultAngleDistance();
85+
angleHeightDifference = plugin.config().angle().defaultAngleHeightDifference();
86+
falloffStrength = plugin.config().generic().defaultFalloffStrength();
87+
mixingStrength = plugin.config().generic().defaultMixingStrength();
88+
axis = plugin.config().generic().defaultAxis();
89+
size = plugin.config().generic().defaultSize();
90+
mask = plugin.config().generic().defaultMask();
91+
brush = plugin.brushManager().cycleForward(null);
9292
blocks.add(Material.STONE);
9393
gui = GUI.create(this);
9494
}
@@ -151,20 +151,20 @@ public void removeBlock(int slot) {
151151
}
152152

153153
public void cycleBrushForward() {
154-
brush = brushManager.cycleForward(brush);
154+
brush = plugin.brushManager().cycleForward(brush);
155155
updateInventory();
156156
}
157157

158158
public void cycleBrushBackwards() {
159-
brush = brushManager.cycleBack(brush);
159+
brush = plugin.brushManager().cycleBack(brush);
160160
updateInventory();
161161
}
162162

163163
public void setSize(int size) {
164-
if (size <= Settings.settings().GENERIC.MAX_SIZE && size > 0) {
164+
if (size <= plugin.config().generic().maxSize() && size > 0) {
165165
this.size = size;
166-
} else if (size > Settings.settings().GENERIC.MAX_SIZE) {
167-
this.size = Settings.settings().GENERIC.MAX_SIZE;
166+
} else if (size > plugin.config().generic().maxSize()) {
167+
this.size = plugin.config().generic().maxSize();
168168
} else {
169169
this.size = 1;
170170
}
@@ -177,13 +177,13 @@ public Inventory getInventory() {
177177

178178
public void increaseBrushSize(boolean x10) {
179179
if (x10) {
180-
if (size + 10 <= Settings.settings().GENERIC.MAX_SIZE) {
180+
if (size + 10 <= plugin.config().generic().maxSize()) {
181181
size += 10;
182182
} else {
183-
size = Settings.settings().GENERIC.MAX_SIZE;
183+
size = plugin.config().generic().maxSize();
184184
}
185185
} else {
186-
if (size < Settings.settings().GENERIC.MAX_SIZE) {
186+
if (size < plugin.config().generic().maxSize()) {
187187
size += 1;
188188
}
189189
}
@@ -226,7 +226,7 @@ public void decreaseChance() {
226226
}
227227

228228
public void increaseThickness() {
229-
if (thickness < Settings.settings().THICKNESS.MAX_THICKNESS) {
229+
if (thickness < plugin.config().thickness().maxThickness()) {
230230
thickness += 1;
231231
}
232232
updateInventory();
@@ -240,7 +240,7 @@ public void decreaseThickness() {
240240
}
241241

242242
public void increaseAngleDistance() {
243-
if (angleDistance < Settings.settings().ANGLE.MAX_ANGLE_DISTANCE) {
243+
if (angleDistance < plugin.config().angle().maxAngleDistance()) {
244244
angleDistance += 1;
245245
}
246246
updateInventory();
@@ -254,7 +254,7 @@ public void decreaseAngleDistance() {
254254
}
255255

256256
public void increaseFractureDistance() {
257-
if (this.fractureDistance < Settings.settings().FRACTURE.MAX_FRACTURE_DISTANCE) {
257+
if (this.fractureDistance < plugin.config().fracture().maxFractureDistance()) {
258258
this.fractureDistance += 1;
259259
}
260260
updateInventory();
@@ -273,8 +273,8 @@ public void increaseAngleHeightDifference(boolean d15) {
273273
} else {
274274
angleHeightDifference += 5.0;
275275
}
276-
if (angleHeightDifference > Settings.settings().ANGLE.MAX_ANGLE_HEIGHT_DIFFERENCE) {
277-
angleHeightDifference = Settings.settings().ANGLE.MAX_ANGLE_HEIGHT_DIFFERENCE;
276+
if (angleHeightDifference > plugin.config().angle().maxAngleHeightDifference()) {
277+
angleHeightDifference = plugin.config().angle().maxAngleHeightDifference();
278278
}
279279
updateInventory();
280280
}
@@ -285,8 +285,8 @@ public void decreaseAngleHeightDifference(boolean d15) {
285285
} else {
286286
angleHeightDifference -= 5.0;
287287
}
288-
if (angleHeightDifference < Settings.settings().ANGLE.MIN_ANGLE_HEIGHT_DIFFERENCE) {
289-
angleHeightDifference = Settings.settings().ANGLE.MIN_ANGLE_HEIGHT_DIFFERENCE;
288+
if (angleHeightDifference < plugin.config().angle().minAngleHeightDifference()) {
289+
angleHeightDifference = plugin.config().angle().minAngleHeightDifference();
290290
}
291291
updateInventory();
292292
}

src/main/java/net/thenextlvl/gopaint/brush/PlayerBrushManager.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@
1919
package net.thenextlvl.gopaint.brush;
2020

2121
import com.google.common.collect.ImmutableList;
22-
import core.i18n.file.ComponentBundle;
2322
import lombok.Getter;
23+
import net.thenextlvl.gopaint.GoPaintPlugin;
2424
import net.thenextlvl.gopaint.objects.brush.*;
2525
import org.bukkit.entity.Player;
2626
import org.jetbrains.annotations.Nullable;
@@ -37,8 +37,9 @@
3737
public class PlayerBrushManager {
3838
private final HashMap<UUID, PlayerBrush> playerBrushes = new HashMap<>();
3939
private final @Getter List<Brush> brushes;
40+
private final GoPaintPlugin plugin;
4041

41-
public PlayerBrushManager(ComponentBundle bundle) {
42+
public PlayerBrushManager(GoPaintPlugin plugin) {
4243
brushes = ImmutableList.of(
4344
new SphereBrush(),
4445
new SprayBrush(),
@@ -50,8 +51,9 @@ public PlayerBrushManager(ComponentBundle bundle) {
5051
new UnderlayBrush(),
5152
new FractureBrush(),
5253
new GradientBrush(),
53-
new PaintBrush(bundle)
54+
new PaintBrush(plugin.bundle())
5455
);
56+
this.plugin = plugin;
5557
}
5658

5759
/**
@@ -61,7 +63,7 @@ public PlayerBrushManager(ComponentBundle bundle) {
6163
* @return The brush for the specified player.
6264
*/
6365
public PlayerBrush getBrush(Player player) {
64-
return playerBrushes.computeIfAbsent(player.getUniqueId(), ignored -> new PlayerBrush(this));
66+
return playerBrushes.computeIfAbsent(player.getUniqueId(), ignored -> new PlayerBrush(plugin));
6567
}
6668

6769
/**

0 commit comments

Comments
 (0)