Fix item duplication/sync glitch in the spellbook

This commit is contained in:
Sollace 2022-09-11 18:52:26 +02:00
parent 697ae2b086
commit 3166fb3919
4 changed files with 8 additions and 27 deletions

View file

@ -147,7 +147,6 @@ public class PlaceableSpell extends AbstractDelegatingSpell {
if (compound.contains("castEntity")) { if (compound.contains("castEntity")) {
castEntity.fromNBT(compound.getCompound("castEntity")); castEntity.fromNBT(compound.getCompound("castEntity"));
} }
} }
@Override @Override

View file

@ -1,6 +1,5 @@
package com.minelittlepony.unicopia.ability.magic.spell.trait; package com.minelittlepony.unicopia.ability.magic.spell.trait;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.EnumMap; import java.util.EnumMap;
@ -188,9 +187,7 @@ public final class SpellTraits implements Iterable<Map.Entry<Trait, Float>> {
} }
public static SpellTraits of(Inventory inventory) { public static SpellTraits of(Inventory inventory) {
List<ItemStack> stacks = new ArrayList<>(); return of(InventoryUtil.stream(inventory).toList());
InventoryUtil.iterate(inventory).forEach(stacks::add);
return of(stacks);
} }
public static SpellTraits of(Collection<ItemStack> stacks) { public static SpellTraits of(Collection<ItemStack> stacks) {

View file

@ -168,18 +168,16 @@ public class SpellbookScreenHandler extends ScreenHandler {
super.onContentChanged(inventory); super.onContentChanged(inventory);
context.run((world, pos) -> { context.run((world, pos) -> {
if (!world.isClient && !gemSlot.getStack().isEmpty()) { if (!world.isClient && !gemSlot.getStack().isEmpty()) {
outputSlot.setStack( ItemStack resultStack = input.hasIngredients() ? world.getServer().getRecipeManager()
world.getServer().getRecipeManager()
.getAllMatches(URecipes.SPELLBOOK, input, world) .getAllMatches(URecipes.SPELLBOOK, input, world)
.stream().sorted(Comparator.comparing(SpellbookRecipe::getPriority)) .stream().sorted(Comparator.comparing(SpellbookRecipe::getPriority))
.findFirst() .findFirst()
.filter(recipe -> result.shouldCraftRecipe(world, (ServerPlayerEntity)this.inventory.player, recipe)) .filter(recipe -> result.shouldCraftRecipe(world, (ServerPlayerEntity)this.inventory.player, recipe))
.map(recipe -> recipe.craft(input)) .map(recipe -> recipe.craft(input))
.orElse(!input.hasIngredients() || input.getItemToModify().getItem() != UItems.GEMSTONE .orElse(input.getTraits().applyTo(UItems.BOTCHED_GEM.getDefaultStack())) : ItemStack.EMPTY;
? ItemStack.EMPTY outputSlot.setStack(resultStack);
: input.getTraits().applyTo(UItems.BOTCHED_GEM.getDefaultStack()))
);
setPreviousTrackedSlot(outputSlot.id, resultStack);
((ServerPlayerEntity)this.inventory.player).networkHandler.sendPacket(new ScreenHandlerSlotUpdateS2CPacket(syncId, nextRevision(), outputSlot.id, outputSlot.getStack())); ((ServerPlayerEntity)this.inventory.player).networkHandler.sendPacket(new ScreenHandlerSlotUpdateS2CPacket(syncId, nextRevision(), outputSlot.id, outputSlot.getStack()));
} }
}); });
@ -299,7 +297,6 @@ public class SpellbookScreenHandler extends ScreenHandler {
super.close(playerEntity); super.close(playerEntity);
context.run((world, pos) -> { context.run((world, pos) -> {
dropInventory(playerEntity, input); dropInventory(playerEntity, input);
dropInventory(playerEntity, result);
}); });
} }
@ -492,7 +489,7 @@ public class SpellbookScreenHandler extends ScreenHandler {
@Override @Override
public void onTakeItem(PlayerEntity player, ItemStack stack) { public void onTakeItem(PlayerEntity player, ItemStack stack) {
Pony pony = Pony.of(player); Pony pony = Pony.of(player);
InventoryUtil.iterate(input).forEach(s -> { InventoryUtil.stream(input).forEach(s -> {
pony.getDiscoveries().unlock(s.getItem()); pony.getDiscoveries().unlock(s.getItem());
}); });
//gemSlot.setStack(ItemStack.EMPTY); //gemSlot.setStack(ItemStack.EMPTY);

View file

@ -2,24 +2,12 @@ package com.minelittlepony.unicopia.util;
import java.util.stream.Stream; import java.util.stream.Stream;
import com.google.common.collect.AbstractIterator;
import net.minecraft.inventory.Inventory; import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
public interface InventoryUtil { public interface InventoryUtil {
static Iterable<ItemStack> iterate(Inventory inventory) { static Stream<ItemStack> stream(Inventory inventory) {
return () -> new AbstractIterator<>() { return slots(inventory).map(inventory::getStack);
private int slot = 0;
@Override
protected ItemStack computeNext() {
if (slot >= inventory.size()) {
return endOfData();
}
return inventory.getStack(slot++);
}
};
} }
static Stream<Integer> slots(Inventory inventory) { static Stream<Integer> slots(Inventory inventory) {