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 @Override
public boolean matches(SpellbookInventory inventory, World world) { public boolean matches(SpellbookInventory inventory, World world) {
return requirement.test(inventory.getItemToModify()) return requirement.test(inventory.getItemToModify()) && requiredTraits.test(inventory.getTraits());
&& requiredTraits.test(SpellTraits.union(
inventory.getTraits(),
SpellTraits.of(output)
));
} }
@Override @Override

View file

@ -200,7 +200,7 @@ public final class SpellType<T extends Spell> implements Affine, SpellPredicate<
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public static <T extends Spell> SpellType<T> getKey(@Nullable Identifier id) { 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) { 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 @Override
public int hashCode() { public int hashCode() {
return traits.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.crafting.SpellbookRecipe;
import com.minelittlepony.unicopia.ability.magic.spell.trait.Trait; import com.minelittlepony.unicopia.ability.magic.spell.trait.Trait;
import com.minelittlepony.unicopia.ability.magic.spell.trait.TraitDiscovery; 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.container.SpellbookScreenHandler.SpellbookSlot;
import com.minelittlepony.unicopia.entity.player.Pony; import com.minelittlepony.unicopia.entity.player.Pony;
import com.minelittlepony.unicopia.item.URecipes; import com.minelittlepony.unicopia.item.URecipes;
@ -120,7 +120,7 @@ public class SpellbookScreen extends HandledScreen<SpellbookScreenHandler> {
if (slot.isEnabled() && slot instanceof SpellbookSlot) { if (slot.isEnabled() && slot instanceof SpellbookSlot) {
drawTexture(matrices, slot.x - 8, slot.y - 8, 0, 0, 32, 32, 32, 32); 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.setShaderColor(1, 1, 1, 0.3F);
RenderSystem.setShaderTexture(0, new Identifier("unicopia", "textures/item/gemstone.png")); RenderSystem.setShaderTexture(0, new Identifier("unicopia", "textures/item/gemstone.png"));
drawTexture(matrices, slot.x, slot.y, 0, 0, 16, 16, 16, 16); 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.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional;
import com.minelittlepony.unicopia.EquinePredicates; import com.minelittlepony.unicopia.EquinePredicates;
import com.minelittlepony.unicopia.ability.magic.spell.trait.SpellTraits; 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.SoundCategory;
import net.minecraft.sound.SoundEvents; import net.minecraft.sound.SoundEvents;
import net.minecraft.util.Identifier; import net.minecraft.util.Identifier;
import net.minecraft.util.collection.DefaultedList;
public class SpellbookScreenHandler extends ScreenHandler { public class SpellbookScreenHandler extends ScreenHandler {
private static final Identifier[] EMPTY_ARMOR_SLOT_TEXTURES = new Identifier[]{ private static final Identifier[] EMPTY_ARMOR_SLOT_TEXTURES = new Identifier[]{
PlayerScreenHandler.EMPTY_BOOTS_SLOT_TEXTURE, PlayerScreenHandler.EMPTY_BOOTS_SLOT_TEXTURE,
PlayerScreenHandler.EMPTY_LEGGINGS_SLOT_TEXTURE, PlayerScreenHandler.EMPTY_LEGGINGS_SLOT_TEXTURE,
@ -50,7 +47,8 @@ public class SpellbookScreenHandler extends ScreenHandler {
private final SpellbookInventory input; private final SpellbookInventory input;
private OutputSlot gemSlot; private InputSlot gemSlot;
private ResultSlot outputSlot;
private final CraftingResultInventory result = new CraftingResultInventory(); private final CraftingResultInventory result = new CraftingResultInventory();
private final PlayerInventory inventory; private final PlayerInventory inventory;
@ -70,18 +68,19 @@ public class SpellbookScreenHandler extends ScreenHandler {
List<int[]> gemPos = new ArrayList<>(); List<int[]> gemPos = new ArrayList<>();
createGrid(grid, gemPos); 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_START = GEM_SLOT_INDEX + 1;
HOTBAR_END = HOTBAR_START + 9; 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++) { for (int i = 0; i < MAX_INGREDIENTS; i++) {
var pos = grid.get(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) { 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));
@ -128,6 +127,8 @@ public class SpellbookScreenHandler extends ScreenHandler {
} }
}); });
addSlot(outputSlot = new ResultSlot(inventory.player, input, result, 0, gemPos.get(0)));
onContentChanged(input); onContentChanged(input);
} }
@ -140,12 +141,14 @@ public class SpellbookScreenHandler extends ScreenHandler {
public void onContentChanged(Inventory inventory) { public void onContentChanged(Inventory inventory) {
context.run((world, pos) -> { context.run((world, pos) -> {
if (!world.isClient && !gemSlot.getStack().isEmpty()) { if (!world.isClient && !gemSlot.getStack().isEmpty()) {
world.getServer().getRecipeManager().getFirstMatch(URecipes.SPELLBOOK, input, world) outputSlot.setStack(
.filter(recipe -> result.shouldCraftRecipe(world, (ServerPlayerEntity)this.inventory.player, recipe)) world.getServer().getRecipeManager()
.map(recipe -> recipe.craft(input)) .getFirstMatch(URecipes.SPELLBOOK, input, world)
.ifPresentOrElse(gemSlot::setCrafted, gemSlot::setUncrafted); .filter(recipe -> result.shouldCraftRecipe(world, (ServerPlayerEntity)this.inventory.player, recipe))
.map(recipe -> recipe.craft(input))
.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 @Override
public void close(PlayerEntity playerEntity) { public void close(PlayerEntity playerEntity) {
gemSlot.setUncrafted();
super.close(playerEntity); super.close(playerEntity);
context.run((world, pos) -> { context.run((world, pos) -> {
dropInventory(playerEntity, input); dropInventory(playerEntity, input);
@ -314,13 +316,12 @@ public class SpellbookScreenHandler extends ScreenHandler {
} }
public class SpellbookInventory extends CraftingInventory { public class SpellbookInventory extends CraftingInventory {
public SpellbookInventory(ScreenHandler handler, int width, int height) { public SpellbookInventory(ScreenHandler handler, int width, int height) {
super(handler, width, height); super(handler, width, height);
} }
public ItemStack getItemToModify() { public ItemStack getItemToModify() {
return gemSlot.uncrafted.orElse(gemSlot.getStack()); return gemSlot.getStack();
} }
public int getRing(int slot) { 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) { public InventorySlot(Inventory inventory, int index, int x, int y) {
super(inventory, index, x, 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; 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]); super(inventory, index, params[0], params[1]);
ring = params[2]; ring = params[2];
} }
@ -374,50 +375,25 @@ public class SpellbookScreenHandler extends ScreenHandler {
return 1; return 1;
} }
@Override
public boolean canInsert(ItemStack stack) {
return true;
}
@Override @Override
public int getRing() { public int getRing() {
return ring; return ring;
} }
} }
public static class OutputSlot extends CraftingResultSlot implements SpellbookSlot { public class InputSlot extends Slot implements SpellbookSlot {
private Optional<ItemStack> uncrafted = Optional.empty();
private final PlayerEntity player;
private final SpellbookInventory input;
private final int ring; private final int ring;
public OutputSlot(PlayerEntity player, SpellbookInventory input, Inventory inventory, int index, int[] params) { public InputSlot(Inventory inventory, int index, int[] params) {
super(player, input, inventory, index, params[0], params[1]); super(inventory, index, params[0], params[1]);
this.player = player;
this.input = input;
this.ring = params[2]; 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 @Override
public int getMaxItemCount() { public int getMaxItemCount() {
return 1; return 1;
@ -429,38 +405,54 @@ public class SpellbookScreenHandler extends ScreenHandler {
} }
@Override @Override
public void onTakeItem(PlayerEntity player, ItemStack stack) { public boolean isEnabled() {
if (uncrafted.isPresent()) { return !outputSlot.isEnabled();
uncrafted = Optional.empty(); }
onCrafted(stack); }
Pony pony = Pony.of(player); public static class ResultSlot extends CraftingResultSlot implements SpellbookSlot {
InventoryUtil.iterate(input).forEach(s -> { private final PlayerEntity player;
pony.getDiscoveries().unlock(s.getItem()); private final SpellbookInventory input;
});
DefaultedList<ItemStack> defaultedList = player.world.getRecipeManager().getRemainingStacks(URecipes.SPELLBOOK, input, player.world); private final int ring;
for (int i = 0; i < defaultedList.size(); ++i) { public ResultSlot(PlayerEntity player, SpellbookInventory input, Inventory inventory, int index, int[] params) {
ItemStack itemStack = input.getStack(i); super(player, input, inventory, index, params[0], params[1]);
ItemStack itemStack2 = defaultedList.get(i); this.player = player;
if (!itemStack.isEmpty()) { this.input = input;
input.removeStack(i, 1); this.ring = params[2];
itemStack = input.getStack(i); }
}
if (!itemStack2.isEmpty()) { @Override
if (itemStack.isEmpty()) { public boolean canInsert(ItemStack stack) {
input.setStack(i, itemStack2); return stack.getItem() == UItems.GEMSTONE;
} else if (ItemStack.areItemsEqualIgnoreDamage(itemStack, itemStack2) && ItemStack.areNbtEqual(itemStack, itemStack2)) { }
itemStack2.increment(itemStack.getCount());
input.setStack(i, itemStack2); @Override
} else if (!player.getInventory().insertStack(itemStack2)) { public void setStack(ItemStack stack) {
player.dropItem(itemStack2, false); 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());
});
super.onTakeItem(player, stack);
}
} }
} }