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-11 19:06:28 +01:00
|
|
|
import com.minelittlepony.unicopia.entity.player.Pony;
|
2021-11-10 18:26:25 +01:00
|
|
|
import com.minelittlepony.unicopia.item.UItems;
|
2021-11-11 19:06:28 +01:00
|
|
|
import com.minelittlepony.unicopia.item.URecipes;
|
|
|
|
import com.minelittlepony.unicopia.util.InventoryUtil;
|
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;
|
2021-11-11 19:51:58 +01:00
|
|
|
import net.minecraft.network.packet.s2c.play.ScreenHandlerSlotUpdateS2CPacket;
|
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;
|
2021-11-11 19:06:28 +01:00
|
|
|
import net.minecraft.server.network.ServerPlayerEntity;
|
2021-11-11 19:51:58 +01:00
|
|
|
import net.minecraft.sound.SoundCategory;
|
|
|
|
import net.minecraft.sound.SoundEvents;
|
2021-11-10 18:26:25 +01:00
|
|
|
import net.minecraft.util.Pair;
|
2021-11-11 19:06:28 +01:00
|
|
|
import net.minecraft.util.collection.DefaultedList;
|
2021-11-10 18:26:25 +01:00
|
|
|
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;
|
|
|
|
|
2021-11-11 19:06:28 +01:00
|
|
|
private final SpellbookInventory input;
|
2021-11-10 18:26:25 +01:00
|
|
|
|
|
|
|
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;
|
|
|
|
|
2021-11-11 19:06:28 +01:00
|
|
|
input = new SpellbookInventory(this, MAX_INGREDIENTS, 1);
|
2021-11-10 18:26:25 +01:00
|
|
|
|
|
|
|
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;
|
2021-11-10 20:51:51 +01:00
|
|
|
if (!world.isClient && !gemSlot.getStack().isEmpty()) {
|
2021-11-11 19:06:28 +01:00
|
|
|
world.getServer().getRecipeManager().getFirstMatch(URecipes.SPELLBOOK, input, world)
|
|
|
|
.filter(recipe -> result.shouldCraftRecipe(world, (ServerPlayerEntity)this.inventory.player, recipe))
|
2021-11-11 19:51:58 +01:00
|
|
|
.map(recipe -> {
|
|
|
|
this.inventory.player.playSound(SoundEvents.BLOCK_END_PORTAL_FRAME_FILL, SoundCategory.MASTER, 1, 0.3F);
|
|
|
|
return recipe.craft(input);
|
|
|
|
})
|
2021-11-10 18:26:25 +01:00
|
|
|
.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)) {
|
2021-11-10 20:51:51 +01:00
|
|
|
if (insertItem(transferredStack, GEM_SLOT_INDEX, GEM_SLOT_INDEX + 1, false)) {
|
|
|
|
onContentChanged(input);
|
2021-11-10 18:26:25 +01:00
|
|
|
return ItemStack.EMPTY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-10 20:51:51 +01:00
|
|
|
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 {
|
2021-11-10 20:51:51 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2021-11-10 20:51:51 +01:00
|
|
|
@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 {}
|
|
|
|
|
2021-11-11 19:06:28 +01:00
|
|
|
public class SpellbookInventory extends CraftingInventory {
|
|
|
|
|
|
|
|
public SpellbookInventory(ScreenHandler handler, int width, int height) {
|
|
|
|
super(handler, width, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
public ItemStack getItemToModify() {
|
|
|
|
return gemSlot.getStack();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-10 18:26:25 +01:00
|
|
|
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();
|
|
|
|
|
2021-11-11 19:06:28 +01:00
|
|
|
private final SpellbookInventory input;
|
|
|
|
|
|
|
|
public OutputSlot(PlayerEntity player, SpellbookInventory input, Inventory inventory, int index, int x, int y) {
|
2021-11-10 18:26:25 +01:00
|
|
|
super(player, input, inventory, index, x, y);
|
2021-11-11 19:06:28 +01:00
|
|
|
this.input = input;
|
2021-11-10 18:26:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2021-11-11 19:06:28 +01:00
|
|
|
onCrafted(stack);
|
|
|
|
|
|
|
|
Pony pony = Pony.of(player);
|
|
|
|
InventoryUtil.iterate(input).forEach(s -> {
|
|
|
|
pony.getDiscoveries().unlock(s.getItem());
|
|
|
|
});
|
|
|
|
|
|
|
|
DefaultedList<ItemStack> defaultedList = player.world.getRecipeManager().getRemainingStacks(URecipes.SPELLBOOK, input, player.world);
|
|
|
|
|
|
|
|
for (int i = 0; i < defaultedList.size(); ++i) {
|
|
|
|
ItemStack itemStack = input.getStack(i);
|
|
|
|
ItemStack itemStack2 = defaultedList.get(i);
|
|
|
|
if (!itemStack.isEmpty()) {
|
|
|
|
input.removeStack(i, 1);
|
|
|
|
itemStack = input.getStack(i);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!itemStack2.isEmpty()) {
|
|
|
|
if (itemStack.isEmpty()) {
|
|
|
|
input.setStack(i, itemStack2);
|
|
|
|
} else if (ItemStack.areItemsEqualIgnoreDamage(itemStack, itemStack2) && ItemStack.areTagsEqual(itemStack, itemStack2)) {
|
|
|
|
itemStack2.increment(itemStack.getCount());
|
|
|
|
input.setStack(i, itemStack2);
|
|
|
|
} else if (!player.getInventory().insertStack(itemStack2)) {
|
|
|
|
player.dropItem(itemStack2, false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-10 18:26:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-11-09 17:29:55 +01:00
|
|
|
}
|