Skip to content

Commit

Permalink
use the new array converter for BlockEntryConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
HSGamer committed Jun 3, 2024
1 parent 4ff1e06 commit 4f774f5
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@

public interface BlockEntryConfig {
@ConfigPath("entries")
default List<BlockEntry> getEntries() {
return Collections.emptyList();
default BlockEntry[] getEntries() {
return new BlockEntry[0];
}

void setEntries(List<BlockEntry> entries);
void setEntries(BlockEntry[] entries);

void reloadConfig();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,44 +1,14 @@
package me.hsgamer.topper.spigot.block;

import com.google.common.reflect.TypeToken;
import me.hsgamer.hscore.common.CollectionUtils;
import me.hsgamer.hscore.config.annotation.converter.Converter;
import me.hsgamer.hscore.config.annotation.converter.manager.DefaultConverterManager;
import me.hsgamer.topper.spigot.config.DefaultConverterRegistry;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

public final class BlockEntryConverterRegistry {
private BlockEntryConverterRegistry() {
// EMPTY
}

@SuppressWarnings("UnstableApiUsage")
public static void register() {
DefaultConverterRegistry.register(new TypeToken<List<BlockEntry>>() {
}, new Converter() {
@Override
public List<BlockEntry> convert(Object raw) {
if (raw == null) return null;
return CollectionUtils.createStringListFromObject(raw, true).stream().map(BlockEntry::deserialize).collect(Collectors.toList());
}

@Override
public List<String> convertToRaw(Object value) {
if (value instanceof List) {
List<String> list = new ArrayList<>();
for (Object object : (List<?>) value) {
if (object instanceof BlockEntry) {
list.add(((BlockEntry) object).serialize());
}
}
return list;
}
return null;
}
});
DefaultConverterManager.registerConverter(BlockEntry.class, new Converter() {
@Override
public BlockEntry convert(Object raw) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected void onBreak(Player player, Location location) {
@Override
public void enable() {
this.registerEvents();
blockEntryConfig.getEntries().forEach(this::add);
for (BlockEntry blockEntry : blockEntryConfig.getEntries()) this.add(blockEntry);

final Queue<BlockEntry> entryQueue = new LinkedList<>();
final AtomicBoolean isBlockUpdating = new AtomicBoolean(false);
Expand Down Expand Up @@ -80,7 +80,7 @@ public void enable() {
public void disable() {
task.cancel();
HandlerList.unregisterAll(this);
blockEntryConfig.setEntries(new ArrayList<>(entries.values()));
blockEntryConfig.setEntries(entries.values().toArray(new BlockEntry[0]));
}

private void registerEvents() {
Expand Down

0 comments on commit 4f774f5

Please sign in to comment.