Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bugs of item collector and block tag recipe 修复与物品收集器和方块标签配方相关的若干问题 #1385

Merged
merged 7 commits into from
Dec 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,11 @@ public boolean cycleDisabled(int slot) {
}

/**
* 判断指定槽位是否允许放入指定物品堆栈
* 判断指定槽位是否允许放入指定物品堆叠
*
* @param slot 槽位
* @param stack 物品堆栈
* @return 指定槽位是否允许放入指定物品堆栈
* @param stack 物品堆叠
* @return 指定槽位是否允许放入指定物品堆叠
*/
public boolean isFiltered(int slot, ItemStack stack) {
ItemStack filter = this.filteredItems.get(slot);
Expand All @@ -156,7 +156,7 @@ public boolean isFiltered(int slot, ItemStack stack) {
* 设置指定槽位的过滤
*
* @param slot 槽位
* @param stack 过滤物品堆栈(不检查NBT)
* @param stack 过滤物品堆叠(不检查NBT)
*/
public boolean setFilter(int slot, @NotNull ItemStack stack) {
if (slot < 0 || slot >= this.filteredItems.size()) return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@
import net.minecraft.world.level.block.state.properties.EnumProperty;
import net.minecraft.world.phys.shapes.Shapes;
import net.minecraft.world.phys.shapes.VoxelShape;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Optional;

/**
* 电力元件
*/
Expand Down Expand Up @@ -112,4 +113,8 @@ default int compareTo(@NotNull IPowerComponent iPowerComponent) {
int i = getComponentType().compareTo(iPowerComponent.getComponentType());
return i == 0 ? 1 : i;
}

default boolean isGridWorking() {
return Optional.ofNullable(this.getGrid()).map(PowerGrid::isWorking).orElse(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public void gridTick() {
* 向集电器添加电荷
*
* @param num 添加至收集器的电荷数
* @return 溢出的电荷数(既未被添加至收集器的电荷数)
* @return 溢出的电荷数(即未被添加至收集器的电荷数)
*/
public double incomingCharge(double num, BlockPos srcPos) {
double overflow = num - (MAX_POWER_PER_INCOMING - this.chargeCount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,13 @@ public Packet<ClientGamePacketListener> getUpdatePacket() {
@Override
public void gridTick() {
if (level == null || level.isClientSide) return;
BlockState state = level.getBlockState(getBlockPos());
if (state.hasProperty(ItemCollectorBlock.POWERED) && state.getValue(ItemCollectorBlock.POWERED)) return;
if (cd > 1) {
cd--;
return;
}
if (!this.isGridWorking()) return;
BlockState state = level.getBlockState(getBlockPos());
if (state.hasProperty(ItemCollectorBlock.POWERED) && state.getValue(ItemCollectorBlock.POWERED)) return;
AABB box = AABB.ofSize(
Vec3.atCenterOf(getBlockPos()),
rangeRadius.get() * 2.0 + 1,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import dev.dubhe.anvilcraft.inventory.IFilterMenu;
import dev.dubhe.anvilcraft.network.MachineEnableFilterPacket;

import dev.dubhe.anvilcraft.util.RenderHelper;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.inventory.AbstractContainerMenu;
Expand Down Expand Up @@ -142,12 +143,12 @@ default void renderDisabledSlot(@NotNull GuiGraphics guiGraphics, @NotNull Slot
*
* @param guiGraphics 画布
* @param slot 槽位
* @param stack 物品堆栈
* @param stack 物品堆叠
*/
default void renderFilterItem(@NotNull GuiGraphics guiGraphics, @NotNull Slot slot, @NotNull ItemStack stack) {
int i = slot.x;
int j = slot.y;
guiGraphics.renderFakeItem(stack, i, j);
RenderHelper.renderItemWithTransparency(stack, guiGraphics.pose(), i, j, 0.52f);
guiGraphics.fill(i, j, i + 16, j + 16, 0x80ffaaaa);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ protected void renderSlotTooltip(@NotNull GuiGraphics guiGraphics, int x, int y)
guiGraphics.renderTooltip(this.font, Component.translatable("screen.anvilcraft.slot.disable.tooltip"), x, y);
}

@Override
@NotNull
public void render(GuiGraphics guiGraphics, int mouseX, int mouseY, float partialTick) {
super.render(guiGraphics, mouseX, mouseY, partialTick);
this.renderTooltip(guiGraphics, mouseX, mouseY);
}

@Override
public ItemCollectorMenu getFilterMenu() {
return this.menu;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,23 @@
import dev.dubhe.anvilcraft.init.ModBlocks;
import dev.dubhe.anvilcraft.init.ModRecipeTypes;
import dev.dubhe.anvilcraft.integration.jei.AnvilCraftJeiPlugin;
import dev.dubhe.anvilcraft.integration.jei.util.BlockTagDisplayHelper;
import dev.dubhe.anvilcraft.integration.jei.util.JeiRecipeUtil;
import dev.dubhe.anvilcraft.integration.jei.util.JeiRenderHelper;
import dev.dubhe.anvilcraft.integration.jei.util.TextureConstants;
import dev.dubhe.anvilcraft.recipe.anvil.BlockCompressRecipe;
import dev.dubhe.anvilcraft.util.RenderHelper;

import net.minecraft.MethodsReturnNonnullByDefault;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.Component;
import net.minecraft.tags.TagKey;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.crafting.RecipeHolder;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState;
import net.neoforged.neoforge.common.util.Lazy;

import mezz.jei.api.gui.ITickTimer;
Expand All @@ -38,6 +38,7 @@
import org.jetbrains.annotations.Nullable;

import javax.annotation.ParametersAreNonnullByDefault;
import java.util.concurrent.atomic.AtomicReference;

@MethodsReturnNonnullByDefault
@ParametersAreNonnullByDefault
Expand Down Expand Up @@ -123,17 +124,20 @@ public void draw(

for (int i = recipe.inputs.size() - 1; i >= 0; i--) {
Either<TagKey<Block>, Block> input = recipe.inputs.get(i);
int finalI = i;
input.ifRight(r -> {
AtomicReference<BlockState> renderedState = new AtomicReference<>();
input.ifRight(r -> renderedState.set(r.defaultBlockState()))
.ifLeft(tag -> BlockTagDisplayHelper.getDisplay(tag)
.ifPresent(block -> renderedState.set(block.defaultBlockState())));
if(renderedState.get() != null){
RenderHelper.renderBlock(
guiGraphics,
r.defaultBlockState(),
renderedState.get(),
50,
30 + 10 * finalI,
10 - 10 * finalI,
30 + 10 * i,
10 - 10 * i,
12,
RenderHelper.SINGLE_BLOCK);
});
}
}

RenderHelper.renderBlock(
Expand All @@ -153,19 +157,19 @@ public void getTooltip(
IRecipeCategory.super.getTooltip(tooltip, recipeHolder, recipeSlotsView, mouseX, mouseY);
BlockCompressRecipe recipe = recipeHolder.value();

// if (mouseX >= 40 && mouseX <= 58) {
// if (mouseY >= 24 && mouseY <= 42) {
// tooltip.add(recipe.inputs.getFirst().getName());
// }
// if (mouseY >= 42 && mouseY <= 52) {
// tooltip.add(recipe.inputs.getLast().getName());
// }
// }
// if (mouseX >= 100 && mouseX <= 120) {
// if (mouseY >= 42 && mouseY <= 52) {
// tooltip.add(recipe.result.getName());
// }
// }
if (mouseX >= 40 && mouseX <= 58) {
if (mouseY >= 24 && mouseY < 42) {
tooltip.addAll(BlockTagDisplayHelper.getTooltipsForInput(recipe.inputs.getFirst()));
}
if (mouseY >= 42 && mouseY <= 52) {
tooltip.addAll(BlockTagDisplayHelper.getTooltipsForInput(recipe.inputs.getLast()));
}
}
if (mouseX >= 100 && mouseX <= 120) {
if (mouseY >= 42 && mouseY <= 52) {
tooltip.add(recipe.result.getName());
}
}
}

public static void registerRecipes(IRecipeRegistration registration) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package dev.dubhe.anvilcraft.integration.jei.util;

import com.mojang.datafixers.util.Either;
import mezz.jei.common.util.RegistryUtil;
import net.minecraft.ChatFormatting;
import net.minecraft.core.registries.Registries;
import net.minecraft.network.chat.Component;
import net.minecraft.tags.TagKey;
import net.minecraft.world.level.block.Block;
import net.neoforged.neoforge.common.Tags;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

public class BlockTagDisplayHelper {

/**
* 根据方块标签,获取当前的用于循环展示的方块。
*
* @param tag 需要显示的方块标签
* @return 用于展示的方块(当<code>tag</code>为空标签或无效标签时,返回值也为空)
*/
public static Optional<Block> getDisplay(TagKey<Block> tag){
return RegistryUtil.getRegistry(Registries.BLOCK).getTag(tag).map(it -> {
if(it.size() == 0) return Optional.<Block>empty();
return it.get((int) ((System.currentTimeMillis() / 1000) % it.size()))
.unwrapKey()
.map(key -> RegistryUtil.getRegistry(Registries.BLOCK).get(key));
}).orElse(Optional.empty());
}

/**
* 根据方块配方输入,获取需要展示的工具提示
*
* @param input 方块标签或方块的配方输入
* @return 展示方块对应的工具提示。若为方块标签,还会展示具体的标签名
*/
public static List<Component> getTooltipsForInput(Either<TagKey<Block>, Block> input){
List<Component> tooltipList = new ArrayList<>();
input.ifRight(block -> tooltipList.add(block.getName()))
.ifLeft(tag -> {
getDisplay(tag).ifPresent(block -> tooltipList.add(block.getName()));
tooltipList.add(Component.translatable("jei.tooltip.recipe.tag", "")
.withStyle(ChatFormatting.GRAY));
tooltipList.add(Component.translatableWithFallback(
Tags.getTagTranslationKey(tag),"#" + tag.location())
.withStyle(ChatFormatting.GRAY));
});
return tooltipList;
}
}
Loading