Skip to content

Commit 32a667e

Browse files
authored
Merge pull request #20 from Hutch79/rewrite/everything
Rewrite/everything
2 parents 7fab7e6 + f843509 commit 32a667e

29 files changed

Lines changed: 824 additions & 285 deletions

File tree

.github/workflows/build.yml

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
# This is a basic workflow to help you get started with Actions
2-
31
name: Compiling Action
42

53
# Controls when the workflow will run
6-
on: [pull_request, push]
4+
on: [pull_request, push, workflow_dispatch]
75

86
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
97
jobs:
@@ -19,7 +17,7 @@ jobs:
1917
uses: actions/checkout@v2
2018

2119
# Setting up JDK 11
22-
- name: Step 2 - Setting up JDK 8
20+
- name: Step 2 - Setting up JDK 11
2321
uses: actions/setup-java@v2
2422
with:
2523
distribution: 'zulu'
@@ -28,19 +26,19 @@ jobs:
2826
# Package Program Using Maven
2927
- name: Step 3 - Package Project using Maven
3028
run: mvn -B package --file pom.xml
31-
29+
3230
# List everything of the current directory
3331
- name: Step 4 - List the current directory
3432
run: ls -a
35-
33+
3634
# Target folder completion
3735
- name: Step 4 - List items in target folder
3836
run: |
3937
cd target
4038
ls -a
41-
39+
4240
# Target folder completion
43-
- name: Step 5 - Copying Files for download
41+
- name: Step 5 - Copying Files for download
4442
uses: actions/upload-artifact@v2
4543
with:
4644
name: Published Jar Files

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,4 @@ buildNumber.properties
111111

112112
# Common working directory
113113
run/
114+
/src/test/java/ch/hutch79/migrationTest/configs/Migrations/

