use of com.loohp.interactivechat.objectholders.ICPlayer in project InteractiveChat by LOOHP.
the class SenderFinder method getSender.
public static Optional<ICPlayer> getSender(Component component, String messageKey) {
ICPlayer keyPlayer = InteractiveChat.keyPlayer.get(messageKey);
if (keyPlayer != null) {
return Optional.of(keyPlayer);
}
String chat = PlainTextComponentSerializer.plainText().serialize(component);
for (Entry<String, UUID> entry : InteractiveChat.messages.entrySet()) {
String msg = entry.getKey();
if (chat.contains(msg)) {
UUID uuid = entry.getValue();
Bukkit.getScheduler().runTaskLaterAsynchronously(InteractiveChat.plugin, () -> InteractiveChat.messages.remove(msg), 5);
ICPlayer wplayer = ICPlayerFactory.getICPlayer(uuid);
if (wplayer != null) {
return Optional.of(wplayer);
}
}
}
String mostsimular = null;
double currentsim = 0.5;
for (Entry<String, UUID> entry : InteractiveChat.messages.entrySet()) {
String msg = entry.getKey();
double sim = CustomStringUtils.similarity(chat, msg);
if (sim > currentsim) {
mostsimular = msg;
currentsim = sim;
}
}
if (mostsimular != null) {
UUID uuid = InteractiveChat.messages.get(mostsimular);
String finalmostsimular = mostsimular;
Bukkit.getScheduler().runTaskLaterAsynchronously(InteractiveChat.plugin, () -> InteractiveChat.messages.remove(finalmostsimular), 5);
ICPlayer wplayer = ICPlayerFactory.getICPlayer(uuid);
if (wplayer != null) {
return Optional.of(wplayer);
}
}
Map<String, UUID> names = new HashMap<>();
ICPlayerFactory.getOnlineICPlayers().forEach((each) -> {
names.put(ChatColorUtils.stripColor(each.getName()), each.getUniqueId());
if (!ChatColorUtils.stripColor(each.getName()).equals(ChatColorUtils.stripColor(each.getDisplayName()))) {
names.put(ChatColorUtils.stripColor(each.getDisplayName()), each.getUniqueId());
}
});
Bukkit.getOnlinePlayers().forEach(each -> {
List<String> list = InteractiveChatAPI.getNicknames(each.getUniqueId());
for (String name : list) {
names.put(ChatColorUtils.stripColor(name), each.getUniqueId());
}
});
UUID currentplayer = null;
int currentpos = 99999;
for (Entry<String, UUID> entry : names.entrySet()) {
int pos = chat.toLowerCase().indexOf(entry.getKey().toLowerCase());
if (pos >= 0 && pos < currentpos) {
currentpos = pos;
currentplayer = entry.getValue();
}
}
if (currentplayer != null) {
ICPlayer wplayer = ICPlayerFactory.getICPlayer(currentplayer);
if (wplayer != null) {
return Optional.of(wplayer);
}
}
return Optional.empty();
}
use of com.loohp.interactivechat.objectholders.ICPlayer in project InteractiveChat by LOOHP.
the class CustomPlaceholderDisplay method process.
public static Component process(Component component, Optional<ICPlayer> optplayer, Player reciever, Collection<ICPlaceholder> placeholderList, long unix) {
for (ICPlaceholder icplaceholder : placeholderList) {
if (icplaceholder.isBuildIn()) {
continue;
}
CustomPlaceholder cp = (CustomPlaceholder) icplaceholder;
ICPlayer parseplayer = (cp.getParsePlayer().equals(ParsePlayer.SENDER) && optplayer.isPresent()) ? optplayer.get() : ICPlayerFactory.getICPlayer(reciever);
if (InteractiveChat.useCustomPlaceholderPermissions && optplayer.isPresent()) {
ICPlayer sender = optplayer.get();
if (!PlayerUtils.hasPermission(sender.getUniqueId(), cp.getPermission(), true, 5)) {
continue;
}
}
String placeholder = cp.getKeyword().pattern();
placeholder = (cp.getParseKeyword()) ? PlaceholderParser.parse(parseplayer, placeholder) : placeholder;
long cooldown = cp.getCooldown();
boolean hoverEnabled = cp.getHover().isEnabled();
String hoverText = cp.getHover().getText();
boolean clickEnabled = cp.getClick().isEnabled();
ClickEventAction clickAction = cp.getClick().getAction();
String clickValue = cp.getClick().getValue();
boolean replaceEnabled = cp.getReplace().isEnabled();
String replaceText = cp.getReplace().getReplaceText();
component = processCustomPlaceholder(parseplayer, Pattern.compile(placeholder), cooldown, hoverEnabled, hoverText, clickEnabled, clickAction, clickValue, replaceEnabled, replaceText, component, optplayer, unix);
}
if (InteractiveChat.t && WebData.getInstance() != null) {
for (CustomPlaceholder cp : WebData.getInstance().getSpecialPlaceholders()) {
ICPlayer parseplayer = (cp.getParsePlayer().equals(ParsePlayer.SENDER) && optplayer.isPresent()) ? optplayer.get() : ICPlayerFactory.getICPlayer(reciever);
String placeholder = cp.getKeyword().pattern();
placeholder = (cp.getParseKeyword()) ? PlaceholderParser.parse(parseplayer, placeholder) : placeholder;
long cooldown = cp.getCooldown();
boolean hoverEnabled = cp.getHover().isEnabled();
String hoverText = cp.getHover().getText();
boolean clickEnabled = cp.getClick().isEnabled();
ClickEventAction clickAction = cp.getClick().getAction();
String clickValue = cp.getClick().getValue();
boolean replaceEnabled = cp.getReplace().isEnabled();
String replaceText = cp.getReplace().getReplaceText();
component = processCustomPlaceholder(parseplayer, Pattern.compile(placeholder), cooldown, hoverEnabled, hoverText, clickEnabled, clickAction, clickValue, replaceEnabled, replaceText, component, optplayer, unix);
}
}
return component;
}
use of com.loohp.interactivechat.objectholders.ICPlayer in project InteractiveChat by LOOHP.
the class ItemDisplay method process.
@SuppressWarnings("deprecation")
public static Component process(Component component, Optional<ICPlayer> optplayer, Player receiver, long unix) throws Exception {
String plain = InteractiveChatComponentSerializer.plainText().serialize(component);
if (InteractiveChat.itemPlaceholder.matcher(plain).find()) {
String regex = InteractiveChat.itemPlaceholder.pattern();
if (InteractiveChat.bungeecordMode && optplayer.isPresent() && optplayer.get().isLocal()) {
ICPlayer player = optplayer.get();
ItemStack[] equipment;
if (InteractiveChat.version.isOld()) {
equipment = new ItemStack[] { player.getEquipment().getHelmet(), player.getEquipment().getChestplate(), player.getEquipment().getLeggings(), player.getEquipment().getBoots(), player.getEquipment().getItemInHand() };
} else {
equipment = new ItemStack[] { player.getEquipment().getHelmet(), player.getEquipment().getChestplate(), player.getEquipment().getLeggings(), player.getEquipment().getBoots(), player.getEquipment().getItemInMainHand(), player.getEquipment().getItemInOffHand() };
}
try {
BungeeMessageSender.forwardEquipment(unix, player.getUniqueId(), player.isRightHanded(), player.getSelectedSlot(), player.getExperienceLevel(), equipment);
} catch (IOException e) {
e.printStackTrace();
}
}
if (optplayer.isPresent()) {
ICPlayer player = optplayer.get();
if (PlayerUtils.hasPermission(player.getUniqueId(), "interactivechat.module.item", true, 5)) {
Component alternativeHover = null;
if (!InteractiveChat.itemHover && !InteractiveChat.itemAlternativeHoverMessage.isEmpty()) {
alternativeHover = LegacyComponentSerializer.legacySection().deserialize(InteractiveChat.itemAlternativeHoverMessage);
}
Component itemComponent = ComponentFlattening.flatten(createItemDisplay(player, receiver, component, unix, InteractiveChat.itemHover, alternativeHover));
component = ComponentReplacing.replace(component, regex, true, itemComponent);
}
} else {
Component message;
if (InteractiveChat.playerNotFoundReplaceEnable) {
message = LegacyComponentSerializer.legacySection().deserialize(InteractiveChat.playerNotFoundReplaceText.replace("{Placeholder}", InteractiveChat.itemName));
} else {
message = Component.text(InteractiveChat.itemName);
}
if (InteractiveChat.playerNotFoundHoverEnable && InteractiveChat.itemHover) {
message = message.hoverEvent(HoverEvent.showText(LegacyComponentSerializer.legacySection().deserialize(InteractiveChat.playerNotFoundHoverText.replace("{Placeholder}", InteractiveChat.itemName))));
}
if (InteractiveChat.playerNotFoundClickEnable) {
String clickValue = ChatColorUtils.translateAlternateColorCodes('&', InteractiveChat.playerNotFoundClickValue.replace("{Placeholder}", InteractiveChat.itemName));
message = message.clickEvent(ClickEvent.clickEvent(ClickEvent.Action.valueOf(InteractiveChat.playerNotFoundClickAction), clickValue));
}
component = ComponentReplacing.replace(component, regex, true, message);
}
}
return component;
}
use of com.loohp.interactivechat.objectholders.ICPlayer in project InteractiveChat by LOOHP.
the class PlayernameDisplay method processPlayer.
private static Component processPlayer(String placeholder, ICPlayer player, Optional<ICPlayer> sender, Player receiver, Component component, Set<HoverEvent<?>> doNotReplace, long unix) {
String plain = InteractiveChatComponentSerializer.plainText().serialize(component);
if (InteractiveChat.usePlayerNameCaseSensitive) {
if (!plain.contains(placeholder)) {
return component;
}
} else {
if (!plain.toLowerCase().contains(placeholder.toLowerCase())) {
return component;
}
}
HoverEvent<?> hoverEvent;
ClickEvent clickEvent;
if (InteractiveChat.usePlayerNameHoverEnable) {
String playertext = ChatColorUtils.translateAlternateColorCodes('&', PlaceholderParser.parse(player, InteractiveChat.usePlayerNameHoverText));
hoverEvent = HoverEvent.showText(LegacyComponentSerializer.legacySection().deserialize(playertext));
} else {
hoverEvent = null;
}
if (InteractiveChat.usePlayerNameClickEnable) {
String playertext = PlaceholderParser.parse(player, InteractiveChat.usePlayerNameClickValue);
clickEvent = ClickEvent.clickEvent(ClickEvent.Action.valueOf(InteractiveChat.usePlayerNameClickAction), playertext);
} else {
clickEvent = null;
}
String regex = InteractiveChat.usePlayerNameCaseSensitive ? CustomStringUtils.escapeMetaCharacters(placeholder) : "(?i)" + CustomStringUtils.escapeMetaCharacters(placeholder);
component = ComponentReplacing.replace(component, regex, true, (result, replaced) -> {
List<Component> children = new ArrayList<>();
boolean doNotReplaceFlag = false;
for (Component c : replaced) {
if (doNotReplace.contains(c.hoverEvent())) {
doNotReplaceFlag = true;
break;
}
Component edited = c;
if (hoverEvent != null && (InteractiveChat.usePlayerNameOverrideHover || edited.hoverEvent() == null)) {
edited = edited.hoverEvent(hoverEvent);
}
if (clickEvent != null && (InteractiveChat.usePlayerNameOverrideClick || edited.clickEvent() == null)) {
edited = edited.clickEvent(clickEvent);
}
children.add(edited);
}
if (doNotReplaceFlag) {
return Component.empty().children(replaced);
}
return Component.empty().children(children);
});
List<Component> children = new ArrayList<>(component.children());
for (int i = 0; i < children.size(); i++) {
Component child = children.get(i);
if (child instanceof TranslatableComponent) {
TranslatableComponent trans = (TranslatableComponent) child;
List<Component> withs = new ArrayList<>(trans.args());
withs.replaceAll(with -> processPlayer(placeholder, player, sender, receiver, with, doNotReplace, unix));
trans = trans.args(withs);
children.set(i, trans);
}
}
doNotReplace.add(hoverEvent);
return ComponentCompacting.optimize(component.children(children));
}
use of com.loohp.interactivechat.objectholders.ICPlayer in project InteractiveChat by LOOHP.
the class ProcessExternalMessage method processAndRespond0.
public String processAndRespond0(Player receiver, String json) throws Exception {
Component component = InteractiveChatComponentSerializer.gson().deserialize(json);
Component originalComponent = component;
try {
if (LegacyComponentSerializer.legacySection().serialize(component).isEmpty()) {
return json;
}
} catch (Exception e) {
return json;
}
if ((InteractiveChat.version.isOld()) && JsonUtils.containsKey(InteractiveChatComponentSerializer.gson().serialize(component), "translate")) {
return json;
}
Optional<ICPlayer> sender = Optional.empty();
String rawMessageKey = InteractiveChatComponentSerializer.plainText().serializeOr(component, "");
InteractiveChat.keyTime.putIfAbsent(rawMessageKey, System.currentTimeMillis());
Long timeKey = InteractiveChat.keyTime.get(rawMessageKey);
long unix = timeKey == null ? System.currentTimeMillis() : timeKey;
ProcessSenderResult commandSender = ProcessCommands.process(component);
if (commandSender.getSender() != null) {
sender = Optional.ofNullable(ICPlayerFactory.getICPlayer(commandSender.getSender()));
}
ProcessSenderResult chatSender = null;
if (!sender.isPresent()) {
if (InteractiveChat.useAccurateSenderFinder) {
chatSender = ProcessAccurateSender.process(component);
if (chatSender.getSender() != null) {
sender = Optional.ofNullable(ICPlayerFactory.getICPlayer(chatSender.getSender()));
}
}
}
if (!sender.isPresent() && !InteractiveChat.useAccurateSenderFinder) {
sender = SenderFinder.getSender(component, rawMessageKey);
}
component = commandSender.getComponent();
if (chatSender != null) {
component = chatSender.getComponent();
}
String text = LegacyComponentSerializer.legacySection().serialize(component);
if (InteractiveChat.messageToIgnore.stream().anyMatch(each -> text.matches(each))) {
return json;
}
if (sender.isPresent()) {
InteractiveChat.keyPlayer.put(rawMessageKey, sender.get());
}
String server;
if (sender.isPresent() && !sender.get().isLocal()) {
try {
TimeUnit.MILLISECONDS.sleep(InteractiveChat.remoteDelay);
} catch (InterruptedException e) {
e.printStackTrace();
}
server = sender.get().getServer();
} else {
server = ICPlayer.LOCAL_SERVER_REPRESENTATION;
}
component = ComponentModernizing.modernize(component);
component = ComponentReplacing.replace(component, Registry.ID_PATTERN.pattern(), Component.empty());
if (InteractiveChat.usePlayerName) {
component = PlayernameDisplay.process(component, sender, receiver, unix);
}
if (InteractiveChat.allowMention && sender.isPresent()) {
PlayerData data = InteractiveChat.playerDataManager.getPlayerData(receiver);
if (data == null || !data.isMentionDisabled()) {
component = MentionDisplay.process(component, receiver, sender.get(), unix, !Bukkit.isPrimaryThread());
}
}
component = ComponentReplacing.replace(component, Registry.MENTION_TAG_CONVERTER.getReversePattern().pattern(), true, (result, components) -> {
return LegacyComponentSerializer.legacySection().deserialize(result.group(2));
});
Collection<ICPlaceholder> serverPlaceholderList = InteractiveChat.remotePlaceholderList.get(server);
if (server.equals(ICPlayer.LOCAL_SERVER_REPRESENTATION) || serverPlaceholderList == null) {
serverPlaceholderList = InteractiveChat.placeholderList.values();
}
component = CustomPlaceholderDisplay.process(component, sender, receiver, serverPlaceholderList, unix);
if (InteractiveChat.useItem) {
component = ItemDisplay.process(component, sender, receiver, unix);
}
if (InteractiveChat.useInventory) {
component = InventoryDisplay.process(component, sender, receiver, unix);
}
if (InteractiveChat.useEnder) {
component = EnderchestDisplay.process(component, sender, receiver, unix);
}
if (InteractiveChat.clickableCommands) {
component = CommandsDisplay.process(component);
}
if (InteractiveChat.version.isNewerOrEqualTo(MCVersion.V1_16) && InteractiveChat.fontTags) {
if (!sender.isPresent() || (sender.isPresent() && PlayerUtils.hasPermission(sender.get().getUniqueId(), "interactivechat.customfont.translate", true, 5))) {
component = ComponentFont.parseFont(component);
}
}
Bukkit.getScheduler().runTaskLater(InteractiveChat.plugin, () -> {
InteractiveChat.keyTime.remove(rawMessageKey);
InteractiveChat.keyPlayer.remove(rawMessageKey);
}, 5);
String newJson = InteractiveChatComponentSerializer.gson().serialize(component);
if (InteractiveChat.sendOriginalIfTooLong && newJson.length() > InteractiveChat.packetStringMaxLength) {
String originalJson = InteractiveChatComponentSerializer.gson().serialize(originalComponent);
if (originalJson.length() > InteractiveChat.packetStringMaxLength) {
return "{\"text\":\"\"}";
} else {
return originalJson;
}
}
return newJson;
}
Aggregations