Search in sources :

Example 1 with BasicStore

use of com.almuradev.almura.feature.store.basic.BasicStore in project Almura by AlmuraDev.

the class ServerStoreManager method handleStoreAdd.

public void handleStoreAdd(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 + ".store.add")) {
        this.notificationManager.sendPopupNotification(player, Text.of(TextColors.RED, "Store"), Text.of("You do not have permission " + "to add stores!"), 5);
        return;
    }
    if (this.getStore(id).isPresent()) {
        this.logger.error("Player '{}' attempted to add store '{}' but it already exists. Syncing store registry...", player.getName(), id);
        this.syncStoreRegistryTo(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 = StoreQueries.createInsertStore(created, creator, id, name, permission, isHidden).build(context).keepStatement(false).execute();
            if (result == 0) {
                this.logger.error("Player '{}' submitted a new store '{}' to the database but it failed. Discarding changes...", player.getName(), id);
                return;
            }
            this.scheduler.createTaskBuilder().execute(() -> {
                final BasicStore basicStore = new BasicStore(id, created, creator, name, permission, isHidden);
                basicStore.syncCreatorNameToUniqueId();
                this.stores.put(id, basicStore);
                Sponge.getServer().getOnlinePlayers().forEach(this::syncStoreRegistryTo);
            }).submit(this.container);
        } catch (final SQLException e) {
            e.printStackTrace();
        }
    }).submit(this.container);
}
Also used : SQLException(java.sql.SQLException) Instant(java.time.Instant) DSLContext(org.jooq.DSLContext) BasicStore(com.almuradev.almura.feature.store.basic.BasicStore) UUID(java.util.UUID)

Example 2 with BasicStore

use of com.almuradev.almura.feature.store.basic.BasicStore in project Almura by AlmuraDev.

the class ClientboundStoresRegistryPacket method readFrom.

@Override
public void readFrom(final ChannelBuf buf) {
    final int count = buf.readInteger();
    if (count > 0) {
        this.stores = 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 BasicStore store = new BasicStore(id, created, creator, name, permission, isHidden);
            store.setCreatorName(creatorName);
            this.stores.add(store);
        }
    }
}
Also used : Instant(java.time.Instant) BasicStore(com.almuradev.almura.feature.store.basic.BasicStore) IOException(java.io.IOException) UUID(java.util.UUID)

Example 3 with BasicStore

use of com.almuradev.almura.feature.store.basic.BasicStore in project Almura by AlmuraDev.

the class ServerStoreManager method loadStores.

/**
 * Store
 */
private void loadStores() {
    final Map<String, Store> stores = new HashMap<>();
    try (final DSLContext context = this.databaseManager.createContext(true)) {
        final Results results = StoreQueries.createFetchAllStores().build(context).keepStatement(false).fetchMany();
        results.forEach(result -> {
            for (Record record : result) {
                final String id = record.getValue(com.almuradev.generated.store.tables.Store.STORE.ID);
                final Timestamp created = record.getValue(com.almuradev.generated.store.tables.Store.STORE.CREATED);
                final UUID creator = SerializationUtil.uniqueIdFromBytes(record.getValue(com.almuradev.generated.store.tables.Store.STORE.CREATOR));
                final String name = record.getValue(com.almuradev.generated.store.tables.Store.STORE.NAME);
                final String permission = record.getValue(com.almuradev.generated.store.tables.Store.STORE.PERMISSION);
                final boolean isHidden = record.getValue(com.almuradev.generated.store.tables.Store.STORE.IS_HIDDEN);
                this.logger.info("Loaded store '{}' ({})", name, id);
                stores.put(id, new BasicStore(id, created.toInstant(), creator, name, permission, isHidden));
            }
        });
    } catch (final SQLException e) {
        e.printStackTrace();
    }
    this.scheduler.createTaskBuilder().execute(() -> {
        this.stores.clear();
        this.stores.putAll(stores);
        if (this.stores.isEmpty()) {
            // TODO TEST CODE, remove before going live
            final BasicStore store = new BasicStore("almura.store.test", Instant.now(), FeatureConstants.UNKNOWN_OWNER, "Test Store", "almura.store.test", false);
            this.stores.put("almura.store.test", store);
            // Yes, I am purposely running this sync
            try (final DSLContext context1 = this.databaseManager.createContext(true)) {
                StoreQueries.createInsertStore(store.getCreated(), store.getCreator(), store.getId(), store.getName(), store.getPermission(), store.isHidden()).build(context1).keepStatement(false).execute();
                this.logger.info("Loaded store '{}' ({})", store.getName(), store.getId());
            } catch (final SQLException e) {
                e.printStackTrace();
            }
        }
        this.logger.info("Loaded [{}] store(s).", this.stores.size());
        this.stores.values().forEach(IngameFeature::syncCreatorNameToUniqueId);
        Sponge.getServer().getOnlinePlayers().forEach(this::syncStoreRegistryTo);
    }).submit(this.container);
}
Also used : HashMap(java.util.HashMap) Results(org.jooq.Results) SQLException(java.sql.SQLException) BasicStore(com.almuradev.almura.feature.store.basic.BasicStore) DSLContext(org.jooq.DSLContext) BasicStore(com.almuradev.almura.feature.store.basic.BasicStore) StoreSellingItemDataRecord(com.almuradev.generated.store.tables.records.StoreSellingItemDataRecord) StoreSellingItemRecord(com.almuradev.generated.store.tables.records.StoreSellingItemRecord) StoreBuyingItemRecord(com.almuradev.generated.store.tables.records.StoreBuyingItemRecord) Record(org.jooq.Record) StoreBuyingItemDataRecord(com.almuradev.generated.store.tables.records.StoreBuyingItemDataRecord) UUID(java.util.UUID) Timestamp(java.sql.Timestamp)

Aggregations

BasicStore (com.almuradev.almura.feature.store.basic.BasicStore)3 UUID (java.util.UUID)3 SQLException (java.sql.SQLException)2 Instant (java.time.Instant)2 DSLContext (org.jooq.DSLContext)2 StoreBuyingItemDataRecord (com.almuradev.generated.store.tables.records.StoreBuyingItemDataRecord)1 StoreBuyingItemRecord (com.almuradev.generated.store.tables.records.StoreBuyingItemRecord)1 StoreSellingItemDataRecord (com.almuradev.generated.store.tables.records.StoreSellingItemDataRecord)1 StoreSellingItemRecord (com.almuradev.generated.store.tables.records.StoreSellingItemRecord)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