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 #4010

Open
wants to merge 10 commits into
base: api-11
Choose a base branch
from
34 changes: 34 additions & 0 deletions gradle/verification-metadata.xml
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,14 @@
<sha256 value="d2b115634f5c085db4b9c9ffc2658e89e231fdbfbe2242121a1cd95d4d948dd7" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="com.google.code.gson" name="gson" version="2.11.0">
<artifact name="gson-2.11.0.jar">
<sha256 value="57928d6e5a6edeb2abd3770a8f95ba44dce45f3b23b7a9dc2b309c581552a78b" origin="Generated by Gradle"/>
</artifact>
<artifact name="gson-2.11.0.pom">
<sha256 value="c0e547bea998888e6e25c5886a90e762272bc88b5275343dd2c05ded6ca2e360" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="com.google.code.gson" name="gson" version="2.2.4">
<artifact name="gson-2.2.4.pom">
<sha256 value="ae984d5d19894ce6dc4689866eaa12f8fc31409113e60ee3b44853b8ac4fd380" origin="Generated by Gradle"/>
Expand All @@ -508,6 +516,11 @@
<sha256 value="4248e0882426c615182385d6086c3ef3262e769957189e29306280b85482b833" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="com.google.code.gson" name="gson-parent" version="2.11.0">
<artifact name="gson-parent-2.11.0.pom">
<sha256 value="8acb1f3b72a6f026916ac0735bad9aab0293d527edb7b365327def13a9367b7a" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="com.google.code.gson" name="gson-parent" version="2.8.0">
<artifact name="gson-parent-2.8.0.pom">
<sha256 value="0f1d8369b1e9f1bc4b467c8dcfdd8b59733aad5b5c8419c3a59b2f9abaf04cd2" origin="Generated by Gradle"/>
Expand Down Expand Up @@ -587,6 +600,14 @@
<sha256 value="aea7e991e2dfdcb47f5fbd508587535f7802bcb9e2fc2d4ebb50be41b684da9f" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="com.google.errorprone" name="error_prone_annotations" version="2.27.0">
<artifact name="error_prone_annotations-2.27.0.jar">
<sha256 value="24c923372c58e35d0b9f16a028929bb9aedc77521867c274f2bd0735df5ba1f5" origin="Generated by Gradle"/>
</artifact>
<artifact name="error_prone_annotations-2.27.0.pom">
<sha256 value="4ca5a35d61235e16549ac346d1e34551cdf0fe27e84aa57e03cbeb255ea4e5da" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="com.google.errorprone" name="error_prone_annotations" version="2.3.4">
<artifact name="error_prone_annotations-2.3.4.jar">
<sha256 value="baf7d6ea97ce606c53e11b6854ba5f2ce7ef5c24dddf0afa18d1260bd25b002c" origin="Generated by Gradle"/>
Expand Down Expand Up @@ -665,6 +686,11 @@
<sha256 value="4a6ad00d31b0a5adcd9a4f605065d5b445fae4a04cbf827e5d1ac1077e2f814d" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="com.google.errorprone" name="error_prone_parent" version="2.27.0">
<artifact name="error_prone_parent-2.27.0.pom">
<sha256 value="fa81829d049559df6927f9c9a6fd6bbd09f8b50e6d4736ae72c8300b6c3d7654" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="com.google.errorprone" name="error_prone_parent" version="2.3.4">
<artifact name="error_prone_parent-2.3.4.pom">
<sha256 value="40495b437a60d2398f0fdfc054b89d9c394a82347a274a0721c2e950a4302186" origin="Generated by Gradle"/>
Expand Down Expand Up @@ -2255,6 +2281,14 @@
<sha256 value="b4bd71c336e4f8265aac276c6519c6e1793c5ce55f583c26896a7bca48658f2a" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="net.kyori" name="mammoth" version="1.4.0">
<artifact name="mammoth-1.4.0.jar">
<sha256 value="b04abb96b666f763d2d7467b0308f23fe8d8b753f8ad89143814cb5ea9901eb3" origin="Generated by Gradle"/>
</artifact>
<artifact name="mammoth-1.4.0.module">
<sha256 value="c204766a8f14ce4e1a124a5302a9839101fcda118de2aba6b76cc62865264aae" origin="Generated by Gradle"/>
</artifact>
</component>
<component group="net.kyori" name="option" version="1.0.0">
<artifact name="option-1.0.0-javadoc.jar">
<sha256 value="97772ea0fa34365c2a44590a18c80a580874d9cdac44dba012f9e73304ab3c8a" origin="Generated by Gradle"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<ViewableInventory> viewable = inventory.asViewable();
if (viewable.isPresent()) {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ private Slot newDummySlot() {
}

// Slot definition Impl:
@Override
public BuildingStep slotsAtIndizes(List<Slot> source, List<Integer> at) {
Validate.isTrue(source.size() == at.size(), "Source and index list sizes differ");
for (int i = 0; i < at.size(); i++) {
Expand All @@ -148,10 +149,12 @@ public BuildingStep slotsAtIndizes(List<Slot> source, List<Integer> at) {
}

// complex redirects - (source/index list generation)
@Override
public BuildingStep slotsAtPositions(List<Slot> source, List<Vector2i> at) {
return this.slotsAtIndizes(source, at.stream().map(this::posToIndex).collect(Collectors.toList()));
}

@Override
public DummyStep fillDummy() {
Slot slot = this.newDummySlot();
List<Integer> indizes = IntStream.range(0, this.info.size).boxed().filter(idx -> !this.slotDefinitions.containsKey(idx)).collect(Collectors.toList());
Expand All @@ -160,25 +163,29 @@ public DummyStep fillDummy() {
return this;
}

@Override
public DummyStep dummySlots(int count, int offset) {
Slot slot = this.newDummySlot();
List<Slot> source = Stream.generate(() -> slot).limit(count).collect(Collectors.toList());
this.slots(source, offset);
return this;
}

@Override
public BuildingStep slots(List<Slot> source, int offset) {
List<Integer> 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<Slot> source = Stream.generate(() -> slot).limit(size.x() * size.y()).collect(Collectors.toList());
this.grid(source, size, offset);
return this;
}

@Override
public BuildingStep grid(List<Slot> source, Vector2i size, Vector2i offset) {
int xMin = offset.x();
int yMin = offset.y();
Expand All @@ -196,14 +203,17 @@ public BuildingStep grid(List<Slot> 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<Slot> 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));
}
Expand All @@ -225,6 +235,7 @@ public EndStep plugin(final PluginContainer plugin) {
return this;
}

@Override
public EndStep identity(UUID uuid) {
this.identity = uuid;
return this;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* This file is part of Sponge, licensed under the MIT License (MIT).
*
* Copyright (c) SpongePowered <https://www.spongepowered.org>
* 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<DataHolder> impl$delegateDataHolder() {
final BlockState state = ((BlockEntity) this).block();
return Arrays.asList(this, state, state.type());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

}
Original file line number Diff line number Diff line change
Expand Up @@ -183,5 +183,4 @@ public Set<Value.Immutable<?>> getValues() {
protected Set<Value.Immutable<?>> api$getVanillaValues() {
return new HashSet<>();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -64,6 +66,20 @@ public Optional<Container> openInventory(final Inventory inventory, final Compon
return Optional.ofNullable((Container) InventoryEventFactory.displayContainer((net.minecraft.server.level.ServerPlayer) (Object) this, inventory, displayName));
}

@Override
public Optional<Container> openInventory(final ContainerType type) {
return this.openInventory(type, null);
}

@Override
public Optional<Container> 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;
Expand Down
1 change: 1 addition & 0 deletions src/mixins/resources/mixins.sponge.api.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down