use of com.almuradev.almura.feature.exchange.basic.BasicExchange in project Almura by AlmuraDev.
the class ClientboundExchangeRegistryPacket method readFrom.
@Override
public void readFrom(final ChannelBuf buf) {
final int count = buf.readInteger();
if (count > 0) {
this.exchanges = new HashSet<>();
for (int i = 0; i < count; i++) {
final String id = buf.readString();
final String name = buf.readString();
final Instant created;
try {
created = SerializationUtil.bytesToObject(buf.readBytes(buf.readInteger()));
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
continue;
}
final UUID creator = SerializationUtil.uniqueIdFromBytes(buf.readBytes(buf.readInteger()));
final String creatorName = buf.readBoolean() ? buf.readString() : null;
final String permission = buf.readString();
final boolean isHidden = buf.readBoolean();
final BasicExchange axs = new BasicExchange(id, created, creator, name, permission, isHidden);
axs.setCreatorName(creatorName);
this.exchanges.add(axs);
}
}
}
use of com.almuradev.almura.feature.exchange.basic.BasicExchange in project Almura by AlmuraDev.
the class ServerExchangeManager method handleExchangeAdd.
public void handleExchangeAdd(final Player player, final String id, final String name, final String permission, final boolean isHidden) {
checkNotNull(player);
checkNotNull(id);
checkNotNull(name);
checkNotNull(permission);
if (!player.hasPermission(Almura.ID + ".exchange.add")) {
this.notificationManager.sendPopupNotification(player, Text.of(TextColors.RED, "Exchange"), Text.of("You do not have permission " + "to add exchanges!"), 5);
return;
}
if (this.getExchange(id).isPresent()) {
this.logger.error("Player '{}' attempted to add exchange '{}' but it already exists. Syncing exchange registry...", player.getName(), id);
this.syncExchangeRegistryTo(player);
return;
}
final UUID creator = player.getUniqueId();
this.scheduler.createTaskBuilder().async().execute(() -> {
try (final DSLContext context = this.databaseManager.createContext(true)) {
final Instant created = Instant.now();
final int result = ExchangeQueries.createInsertExchange(created, creator, id, name, permission, isHidden).build(context).keepStatement(false).execute();
if (result == 0) {
this.logger.error("Player '{}' submitted a new exchange '{}' to the database but it failed. Discarding changes...", player.getName(), id);
return;
}
this.scheduler.createTaskBuilder().execute(() -> {
final BasicExchange basicExchange = new BasicExchange(id, created, creator, name, permission, isHidden);
basicExchange.syncCreatorNameToUniqueId();
this.exchanges.put(id, basicExchange);
Sponge.getServer().getOnlinePlayers().forEach(this::syncExchangeRegistryTo);
}).submit(this.container);
} catch (SQLException e) {
e.printStackTrace();
}
}).submit(this.container);
}
use of com.almuradev.almura.feature.exchange.basic.BasicExchange in project Almura by AlmuraDev.
the class ServerExchangeManager method loadExchanges.
/**
* Exchange
*/
private void loadExchanges() {
final Map<String, Exchange> exchanges = new HashMap<>();
try (final DSLContext context = this.databaseManager.createContext(true)) {
final Results results = ExchangeQueries.createFetchAllExchanges().build(context).keepStatement(false).fetchMany();
results.forEach(result -> {
for (Record record : result) {
final String id = record.getValue(Axs.AXS.ID);
final Timestamp created = record.getValue(Axs.AXS.CREATED);
final UUID creator = SerializationUtil.uniqueIdFromBytes(record.getValue(Axs.AXS.CREATOR));
final String name = record.getValue(Axs.AXS.NAME);
final String permission = record.getValue(Axs.AXS.PERMISSION);
final boolean isHidden = record.getValue(Axs.AXS.IS_HIDDEN);
this.logger.info("Loaded exchange '{}' ({})", name, id);
exchanges.put(id, new BasicExchange(id, created.toInstant(), creator, name, permission, isHidden));
}
});
} catch (SQLException e) {
e.printStackTrace();
}
this.scheduler.createTaskBuilder().execute(() -> {
this.exchanges.clear();
this.exchanges.putAll(exchanges);
if (this.exchanges.isEmpty()) {
final BasicExchange exchange = new BasicExchange("almura.exchange.global", Instant.now(), FeatureConstants.UNKNOWN_OWNER, "Global " + "Exchange", "almura.exchange.global", false);
this.exchanges.put("almura.exchange.global", exchange);
// Yes, I am purposely running this sync
try (final DSLContext context1 = this.databaseManager.createContext(true)) {
ExchangeQueries.createInsertExchange(exchange.getCreated(), exchange.getCreator(), exchange.getId(), exchange.getName(), exchange.getPermission(), exchange.isHidden()).build(context1).keepStatement(false).execute();
this.logger.info("Loaded exchange '{}' ({})", exchange.getName(), exchange.getId());
} catch (SQLException e) {
e.printStackTrace();
}
}
this.logger.info("Loaded [{}] exchange(s).", this.exchanges.size());
this.exchanges.values().forEach(IngameFeature::syncCreatorNameToUniqueId);
Sponge.getServer().getOnlinePlayers().forEach(this::syncExchangeRegistryTo);
}).submit(this.container);
}
Aggregations