MMOCore managers are now marked final

This commit is contained in:
Indyuce 2022-03-05 10:40:53 +01:00
parent 9727f02051
commit 7d5f2fd9f2
4 changed files with 82 additions and 55 deletions

View File

@ -61,11 +61,10 @@ import java.util.logging.Level;
public class MMOCore extends LuminePlugin { public class MMOCore extends LuminePlugin {
public static MMOCore plugin; public static MMOCore plugin;
public ConfigManager configManager;
public final WaypointManager waypointManager = new WaypointManager(); public final WaypointManager waypointManager = new WaypointManager();
public SoundManager soundManager; public final SoundManager soundManager = new SoundManager();
public RequestManager requestManager; public final RequestManager requestManager = new RequestManager();
public ConfigItemManager configItems; public final ConfigItemManager configItems = new ConfigItemManager();
public final PlayerActionBar actionBarManager = new PlayerActionBar(); public final PlayerActionBar actionBarManager = new PlayerActionBar();
public final SkillManager skillManager = new SkillManager(); public final SkillManager skillManager = new SkillManager();
public final ClassManager classManager = new ClassManager(); public final ClassManager classManager = new ClassManager();
@ -82,6 +81,15 @@ public class MMOCore extends LuminePlugin {
@Deprecated @Deprecated
public final SkillTreeManager skillTreeManager = new SkillTreeManager(); public final SkillTreeManager skillTreeManager = new SkillTreeManager();
// Profession managers
public final CustomBlockManager mineManager = new CustomBlockManager();
public final FishingManager fishingManager = new FishingManager();
public final AlchemyManager alchemyManager = new AlchemyManager();
public final EnchantManager enchantManager = new EnchantManager();
public final SmithingManager smithingManager = new SmithingManager();
@NotNull
public ConfigManager configManager;
public VaultEconomy economy; public VaultEconomy economy;
public RegionHandler regionHandler = new DefaultRegionHandler(); public RegionHandler regionHandler = new DefaultRegionHandler();
public PlaceholderParser placeholderParser = new DefaultParser(); public PlaceholderParser placeholderParser = new DefaultParser();
@ -91,13 +99,6 @@ public class MMOCore extends LuminePlugin {
@NotNull @NotNull
public PartyModule partyModule; public PartyModule partyModule;
// Profession managers
public final CustomBlockManager mineManager = new CustomBlockManager();
public final FishingManager fishingManager = new FishingManager();
public final AlchemyManager alchemyManager = new AlchemyManager();
public final EnchantManager enchantManager = new EnchantManager();
public final SmithingManager smithingManager = new SmithingManager();
public boolean shouldDebugSQL = false; public boolean shouldDebugSQL = false;
private static final int MYTHICLIB_COMPATIBILITY_INDEX = 7; private static final int MYTHICLIB_COMPATIBILITY_INDEX = 7;
@ -408,10 +409,9 @@ public class MMOCore extends LuminePlugin {
lootChests.initialize(clearBefore); lootChests.initialize(clearBefore);
restrictionManager.initialize(clearBefore); restrictionManager.initialize(clearBefore);
waypointManager.initialize(clearBefore); waypointManager.initialize(clearBefore);
requestManager.initialize(clearBefore);
requestManager = new RequestManager(); soundManager.initialize(clearBefore);
soundManager = new SoundManager(new ConfigFile("sounds").getConfig()); configItems.initialize(clearBefore);
configItems = new ConfigItemManager(new ConfigFile("items").getConfig());
if (getConfig().isConfigurationSection("action-bar")) if (getConfig().isConfigurationSection("action-bar"))
actionBarManager.reload(getConfig().getConfigurationSection("action-bar")); actionBarManager.reload(getConfig().getConfigurationSection("action-bar"));

View File

@ -1,18 +1,33 @@
package net.Indyuce.mmocore.manager; package net.Indyuce.mmocore.manager;
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.ConfigFile;
import net.Indyuce.mmocore.util.item.ConfigItem;
import org.bukkit.configuration.file.FileConfiguration;
import org.jetbrains.annotations.Nullable;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.logging.Level; import java.util.logging.Level;
import org.bukkit.configuration.file.FileConfiguration; public class ConfigItemManager implements MMOCoreManager {
import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.util.item.ConfigItem;
public class ConfigItemManager {
private final Map<String, ConfigItem> map = new HashMap<>(); private final Map<String, ConfigItem> map = new HashMap<>();
public ConfigItemManager(FileConfiguration config) { public void register(ConfigItem item) {
map.put(item.getId(), item);
}
@Nullable
public ConfigItem get(String id) {
return map.get(id);
}
@Override
public void initialize(boolean clearBefore) {
if (clearBefore)
map.clear();
FileConfiguration config = new ConfigFile("items").getConfig();
for (String key : config.getKeys(false)) for (String key : config.getKeys(false))
try { try {
register(new ConfigItem(config.getConfigurationSection(key))); register(new ConfigItem(config.getConfigurationSection(key)));
@ -20,12 +35,4 @@ public class ConfigItemManager {
MMOCore.plugin.getLogger().log(Level.WARNING, "Could not load config item " + key); MMOCore.plugin.getLogger().log(Level.WARNING, "Could not load config item " + key);
} }
} }
public void register(ConfigItem item) {
map.put(item.getId(), item);
}
public ConfigItem get(String id) {
return map.get(id);
}
} }

View File

@ -1,5 +1,6 @@
package net.Indyuce.mmocore.manager; package net.Indyuce.mmocore.manager;
import net.Indyuce.mmocore.api.ConfigFile;
import net.Indyuce.mmocore.api.SoundEvent; import net.Indyuce.mmocore.api.SoundEvent;
import net.Indyuce.mmocore.api.SoundObject; import net.Indyuce.mmocore.api.SoundObject;
import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.configuration.file.FileConfiguration;
@ -9,16 +10,21 @@ import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Objects; import java.util.Objects;
public class SoundManager { public class SoundManager implements MMOCoreManager {
private final Map<SoundEvent, SoundObject> sounds = new HashMap<>(); private final Map<SoundEvent, SoundObject> sounds = new HashMap<>();
public SoundManager(FileConfiguration config) {
for (SoundEvent sound : SoundEvent.values())
sounds.put(sound, new SoundObject(config.getString(sound.name().replace("_", "-").toLowerCase())));
}
@NotNull @NotNull
public SoundObject getSound(SoundEvent event) { public SoundObject getSound(SoundEvent event) {
return Objects.requireNonNull(sounds.get(event), "Could not find sound for " + event.name()); return Objects.requireNonNull(sounds.get(event), "Could not find sound for " + event.name());
} }
@Override
public void initialize(boolean clearBefore) {
if (clearBefore)
sounds.clear();
FileConfiguration config = new ConfigFile("sounds").getConfig();
for (SoundEvent sound : SoundEvent.values())
sounds.put(sound, new SoundObject(config.getString(sound.name().replace("_", "-").toLowerCase())));
}
} }

View File

@ -2,35 +2,35 @@ package net.Indyuce.mmocore.manager.social;
import net.Indyuce.mmocore.MMOCore; import net.Indyuce.mmocore.MMOCore;
import net.Indyuce.mmocore.api.player.social.Request; import net.Indyuce.mmocore.api.player.social.Request;
import net.Indyuce.mmocore.manager.MMOCoreManager;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.HashMap; import java.util.*;
import java.util.Iterator;
import java.util.Map;
import java.util.UUID;
public class RequestManager { public class RequestManager implements MMOCoreManager {
private final Map<UUID, Request> requests = new HashMap<>(); private final Map<UUID, Request> requests = new HashMap<>();
/** private boolean ENABLED;
* Flushes friend requests every 5 minutes so there is no memory overleak
*/
public RequestManager() {
Bukkit.getScheduler().runTaskTimer(MMOCore.plugin, this::flushRequests, 60 * 20, 60 * 20 * 5);
}
@NotNull
public Request getRequest(UUID uuid) { public Request getRequest(UUID uuid) {
return requests.get(uuid); return Objects.requireNonNull(requests.get(uuid), "Could not find request with UUID '" + uuid.toString() + "'");
} }
public void registerRequest(Request request) { public void registerRequest(Request request) {
requests.put(request.getUniqueId(), request); requests.put(request.getUniqueId(), request);
} }
public void unregisterRequest(UUID uuid) { @Nullable
requests.remove(uuid); public Request unregisterRequest(UUID uuid) {
return requests.remove(uuid);
} }
/**
* Flushes friend, guild or party invites every 5 minutes to prevent memory leaks
*/
private void flushRequests() { private void flushRequests() {
for (Iterator<Request> iterator = requests.values().iterator(); iterator.hasNext(); ) { for (Iterator<Request> iterator = requests.values().iterator(); iterator.hasNext(); ) {
Request next = iterator.next(); Request next = iterator.next();
@ -38,4 +38,18 @@ public class RequestManager {
iterator.remove(); iterator.remove();
} }
} }
/**
* The request map is NOT cleared, whatever state of the <code>clearBefore</code>
* boolean as it's useless because they are guaranteed to disappear with time.
*
* @param clearBefore Useless here
*/
@Override
public void initialize(boolean clearBefore) {
if (!ENABLED) {
Bukkit.getScheduler().runTaskTimer(MMOCore.plugin, this::flushRequests, 60 * 20, 60 * 20 * 5);
ENABLED = true;
}
}
} }