Skip to content

Commit

Permalink
managers and providers
Browse files Browse the repository at this point in the history
  • Loading branch information
KrLite committed Mar 9, 2024
1 parent d847509 commit e9db362
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 52 deletions.
47 changes: 45 additions & 2 deletions src/main/java/net/krlite/knowledges/KnowledgesClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@
import net.fabricmc.loader.api.ModContainer;
import net.krlite.knowledges.api.entrypoint.ComponentProvider;
import net.krlite.knowledges.api.entrypoint.DataProvider;
import net.krlite.knowledges.api.entrypoint.TagProvider;
import net.krlite.knowledges.api.representable.PacketByteBufWritable;
import net.krlite.knowledges.api.representable.Representable;
import net.krlite.knowledges.impl.component.AbstractInfoComponent;
import net.krlite.knowledges.config.KnowledgesConfig;
import net.krlite.knowledges.manager.KnowledgesComponentManager;
import net.krlite.knowledges.manager.KnowledgesDataManager;
import net.krlite.knowledges.manager.KnowledgesTagManager;
import net.krlite.knowledges.networking.KnowledgesNetworking;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.text.MutableText;
Expand All @@ -36,6 +37,8 @@ public class KnowledgesClient implements ClientModInitializer {

public static final KnowledgesComponentManager COMPONENTS = new KnowledgesComponentManager();
public static final KnowledgesDataManager DATA = new KnowledgesDataManager();
public static final KnowledgesTagManager TAGS = new KnowledgesTagManager();

public static final KnowledgesHud HUD = new KnowledgesHud();

static {
Expand Down Expand Up @@ -123,15 +126,51 @@ public void onInitializeClient() {
.forEach(data -> DATA.register(namespace, data));
});

// Tags
FabricLoader.getInstance().getEntrypointContainers(KnowledgesCommon.ID, TagProvider.class).forEach(entrypoint -> {
TagProvider provider = entrypoint.getEntrypoint();
var classes = provider.provide();
if (classes.isEmpty()) return;

ModContainer mod = entrypoint.getProvider();
String namespace = mod.getMetadata().getId(), name = mod.getMetadata().getName();

LOGGER.info(String.format(
"Registering %d %s for %s...",
classes.size(),
classes.size() <= 1 ? "tag" : "tags",
name
));

classes.stream()
.distinct()
.map(clazz -> {
try {
return clazz.getDeclaredConstructor().newInstance();
} catch (Throwable throwable) {
throw new RuntimeException(String.format(
"Failed to register tag for %s: constructor not found",
clazz.getName()
), throwable);
}
})
.forEach(tag -> TAGS.register(namespace, tag));
});

if (!COMPONENTS.asMap().isEmpty()) {
LOGGER.info(String.format(
"Successfully registered %d %s for %d %s and %d %s for %d %s. %s you wiser! 📚",
"Successfully registered %d %s for %d %s, %d %s for %d %s, and %d %s for %d %s. %s you wiser! 📚",

COMPONENTS.asList().size(),
COMPONENTS.asList().size() <= 1 ? "knowledge" : "knowledges",
COMPONENTS.asMap().keySet().size(),
COMPONENTS.asMap().keySet().size() <= 1 ? "mod" : "mods",

TAGS.asList().size(),
TAGS.asList().size() <= 1 ? "tag" : "tags",
TAGS.asMap().keySet().size(),
TAGS.asMap().keySet().size() <= 1 ? "mod" : "mods",

DATA.asList().size(),
"data",
DATA.asMap().keySet().size(),
Expand All @@ -147,6 +186,10 @@ public void onInitializeClient() {
}));
}

public static void tidyUp() {

}

public static void requestDataFor(PacketByteBufWritable writable, Identifier channel) {
PacketByteBuf buf = PacketByteBufs.create();
writable.writeToBuf(buf);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package net.krlite.knowledges.api.entrypoint;

import net.krlite.knowledges.api.tag.AdditionalTag;

public interface TagProvider extends Provider<AdditionalTag<?>> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package net.krlite.knowledges.api.tag;

import net.krlite.knowledges.api.core.path.WithPath;
import net.krlite.knowledges.api.representable.Representable;
import net.minecraft.nbt.NbtCompound;

public interface AdditionalTag<R extends Representable<?>> extends WithPath {
void append(NbtCompound data, R representable);
}
19 changes: 14 additions & 5 deletions src/main/java/net/krlite/knowledges/config/KnowledgesConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import net.krlite.knowledges.impl.component.CrosshairComponent;
import net.krlite.knowledges.impl.data.info.block.blockinformation.NoteBlockInformationData;

import java.util.ArrayList;
import java.util.HashMap;

@Config(name = "knowledges")
public class KnowledgesConfig extends PartitioningSerializer.GlobalData {
Expand All @@ -20,16 +20,20 @@ public class KnowledgesConfig extends PartitioningSerializer.GlobalData {
@ConfigEntry.Category("data")
public Data data = new Data();

@ConfigEntry.Category("tags")
public Tags tags = new Tags();

@Config(name = "general")
public static class Global implements ConfigData {
public int mainScalar = 1000;
public int crosshairSafeAreaScalar = 1000;
public boolean visibleInDebugHud = false;
public boolean autoTidiesUp = false;
}

@Config(name = "components")
public static class Components implements ConfigData {
public ArrayList<String> disabled = new ArrayList<>();
public HashMap<String, Boolean> map = new HashMap<>();

public Crosshair crosshair = new Crosshair();
public InfoBlock infoBlock = new InfoBlock();
Expand All @@ -45,11 +49,11 @@ public static class Crosshair {
}

public static class InfoBlock {
public boolean showBlockPoweredStatus = true;
public boolean showsBlockPoweredStatus = true;
}

public static class InfoEntity {
public boolean showNumericHealth = false;
public boolean showsNumericHealth = false;
}

public static class InfoFluid {
Expand All @@ -61,7 +65,7 @@ public static class InfoFluid {

@Config(name = "data")
public static class Data implements ConfigData {
public ArrayList<String> disabled = new ArrayList<>();
public HashMap<String, Boolean> map = new HashMap<>();

public NoteBlockInformation noteBlockInformation = new NoteBlockInformation();

Expand All @@ -70,4 +74,9 @@ public static class NoteBlockInformation {
public NoteBlockInformationData.MusicalAlphabet musicalAlphabet = NoteBlockInformationData.MusicalAlphabet.ENGLISH;
}
}

@Config(name = "tags")
public static class Tags implements ConfigData {
public HashMap<String, Boolean> map = new HashMap<>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public void render(RenderProxy renderProxy, @NotNull Representable<?> representa
}

// Numeric health
if (KnowledgesClient.CONFIG.components.infoEntity.showNumericHealth) {
if (KnowledgesClient.CONFIG.components.infoEntity.showsNumericHealth) {
renderProxy.draw(
FrameInfo.scaled()
.center(Vector.ZERO)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,45 +18,47 @@
public class BannerBlockInformationData extends AbstractBlockInformationData {
@Override
public Optional<MutableText> blockInformation(BlockRepresentable representable) {
if (representable.blockState().isIn(BlockTags.BANNERS) && representable.blockEntity().isPresent()) {
if (!(representable.blockEntity().get() instanceof BannerBlockEntity bannerBlockEntity)) return Optional.empty();
return representable.blockEntity().flatMap(blockEntity -> {
if (representable.blockState().isIn(BlockTags.BANNERS)) {
if (blockEntity instanceof BannerBlockEntity bannerBlockEntity) {
var patterns = bannerBlockEntity.getPatterns();
int available = patterns.size() - 1;
// The first pattern is always the background color, so ignore it

var patterns = bannerBlockEntity.getPatterns();
int available = patterns.size() - 1;
// The first pattern is always the background color, so ignore it
if (available > 0) {
return patterns.get(1).getFirst().getKey()
.map(RegistryKey::getValue)
.map(Identifier::toShortTranslationKey)
.map(translationKey -> {
MutableText name = Text.translatable(
localizationKey("pattern"),
Text.translatable("block.minecraft.banner." + translationKey + "." + patterns.get(1).getSecond().getName()).getString()
);

if (available > 0) {
return patterns.get(1).getFirst().getKey()
.map(RegistryKey::getValue)
.map(Identifier::toShortTranslationKey)
.map(translationKey -> {
MutableText name = Text.translatable(
localizationKey("pattern"),
Text.translatable("block.minecraft.banner." + translationKey + "." + patterns.get(1).getSecond().getName()).getString()
);

if (available > 2) {
return Text.translatable(
localizationKey("more_patterns"),
name.getString(),
available - 1,
// Counts the rest of the patterns. Use '%2$d' to reference.
available
// Counts all the patterns. Use '%3$d' to reference.
);
} else if (available > 1) {
return Text.translatable(
localizationKey("one_more_pattern"),
name.getString()
);
} else {
return name;
}
});
if (available > 2) {
return Text.translatable(
localizationKey("more_patterns"),
name.getString(),
available - 1,
// Counts the rest of the patterns. Use '%2$d' to reference.
available
// Counts all the patterns. Use '%3$d' to reference.
);
} else if (available > 1) {
return Text.translatable(
localizationKey("one_more_pattern"),
name.getString()
);
} else {
return name;
}
});
}
}
}
}

return Optional.empty();
return Optional.empty();
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.registry.Registries;
import net.minecraft.registry.tag.BlockTags;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import org.jetbrains.annotations.NotNull;
Expand All @@ -19,8 +20,11 @@
public class BeehiveBlockInformationData extends AbstractBlockInformationData {
@Override
public Optional<MutableText> blockInformation(BlockRepresentable representable) {
// TODO: Make this stuff work
/*
representable.blockEntity().flatMap(blockEntity -> {
})
Optional<BlockEntity> blockEntity = Knowledge.Info.crosshairBlockEntity();
if (blockEntity.isPresent() && blockEntity.get() instanceof BeehiveBlockEntity beehiveBlockEntity) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

public class KnowledgesComponentManager extends KnowledgesManager<Knowledge> {
public KnowledgesComponentManager() {
super(() -> KnowledgesClient.CONFIG.components.disabled);
super(() -> KnowledgesClient.CONFIG.components.map);
}

public void render(RenderProxy renderProxy, Representable<?> representable) {
Expand Down
30 changes: 23 additions & 7 deletions src/main/java/net/krlite/knowledges/manager/KnowledgesManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,17 @@

public abstract class KnowledgesManager<T extends WithPath> {
private final HashMap<String, List<T>> map = new HashMap<>();
private final Supplier<ArrayList<String>> supplier;
private final Supplier<HashMap<String, Boolean>> configSupplier;

KnowledgesManager(Supplier<ArrayList<String>> supplier) {
this.supplier = supplier;
KnowledgesManager(Supplier<HashMap<String, Boolean>> configSupplier) {
this.configSupplier = configSupplier;
}

protected abstract String localizationPrefix();

public void register(String namespace, T t) {
Shortcuts.Map.fastMerge(map, namespace, t);
identifier(t).ifPresent(key -> configSupplier.get().putIfAbsent(key.toString(), true));
}

public Map<String, List<T>> asMap() {
Expand Down Expand Up @@ -78,23 +79,38 @@ public boolean isInDefaultNamespace(T t) {
public boolean isEnabled(T t) {
return identifier(t)
.map(Identifier::toString)
.filter(supplier.get()::contains)
.isEmpty();
.filter(key -> {
var config = configSupplier.get();

if (config.containsKey(key)) {
return config.get(key);
} else {
config.put(key, true);
return true;
}
})
.isPresent();
}

public void setEnabled(T t, boolean enabled) {
identifier(t)
.map(Identifier::toString)
.ifPresent(key -> {
var config = configSupplier.get();

if (enabled && !isEnabled(t)) {
supplier.get().remove(key);
config.put(key, false);
}

if (!enabled && isEnabled(t)) {
supplier.get().add(key);
config.put(key, true);
}
});

KnowledgesClient.CONFIG_HOLDER.save();
}

public void tidyUp() {
configSupplier.get().keySet().removeIf(key -> byId(key).isEmpty());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package net.krlite.knowledges.manager;

import net.krlite.knowledges.KnowledgesClient;
import net.krlite.knowledges.api.tag.AdditionalTag;

public class KnowledgesTagManager extends KnowledgesManager<AdditionalTag<?>> {
public KnowledgesTagManager() {
super(() -> KnowledgesClient.CONFIG.tags.map);
}

@Override
protected String localizationPrefix() {
return "tag";
}
}

0 comments on commit e9db362

Please sign in to comment.