Skip to content

Commit

Permalink
chore: 优化
Browse files Browse the repository at this point in the history
  • Loading branch information
mcchampions committed Feb 6, 2025
1 parent 06c7fbe commit a1ce2c3
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@
import io.github.thebusybiscuit.slimefun4.utils.ChatUtils;
import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils;
import io.github.thebusybiscuit.slimefun4.utils.HeadTexture;

import java.util.*;
import java.util.logging.Level;
import java.util.regex.Pattern;

import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ChestMenu;
import me.qscbm.slimefun4.utils.TextUtils;
import org.bukkit.Bukkit;
import org.bukkit.NamespacedKey;
import org.bukkit.OfflinePlayer;
Expand All @@ -23,6 +19,9 @@
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.persistence.PersistentDataType;

import java.util.*;
import java.util.logging.Level;

/**
* The {@link AndroidShareMenu} is responsibility to modify trusted users
* in {@link ProgrammableAndroid}.
Expand All @@ -34,7 +33,6 @@ public final class AndroidShareMenu {
private static final int DISPLAY_START_SLOT = 9;
private static final NamespacedKey BLOCK_INFO_KEY = new NamespacedKey(Slimefun.instance(), "share-users");
private static final int SHARED_USERS_LIMIT = 15;
private static final Pattern SQLIT_PATTERN = Pattern.compile(", ");

private AndroidShareMenu() {
}
Expand Down Expand Up @@ -172,7 +170,7 @@ private static List<String> parseBlockInfoToList(String value) {
if (replacedText.isEmpty()) {
return Collections.emptyList();
} else {
return new ArrayList<>(Arrays.asList(SQLIT_PATTERN.split(replacedText)));
return TextUtils.split(replacedText,", ");
}
}

Expand Down
22 changes: 12 additions & 10 deletions src/main/java/me/qscbm/slimefun4/commands/CalcCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,8 @@
import org.bukkit.inventory.ItemStack;

import java.util.*;
import java.util.regex.Pattern;

public class CalcCommand extends SubCommand {
private static final Pattern PARAM_FIRST_PATTERN = Pattern.compile("%1");
private static final Pattern PARAM_SECOND_PATTERN = Pattern.compile("%2");
private static final Pattern PARAM_THIRD_PATTERN = Pattern.compile("%3");
private static final Pattern PARAM_FOUTH_PATTERN = Pattern.compile("%4");

public CalcCommand(Slimefun plugin, SlimefunCommand cmd) {
super(plugin, cmd, "calc", false);
}
Expand Down Expand Up @@ -101,9 +95,12 @@ public static void printResults(CommandSender sender, SlimefunItem item, long am
String header;
String name = item.getItemNormalName();
if (amount == 1) {
header = PARAM_FIRST_PATTERN.matcher(Slimefun.getLocalization().getMessage("commands.calc.header-string")).replaceFirst(name);
header = Slimefun.getLocalization().getMessage("commands.calc.header-string")
.replace("%1", name);
} else {
header = PARAM_SECOND_PATTERN.matcher(PARAM_FIRST_PATTERN.matcher(Slimefun.getLocalization().getMessage("commands.calc.header-amount-string")).replaceFirst(name)).replaceFirst(String.valueOf(amount));
header = Slimefun.getLocalization().getMessage("commands.calc.header-amount-string")
.replace("%1", name)
.replace("%2", String.valueOf(amount));
}

ChatUtils.sendMessage(sender, header);
Expand All @@ -119,11 +116,16 @@ public static void printResults(CommandSender sender, SlimefunItem item, long am
if (originalValues <= maxStackSize) {
parsedAmount = Long.toString(originalValues);
} else {
parsedAmount = PARAM_FOUTH_PATTERN.matcher(PARAM_THIRD_PATTERN.matcher(PARAM_SECOND_PATTERN.matcher(PARAM_FIRST_PATTERN.matcher(Slimefun.getLocalization().getMessage("commands.calc.stack-string")).replaceFirst(String.valueOf(originalValues))).replaceFirst(String.valueOf(Math.floor(originalValues / (float) maxStackSize)))).replaceFirst(String.valueOf(maxStackSize))).replaceFirst(String.valueOf(originalValues % maxStackSize));
parsedAmount = Slimefun.getLocalization().getMessage("commands.calc.stack-string")
.replace("%1", String.valueOf(originalValues))
.replace("%2", String.valueOf(Math.floor(originalValues / (float) maxStackSize)))
.replace("%3", String.valueOf(maxStackSize))
.replace("%4", String.valueOf(originalValues % maxStackSize));
}
Slimefun.getLocalization().sendMessage(
sender, "commands.calc.amount-string", (m) ->
PARAM_SECOND_PATTERN.matcher(PARAM_FIRST_PATTERN.matcher(m).replaceFirst(ItemHelper.getItemName(entry.getKey()))).replaceFirst(parsedAmount)
m.replace("%1", ItemHelper.getItemName(entry.getKey()))
.replace("%2", parsedAmount)
);
}
});
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/me/qscbm/slimefun4/utils/TextUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;

import java.util.ArrayList;
import java.util.List;

public class TextUtils {
public static String toPlainText(Component component) {
return PlainTextComponentSerializer.plainText().serialize(component);
Expand Down Expand Up @@ -50,6 +53,26 @@ public static String toLegacyText(Component component) {
return LegacyComponentSerializer.legacyAmpersand().serialize(component);
}

public static List<String> split(String str, String character) {
int off = 0;
int next;
List<String> list = new ArrayList<>();
while ((next = str.indexOf(character, off)) != -1) {
list.add(str.substring(off, next));
off = next + 1;
}
if (off == 0)
return List.of(str);

list.add(str.substring(off));

int resultSize = list.size();
while (resultSize > 0 && list.get(resultSize - 1).isEmpty()) {
resultSize--;
}
return list.subList(0, resultSize);
}

public static String translateAlternateColorCodes(String text) {
final char[] chars = text.toCharArray();
int i = 0;
Expand Down

0 comments on commit a1ce2c3

Please sign in to comment.