Skip to content

Commit

Permalink
Add ingredient bytecodec and check if mouse is in bounds to try curso…
Browse files Browse the repository at this point in the history
…r checks.

<log>Check if the mouse is in bounds to try cursor checks.</log>
  • Loading branch information
ThatGravyBoat committed Jan 18, 2024
1 parent 1f76972 commit 517a1de
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.teamresourceful.resourcefullib.client.screens;

import com.teamresourceful.resourcefullib.client.utils.CursorUtils;
import com.teamresourceful.resourcefullib.client.utils.ScreenUtils;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.network.chat.Component;
Expand All @@ -16,21 +17,26 @@ protected BaseCursorScreen(Component component) {

@Override
public void render(@NotNull GuiGraphics graphics, int mouseX, int mouseY, float f) {
setCursor(Cursor.DEFAULT);
actuallyRender(graphics, mouseX, mouseY, f);
setCursor(children(), mouseX, mouseY);

switch (cursor) {
case DEFAULT -> CursorUtils.setDefault();
case POINTER -> CursorUtils.setPointing();
case DISABLED -> CursorUtils.setDisabled();
case TEXT -> CursorUtils.setText();
case CROSSHAIR -> CursorUtils.setCrosshair();
case RESIZE_EW -> CursorUtils.setResizeEastWest();
case RESIZE_NS -> CursorUtils.setResizeNorthSouth();
case RESIZE_NESW -> CursorUtils.setResizeNorthEastSouthWest();
case RESIZE_NWSE -> CursorUtils.setResizeNorthWestSouthEast();
case RESIZE_ALL -> CursorUtils.setResizeAll();
boolean wihinBounds = ScreenUtils.inBounds(this.getRectangle(), mouseX, mouseY);
if (!wihinBounds) {
actuallyRender(graphics, mouseX, mouseY, f);
} else {
setCursor(Cursor.DEFAULT);
actuallyRender(graphics, mouseX, mouseY, f);
setCursor(children(), mouseX, mouseY);

switch (cursor) {
case DEFAULT -> CursorUtils.setDefault();
case POINTER -> CursorUtils.setPointing();
case DISABLED -> CursorUtils.setDisabled();
case TEXT -> CursorUtils.setText();
case CROSSHAIR -> CursorUtils.setCrosshair();
case RESIZE_EW -> CursorUtils.setResizeEastWest();
case RESIZE_NS -> CursorUtils.setResizeNorthSouth();
case RESIZE_NESW -> CursorUtils.setResizeNorthEastSouthWest();
case RESIZE_NWSE -> CursorUtils.setResizeNorthWestSouthEast();
case RESIZE_ALL -> CursorUtils.setResizeAll();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.teamresourceful.resourcefullib.common.exceptions.UtilityClassException;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.navigation.ScreenRectangle;
import net.minecraft.client.gui.screens.inventory.tooltip.DefaultTooltipPositioner;
import net.minecraft.network.chat.Component;
import net.minecraft.util.FormattedCharSequence;
Expand Down Expand Up @@ -65,4 +66,8 @@ public static void sendClick(int containerId, int buttonId) {
Minecraft.getInstance().gameMode.handleInventoryButtonClick(containerId, buttonId);
}
}

public static boolean inBounds(ScreenRectangle rectangle, int x, int y) {
return x >= rectangle.left() && x <= rectangle.right() && y >= rectangle.top() && y <= rectangle.bottom();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@
import com.teamresourceful.bytecodecs.base.ByteCodec;
import com.teamresourceful.bytecodecs.base.object.ObjectByteCodec;
import com.teamresourceful.resourcefullib.common.exceptions.UtilityClassException;
import io.netty.buffer.ByteBuf;
import net.minecraft.core.*;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.Level;
import org.joml.Vector3f;
Expand Down Expand Up @@ -53,6 +56,10 @@ private ExtraByteCodecs() throws UtilityClassException {

public static final ByteCodec<Item> ITEM = registry(BuiltInRegistries.ITEM);
public static final ByteCodec<ItemStack> ITEM_STACK = ItemStackByteCodec.CODEC;
public static final ByteCodec<Ingredient> INGREDIENT = ByteCodec.passthrough(
(buf, ingredient) -> ingredient.toNetwork(toFriendly(buf)),
buf -> Ingredient.fromNetwork(toFriendly(buf))
);


public static <T, R extends Registry<T>> ByteCodec<ResourceKey<T>> resourceKey(ResourceKey<R> registry) {
Expand All @@ -62,4 +69,8 @@ public static <T, R extends Registry<T>> ByteCodec<ResourceKey<T>> resourceKey(R
public static <T> ByteCodec<T> registry(IdMap<T> map) {
return new IdMapByteCodec<>(map);
}

public static FriendlyByteBuf toFriendly(ByteBuf buffer) {
return buffer instanceof FriendlyByteBuf friendlyByteBuf ? friendlyByteBuf : new FriendlyByteBuf(buffer);
}
}

0 comments on commit 517a1de

Please sign in to comment.