Skip to content

Commit

Permalink
Merge pull request #20 from Hutch79/rewrite/everything
Browse files Browse the repository at this point in the history
Rewrite/everything
  • Loading branch information
Hutch79 authored Apr 4, 2024
2 parents 7fab7e6 + f843509 commit 32a667e
Show file tree
Hide file tree
Showing 29 changed files with 824 additions and 285 deletions.
14 changes: 6 additions & 8 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
# This is a basic workflow to help you get started with Actions

name: Compiling Action

# Controls when the workflow will run
on: [pull_request, push]
on: [pull_request, push, workflow_dispatch]

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

# Setting up JDK 11
- name: Step 2 - Setting up JDK 8
- name: Step 2 - Setting up JDK 11
uses: actions/setup-java@v2
with:
distribution: 'zulu'
Expand All @@ -28,19 +26,19 @@ jobs:
# Package Program Using Maven
- name: Step 3 - Package Project using Maven
run: mvn -B package --file pom.xml

# List everything of the current directory
- name: Step 4 - List the current directory
run: ls -a

# Target folder completion
- name: Step 4 - List items in target folder
run: |
cd target
ls -a
# Target folder completion
- name: Step 5 - Copying Files for download
- name: Step 5 - Copying Files for download
uses: actions/upload-artifact@v2
with:
name: Published Jar Files
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,4 @@ buildNumber.properties

