Fixed spell crafting

This commit is contained in:
Sollace 2022-01-03 14:05:32 +02:00
parent c3af3f9631
commit 256cbdcabc
5 changed files with 80 additions and 87 deletions

View file

@ -42,11 +42,7 @@ public class TraitRequirementRecipe implements SpellbookRecipe {
@Override
public boolean matches(SpellbookInventory inventory, World world) {
return requirement.test(inventory.getItemToModify())
&& requiredTraits.test(SpellTraits.union(
inventory.getTraits(),
SpellTraits.of(output)
));
return requirement.test(inventory.getItemToModify()) && requiredTraits.test(inventory.getTraits());
}
@Override

View file

@ -200,7 +200,7 @@ public final class SpellType<T extends Spell> implements Affine, SpellPredicate<
@SuppressWarnings("unchecked")
public static <T extends Spell> SpellType<T> getKey(@Nullable Identifier id) {
return (SpellType<T>)REGISTRY.get(id);
return (SpellType<T>)REGISTRY.getOrEmpty(id).orElse(EMPTY_KEY);
}
public static Set<SpellType<?>> byAffinity(Affinity affinity) {

View file

@ -127,6 +127,11 @@ public final class SpellTraits implements Iterable<Map.Entry<Trait, Float>> {
});
}
@Override
public String toString() {
return super.toString() + "[" + traits.entrySet().stream().map(e -> e.getKey() + "=" + e.getValue()).collect(Collectors.joining(",")) + "]";
}
@Override
public int hashCode() {
return traits.hashCode();

View file

@ -7,7 +7,7 @@ import com.minelittlepony.common.client.gui.sprite.TextureSprite;
import com.minelittlepony.unicopia.ability.magic.spell.crafting.SpellbookRecipe;
import com.minelittlepony.unicopia.ability.magic.spell.trait.Trait;
import com.minelittlepony.unicopia.ability.magic.spell.trait.TraitDiscovery;
import com.minelittlepony.unicopia.container.SpellbookScreenHandler.OutputSlot;
import com.minelittlepony.unicopia.container.SpellbookScreenHandler.InputSlot;
import com.minelittlepony.unicopia.container.SpellbookScreenHandler.SpellbookSlot;
import com.minelittlepony.unicopia.entity.player.Pony;
import com.minelittlepony.unicopia.item.URecipes;
@ -120,7 +120,7 @@ public class SpellbookScreen extends HandledScreen<SpellbookScreenHandler> {
if (slot.isEnabled() && slot instanceof SpellbookSlot) {
drawTexture(matrices, slot.x - 8, slot.y - 8, 0, 0, 32, 32, 32, 32);
if (slot instanceof OutputSlot) {
if (slot instanceof InputSlot) {
RenderSystem.setShaderColor(1, 1, 1, 0.3F);
RenderSystem.setShaderTexture(0, new Identifier("unicopia", "textures/item/gemstone.png"));
drawTexture(matrices, slot.x, slot.y, 0, 0, 16, 16, 16, 16);

View file

@ -2,7 +2,6 @@ package com.minelittlepony.unicopia.container;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import com.minelittlepony.unicopia.EquinePredicates;
import com.minelittlepony.unicopia.ability.magic.spell.trait.SpellTraits;
@ -31,10 +30,8 @@ import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.sound.SoundCategory;
import net.minecraft.sound.SoundEvents;
import net.minecraft.util.Identifier;
import net.minecraft.util.collection.DefaultedList;
public class SpellbookScreenHandler extends ScreenHandler {
private static final Identifier[] EMPTY_ARMOR_SLOT_TEXTURES = new Identifier[]{
PlayerScreenHandler.EMPTY_BOOTS_SLOT_TEXTURE,
PlayerScreenHandler.EMPTY_LEGGINGS_SLOT_TEXTURE,
@ -50,7 +47,8 @@ public class SpellbookScreenHandler extends ScreenHandler {
private final SpellbookInventory input;
private OutputSlot gemSlot;
private InputSlot gemSlot;
private ResultSlot outputSlot;
private final CraftingResultInventory result = new CraftingResultInventory();
private final PlayerInventory inventory;
@ -70,18 +68,19 @@ public class SpellbookScreenHandler extends ScreenHandler {
List<int[]> gemPos = new ArrayList<>();
createGrid(grid, gemPos);
GEM_SLOT_INDEX = MAX_INGREDIENTS = grid.size();
MAX_INGREDIENTS = grid.size();
GEM_SLOT_INDEX = MAX_INGREDIENTS;
HOTBAR_START = GEM_SLOT_INDEX + 1;
HOTBAR_END = HOTBAR_START + 9;
input = new SpellbookInventory(this, MAX_INGREDIENTS, 1);
input = new SpellbookInventory(this, MAX_INGREDIENTS + 1, 1);
for (int i = 0; i < MAX_INGREDIENTS; i++) {
var pos = grid.get(i);
addSlot(new InputSlot(input, i, pos));
addSlot(new ModifierSlot(input, i, pos));
}
addSlot(gemSlot = new OutputSlot(inventory.player, input, result, 0, gemPos.get(0)));
addSlot(gemSlot = new InputSlot(input, MAX_INGREDIENTS, gemPos.get(0)));
for (int i = 0; i < 9; ++i) {
addSlot(new Slot(inventory, i, 121 + i * 18, 195));
@ -128,6 +127,8 @@ public class SpellbookScreenHandler extends ScreenHandler {
}
});
addSlot(outputSlot = new ResultSlot(inventory.player, input, result, 0, gemPos.get(0)));
onContentChanged(input);
}
@ -140,12 +141,14 @@ public class SpellbookScreenHandler extends ScreenHandler {
public void onContentChanged(Inventory inventory) {
context.run((world, pos) -> {
if (!world.isClient && !gemSlot.getStack().isEmpty()) {
world.getServer().getRecipeManager().getFirstMatch(URecipes.SPELLBOOK, input, world)
outputSlot.setStack(
world.getServer().getRecipeManager()
.getFirstMatch(URecipes.SPELLBOOK, input, world)
.filter(recipe -> result.shouldCraftRecipe(world, (ServerPlayerEntity)this.inventory.player, recipe))
.map(recipe -> recipe.craft(input))
.ifPresentOrElse(gemSlot::setCrafted, gemSlot::setUncrafted);
.orElse(ItemStack.EMPTY));
((ServerPlayerEntity)this.inventory.player).networkHandler.sendPacket(new ScreenHandlerSlotUpdateS2CPacket(syncId, nextRevision(), GEM_SLOT_INDEX, gemSlot.getStack()));
((ServerPlayerEntity)this.inventory.player).networkHandler.sendPacket(new ScreenHandlerSlotUpdateS2CPacket(syncId, nextRevision(), outputSlot.id, outputSlot.getStack()));
}
});
}
@ -261,7 +264,6 @@ public class SpellbookScreenHandler extends ScreenHandler {
@Override
public void close(PlayerEntity playerEntity) {
gemSlot.setUncrafted();
super.close(playerEntity);
context.run((world, pos) -> {
dropInventory(playerEntity, input);
@ -314,13 +316,12 @@ public class SpellbookScreenHandler extends ScreenHandler {
}
public class SpellbookInventory extends CraftingInventory {
public SpellbookInventory(ScreenHandler handler, int width, int height) {
super(handler, width, height);
}
public ItemStack getItemToModify() {
return gemSlot.uncrafted.orElse(gemSlot.getStack());
return gemSlot.getStack();
}
public int getRing(int slot) {
@ -345,7 +346,7 @@ public class SpellbookScreenHandler extends ScreenHandler {
}
}
public class InventorySlot extends Slot implements SpellbookSlot {
public static class InventorySlot extends Slot implements SpellbookSlot {
public InventorySlot(Inventory inventory, int index, int x, int y) {
super(inventory, index, x, y);
}
@ -361,10 +362,10 @@ public class SpellbookScreenHandler extends ScreenHandler {
}
}
public class InputSlot extends Slot implements SpellbookSlot {
public static class ModifierSlot extends Slot implements SpellbookSlot {
private final int ring;
public InputSlot(Inventory inventory, int index, int[] params) {
public ModifierSlot(Inventory inventory, int index, int[] params) {
super(inventory, index, params[0], params[1]);
ring = params[2];
}
@ -374,50 +375,25 @@ public class SpellbookScreenHandler extends ScreenHandler {
return 1;
}
@Override
public boolean canInsert(ItemStack stack) {
return true;
}
@Override
public int getRing() {
return ring;
}
}
public static class OutputSlot extends CraftingResultSlot implements SpellbookSlot {
private Optional<ItemStack> uncrafted = Optional.empty();
private final PlayerEntity player;
private final SpellbookInventory input;
public class InputSlot extends Slot implements SpellbookSlot {
private final int ring;
public OutputSlot(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;
public InputSlot(Inventory inventory, int index, int[] params) {
super(inventory, index, params[0], params[1]);
this.ring = params[2];
}
public void setCrafted(ItemStack crafted) {
uncrafted = uncrafted.or(() -> Optional.of(getStack()));
ItemStack old = getStack();
setStack(crafted);
if (!ItemStack.areEqual(old, crafted)) {
player.playSound(SoundEvents.BLOCK_END_PORTAL_FRAME_FILL, SoundCategory.MASTER, 1, 0.3F);
}
}
public void setUncrafted() {
uncrafted = uncrafted.filter(stack -> {
player.playSound(SoundEvents.BLOCK_END_PORTAL_FRAME_FILL, SoundCategory.MASTER, 0.2F, 0.2F);
setStack(stack);
return false;
});
}
@Override
public boolean canInsert(ItemStack stack) {
return stack.getItem() == UItems.GEMSTONE;
}
@Override
public int getMaxItemCount() {
return 1;
@ -429,38 +405,54 @@ public class SpellbookScreenHandler extends ScreenHandler {
}
@Override
public void onTakeItem(PlayerEntity player, ItemStack stack) {
if (uncrafted.isPresent()) {
uncrafted = Optional.empty();
onCrafted(stack);
public boolean isEnabled() {
return !outputSlot.isEnabled();
}
}
public static 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 boolean canInsert(ItemStack stack) {
return stack.getItem() == UItems.GEMSTONE;
}
@Override
public void setStack(ItemStack stack) {
super.setStack(stack);
if (!stack.isEmpty()) {
player.playSound(SoundEvents.BLOCK_END_PORTAL_FRAME_FILL, SoundCategory.MASTER, 1, 0.3F);
}
}
@Override
public int getRing() {
return ring;
}
@Override
public boolean isEnabled() {
return hasStack();
}
@Override
public void onTakeItem(PlayerEntity player, ItemStack 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.areNbtEqual(itemStack, itemStack2)) {
itemStack2.increment(itemStack.getCount());
input.setStack(i, itemStack2);
} else if (!player.getInventory().insertStack(itemStack2)) {
player.dropItem(itemStack2, false);
}
}
}
}
super.onTakeItem(player, stack);
}
}
}