More cleanup and refactoring

This commit is contained in:
Sollace 2022-09-11 19:43:36 +02:00
parent 8a7c66b6c1
commit b1599c74af
17 changed files with 304 additions and 265 deletions

View file

@ -5,7 +5,7 @@ import java.util.List;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.minelittlepony.unicopia.ability.magic.spell.effect.SpellType; import com.minelittlepony.unicopia.ability.magic.spell.effect.SpellType;
import com.minelittlepony.unicopia.ability.magic.spell.trait.SpellTraits; import com.minelittlepony.unicopia.ability.magic.spell.trait.SpellTraits;
import com.minelittlepony.unicopia.container.SpellbookScreenHandler.SpellbookInventory; import com.minelittlepony.unicopia.container.inventory.SpellbookInventory;
import com.minelittlepony.unicopia.item.GemstoneItem; import com.minelittlepony.unicopia.item.GemstoneItem;
import com.minelittlepony.unicopia.item.URecipes; import com.minelittlepony.unicopia.item.URecipes;

View file

@ -2,7 +2,7 @@ package com.minelittlepony.unicopia.ability.magic.spell.crafting;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.minelittlepony.unicopia.ability.magic.spell.effect.SpellType; import com.minelittlepony.unicopia.ability.magic.spell.effect.SpellType;
import com.minelittlepony.unicopia.container.SpellbookScreenHandler.SpellbookInventory; import com.minelittlepony.unicopia.container.inventory.SpellbookInventory;
import com.minelittlepony.unicopia.item.*; import com.minelittlepony.unicopia.item.*;
import com.minelittlepony.unicopia.util.InventoryUtil; import com.minelittlepony.unicopia.util.InventoryUtil;

View file

@ -2,7 +2,7 @@ package com.minelittlepony.unicopia.ability.magic.spell.crafting;
import com.google.gson.JsonObject; import com.google.gson.JsonObject;
import com.minelittlepony.unicopia.ability.magic.spell.trait.SpellTraits; import com.minelittlepony.unicopia.ability.magic.spell.trait.SpellTraits;
import com.minelittlepony.unicopia.container.SpellbookScreenHandler.SpellbookInventory; import com.minelittlepony.unicopia.container.inventory.SpellbookInventory;
import com.minelittlepony.unicopia.item.*; import com.minelittlepony.unicopia.item.*;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;

View file

@ -3,7 +3,7 @@ package com.minelittlepony.unicopia.ability.magic.spell.crafting;
import java.util.List; import java.util.List;
import com.minelittlepony.unicopia.ability.magic.spell.trait.Trait; import com.minelittlepony.unicopia.ability.magic.spell.trait.Trait;
import com.minelittlepony.unicopia.container.SpellbookScreenHandler.SpellbookInventory; import com.minelittlepony.unicopia.container.inventory.SpellbookInventory;
import com.minelittlepony.unicopia.item.UItems; import com.minelittlepony.unicopia.item.UItems;
import com.minelittlepony.unicopia.item.URecipes; import com.minelittlepony.unicopia.item.URecipes;

View file

