Search in sources :

Example 1 with AbstractStoreItem

use of com.almuradev.almura.feature.store.basic.listing.AbstractStoreItem in project Almura by AlmuraDev.

the class ClientboundListItemsResponsePacket method readFrom.

@Override
public void readFrom(final ChannelBuf buf) {
    this.id = buf.readString();
    this.type = StoreItemSegmentType.valueOf(buf.readString());
    final int count = buf.readInteger();
    if (count > 0) {
        if (type == StoreItemSegmentType.SELLING) {
            this.storeItems = new ArrayList<SellingItem>();
        } else {
            this.storeItems = new ArrayList<BuyingItem>();
        }
        for (int i = 0; i < count; i++) {
            final int record = buf.readInteger();
            final Instant created;
            try {
                created = SerializationUtil.bytesToObject(buf.readBytes(buf.readInteger()));
            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
                continue;
            }
            final ResourceLocation location = new ResourceLocation(buf.readString(), buf.readString());
            final Item item = ForgeRegistries.ITEMS.getValue(location);
            if (item == null) {
                new IOException("Unknown item id '" + location.toString() + "' when receiving item! . Skipping...").printStackTrace();
                continue;
            }
            final int quantity = buf.readInteger();
            final int metadata = buf.readInteger();
            final BigDecimal price = ByteBufUtil.readBigDecimal((ByteBuf) buf);
            final int index = buf.readInteger();
            final int compoundDataLength = buf.readInteger();
            NBTTagCompound compound = null;
            if (compoundDataLength > 0) {
                try {
                    compound = SerializationUtil.compoundFromBytes(buf.readBytes(compoundDataLength));
                } catch (IOException e) {
                    e.printStackTrace();
                    continue;
                }
            }
            final Constructor<? extends AbstractStoreItem> constructor;
            try {
                constructor = this.type.getItemClass().getConstructor(Integer.TYPE, Instant.class, Item.class, Integer.TYPE, Integer.TYPE, BigDecimal.class, Integer.TYPE, NBTTagCompound.class);
                final AbstractStoreItem storeItem = constructor.newInstance(record, created, item, quantity, metadata, price, index, compound);
                if (this.type == StoreItemSegmentType.SELLING) {
                    ((List<SellingItem>) this.storeItems).add((SellingItem) storeItem);
                } else {
                    ((List<BuyingItem>) this.storeItems).add((BuyingItem) storeItem);
                }
            } catch (NoSuchMethodException | IllegalAccessException | InstantiationException | InvocationTargetException e) {
                e.printStackTrace();
            }
        }
    }
}
Also used : AbstractStoreItem(com.almuradev.almura.feature.store.basic.listing.AbstractStoreItem) Instant(java.time.Instant) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) IOException(java.io.IOException) BasicSellingItem(com.almuradev.almura.feature.store.basic.listing.BasicSellingItem) SellingItem(com.almuradev.almura.feature.store.listing.SellingItem) BigDecimal(java.math.BigDecimal) InvocationTargetException(java.lang.reflect.InvocationTargetException) AbstractStoreItem(com.almuradev.almura.feature.store.basic.listing.AbstractStoreItem) Item(net.minecraft.item.Item) BasicSellingItem(com.almuradev.almura.feature.store.basic.listing.BasicSellingItem) BuyingItem(com.almuradev.almura.feature.store.listing.BuyingItem) SellingItem(com.almuradev.almura.feature.store.listing.SellingItem) StoreItem(com.almuradev.almura.feature.store.listing.StoreItem) ResourceLocation(net.minecraft.util.ResourceLocation) BuyingItem(com.almuradev.almura.feature.store.listing.BuyingItem) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

AbstractStoreItem (com.almuradev.almura.feature.store.basic.listing.AbstractStoreItem)1 BasicSellingItem (com.almuradev.almura.feature.store.basic.listing.BasicSellingItem)1 BuyingItem (com.almuradev.almura.feature.store.listing.BuyingItem)1 SellingItem (com.almuradev.almura.feature.store.listing.SellingItem)1 StoreItem (com.almuradev.almura.feature.store.listing.StoreItem)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 BigDecimal (java.math.BigDecimal)1 Instant (java.time.Instant)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Item (net.minecraft.item.Item)1 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)1 ResourceLocation (net.minecraft.util.ResourceLocation)1