Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removed DI Container #21

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

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

Expand Down Expand Up @@ -127,11 +127,6 @@
<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>
Expand Down
30 changes: 21 additions & 9 deletions src/main/java/ch/hutch79/application/FCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@
import ch.hutch79.application.command.CommandTab;
import ch.hutch79.application.configManager.ConfigManager;
import ch.hutch79.application.configManager.ConfigMigrator;
import ch.hutch79.application.events.EventHandler;
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 ch.hutch79.domain.configs.v1.Config;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginDescriptionFile;
Expand All @@ -18,26 +17,35 @@
import com.jeff_media.updatechecker.UpdateCheckSource;
import com.jeff_media.updatechecker.UserAgentBuilder;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Objects;

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

@Override
public void onEnable() {
getConfig().options().copyDefaults();
saveDefaultConfig();
instance = this;
Injector injector = Guice.createInjector(new DiContainerInstances(instance));
injector.getInstance(ConfigMigrator.class);

new ConsoleMessanger(injector.getInstance(ConfigManager.class)); // Give ConfigManager Instance to ConsoleMessanger
configManager = new ConfigManager(instance.getDataFolder());
try {
configManager.loadConfig(Config.class, "config.yml");
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
}
new ConsoleMessanger(configManager);

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

eventHandler = new EventHandler(configManager);
Bukkit.getPluginManager().registerEvents(new EventRecivers(eventHandler), this);

new Metrics(this, 17738); // bStats

Expand Down Expand Up @@ -102,4 +110,8 @@ public String replacePlaceholders(Player player, String input) {
}
return input;
}

public PluginDescriptionFile getPdf() {
return pdf;
}
}
2 changes: 0 additions & 2 deletions src/main/java/ch/hutch79/application/command/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

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;
Expand All @@ -13,7 +12,6 @@ public class Command implements CommandExecutor {

private final ConfigManager configManager;

@Inject
public Command(ConfigManager _configManager) {
configManager = _configManager;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,20 @@
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 {
public ConfigMigrator(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);
var migrationV1 = new MigrationV1();
Config configNew = migrationV1.configMigration(Paths.get("plugins" + File.separator + "F-Command"));
configManager.writeConfig(configNew, "config.yml");
} catch (Exception e2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import ch.hutch79.application.messages.ConsoleMessanger;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
Expand Down
2 changes: 0 additions & 2 deletions src/main/java/ch/hutch79/application/events/EventHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import ch.hutch79.application.FCommand;
import ch.hutch79.application.configManager.ConfigManager;
import ch.hutch79.application.messages.ConsoleMessanger;
import com.google.inject.Inject;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
Expand All @@ -25,7 +24,6 @@ public class EventHandler {
private Player player;
private ConsoleMessanger debug = new ConsoleMessanger(true);

@Inject
public EventHandler(ConfigManager configManager) {

this.configManager = configManager;
Expand Down
15 changes: 5 additions & 10 deletions src/main/java/ch/hutch79/application/events/EventRecivers.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,27 @@
package ch.hutch79.application.events;

import ch.hutch79.application.messages.ConsoleMessanger;
import com.google.inject.Inject;
import com.google.inject.Injector;
import org.bukkit.event.Listener;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.event.player.PlayerDropItemEvent;
import org.bukkit.event.player.PlayerSwapHandItemsEvent;

public class EventRecivers implements Listener {
private final Injector injector;
private final EventHandler eventHandler;
private Boolean ignoreEvent = false;
ConsoleMessanger debug = new ConsoleMessanger(true);

@Inject
public EventRecivers(Injector injector) {
public EventRecivers(EventHandler eventHandler) {

this.injector = injector;
this.eventHandler = eventHandler;
}


@org.bukkit.event.EventHandler
private void onSwapHandItemsEvent(PlayerSwapHandItemsEvent e) {
debug.message("PlayerSwapHandItemsEvent detected");
EventHandler eventHandler = injector.getInstance(EventHandler.class);
eventHandler.handler(e);
this.eventHandler.handler(e);
}
@org.bukkit.event.EventHandler
private void inventoryClickEvent(InventoryClickEvent e) {
Expand All @@ -40,8 +36,7 @@ private void dropItemEvent(PlayerDropItemEvent e) {
debug.message("PlayerDropItemEvent detected: " + e.getPlayer());
if (!ignoreEvent) {
debug.message("Executed");
EventHandler eventHandler = injector.getInstance(EventHandler.class);
eventHandler.handler(e);
this.eventHandler.handler(e);
}
else {
debug.message("Ignored");
Expand Down

This file was deleted.

2 changes: 2 additions & 0 deletions src/main/java/ch/hutch79/domain/configs/v1/Config.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package ch.hutch79.domain.configs.v1;

import org.bukkit.Bukkit;

import java.util.HashMap;
import java.util.Map;

Expand Down
Loading