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

Inventory properties #995

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -97,7 +97,7 @@ public Inventory build(Object plugin) {
@Override
public Inventory.Builder from(Inventory value) {
if (value instanceof CustomInventory) {
this.archetype = ((CustomInventory) value).getArchetype();
this.archetype = value.getArchetype();
this.properties.putAll(((CustomInventory) value).getProperties());
return this;
}
Expand Down
Expand Up @@ -26,6 +26,7 @@

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.collect.Streams;
import net.minecraft.inventory.IInventory;
import org.spongepowered.api.item.ItemType;
import org.spongepowered.api.item.ItemTypes;
Expand All @@ -35,12 +36,15 @@
import org.spongepowered.api.item.inventory.InventoryProperty;
import org.spongepowered.api.item.inventory.ItemStack;
import org.spongepowered.api.item.inventory.Slot;
import org.spongepowered.api.item.inventory.property.InventoryTitle;
import org.spongepowered.api.item.inventory.transaction.InventoryTransactionResult;
import org.spongepowered.api.item.inventory.transaction.InventoryTransactionResult.Type;
import org.spongepowered.api.plugin.PluginContainer;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.translation.Translation;
import org.spongepowered.common.item.inventory.EmptyInventoryImpl;
import org.spongepowered.common.item.inventory.adapter.InventoryAdapter;
import org.spongepowered.common.item.inventory.custom.CustomInventory;
import org.spongepowered.common.item.inventory.lens.Fabric;
import org.spongepowered.common.item.inventory.lens.Lens;
import org.spongepowered.common.item.inventory.lens.LensProvider;
Expand All @@ -51,6 +55,7 @@
import org.spongepowered.common.item.inventory.lens.slots.SlotLens;
import org.spongepowered.common.item.inventory.observer.InventoryEventArgs;
import org.spongepowered.common.item.inventory.util.ItemStackUtil;
import org.spongepowered.common.text.SpongeTexts;
import org.spongepowered.common.text.translation.SpongeTranslation;

import java.util.ArrayList;
Expand Down Expand Up @@ -283,6 +288,56 @@ public static int getCapacity(Fabric<IInventory> inv, Lens<IInventory, net.minec
return Collections.emptyList();
}

static <T extends InventoryProperty<?, ?>> Collection<T> getRootProperties(InventoryAdapter<IInventory, net.minecraft.item.ItemStack> adapter, Class<T> property) {
adapter = inventoryRoot(adapter);
if (adapter instanceof CustomInventory) {
return ((CustomInventory) adapter).getProperties().values().stream().filter(p -> property.equals(p.getClass()))
.map(property::cast).collect(Collectors.toList());
}
return Streams.stream(findRootProperty(adapter, property)).collect(Collectors.toList());
}

static <T extends InventoryProperty<?, ?>> Optional<T> getRootProperty(InventoryAdapter<IInventory, net.minecraft.item.ItemStack> adapter, Class<T> property, Object key) {
adapter = inventoryRoot(adapter);
if (adapter instanceof CustomInventory) {
InventoryProperty forKey = ((CustomInventory) adapter).getProperties().get(key);
if (forKey != null && property.equals(forKey.getClass())) {
return Optional.of((T) forKey);
}
}
return findRootProperty(adapter, property);
}

private static <T extends InventoryProperty<?, ?>> Optional<T> findRootProperty(InventoryAdapter<IInventory, net.minecraft.item.ItemStack> adapter, Class<T> property) {
if (property == InventoryTitle.class) {
if (adapter instanceof Container) {
IInventory inv = adapter.getInventory().allInventories().iterator().next();
Text text = SpongeTexts.toText(inv.getDisplayName());
return ((Optional<T>) Optional.of(InventoryTitle.of(text)));
}
if (adapter instanceof IInventory) {
Text text = SpongeTexts.toText(((IInventory) adapter).getDisplayName());
return ((Optional<T>) Optional.of(InventoryTitle.of(text)));
}
}
// TODO more properties of top level inventory
return Optional.empty();
}

private static InventoryAdapter<IInventory, net.minecraft.item.ItemStack> inventoryRoot(InventoryAdapter<IInventory, net.minecraft.item.ItemStack> adapter) {
// Get Root Inventory
adapter = ((InventoryAdapter) adapter.root());
if (adapter instanceof Container) {
// If Root is a Container get the viewed inventory
IInventory first = adapter.getInventory().allInventories().iterator().next();
if (first instanceof CustomInventory) {
// if viewed inventory is a custom inventory get it instead
adapter = ((InventoryAdapter) first);
}
}
return adapter;
}

public static boolean contains(InventoryAdapter<IInventory, net.minecraft.item.ItemStack> adapter, ItemStack stack) {
return Logic.contains(adapter.getInventory(), adapter.getRootLens(), stack, stack.getQuantity());
}
Expand Down
Expand Up @@ -39,7 +39,6 @@
import org.spongepowered.common.item.inventory.query.Query;

import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.Optional;

Expand Down Expand Up @@ -143,7 +142,7 @@ default void setMaxStackSize(int size) {
@Override
default <T extends InventoryProperty<?, ?>> Collection<T> getProperties(Class<T> property) {
if (this.parent() == this) {
return Collections.emptyList(); // TODO top level inventory properties
return Adapter.Logic.getRootProperties(this, property);
}
return this.parent().getProperties(this, property);
}
Expand All @@ -153,7 +152,7 @@ default void setMaxStackSize(int size) {
default <T extends InventoryProperty<?, ?>> Optional<T> getProperty(Inventory child, Class<T> property, Object key) {
for (InventoryProperty<?, ?> prop : Adapter.Logic.getProperties(this, child, property)) {
if (key.equals(prop.getKey())) {
return Optional.of(((T) prop));
return Optional.of((T)prop);
}
}
return Optional.empty();
Expand All @@ -162,7 +161,7 @@ default void setMaxStackSize(int size) {
@Override
default <T extends InventoryProperty<?, ?>> Optional<T> getProperty(Class<T> property, Object key) {
if (this.parent() == this) {
return Optional.empty(); // TODO top level inventory properties
return Adapter.Logic.getRootProperty(this, property, key);
}
return this.parent().getProperty(this, property, key);
}
Expand Down
Expand Up @@ -73,10 +73,8 @@ public LensHandle() {
public LensHandle(Lens<TInventory, TStack> lens, InventoryProperty<?, ?>... properties) {
this.lens = lens;
if (properties != null && properties.length > 0) {
this.properties = new ArrayList<InventoryProperty<?, ?>>();
for (InventoryProperty<?, ?> property : properties) {
this.properties.add(property);
}
this.properties = new ArrayList<>();
Collections.addAll(this.properties, properties);
}
}

Expand All @@ -90,13 +88,13 @@ public LensHandle(Lens<TInventory, TStack> lens, InventoryProperty<?, ?>... prop
public LensHandle(Lens<TInventory, TStack> lens, Collection<InventoryProperty<?, ?>> properties) {
this.lens = lens;
if (properties != null && properties.size() > 0) {
this.properties = new ArrayList<InventoryProperty<?, ?>>(properties);
this.properties = new ArrayList<>(properties);
}
}

public Collection<InventoryProperty<?, ?>> getProperties() {
if (this.properties == null) {
return Collections.<InventoryProperty<?, ?>>emptyList();
return Collections.emptyList();
}
return Collections.unmodifiableCollection(this.properties);
}
Expand Down