Skip to content

Commit

Permalink
enables retrieving Properties from CustomInventories
Browse files Browse the repository at this point in the history
  • Loading branch information
Faithcaio committed Feb 25, 2017
1 parent 162ab1f commit 2b82d9a
Showing 1 changed file with 15 additions and 3 deletions.
Expand Up @@ -35,12 +35,14 @@
import org.spongepowered.api.text.translation.Translation;
import org.spongepowered.common.item.inventory.InventoryIterator;
import org.spongepowered.common.item.inventory.adapter.InventoryAdapter;
import org.spongepowered.common.item.inventory.custom.CustomInventory;
import org.spongepowered.common.item.inventory.query.Query;

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

public interface MinecraftInventoryAdapter extends InventoryAdapter<IInventory, net.minecraft.item.ItemStack> {

Expand Down Expand Up @@ -137,7 +139,11 @@ 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
if (this instanceof CustomInventory) {
return ((CustomInventory) this).getProperties().values().stream().filter(p -> property.equals(p.getClass()))
.map(p -> property.cast(p)).collect(Collectors.toList());
}
return Collections.emptyList(); // TODO properties of top level inventory?
}
return this.parent().getProperties(this, property);
}
Expand All @@ -146,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 @@ -155,7 +161,13 @@ 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
if (this instanceof CustomInventory) {
InventoryProperty forKey = ((CustomInventory) this).getProperties().get(key);
if (property.equals(forKey.getClass())) {
return Optional.of((T) forKey);
}
}
return Optional.empty(); // TODO properties of top level inventory?
}
return this.parent().getProperty(this, property, key);
}
Expand Down

0 comments on commit 2b82d9a

Please sign in to comment.