Search in sources :

Example 1 with BasicExchange

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);
        }
    }
}
Also used : Instant(java.time.Instant) IOException(java.io.IOException) UUID(java.util.UUID) BasicExchange(com.almuradev.almura.feature.exchange.basic.BasicExchange)

Example 2 with BasicExchange

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);
}
Also used : SQLException(java.sql.SQLException) Instant(java.time.Instant) DSLContext(org.jooq.DSLContext) UUID(java.util.UUID) BasicExchange(com.almuradev.almura.feature.exchange.basic.BasicExchange)

Example 3 with BasicExchange

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);
}
Also used : BasicExchange(com.almuradev.almura.feature.exchange.basic.BasicExchange) HashMap(java.util.HashMap) Results(org.jooq.Results) SQLException(java.sql.SQLException) DSLContext(org.jooq.DSLContext) AxsListItemRecord(com.almuradev.generated.axs.tables.records.AxsListItemRecord) AxsListItemDataRecord(com.almuradev.generated.axs.tables.records.AxsListItemDataRecord) AxsForSaleItemRecord(com.almuradev.generated.axs.tables.records.AxsForSaleItemRecord) Record(org.jooq.Record) UUID(java.util.UUID) BasicExchange(com.almuradev.almura.feature.exchange.basic.BasicExchange) Timestamp(java.sql.Timestamp)

Aggregations

BasicExchange (com.almuradev.almura.feature.exchange.basic.BasicExchange)3 UUID (java.util.UUID)3 SQLException (java.sql.SQLException)2 Instant (java.time.Instant)2 DSLContext (org.jooq.DSLContext)2 AxsForSaleItemRecord (com.almuradev.generated.axs.tables.records.AxsForSaleItemRecord)1 AxsListItemDataRecord (com.almuradev.generated.axs.tables.records.AxsListItemDataRecord)1 AxsListItemRecord (com.almuradev.generated.axs.tables.records.AxsListItemRecord)1 IOException (java.io.IOException)1 Timestamp (java.sql.Timestamp)1 HashMap (java.util.HashMap)1 Record (org.jooq.Record)1 Results (org.jooq.Results)1