Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open container from type & some fixes #2504

Open
wants to merge 8 commits into
base: api-11
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public interface Chest extends NameableCarrierBlockEntity {
* @return The attachment type of this chest.
*/
default Value.Mutable<ChestAttachmentType> attachmentType() {
return this.requireValue(Keys.CHEST_ATTACHMENT_TYPE).asMutable();
return this.block().requireValue(Keys.CHEST_ATTACHMENT_TYPE).asMutable();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this is subtle, but why?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because Keys.CHEST_ATTACHMENT_TYPE is registered for BlockState, not BlockEntity. And currently this method simply throw an exception

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that we have code (SpongeDataHolder#impl$delegateDataHolder) that could make this work on BlockEntity too.
e.g.
https://github.com/SpongePowered/Sponge/blob/api-12/src/main/java/org/spongepowered/common/world/server/SpongeServerLocation.java#L280

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, that's cool. I guess it would be better to use this method then

Copy link
Contributor Author

@MrHell228 MrHell228 Sep 13, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverted change in Chest file, in impl made BlockEntity delegate DataHolder to its #block() and #block().type().
Also reverted Container#type() change since it's breaking (and in api-12 Faithcaio fixed this).

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.spongepowered.api.entity.living.player.tab.TabList;
import org.spongepowered.api.event.Cause;
import org.spongepowered.api.item.inventory.Container;
import org.spongepowered.api.item.inventory.ContainerType;
import org.spongepowered.api.item.inventory.Inventory;
import org.spongepowered.api.network.ServerPlayerConnection;
import org.spongepowered.api.resourcepack.ResourcePack;
Expand All @@ -59,6 +60,7 @@
import java.time.Instant;
import java.util.Collection;
import java.util.Optional;
import java.util.function.Supplier;

public interface ServerPlayer extends Player, Subject {

Expand Down Expand Up @@ -117,6 +119,31 @@ default boolean isViewingInventory() {
*/
Optional<Container> openInventory(Inventory inventory, Component displayName);

/**
* Constructs Container from a given type and opens it for player to view.
*
* @param type The container type to construct and view
* @return The opened Container if it was constructed and opened, otherwise {@link Optional#empty()}
*/
Optional<Container> openInventory(ContainerType type);

default Optional<Container> openInventory(Supplier<ContainerType> type) {
return this.openInventory(type.get());
}

/**
* Constructs Container from a given type and opens it for player to view with a custom displayName.
*
* @param type The container type to construct and view
* @param displayName The display name to set
* @return The opened Container if it was constructed and opened, otherwise {@link Optional#empty()}
*/
Optional<Container> openInventory(ContainerType type, Component displayName);

default Optional<Container> openInventory(Supplier<ContainerType> type, Component displayName) {
return this.openInventory(type.get(), displayName);
}

/**
* Closes the currently viewed entity of this player, if it is currently
* viewing one.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ public interface Container extends Inventory {
/**
* Returns the {@link ContainerType} of this container.
*
* @return the ContainerType of this container.
* @return the ContainerType of this container if present.
*/
ContainerType type();
Optional<ContainerType> type();

/**
* Returns the {@link InventoryMenu} if this container has been opened by one.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ public final class ContainerTypes {
*/
public static final DefaultedRegistryReference<ContainerType> GENERIC_3X3 = ContainerTypes.key(ResourceKey.minecraft("generic_3x3"));

public static final DefaultedRegistryReference<ContainerType> CRAFTER_3X3 = ContainerTypes.key(ResourceKey.minecraft("crafter_3x3"));

public static final DefaultedRegistryReference<ContainerType> GENERIC_9X1 = ContainerTypes.key(ResourceKey.minecraft("generic_9x1"));

public static final DefaultedRegistryReference<ContainerType> GENERIC_9X2 = ContainerTypes.key(ResourceKey.minecraft("generic_9x2"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.spongepowered.api.item.ItemType;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.item.inventory.ItemStackSnapshot;
import org.spongepowered.api.registry.DefaultedRegistryReference;

import java.util.List;
import java.util.function.Predicate;
Expand Down Expand Up @@ -119,7 +118,7 @@ static Ingredient of(ItemStackSnapshot @Nullable ... items) {
* @return The new ingredient
*/
@SafeVarargs
static Ingredient of(DefaultedRegistryReference<? extends ItemType> @Nullable ... itemTypes) {
static Ingredient of(Supplier<? extends ItemType> @Nullable ... itemTypes) {
if (itemTypes == null || itemTypes.length == 0) {
return Ingredient.empty();
}
Expand Down