Search in sources :

Example 1 with CustomInventory

use of org.spongepowered.common.item.inventory.custom.CustomInventory in project SpongeCommon by SpongePowered.

the class SpongeInventoryBuilder method from.

@Override
public Inventory.Builder from(Inventory value) {
    if (value instanceof CustomInventory) {
        this.archetype = value.getArchetype();
        this.properties.putAll(((CustomInventory) value).getProperties());
        return this;
    }
    InventoryArchetype archetype = inventoryTypes.get(value.getClass());
    if (archetype == null)
        throw new UnsupportedOperationException("Currently not supported for all inventories");
    // TODO how to get Archetype from inventory?
    this.archetype = archetype;
    this.properties = new HashMap<>();
    return this;
}
Also used : InventoryArchetype(org.spongepowered.api.item.inventory.InventoryArchetype) CustomInventory(org.spongepowered.common.item.inventory.custom.CustomInventory)

Example 2 with CustomInventory

use of org.spongepowered.common.item.inventory.custom.CustomInventory in project SpongeCommon by SpongePowered.

the class SpongeCommonEventFactory method displayContainer.

@Nullable
public static Container displayContainer(EntityPlayerMP player, Inventory inventory) {
    net.minecraft.inventory.Container previousContainer = player.openContainer;
    net.minecraft.inventory.Container container;
    if (inventory instanceof CustomInventory) {
        if (!checkValidVanillaCustomInventory(((CustomInventory) inventory))) {
            // Invalid size for vanilla inventory ; This is to
            return null;
        // prevent crashing the client with invalid data
        }
    }
    if (inventory instanceof IInteractionObject) {
        final String guiId = ((IInteractionObject) inventory).getGuiID();
        switch(guiId) {
            case "EntityHorse":
                if (inventory instanceof CarriedInventory) {
                    CarriedInventory<?> cinventory = (CarriedInventory<?>) inventory;
                    if (cinventory.getCarrier().isPresent() && cinventory.getCarrier().get() instanceof AbstractHorse) {
                        player.openGuiHorseInventory(((AbstractHorse) cinventory.getCarrier().get()), (IInventory) inventory);
                    }
                }
                break;
            case "minecraft:chest":
                player.displayGUIChest((IInventory) inventory);
                break;
            case "minecraft:crafting_table":
            case "minecraft:anvil":
            case "minecraft:enchanting_table":
                player.displayGui((IInteractionObject) inventory);
                break;
            default:
                player.displayGUIChest((IInventory) inventory);
                break;
        }
    } else if (inventory instanceof IInventory) {
        player.displayGUIChest(((IInventory) inventory));
    } else {
        return null;
    }
    container = player.openContainer;
    if (previousContainer == container) {
        return null;
    }
    if (!callInteractInventoryOpenEvent(player)) {
        return null;
    }
    if (container instanceof IMixinContainer) {
        // This overwrites the normal container behaviour and allows viewing
        // inventories that are more than 8 blocks away
        // This currently actually only works for the Containers mixed into
        // by MixinContainerCanInteract ; but throws no errors for other
        // containers
        // Allow viewing inventory; except when dead
        ((IMixinContainer) container).setCanInteractWith(p -> !p.isDead);
    }
    return container;
}
Also used : IInventory(net.minecraft.inventory.IInventory) IMixinContainer(org.spongepowered.common.interfaces.IMixinContainer) AbstractHorse(net.minecraft.entity.passive.AbstractHorse) IInteractionObject(net.minecraft.world.IInteractionObject) CarriedInventory(org.spongepowered.api.item.inventory.type.CarriedInventory) CustomInventory(org.spongepowered.common.item.inventory.custom.CustomInventory) Container(net.minecraft.inventory.Container) Nullable(javax.annotation.Nullable)

Aggregations

CustomInventory (org.spongepowered.common.item.inventory.custom.CustomInventory)2 Nullable (javax.annotation.Nullable)1 AbstractHorse (net.minecraft.entity.passive.AbstractHorse)1 Container (net.minecraft.inventory.Container)1 IInventory (net.minecraft.inventory.IInventory)1 IInteractionObject (net.minecraft.world.IInteractionObject)1 InventoryArchetype (org.spongepowered.api.item.inventory.InventoryArchetype)1 CarriedInventory (org.spongepowered.api.item.inventory.type.CarriedInventory)1 IMixinContainer (org.spongepowered.common.interfaces.IMixinContainer)1