# Common working directory
run/
/src/test/java/ch/hutch79/migrationTest/configs/Migrations/
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ command:
# Which command should be executed as the player?
# You can also use PlaceholderAPI here!
# Enter without /
command: say f no shift
commandList:
- say f no shift
# Stopping item drop or hand swap from happening
cancel: true
# Should the command be executed from the Server?
Expand All @@ -78,7 +79,9 @@ command:
key: F
requireShift: True
permission: f-command.example
command: say F shift
commandList:
- say F shift
- say Hello there
cancel: true
executeAsServer: false
```
Expand Down
34 changes: 32 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@

<groupId>ch.hutch79</groupId>
<artifactId>F-Command</artifactId>
<version>2.5.3</version>
<version>2.6.1</version>
<packaging>jar</packaging>
<name>F-Command</name>

<description>F-Command executes a command when a Player switches his/her offhand</description>
<properties>
<java.version>1.8</java.version>
<java.version>11</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<url>https://hutch79.ch/</url>
Expand Down Expand Up @@ -46,6 +46,14 @@
<!-- Replace this with your package! -->
<shadedPattern>ch.hutch79</shadedPattern>
</relocation>
<relocation>
<pattern>com.fasterxml.jackson</pattern>
<shadedPattern>ch.hutch79.jackson</shadedPattern>
</relocation>
<relocation>
<pattern>org.yaml.snakeyaml</pattern>
<shadedPattern>ch.hutch79.snakeyaml</shadedPattern>
</relocation>
</relocations>
</configuration>
<executions>
Expand All @@ -60,6 +68,11 @@
</execution>
</executions>
</plugin>

<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
</plugins>
<resources>
<resource>
Expand Down Expand Up @@ -109,5 +122,22 @@
<version>2.11.2</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>2.13.0</version>
</dependency>
<dependency>
<groupId>com.google.inject</groupId>
<artifactId>guice</artifactId>
<version>7.0.0</version>
</dependency>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>5.10.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -1,43 +1,46 @@
package ch.hutch79;

import ch.hutch79.command.Command;
import ch.hutch79.command.CommandTab;
import ch.hutch79.events.EventHandler;
package ch.hutch79.application;

import ch.hutch79.application.command.Command;
import ch.hutch79.application.command.CommandTab;
import ch.hutch79.application.configManager.ConfigManager;
import ch.hutch79.application.configManager.ConfigMigrator;
import ch.hutch79.application.events.EventRecivers;
import ch.hutch79.application.guice.DiContainerInstances;
import ch.hutch79.application.messages.ConsoleMessanger;
import com.google.inject.Guice;
import com.google.inject.Injector;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.java.JavaPlugin;
import org.bstats.bukkit.Metrics;
import com.jeff_media.updatechecker.*;
import com.jeff_media.updatechecker.UpdateChecker;
import com.jeff_media.updatechecker.UpdateCheckSource;
import com.jeff_media.updatechecker.UserAgentBuilder;

import java.util.Objects;

public final class FCommand extends JavaPlugin {
PluginDescriptionFile pdf = this.getDescription();
private static FCommand instance;
private static EventHandler eventHandler;
private boolean isPlaceholderApiInstalled = false;
private static boolean debug;

@Override
public void onEnable() {
instance = this;

eventHandler = new EventHandler();

// getConfig().options().copyDefaults();
getConfig().options().copyDefaults();
saveDefaultConfig();
reloadConfig();
instance = this;
Injector injector = Guice.createInjector(new DiContainerInstances(instance));
injector.getInstance(ConfigMigrator.class);

eventHandler.eventListenerInit();
Bukkit.getPluginManager().registerEvents(eventHandler, this);
new ConsoleMessanger(injector.getInstance(ConfigManager.class)); // Give ConfigManager Instance to ConsoleMessanger

Objects.requireNonNull(getCommand("fcommand")).setExecutor(new Command());
Objects.requireNonNull(getCommand("fcommand")).setExecutor(injector.getInstance(Command.class));
Objects.requireNonNull(getCommand("fcommand")).setTabCompleter(new CommandTab());
Bukkit.getPluginManager().registerEvents(injector.getInstance(EventRecivers.class), this);

new Metrics(this, 17738); // bStats

debug = getConfig().getBoolean("debug");

final int SPIGOT_RESOURCE_ID = 108009; // Update checker

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




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

public static EventHandler getListener() {
return eventHandler;
}

public static boolean getDebug() {
return debug;
}

public static void setDebug(Boolean value) {
debug = value;
}

public String replacePlaceholders(Player player, String input) {
if(isPlaceholderApiInstalled) {
return me.clip.placeholderapi.PlaceholderAPI.setPlaceholders(player, input);
}
return input;
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
package ch.hutch79.command;
package ch.hutch79.application.command;

import ch.hutch79.FCommand;
import ch.hutch79.application.configManager.ConfigManager;
import ch.hutch79.domain.configs.v1.Config;
import jakarta.inject.Inject;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;

import java.io.FileNotFoundException;

public class Command implements CommandExecutor {

private final ConfigManager configManager;

@Inject
public Command(ConfigManager _configManager) {
configManager = _configManager;
}

@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull org.bukkit.command.Command command, @NotNull String label, @NotNull String[] args) {

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

if (args[0].equalsIgnoreCase("reload")) {
FCommand.getListener().eventListenerInit();
try {
configManager.loadConfig(Config.class, "config.yml");
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
sender.sendMessage("§dF-Command §8> §7Config has been reloaded");
return true;
}


return false;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ch.hutch79.command;
package ch.hutch79.application.command;

import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package ch.hutch79.application.configManager;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;

public class ConfigManager {

private String _pluginPath;
private ObjectMapper writeMapper;
private ObjectMapper readMapper;
private HashMap<Class<?>, Object> configCache = new HashMap<>();
public ConfigManager(@NotNull File pluginPath) {
_pluginPath = pluginPath.toString();

writeMapper = new ObjectMapper(new YAMLFactory().disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER));
writeMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
writeMapper.findAndRegisterModules();

readMapper = new ObjectMapper(new YAMLFactory());
readMapper.findAndRegisterModules();
}


public void writeConfig (Object configClass, String localPath) throws FileNotFoundException {
try {
writeMapper.writeValue(new File(_pluginPath + File.separator + localPath), configClass);
} catch (IOException e) {
throw new FileNotFoundException();
}
}

public <T> ConfigManager loadConfig(Class<?> configClass, String localPath) throws FileNotFoundException {
T config;
try {
config = (T) readMapper.readValue(new File(_pluginPath + File.separator + localPath), configClass);
} catch (IOException e) {
throw new FileNotFoundException();
}
configCache.put(configClass, config);
return this;
}

public <T> T getConfig(Class<?> configClass) {
return (T) configCache.get(configClass);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package ch.hutch79.application.configManager;

import ch.hutch79.application.configManager.Migrations.MigrationV1;
import ch.hutch79.domain.configs.v1.Config;
import ch.hutch79.application.messages.ConsoleMessanger;
import com.google.inject.Inject;
import com.google.inject.Injector;

import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;


public class ConfigMigrator {

@Inject
public ConfigMigrator(Injector injector, ConfigManager configManager) throws IOException {
ConsoleMessanger messanger = new ConsoleMessanger();
try {
configManager.loadConfig(Config.class, "config.yml");
} catch (Exception e1) {
try {
var migrationV1 = injector.getInstance(MigrationV1.class);
Config configNew = migrationV1.configMigration(Paths.get("plugins" + File.separator + "F-Command"));
configManager.writeConfig(configNew, "config.yml");
} catch (Exception e2) {
messanger.message("§cThe Config Migration failed. Therefore the Plugin will be disabled.");
throw e2;
}
}
configManager.loadConfig(Config.class, "config.yml");
}
}
Loading

0 comments on commit 32a667e

Please sign in to comment.