Skip to content

Commit

Permalink
Merge pull request #896 from Gu-ZT/releases/1.20.1
Browse files Browse the repository at this point in the history
实现Emi拖拽设置过滤
  • Loading branch information
Gu-ZT authored Jun 12, 2024
2 parents 6cc1f59 + cbf7626 commit 325eb80
Show file tree
Hide file tree
Showing 15 changed files with 133 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
public class FilteredItemDepository extends ItemDepository {

public static final Codec<FilteredItemDepository> CODEC = RecordCodecBuilder.create(ins -> ins.group(
Codec.BOOL.fieldOf("filterEnabled").forGetter(o -> o.filterEnabled),
ItemStack.CODEC.listOf().fieldOf("filteredItems").forGetter(o -> o.filteredItems),
Codec.BOOL.listOf().fieldOf("disabled").forGetter(o -> o.disabled)
Codec.BOOL.fieldOf("filterEnabled").forGetter(o -> o.filterEnabled),
ItemStack.CODEC.listOf().fieldOf("filteredItems").forGetter(o -> o.filteredItems),
Codec.BOOL.listOf().fieldOf("disabled").forGetter(o -> o.disabled)
).apply(ins, FilteredItemDepository::new));

private boolean filterEnabled = false;
Expand Down Expand Up @@ -140,10 +140,12 @@ public boolean isFiltered(int slot, ItemStack stack) {
* @param slot 槽位
* @param stack 过滤物品堆栈(不检查NBT)
*/
public void setFilter(int slot, @NotNull ItemStack stack) {
if (stack.isEmpty()) return;
public boolean setFilter(int slot, @NotNull ItemStack stack) {
if (slot < 0 || slot >= this.filteredItems.size()) return false;
if (stack.isEmpty()) return false;
this.setSlotDisabled(slot, false);
this.filteredItems.set(slot, new ItemStack(stack.getItem(), 1));
return true;
}

/**
Expand Down Expand Up @@ -203,18 +205,18 @@ public void deserializeNbt(@NotNull CompoundTag tag) {
*/
public CompoundTag serializeFiltering() {
return (CompoundTag) CODEC.encodeStart(NbtOps.INSTANCE, this)
.getOrThrow(false, e -> {
});
.getOrThrow(false, e -> {
});
}

/**
*
*/
public void deserializeFiltering(@NotNull CompoundTag tag) {
FilteredItemDepository depository = CODEC.decode(NbtOps.INSTANCE, tag)
.getOrThrow(false, s -> {
})
.getFirst();
.getOrThrow(false, s -> {
})
.getFirst();
if (this.getSize() != depository.getSize()) throw new IllegalArgumentException("Depository size mismatch");
this.filterEnabled = tag.getBoolean("filterEnabled");
int size = depository.filteredItems.size();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ default ItemStack getFilter(int slot) {
* @param slot 槽位
* @param filter 过滤
*/
default void setFilter(int slot, ItemStack filter) {
this.getFilteredItemDepository().setFilter(slot, filter);
default boolean setFilter(int slot, ItemStack filter) {
return this.getFilteredItemDepository().setFilter(slot, filter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import dev.dubhe.anvilcraft.api.depository.ItemDepositorySlot;
import dev.dubhe.anvilcraft.client.gui.component.EnableFilterButton;
import dev.dubhe.anvilcraft.inventory.AutoCrafterMenu;
import dev.dubhe.anvilcraft.inventory.IFilterMenu;
import dev.dubhe.anvilcraft.network.SlotDisableChangePack;
import lombok.Getter;
import net.minecraft.client.gui.GuiGraphics;
Expand All @@ -17,7 +16,7 @@

import java.util.function.BiFunction;

public class AutoCrafterScreen extends BaseMachineScreen<AutoCrafterMenu> implements IFilterScreen {
public class AutoCrafterScreen extends BaseMachineScreen<AutoCrafterMenu> implements IFilterScreen<AutoCrafterMenu> {
private static final ResourceLocation CONTAINER_LOCATION =
AnvilCraft.of("textures/gui/container/machine/background/auto_crafter.png");
BiFunction<Integer, Integer, EnableFilterButton> enableFilterButtonSupplier = this
Expand Down Expand Up @@ -67,7 +66,7 @@ protected void renderSlotTooltip(@NotNull GuiGraphics guiGraphics, int x, int y)
}

@Override
public IFilterMenu getFilterMenu() {
public AutoCrafterMenu getFilterMenu() {
return this.menu;
}

Expand All @@ -92,4 +91,14 @@ protected void slotClicked(@NotNull Slot slot, int slotId, int mouseButton, @Not
}
super.slotClicked(slot, slotId, mouseButton, type);
}

@Override
public int getOffsetX() {
return (this.width - this.imageWidth) / 2;
}

@Override
public int getOffsetY() {
return (this.height - this.imageHeight) / 2;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import dev.dubhe.anvilcraft.api.depository.ItemDepositorySlot;
import dev.dubhe.anvilcraft.client.gui.component.EnableFilterButton;
import dev.dubhe.anvilcraft.inventory.ChuteMenu;
import dev.dubhe.anvilcraft.inventory.IFilterMenu;
import dev.dubhe.anvilcraft.network.SlotDisableChangePack;
import lombok.Getter;
import net.minecraft.client.gui.GuiGraphics;
Expand All @@ -19,7 +18,7 @@

import java.util.function.BiFunction;

public class ChuteScreen extends BaseMachineScreen<ChuteMenu> implements IFilterScreen {
public class ChuteScreen extends BaseMachineScreen<ChuteMenu> implements IFilterScreen<ChuteMenu> {
private static final ResourceLocation CONTAINER_LOCATION =
AnvilCraft.of("textures/gui/container/machine/background/chute.png");

Expand Down Expand Up @@ -72,7 +71,7 @@ protected void renderSlotTooltip(@NotNull GuiGraphics guiGraphics, int x, int y)
}

@Override
public IFilterMenu getFilterMenu() {
public ChuteMenu getFilterMenu() {
return menu;
}

Expand All @@ -98,4 +97,14 @@ protected void slotClicked(@NotNull Slot slot, int slotId, int mouseButton, @Not
}
super.slotClicked(slot, slotId, mouseButton, type);
}

@Override
public int getOffsetX() {
return (this.width - this.imageWidth) / 2;
}

@Override
public int getOffsetY() {
return (this.height - this.imageHeight) / 2;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import dev.dubhe.anvilcraft.network.MachineEnableFilterPack;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.NotNull;
Expand All @@ -17,10 +18,10 @@
/**
* 有过滤的 GUI
*/
public interface IFilterScreen {
public interface IFilterScreen<T extends AbstractContainerMenu & IFilterMenu> {
ResourceLocation DISABLED_SLOT = AnvilCraft.of("textures/gui/container/machine/disabled_slot.png");

IFilterMenu getFilterMenu();
T getFilterMenu();

/**
* 获取是否开启过滤
Expand Down Expand Up @@ -65,8 +66,8 @@ default boolean isSlotDisabled(int slot) {
* @param slot 槽位
* @param filter 过滤
*/
default void setFilter(int slot, ItemStack filter) {
this.getFilterMenu().setFilter(slot, filter);
default boolean setFilter(int slot, ItemStack filter) {
return this.getFilterMenu().setFilter(slot, filter);
}

/**
Expand Down Expand Up @@ -143,4 +144,12 @@ default void renderFilterItem(@NotNull GuiGraphics guiGraphics, @NotNull Slot sl
guiGraphics.renderFakeItem(stack, i, j);
guiGraphics.fill(i, j, i + 16, j + 16, 0x80ffaaaa);
}

default int getOffsetY() {
return 0;
}

default int getOffsetX() {
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import dev.dubhe.anvilcraft.client.gui.component.EnableFilterButton;
import dev.dubhe.anvilcraft.client.gui.component.ItemCollectorButton;
import dev.dubhe.anvilcraft.client.gui.component.TextWidget;
import dev.dubhe.anvilcraft.inventory.IFilterMenu;
import dev.dubhe.anvilcraft.inventory.ItemCollectorMenu;
import dev.dubhe.anvilcraft.network.SlotDisableChangePack;
import lombok.Getter;
Expand All @@ -21,7 +20,8 @@

import java.util.function.BiFunction;

public class ItemCollectorScreen extends AbstractContainerScreen<ItemCollectorMenu> implements IFilterScreen {
public class ItemCollectorScreen extends AbstractContainerScreen<ItemCollectorMenu>
implements IFilterScreen<ItemCollectorMenu> {
private static final ResourceLocation CONTAINER_LOCATION =
AnvilCraft.of("textures/gui/container/machine/background/item_collector.png");
BiFunction<Integer, Integer, EnableFilterButton> enableFilterButtonSupplier =
Expand Down Expand Up @@ -123,7 +123,7 @@ protected void renderSlotTooltip(@NotNull GuiGraphics guiGraphics, int x, int y)
}

@Override
public IFilterMenu getFilterMenu() {
public ItemCollectorMenu getFilterMenu() {
return this.menu;
}

Expand All @@ -148,4 +148,14 @@ protected void slotClicked(@NotNull Slot slot, int slotId, int mouseButton, @Not
}
super.slotClicked(slot, slotId, mouseButton, type);
}

@Override
public int getOffsetX() {
return (this.width - this.imageWidth) / 2;
}

@Override
public int getOffsetY() {
return (this.height - this.imageHeight) / 2;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package dev.dubhe.anvilcraft.integration.emi;

import dev.dubhe.anvilcraft.api.recipe.AnvilRecipeManager;
import dev.dubhe.anvilcraft.client.gui.screen.inventory.AutoCrafterScreen;
import dev.dubhe.anvilcraft.client.gui.screen.inventory.ChuteScreen;
import dev.dubhe.anvilcraft.client.gui.screen.inventory.ItemCollectorScreen;
import dev.dubhe.anvilcraft.init.ModBlocks;
import dev.dubhe.anvilcraft.integration.emi.recipe.AnvilProcessEmiRecipe;
import dev.emi.emi.api.EmiEntrypoint;
Expand All @@ -16,6 +19,10 @@ public class AnvilCraftEmiPlugin implements EmiPlugin {
public void register(@NotNull EmiRegistry registry) {
AnvilRecipeCategory.ALL.forEach(registry::addCategory);

registry.addDragDropHandler(AutoCrafterScreen.class, new GhostIngredientHandler<>());
registry.addDragDropHandler(ChuteScreen.class, new GhostIngredientHandler<>());
registry.addDragDropHandler(ItemCollectorScreen.class, new GhostIngredientHandler<>());

registry.addWorkstation(AnvilRecipeCategory.STAMPING, EmiStack.of(ModBlocks.STAMPING_PLATFORM.get()));
registry.addWorkstation(AnvilRecipeCategory.STAMPING, EmiStack.of(ModBlocks.ROYAL_ANVIL));
registry.addWorkstation(AnvilRecipeCategory.STAMPING, EmiStack.of(Blocks.ANVIL));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package dev.dubhe.anvilcraft.integration.emi;

import dev.dubhe.anvilcraft.client.gui.screen.inventory.IFilterScreen;
import dev.dubhe.anvilcraft.network.SlotFilterChangePack;
import dev.emi.emi.api.EmiDragDropHandler;
import dev.emi.emi.api.stack.EmiIngredient;
import dev.emi.emi.api.stack.EmiStack;
import net.minecraft.client.gui.screens.Screen;
import net.minecraft.client.renderer.Rect2i;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.NotNull;

import java.util.List;

public class GhostIngredientHandler<T extends Screen & IFilterScreen<?>> implements EmiDragDropHandler<T> {
@Override
public boolean dropStack(
@NotNull T screen, @NotNull EmiIngredient ingredient, int x, int y
) {
if (!screen.isFilterEnabled()) return false;
List<EmiStack> stacks = ingredient.getEmiStacks();
if (stacks.size() != 1) return false;
ItemStack stack = stacks.get(0).getItemStack();
if (stack.isEmpty()) return false;
for (Slot slot : screen.getFilterMenu().slots) {
Rect2i rect2i = new Rect2i(screen.getOffsetX() + slot.x, screen.getOffsetY() + slot.y, 16, 16);
if (rect2i.contains(x, y)) {
if (screen.setFilter(screen.getFilterMenu().getFilterSlotIndex(slot), stack)) {
new SlotFilterChangePack(screen.getFilterMenu().getFilterSlotIndex(slot), stack).send();
return true;
}
}
}
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ public IFilterBlockEntity getFilterBlockEntity() {
return this.blockEntity;
}

@Override
public int getFilterSlotIndex(@NotNull Slot slot) {
return slot.index - 36;
}

@Override
public void setItem(int slotId, int stateId, @NotNull ItemStack stack) {
super.setItem(slotId, stateId, stack);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,4 +134,9 @@ public boolean stillValid(@NotNull Player player) {
public IFilterBlockEntity getFilterBlockEntity() {
return blockEntity;
}

@Override
public int getFilterSlotIndex(@NotNull Slot slot) {
return slot.index - 36;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import dev.dubhe.anvilcraft.block.entity.IFilterBlockEntity;
import net.minecraft.core.NonNullList;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;

/**
Expand Down Expand Up @@ -76,10 +77,12 @@ default NonNullList<ItemStack> getFilteredItems() {
* @param slot 槽位
* @param filter 过滤
*/
default void setFilter(int slot, ItemStack filter) {
this.getFilterBlockEntity().setFilter(slot, filter);
default boolean setFilter(int slot, ItemStack filter) {
return this.getFilterBlockEntity().setFilter(slot, filter);
}

int getFilterSlotIndex(Slot slot);

/**
* 刷新
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,9 @@ public void notify(int index, String name) {
}
}
}

@Override
public int getFilterSlotIndex(@NotNull Slot slot) {
return slot.index - 36;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public void handler(@NotNull MinecraftServer server, ServerPlayer player) {
public void handler() {
Minecraft client = Minecraft.getInstance();
client.execute(() -> {
if (client.screen instanceof IFilterScreen screen) {
if (client.screen instanceof IFilterScreen<?> screen) {
screen.setFilterEnabled(this.isFilterEnabled());
screen.flush();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public void handler(@NotNull MinecraftServer server, ServerPlayer player) {
if (!player.hasContainerOpen()) return;
if (!(player.containerMenu instanceof IFilterMenu menu)) return;
menu.setSlotDisabled(this.index, this.state);
menu.flush();
this.send(player);
});
}
Expand All @@ -54,7 +55,7 @@ public void handler(@NotNull MinecraftServer server, ServerPlayer player) {
public void handler() {
Minecraft client = Minecraft.getInstance();
client.execute(() -> {
if (!(client.screen instanceof IFilterScreen screen)) return;
if (!(client.screen instanceof IFilterScreen<?> screen)) return;
screen.setSlotDisabled(this.index, this.state);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public void handler(@NotNull MinecraftServer server, ServerPlayer player) {
if (!player.hasContainerOpen()) return;
if (!(player.containerMenu instanceof IFilterMenu menu)) return;
menu.setFilter(this.index, this.filter);
menu.flush();
this.send(player);
});
}
Expand All @@ -60,7 +61,7 @@ public void handler(@NotNull MinecraftServer server, ServerPlayer player) {
public void handler() {
Minecraft client = Minecraft.getInstance();
client.execute(() -> {
if (!(client.screen instanceof IFilterScreen screen)) return;
if (!(client.screen instanceof IFilterScreen<?> screen)) return;
screen.setFilter(this.index, this.filter);
});
}
Expand Down

0 comments on commit 325eb80

Please sign in to comment.