-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathModernVillagerShopPlugin.java
More file actions
239 lines (222 loc) · 11.7 KB
/
Copy pathModernVillagerShopPlugin.java
File metadata and controls
239 lines (222 loc) · 11.7 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package me.f0reach.vshop;
import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents;
import me.f0reach.vshop.api.ModernVillagerShopAPI;
import me.f0reach.vshop.api.price.PriceRegistry;
import me.f0reach.vshop.command.VShopCommand;
import me.f0reach.vshop.config.PluginConfig;
import me.f0reach.vshop.economy.EconomyService;
import me.f0reach.vshop.integration.MvshopPlaceholders;
import me.f0reach.vshop.locale.MessageManager;
import me.f0reach.vshop.shop.ShopOpenService;
import me.f0reach.vshop.shop.ShopRegistry;
import me.f0reach.vshop.shop.ShopService;
import me.f0reach.vshop.shop.ShopVillagerManager;
import me.f0reach.vshop.shop.VillagerTeleportGuard;
import me.f0reach.vshop.shop.admin.AdminShopSlotIO;
import me.f0reach.vshop.shop.cache.PlayerCacheService;
import me.f0reach.vshop.shop.coowner.CoOwnerFlow;
import me.f0reach.vshop.shop.edit.ShopActionMenu;
import me.f0reach.vshop.shop.edit.ShopEditListener;
import me.f0reach.vshop.shop.edit.ShopEditService;
import me.f0reach.vshop.shop.edit.SlotEditFlow;
import me.f0reach.vshop.shop.egg.SpawnEggFactory;
import me.f0reach.vshop.shop.listener.NotificationFlushListener;
import me.f0reach.vshop.shop.listener.PlayerCacheListener;
import me.f0reach.vshop.shop.listener.ShopEggListener;
import me.f0reach.vshop.shop.listener.ShopVillagerListener;
import me.f0reach.vshop.shop.listener.VillagerLookListener;
import me.f0reach.vshop.shop.trade.PriceResolver;
import me.f0reach.vshop.shop.trade.TradeFlow;
import me.f0reach.vshop.shop.trade.TradeNotifier;
import me.f0reach.vshop.shop.trade.TradeService;
import me.f0reach.vshop.sound.SoundService;
import me.f0reach.vshop.storage.StorageManager;
import me.f0reach.vshop.ui.chest.IconConfig;
import me.f0reach.vshop.ui.chest.PlayerPickerListener;
import me.f0reach.vshop.ui.chest.PlayerPickerUi;
import me.f0reach.vshop.ui.chest.ShopBrowseListener;
import me.f0reach.vshop.ui.chest.ShopBrowseUi;
import me.f0reach.vshop.ui.chest.ShopEditUi;
import me.f0reach.vshop.ui.chest.ShopRestockListener;
import me.f0reach.vshop.ui.chest.ShopRestockUi;
import me.f0reach.vshop.ui.dialog.DialogService;
import org.bukkit.plugin.java.JavaPlugin;
import java.sql.SQLException;
import java.time.Clock;
@SuppressWarnings("UnstableApiUsage")
public final class ModernVillagerShopPlugin extends JavaPlugin {
private PluginConfig config;
private MessageManager messages;
private StorageManager storage;
private ShopRegistry registry;
private ShopService shopService;
private SpawnEggFactory eggFactory;
private ShopVillagerManager villagerManager;
private DialogService dialogService;
private IconConfig iconConfig;
private ShopBrowseUi browseUi;
private ShopOpenService openService;
private EconomyService economyService;
private TradeService tradeService;
private TradeNotifier tradeNotifier;
private TradeFlow tradeFlow;
private me.f0reach.vshop.shop.trade.CommandDispatcher commandDispatcher;
private ShopEditService editService;
private ShopEditUi editUi;
private SlotEditFlow slotEditFlow;
private CoOwnerFlow coOwnerFlow;
private PriceRegistry priceRegistry;
private PriceResolver priceResolver;
private ModernVillagerShopAPI api;
private MvshopPlaceholders papiExpansion;
private PlayerCacheService playerCacheService;
private PlayerPickerUi playerPickerUi;
private ShopRestockUi restockUi;
private ShopActionMenu actionMenu;
private AdminShopSlotIO adminShopSlotIO;
private SoundService soundService;
private VillagerTeleportGuard villagerTeleportGuard;
@Override
public void onEnable() {
saveDefaultConfig();
reloadConfigInternal();
this.storage = new StorageManager(this, config);
try {
storage.initSchema();
} catch (SQLException ex) {
getLogger().severe("Failed to initialize storage: " + ex.getMessage());
ex.printStackTrace();
getServer().getPluginManager().disablePlugin(this);
return;
}
this.economyService = new EconomyService(this, config);
if (!economyService.setup()) {
getLogger().severe("Vault Economy is required but unavailable. Disabling.");
getServer().getPluginManager().disablePlugin(this);
return;
}
this.soundService = new SoundService(config, getLogger());
this.registry = new ShopRegistry();
this.villagerTeleportGuard = new VillagerTeleportGuard();
this.villagerManager = new ShopVillagerManager(this, messages, storage.coOwners());
this.shopService = new ShopService(storage, registry, villagerManager, config);
this.eggFactory = new SpawnEggFactory(this, messages);
this.dialogService = new DialogService(this);
this.iconConfig = new IconConfig(messages, config);
this.priceRegistry = new PriceRegistry();
this.priceResolver = new PriceResolver(priceRegistry, config, storage);
this.browseUi = new ShopBrowseUi(storage, iconConfig, messages, priceResolver, economyService);
this.openService = new ShopOpenService(browseUi, messages, config);
this.tradeNotifier = new TradeNotifier(this, messages, storage, economyService);
this.editService = new ShopEditService(storage, registry);
this.commandDispatcher = new me.f0reach.vshop.shop.trade.CommandDispatcher(
this, messages, economyService);
this.tradeService = new TradeService(storage, economyService, config, tradeNotifier,
editService, priceResolver, commandDispatcher);
this.tradeFlow = new TradeFlow(dialogService, tradeService, messages, economyService, config,
priceResolver, storage, soundService);
this.editUi = new ShopEditUi(storage, iconConfig, messages, economyService);
this.slotEditFlow = new SlotEditFlow(dialogService, messages, economyService, editService, config);
this.playerCacheService = new PlayerCacheService(this);
this.playerPickerUi = new PlayerPickerUi(playerCacheService, messages, dialogService, iconConfig);
this.coOwnerFlow = new CoOwnerFlow(dialogService, messages, storage, shopService, villagerManager,
playerPickerUi, playerCacheService);
this.restockUi = new ShopRestockUi(storage, messages, editService, iconConfig);
this.actionMenu = new ShopActionMenu(this, dialogService, messages, editService, restockUi, coOwnerFlow);
this.adminShopSlotIO = new AdminShopSlotIO(getDataFolder(), storage.slots(), storage.inventory(), Clock.systemUTC());
this.api = new ModernVillagerShopAPI(registry, storage, priceRegistry);
getServer().getServicesManager().register(ModernVillagerShopAPI.class, api, this,
org.bukkit.plugin.ServicePriority.Normal);
try {
shopService.loadAll();
} catch (SQLException ex) {
getLogger().severe("Failed to load existing shops: " + ex.getMessage());
}
var pm = getServer().getPluginManager();
pm.registerEvents(new ShopEggListener(this, eggFactory, shopService, messages), this);
pm.registerEvents(new ShopVillagerListener(registry, shopService, villagerManager, openService,
actionMenu, config, soundService, villagerTeleportGuard), this);
pm.registerEvents(new VillagerLookListener(registry, config, villagerTeleportGuard), this);
pm.registerEvents(new ShopBrowseListener(this, registry, browseUi, storage, tradeFlow, messages), this);
pm.registerEvents(new NotificationFlushListener(this, tradeNotifier), this);
pm.registerEvents(new ShopEditListener(this, registry, editUi, editService, slotEditFlow,
storage, messages), this);
pm.registerEvents(new ShopRestockListener(this, storage, messages, editService, config, restockUi), this);
pm.registerEvents(new PlayerPickerListener(this, playerPickerUi), this);
pm.registerEvents(new PlayerCacheListener(playerCacheService), this);
VShopCommand cmd = new VShopCommand(this);
getLifecycleManager().registerEventHandler(LifecycleEvents.COMMANDS,
event -> event.registrar().register(cmd.build(),
"ModernVillagerShop main command", java.util.List.of("vs")));
// Hook up PlaceholderAPI when present + enabled in config.
if (config.placeholderApiEnabled()
&& getServer().getPluginManager().isPluginEnabled("PlaceholderAPI")) {
this.papiExpansion = new MvshopPlaceholders(this);
if (papiExpansion.register()) {
getLogger().info("Registered PlaceholderAPI expansion.");
} else {
getLogger().warning("Failed to register PlaceholderAPI expansion.");
}
}
getLogger().info("ModernVillagerShop enabled (locale=" + messages.primaryLocale()
+ ", storage=" + config.storageType() + ", shops=" + registry.all().size() + ")");
}
@Override
public void onDisable() {
if (papiExpansion != null) {
try { papiExpansion.unregister(); } catch (Throwable ignored) {}
papiExpansion = null;
}
if (storage != null) {
storage.close();
storage = null;
}
getLogger().info("ModernVillagerShop disabled");
}
public void reloadConfigInternal() {
reloadConfig();
if (this.config == null) {
this.config = new PluginConfig(getConfig());
} else {
// Refresh in place so every service that already holds this reference
// (EconomyService, ShopService, TradeService, IconConfig via uiSection, ...)
// sees the new values on the next access.
this.config.reload(getConfig());
}
if (this.messages == null) {
this.messages = new MessageManager(this);
}
this.messages.load(config.locale(), config.fallbackLocale());
// Sound specs are parsed eagerly and cached, so refresh explicitly.
if (this.soundService != null) {
this.soundService.reload();
}
}
public PluginConfig pluginConfig() { return config; }
public MessageManager messages() { return messages; }
public StorageManager storage() { return storage; }
public ShopRegistry registry() { return registry; }
public ShopService shopService() { return shopService; }
public SpawnEggFactory eggFactory() { return eggFactory; }
public ShopVillagerManager villagerManager() { return villagerManager; }
public DialogService dialogService() { return dialogService; }
public ShopBrowseUi browseUi() { return browseUi; }
public ShopOpenService openService() { return openService; }
public EconomyService economyService() { return economyService; }
public TradeService tradeService() { return tradeService; }
public TradeFlow tradeFlow() { return tradeFlow; }
public TradeNotifier tradeNotifier() { return tradeNotifier; }
public ShopEditService editService() { return editService; }
public ShopEditUi editUi() { return editUi; }
public SlotEditFlow slotEditFlow() { return slotEditFlow; }
public CoOwnerFlow coOwnerFlow() { return coOwnerFlow; }
public PriceRegistry priceRegistry() { return priceRegistry; }
public PriceResolver priceResolver() { return priceResolver; }
public ModernVillagerShopAPI api() { return api; }
public PlayerCacheService playerCacheService() { return playerCacheService; }
public PlayerPickerUi playerPickerUi() { return playerPickerUi; }
public ShopRestockUi restockUi() { return restockUi; }
public ShopActionMenu actionMenu() { return actionMenu; }
public AdminShopSlotIO adminShopSlotIO() { return adminShopSlotIO; }
public SoundService soundService() { return soundService; }
}