Skip to content

Commit

Permalink
fix misbehaviors
Browse files Browse the repository at this point in the history
  • Loading branch information
KrLite committed Mar 9, 2024
1 parent 0d9ace5 commit ef74aee
Show file tree
Hide file tree
Showing 10 changed files with 35 additions and 29 deletions.
5 changes: 1 addition & 4 deletions src/main/java/net/krlite/knowledges/KnowledgesHud.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@
import com.mojang.blaze3d.systems.RenderSystem;
import net.krlite.equator.render.frame.FrameInfo;
import net.krlite.knowledges.api.proxy.RenderProxy;
import net.krlite.knowledges.api.representable.BlockRepresentable;
import net.krlite.knowledges.api.representable.Representable;
import net.krlite.knowledges.impl.representable.EmptyRepresentable;
import net.krlite.knowledges.impl.representable.KnowledgesBlockRepresentable;
import net.krlite.knowledges.impl.representable.KnowledgesEntityRepresentable;
import net.krlite.knowledges.impl.representable.KnowledgesRepresentable;
import net.krlite.knowledges.mixin.client.InGameHudInvoker;
import net.krlite.knowledges.networking.KnowledgesNetworking;
import net.minecraft.client.MinecraftClient;
Expand All @@ -23,7 +21,6 @@
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.GameMode;
import net.minecraft.world.World;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Optional;
Expand Down Expand Up @@ -115,7 +112,7 @@ public void tick(MinecraftClient client) {
.entity(entityHitResult.getEntity())
.build();
}

this.representable = representable;