@ -5,7 +5,6 @@ import com.minelittlepony.common.client.gui.ScrollContainer;
import com.minelittlepony.common.client.gui.element.Label; import com.minelittlepony.common.client.gui.element.Label;
import com.minelittlepony.unicopia.ability.magic.spell.crafting.SpellbookRecipe; import com.minelittlepony.unicopia.ability.magic.spell.crafting.SpellbookRecipe;
import com.minelittlepony.unicopia.client.gui.DrawableUtil; import com.minelittlepony.unicopia.client.gui.DrawableUtil;
import com.minelittlepony.unicopia.container.SpellbookPage;
import com.minelittlepony.unicopia.container.SpellbookState; import com.minelittlepony.unicopia.container.SpellbookState;
import com.minelittlepony.unicopia.item.URecipes; import com.minelittlepony.unicopia.item.URecipes;
import com.mojang.blaze3d.systems.RenderSystem; import com.mojang.blaze3d.systems.RenderSystem;
@ -15,6 +14,10 @@ import net.minecraft.text.Text;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
public class SpellbookCraftingPageContent extends ScrollContainer implements SpellbookChapterList.Content, SpellbookScreen.RecipesChangedListener { public class SpellbookCraftingPageContent extends ScrollContainer implements SpellbookChapterList.Content, SpellbookScreen.RecipesChangedListener {
public static final Text INVENTORY_TITLE = Text.translatable("gui.unicopia.spellbook.page.inventory");
public static final Text RECIPES_TITLE = Text.translatable("gui.unicopia.spellbook.page.recipes");
public static final int TOTAL_PAGES = 2;
private final SpellbookScreen screen; private final SpellbookScreen screen;
private SpellbookState.PageState state = new SpellbookState.PageState(); private SpellbookState.PageState state = new SpellbookState.PageState();
@ -29,7 +32,7 @@ public class SpellbookCraftingPageContent extends ScrollContainer implements Spe
public void init(SpellbookScreen screen, Identifier pageId) { public void init(SpellbookScreen screen, Identifier pageId) {
state = screen.getState().getState(pageId); state = screen.getState().getState(pageId);
screen.addPageButtons(187, 300, 350, incr -> { screen.addPageButtons(187, 300, 350, incr -> {
state.swap(incr, SpellbookPage.VALUES.length); state.swap(incr, TOTAL_PAGES);
}); });
initContents(); initContents();
screen.addDrawable(this); screen.addDrawable(this);
@ -41,9 +44,9 @@ public class SpellbookCraftingPageContent extends ScrollContainer implements Spe
int headerColor = mouseY % 255; int headerColor = mouseY % 255;
DrawableUtil.drawScaledText(matrices, SpellbookPage.VALUES[state.getOffset()].getLabel(), screen.getFrameBounds().left + screen.getFrameBounds().width / 2 + 20, SpellbookScreen.TITLE_Y, 1.3F, headerColor); DrawableUtil.drawScaledText(matrices, state.getOffset() == 0 ? INVENTORY_TITLE : RECIPES_TITLE, screen.getFrameBounds().left + screen.getFrameBounds().width / 2 + 20, SpellbookScreen.TITLE_Y, 1.3F, headerColor);
Text pageText = Text.translatable("%s/%s", state.getOffset() + 1, SpellbookPage.VALUES.length); Text pageText = Text.translatable("%s/%s", state.getOffset() + 1, TOTAL_PAGES);
textRenderer.draw(matrices, pageText, 337 - textRenderer.getWidth(pageText) / 2F, 190, headerColor); textRenderer.draw(matrices, pageText, 337 - textRenderer.getWidth(pageText) / 2F, 190, headerColor);
} }
@ -58,18 +61,14 @@ public class SpellbookCraftingPageContent extends ScrollContainer implements Spe
@Override @Override
public boolean showInventory() { public boolean showInventory() {
return SpellbookPage.VALUES[state.getOffset()] == SpellbookPage.INVENTORY; return state.getOffset() == 0;
} }
private void initPageContent() { private void initPageContent() {
getContentPadding().setVertical(10); getContentPadding().setVertical(10);
getContentPadding().bottom = 30; getContentPadding().bottom = 30;
switch (SpellbookPage.VALUES[state.getOffset()]) { if (state.getOffset() == 1) {
case INVENTORY:
// handled elsewhere
break;
case RECIPES:
int top = 0; int top = 0;
for (SpellbookRecipe recipe : this.client.world.getRecipeManager().listAllOfType(URecipes.SPELLBOOK)) { for (SpellbookRecipe recipe : this.client.world.getRecipeManager().listAllOfType(URecipes.SPELLBOOK)) {
if (client.player.getRecipeBook().contains(recipe)) { if (client.player.getRecipeBook().contains(recipe)) {

View file

@ -4,7 +4,6 @@ import com.minelittlepony.common.client.gui.IViewRoot;
import com.minelittlepony.common.client.gui.dimension.Bounds; import com.minelittlepony.common.client.gui.dimension.Bounds;
import com.minelittlepony.unicopia.Unicopia; import com.minelittlepony.unicopia.Unicopia;
import com.minelittlepony.unicopia.client.gui.DrawableUtil; import com.minelittlepony.unicopia.client.gui.DrawableUtil;
import com.minelittlepony.unicopia.container.SpellbookPage;
import com.minelittlepony.unicopia.entity.player.*; import com.minelittlepony.unicopia.entity.player.*;
import com.mojang.blaze3d.systems.RenderSystem; import com.mojang.blaze3d.systems.RenderSystem;
@ -54,7 +53,7 @@ public class SpellbookProfilePageContent extends DrawableHelper implements Spell
matrices.push(); matrices.push();
matrices.translate(screen.getBackgroundWidth() / 2 + SpellbookScreen.TITLE_X - 10, y, 0); matrices.translate(screen.getBackgroundWidth() / 2 + SpellbookScreen.TITLE_X - 10, y, 0);
matrices.scale(1.3F, 1.3F, 1); matrices.scale(1.3F, 1.3F, 1);
font.draw(matrices, SpellbookPage.INVENTORY.getLabel(), 0, 0, SpellbookScreen.TITLE_COLOR); font.draw(matrices, SpellbookCraftingPageContent.INVENTORY_TITLE, 0, 0, SpellbookScreen.TITLE_COLOR);
matrices.pop(); matrices.pop();
Bounds bounds = screen.getFrameBounds(); Bounds bounds = screen.getFrameBounds();

View file

@ -11,7 +11,7 @@ import com.minelittlepony.common.client.gui.sprite.TextureSprite;
import com.minelittlepony.unicopia.Unicopia; import com.minelittlepony.unicopia.Unicopia;
import com.minelittlepony.unicopia.client.gui.spellbook.SpellbookChapterList.*; import com.minelittlepony.unicopia.client.gui.spellbook.SpellbookChapterList.*;
import com.minelittlepony.unicopia.container.*; import com.minelittlepony.unicopia.container.*;
import com.minelittlepony.unicopia.container.SpellbookScreenHandler.*; import com.minelittlepony.unicopia.container.inventory.*;
import com.minelittlepony.unicopia.network.Channel; import com.minelittlepony.unicopia.network.Channel;
import com.minelittlepony.unicopia.network.MsgSpellbookStateChanged; import com.minelittlepony.unicopia.network.MsgSpellbookStateChanged;
import com.mojang.blaze3d.platform.GlStateManager; import com.mojang.blaze3d.platform.GlStateManager;

View file

@ -1,16 +0,0 @@
package com.minelittlepony.unicopia.container;
import net.minecraft.text.Text;
public enum SpellbookPage {
INVENTORY,
RECIPES;
public static final SpellbookPage[] VALUES = values();
private final Text label = Text.translatable("gui.unicopia.spellbook.page." + name().toLowerCase());
public Text getLabel() {
return label;
}
}

View file

@ -6,14 +6,10 @@ import java.util.function.Predicate;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
import com.minelittlepony.unicopia.EquinePredicates; import com.minelittlepony.unicopia.EquinePredicates;
import com.minelittlepony.unicopia.USounds;
import com.minelittlepony.unicopia.ability.magic.spell.crafting.SpellbookRecipe; import com.minelittlepony.unicopia.ability.magic.spell.crafting.SpellbookRecipe;
import com.minelittlepony.unicopia.ability.magic.spell.trait.SpellTraits; import com.minelittlepony.unicopia.container.inventory.*;
import com.minelittlepony.unicopia.client.sound.BufferedExecutor;
import com.minelittlepony.unicopia.entity.player.Pony;
import com.minelittlepony.unicopia.item.UItems; import com.minelittlepony.unicopia.item.UItems;
import com.minelittlepony.unicopia.item.URecipes; import com.minelittlepony.unicopia.item.URecipes;
import com.minelittlepony.unicopia.util.InventoryUtil;
import com.mojang.datafixers.util.Pair; import com.mojang.datafixers.util.Pair;
import net.minecraft.enchantment.EnchantmentHelper; import net.minecraft.enchantment.EnchantmentHelper;
@ -21,7 +17,6 @@ import net.minecraft.entity.EquipmentSlot;
import net.minecraft.entity.mob.MobEntity; import net.minecraft.entity.mob.MobEntity;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory; import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.CraftingInventory;
import net.minecraft.inventory.CraftingResultInventory; import net.minecraft.inventory.CraftingResultInventory;
import net.minecraft.inventory.Inventory; import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
@ -30,10 +25,8 @@ import net.minecraft.network.packet.s2c.play.ScreenHandlerSlotUpdateS2CPacket;
import net.minecraft.screen.PlayerScreenHandler; import net.minecraft.screen.PlayerScreenHandler;
import net.minecraft.screen.ScreenHandler; import net.minecraft.screen.ScreenHandler;
import net.minecraft.screen.ScreenHandlerContext; import net.minecraft.screen.ScreenHandlerContext;
import net.minecraft.screen.slot.CraftingResultSlot;
import net.minecraft.screen.slot.Slot; import net.minecraft.screen.slot.Slot;
import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.sound.SoundCategory;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
public class SpellbookScreenHandler extends ScreenHandler { public class SpellbookScreenHandler extends ScreenHandler {
@ -46,21 +39,21 @@ public class SpellbookScreenHandler extends ScreenHandler {
private final int MAX_INGREDIENTS; private final int MAX_INGREDIENTS;
private final int GEM_SLOT_INDEX; public final int GEM_SLOT_INDEX;
private final int HOTBAR_START; private final int HOTBAR_START;
private final int HOTBAR_END; private final int HOTBAR_END;
private final SpellbookInventory input; private final SpellbookInventory input;
private InputSlot gemSlot; public InputSlot gemSlot;
private ResultSlot outputSlot; public OutputSlot outputSlot;
private final CraftingResultInventory result = new CraftingResultInventory(); private final CraftingResultInventory result = new CraftingResultInventory();
private final PlayerInventory inventory; private final PlayerInventory inventory;
private final ScreenHandlerContext context; private final ScreenHandlerContext context;
private Predicate<SlotType> canShowSlots; public Predicate<SlotType> canShowSlots;
private final SpellbookState state; private final SpellbookState state;
@ -80,7 +73,7 @@ public class SpellbookScreenHandler extends ScreenHandler {
List<int[]> grid = new ArrayList<>(); List<int[]> grid = new ArrayList<>();
List<int[]> gemPos = new ArrayList<>(); List<int[]> gemPos = new ArrayList<>();
createGrid(grid, gemPos); SpellbookCraftingGrid.createGrid(grid, gemPos);
MAX_INGREDIENTS = grid.size(); MAX_INGREDIENTS = grid.size();
GEM_SLOT_INDEX = MAX_INGREDIENTS; GEM_SLOT_INDEX = MAX_INGREDIENTS;
@ -91,10 +84,10 @@ public class SpellbookScreenHandler extends ScreenHandler {
for (int i = 0; i < MAX_INGREDIENTS; i++) { for (int i = 0; i < MAX_INGREDIENTS; i++) {
var pos = grid.get(i); var pos = grid.get(i);
addSlot(new ModifierSlot(input, i, pos)); addSlot(new IngredientSlot(this, input, i, pos));
} }
addSlot(gemSlot = new InputSlot(input, MAX_INGREDIENTS, gemPos.get(0))); addSlot(gemSlot = new InputSlot(this, input, MAX_INGREDIENTS, gemPos.get(0)));
for (int i = 0; i < 9; ++i) { for (int i = 0; i < 9; ++i) {
addSlot(new Slot(inventory, i, 121 + i * 18, 195)); addSlot(new Slot(inventory, i, 121 + i * 18, 195));
@ -102,12 +95,12 @@ public class SpellbookScreenHandler extends ScreenHandler {
for (int i = 0; i < PlayerInventory.MAIN_SIZE - 9; ++i) { for (int i = 0; i < PlayerInventory.MAIN_SIZE - 9; ++i) {
int x = i % 5; int x = i % 5;
int y = i / 5; int y = i / 5;
addSlot(new InventorySlot(inventory, i + 9, 225 + x * 20, 50 + y * 20)); addSlot(new InventorySlot(this, inventory, i + 9, 225 + x * 20, 50 + y * 20));
} }
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
final EquipmentSlot eq = EquipmentSlot.values()[5 - i]; final EquipmentSlot eq = EquipmentSlot.values()[5 - i];
addSlot(new InventorySlot(inventory, PlayerInventory.OFF_HAND_SLOT - i - 1, 340, 50 + (i * 20)) { addSlot(new InventorySlot(this, inventory, PlayerInventory.OFF_HAND_SLOT - i - 1, 340, 50 + (i * 20)) {
@Override @Override
public int getMaxItemCount() { public int getMaxItemCount() {
return 1; return 1;
@ -134,14 +127,14 @@ public class SpellbookScreenHandler extends ScreenHandler {
}); });
} }
addSlot(new InventorySlot(inventory, PlayerInventory.OFF_HAND_SLOT, 340, 150) { addSlot(new InventorySlot(this, inventory, PlayerInventory.OFF_HAND_SLOT, 340, 150) {
@Override @Override
public Pair<Identifier, Identifier> getBackgroundSprite() { public Pair<Identifier, Identifier> getBackgroundSprite() {
return Pair.of(PlayerScreenHandler.BLOCK_ATLAS_TEXTURE, PlayerScreenHandler.EMPTY_OFFHAND_ARMOR_SLOT); return Pair.of(PlayerScreenHandler.BLOCK_ATLAS_TEXTURE, PlayerScreenHandler.EMPTY_OFFHAND_ARMOR_SLOT);
} }
}); });
addSlot(outputSlot = new ResultSlot(inventory.player, input, result, 0, gemPos.get(0))); addSlot(outputSlot = new OutputSlot(this, inventory.player, input, result, 0, gemPos.get(0)));
onContentChanged(input); onContentChanged(input);
} }
@ -194,11 +187,11 @@ public class SpellbookScreenHandler extends ScreenHandler {
ItemStack transferredStack = sourceSlot.getStack(); ItemStack transferredStack = sourceSlot.getStack();
ItemStack stack = transferredStack.copy(); ItemStack stack = transferredStack.copy();
if (sourceSlot instanceof ResultSlot result) { if (sourceSlot instanceof OutputSlot result) {
result.onTakeItem(player, stack); result.onTakeItem(player, stack);
} }
if (index >= HOTBAR_START && !(sourceSlot instanceof ResultSlot || sourceSlot instanceof InputSlot)) { if (index >= HOTBAR_START && !(sourceSlot instanceof OutputSlot || sourceSlot instanceof InputSlot)) {
if (!gemSlot.hasStack() && gemSlot.canInsert(stack)) { if (!gemSlot.hasStack() && gemSlot.canInsert(stack)) {
if (insertItem(transferredStack, GEM_SLOT_INDEX, GEM_SLOT_INDEX + 1, false)) { if (insertItem(transferredStack, GEM_SLOT_INDEX, GEM_SLOT_INDEX + 1, false)) {
onContentChanged(input); onContentChanged(input);
@ -304,205 +297,4 @@ public class SpellbookScreenHandler extends ScreenHandler {
}); });
} }
/**
* Creates a hexagonal crafting grid.
* @param grid Output for normal slot positions.
* @param gemPos Output for the gem slot position.
*/
private static void createGrid(List<int[]> grid, List<int[]> 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++) {
int ring = 3;
if (row == 0 || row == 6) {
ring = 1;
} else if ((row == 1 || row == 5) && i > 0 && i < cols - 1) {
ring = 2;
} else {
if (i == 0 || i == cols - 1) {
ring = 1;
} else if (i == 1 || i == cols - 2) {
ring = 2;
}
}
(row == 3 && i == 3 ? gemPos : grid).add(new int[] {
left + (i * spacing),
top,
row == 3 && i == 3 ? 4 : ring
});
}
top += spacing * 0.9;
left -= (spacing / 2) * (row > 2 ? -1 : 1);
cols += row > 2 ? -1 : 1;
}
}
public interface SpellbookSlot {
int getRing();
}
public class SpellbookInventory extends CraftingInventory {
public SpellbookInventory(ScreenHandler handler, int width, int height) {
super(handler, width, height);
}
public ItemStack getItemToModify() {
return gemSlot.getStack();
}
public boolean hasIngredients() {
for (int i = 0; i < GEM_SLOT_INDEX; i++) {
if (!getStack(i).isEmpty()) {
return true;
}
}
return false;
}
public int getRing(int slot) {
Slot s = slots.get(slot);
return s instanceof SpellbookSlot ? ((SpellbookSlot)s).getRing() : 0;
}
public SpellTraits getTraits() {
return SpellTraits.union(InventoryUtil.slots(this)
.map(slot -> SpellTraits.of(getStack(slot)).multiply(getRingFactor(getRing(slot))))
.toArray(SpellTraits[]::new)
);
}
public static float getRingFactor(int ring) {
switch (ring) {
case 1: return 1;
case 2: return 0.6F;
case 3: return 0.3F;
default: return 0;
}
}
}
public class InventorySlot extends Slot implements SpellbookSlot {
public InventorySlot(Inventory inventory, int index, int x, int y) {
super(inventory, index, x, y);
}
@Override
public int getRing() {
return 0;
}
@Override
public boolean isEnabled() {
return canShowSlots.test(SlotType.INVENTORY);
}
}
public class ModifierSlot extends Slot implements SpellbookSlot {
private final int ring;
public ModifierSlot(Inventory inventory, int index, int[] params) {
super(inventory, index, params[0], params[1]);
ring = params[2];
}
@Override
public int getMaxItemCount() {
return 1;
}
@Override
public boolean canInsert(ItemStack stack) {
return true;
}
@Override
public int getRing() {
return ring;
}
@Override
public boolean isEnabled() {
return canShowSlots.test(SlotType.CRAFTING) && super.isEnabled();
}
}
public class InputSlot extends Slot implements SpellbookSlot {
private final int ring;
public InputSlot(Inventory inventory, int index, int[] params) {
super(inventory, index, params[0], params[1]);
this.ring = params[2];
}
@Override
public int getMaxItemCount() {
return 1;
}
@Override
public int getRing() {
return ring;
}
@Override
public boolean isEnabled() {
return canShowSlots.test(SlotType.CRAFTING) && !outputSlot.isEnabled();
}
}
public class ResultSlot extends CraftingResultSlot implements SpellbookSlot {
private final PlayerEntity player;
private final SpellbookInventory input;
private final int ring;
public ResultSlot(PlayerEntity player, SpellbookInventory input, Inventory inventory, int index, int[] params) {
super(player, input, inventory, index, params[0], params[1]);
this.player = player;
this.input = input;
this.ring = params[2];
}
@Override
public void setStack(ItemStack stack) {
if (!stack.isEmpty() && !ItemStack.areEqual(stack, getStack())) {
BufferedExecutor.bufferExecution(player, () -> {
player.playSound(stack.getItem() == UItems.BOTCHED_GEM ? USounds.GUI_ABILITY_FAIL : USounds.GUI_SPELL_CRAFT_SUCCESS, SoundCategory.MASTER, 1, 0.3F);
});
}
super.setStack(stack);
}
@Override
public int getRing() {
return ring;
}
@Override
public boolean isEnabled() {
return canShowSlots.test(SlotType.CRAFTING) && hasStack();
}
@Override
public void onTakeItem(PlayerEntity player, ItemStack stack) {
Pony pony = Pony.of(player);
InventoryUtil.stream(input).forEach(s -> {
pony.getDiscoveries().unlock(s.getItem());
});
//gemSlot.setStack(ItemStack.EMPTY);
super.onTakeItem(player, stack);
}
}
public enum SlotType {
INVENTORY,
CRAFTING
}
} }

View file

@ -0,0 +1,38 @@
package com.minelittlepony.unicopia.container.inventory;
import com.minelittlepony.unicopia.container.*;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack;
import net.minecraft.screen.slot.Slot;
public class IngredientSlot extends Slot implements SpellbookSlot {
private final SpellbookScreenHandler handler;
private final int ring;
public IngredientSlot(SpellbookScreenHandler handler, Inventory inventory, int index, int[] params) {
super(inventory, index, params[0], params[1]);
this.handler = handler;
ring = params[2];
}
@Override
public int getMaxItemCount() {
return 1;
}
@Override
public boolean canInsert(ItemStack stack) {
return true;
}
@Override
public int getRing() {
return ring;
}
@Override
public boolean isEnabled() {
return handler.canShowSlots.test(SlotType.CRAFTING) && super.isEnabled();
}
}

View file

@ -0,0 +1,32 @@
package com.minelittlepony.unicopia.container.inventory;
import com.minelittlepony.unicopia.container.*;
import net.minecraft.inventory.Inventory;
import net.minecraft.screen.slot.Slot;
public class InputSlot extends Slot implements SpellbookSlot {
private final SpellbookScreenHandler handler;
private final int ring;
public InputSlot(SpellbookScreenHandler handler, Inventory inventory, int index, int[] params) {
super(inventory, index, params[0], params[1]);
this.handler = handler;
this.ring = params[2];
}
@Override
public int getMaxItemCount() {
return 1;
}
@Override
public int getRing() {
return ring;
}
@Override
public boolean isEnabled() {
return handler.canShowSlots.test(SlotType.CRAFTING) && !handler.outputSlot.isEnabled();
}
}

View file

@ -0,0 +1,25 @@
package com.minelittlepony.unicopia.container.inventory;
import com.minelittlepony.unicopia.container.*;
import net.minecraft.inventory.Inventory;
import net.minecraft.screen.slot.Slot;
public class InventorySlot extends Slot implements SpellbookSlot {
private final SpellbookScreenHandler handler;
public InventorySlot(SpellbookScreenHandler handler, Inventory inventory, int index, int x, int y) {
super(inventory, index, x, y);
this.handler = handler;
}
@Override
public int getRing() {
return 0;
}
@Override
public boolean isEnabled() {
return handler.canShowSlots.test(SlotType.INVENTORY);
}
}

View file

@ -0,0 +1,60 @@
package com.minelittlepony.unicopia.container.inventory;
import com.minelittlepony.unicopia.USounds;
import com.minelittlepony.unicopia.client.sound.BufferedExecutor;
import com.minelittlepony.unicopia.container.*;
import com.minelittlepony.unicopia.entity.player.Pony;
import com.minelittlepony.unicopia.item.UItems;
import com.minelittlepony.unicopia.util.InventoryUtil;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack;
import net.minecraft.screen.slot.CraftingResultSlot;
import net.minecraft.sound.SoundCategory;
public class OutputSlot extends CraftingResultSlot implements SpellbookSlot {
private final SpellbookScreenHandler handler;
private final PlayerEntity player;
private final SpellbookInventory input;
private final int ring;
public OutputSlot(SpellbookScreenHandler spellbookScreenHandler, PlayerEntity player, SpellbookInventory input, Inventory inventory, int index, int[] params) {
super(player, input, inventory, index, params[0], params[1]);
handler = spellbookScreenHandler;
this.player = player;
this.input = input;
this.ring = params[2];
}
@Override
public void setStack(ItemStack stack) {
if (!stack.isEmpty() && !ItemStack.areEqual(stack, getStack())) {
BufferedExecutor.bufferExecution(player, () -> {
player.playSound(stack.getItem() == UItems.BOTCHED_GEM ? USounds.GUI_ABILITY_FAIL : USounds.GUI_SPELL_CRAFT_SUCCESS, SoundCategory.MASTER, 1, 0.3F);
});
}
super.setStack(stack);
}
@Override
public int getRing() {
return ring;
}
@Override
public boolean isEnabled() {
return handler.canShowSlots.test(SlotType.CRAFTING) && hasStack();
}
@Override
public void onTakeItem(PlayerEntity player, ItemStack stack) {
Pony pony = Pony.of(player);
InventoryUtil.stream(input).forEach(s -> {
pony.getDiscoveries().unlock(s.getItem());
});
//gemSlot.setStack(ItemStack.EMPTY);
super.onTakeItem(player, stack);
}
}

View file

@ -0,0 +1,6 @@
package com.minelittlepony.unicopia.container.inventory;
public enum SlotType {
INVENTORY,
CRAFTING
}

View file

@ -0,0 +1,47 @@
package com.minelittlepony.unicopia.container.inventory;
import java.util.List;
public interface SpellbookCraftingGrid {
/**
* Creates a hexagonal crafting grid.
* @param grid Output for normal slot positions.
* @param gemPos Output for the gem slot position.
*/
static void createGrid(List<int[]> grid, List<int[]> 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++) {
int ring = 3;
if (row == 0 || row == 6) {
ring = 1;
} else if ((row == 1 || row == 5) && i > 0 && i < cols - 1) {
ring = 2;
} else {
if (i == 0 || i == cols - 1) {
ring = 1;
} else if (i == 1 || i == cols - 2) {
ring = 2;
}
}
(row == 3 && i == 3 ? gemPos : grid).add(new int[] {
left + (i * spacing),
top,
row == 3 && i == 3 ? 4 : ring
});
}
top += spacing * 0.9;
left -= (spacing / 2) * (row > 2 ? -1 : 1);
cols += row > 2 ? -1 : 1;
}
}
}

View file

@ -0,0 +1,52 @@
package com.minelittlepony.unicopia.container.inventory;
import com.minelittlepony.unicopia.ability.magic.spell.trait.SpellTraits;
import com.minelittlepony.unicopia.container.SpellbookScreenHandler;
import com.minelittlepony.unicopia.util.InventoryUtil;
import net.minecraft.inventory.CraftingInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.screen.slot.Slot;
public class SpellbookInventory extends CraftingInventory {
private final SpellbookScreenHandler handler;
public SpellbookInventory(SpellbookScreenHandler handler, int width, int height) {
super(handler, width, height);
this.handler = handler;
}
public ItemStack getItemToModify() {
return handler.gemSlot.getStack();
}
public boolean hasIngredients() {
for (int i = 0; i < handler.GEM_SLOT_INDEX; i++) {
if (!getStack(i).isEmpty()) {
return true;
}
}
return false;
}
public int getRing(int slot) {
Slot s = handler.slots.get(slot);
return s instanceof SpellbookSlot ? ((SpellbookSlot)s).getRing() : 0;
}
public SpellTraits getTraits() {
return SpellTraits.union(InventoryUtil.slots(this)
.map(slot -> SpellTraits.of(getStack(slot)).multiply(getRingFactor(getRing(slot))))
.toArray(SpellTraits[]::new)
);
}
public static float getRingFactor(int ring) {
switch (ring) {
case 1: return 1;
case 2: return 0.6F;
case 3: return 0.3F;
default: return 0;
}
}
}

View file

@ -0,0 +1,5 @@
package com.minelittlepony.unicopia.container.inventory;
public interface SpellbookSlot {
int getRing();
}