use of de.codecrafter47.data.api.DataKey in project BungeeTabListPlus by CodeCrafter47.
the class RedisPlayerManager method onRedisMessage.
@EventHandler
@SuppressWarnings("unchecked")
public void onRedisMessage(PubSubMessageEvent event) {
String channel = event.getChannel();
if (channel.equals(CHANNEL_DATA_REQUEST)) {
ByteArrayDataInput input = ByteStreams.newDataInput(Base64.getDecoder().decode(event.getMessage()));
try {
UUID uuid = DataStreamUtils.readUUID(input);
ProxiedPlayer proxiedPlayer = ProxyServer.getInstance().getPlayer(uuid);
if (proxiedPlayer != null) {
BungeePlayer player = bungeePlayerProvider.getPlayerIfPresent(proxiedPlayer);
if (player != null) {
DataKey<?> key = DataStreamUtils.readDataKey(input, keyRegistry, missingDataKeyLogger);
if (key != null) {
player.addDataChangeListener((DataKey<Object>) key, new DataChangeListener(player, (DataKey<Object>) key));
updateData(uuid, (DataKey<Object>) key, player.get(key));
}
}
}
} catch (IOException ex) {
logger.log(Level.SEVERE, "Unexpected error reading redis message", ex);
}
} else if (channel.equals(CHANNEL_DATA_UPDATE)) {
ByteArrayDataInput input = ByteStreams.newDataInput(Base64.getDecoder().decode(event.getMessage()));
try {
UUID uuid = DataStreamUtils.readUUID(input);
RedisPlayer player = byUUID.get(uuid);
if (player != null) {
DataCache cache = player.getData();
DataKey<?> key = DataStreamUtils.readDataKey(input, keyRegistry, missingDataKeyLogger);
if (key != null) {
boolean removed = input.readBoolean();
if (removed) {
mainThread.execute(() -> cache.updateValue(key, null));
} else {
Object value = typeRegistry.getTypeAdapter(key.getType()).read(input);
mainThread.execute(() -> cache.updateValue((DataKey<Object>) key, value));
}
}
}
} catch (IOException ex) {
logger.log(Level.SEVERE, "Unexpected error reading redis message", ex);
}
} else if (channel.equals(CHANNEL_DATA_OLD) || channel.equals(CHANNEL_REQUEST_DATA_OLD)) {
logger.warning("BungeeTabListPlus on at least one proxy in your network is outdated.");
}
}
use of de.codecrafter47.data.api.DataKey in project BungeeTabListPlus by CodeCrafter47.
the class PlayerPlaceholderResolver method resolve.
@Nonnull
@Override
public PlaceholderBuilder<?, ?> resolve(PlaceholderBuilder<Player, ?> builder, List<PlaceholderArg> args, TemplateCreationContext tcc) throws UnknownPlaceholderException, PlaceholderException {
if (!args.isEmpty() && aliasMap.containsKey(args.get(0).getText())) {
String replacement = aliasMap.get(args.get(0).getText());
tcc.getErrorHandler().addWarning("Placeholder '" + args.get(0).getText() + "' has been deprecated. Use '" + replacement + "' instead.", null);
args.set(0, new PlaceholderArg.Text(replacement));
}
try {
return super.resolve(builder, args, tcc);
} catch (UnknownPlaceholderException e) {
if (!args.isEmpty()) {
String id = args.get(0).getText().toLowerCase();
PlaceholderBuilder<?, ?> result = null;
if (customPlaceholderDataKeys.containsKey(id)) {
DataKey<String> dataKey = customPlaceholderDataKeys.get(id);
result = builder.acquireData(new PlayerPlaceholderDataProviderSupplier<>(TypeToken.STRING, dataKey, (player, replacement) -> replacement), TypeToken.STRING);
} else if (bridgeCustomPlaceholderDataKeys.containsKey(id)) {
DataKey<String> dataKey = bridgeCustomPlaceholderDataKeys.get(id);
result = builder.acquireData(new PlayerPlaceholderDataProviderSupplier<>(TypeToken.STRING, dataKey, (player, replacement) -> replacement), TypeToken.STRING);
} else {
for (String prefix : placeholderAPIPluginPrefixes) {
if (id.length() >= prefix.length() && id.substring(0, prefix.length()).equalsIgnoreCase(prefix)) {
id = args.remove(0).getText();
val resolver = create(BTLPDataKeys.createPlaceholderAPIDataKey("%" + id + "%"));
addPlaceholder(id, resolver);
return resolver.resolve(builder, args, tcc);
}
}
}
if (result == null) {
// prevent errors because bridge information has not been synced yet
if (cache.getCustomPlaceholdersBridge().contains(id)) {
result = builder.acquireData(new PlayerPlaceholderDataProviderSupplier<>(TypeToken.STRING, BTLPDataKeys.createThirdPartyVariableDataKey(id), (player, replacement) -> replacement), TypeToken.STRING);
} else {
for (String prefix : cache.getPAPIPrefixes()) {
if (id.length() >= prefix.length() && id.substring(0, prefix.length()).equalsIgnoreCase(prefix)) {
result = builder.acquireData(new PlayerPlaceholderDataProviderSupplier<>(TypeToken.STRING, BTLPDataKeys.createPlaceholderAPIDataKey("%" + id + "%"), (player, replacement) -> replacement), TypeToken.STRING);
}
}
}
}
if (result != null) {
args.remove(0);
return result;
}
}
throw e;
}
}
Aggregations