if (representable != null && representable.hasServer()) {
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/net/krlite/knowledges/api/proxy/KnowledgeProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import net.minecraft.client.MinecraftClient;
import net.minecraft.entity.decoration.painting.PaintingEntity;
import net.minecraft.entity.decoration.painting.PaintingVariant;
import net.minecraft.fluid.EmptyFluid;
import net.minecraft.fluid.Fluid;
import net.minecraft.fluid.FluidState;
import net.minecraft.fluid.Fluids;
Expand All @@ -31,6 +32,10 @@
import java.util.Optional;

public class KnowledgeProxy {
public static boolean hitResultNotAir(HitResult hitResult) {
return hitResult != null && (hitResult.getType() != HitResult.Type.MISS || KnowledgeProxy.getFluidStateNonEmpty(hitResult).isPresent());
}

public static Identifier getId(Block block) {
return Registries.BLOCK.getId(block);
}
Expand Down Expand Up @@ -61,6 +66,7 @@ public static Optional<FluidState> getFluidState(HitResult hitResult) {
if (fluidState == null) return Optional.empty();

Fluid fluid = fluidState.getFluid();

boolean fluidIsWater = fluid == Fluids.WATER || fluid == Fluids.FLOWING_WATER, fluidIsLava = fluid == Fluids.LAVA || fluid == Fluids.FLOWING_LAVA;
if (KnowledgesClient.CONFIG.components.infoFluid.ignoresWater && fluidIsWater) return Optional.empty();
if (KnowledgesClient.CONFIG.components.infoFluid.ignoresLava && fluidIsLava) return Optional.empty();
Expand All @@ -69,6 +75,10 @@ public static Optional<FluidState> getFluidState(HitResult hitResult) {
return Optional.of(fluidState);
}

public static Optional<FluidState> getFluidStateNonEmpty(HitResult hitResult) {
return getFluidState(hitResult).filter(fluidState -> !fluidState.isEmpty());
}

public static String getNamespace(ItemStack itemStack) {
String namespace = Registries.ITEM.getId(itemStack.getItem()).getNamespace();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.Direction;

import java.util.Optional;
import java.util.function.Supplier;

public interface BlockRepresentable extends Representable<BlockHitResult> {
Block block();

BlockState blockState();

BlockEntity blockEntity();
Optional<BlockEntity> blockEntity();

BlockPos blockPos();

Expand All @@ -30,15 +31,15 @@ interface Builder extends Representable.Builder<BlockHitResult, BlockRepresentab
Builder blockState(BlockState blockState);

default Builder blockEntity(BlockEntity blockEntity) {
return blockEntitySupplier(() -> blockEntity);
return blockEntity != null ? blockEntitySupplier(() -> blockEntity) : this;
}

Builder blockEntitySupplier(Supplier<BlockEntity> blockEntitySupplier);

static Builder append(Builder builder, BlockRepresentable representable) {
return Representable.Builder.append(builder, representable)
.blockState(representable.blockState())
.blockEntity(representable.blockEntity());
.blockEntity(representable.blockEntity().orElse(null));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.krlite.knowledges.api.representable;

import net.krlite.knowledges.api.proxy.KnowledgeProxy;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.nbt.NbtCompound;
import net.minecraft.util.hit.HitResult;
Expand All @@ -8,10 +9,6 @@
import java.util.function.Supplier;

public interface Representable<H extends HitResult> extends PacketByteBufWritable {
default boolean hasHitResult() {
return hitResult() != null && hitResult().getType() != HitResult.Type.MISS;
}

H hitResult();

World world();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import net.krlite.knowledges.Shortcuts;
import net.krlite.knowledges.animation.InterpolatedText;
import net.krlite.knowledges.api.component.Knowledge;
import net.krlite.knowledges.api.proxy.KnowledgeProxy;
import net.minecraft.client.MinecraftClient;
import net.minecraft.text.MutableText;
import net.minecraft.util.hit.HitResult;
Expand Down Expand Up @@ -231,12 +232,12 @@ public static void registerEvents() {
ClientTickEvents.END_CLIENT_TICK.register(client -> {
if (client != null) {
HitResult hitResult = MinecraftClient.getInstance().crosshairTarget;
boolean hasHitResult = hitResult != null && hitResult.getType() != HitResult.Type.MISS;
boolean hitResultNotAir = KnowledgeProxy.hitResultNotAir(hitResult);

Ring.ovalOpacity.target(hasHitResult ? 1D : 0D);
Ring.ovalOpacity.target(hitResultNotAir ? 1D : 0D);

if (Ring.focusingBlock.isPositive() != hasHitResult) {
Ring.focusingBlock.speedDirection(hasHitResult);
if (Ring.focusingBlock.isPositive() != hitResultNotAir) {
Ring.focusingBlock.speedDirection(hitResultNotAir);
Ring.focusingBlock.play();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
public class FluidInfoComponent extends AbstractInfoComponent {
@Override
public void render(RenderProxy renderProxy, @NotNull Representable<?> representable) {
KnowledgeProxy.getFluidState(representable.hitResult()).ifPresent(fluidState -> {
KnowledgeProxy.getFluidStateNonEmpty(representable.hitResult()).ifPresent(fluidState -> {
BlockState blockState = fluidState.getBlockState();
MutableText fluidName = blockState.getBlock().getName();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,20 @@
import net.krlite.equator.visual.text.Paragraph;
import net.krlite.equator.visual.text.Section;
import net.krlite.knowledges.KnowledgesClient;
import net.krlite.knowledges.api.component.Knowledge;
import net.krlite.knowledges.api.proxy.KnowledgeProxy;
import net.krlite.knowledges.api.proxy.LayoutProxy;
import net.krlite.knowledges.api.proxy.RenderProxy;
import net.krlite.knowledges.api.representable.Representable;
import net.krlite.knowledges.impl.component.AbstractInfoComponent;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.world.ClientWorld;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.text.Text;
import org.jetbrains.annotations.NotNull;

public class InfoComponent extends AbstractInfoComponent {
@Override
public void render(RenderProxy renderProxy, @NotNull Representable<?> representable) {
if (!representable.hasHitResult()) reset();
if (!KnowledgeProxy.hitResultNotAir(representable.hitResult())) reset();
System.out.println(KnowledgeProxy.hitResultNotAir(representable.hitResult()));

Box textsRight = FrameInfo.scaled()
.leftCenter(LayoutProxy.crosshairSafeArea().rightCenter())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
public class BannerBlockInformationData extends AbstractBlockInformationData {
@Override
public Optional<MutableText> blockInformation(BlockRepresentable representable) {
if (representable.blockState().isIn(BlockTags.BANNERS)) {
if (!(representable.blockEntity() instanceof BannerBlockEntity bannerBlockEntity)) return Optional.empty();
if (representable.blockState().isIn(BlockTags.BANNERS) && representable.blockEntity().isPresent()) {
if (!(representable.blockEntity().get() instanceof BannerBlockEntity bannerBlockEntity)) return Optional.empty();

var patterns = bannerBlockEntity.getPatterns();
int available = patterns.size() - 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
import net.minecraft.util.math.Direction;
import net.minecraft.world.World;

import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Supplier;

Expand All @@ -41,8 +43,8 @@ public BlockState blockState() {
}

@Override
public BlockEntity blockEntity() {
return blockEntitySupplier.get();
public Optional<BlockEntity> blockEntity() {
return Optional.ofNullable(blockEntitySupplier).map(Supplier::get);
}

@Override
Expand All @@ -62,16 +64,16 @@ public static void onRequest(PacketByteBuf buf, ServerPlayerEntity player, Consu
ServerWorld world = player.getServerWorld();
if (!world.canSetBlock(pos)) return;

BlockEntity blockEntity = representable.blockEntity();
if (blockEntity == null) return;
Optional<BlockEntity> blockEntity = representable.blockEntity();
if (blockEntity.isEmpty()) return;

// TODO

NbtCompound compound = representable.data();
compound.putInt("x", pos.getX());
compound.putInt("y", pos.getY());
compound.putInt("z", pos.getZ());
compound.putString("id", KnowledgeProxy.getId(blockEntity.getType()).toString());
compound.putString("id", KnowledgeProxy.getId(blockEntity.get().getType()).toString());

responseSender.accept(compound);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ protected KnowledgesRepresentable(Supplier<H> hitResultSupplier, World world, Pl
this.hitResultSupplier = hitResultSupplier;
this.world = world;
this.player = player;
this.data = data;
this.data = data != null ? data : new NbtCompound();
this.hasServer = hasServer;
}

Expand Down

0 comments on commit ef74aee

Please sign in to comment.