9
0
mirror of https://github.com/Xiao-MoMi/Custom-Nameplates.git synced 2025-12-25 01:49:16 +00:00

preparation for the new width system

This commit is contained in:
XiaoMoMi
2024-01-18 02:54:28 +08:00
parent d9df9b22ae
commit 8dd4745c15
2 changed files with 71 additions and 3 deletions

View File

@@ -11,6 +11,7 @@ import net.momirealms.customnameplates.paper.helper.LibraryLoader;
import net.momirealms.customnameplates.paper.mechanic.actionbar.ActionBarManagerImpl;
import net.momirealms.customnameplates.paper.mechanic.background.BackGroundManagerImpl;
import net.momirealms.customnameplates.paper.mechanic.bossbar.BossBarManagerImpl;
import net.momirealms.customnameplates.paper.mechanic.font.WidthManagerImpl;
import net.momirealms.customnameplates.paper.mechanic.image.ImageManagerImpl;
import net.momirealms.customnameplates.paper.mechanic.misc.CoolDownManager;
import net.momirealms.customnameplates.paper.mechanic.misc.PacketManager;
@@ -55,6 +56,7 @@ public class CustomNameplatesPluginImpl extends CustomNameplatesPlugin {
this.resourcePackManager = new ResourcePackManagerImpl(this);
this.nameplateManager = new NameplateManagerImpl(this);
this.teamManager = new TeamManagerImpl(this);
this.widthManager = new WidthManagerImpl(this);
this.actionBarManager = new ActionBarManagerImpl(this);
this.coolDownManager = new CoolDownManager(this);
this.packetManager = new PacketManager(this);
@@ -78,6 +80,7 @@ public class CustomNameplatesPluginImpl extends CustomNameplatesPlugin {
((PlaceholderManagerImpl) this.placeholderManager).unload();
((RequirementManagerImpl) this.requirementManager).unload();
((ResourcePackManagerImpl) this.resourcePackManager).unload();
((WidthManagerImpl) this.widthManager).unload();
((StorageManagerImpl) this.storageManager).disable();
((AdventureManagerImpl) this.adventureManager).close();
}
@@ -87,6 +90,7 @@ public class CustomNameplatesPluginImpl extends CustomNameplatesPlugin {
CNConfig.load();
CNLocale.load();
((SchedulerImpl) this.scheduler).reload();
((WidthManagerImpl) this.widthManager).reload();
((NameplateManagerImpl) this.nameplateManager).reload();
((BackGroundManagerImpl) this.backGroundManager).reload();
((TeamManagerImpl) this.teamManager).reload();

View File

@@ -1,14 +1,24 @@
package net.momirealms.customnameplates.paper.mechanic.font;
import me.clip.placeholderapi.PlaceholderAPI;
import net.momirealms.customnameplates.api.CustomNameplatesPlugin;
import net.momirealms.customnameplates.api.common.Key;
import net.momirealms.customnameplates.api.manager.WidthManager;
import net.momirealms.customnameplates.api.mechanic.font.FontData;
import net.momirealms.customnameplates.api.util.LogUtils;
import net.momirealms.customnameplates.paper.adventure.AdventureManagerImpl;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.configuration.file.YamlConfiguration;
import org.jetbrains.annotations.Nullable;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
public class WidthManagerImpl implements WidthManager {
private final CustomNameplatesPlugin plugin;
private final HashMap<String, FontData> fontDataMap;
private final HashMap<Key, FontData> fontDataMap;
private char TAG_START = '<';
private char TAG_END = '>';
@@ -21,17 +31,71 @@ public class WidthManagerImpl implements WidthManager {
}
public void reload() {
unload();
load();
}
public void load() {
YamlConfiguration config = plugin.getConfig("configs" + File.separator + "image-width.yml");
for (Map.Entry<String, Object> entry : config.getValues(false).entrySet()) {
if (entry.getValue() instanceof ConfigurationSection innerSection) {
FontData fontData = new FontData(8);
for (Map.Entry<String, Object> innerEntry : innerSection.getValues(false).entrySet()) {
String key = innerEntry.getKey();
if (key.contains("%") && !key.equals("%")) {
String stripped = AdventureManagerImpl.getInstance().stripTags(AdventureManagerImpl.getInstance().legacyToMiniMessage(PlaceholderAPI.setPlaceholders(null, key)));
if (stripped.length() != 1) {
LogUtils.warn(key + " is not a supported placeholder");
continue;
}
fontData.registerCharWidth(stripped.charAt(0), (Integer) entry.getValue());
} else if (key.length() == 1) {
fontData.registerCharWidth(key.charAt(0), (Integer) entry.getValue());
} else {
LogUtils.warn("Illegal image format: " + key);
}
}
}
}
}
public void unload() {
fontDataMap.clear();
}
@Override
public boolean registerFontData(Key key, FontData fontData) {
if (fontDataMap.containsKey(key)) {
return false;
}
fontDataMap.put(key, fontData);
return true;
}
@Override
public boolean unregisterFontData(Key key) {
return fontDataMap.remove(key) != null;
}
@Override
public void registerImageWidth(Key key, char c, int width) {
FontData fontData = fontDataMap.get(key);
if (fontData == null) {
FontData newData = new FontData(8);
fontDataMap.put(key, newData);
newData.registerCharWidth(c, width);
} else {
fontData.registerCharWidth(c, width);
}
}
@Nullable
@Override
public FontData getFontData(Key key) {
return fontDataMap.get(key);
}
@Override
public int getTextWidth(String textWithTags) {
return 0;
}