Skip to content

Commit

Permalink
Merge pull request #257 from Gu-ZT/Direction-Button
Browse files Browse the repository at this point in the history
修复自动合成器和溜槽GUI中改变方向的按钮无法正常使用
  • Loading branch information
Gugle2308 authored Apr 12, 2024
2 parents e20a5d0 + 07cbf52 commit f1158c4
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import dev.dubhe.anvilcraft.network.SlotDisableChangePack;
import lombok.Getter;
import net.minecraft.client.gui.GuiGraphics;
import net.minecraft.core.Direction;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;
Expand Down Expand Up @@ -37,6 +38,7 @@ public ChuteScreen(ChuteMenu menu, Inventory playerInventory, Component title) {
protected void init() {
super.init();
this.enableFilterButton = enableFilterButtonSupplier.apply(this.leftPos, this.topPos);
this.getDirectionButton().skip(Direction.UP);
this.addRenderableWidget(this.enableFilterButton);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import java.util.Optional;

@Getter
public class AutoCrafterMenu extends AbstractContainerMenu implements IFilterMenu, ContainerListener {
public class AutoCrafterMenu extends BaseMachineMenu implements IFilterMenu, ContainerListener {
public final AutoCrafterBlockEntity blockEntity;
private final Slot resultSlot;
private final Level level;
Expand All @@ -47,7 +47,7 @@ public AutoCrafterMenu(
* @param blockEntity 方块实体
*/
public AutoCrafterMenu(MenuType<?> menuType, int containerId, Inventory inventory, BlockEntity blockEntity) {
super(menuType, containerId);
super(menuType, containerId, blockEntity);
AutoCrafterMenu.checkContainerSize(inventory, 9);

this.blockEntity = (AutoCrafterBlockEntity) blockEntity;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,69 +1,23 @@
package dev.dubhe.anvilcraft.inventory;

import dev.dubhe.anvilcraft.block.entity.BaseMachineBlockEntity;
import dev.dubhe.anvilcraft.block.entity.IFilterBlockEntity;
import lombok.Getter;
import net.minecraft.core.Direction;
import net.minecraft.world.Container;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.block.entity.BlockEntity;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

@Getter
public abstract class BaseMachineMenu extends AbstractContainerMenu {
protected final Container machine;
protected final BlockEntity machine;

protected BaseMachineMenu(@Nullable MenuType<?> menuType, int containerId, @NotNull Container machine) {
protected BaseMachineMenu(@Nullable MenuType<?> menuType, int containerId, @NotNull BlockEntity machine) {
super(menuType, containerId);
this.machine = machine;
}

@Override
public @NotNull ItemStack quickMoveStack(@NotNull Player player, int index) {
ItemStack itemStack = ItemStack.EMPTY;
if (index >= this.slots.size()) return itemStack;
Slot slot = this.slots.get(index);
if (!slot.hasItem()) return itemStack;
ItemStack itemStack2 = slot.getItem();
itemStack = itemStack2.copy();
if (quickMoveFilter(index, itemStack2)) {
return ItemStack.EMPTY;
}
if (itemStack2.isEmpty()) slot.setByPlayer(ItemStack.EMPTY);
else slot.setChanged();
if (itemStack2.getCount() == itemStack.getCount()) return ItemStack.EMPTY;
slot.onTake(player, itemStack2);
return itemStack;
}

private boolean quickMoveFilter(int index, ItemStack itemStack) {
if (index < this.machine.getContainerSize()) {
return !this.moveItemStackTo(
itemStack, this.machine.getContainerSize(), this.machine.getContainerSize() + 36, true
);
} else {
boolean bl = false;
if (this.getMachine() instanceof IFilterBlockEntity entity) {
for (int i = 0; i < this.machine.getContainerSize(); i++) {
ItemStack filter = entity.getFilter(i);
if (filter.is(itemStack.getItem()) || !entity.isSlotDisabled(i)) {
bl = !this.moveItemStackTo(itemStack, i, i + 1, false);
}
}
}
return bl;
}
}

@Override
public boolean stillValid(@NotNull Player player) {
return this.machine.stillValid(player);
}

public void setDirection(Direction direction) {
if (this.machine instanceof BaseMachineBlockEntity entity) entity.setDirection(direction);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.ContainerLevelAccess;
import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.inventory.Slot;
Expand All @@ -20,7 +19,7 @@
import org.jetbrains.annotations.Nullable;

@Getter
public class ChuteMenu extends AbstractContainerMenu implements IFilterMenu {
public class ChuteMenu extends BaseMachineMenu implements IFilterMenu {

public final ChuteBlockEntity blockEntity;
private final Level level;
Expand All @@ -40,7 +39,7 @@ public ChuteMenu(
* @param blockEntity 方块实体
*/
public ChuteMenu(MenuType<?> menuType, int containerId, Inventory inventory, BlockEntity blockEntity) {
super(menuType, containerId);
super(menuType, containerId, blockEntity);
this.blockEntity = (ChuteBlockEntity) blockEntity;
this.level = inventory.player.level();

Expand Down

0 comments on commit f1158c4

Please sign in to comment.