9
0
mirror of https://github.com/Xiao-MoMi/Custom-Fishing.git synced 2025-12-29 11:59:11 +00:00

1.2半成品

This commit is contained in:
Xiao-MoMi
2022-10-17 01:55:10 +08:00
parent cbb929c7ad
commit 3a4d91be99
180 changed files with 5390 additions and 5675 deletions

View File

@@ -0,0 +1,94 @@
/*
* Copyright (C) <2022> <XiaoMoMi>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package net.momirealms.customfishing.object;
import net.momirealms.customfishing.CustomFishing;
import net.momirealms.customfishing.integration.papi.PlaceholderManager;
import org.bukkit.entity.Player;
import java.util.ArrayList;
import java.util.List;
public class TextCache {
//所属玩家
private final Player owner;
//初始值
private final String rawValue;
//原始文字加工后的值
private String originalValue;
//最近一次替换值
private String latestValue;
//持有者占位符
private String[] ownerPlaceholders;
public TextCache(Player owner, String rawValue) {
this.owner = owner;
this.rawValue = rawValue;
analyze(this.rawValue);
}
private void analyze(String value) {
List<String> placeholdersOwner = new ArrayList<>(CustomFishing.plugin.getIntegrationManager().getPlaceholderManager().detectPlaceholders(value));
String origin = value;
for (String placeholder : placeholdersOwner) {
origin = origin.replace(placeholder, "%s");
}
originalValue = origin;
ownerPlaceholders = placeholdersOwner.toArray(new String[0]);
latestValue = originalValue;
update();
}
public String getRawValue() {
return rawValue;
}
public String updateAndGet() {
update();
return getLatestValue();
}
public String getLatestValue() {
return latestValue;
}
//返回更新结果是否不一致
public boolean update() {
if (ownerPlaceholders.length == 0) return false;
PlaceholderManager placeholderManager = CustomFishing.plugin.getIntegrationManager().getPlaceholderManager();
if (placeholderManager == null) return false;
String string;
if ("%s".equals(originalValue)) {
string = placeholderManager.parse(owner, ownerPlaceholders[0]);
}
else {
Object[] values = new String[ownerPlaceholders.length];
for (int i = 0; i < ownerPlaceholders.length; i++) {
values[i] = placeholderManager.parse(owner, ownerPlaceholders[i]);
}
string = String.format(originalValue, values);
}
if (!latestValue.equals(string)) {
latestValue = string;
return true;
}
return false;
}
}