forked from MLG-Fortress/AutomaticInventory
-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathAutomaticInventory.java
More file actions
122 lines (101 loc) · 4.09 KB
/
Copy pathAutomaticInventory.java
File metadata and controls
122 lines (101 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package dev.chaws.automaticinventory;
import dev.chaws.automaticinventory.commands.*;
import dev.chaws.automaticinventory.configuration.Features;
import dev.chaws.automaticinventory.configuration.GlobalConfig;
import dev.chaws.automaticinventory.configuration.PlayerConfig;
import dev.chaws.automaticinventory.hooks.LWCHook;
import dev.chaws.automaticinventory.listeners.*;
import dev.chaws.automaticinventory.messaging.LocalizedMessages;
import dev.chaws.automaticinventory.utilities.Metrics;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.util.logging.Logger;
public class AutomaticInventory extends JavaPlugin {
public static AutomaticInventory instance;
public static Logger log;
public void onEnable() {
log = getLogger();
instance = this;
var dataFolder = this.initializeDataFolder();
GlobalConfig.initialize(dataFolder);
LocalizedMessages.initialize(dataFolder);
var playerConfigFolder = this.initializePlayerConfigFolder();
PlayerConfig.initialize(playerConfigFolder);
for (Player player : this.getServer().getOnlinePlayers()) {
PlayerConfig.initializePlayer(player);
}
var pluginManager = this.getServer().getPluginManager();
pluginManager.registerEvents(new ConfigurationListener(), this);
pluginManager.registerEvents(new DepositAllAdvertisementListener(), this);
pluginManager.registerEvents(new QuickDepositListener(), this);
pluginManager.registerEvents(new RefillStacksListener(), this);
pluginManager.registerEvents(new SortChestsListener(), this);
pluginManager.registerEvents(new SortInventoryListener(), this);
this.registerCommand("autorefill", new AutoRefillCommand());
this.registerCommand("autosort", new AutoSortCommand());
this.registerCommand("depositall", new DepositAllCommand());
this.registerCommand("quickdeposit", new QuickDepositCommand());
if (pluginManager.getPlugin("LWC") != null) {
LWCHook.enable();
}
try {
new Metrics(this, 16822);
} catch (Throwable ignored) {
}
}
private <T extends AutomaticInventoryCommand> void registerCommand(String commandName, T command) {
var pluginCommand = this.getCommand(commandName);
if (pluginCommand == null) {
return;
}
pluginCommand.setExecutor(command);
pluginCommand.setTabCompleter(command);
}
public void onDisable() {
for (Player player : this.getServer().getOnlinePlayers()) {
var data = PlayerConfig.fromPlayer(player);
data.saveChanges();
data.waitForSaveComplete();
}
log.info("AutomaticInventory disabled.");
}
public static boolean hasPermission(Features feature, Player player) {
return switch (feature) {
case SortInventory -> player.hasPermission("automaticinventory.sortinventory");
case SortChests -> player.hasPermission("automaticinventory.sortchests");
case RefillStacks -> player.hasPermission("automaticinventory.refillstacks");
case QuickDeposit -> player.hasPermission("automaticinventory.quickdeposit");
case DepositAll -> player.hasPermission("automaticinventory.depositall");
};
}
private File initializeDataFolder() {
var dataFolder = this.getDataFolder();
if (!dataFolder.exists()) {
var oldDataFolder = new File(dataFolder.getPath() + File.separator + ".." + File.separator + "AutomaticInventory").getAbsoluteFile();
if (oldDataFolder.exists()) {
AutomaticInventory.log.info("Migrating config folder...");
if (oldDataFolder.renameTo(dataFolder)) {
AutomaticInventory.log.info("Migrated config folder successfully.");
return dataFolder;
} else {
AutomaticInventory.log.warning("Failed to migrate config folder!");
}
}
if (!dataFolder.mkdirs()) {
AutomaticInventory.log.warning("Could not create config directory.");
}
}
return dataFolder;
}
private File initializePlayerConfigFolder() {
var dataFolder = this.getDataFolder();
var playerConfigFolder = new File(dataFolder.getPath() + File.separator + "PlayerConfig");
if (!playerConfigFolder.exists()) {
if (!playerConfigFolder.mkdirs()) {
AutomaticInventory.log.warning("Could not create player config directory.");
}
}
return playerConfigFolder;
}
}