Skip to content

Commit

Permalink
protocols
Browse files Browse the repository at this point in the history
  • Loading branch information
KrLite committed Mar 10, 2024
1 parent d33997b commit 7d919e5
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 29 deletions.
4 changes: 3 additions & 1 deletion src/main/java/net/krlite/knowledges/api/tag/TagProtocol.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package net.krlite.knowledges.api.tag;

public interface TagProtocol<T extends AdditionalTag<?, ?>, E extends Enum<E> & TagProtocol<T, E>> {
import net.krlite.knowledges.api.tag.caster.NbtCaster;

public interface TagProtocol<T extends AdditionalTag<?, ?>, E extends Enum<E> & TagProtocol<T, E>> {
NbtCaster<?> caster();
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public NbtByteArrayCaster(String identifier) {
);
}

public void set(NbtCompound data, byte[] bytes) {
super.set(data, Bytes.asList(bytes));
public void put(NbtCompound data, byte[] bytes) {
super.put(data, Bytes.asList(bytes));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public Optional<T> get(NbtCompound data) {
return Optional.ofNullable(hasValueIn(data) ? fromData.get(data, identifier) : null);
}

public void set(NbtCompound data, T t) {
public void put(NbtCompound data, T t) {
toData.set(data, identifier, t);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public NbtIntArrayCaster(String identifier) {
);
}

public void set(NbtCompound data, int[] integers) {
super.set(data, Arrays.stream(integers).boxed().toList());
public void put(NbtCompound data, int[] integers) {
super.put(data, Arrays.stream(integers).boxed().toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public NbtLongArrayCaster(String identifier) {
);
}

public void set(NbtCompound data, long[] longs) {
super.set(data, Arrays.stream(longs).boxed().toList());
public void put(NbtCompound data, long[] longs) {
super.put(data, Arrays.stream(longs).boxed().toList());
}
}
Original file line number Diff line number Diff line change
@@ -1,54 +1,49 @@
package net.krlite.knowledges.impl.data.info.block.blockinformation;

import net.krlite.knowledges.Shortcuts;
import net.krlite.knowledges.api.component.Knowledge;
import net.krlite.knowledges.api.proxy.KnowledgeProxy;
import net.krlite.knowledges.api.representable.BlockRepresentable;
import net.krlite.knowledges.api.tag.caster.NbtBooleanCaster;
import net.krlite.knowledges.api.tag.caster.NbtByteCaster;
import net.krlite.knowledges.impl.data.info.block.AbstractBlockInformationData;
import net.krlite.knowledges.impl.tag.block.BeehiveAdditionalTag;
import net.minecraft.block.BeehiveBlock;
import net.minecraft.block.BlockState;
import net.minecraft.block.Blocks;
import net.minecraft.block.entity.BeehiveBlockEntity;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.nbt.NbtCompound;
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;

import java.util.Optional;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Predicate;

public class BeehiveBlockInformationData extends AbstractBlockInformationData {
@Override
public Optional<MutableText> blockInformation(BlockRepresentable representable) {
return representable.blockEntity().flatMap(blockEntity -> {
if (blockEntity instanceof BeehiveBlockEntity) {
NbtCompound data = representable.data();
System.out.println(data);

int honeyLevel = representable.blockState().get(BeehiveBlock.HONEY_LEVEL), fullHoneyLevel = BeehiveBlock.FULL_HONEY_LEVEL;
MutableText honeyLevelText = Text.translatable(
localizationKey("honey_level"),
honeyLevel, fullHoneyLevel
);

MutableText beeCountText = Text.empty();
if (data.contains("Bees")) {
byte beeCount = data.getByte("Bees");
beeCountText = beeCount == 0 ? localize("bee_count", "empty") : Text.translatable(
final AtomicReference<MutableText> beeCountText = new AtomicReference<>(Text.empty());
((NbtByteCaster) BeehiveAdditionalTag.Protocol.BEES_BYTE.caster()).get(data).ifPresent(beeCount -> {
beeCountText.set(beeCount == 0 ? localize("bee_count", "empty") : Text.translatable(
localizationKey("bee_count"),
beeCount
);
}
));
});
((NbtBooleanCaster) BeehiveAdditionalTag.Protocol.FULL_BOOLEAN.caster()).get(data).ifPresent(full -> {
if (full) beeCountText.set(localize("bee_count", "full"));
});

if (data.contains("Full")) {
boolean isFull = data.getBoolean("Full");
if (isFull) beeCountText = localize("bee_count", "full");
}

return Shortcuts.Text.combineToMultiline(honeyLevelText, beeCountText);
return Shortcuts.Text.combineToMultiline(honeyLevelText, beeCountText.get());
}

return Optional.empty();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
import net.krlite.knowledges.api.proxy.KnowledgeProxy;
import net.krlite.knowledges.api.representable.BlockRepresentable;
import net.krlite.knowledges.api.tag.AdditionalBlockTag;
import net.krlite.knowledges.api.tag.TagProtocol;
import net.krlite.knowledges.api.tag.caster.NbtBooleanCaster;
import net.krlite.knowledges.api.tag.caster.NbtByteCaster;
import net.krlite.knowledges.api.tag.caster.NbtCaster;
import net.minecraft.block.BeehiveBlock;
import net.minecraft.block.Block;
import net.minecraft.block.Blocks;
Expand All @@ -11,6 +15,22 @@
import org.jetbrains.annotations.NotNull;

public class BeehiveAdditionalTag implements AdditionalBlockTag {
public enum Protocol implements TagProtocol<BeehiveAdditionalTag, Protocol> {
BEES_BYTE(new NbtByteCaster("Bees")),
FULL_BOOLEAN(new NbtBooleanCaster("Full"));

private final NbtCaster<?> caster;

Protocol(NbtCaster<?> caster) {
this.caster = caster;
}

@Override
public NbtCaster<?> caster() {
return caster;
}
}

@Override
public boolean shouldApply(Block block) {
System.out.println(block);
Expand All @@ -21,8 +41,8 @@ public boolean shouldApply(Block block) {
public void append(NbtCompound data, BlockRepresentable representable) {
representable.blockEntity().ifPresent(blockEntity -> {
if (blockEntity instanceof BeehiveBlockEntity beehiveBlockEntity) {
data.putByte("Bees", (byte) beehiveBlockEntity.getBeeCount());
data.putBoolean("Full", beehiveBlockEntity.isFullOfBees());
((NbtByteCaster) Protocol.BEES_BYTE.caster()).put(data, (byte) beehiveBlockEntity.getBeeCount());
((NbtBooleanCaster) Protocol.FULL_BOOLEAN.caster()).put(data, beehiveBlockEntity.isFullOfBees());
}
});
}
Expand Down

0 comments on commit 7d919e5

Please sign in to comment.