From e3c5f6fc4b3719630ba040d45e50de682e2705f7 Mon Sep 17 00:00:00 2001 From: Sollace Date: Mon, 1 Jun 2020 17:00:22 +0200 Subject: [PATCH] Fixed the spellbook --- .../unicopia/client/gui/SpellBookScreen.java | 21 +++++++----------- .../unicopia/client/gui/UScreens.java | 1 + .../container/SpellBookContainer.java | 2 +- .../unicopia/enchanting/PageInstance.java | 4 +++- .../unicopia/enchanting/PageOwner.java | 6 +++-- .../unicopia/enchanting/Pages.java | 3 +++ .../unicopia/entity/SpellbookEntity.java | 19 ++++++++-------- .../ingredient/PredicatedIngredient.java | 22 +++++++++---------- .../recipes/enchantments/alicorn_amulet.json | 6 ++--- 9 files changed, 42 insertions(+), 42 deletions(-) diff --git a/src/main/java/com/minelittlepony/unicopia/client/gui/SpellBookScreen.java b/src/main/java/com/minelittlepony/unicopia/client/gui/SpellBookScreen.java index 05379731..79666570 100644 --- a/src/main/java/com/minelittlepony/unicopia/client/gui/SpellBookScreen.java +++ b/src/main/java/com/minelittlepony/unicopia/client/gui/SpellBookScreen.java @@ -17,27 +17,22 @@ import net.minecraft.client.gui.screen.ingame.ContainerScreen; import net.minecraft.client.texture.MissingSprite; import net.minecraft.container.Slot; import net.minecraft.entity.player.PlayerEntity; -import net.minecraft.text.LiteralText; import net.minecraft.util.Identifier; +import net.minecraft.util.PacketByteBuf; public class SpellBookScreen extends ContainerScreen implements IPageUnlockListener { private static Page currentPage; - public static final Identifier spellBookGuiTextures = new Identifier("unicopia", "textures/gui/container/book.png"); + public static final Identifier TEXTURE = new Identifier("unicopia", "textures/gui/container/book.png"); private Pony player; private PageButton nextPage; private PageButton prevPage; - public SpellBookScreen(PlayerEntity player) { - super( - new SpellBookContainer(0, null, player, null), - player.inventory, - new LiteralText("item.spellbook.name") - ); - player.container = container; + public SpellBookScreen(int sync, Identifier id, PlayerEntity player, PacketByteBuf buf) { + super(new SpellBookContainer(sync, id, player, buf), player.inventory, buf.readText()); containerWidth = 405; containerHeight = 219; @@ -108,7 +103,7 @@ public class SpellBookScreen extends ContainerScreen impleme GlStateManager.enableBlend(); GL11.glDisable(GL11.GL_ALPHA_TEST); - minecraft.getTextureManager().bindTexture(spellBookGuiTextures); + minecraft.getTextureManager().bindTexture(TEXTURE); blit(slot.xPosition - 1, slot.yPosition - 1, 74, 223, 18, 18, 512, 256); GL11.glEnable(GL11.GL_ALPHA_TEST); @@ -143,13 +138,13 @@ public class SpellBookScreen extends ContainerScreen impleme int left = (width - containerWidth) / 2; int top = (height - containerHeight) / 2; - minecraft.getTextureManager().bindTexture(spellBookGuiTextures); + minecraft.getTextureManager().bindTexture(TEXTURE); blit(left, top, 0, 0, containerWidth, containerHeight, 512, 256); GlStateManager.enableBlend(); GL11.glDisable(GL11.GL_ALPHA_TEST); - minecraft.getTextureManager().bindTexture(spellBookGuiTextures); + minecraft.getTextureManager().bindTexture(TEXTURE); blit(left + 147, top + 49, 407, 2, 100, 101, 512, 256); if (player.getPages().getPageState(currentPage) != PageState.LOCKED) { @@ -205,7 +200,7 @@ public class SpellBookScreen extends ContainerScreen impleme } GlStateManager.color4f(1, 1, 1, 1); - minecraft.getTextureManager().bindTexture(spellBookGuiTextures); + minecraft.getTextureManager().bindTexture(TEXTURE); int u = 0; int v = 220; diff --git a/src/main/java/com/minelittlepony/unicopia/client/gui/UScreens.java b/src/main/java/com/minelittlepony/unicopia/client/gui/UScreens.java index b2150f43..3cbff9fa 100644 --- a/src/main/java/com/minelittlepony/unicopia/client/gui/UScreens.java +++ b/src/main/java/com/minelittlepony/unicopia/client/gui/UScreens.java @@ -8,5 +8,6 @@ public interface UScreens { static void bootstrap() { ScreenProviderRegistry.INSTANCE.registerFactory(UContainers.BAG_OF_HOLDING, BagOfHoldingScreen::new); + ScreenProviderRegistry.INSTANCE.registerFactory(UContainers.SPELL_BOOK, SpellBookScreen::new); } } diff --git a/src/main/java/com/minelittlepony/unicopia/container/SpellBookContainer.java b/src/main/java/com/minelittlepony/unicopia/container/SpellBookContainer.java index 5c8997fb..2f5cdf8a 100644 --- a/src/main/java/com/minelittlepony/unicopia/container/SpellBookContainer.java +++ b/src/main/java/com/minelittlepony/unicopia/container/SpellBookContainer.java @@ -38,7 +38,7 @@ public class SpellBookContainer extends Container { private final PlayerEntity player; public SpellBookContainer(int sync, Identifier id, PlayerEntity player, PacketByteBuf buf) { - super(null, 0); + super(null, sync); worldObj = player.world; this.player = player; diff --git a/src/main/java/com/minelittlepony/unicopia/enchanting/PageInstance.java b/src/main/java/com/minelittlepony/unicopia/enchanting/PageInstance.java index 90e933c3..573b371f 100644 --- a/src/main/java/com/minelittlepony/unicopia/enchanting/PageInstance.java +++ b/src/main/java/com/minelittlepony/unicopia/enchanting/PageInstance.java @@ -83,6 +83,9 @@ class PageInstance implements Page { @Override public Page next() { int i = Math.min(Pages.instance().getTotalPages() - 1, index + 1); + if (i == index) { + return this; + } return Pages.instance().getByIndex(i); } @@ -91,7 +94,6 @@ class PageInstance implements Page { if (index <= 0) { return this; } - return Pages.instance().getByIndex(index - 1); } diff --git a/src/main/java/com/minelittlepony/unicopia/enchanting/PageOwner.java b/src/main/java/com/minelittlepony/unicopia/enchanting/PageOwner.java index bfc6d2a7..02f247b5 100644 --- a/src/main/java/com/minelittlepony/unicopia/enchanting/PageOwner.java +++ b/src/main/java/com/minelittlepony/unicopia/enchanting/PageOwner.java @@ -32,12 +32,14 @@ public interface PageOwner extends Transmittable { } default boolean hasPageStateRelative(Page page, PageState state, Function iter) { - while ((page = iter.apply(page)) != null) { + for (Page prev = null; + (page = iter.apply(page)) != null && page != prev; + prev = page + ) { if (getPageState(page) == state) { return true; } } - return false; } } diff --git a/src/main/java/com/minelittlepony/unicopia/enchanting/Pages.java b/src/main/java/com/minelittlepony/unicopia/enchanting/Pages.java index f54a4d89..c412c88e 100644 --- a/src/main/java/com/minelittlepony/unicopia/enchanting/Pages.java +++ b/src/main/java/com/minelittlepony/unicopia/enchanting/Pages.java @@ -103,6 +103,9 @@ public class Pages extends JsonDataLoader implements IdentifiableResourceReloadL @Nullable public Page getByIndex(int index) { + if (index < 0 || index >= pagesByIndex.size()) { + return null; + } return pagesByIndex.get(index); } diff --git a/src/main/java/com/minelittlepony/unicopia/entity/SpellbookEntity.java b/src/main/java/com/minelittlepony/unicopia/entity/SpellbookEntity.java index 492d3822..5f97f568 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/SpellbookEntity.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/SpellbookEntity.java @@ -1,11 +1,11 @@ package com.minelittlepony.unicopia.entity; import com.minelittlepony.unicopia.EquinePredicates; +import com.minelittlepony.unicopia.container.UContainers; import com.minelittlepony.unicopia.ducks.PickedItemSupplier; import com.minelittlepony.unicopia.item.UItems; -import net.minecraft.container.Container; -import net.minecraft.container.NameableContainerFactory; +import net.fabricmc.fabric.api.container.ContainerProviderRegistry; import net.minecraft.entity.EntityType; import net.minecraft.entity.damage.DamageSource; import net.minecraft.entity.data.DataTracker; @@ -13,11 +13,11 @@ import net.minecraft.entity.data.TrackedData; import net.minecraft.entity.data.TrackedDataHandlerRegistry; import net.minecraft.entity.mob.MobEntity; import net.minecraft.entity.player.PlayerEntity; -import net.minecraft.entity.player.PlayerInventory; import net.minecraft.item.ItemStack; import net.minecraft.nbt.CompoundTag; import net.minecraft.particle.ParticleTypes; import net.minecraft.predicate.entity.EntityPredicates; +import net.minecraft.server.network.ServerPlayerEntity; import net.minecraft.sound.BlockSoundGroup; import net.minecraft.sound.SoundCategory; import net.minecraft.sound.SoundEvents; @@ -27,7 +27,7 @@ import net.minecraft.util.math.Vec3d; import net.minecraft.world.GameRules; import net.minecraft.world.World; -public class SpellbookEntity extends MobEntity implements NameableContainerFactory, IMagicals, PickedItemSupplier { +public class SpellbookEntity extends MobEntity implements IMagicals, PickedItemSupplier { private static final TrackedData OPENED = DataTracker.registerData(SpellbookEntity.class, TrackedDataHandlerRegistry.BOOLEAN); private static final TrackedData ALTERED = DataTracker.registerData(SpellbookEntity.class, TrackedDataHandlerRegistry.BOOLEAN); @@ -175,19 +175,18 @@ public class SpellbookEntity extends MobEntity implements NameableContainerFacto } if (EquinePredicates.PLAYER_UNICORN.test(player)) { + if (player instanceof ServerPlayerEntity) { + ContainerProviderRegistry.INSTANCE.openContainer(UContainers.SPELL_BOOK, player, o -> { + o.writeText(getName()); + }); + } player.playSound(SoundEvents.BLOCK_FURNACE_FIRE_CRACKLE, 2, 1); - player.openContainer(this); return ActionResult.SUCCESS; } return ActionResult.PASS; } - @Override - public Container createMenu(int id, PlayerInventory inventory, PlayerEntity player) { - return null; - } - @Override public void readCustomDataFromTag(CompoundTag compound) { super.readCustomDataFromTag(compound); diff --git a/src/main/java/com/minelittlepony/unicopia/recipe/ingredient/PredicatedIngredient.java b/src/main/java/com/minelittlepony/unicopia/recipe/ingredient/PredicatedIngredient.java index f84a35fa..5ff07b27 100644 --- a/src/main/java/com/minelittlepony/unicopia/recipe/ingredient/PredicatedIngredient.java +++ b/src/main/java/com/minelittlepony/unicopia/recipe/ingredient/PredicatedIngredient.java @@ -36,9 +36,7 @@ public class PredicatedIngredient { .filter(s -> matches(s, 1)) .collect(Collectors.toList()); }); - this.preview = new Lazy<>(() -> { - return net.minecraft.recipe.Ingredient.ofStacks(getMatchingStacks().toArray(ItemStack[]::new)); - }); + this.preview = new Lazy<>(() -> Ingredient.ofStacks(getMatchingStacks().toArray(ItemStack[]::new))); } public Stream getMatchingStacks() { @@ -82,17 +80,17 @@ public class PredicatedIngredient { if (primary != Predicate.EMPTY && secondary != Predicate.EMPTY) { throw new JsonParseException("Invalid ingredient. Cannot have both an item and a tag requirement."); } - if (primary == secondary) { - throw new JsonParseException("Invalid ingredient. Must have either an item or tag requirement."); - } - DefaultedList predicates = DefaultedList.of(); - predicates.add(primary); - predicates.add(secondary); - PredicateSerializer.JSON_READERS.stream() - .map(reader -> reader.read(obj)) + DefaultedList predicates = Stream.concat( + Stream.of(primary, secondary), + PredicateSerializer.JSON_READERS.stream().map(reader -> reader.read(obj)) + ) .filter(i -> i != Predicate.EMPTY) - .forEach(predicates::add); + .collect(Collectors.toCollection(DefaultedList::of)); + + if (predicates.isEmpty()) { + return EMPTY; + } return new PredicatedIngredient(predicates); } diff --git a/src/main/resources/data/unicopia/recipes/enchantments/alicorn_amulet.json b/src/main/resources/data/unicopia/recipes/enchantments/alicorn_amulet.json index b4a8e8c6..08528b6e 100644 --- a/src/main/resources/data/unicopia/recipes/enchantments/alicorn_amulet.json +++ b/src/main/resources/data/unicopia/recipes/enchantments/alicorn_amulet.json @@ -2,9 +2,9 @@ "type": "unicopia:enchanting_spell", "input": { "item": "unicopia:corrupted_gem" }, "ingredients": [ - { "item": "unicopia:gem", "spell": "inferno" }, - { "item": "unicopia:gem", "spell": "darkness" }, - { "item": "unicopia:gem", "spell": "necromancy" } + { "spell": "inferno" }, + { "spell": "darkness" }, + { "spell": "necromancy" } ], "result": { "item": "unicopia:alicorn_amulet" } }