PrettyPipes/src/main/java/de/ellpeck/prettypipes/pipe/containers/AbstractPipeContainer.java

65 lines
2.4 KiB
Java
Raw Normal View History

package de.ellpeck.prettypipes.pipe.containers;
2020-04-14 01:38:48 +02:00
2020-04-16 04:42:42 +02:00
import de.ellpeck.prettypipes.Utility;
2020-04-16 00:39:53 +02:00
import de.ellpeck.prettypipes.items.IModule;
2020-05-07 18:30:40 +02:00
import de.ellpeck.prettypipes.misc.FilterSlot;
2021-12-02 14:44:26 +01:00
import de.ellpeck.prettypipes.pipe.PipeBlockEntity;
import net.minecraft.core.BlockPos;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.inventory.ClickType;
import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.inventory.Slot;
2021-12-02 12:31:04 +01:00
import net.minecraft.world.item.ItemStack;
2020-05-07 18:30:40 +02:00
import org.apache.commons.lang3.tuple.Pair;
2020-04-14 01:38:48 +02:00
import javax.annotation.Nullable;
2021-12-02 14:44:26 +01:00
public abstract class AbstractPipeContainer<T extends IModule> extends AbstractContainerMenu {
2021-12-02 14:44:26 +01:00
public final PipeBlockEntity tile;
2020-04-16 04:42:42 +02:00
public final T module;
public final int moduleIndex;
2020-04-16 04:42:42 +02:00
public final ItemStack moduleStack;
2021-12-02 14:44:26 +01:00
public AbstractPipeContainer(@Nullable MenuType<?> type, int id, Player player, BlockPos pos, int moduleIndex) {
2020-04-14 01:38:48 +02:00
super(type, id);
2023-07-07 19:54:52 +02:00
this.tile = Utility.getBlockEntity(PipeBlockEntity.class, player.level(), pos);
2020-04-16 04:42:42 +02:00
this.moduleStack = moduleIndex < 0 ? null : this.tile.modules.getStackInSlot(moduleIndex);
this.module = moduleIndex < 0 ? null : (T) this.moduleStack.getItem();
this.moduleIndex = moduleIndex;
2020-04-14 01:38:48 +02:00
2020-04-16 04:42:42 +02:00
// needs to be done here so transferStackInSlot works correctly, bleh
this.addSlots();
2020-04-14 01:38:48 +02:00
2021-12-02 14:44:26 +01:00
for (var l = 0; l < 3; ++l)
for (var j1 = 0; j1 < 9; ++j1)
this.addSlot(new Slot(player.getInventory(), j1 + l * 9 + 9, 8 + j1 * 18, 89 + l * 18 + 32));
for (var i1 = 0; i1 < 9; ++i1)
this.addSlot(new Slot(player.getInventory(), i1, 8 + i1 * 18, 147 + 32));
2020-04-14 01:38:48 +02:00
}
2020-04-16 04:42:42 +02:00
protected abstract void addSlots();
2020-04-14 01:38:48 +02:00
@Override
2021-12-02 14:44:26 +01:00
public ItemStack quickMoveStack(Player player, int slotIndex) {
return Utility.transferStackInSlot(this, this::moveItemStackTo, player, slotIndex, stack -> {
2020-05-07 18:30:40 +02:00
if (stack.getItem() instanceof IModule)
return Pair.of(0, 3);
return null;
});
2020-04-14 01:38:48 +02:00
}
2020-04-16 04:42:42 +02:00
@Override
2021-12-02 14:44:26 +01:00
public void clicked(int slotId, int dragType, ClickType clickTypeIn, Player player) {
if (FilterSlot.checkFilter(this, slotId))
2021-12-02 14:44:26 +01:00
return;
super.clicked(slotId, dragType, clickTypeIn, player);
}
@Override
public boolean stillValid(Player player) {
return true;
2020-04-16 04:42:42 +02:00
}
2020-04-14 01:38:48 +02:00
}