Skip to content
This repository has been archived by the owner on Nov 3, 2022. It is now read-only.

Commit

Permalink
Kits now load again.
Browse files Browse the repository at this point in the history
  • Loading branch information
dualspiral committed Jun 21, 2022
1 parent a25ca17 commit e9c0a89
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ static Kit create(final String name) {
* Gets the name of the kit.
*
* @return The name
* @deprecated This will be removed for version 3.
*/
@Deprecated
String getName();

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@
import io.github.nucleuspowered.nucleus.core.scaffold.listener.ListenerBase;
import io.github.nucleuspowered.nucleus.core.scaffold.task.TaskBase;
import io.github.nucleuspowered.nucleus.core.services.INucleusServiceCollection;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.data.DataRegistration;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.lifecycle.RegisterDataEvent;
import org.spongepowered.api.event.lifecycle.RegisterRegistryValueEvent;

import java.util.Arrays;
import java.util.Collection;
Expand All @@ -66,7 +61,7 @@ public void init(final INucleusServiceCollection serviceCollection) {
final KitService kitService = new KitService(serviceCollection);
serviceCollection.registerService(KitService.class, kitService, false);
// Data
serviceCollection.game().dataManager().registerBuilder(Kit.class, new KitDataBuilder());
serviceCollection.game().dataManager().registerBuilder(Kit.class, new KitDataBuilder(serviceCollection.logger()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class KitReloadCommand implements ICommandExecutor {

@Override
public ICommandResult execute(final ICommandContext context) throws CommandException {
final CompletableFuture<Void> res = context.getServiceCollection().getServiceUnchecked(KitService.class).getKitService().reload();
final CompletableFuture<Void> res = context.getServiceCollection().getServiceUnchecked(KitService.class).getKitStorageService().reload();
res.whenComplete((v, e) -> {
if (e == null) {
context.sendMessage("command.kit.reload.success");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,25 @@
package io.github.nucleuspowered.nucleus.modules.kit.data;

import io.github.nucleuspowered.nucleus.api.module.kit.data.Kit;
import org.apache.logging.log4j.Logger;
import org.spongepowered.api.data.persistence.AbstractDataBuilder;
import org.spongepowered.api.data.persistence.DataContainer;
import org.spongepowered.api.data.persistence.DataContentUpdater;
import org.spongepowered.api.data.persistence.DataQuery;
import org.spongepowered.api.data.persistence.DataView;
import org.spongepowered.api.data.persistence.InvalidDataException;
import org.spongepowered.api.data.persistence.Queries;
import org.spongepowered.api.item.inventory.ItemStackSnapshot;

import java.time.Duration;
import java.util.ArrayList;
import java.util.NoSuchElementException;
import java.util.Optional;

public final class KitDataBuilder extends AbstractDataBuilder<Kit> {

private final Logger logger;

public static final int CONTENT_VERSION = 1;

private static final DataQuery STACKS = DataQuery.of("stacks");
Expand All @@ -30,16 +37,34 @@ public final class KitDataBuilder extends AbstractDataBuilder<Kit> {
private static final DataQuery FIRSTJOIN = DataQuery.of("firstJoin");
private static final DataQuery COOLDOWN = DataQuery.of("cooldown");

public KitDataBuilder() {
public KitDataBuilder(final Logger logger) {
super(Kit.class, KitDataBuilder.CONTENT_VERSION);
this.logger = logger;
}

@Override
protected Optional<Kit> buildContent(final DataView container) throws InvalidDataException {
if (!container.contains(Queries.CONTENT_VERSION)) {
Updater0to1.Holder.INSTANCE.update(container);
}
return Optional.empty();
try {
return Optional.of(new SingleKit(
container.name(), // Remove when possible
container.getSerializableList(KitDataBuilder.STACKS, ItemStackSnapshot.class).get(),
container.getLong(KitDataBuilder.COOLDOWN).map(Duration::ofSeconds).orElse(null),
container.getLong(KitDataBuilder.COST).get(),
container.getBoolean(KitDataBuilder.AUTOREDEEM).get(),
container.getBoolean(KitDataBuilder.ONETIME).get(),
container.getBoolean(KitDataBuilder.DISPLAYONREDEEM).get(),
container.getBoolean(KitDataBuilder.IGNORESPERMISSION).get(),
container.getBoolean(KitDataBuilder.HIDDEN).get(),
container.getStringList(KitDataBuilder.COMMANDS).orElseGet(ArrayList::new),
container.getBoolean(KitDataBuilder.FIRSTJOIN).get()
));
} catch (final NoSuchElementException e) {
this.logger.error("Could not load kit {}, it has been ignored", container.name(), e);
return Optional.empty();
}
}

// So that things are in the same place.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import io.github.nucleuspowered.nucleus.core.services.interfaces.IStorageManager;
import io.github.nucleuspowered.nucleus.core.services.impl.storage.services.IStorageService;
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
import net.kyori.adventure.text.serializer.plain.PlainComponentSerializer;
import net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer;
import org.apache.logging.log4j.Logger;
import org.spongepowered.api.Sponge;
Expand Down Expand Up @@ -405,7 +404,7 @@ public void saveKit(final Kit kit) {
}

public void saveKit(final Kit kit, final boolean save) {
final IStorageService.Single<IKitDataObject> kdo = this.getKitService();
final IStorageService.Single<IKitDataObject> kdo = this.getKitStorageService();
final IKitDataObject kitDataObject = kdo.getOrNewOnThread();
final Map<String, Kit> kits = new HashMap<>(kitDataObject.getKitMap());
Util.getKeyIgnoreCase(this.getKitNames(true), kit.getName()).ifPresent(kits::remove);
Expand Down Expand Up @@ -556,12 +555,12 @@ public void onReload(final INucleusServiceCollection serviceCollection) {
this.isCommandsEnabled = kitConfig.isEnableKitCommands();
}

public IStorageService.SingleCached<IKitDataObject> getKitService() {
public IStorageService.SingleCached<IKitDataObject> getKitStorageService() {
return this.storageManager.getAdditionalStorageServiceForDataObject(KitStorageModule.class).get();
}

private IKitDataObject getKits() {
return this.getKitService().getOrNewOnThread();
return this.getKitStorageService().getOrNewOnThread();
}

public ViewableInventory getKitInventoryBuilder() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ protected IKitDataObject createNew(final DataView dataView) {
@Override
protected IKitDataObject loadFromDataContainer(final DataView dataView) throws InvalidDataException {
final IKitDataObject dataObject = this.createNew();
dataObject
.setKitMap(dataView.keys(false)
.stream()
.map(query -> dataView.getSerializable(query, Kit.class).map(x -> Tuple.of(query.parts().get(0), x)).orElse(null))
.filter(Objects::nonNull)
.collect(Collectors.toMap(k -> k._1, k -> k._2)));
dataObject.setKitMap(
dataView.keys(false)
.stream()
.map(query -> dataView.getSerializable(query, Kit.class).map(x -> Tuple.of(query.parts().get(0), x)).orElse(null))
.filter(Objects::nonNull)
.collect(Collectors.toMap(k -> k._1, k -> k._2)));
return dataObject;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,4 @@ public void detach() {
this.repository = null;
}

public IKitDataObject getKits() {
final IStorageService.SingleCached<IKitDataObject> gs = this.kitsService;
return gs.getCached().orElseGet(() -> gs.getOrNew().join());
}

}

0 comments on commit e9c0a89

Please sign in to comment.