From e753a230154975399818539193922819d61f30d8 Mon Sep 17 00:00:00 2001 From: MrHell228 Date: Wed, 15 May 2024 05:52:26 +0300 Subject: [PATCH 1/9] Open container from type --- .../inventory/InventoryEventFactory.java | 30 +++++++++++++++---- .../ServerPlayerMixin_Inventory_API.java | 16 ++++++++++ 2 files changed, 41 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/spongepowered/common/event/inventory/InventoryEventFactory.java b/src/main/java/org/spongepowered/common/event/inventory/InventoryEventFactory.java index 07c6de04c64..dfa469a4a84 100644 --- a/src/main/java/org/spongepowered/common/event/inventory/InventoryEventFactory.java +++ b/src/main/java/org/spongepowered/common/event/inventory/InventoryEventFactory.java @@ -38,6 +38,7 @@ import net.minecraft.world.inventory.AbstractContainerMenu; import net.minecraft.world.inventory.AnvilMenu; import net.minecraft.world.inventory.EnchantmentMenu; +import net.minecraft.world.inventory.MenuType; import net.minecraft.world.inventory.MerchantMenu; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.enchantment.EnchantmentInstance; @@ -239,9 +240,8 @@ public static boolean callInteractContainerOpenEvent(final ServerPlayer player) return true; } - public static @org.checkerframework.checker.nullness.qual.Nullable AbstractContainerMenu displayContainer(final ServerPlayer player, final Inventory inventory, final Component displayName) { - final net.minecraft.world.inventory.AbstractContainerMenu previousContainer = player.containerMenu; - final net.minecraft.world.inventory.AbstractContainerMenu container; + public static @Nullable AbstractContainerMenu displayContainer(final ServerPlayer player, final Inventory inventory, final Component displayName) { + final AbstractContainerMenu previousContainer = player.containerMenu; Optional viewable = inventory.asViewable(); if (viewable.isPresent()) { @@ -276,8 +276,7 @@ public static boolean callInteractContainerOpenEvent(final ServerPlayer player) horse.openCustomInventoryScreen(player); } - container = player.containerMenu; - + final AbstractContainerMenu container = player.containerMenu; if (previousContainer == container) { return null; } @@ -300,6 +299,27 @@ public static boolean callInteractContainerOpenEvent(final ServerPlayer player) return container; } + public static @Nullable AbstractContainerMenu displayContainer(final ServerPlayer player, final MenuType type, Component displayName) { + final AbstractContainerMenu previousContainer = player.containerMenu; + + if (displayName == null) { + displayName = Component.text("CustomContainer"); + } + + final MenuProvider menuProvider = new SimpleMenuProvider((menuId, playerInv, p) -> type.create(menuId, playerInv), SpongeAdventure.asVanilla(displayName)); + player.openMenu(menuProvider); + final AbstractContainerMenu container = player.containerMenu; + if (previousContainer == container) { + return null; + } + + if (!InventoryEventFactory.callInteractContainerOpenEvent(player)) { + return null; + } + + return player.containerMenu; + } + public static TransferInventoryEvent.Pre callTransferPre(final Inventory source, final Inventory destination) { PhaseTracker.getCauseStackManager().pushCause(source); final TransferInventoryEvent.Pre event = SpongeEventFactory.createTransferInventoryEventPre( diff --git a/src/mixins/java/org/spongepowered/common/mixin/inventory/api/server/level/ServerPlayerMixin_Inventory_API.java b/src/mixins/java/org/spongepowered/common/mixin/inventory/api/server/level/ServerPlayerMixin_Inventory_API.java index 3c4711ab9f7..c0913065f32 100644 --- a/src/mixins/java/org/spongepowered/common/mixin/inventory/api/server/level/ServerPlayerMixin_Inventory_API.java +++ b/src/mixins/java/org/spongepowered/common/mixin/inventory/api/server/level/ServerPlayerMixin_Inventory_API.java @@ -25,9 +25,11 @@ package org.spongepowered.common.mixin.inventory.api.server.level; import net.kyori.adventure.text.Component; +import net.minecraft.world.inventory.MenuType; import org.checkerframework.checker.nullness.qual.NonNull; import org.spongepowered.api.entity.living.player.server.ServerPlayer; import org.spongepowered.api.item.inventory.Container; +import org.spongepowered.api.item.inventory.ContainerType; import org.spongepowered.api.item.inventory.Inventory; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.common.bridge.world.inventory.container.ContainerBridge; @@ -64,6 +66,20 @@ public Optional openInventory(final Inventory inventory, final Compon return Optional.ofNullable((Container) InventoryEventFactory.displayContainer((net.minecraft.server.level.ServerPlayer) (Object) this, inventory, displayName)); } + @Override + public Optional openInventory(final ContainerType type) { + return this.openInventory(type, null); + } + + @Override + public Optional openInventory(final ContainerType type, final Component displayName) { + final ContainerBridge openContainer = (ContainerBridge) this.containerMenu; + if (openContainer.bridge$isInUse()) { + throw new UnsupportedOperationException("This player is currently modifying an open container. Opening a new one must be delayed!"); + } + return Optional.ofNullable((Container) InventoryEventFactory.displayContainer((net.minecraft.server.level.ServerPlayer) (Object) this, (MenuType) type, displayName)); + } + @Override public boolean closeInventory() throws IllegalArgumentException { final net.minecraft.world.inventory.AbstractContainerMenu openContainer = this.containerMenu; From 6a56cb0ab6a6870de719a3a9a3529fe8d7d64a7f Mon Sep 17 00:00:00 2001 From: MrHell228 Date: Wed, 15 May 2024 05:56:31 +0300 Subject: [PATCH 2/9] Change Container#type() to Optional --- .../api/inventory/container/ContainerMixin_Inventory_API.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mixins/java/org/spongepowered/common/mixin/inventory/api/inventory/container/ContainerMixin_Inventory_API.java b/src/mixins/java/org/spongepowered/common/mixin/inventory/api/inventory/container/ContainerMixin_Inventory_API.java index 5f169c84429..b553b02a922 100644 --- a/src/mixins/java/org/spongepowered/common/mixin/inventory/api/inventory/container/ContainerMixin_Inventory_API.java +++ b/src/mixins/java/org/spongepowered/common/mixin/inventory/api/inventory/container/ContainerMixin_Inventory_API.java @@ -121,8 +121,8 @@ public boolean isOpen() { } @Override - public ContainerType type() { - return ((ContainerType) this.menuType); + public Optional type() { + return Optional.ofNullable((ContainerType) this.menuType); } private List listeners() { From 32c3c30180507823e85992804756dbcf0e1584d3 Mon Sep 17 00:00:00 2001 From: MrHell228 Date: Wed, 15 May 2024 05:59:14 +0300 Subject: [PATCH 3/9] Add missing Override annotations --- .../custom/SpongeViewableInventoryBuilder.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/main/java/org/spongepowered/common/inventory/custom/SpongeViewableInventoryBuilder.java b/src/main/java/org/spongepowered/common/inventory/custom/SpongeViewableInventoryBuilder.java index 23b0461ecdd..3c70687523f 100644 --- a/src/main/java/org/spongepowered/common/inventory/custom/SpongeViewableInventoryBuilder.java +++ b/src/main/java/org/spongepowered/common/inventory/custom/SpongeViewableInventoryBuilder.java @@ -136,6 +136,7 @@ private Slot newDummySlot() { } // Slot definition Impl: + @Override public BuildingStep slotsAtIndizes(List source, List at) { Validate.isTrue(source.size() == at.size(), "Source and index list sizes differ"); for (int i = 0; i < at.size(); i++) { @@ -148,10 +149,12 @@ public BuildingStep slotsAtIndizes(List source, List at) { } // complex redirects - (source/index list generation) + @Override public BuildingStep slotsAtPositions(List source, List at) { return this.slotsAtIndizes(source, at.stream().map(this::posToIndex).collect(Collectors.toList())); } + @Override public DummyStep fillDummy() { Slot slot = this.newDummySlot(); List indizes = IntStream.range(0, this.info.size).boxed().filter(idx -> !this.slotDefinitions.containsKey(idx)).collect(Collectors.toList()); @@ -160,6 +163,7 @@ public DummyStep fillDummy() { return this; } + @Override public DummyStep dummySlots(int count, int offset) { Slot slot = this.newDummySlot(); List source = Stream.generate(() -> slot).limit(count).collect(Collectors.toList()); @@ -167,11 +171,13 @@ public DummyStep dummySlots(int count, int offset) { return this; } + @Override public BuildingStep slots(List source, int offset) { List indizes = IntStream.range(offset, offset + source.size()).boxed().collect(Collectors.toList()); return this.slotsAtIndizes(source, indizes); } + @Override public DummyStep dummyGrid(Vector2i size, Vector2i offset) { Slot slot = this.newDummySlot(); List source = Stream.generate(() -> slot).limit(size.x() * size.y()).collect(Collectors.toList()); @@ -179,6 +185,7 @@ public DummyStep dummyGrid(Vector2i size, Vector2i offset) { return this; } + @Override public BuildingStep grid(List source, Vector2i size, Vector2i offset) { int xMin = offset.x(); int yMin = offset.y(); @@ -196,14 +203,17 @@ public BuildingStep grid(List source, Vector2i size, Vector2i offset) { // simple redirects + @Override public DummyStep dummySlots(int count, Vector2i offset) { return this.dummySlots(count, this.posToIndex(offset)); } + @Override public BuildingStep slots(List source, Vector2i offset) { return this.slots(source, this.posToIndex(offset)); } + @Override public DummyStep dummyGrid(Vector2i size, int offset) { return this.dummyGrid(size, this.indexToPos(offset)); } @@ -225,6 +235,7 @@ public EndStep plugin(final PluginContainer plugin) { return this; } + @Override public EndStep identity(UUID uuid) { this.identity = uuid; return this; From 855edbef3f195c13e5551b4e281cc92f056ff01f Mon Sep 17 00:00:00 2001 From: MrHell228 Date: Wed, 15 May 2024 06:23:09 +0300 Subject: [PATCH 4/9] Bump API --- SpongeAPI | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SpongeAPI b/SpongeAPI index 1621e3dc3ff..7d68b8d4a3a 160000 --- a/SpongeAPI +++ b/SpongeAPI @@ -1 +1 @@ -Subproject commit 1621e3dc3ff8ed79ff8ecfab3eed86975a081059 +Subproject commit 7d68b8d4a3a789d6cc7da8697355ed193eab6dec From b197afc6898474fe150fca6be1120b14617499f0 Mon Sep 17 00:00:00 2001 From: MrHell228 Date: Sat, 14 Sep 2024 00:13:45 +0300 Subject: [PATCH 5/9] make BlockEntity delegating DataHolder --- SpongeAPI | 2 +- .../common/mixin/api/data/DataHolderMixin_API.java | 3 +-- .../level/block/entity/BlockEntityMixin_API.java | 11 ++++++++++- 3 files changed, 12 insertions(+), 4 deletions(-) diff --git a/SpongeAPI b/SpongeAPI index 7d68b8d4a3a..21270d3cc6c 160000 --- a/SpongeAPI +++ b/SpongeAPI @@ -1 +1 @@ -Subproject commit 7d68b8d4a3a789d6cc7da8697355ed193eab6dec +Subproject commit 21270d3cc6c421f8359041df6b570f685dff1df7 diff --git a/src/mixins/java/org/spongepowered/common/mixin/api/data/DataHolderMixin_API.java b/src/mixins/java/org/spongepowered/common/mixin/api/data/DataHolderMixin_API.java index 846e1d75c4a..4d2a31a5efc 100644 --- a/src/mixins/java/org/spongepowered/common/mixin/api/data/DataHolderMixin_API.java +++ b/src/mixins/java/org/spongepowered/common/mixin/api/data/DataHolderMixin_API.java @@ -26,12 +26,11 @@ import net.minecraft.world.entity.Entity; import net.minecraft.world.item.ItemStack; -import net.minecraft.world.level.block.entity.BlockEntity; import org.spongepowered.api.data.DataHolder; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.common.data.holder.SpongeMutableDataHolder; -@Mixin(value = {BlockEntity.class, Entity.class, ItemStack.class}, priority = 899) +@Mixin(value = {Entity.class, ItemStack.class}, priority = 899) public abstract class DataHolderMixin_API implements DataHolder, SpongeMutableDataHolder { } diff --git a/src/mixins/java/org/spongepowered/common/mixin/api/minecraft/world/level/block/entity/BlockEntityMixin_API.java b/src/mixins/java/org/spongepowered/common/mixin/api/minecraft/world/level/block/entity/BlockEntityMixin_API.java index 3e26a137d77..0c27815894a 100644 --- a/src/mixins/java/org/spongepowered/common/mixin/api/minecraft/world/level/block/entity/BlockEntityMixin_API.java +++ b/src/mixins/java/org/spongepowered/common/mixin/api/minecraft/world/level/block/entity/BlockEntityMixin_API.java @@ -33,6 +33,7 @@ import org.spongepowered.api.block.entity.BlockEntity; import org.spongepowered.api.block.entity.BlockEntityArchetype; import org.spongepowered.api.block.entity.BlockEntityType; +import org.spongepowered.api.data.DataHolder; import org.spongepowered.api.data.persistence.DataContainer; import org.spongepowered.api.data.persistence.DataView; import org.spongepowered.api.data.persistence.Queries; @@ -46,17 +47,20 @@ import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.common.SpongeCommon; import org.spongepowered.common.block.entity.SpongeBlockEntityArchetypeBuilder; +import org.spongepowered.common.data.holder.SpongeMutableDataHolder; import org.spongepowered.common.data.persistence.NBTTranslator; import org.spongepowered.common.util.Constants; import org.spongepowered.common.util.VecHelper; import org.spongepowered.common.world.server.SpongeLocatableBlockBuilder; import org.spongepowered.math.vector.Vector3i; +import java.util.Arrays; import java.util.HashSet; +import java.util.List; import java.util.Set; @Mixin(net.minecraft.world.level.block.entity.BlockEntity.class) -public abstract class BlockEntityMixin_API implements BlockEntity { +public abstract class BlockEntityMixin_API implements BlockEntity, SpongeMutableDataHolder { //@formatter:off @Shadow @Final private net.minecraft.world.level.block.entity.BlockEntityType type; @@ -181,4 +185,9 @@ public Set> getValues() { return new HashSet<>(); } + @Override + public List impl$delegateDataHolder() { + final BlockState state = this.block(); + return Arrays.asList(this, state, state.type()); + } } From 438bd00d8c57897a02709dccae3777cf3fcd2e67 Mon Sep 17 00:00:00 2001 From: MrHell228 Date: Sat, 14 Sep 2024 00:13:54 +0300 Subject: [PATCH 6/9] Revert "Change Container#type() to Optional" This reverts commit 6a56cb0ab6a6870de719a3a9a3529fe8d7d64a7f. --- .../api/inventory/container/ContainerMixin_Inventory_API.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/mixins/java/org/spongepowered/common/mixin/inventory/api/inventory/container/ContainerMixin_Inventory_API.java b/src/mixins/java/org/spongepowered/common/mixin/inventory/api/inventory/container/ContainerMixin_Inventory_API.java index b553b02a922..5f169c84429 100644 --- a/src/mixins/java/org/spongepowered/common/mixin/inventory/api/inventory/container/ContainerMixin_Inventory_API.java +++ b/src/mixins/java/org/spongepowered/common/mixin/inventory/api/inventory/container/ContainerMixin_Inventory_API.java @@ -121,8 +121,8 @@ public boolean isOpen() { } @Override - public Optional type() { - return Optional.ofNullable((ContainerType) this.menuType); + public ContainerType type() { + return ((ContainerType) this.menuType); } private List listeners() { From 86a40b5ef7b70fab6b5f50dbcb9974e7e280eff7 Mon Sep 17 00:00:00 2001 From: MrHell228 Date: Sat, 14 Sep 2024 00:27:31 +0300 Subject: [PATCH 7/9] Bump API --- SpongeAPI | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SpongeAPI b/SpongeAPI index 21270d3cc6c..0ba43afee9f 160000 --- a/SpongeAPI +++ b/SpongeAPI @@ -1 +1 @@ -Subproject commit 21270d3cc6c421f8359041df6b570f685dff1df7 +Subproject commit 0ba43afee9fc35e5478ee09cbd7ce8bf1ca50167 From f6c7110afd3d88e2c9f8dc9a5d79a3d1ffaa75f4 Mon Sep 17 00:00:00 2001 From: MrHell228 Date: Sat, 14 Sep 2024 00:34:04 +0300 Subject: [PATCH 8/9] Update verification-metadata.xml --- gradle/verification-metadata.xml | 34 ++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/gradle/verification-metadata.xml b/gradle/verification-metadata.xml index 7a0e8d0074b..4af7b96a5e2 100644 --- a/gradle/verification-metadata.xml +++ b/gradle/verification-metadata.xml @@ -485,6 +485,14 @@ + + + + + + + + @@ -508,6 +516,11 @@ + + + + + @@ -587,6 +600,14 @@ + + + + + + + + @@ -665,6 +686,11 @@ + + + + + @@ -2255,6 +2281,14 @@ + + + + + + + + From fd90365cd85f977d38a4fa9373090f40acd7c30e Mon Sep 17 00:00:00 2001 From: MrHell228 Date: Sat, 14 Sep 2024 01:07:26 +0300 Subject: [PATCH 9/9] actually make BlockEntity delegating DataHolder --- .../data/BlockEntity_DataHolderMixin_API.java | 44 +++++++++++++++++++ .../block/entity/BlockEntityMixin_API.java | 12 +---- src/mixins/resources/mixins.sponge.api.json | 1 + 3 files changed, 46 insertions(+), 11 deletions(-) create mode 100644 src/mixins/java/org/spongepowered/common/mixin/api/data/BlockEntity_DataHolderMixin_API.java diff --git a/src/mixins/java/org/spongepowered/common/mixin/api/data/BlockEntity_DataHolderMixin_API.java b/src/mixins/java/org/spongepowered/common/mixin/api/data/BlockEntity_DataHolderMixin_API.java new file mode 100644 index 00000000000..823b337eeee --- /dev/null +++ b/src/mixins/java/org/spongepowered/common/mixin/api/data/BlockEntity_DataHolderMixin_API.java @@ -0,0 +1,44 @@ +/* + * This file is part of Sponge, licensed under the MIT License (MIT). + * + * Copyright (c) SpongePowered + * Copyright (c) contributors + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package org.spongepowered.common.mixin.api.data; + +import org.spongepowered.api.block.BlockState; +import org.spongepowered.api.block.entity.BlockEntity; +import org.spongepowered.api.data.DataHolder; +import org.spongepowered.asm.mixin.Mixin; +import org.spongepowered.common.data.holder.SpongeMutableDataHolder; + +import java.util.Arrays; +import java.util.List; + +@Mixin(net.minecraft.world.level.block.entity.BlockEntity.class) +public abstract class BlockEntity_DataHolderMixin_API implements DataHolder, SpongeMutableDataHolder { + + @Override + public List impl$delegateDataHolder() { + final BlockState state = ((BlockEntity) this).block(); + return Arrays.asList(this, state, state.type()); + } +} diff --git a/src/mixins/java/org/spongepowered/common/mixin/api/minecraft/world/level/block/entity/BlockEntityMixin_API.java b/src/mixins/java/org/spongepowered/common/mixin/api/minecraft/world/level/block/entity/BlockEntityMixin_API.java index d38387dc629..67914d7391d 100644 --- a/src/mixins/java/org/spongepowered/common/mixin/api/minecraft/world/level/block/entity/BlockEntityMixin_API.java +++ b/src/mixins/java/org/spongepowered/common/mixin/api/minecraft/world/level/block/entity/BlockEntityMixin_API.java @@ -34,7 +34,6 @@ import org.spongepowered.api.block.entity.BlockEntity; import org.spongepowered.api.block.entity.BlockEntityArchetype; import org.spongepowered.api.block.entity.BlockEntityType; -import org.spongepowered.api.data.DataHolder; import org.spongepowered.api.data.persistence.DataContainer; import org.spongepowered.api.data.persistence.DataView; import org.spongepowered.api.data.persistence.Queries; @@ -48,20 +47,17 @@ import org.spongepowered.asm.mixin.Shadow; import org.spongepowered.common.SpongeCommon; import org.spongepowered.common.block.entity.SpongeBlockEntityArchetypeBuilder; -import org.spongepowered.common.data.holder.SpongeMutableDataHolder; import org.spongepowered.common.data.persistence.NBTTranslator; import org.spongepowered.common.util.Constants; import org.spongepowered.common.util.VecHelper; import org.spongepowered.common.world.server.SpongeLocatableBlockBuilder; import org.spongepowered.math.vector.Vector3i; -import java.util.Arrays; import java.util.HashSet; -import java.util.List; import java.util.Set; @Mixin(net.minecraft.world.level.block.entity.BlockEntity.class) -public abstract class BlockEntityMixin_API implements BlockEntity, SpongeMutableDataHolder { +public abstract class BlockEntityMixin_API implements BlockEntity { //@formatter:off @Shadow @Final private net.minecraft.world.level.block.entity.BlockEntityType type; @@ -187,10 +183,4 @@ public Set> getValues() { protected Set> api$getVanillaValues() { return new HashSet<>(); } - - @Override - public List impl$delegateDataHolder() { - final BlockState state = this.block(); - return Arrays.asList(this, state, state.type()); - } } diff --git a/src/mixins/resources/mixins.sponge.api.json b/src/mixins/resources/mixins.sponge.api.json index 621a2304140..ff00fce102a 100644 --- a/src/mixins/resources/mixins.sponge.api.json +++ b/src/mixins/resources/mixins.sponge.api.json @@ -5,6 +5,7 @@ "mixinPriority": 1100, "mixins": [ "common.entity.living.HumanEntityMixin_API", + "data.BlockEntity_DataHolderMixin_API", "data.DataHolderMixin_API", "data.persistence.DataContainerMixin_API", "entity.ai.goal.AbstractGoalMixin_API",