Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ private boolean checkConfigVersion() {

private void loadValues() {

ConfigFile.Language.LangFiles.clear();
ConfigFile.Language.LangFiles.addAll(config.getStringList("Language.LangFiles"));
ConfigFile.Language.DefaultLanguage = config.getString("Language.DefaultLanguage");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,37 +10,38 @@
import org.bukkit.block.Block;
import org.bukkit.entity.Player;

import java.util.HashSet;
import java.util.Iterator;
import java.util.UUID;

public class ActionBarDisplayTask implements Runnable {
@Override
public void run() {
for (UUID uuid : new HashSet<>(PlayerDataManager.getDisplaySet())) {
Iterator<UUID> iterator = PlayerDataManager.getDisplaySet().iterator();
while (iterator.hasNext()) {
UUID uuid = iterator.next();
Player player = Bukkit.getPlayer(uuid);

Block block;

// player is offline or something
if (player == null) {
iterator.remove();
continue;
}
block = player.getTargetBlockExact(5);

if (!player.hasPermission("debugstickpro.use")) {
PlayerDataManager.removePlayerFromDisplayList(uuid);
removeFromDisplayList(iterator, uuid);
continue;
}

if (!DebugStickItem.checkPlayer(player)) {
PlayerDataManager.removePlayerFromDisplayList(uuid);
removeFromDisplayList(iterator, uuid);
continue;
}

PlayerData playerData = PlayerDataManager.getOrCreatePlayerData(uuid);

switch (playerData.getDebugStickMode()) {
case CLASSIC:
Block block = player.getTargetBlockExact(5);
if (block == null) {
ActionbarUtil.removeActionBar(uuid);
continue;
Expand All @@ -56,4 +57,9 @@ public void run() {
}
}
}

private void removeFromDisplayList(Iterator<UUID> iterator, UUID uuid) {
iterator.remove();
ActionbarUtil.removeActionBar(uuid);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@

public class ActionbarUtil {

private static final MiniMessage MINI_MESSAGE = MiniMessage.miniMessage();
private static final Set<UUID> lastIsRemove = new java.util.HashSet<>();

public static void removeActionBar(UUID uuid) {
Player player = Bukkit.getPlayer(uuid);
if (lastIsRemove.contains(uuid)) {
Expand All @@ -23,8 +25,7 @@ public static void removeActionBar(UUID uuid) {
}

public static void sendActionBar(Player player, String message) {
var mm = MiniMessage.miniMessage();
Component parsed = mm.deserialize(message);
Component parsed = MINI_MESSAGE.deserialize(message);
player.sendActionBar(parsed);
lastIsRemove.remove(player.getUniqueId());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ public class LangFileReader {
/**
* This is the cache of the language file
*/
private static HashMap<String,String> cache = new HashMap<>();
private static final HashMap<String, String> cache = new HashMap<>();
private static final HashMap<String, String> optionalStringCache = new HashMap<>();

/**
* Clear the cache of the language file
*/
public static void clearCache() {
cache.clear();
optionalStringCache.clear();
}

/**
Expand Down Expand Up @@ -99,15 +101,19 @@ public boolean checkLangFileVersion() {
*/
public String getString(String key) {

if (cache.containsKey(locale + key)) {
return cache.get(locale + key);
String cacheKey = cacheKey(key);
if (cache.containsKey(cacheKey)) {
return cache.get(cacheKey);
}

String value;
try {
if (this.langFile.getString(key) == null) {
value = this.langFile.getString(key);
if (value == null) {
set(key, LangFileManager.getLang("en_US").getString(key));
Log.warning("Missing key: " + key + " in " + locale + ".yml");
if (this.langFile.getString(key) == null) {
value = this.langFile.getString(key);
if (value == null) {
LangFileManager.getLang("en_US").set(key, "Missing...");
return "Missing key: \"" + key + "\" in en_US.yml";
}
Expand All @@ -117,8 +123,8 @@ public String getString(String key) {
return "Missing key: \"" + key + "\"" + " in " + locale + ".yml";
}

cache.put(locale + key, this.langFile.getString(key));
return this.langFile.getString(key);
cache.put(cacheKey, value);
return value;
}

/**
Expand All @@ -128,7 +134,14 @@ public String getString(String key) {
* @return the string of the key, or null if it is not configured
*/
public String getOptionalString(String key) {
return this.langFile.getString(key);
String cacheKey = cacheKey(key);
if (optionalStringCache.containsKey(cacheKey)) {
return optionalStringCache.get(cacheKey);
}

String value = this.langFile.getString(key);
optionalStringCache.put(cacheKey, value);
return value;
}

/**
Expand Down Expand Up @@ -164,9 +177,15 @@ public List<String> getList(String key) {
*/
public void set(String path, Object value) {
this.langFile.set(path, value);
cache.remove(cacheKey(path));
optionalStringCache.remove(cacheKey(path));
save();
}

private String cacheKey(String key) {
return locale + ":" + key;
}

/**
* Save the language file
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,24 @@ public static String getDisplay(UUID playerUUID, BlockData blockData) {
}

int sort = 0;
int displayListSize = displayList.size();
if (ConfigFile.ActionBarDisplay.AutoToCenter) {
sort = selectedIndex - displayList.size() / 2 + 1 + displayList.size() - (displayList.size() % 2);
sort = selectedIndex - displayListSize / 2 + 1 + displayListSize - (displayListSize % 2);
}

// 排序顯示順序
StringBuilder stringBuilder = new StringBuilder();
StringBuilder stringBuilder = new StringBuilder(displayListSize * 32);
String selectedDataFormat = I18n.string(playerUUID, Lang.ActionBar.SelectedDataFormat);
String notSelectedDataFormat = I18n.string(playerUUID, Lang.ActionBar.NotSelectedDataFormat);

for (int i = 0; i < displayList.size(); i++) {
SubBlockData subBlockData = displayList.get((i + sort) % displayList.size());
for (int i = 0; i < displayListSize; i++) {
SubBlockData subBlockData = displayList.get((i + sort) % displayListSize);
String value = I18n.blockDataValue(playerUUID, subBlockData);
String dataName = I18n.string(playerUUID, subBlockData.dataName());
if (subBlockData.isUsing()) {
stringBuilder.append(Lang.ActionBar.formatSelectedData(I18n.string(playerUUID, Lang.ActionBar.SelectedDataFormat), I18n.string(playerUUID, subBlockData.dataName()), value)).append(" ");
stringBuilder.append(Lang.ActionBar.formatSelectedData(selectedDataFormat, dataName, value)).append(" ");
} else {
stringBuilder.append(Lang.ActionBar.formatNotSelectedData(I18n.string(playerUUID, Lang.ActionBar.NotSelectedDataFormat), I18n.string(playerUUID, subBlockData.dataName()), value)).append(" ");
stringBuilder.append(Lang.ActionBar.formatNotSelectedData(notSelectedDataFormat, dataName, value)).append(" ");
}
}
return stringBuilder.toString();
Expand Down
10 changes: 6 additions & 4 deletions src/main/java/dev/twme/debugstickpro/utils/DebugStickItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,20 @@
import org.bukkit.persistence.PersistentDataType;

public final class DebugStickItem {
private static final MiniMessage MINI_MESSAGE = MiniMessage.miniMessage();

public static boolean isDebugStickItem(ItemStack item) {
if (item == null) {
return false;
}
if (item.getType() != ConfigFile.DebugStickItem.Material) {
return false;
}
if (item.getItemMeta() == null) {
ItemMeta itemMeta = item.getItemMeta();
if (itemMeta == null) {
return false;
}
if (!item.getItemMeta().getPersistentDataContainer().has(PersistentKeys.DEBUG_STICK_ITEM)) {
if (!itemMeta.getPersistentDataContainer().has(PersistentKeys.DEBUG_STICK_ITEM)) {
return false;
}
return true;
Expand All @@ -34,8 +37,7 @@ public static boolean checkPlayer(Player player) {
public static ItemStack getDebugStickItem() {
ItemStack itemStack = new ItemStack(ConfigFile.DebugStickItem.Material);
ItemMeta itemMeta = itemStack.getItemMeta();
MiniMessage mm = MiniMessage.miniMessage();
Component displayName = mm.deserialize(ConfigFile.DebugStickItem.DisplayName);
Component displayName = MINI_MESSAGE.deserialize(ConfigFile.DebugStickItem.DisplayName);
itemMeta.displayName(displayName);
itemMeta.lore(ConfigFile.DebugStickItem.Lore);
itemMeta.getPersistentDataContainer().set(PersistentKeys.DEBUG_STICK_ITEM, PersistentDataType.STRING, "debugstickpro");
Expand Down