2015-07-30 16:25:16 +02:00
|
|
|
package com.plotsquared.sponge.util;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
2015-10-07 08:33:33 +02:00
|
|
|
import org.spongepowered.api.entity.living.player.Player;
|
2015-07-30 16:25:16 +02:00
|
|
|
import org.spongepowered.api.item.ItemType;
|
|
|
|
import org.spongepowered.api.item.ItemTypes;
|
|
|
|
import org.spongepowered.api.item.inventory.Carrier;
|
|
|
|
import org.spongepowered.api.item.inventory.ItemStack;
|
|
|
|
import org.spongepowered.api.item.inventory.custom.CustomInventory;
|
|
|
|
import org.spongepowered.api.item.inventory.property.SlotIndex;
|
|
|
|
import org.spongepowered.api.item.inventory.type.CarriedInventory;
|
|
|
|
|
|
|
|
import com.intellectualcrafters.plot.object.PlotInventory;
|
|
|
|
import com.intellectualcrafters.plot.object.PlotItemStack;
|
|
|
|
import com.intellectualcrafters.plot.object.PlotPlayer;
|
|
|
|
import com.intellectualcrafters.plot.util.InventoryUtil;
|
|
|
|
import com.plotsquared.sponge.SpongeMain;
|
2015-07-30 19:24:01 +02:00
|
|
|
import com.plotsquared.sponge.object.SpongePlayer;
|
2015-07-30 16:25:16 +02:00
|
|
|
|
2015-09-13 06:04:31 +02:00
|
|
|
public class SpongeInventoryUtil extends InventoryUtil {
|
2015-11-26 04:40:22 +01:00
|
|
|
|
|
|
|
public ItemStack.Builder builder;
|
|
|
|
|
2015-09-13 06:04:31 +02:00
|
|
|
public SpongeInventoryUtil() {
|
2015-11-26 04:40:22 +01:00
|
|
|
builder = SpongeMain.THIS.getGame().getRegistry().createBuilder(ItemStack.Builder.class);
|
2015-07-30 16:25:16 +02:00
|
|
|
}
|
2015-11-26 04:40:22 +01:00
|
|
|
|
2015-07-30 16:25:16 +02:00
|
|
|
@Override
|
2015-09-13 06:04:31 +02:00
|
|
|
public void open(final PlotInventory inv) {
|
2015-07-30 16:25:16 +02:00
|
|
|
// TODO Auto-generated method stub
|
2015-09-11 12:09:22 +02:00
|
|
|
final SpongePlayer sp = (SpongePlayer) inv.player;
|
|
|
|
final Player player = sp.player;
|
2015-11-26 04:40:22 +01:00
|
|
|
|
|
|
|
final CustomInventory inventory = SpongeMain.THIS.getGame().getRegistry().createBuilder(CustomInventory.Builder.class).name(SpongeMain.THIS.getTranslation(inv.getTitle()).getTranslation()).size(inv.size).build();
|
2015-09-11 12:09:22 +02:00
|
|
|
final PlotItemStack[] items = inv.getItems();
|
2015-09-13 06:04:31 +02:00
|
|
|
for (int i = 0; i < (inv.size * 9); i++) {
|
2015-09-11 12:09:22 +02:00
|
|
|
final PlotItemStack item = items[i];
|
2015-09-13 06:04:31 +02:00
|
|
|
if (item != null) {
|
2015-07-30 16:25:16 +02:00
|
|
|
inventory.set(new SlotIndex(i), getItem(item));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
inv.player.setMeta("inventory", inv);
|
|
|
|
player.openInventory(inventory);
|
|
|
|
}
|
2015-11-26 04:40:22 +01:00
|
|
|
|
2015-09-13 06:04:31 +02:00
|
|
|
public ItemStack getItem(final PlotItemStack item) {
|
2015-07-30 16:25:16 +02:00
|
|
|
// FIXME item type, item data, item name, item lore
|
|
|
|
return builder.itemType(ItemTypes.SPONGE).quantity(item.amount).build();
|
|
|
|
}
|
2015-11-26 04:40:22 +01:00
|
|
|
|
2015-07-30 16:25:16 +02:00
|
|
|
@Override
|
2015-09-13 06:04:31 +02:00
|
|
|
public void close(final PlotInventory inv) {
|
|
|
|
if (!inv.isOpen()) {
|
|
|
|
return;
|
|
|
|
}
|
2015-07-30 16:25:16 +02:00
|
|
|
inv.player.deleteMeta("inventory");
|
2015-09-11 12:09:22 +02:00
|
|
|
final SpongePlayer sp = (SpongePlayer) inv.player;
|
2015-07-30 16:25:16 +02:00
|
|
|
sp.player.closeInventory();
|
|
|
|
}
|
2015-11-26 04:40:22 +01:00
|
|
|
|
2015-07-30 16:25:16 +02:00
|
|
|
@Override
|
2015-09-13 06:04:31 +02:00
|
|
|
public void setItem(final PlotInventory inv, final int index, final PlotItemStack item) {
|
|
|
|
if (!inv.isOpen()) {
|
|
|
|
return;
|
|
|
|
}
|
2015-09-11 12:09:22 +02:00
|
|
|
final SpongePlayer sp = (SpongePlayer) inv.player;
|
|
|
|
final Player player = sp.player;
|
|
|
|
player.getOpenInventory().get();
|
2015-07-30 16:25:16 +02:00
|
|
|
throw new UnsupportedOperationException("NOT IMPLEMENTED YET");
|
2015-11-26 04:40:22 +01:00
|
|
|
|
2015-07-30 16:25:16 +02:00
|
|
|
}
|
2015-11-26 04:40:22 +01:00
|
|
|
|
2015-09-13 06:04:31 +02:00
|
|
|
public PlotItemStack getItem(final ItemStack item) {
|
|
|
|
if (item == null) {
|
|
|
|
return null;
|
|
|
|
}
|
2015-09-11 12:09:22 +02:00
|
|
|
final ItemType type = item.getItem();
|
|
|
|
final String id = type.getId();
|
|
|
|
final int amount = item.getQuantity();
|
2015-07-30 16:25:16 +02:00
|
|
|
// TODO name / lore
|
|
|
|
return new PlotItemStack(id, amount, null);
|
|
|
|
}
|
2015-11-26 04:40:22 +01:00
|
|
|
|
2015-07-30 16:25:16 +02:00
|
|
|
@Override
|
2015-09-13 06:04:31 +02:00
|
|
|
public PlotItemStack[] getItems(final PlotPlayer player) {
|
2015-09-11 12:09:22 +02:00
|
|
|
final SpongePlayer sp = (SpongePlayer) player;
|
|
|
|
sp.player.getInventory();
|
|
|
|
new ArrayList<PlotItemStack>();
|
2015-11-26 04:40:22 +01:00
|
|
|
|
2015-07-30 16:25:16 +02:00
|
|
|
throw new UnsupportedOperationException("NOT IMPLEMENTED YET");
|
2015-11-26 04:40:22 +01:00
|
|
|
|
2015-09-11 12:09:22 +02:00
|
|
|
// return list.toArray();
|
2015-07-30 16:25:16 +02:00
|
|
|
}
|
2015-11-26 04:40:22 +01:00
|
|
|
|
2015-07-30 16:25:16 +02:00
|
|
|
@Override
|
2015-09-13 06:04:31 +02:00
|
|
|
public boolean isOpen(final PlotInventory inv) {
|
|
|
|
if (!inv.isOpen()) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-09-11 12:09:22 +02:00
|
|
|
final SpongePlayer sp = (SpongePlayer) inv.player;
|
|
|
|
final Player player = sp.player;
|
2015-09-13 06:04:31 +02:00
|
|
|
if (player.isViewingInventory()) {
|
2015-09-11 12:09:22 +02:00
|
|
|
final CarriedInventory<? extends Carrier> inventory = player.getInventory();
|
2015-11-15 03:30:52 +01:00
|
|
|
return inv.getTitle().equals(inventory.getName().getId()); // TODO getId()
|
2015-07-30 16:25:16 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2015-11-26 04:40:22 +01:00
|
|
|
|
2015-07-30 16:25:16 +02:00
|
|
|
}
|