Unicopia/src/main/java/com/minelittlepony/unicopia/container/SpellbookScreenHandler.java

255 lines
7.9 KiB
Java
Raw Normal View History

2021-11-09 17:29:55 +01:00
package com.minelittlepony.unicopia.container;
2021-11-10 18:26:25 +01:00
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
2021-11-09 17:29:55 +01:00
import com.minelittlepony.unicopia.EquinePredicates;
2021-11-10 18:26:25 +01:00
import com.minelittlepony.unicopia.item.UItems;
2021-11-09 17:29:55 +01:00
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
2021-11-10 18:26:25 +01:00
import net.minecraft.inventory.CraftingInventory;
import net.minecraft.inventory.CraftingResultInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack;
import net.minecraft.recipe.RecipeType;
2021-11-09 17:29:55 +01:00
import net.minecraft.screen.ScreenHandler;
2021-11-10 18:26:25 +01:00
import net.minecraft.screen.slot.CraftingResultSlot;
import net.minecraft.screen.slot.Slot;
import net.minecraft.util.Pair;
import net.minecraft.world.World;
public class SpellbookScreenHandler extends ScreenHandler {
private final int MAX_INGREDIENTS;
private final int GEM_SLOT_INDEX;
private final int HOTBAR_START;
private final int HOTBAR_END;
private final CraftingInventory input;
private OutputSlot gemSlot;
private final CraftingResultInventory result = new CraftingResultInventory();
2021-11-09 17:29:55 +01:00
2021-11-10 18:26:25 +01:00
private final PlayerInventory inventory;
2021-11-09 17:29:55 +01:00
2021-11-10 18:26:25 +01:00
protected SpellbookScreenHandler(int syncId, PlayerInventory inv) {
2021-11-09 17:29:55 +01:00
super(UScreenHandlers.SPELL_BOOK, syncId);
2021-11-10 18:26:25 +01:00
inventory = inv;
List<Pair<Integer, Integer>> grid = new ArrayList<>();
List<Pair<Integer, Integer>> gemPos = new ArrayList<>();
createGrid(grid, gemPos);
GEM_SLOT_INDEX = MAX_INGREDIENTS = grid.size();
HOTBAR_START = GEM_SLOT_INDEX + 1;
HOTBAR_END = HOTBAR_START + 9;
input = new CraftingInventory(this, MAX_INGREDIENTS, 1);
for (int i = 0; i < MAX_INGREDIENTS; i++) {
var pos = grid.get(i);
addSlot(new InputSlot(input, i, pos.getLeft(), pos.getRight()));
}
addSlot(gemSlot = new OutputSlot(inventory.player, input, result, 0, gemPos.get(0).getLeft(), gemPos.get(0).getRight()));
for (int i = 0; i < 9; ++i) {
addSlot(new Slot(inventory, i, 121 + i * 18, 195));
}
onContentChanged(input);
2021-11-09 17:29:55 +01:00
}
@Override
public boolean canUse(PlayerEntity player) {
return EquinePredicates.IS_CASTER.test(player);
}
2021-11-10 18:26:25 +01:00
@Override
public void onContentChanged(Inventory inventory) {
World world = this.inventory.player.world;
if (!world.isClient && !gemSlot.getStack().isEmpty()) {
2021-11-10 18:26:25 +01:00
world.getServer().getRecipeManager().getFirstMatch(RecipeType.CRAFTING, input, world)
.map(recipe -> recipe.craft(input))
.ifPresentOrElse(gemSlot::setCrafted, gemSlot::setUncrafted);
}
}
@Override
public ItemStack transferSlot(PlayerEntity player, int index) {
Slot sourceSlot = slots.get(index);
if (sourceSlot == null || !sourceSlot.hasStack()) {
return ItemStack.EMPTY;
}
ItemStack transferredStack = sourceSlot.getStack();
ItemStack stack = transferredStack.copy();
if (index >= HOTBAR_START) {
if (!gemSlot.hasStack() && gemSlot.canInsert(stack)) {
if (insertItem(transferredStack, GEM_SLOT_INDEX, GEM_SLOT_INDEX + 1, false)) {
onContentChanged(input);
2021-11-10 18:26:25 +01:00
return ItemStack.EMPTY;
}
}
if (insertItem(transferredStack, 0, GEM_SLOT_INDEX, false)) {
sourceSlot.onQuickTransfer(transferredStack, stack);
onContentChanged(input);
2021-11-10 18:26:25 +01:00
return ItemStack.EMPTY;
}
} else {
if (insertItem(transferredStack, HOTBAR_START, HOTBAR_END, true)) {
sourceSlot.onQuickTransfer(transferredStack, stack);
onContentChanged(input);
2021-11-10 18:26:25 +01:00
return ItemStack.EMPTY;
}
}
if (transferredStack.getCount() == stack.getCount()) {
return ItemStack.EMPTY;
}
sourceSlot.onTakeItem(player, transferredStack);
return stack;
}
@Override
protected boolean insertItem(ItemStack stack, int startIndex, int endIndex, boolean fromLast) {
boolean success = false;
int i = fromLast ? endIndex - 1 : startIndex;
while (true) {
if (i < startIndex || i >= endIndex) {
break;
}
Slot slot = getSlot(i);
ItemStack current = slot.getStack();
if (!current.isEmpty() && ItemStack.canCombine(stack, current)) {
// abide by the slot's max item count when trying to insert stacks
int available = Math.min(Math.min(current.getMaxCount(), slot.getMaxItemCount()) - current.getCount(), stack.getCount());
if (available > 0) {
current.increment(available);
stack.decrement(available);
slot.markDirty();
success = true;
}
}
i += fromLast ? -1 : 1;
}
i = fromLast ? endIndex - 1 : startIndex;
while (true) {
if (i < startIndex || i >= endIndex) {
break;
}
Slot slot = getSlot(i);
ItemStack current = slot.getStack();
if (current.isEmpty() && slot.canInsert(stack)) {
if (stack.getCount() > slot.getMaxItemCount()) {
slot.setStack(stack.split(slot.getMaxItemCount()));
} else {
slot.setStack(stack.split(stack.getCount()));
}
slot.markDirty();
success = true;
break;
}
i += fromLast ? -1 : 1;
}
return success;
}
2021-11-10 18:26:25 +01:00
@Override
public void close(PlayerEntity playerEntity) {
gemSlot.setUncrafted();
super.close(playerEntity);
dropInventory(playerEntity, input);
dropInventory(playerEntity, result);
}
private static void createGrid(List<Pair<Integer, Integer>> grid, List<Pair<Integer, Integer>> gemPos) {
int cols = 4;
int spacing = 23;
int top = 34;
int left = 65;
for (int row = 0; row < 7; row++) {
for (int i = 0; i < cols; i++) {
(row == 3 && i == 3 ? gemPos : grid).add(new Pair<>(left + (i * spacing), top));
}
top += spacing * 0.9;
left -= (spacing / 2) * (row > 2 ? -1 : 1);
cols += row > 2 ? -1 : 1;
}
}
public interface SpellbookSlot {}
public class InputSlot extends Slot implements SpellbookSlot {
public InputSlot(Inventory inventory, int index, int xPosition, int yPosition) {
super(inventory, index, xPosition, yPosition);
}
@Override
public int getMaxItemCount() {
return 1;
}
}
public static class OutputSlot extends CraftingResultSlot implements SpellbookSlot {
private Optional<ItemStack> uncrafted = Optional.empty();
public OutputSlot(PlayerEntity player, CraftingInventory input, Inventory inventory, int index, int x, int y) {
super(player, input, inventory, index, x, y);
}
public void setCrafted(ItemStack crafted) {
uncrafted = uncrafted.or(() -> Optional.of(getStack()));
setStack(crafted);
}
public void setUncrafted() {
uncrafted = uncrafted.filter(stack -> {
setStack(stack);
return false;
});
}
@Override
public boolean canInsert(ItemStack stack) {
return stack.getItem() == UItems.GEMSTONE;
}
@Override
public int getMaxItemCount() {
return 1;
}
@Override
public void onTakeItem(PlayerEntity player, ItemStack stack) {
if (uncrafted.isPresent()) {
uncrafted = Optional.empty();
super.onTakeItem(player, stack);
}
}
}
2021-11-09 17:29:55 +01:00
}