README.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ command:
6868
# Which command should be executed as the player?
6969
# You can also use PlaceholderAPI here!
7070
# Enter without /
71-
command: say f no shift
71+
commandList:
72+
- say f no shift
7273
# Stopping item drop or hand swap from happening
7374
cancel: true
7475
# Should the command be executed from the Server?
@@ -78,7 +79,9 @@ command:
7879
key: F
7980
requireShift: True
8081
permission: f-command.example
81-
command: say F shift
82+
commandList:
83+
- say F shift
84+
- say Hello there
8285
cancel: true
8386
executeAsServer: false
8487
```

pom.xml

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@
66

77
<groupId>ch.hutch79</groupId>
88
<artifactId>F-Command</artifactId>
9-
<version>2.5.3</version>
9+
<version>2.6.1</version>
1010
<packaging>jar</packaging>
1111
<name>F-Command</name>
1212

1313
<description>F-Command executes a command when a Player switches his/her offhand</description>
1414
<properties>
15-
<java.version>1.8</java.version>
15+
<java.version>11</java.version>
1616
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1717
</properties>
1818
<url>https://hutch79.ch/</url>
@@ -46,6 +46,14 @@
4646
<!-- Replace this with your package! -->
4747
<shadedPattern>ch.hutch79</shadedPattern>
4848
</relocation>
49+
<relocation>
50+
<pattern>com.fasterxml.jackson</pattern>
51+
<shadedPattern>ch.hutch79.jackson</shadedPattern>
52+
</relocation>
53+
<relocation>
54+
<pattern>org.yaml.snakeyaml</pattern>
55+
<shadedPattern>ch.hutch79.snakeyaml</shadedPattern>
56+
</relocation>
4957
</relocations>
5058
</configuration>
5159
<executions>
@@ -60,6 +68,11 @@
6068
</execution>
6169
</executions>
6270
</plugin>
71+
72+
<plugin>
73+
<artifactId>maven-surefire-plugin</artifactId>
74+
<version>2.22.2</version>
75+
</plugin>
6376
</plugins>
6477
<resources>
6578
<resource>
@@ -109,5 +122,22 @@
109122
<version>2.11.2</version>
110123
<scope>provided</scope>
111124
</dependency>
125+
<dependency>
126+
<groupId>com.fasterxml.jackson.dataformat</groupId>
127+
<artifactId>jackson-dataformat-yaml</artifactId>
128+
<version>2.13.0</version>
129+
</dependency>
130+
<dependency>
131+
<groupId>com.google.inject</groupId>
132+
<artifactId>guice</artifactId>
133+
<version>7.0.0</version>
134+
</dependency>
135+
<dependency>
136+
<groupId>org.junit</groupId>
137+
<artifactId>junit-bom</artifactId>
138+
<version>5.10.2</version>
139+
<type>pom</type>
140+
<scope>import</scope>
141+
</dependency>
112142
</dependencies>
113143
</project>

src/main/java/ch/hutch79/FCommand.java renamed to src/main/java/ch/hutch79/application/FCommand.java

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,46 @@
1-
package ch.hutch79;
2-
3-
import ch.hutch79.command.Command;
4-
import ch.hutch79.command.CommandTab;
5-
import ch.hutch79.events.EventHandler;
1+
package ch.hutch79.application;
2+
3+
import ch.hutch79.application.command.Command;
4+
import ch.hutch79.application.command.CommandTab;
5+
import ch.hutch79.application.configManager.ConfigManager;
6+
import ch.hutch79.application.configManager.ConfigMigrator;
7+
import ch.hutch79.application.events.EventRecivers;
8+
import ch.hutch79.application.guice.DiContainerInstances;
9+
import ch.hutch79.application.messages.ConsoleMessanger;
10+
import com.google.inject.Guice;
11+
import com.google.inject.Injector;
612
import org.bukkit.Bukkit;
713
import org.bukkit.entity.Player;
814
import org.bukkit.plugin.PluginDescriptionFile;
915
import org.bukkit.plugin.java.JavaPlugin;
1016
import org.bstats.bukkit.Metrics;
11-
import com.jeff_media.updatechecker.*;
17+
import com.jeff_media.updatechecker.UpdateChecker;
18+
import com.jeff_media.updatechecker.UpdateCheckSource;
19+
import com.jeff_media.updatechecker.UserAgentBuilder;
20+
1221
import java.util.Objects;
1322

1423
public final class FCommand extends JavaPlugin {
1524
PluginDescriptionFile pdf = this.getDescription();
1625
private static FCommand instance;
17-
private static EventHandler eventHandler;
1826
private boolean isPlaceholderApiInstalled = false;
19-
private static boolean debug;
2027

2128
@Override
2229
public void onEnable() {
23-
instance = this;
24-
25-
eventHandler = new EventHandler();
26-
27-
// getConfig().options().copyDefaults();
30+
getConfig().options().copyDefaults();
2831
saveDefaultConfig();
29-
reloadConfig();
32+
instance = this;
33+
Injector injector = Guice.createInjector(new DiContainerInstances(instance));
34+
injector.getInstance(ConfigMigrator.class);
3035

31-
eventHandler.eventListenerInit();
32-
Bukkit.getPluginManager().registerEvents(eventHandler, this);
36+
new ConsoleMessanger(injector.getInstance(ConfigManager.class)); // Give ConfigManager Instance to ConsoleMessanger
3337

34-
Objects.requireNonNull(getCommand("fcommand")).setExecutor(new Command());
38+
Objects.requireNonNull(getCommand("fcommand")).setExecutor(injector.getInstance(Command.class));
3539
Objects.requireNonNull(getCommand("fcommand")).setTabCompleter(new CommandTab());
40+
Bukkit.getPluginManager().registerEvents(injector.getInstance(EventRecivers.class), this);
3641

3742
new Metrics(this, 17738); // bStats
3843

39-
debug = getConfig().getBoolean("debug");
40-
4144
final int SPIGOT_RESOURCE_ID = 108009; // Update checker
4245

4346
new UpdateChecker(this, UpdateCheckSource.SPIGET, "" + SPIGOT_RESOURCE_ID + "")
@@ -51,8 +54,6 @@ public void onEnable() {
5154
.checkNow();
5255

5356

54-
55-
5657
if (pdf.getVersion().contains("Beta")) {
5758
getLogger().warning("It seems you're using a dev Build");
5859
getLogger().warning("You can use this Build on Production Servers but for some reasons i would not recommend that.");
@@ -95,23 +96,10 @@ public static FCommand getInstance() {
9596
return instance;
9697
}
9798

98-
public static EventHandler getListener() {
99-
return eventHandler;
100-
}
101-
102-
public static boolean getDebug() {
103-
return debug;
104-
}
105-
106-
public static void setDebug(Boolean value) {
107-
debug = value;
108-
}
109-
11099
public String replacePlaceholders(Player player, String input) {
111100
if(isPlaceholderApiInstalled) {
112101
return me.clip.placeholderapi.PlaceholderAPI.setPlaceholders(player, input);
113102
}
114103
return input;
115104
}
116-
117105
}
Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
1-
package ch.hutch79.command;
1+
package ch.hutch79.application.command;
22

3-
import ch.hutch79.FCommand;
3+
import ch.hutch79.application.configManager.ConfigManager;
4+
import ch.hutch79.domain.configs.v1.Config;
5+
import jakarta.inject.Inject;
46
import org.bukkit.command.CommandExecutor;
57
import org.bukkit.command.CommandSender;
68
import org.jetbrains.annotations.NotNull;
79

10+
import java.io.FileNotFoundException;
11+
812
public class Command implements CommandExecutor {
13+
14+
private final ConfigManager configManager;
15+
16+
@Inject
17+
public Command(ConfigManager _configManager) {
18+
configManager = _configManager;
19+
}
20+
921
@Override
1022
public boolean onCommand(@NotNull CommandSender sender, @NotNull org.bukkit.command.Command command, @NotNull String label, @NotNull String[] args) {
1123

@@ -17,11 +29,15 @@ public boolean onCommand(@NotNull CommandSender sender, @NotNull org.bukkit.comm
1729
}
1830

1931
if (args[0].equalsIgnoreCase("reload")) {
20-
FCommand.getListener().eventListenerInit();
32+
try {
33+
configManager.loadConfig(Config.class, "config.yml");
34+
} catch (FileNotFoundException e) {
35+
throw new RuntimeException(e);
36+
}
2137
sender.sendMessage("§dF-Command §8> §7Config has been reloaded");
38+
return true;
2239
}
2340

24-
2541
return false;
2642
}
2743
}

src/main/java/ch/hutch79/command/CommandTab.java renamed to src/main/java/ch/hutch79/application/command/CommandTab.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package ch.hutch79.command;
1+
package ch.hutch79.application.command;
22

33
import org.bukkit.command.Command;
44
import org.bukkit.command.CommandSender;
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package ch.hutch79.application.configManager;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.fasterxml.jackson.databind.SerializationFeature;
5+
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
6+
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
7+
import org.jetbrains.annotations.NotNull;
8+
9+
import java.io.File;
10+
import java.io.FileNotFoundException;
11+
import java.io.IOException;
12+
import java.util.HashMap;
13+
14+
public class ConfigManager {
15+
16+
private String _pluginPath;
17+
private ObjectMapper writeMapper;
18+
private ObjectMapper readMapper;
19+
private HashMap<Class<?>, Object> configCache = new HashMap<>();
20+
public ConfigManager(@NotNull File pluginPath) {
21+
_pluginPath = pluginPath.toString();
22+
23+
writeMapper = new ObjectMapper(new YAMLFactory().disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER));
24+
writeMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
25+
writeMapper.findAndRegisterModules();
26+
27+
readMapper = new ObjectMapper(new YAMLFactory());
28+
readMapper.findAndRegisterModules();
29+
}
30+
31+
32+
public void writeConfig (Object configClass, String localPath) throws FileNotFoundException {
33+
try {
34+
writeMapper.writeValue(new File(_pluginPath + File.separator + localPath), configClass);
35+
} catch (IOException e) {
36+
throw new FileNotFoundException();
37+
}
38+
}
39+
40+
public <T> ConfigManager loadConfig(Class<?> configClass, String localPath) throws FileNotFoundException {
41+
T config;
42+
try {
43+
config = (T) readMapper.readValue(new File(_pluginPath + File.separator + localPath), configClass);
44+
} catch (IOException e) {
45+
throw new FileNotFoundException();
46+
}
47+
configCache.put(configClass, config);
48+
return this;
49+
}
50+
51+
public <T> T getConfig(Class<?> configClass) {
52+
return (T) configCache.get(configClass);
53+
}
54+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package ch.hutch79.application.configManager;
2+
3+
import ch.hutch79.application.configManager.Migrations.MigrationV1;
4+
import ch.hutch79.domain.configs.v1.Config;
5+
import ch.hutch79.application.messages.ConsoleMessanger;
6+
import com.google.inject.Inject;
7+
import com.google.inject.Injector;
8+
9+
import java.io.File;
10+
import java.io.IOException;
11+
import java.nio.file.Paths;
12+
13+
14+
public class ConfigMigrator {
15+
16+
@Inject
17+
public ConfigMigrator(Injector injector, ConfigManager configManager) throws IOException {
18+
ConsoleMessanger messanger = new ConsoleMessanger();
19+
try {
20+
configManager.loadConfig(Config.class, "config.yml");
21+
} catch (Exception e1) {
22+
try {
23+
var migrationV1 = injector.getInstance(MigrationV1.class);
24+
Config configNew = migrationV1.configMigration(Paths.get("plugins" + File.separator + "F-Command"));
25+
configManager.writeConfig(configNew, "config.yml");
26+
} catch (Exception e2) {
27+
messanger.message("§cThe Config Migration failed. Therefore the Plugin will be disabled.");
28+
throw e2;
29+
}
30+
}
31+
configManager.loadConfig(Config.class, "config.yml");
32+
}
33+
}

0 commit comments

Comments
 (0)